19fe1ebebSAneesh Kumar K.V /*
29fe1ebebSAneesh Kumar K.V * Virtio PCI Bindings
39fe1ebebSAneesh Kumar K.V *
49fe1ebebSAneesh Kumar K.V * Copyright IBM, Corp. 2007
59fe1ebebSAneesh Kumar K.V * Copyright (c) 2009 CodeSourcery
69fe1ebebSAneesh Kumar K.V *
79fe1ebebSAneesh Kumar K.V * Authors:
89fe1ebebSAneesh Kumar K.V * Anthony Liguori <aliguori@us.ibm.com>
99fe1ebebSAneesh Kumar K.V * Paul Brook <paul@codesourcery.com>
109fe1ebebSAneesh Kumar K.V *
119fe1ebebSAneesh Kumar K.V * This work is licensed under the terms of the GNU GPL, version 2. See
129fe1ebebSAneesh Kumar K.V * the COPYING file in the top-level directory.
139fe1ebebSAneesh Kumar K.V */
149fe1ebebSAneesh Kumar K.V
159fe1ebebSAneesh Kumar K.V #ifndef QEMU_VIRTIO_PCI_H
169fe1ebebSAneesh Kumar K.V #define QEMU_VIRTIO_PCI_H
179fe1ebebSAneesh Kumar K.V
18cf7c3f0cSKONRAD Frederic #include "hw/pci/msi.h"
190d09e41aSPaolo Bonzini #include "hw/virtio/virtio-bus.h"
20db1015e9SEduardo Habkost #include "qom/object.h"
21b307d308SGonglei
22085bccb7SKONRAD Frederic
230a2acf5eSKONRAD Frederic /* virtio-pci-bus */
240a2acf5eSKONRAD Frederic
250a2acf5eSKONRAD Frederic typedef struct VirtioBusState VirtioPCIBusState;
260a2acf5eSKONRAD Frederic typedef struct VirtioBusClass VirtioPCIBusClass;
270a2acf5eSKONRAD Frederic
280a2acf5eSKONRAD Frederic #define TYPE_VIRTIO_PCI_BUS "virtio-pci-bus"
298110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(VirtioPCIBusState, VirtioPCIBusClass,
308110fa1dSEduardo Habkost VIRTIO_PCI_BUS, TYPE_VIRTIO_PCI_BUS)
319fe1ebebSAneesh Kumar K.V
32fc1769b7SMarcel Apfelbaum enum {
33fc1769b7SMarcel Apfelbaum VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT,
34fc1769b7SMarcel Apfelbaum VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT,
35fc1769b7SMarcel Apfelbaum VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT,
36d9997d89SMarcel Apfelbaum VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT,
37615c4ed2SJason Wang VIRTIO_PCI_FLAG_ATS_BIT,
38c2cabb34SMarcel Apfelbaum VIRTIO_PCI_FLAG_INIT_DEVERR_BIT,
39d584f1b9SMarcel Apfelbaum VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT,
4027ce0f3aSMarcel Apfelbaum VIRTIO_PCI_FLAG_INIT_PM_BIT,
41eb1556c4SJulia Suvorova VIRTIO_PCI_FLAG_INIT_FLR_BIT,
42fdfa3b1dSAndrew Melnychenko VIRTIO_PCI_FLAG_AER_BIT,
43d83f46d1SJason Wang VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT,
445d98e188SJiqian Chen VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET_BIT,
45fc1769b7SMarcel Apfelbaum };
46fc1769b7SMarcel Apfelbaum
4768a27b20SMichael S. Tsirkin /* Need to activate work-arounds for buggy guests at vmstate load. */
4868a27b20SMichael S. Tsirkin #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
4968a27b20SMichael S. Tsirkin (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
5068a27b20SMichael S. Tsirkin
515745e38aSAneesh Kumar K.V /* Performance improves when virtqueue kick processing is decoupled from the
525745e38aSAneesh Kumar K.V * vcpu thread using ioeventfd for some devices. */
535745e38aSAneesh Kumar K.V #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT)
545745e38aSAneesh Kumar K.V
559824d2a3SJason Wang /* have pio notification for modern device ? */
569824d2a3SJason Wang #define VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY \
579824d2a3SJason Wang (1 << VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT)
589824d2a3SJason Wang
59d9997d89SMarcel Apfelbaum /* page per vq flag to be used by split drivers within guests */
60d9997d89SMarcel Apfelbaum #define VIRTIO_PCI_FLAG_PAGE_PER_VQ \
61d9997d89SMarcel Apfelbaum (1 << VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT)
62d9997d89SMarcel Apfelbaum
63615c4ed2SJason Wang /* address space translation service */
64615c4ed2SJason Wang #define VIRTIO_PCI_FLAG_ATS (1 << VIRTIO_PCI_FLAG_ATS_BIT)
65615c4ed2SJason Wang
66c2cabb34SMarcel Apfelbaum /* Init error enabling flags */
67c2cabb34SMarcel Apfelbaum #define VIRTIO_PCI_FLAG_INIT_DEVERR (1 << VIRTIO_PCI_FLAG_INIT_DEVERR_BIT)
68c2cabb34SMarcel Apfelbaum
69d584f1b9SMarcel Apfelbaum /* Init Link Control register */
70d584f1b9SMarcel Apfelbaum #define VIRTIO_PCI_FLAG_INIT_LNKCTL (1 << VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT)
71d584f1b9SMarcel Apfelbaum
7227ce0f3aSMarcel Apfelbaum /* Init Power Management */
7327ce0f3aSMarcel Apfelbaum #define VIRTIO_PCI_FLAG_INIT_PM (1 << VIRTIO_PCI_FLAG_INIT_PM_BIT)
7427ce0f3aSMarcel Apfelbaum
755d98e188SJiqian Chen /* Init The No_Soft_Reset bit of Power Management */
765d98e188SJiqian Chen #define VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET \
775d98e188SJiqian Chen (1 << VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET_BIT)
785d98e188SJiqian Chen
79eb1556c4SJulia Suvorova /* Init Function Level Reset capability */
80eb1556c4SJulia Suvorova #define VIRTIO_PCI_FLAG_INIT_FLR (1 << VIRTIO_PCI_FLAG_INIT_FLR_BIT)
81eb1556c4SJulia Suvorova
82fdfa3b1dSAndrew Melnychenko /* Advanced Error Reporting capability */
83fdfa3b1dSAndrew Melnychenko #define VIRTIO_PCI_FLAG_AER (1 << VIRTIO_PCI_FLAG_AER_BIT)
84fdfa3b1dSAndrew Melnychenko
85d83f46d1SJason Wang /* Page Aligned Address space Translation Service */
86d83f46d1SJason Wang #define VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED \
87d83f46d1SJason Wang (1 << VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT)
88d83f46d1SJason Wang
899fe1ebebSAneesh Kumar K.V typedef struct {
90774345f9SMichael S. Tsirkin MSIMessage msg;
917d37d351SJan Kiszka int virq;
927d37d351SJan Kiszka unsigned int users;
937d37d351SJan Kiszka } VirtIOIRQFD;
947d37d351SJan Kiszka
95085bccb7SKONRAD Frederic /*
96085bccb7SKONRAD Frederic * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
97085bccb7SKONRAD Frederic */
98085bccb7SKONRAD Frederic #define TYPE_VIRTIO_PCI "virtio-pci"
99a489d195SEduardo Habkost OBJECT_DECLARE_TYPE(VirtIOPCIProxy, VirtioPCIClass, VIRTIO_PCI)
100085bccb7SKONRAD Frederic
101db1015e9SEduardo Habkost struct VirtioPCIClass {
102085bccb7SKONRAD Frederic PCIDeviceClass parent_class;
1030560b0e9SShmulik Ladkani DeviceRealize parent_dc_realize;
104fc079951SMarkus Armbruster void (*realize)(VirtIOPCIProxy *vpci_dev, Error **errp);
105db1015e9SEduardo Habkost };
106085bccb7SKONRAD Frederic
107588255adSGerd Hoffmann typedef struct VirtIOPCIRegion {
108588255adSGerd Hoffmann MemoryRegion mr;
109a3cc2e81SGerd Hoffmann uint32_t offset;
110b6ce27a5SGerd Hoffmann uint32_t size;
111fc004905SGerd Hoffmann uint32_t type;
112588255adSGerd Hoffmann } VirtIOPCIRegion;
113588255adSGerd Hoffmann
114a6df8adfSJason Wang typedef struct VirtIOPCIQueue {
115a6df8adfSJason Wang uint16_t num;
116a6df8adfSJason Wang bool enabled;
117805d782dSXuan Zhuo /*
118805d782dSXuan Zhuo * No need to migrate the reset status, because it is always 0
119805d782dSXuan Zhuo * when the migration starts.
120805d782dSXuan Zhuo */
121805d782dSXuan Zhuo bool reset;
122a6df8adfSJason Wang uint32_t desc[2];
123a6df8adfSJason Wang uint32_t avail[2];
124a6df8adfSJason Wang uint32_t used[2];
125a6df8adfSJason Wang } VirtIOPCIQueue;
126a6df8adfSJason Wang
127085bccb7SKONRAD Frederic struct VirtIOPCIProxy {
1289fe1ebebSAneesh Kumar K.V PCIDevice pci_dev;
129da146d0aSAvi Kivity MemoryRegion bar;
130a93c8d82SAlexey Kardashevskiy union {
131a93c8d82SAlexey Kardashevskiy struct {
132588255adSGerd Hoffmann VirtIOPCIRegion common;
133588255adSGerd Hoffmann VirtIOPCIRegion isr;
134588255adSGerd Hoffmann VirtIOPCIRegion device;
135588255adSGerd Hoffmann VirtIOPCIRegion notify;
1369824d2a3SJason Wang VirtIOPCIRegion notify_pio;
137a93c8d82SAlexey Kardashevskiy };
138a93c8d82SAlexey Kardashevskiy VirtIOPCIRegion regs[5];
139a93c8d82SAlexey Kardashevskiy };
140dfb8e184SMichael S. Tsirkin MemoryRegion modern_bar;
1419824d2a3SJason Wang MemoryRegion io_bar;
14255fa4be6SGao Shiyuan /* address space for VirtIOPCIRegions */
14355fa4be6SGao Shiyuan AddressSpace modern_cfg_mem_as;
14455fa4be6SGao Shiyuan AddressSpace modern_cfg_io_as;
1457a25126dSChen Fan uint32_t legacy_io_bar_idx;
1467a25126dSChen Fan uint32_t msix_bar_idx;
1477a25126dSChen Fan uint32_t modern_io_bar_idx;
1487a25126dSChen Fan uint32_t modern_mem_bar_idx;
149ada434cdSMichael S. Tsirkin int config_cap;
150*3f9cfaa9SAkihiko Odaki uint16_t last_pcie_cap_offset;
1519fe1ebebSAneesh Kumar K.V uint32_t flags;
1529a4c0e22SMarcel Apfelbaum bool disable_modern;
15366d1c4c1SMaxime Coquelin bool ignore_backend_features;
1549a4c0e22SMarcel Apfelbaum OnOffAuto disable_legacy;
15522733245SLongpeng /* Transitional device id */
15622733245SLongpeng uint16_t trans_devid;
1579fe1ebebSAneesh Kumar K.V uint32_t class_code;
1589fe1ebebSAneesh Kumar K.V uint32_t nvectors;
159dfb8e184SMichael S. Tsirkin uint32_t dfselect;
160dfb8e184SMichael S. Tsirkin uint32_t gfselect;
161dfb8e184SMichael S. Tsirkin uint32_t guest_features[2];
162a6df8adfSJason Wang VirtIOPCIQueue vqs[VIRTIO_QUEUE_MAX];
163dfb8e184SMichael S. Tsirkin
1647d37d351SJan Kiszka VirtIOIRQFD *vector_irqfd;
1652d620f59SMichael S. Tsirkin int nvqs_with_notifiers;
166085bccb7SKONRAD Frederic VirtioBusState bus;
167085bccb7SKONRAD Frederic };
1689fe1ebebSAneesh Kumar K.V
virtio_pci_modern(VirtIOPCIProxy * proxy)1699a4c0e22SMarcel Apfelbaum static inline bool virtio_pci_modern(VirtIOPCIProxy *proxy)
1709a4c0e22SMarcel Apfelbaum {
171dd56040dSDr. David Alan Gilbert return !proxy->disable_modern;
1729a4c0e22SMarcel Apfelbaum }
1739a4c0e22SMarcel Apfelbaum
virtio_pci_legacy(VirtIOPCIProxy * proxy)1749a4c0e22SMarcel Apfelbaum static inline bool virtio_pci_legacy(VirtIOPCIProxy *proxy)
1759a4c0e22SMarcel Apfelbaum {
176dd56040dSDr. David Alan Gilbert return proxy->disable_legacy == ON_OFF_AUTO_OFF;
1779a4c0e22SMarcel Apfelbaum }
1789a4c0e22SMarcel Apfelbaum
virtio_pci_force_virtio_1(VirtIOPCIProxy * proxy)179dd56040dSDr. David Alan Gilbert static inline void virtio_pci_force_virtio_1(VirtIOPCIProxy *proxy)
1809a4c0e22SMarcel Apfelbaum {
181dd56040dSDr. David Alan Gilbert proxy->disable_modern = false;
182dd56040dSDr. David Alan Gilbert proxy->disable_legacy = ON_OFF_AUTO_ON;
1839a4c0e22SMarcel Apfelbaum }
184bc7b90a0SKONRAD Frederic
virtio_pci_disable_modern(VirtIOPCIProxy * proxy)185d1b4259fSMaxime Coquelin static inline void virtio_pci_disable_modern(VirtIOPCIProxy *proxy)
186d1b4259fSMaxime Coquelin {
187dd56040dSDr. David Alan Gilbert proxy->disable_modern = true;
188d1b4259fSMaxime Coquelin }
189d1b4259fSMaxime Coquelin
19022733245SLongpeng uint16_t virtio_pci_get_trans_devid(uint16_t device_id);
19122733245SLongpeng uint16_t virtio_pci_get_class_id(uint16_t device_id);
19222733245SLongpeng
193bc7b90a0SKONRAD Frederic /*
194f958c8aaSGerd Hoffmann * virtio-input-pci: This extends VirtioPCIProxy.
195f958c8aaSGerd Hoffmann */
196f958c8aaSGerd Hoffmann #define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci"
197710e2d90SGerd Hoffmann
198befeac45SMichael S. Tsirkin /* Virtio ABI version, if we increment this, we break the guest driver. */
199befeac45SMichael S. Tsirkin #define VIRTIO_PCI_ABI_VERSION 0
200befeac45SMichael S. Tsirkin
201a4ee4c8bSEduardo Habkost /* Input for virtio_pci_types_register() */
202a4ee4c8bSEduardo Habkost typedef struct VirtioPCIDeviceTypeInfo {
203a4ee4c8bSEduardo Habkost /*
204a4ee4c8bSEduardo Habkost * Common base class for the subclasses below.
205a4ee4c8bSEduardo Habkost *
206a4ee4c8bSEduardo Habkost * Required only if transitional_name or non_transitional_name is set.
207a4ee4c8bSEduardo Habkost *
208a4ee4c8bSEduardo Habkost * We need a separate base type instead of making all types
209a4ee4c8bSEduardo Habkost * inherit from generic_name for two reasons:
210a4ee4c8bSEduardo Habkost * 1) generic_name implements INTERFACE_PCIE_DEVICE, but
211a4ee4c8bSEduardo Habkost * transitional_name does not.
212a4ee4c8bSEduardo Habkost * 2) generic_name has the "disable-legacy" and "disable-modern"
213a4ee4c8bSEduardo Habkost * properties, transitional_name and non_transitional name don't.
214a4ee4c8bSEduardo Habkost */
215a4ee4c8bSEduardo Habkost const char *base_name;
216a4ee4c8bSEduardo Habkost /*
217a4ee4c8bSEduardo Habkost * Generic device type. Optional.
218a4ee4c8bSEduardo Habkost *
219a4ee4c8bSEduardo Habkost * Supports both transitional and non-transitional modes,
220a4ee4c8bSEduardo Habkost * using the disable-legacy and disable-modern properties.
221a4ee4c8bSEduardo Habkost * If disable-legacy=auto, (non-)transitional mode is selected
222a4ee4c8bSEduardo Habkost * depending on the bus where the device is plugged.
223a4ee4c8bSEduardo Habkost *
224a4ee4c8bSEduardo Habkost * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE,
225a4ee4c8bSEduardo Habkost * but PCI Express is supported only in non-transitional mode.
226a4ee4c8bSEduardo Habkost *
227a4ee4c8bSEduardo Habkost * The only type implemented by QEMU 3.1 and older.
228a4ee4c8bSEduardo Habkost */
229a4ee4c8bSEduardo Habkost const char *generic_name;
230a4ee4c8bSEduardo Habkost /*
231a4ee4c8bSEduardo Habkost * The transitional device type. Optional.
232a4ee4c8bSEduardo Habkost *
233a4ee4c8bSEduardo Habkost * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE.
234a4ee4c8bSEduardo Habkost */
235a4ee4c8bSEduardo Habkost const char *transitional_name;
236a4ee4c8bSEduardo Habkost /*
237a4ee4c8bSEduardo Habkost * The non-transitional device type. Optional.
238a4ee4c8bSEduardo Habkost *
239a4ee4c8bSEduardo Habkost * Implements INTERFACE_CONVENTIONAL_PCI_DEVICE only.
240a4ee4c8bSEduardo Habkost */
241a4ee4c8bSEduardo Habkost const char *non_transitional_name;
242a4ee4c8bSEduardo Habkost
243a4ee4c8bSEduardo Habkost /* Parent type. If NULL, TYPE_VIRTIO_PCI is used */
244a4ee4c8bSEduardo Habkost const char *parent;
245a4ee4c8bSEduardo Habkost
246a4ee4c8bSEduardo Habkost /* Same as TypeInfo fields: */
247a4ee4c8bSEduardo Habkost size_t instance_size;
2488ea90ee6SGerd Hoffmann size_t class_size;
249a4ee4c8bSEduardo Habkost void (*instance_init)(Object *obj);
250837053a7SPhilippe Mathieu-Daudé void (*instance_finalize)(Object *obj);
25112d1a768SPhilippe Mathieu-Daudé void (*class_init)(ObjectClass *klass, const void *data);
252231bf6ddSPhilippe Mathieu-Daudé const InterfaceInfo *interfaces;
253a4ee4c8bSEduardo Habkost } VirtioPCIDeviceTypeInfo;
254a4ee4c8bSEduardo Habkost
255a4ee4c8bSEduardo Habkost /* Register virtio-pci type(s). @t must be static. */
256a4ee4c8bSEduardo Habkost void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t);
257a4ee4c8bSEduardo Habkost
2581436f32aSStefan Hajnoczi /**
2591436f32aSStefan Hajnoczi * virtio_pci_optimal_num_queues:
2601436f32aSStefan Hajnoczi * @fixed_queues: number of queues that are always present
2611436f32aSStefan Hajnoczi *
2621436f32aSStefan Hajnoczi * Returns: The optimal number of queues for a multi-queue device, excluding
2631436f32aSStefan Hajnoczi * @fixed_queues.
2641436f32aSStefan Hajnoczi */
2651436f32aSStefan Hajnoczi unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues);
26616805428SCindy Lu void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, VirtQueue *vq,
26716805428SCindy Lu int n, bool assign,
26816805428SCindy Lu bool with_irqfd);
269605a16a7SDr. David Alan Gilbert
270605a16a7SDr. David Alan Gilbert int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy, uint8_t bar, uint64_t offset,
271605a16a7SDr. David Alan Gilbert uint64_t length, uint8_t id);
272605a16a7SDr. David Alan Gilbert
2739fe1ebebSAneesh Kumar K.V #endif
274