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_MIGRATE_EXTRA_BIT, 36fc1769b7SMarcel Apfelbaum VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, 37fc1769b7SMarcel Apfelbaum VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, 38d9997d89SMarcel Apfelbaum VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, 39615c4ed2SJason Wang VIRTIO_PCI_FLAG_ATS_BIT, 40c2cabb34SMarcel Apfelbaum VIRTIO_PCI_FLAG_INIT_DEVERR_BIT, 41d584f1b9SMarcel Apfelbaum VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, 4227ce0f3aSMarcel Apfelbaum VIRTIO_PCI_FLAG_INIT_PM_BIT, 43eb1556c4SJulia Suvorova VIRTIO_PCI_FLAG_INIT_FLR_BIT, 44fdfa3b1dSAndrew Melnychenko VIRTIO_PCI_FLAG_AER_BIT, 45d83f46d1SJason Wang VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT, 46*5d98e188SJiqian Chen VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET_BIT, 47fc1769b7SMarcel Apfelbaum }; 48fc1769b7SMarcel Apfelbaum 4968a27b20SMichael S. Tsirkin /* Need to activate work-arounds for buggy guests at vmstate load. */ 5068a27b20SMichael S. Tsirkin #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \ 5168a27b20SMichael S. Tsirkin (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT) 5268a27b20SMichael S. Tsirkin 535745e38aSAneesh Kumar K.V /* Performance improves when virtqueue kick processing is decoupled from the 545745e38aSAneesh Kumar K.V * vcpu thread using ioeventfd for some devices. */ 555745e38aSAneesh Kumar K.V #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT) 565745e38aSAneesh Kumar K.V 57e266d421SGerd Hoffmann /* virtio version flags */ 581811e64cSMarcel Apfelbaum #define VIRTIO_PCI_FLAG_DISABLE_PCIE (1 << VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT) 59e266d421SGerd Hoffmann 60a6df8adfSJason Wang /* migrate extra state */ 61a6df8adfSJason Wang #define VIRTIO_PCI_FLAG_MIGRATE_EXTRA (1 << VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT) 62a6df8adfSJason Wang 639824d2a3SJason Wang /* have pio notification for modern device ? */ 649824d2a3SJason Wang #define VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY \ 659824d2a3SJason Wang (1 << VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT) 669824d2a3SJason Wang 67d9997d89SMarcel Apfelbaum /* page per vq flag to be used by split drivers within guests */ 68d9997d89SMarcel Apfelbaum #define VIRTIO_PCI_FLAG_PAGE_PER_VQ \ 69d9997d89SMarcel Apfelbaum (1 << VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT) 70d9997d89SMarcel Apfelbaum 71615c4ed2SJason Wang /* address space translation service */ 72615c4ed2SJason Wang #define VIRTIO_PCI_FLAG_ATS (1 << VIRTIO_PCI_FLAG_ATS_BIT) 73615c4ed2SJason Wang 74c2cabb34SMarcel Apfelbaum /* Init error enabling flags */ 75c2cabb34SMarcel Apfelbaum #define VIRTIO_PCI_FLAG_INIT_DEVERR (1 << VIRTIO_PCI_FLAG_INIT_DEVERR_BIT) 76c2cabb34SMarcel Apfelbaum 77d584f1b9SMarcel Apfelbaum /* Init Link Control register */ 78d584f1b9SMarcel Apfelbaum #define VIRTIO_PCI_FLAG_INIT_LNKCTL (1 << VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT) 79d584f1b9SMarcel Apfelbaum 8027ce0f3aSMarcel Apfelbaum /* Init Power Management */ 8127ce0f3aSMarcel Apfelbaum #define VIRTIO_PCI_FLAG_INIT_PM (1 << VIRTIO_PCI_FLAG_INIT_PM_BIT) 8227ce0f3aSMarcel Apfelbaum 83*5d98e188SJiqian Chen /* Init The No_Soft_Reset bit of Power Management */ 84*5d98e188SJiqian Chen #define VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET \ 85*5d98e188SJiqian Chen (1 << VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET_BIT) 86*5d98e188SJiqian Chen 87eb1556c4SJulia Suvorova /* Init Function Level Reset capability */ 88eb1556c4SJulia Suvorova #define VIRTIO_PCI_FLAG_INIT_FLR (1 << VIRTIO_PCI_FLAG_INIT_FLR_BIT) 89eb1556c4SJulia Suvorova 90fdfa3b1dSAndrew Melnychenko /* Advanced Error Reporting capability */ 91fdfa3b1dSAndrew Melnychenko #define VIRTIO_PCI_FLAG_AER (1 << VIRTIO_PCI_FLAG_AER_BIT) 92fdfa3b1dSAndrew Melnychenko 93d83f46d1SJason Wang /* Page Aligned Address space Translation Service */ 94d83f46d1SJason Wang #define VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED \ 95d83f46d1SJason Wang (1 << VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT) 96d83f46d1SJason Wang 979fe1ebebSAneesh Kumar K.V typedef struct { 98774345f9SMichael S. Tsirkin MSIMessage msg; 997d37d351SJan Kiszka int virq; 1007d37d351SJan Kiszka unsigned int users; 1017d37d351SJan Kiszka } VirtIOIRQFD; 1027d37d351SJan Kiszka 103085bccb7SKONRAD Frederic /* 104085bccb7SKONRAD Frederic * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. 105085bccb7SKONRAD Frederic */ 106085bccb7SKONRAD Frederic #define TYPE_VIRTIO_PCI "virtio-pci" 107a489d195SEduardo Habkost OBJECT_DECLARE_TYPE(VirtIOPCIProxy, VirtioPCIClass, VIRTIO_PCI) 108085bccb7SKONRAD Frederic 109db1015e9SEduardo Habkost struct VirtioPCIClass { 110085bccb7SKONRAD Frederic PCIDeviceClass parent_class; 1110560b0e9SShmulik Ladkani DeviceRealize parent_dc_realize; 112fc079951SMarkus Armbruster void (*realize)(VirtIOPCIProxy *vpci_dev, Error **errp); 113db1015e9SEduardo Habkost }; 114085bccb7SKONRAD Frederic 115588255adSGerd Hoffmann typedef struct VirtIOPCIRegion { 116588255adSGerd Hoffmann MemoryRegion mr; 117a3cc2e81SGerd Hoffmann uint32_t offset; 118b6ce27a5SGerd Hoffmann uint32_t size; 119fc004905SGerd Hoffmann uint32_t type; 120588255adSGerd Hoffmann } VirtIOPCIRegion; 121588255adSGerd Hoffmann 122a6df8adfSJason Wang typedef struct VirtIOPCIQueue { 123a6df8adfSJason Wang uint16_t num; 124a6df8adfSJason Wang bool enabled; 125805d782dSXuan Zhuo /* 126805d782dSXuan Zhuo * No need to migrate the reset status, because it is always 0 127805d782dSXuan Zhuo * when the migration starts. 128805d782dSXuan Zhuo */ 129805d782dSXuan Zhuo bool reset; 130a6df8adfSJason Wang uint32_t desc[2]; 131a6df8adfSJason Wang uint32_t avail[2]; 132a6df8adfSJason Wang uint32_t used[2]; 133a6df8adfSJason Wang } VirtIOPCIQueue; 134a6df8adfSJason Wang 135085bccb7SKONRAD Frederic struct VirtIOPCIProxy { 1369fe1ebebSAneesh Kumar K.V PCIDevice pci_dev; 137da146d0aSAvi Kivity MemoryRegion bar; 138a93c8d82SAlexey Kardashevskiy union { 139a93c8d82SAlexey Kardashevskiy struct { 140588255adSGerd Hoffmann VirtIOPCIRegion common; 141588255adSGerd Hoffmann VirtIOPCIRegion isr; 142588255adSGerd Hoffmann VirtIOPCIRegion device; 143588255adSGerd Hoffmann VirtIOPCIRegion notify; 1449824d2a3SJason Wang VirtIOPCIRegion notify_pio; 145a93c8d82SAlexey Kardashevskiy }; 146a93c8d82SAlexey Kardashevskiy VirtIOPCIRegion regs[5]; 147a93c8d82SAlexey Kardashevskiy }; 148dfb8e184SMichael S. Tsirkin MemoryRegion modern_bar; 1499824d2a3SJason Wang MemoryRegion io_bar; 1507a25126dSChen Fan uint32_t legacy_io_bar_idx; 1517a25126dSChen Fan uint32_t msix_bar_idx; 1527a25126dSChen Fan uint32_t modern_io_bar_idx; 1537a25126dSChen Fan uint32_t modern_mem_bar_idx; 154ada434cdSMichael S. Tsirkin int config_cap; 1559fe1ebebSAneesh Kumar K.V uint32_t flags; 1569a4c0e22SMarcel Apfelbaum bool disable_modern; 15766d1c4c1SMaxime Coquelin bool ignore_backend_features; 1589a4c0e22SMarcel Apfelbaum OnOffAuto disable_legacy; 15922733245SLongpeng /* Transitional device id */ 16022733245SLongpeng uint16_t trans_devid; 1619fe1ebebSAneesh Kumar K.V uint32_t class_code; 1629fe1ebebSAneesh Kumar K.V uint32_t nvectors; 163dfb8e184SMichael S. Tsirkin uint32_t dfselect; 164dfb8e184SMichael S. Tsirkin uint32_t gfselect; 165dfb8e184SMichael S. Tsirkin uint32_t guest_features[2]; 166a6df8adfSJason Wang VirtIOPCIQueue vqs[VIRTIO_QUEUE_MAX]; 167dfb8e184SMichael S. Tsirkin 1687d37d351SJan Kiszka VirtIOIRQFD *vector_irqfd; 1692d620f59SMichael S. Tsirkin int nvqs_with_notifiers; 170085bccb7SKONRAD Frederic VirtioBusState bus; 171085bccb7SKONRAD Frederic }; 1729fe1ebebSAneesh Kumar K.V 1739a4c0e22SMarcel Apfelbaum static inline bool virtio_pci_modern(VirtIOPCIProxy *proxy) 1749a4c0e22SMarcel Apfelbaum { 175dd56040dSDr. David Alan Gilbert return !proxy->disable_modern; 1769a4c0e22SMarcel Apfelbaum } 1779a4c0e22SMarcel Apfelbaum 1789a4c0e22SMarcel Apfelbaum static inline bool virtio_pci_legacy(VirtIOPCIProxy *proxy) 1799a4c0e22SMarcel Apfelbaum { 180dd56040dSDr. David Alan Gilbert return proxy->disable_legacy == ON_OFF_AUTO_OFF; 1819a4c0e22SMarcel Apfelbaum } 1829a4c0e22SMarcel Apfelbaum 183dd56040dSDr. David Alan Gilbert static inline void virtio_pci_force_virtio_1(VirtIOPCIProxy *proxy) 1849a4c0e22SMarcel Apfelbaum { 185dd56040dSDr. David Alan Gilbert proxy->disable_modern = false; 186dd56040dSDr. David Alan Gilbert proxy->disable_legacy = ON_OFF_AUTO_ON; 1879a4c0e22SMarcel Apfelbaum } 188bc7b90a0SKONRAD Frederic 189d1b4259fSMaxime Coquelin static inline void virtio_pci_disable_modern(VirtIOPCIProxy *proxy) 190d1b4259fSMaxime Coquelin { 191dd56040dSDr. David Alan Gilbert proxy->disable_modern = true; 192d1b4259fSMaxime Coquelin } 193d1b4259fSMaxime Coquelin 19422733245SLongpeng uint16_t virtio_pci_get_trans_devid(uint16_t device_id); 19522733245SLongpeng uint16_t virtio_pci_get_class_id(uint16_t device_id); 19622733245SLongpeng 197bc7b90a0SKONRAD Frederic /* 198f958c8aaSGerd Hoffmann * virtio-input-pci: This extends VirtioPCIProxy. 199f958c8aaSGerd Hoffmann */ 200f958c8aaSGerd Hoffmann #define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci" 201710e2d90SGerd Hoffmann 202befeac45SMichael S. Tsirkin /* Virtio ABI version, if we increment this, we break the guest driver. */ 203befeac45SMichael S. Tsirkin #define VIRTIO_PCI_ABI_VERSION 0 204befeac45SMichael S. Tsirkin 205a4ee4c8bSEduardo Habkost /* Input for virtio_pci_types_register() */ 206a4ee4c8bSEduardo Habkost typedef struct VirtioPCIDeviceTypeInfo { 207a4ee4c8bSEduardo Habkost /* 208a4ee4c8bSEduardo Habkost * Common base class for the subclasses below. 209a4ee4c8bSEduardo Habkost * 210a4ee4c8bSEduardo Habkost * Required only if transitional_name or non_transitional_name is set. 211a4ee4c8bSEduardo Habkost * 212a4ee4c8bSEduardo Habkost * We need a separate base type instead of making all types 213a4ee4c8bSEduardo Habkost * inherit from generic_name for two reasons: 214a4ee4c8bSEduardo Habkost * 1) generic_name implements INTERFACE_PCIE_DEVICE, but 215a4ee4c8bSEduardo Habkost * transitional_name does not. 216a4ee4c8bSEduardo Habkost * 2) generic_name has the "disable-legacy" and "disable-modern" 217a4ee4c8bSEduardo Habkost * properties, transitional_name and non_transitional name don't. 218a4ee4c8bSEduardo Habkost */ 219a4ee4c8bSEduardo Habkost const char *base_name; 220a4ee4c8bSEduardo Habkost /* 221a4ee4c8bSEduardo Habkost * Generic device type. Optional. 222a4ee4c8bSEduardo Habkost * 223a4ee4c8bSEduardo Habkost * Supports both transitional and non-transitional modes, 224a4ee4c8bSEduardo Habkost * using the disable-legacy and disable-modern properties. 225a4ee4c8bSEduardo Habkost * If disable-legacy=auto, (non-)transitional mode is selected 226a4ee4c8bSEduardo Habkost * depending on the bus where the device is plugged. 227a4ee4c8bSEduardo Habkost * 228a4ee4c8bSEduardo Habkost * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE, 229a4ee4c8bSEduardo Habkost * but PCI Express is supported only in non-transitional mode. 230a4ee4c8bSEduardo Habkost * 231a4ee4c8bSEduardo Habkost * The only type implemented by QEMU 3.1 and older. 232a4ee4c8bSEduardo Habkost */ 233a4ee4c8bSEduardo Habkost const char *generic_name; 234a4ee4c8bSEduardo Habkost /* 235a4ee4c8bSEduardo Habkost * The transitional device type. Optional. 236a4ee4c8bSEduardo Habkost * 237a4ee4c8bSEduardo Habkost * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE. 238a4ee4c8bSEduardo Habkost */ 239a4ee4c8bSEduardo Habkost const char *transitional_name; 240a4ee4c8bSEduardo Habkost /* 241a4ee4c8bSEduardo Habkost * The non-transitional device type. Optional. 242a4ee4c8bSEduardo Habkost * 243a4ee4c8bSEduardo Habkost * Implements INTERFACE_CONVENTIONAL_PCI_DEVICE only. 244a4ee4c8bSEduardo Habkost */ 245a4ee4c8bSEduardo Habkost const char *non_transitional_name; 246a4ee4c8bSEduardo Habkost 247a4ee4c8bSEduardo Habkost /* Parent type. If NULL, TYPE_VIRTIO_PCI is used */ 248a4ee4c8bSEduardo Habkost const char *parent; 249a4ee4c8bSEduardo Habkost 250a4ee4c8bSEduardo Habkost /* Same as TypeInfo fields: */ 251a4ee4c8bSEduardo Habkost size_t instance_size; 2528ea90ee6SGerd Hoffmann size_t class_size; 253a4ee4c8bSEduardo Habkost void (*instance_init)(Object *obj); 254837053a7SPhilippe Mathieu-Daudé void (*instance_finalize)(Object *obj); 255a4ee4c8bSEduardo Habkost void (*class_init)(ObjectClass *klass, void *data); 2561e33b513SDavid Hildenbrand InterfaceInfo *interfaces; 257a4ee4c8bSEduardo Habkost } VirtioPCIDeviceTypeInfo; 258a4ee4c8bSEduardo Habkost 259a4ee4c8bSEduardo Habkost /* Register virtio-pci type(s). @t must be static. */ 260a4ee4c8bSEduardo Habkost void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t); 261a4ee4c8bSEduardo Habkost 2621436f32aSStefan Hajnoczi /** 2631436f32aSStefan Hajnoczi * virtio_pci_optimal_num_queues: 2641436f32aSStefan Hajnoczi * @fixed_queues: number of queues that are always present 2651436f32aSStefan Hajnoczi * 2661436f32aSStefan Hajnoczi * Returns: The optimal number of queues for a multi-queue device, excluding 2671436f32aSStefan Hajnoczi * @fixed_queues. 2681436f32aSStefan Hajnoczi */ 2691436f32aSStefan Hajnoczi unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues); 27016805428SCindy Lu void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, VirtQueue *vq, 27116805428SCindy Lu int n, bool assign, 27216805428SCindy Lu bool with_irqfd); 273605a16a7SDr. David Alan Gilbert 274605a16a7SDr. David Alan Gilbert int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy, uint8_t bar, uint64_t offset, 275605a16a7SDr. David Alan Gilbert uint64_t length, uint8_t id); 276605a16a7SDr. David Alan Gilbert 2779fe1ebebSAneesh Kumar K.V #endif 278