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, 46fc1769b7SMarcel Apfelbaum }; 47fc1769b7SMarcel Apfelbaum 4868a27b20SMichael S. Tsirkin /* Need to activate work-arounds for buggy guests at vmstate load. */ 4968a27b20SMichael S. Tsirkin #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \ 5068a27b20SMichael S. Tsirkin (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT) 5168a27b20SMichael S. Tsirkin 525745e38aSAneesh Kumar K.V /* Performance improves when virtqueue kick processing is decoupled from the 535745e38aSAneesh Kumar K.V * vcpu thread using ioeventfd for some devices. */ 545745e38aSAneesh Kumar K.V #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT) 555745e38aSAneesh Kumar K.V 56e266d421SGerd Hoffmann /* virtio version flags */ 571811e64cSMarcel Apfelbaum #define VIRTIO_PCI_FLAG_DISABLE_PCIE (1 << VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT) 58e266d421SGerd Hoffmann 59a6df8adfSJason Wang /* migrate extra state */ 60a6df8adfSJason Wang #define VIRTIO_PCI_FLAG_MIGRATE_EXTRA (1 << VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT) 61a6df8adfSJason Wang 629824d2a3SJason Wang /* have pio notification for modern device ? */ 639824d2a3SJason Wang #define VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY \ 649824d2a3SJason Wang (1 << VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT) 659824d2a3SJason Wang 66d9997d89SMarcel Apfelbaum /* page per vq flag to be used by split drivers within guests */ 67d9997d89SMarcel Apfelbaum #define VIRTIO_PCI_FLAG_PAGE_PER_VQ \ 68d9997d89SMarcel Apfelbaum (1 << VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT) 69d9997d89SMarcel Apfelbaum 70615c4ed2SJason Wang /* address space translation service */ 71615c4ed2SJason Wang #define VIRTIO_PCI_FLAG_ATS (1 << VIRTIO_PCI_FLAG_ATS_BIT) 72615c4ed2SJason Wang 73c2cabb34SMarcel Apfelbaum /* Init error enabling flags */ 74c2cabb34SMarcel Apfelbaum #define VIRTIO_PCI_FLAG_INIT_DEVERR (1 << VIRTIO_PCI_FLAG_INIT_DEVERR_BIT) 75c2cabb34SMarcel Apfelbaum 76d584f1b9SMarcel Apfelbaum /* Init Link Control register */ 77d584f1b9SMarcel Apfelbaum #define VIRTIO_PCI_FLAG_INIT_LNKCTL (1 << VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT) 78d584f1b9SMarcel Apfelbaum 7927ce0f3aSMarcel Apfelbaum /* Init Power Management */ 8027ce0f3aSMarcel Apfelbaum #define VIRTIO_PCI_FLAG_INIT_PM (1 << VIRTIO_PCI_FLAG_INIT_PM_BIT) 8127ce0f3aSMarcel Apfelbaum 82eb1556c4SJulia Suvorova /* Init Function Level Reset capability */ 83eb1556c4SJulia Suvorova #define VIRTIO_PCI_FLAG_INIT_FLR (1 << VIRTIO_PCI_FLAG_INIT_FLR_BIT) 84eb1556c4SJulia Suvorova 85fdfa3b1dSAndrew Melnychenko /* Advanced Error Reporting capability */ 86fdfa3b1dSAndrew Melnychenko #define VIRTIO_PCI_FLAG_AER (1 << VIRTIO_PCI_FLAG_AER_BIT) 87fdfa3b1dSAndrew Melnychenko 88d83f46d1SJason Wang /* Page Aligned Address space Translation Service */ 89d83f46d1SJason Wang #define VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED \ 90d83f46d1SJason Wang (1 << VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT) 91d83f46d1SJason Wang 929fe1ebebSAneesh Kumar K.V typedef struct { 93774345f9SMichael S. Tsirkin MSIMessage msg; 947d37d351SJan Kiszka int virq; 957d37d351SJan Kiszka unsigned int users; 967d37d351SJan Kiszka } VirtIOIRQFD; 977d37d351SJan Kiszka 98085bccb7SKONRAD Frederic /* 99085bccb7SKONRAD Frederic * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. 100085bccb7SKONRAD Frederic */ 101085bccb7SKONRAD Frederic #define TYPE_VIRTIO_PCI "virtio-pci" 102a489d195SEduardo Habkost OBJECT_DECLARE_TYPE(VirtIOPCIProxy, VirtioPCIClass, VIRTIO_PCI) 103085bccb7SKONRAD Frederic 104db1015e9SEduardo Habkost struct VirtioPCIClass { 105085bccb7SKONRAD Frederic PCIDeviceClass parent_class; 1060560b0e9SShmulik Ladkani DeviceRealize parent_dc_realize; 107fc079951SMarkus Armbruster void (*realize)(VirtIOPCIProxy *vpci_dev, Error **errp); 108db1015e9SEduardo Habkost }; 109085bccb7SKONRAD Frederic 110588255adSGerd Hoffmann typedef struct VirtIOPCIRegion { 111588255adSGerd Hoffmann MemoryRegion mr; 112a3cc2e81SGerd Hoffmann uint32_t offset; 113b6ce27a5SGerd Hoffmann uint32_t size; 114fc004905SGerd Hoffmann uint32_t type; 115588255adSGerd Hoffmann } VirtIOPCIRegion; 116588255adSGerd Hoffmann 117a6df8adfSJason Wang typedef struct VirtIOPCIQueue { 118a6df8adfSJason Wang uint16_t num; 119a6df8adfSJason Wang bool enabled; 120805d782dSXuan Zhuo /* 121805d782dSXuan Zhuo * No need to migrate the reset status, because it is always 0 122805d782dSXuan Zhuo * when the migration starts. 123805d782dSXuan Zhuo */ 124805d782dSXuan Zhuo bool reset; 125a6df8adfSJason Wang uint32_t desc[2]; 126a6df8adfSJason Wang uint32_t avail[2]; 127a6df8adfSJason Wang uint32_t used[2]; 128a6df8adfSJason Wang } VirtIOPCIQueue; 129a6df8adfSJason Wang 130085bccb7SKONRAD Frederic struct VirtIOPCIProxy { 1319fe1ebebSAneesh Kumar K.V PCIDevice pci_dev; 132da146d0aSAvi Kivity MemoryRegion bar; 133a93c8d82SAlexey Kardashevskiy union { 134a93c8d82SAlexey Kardashevskiy struct { 135588255adSGerd Hoffmann VirtIOPCIRegion common; 136588255adSGerd Hoffmann VirtIOPCIRegion isr; 137588255adSGerd Hoffmann VirtIOPCIRegion device; 138588255adSGerd Hoffmann VirtIOPCIRegion notify; 1399824d2a3SJason Wang VirtIOPCIRegion notify_pio; 140a93c8d82SAlexey Kardashevskiy }; 141a93c8d82SAlexey Kardashevskiy VirtIOPCIRegion regs[5]; 142a93c8d82SAlexey Kardashevskiy }; 143dfb8e184SMichael S. Tsirkin MemoryRegion modern_bar; 1449824d2a3SJason Wang MemoryRegion io_bar; 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; 1509fe1ebebSAneesh Kumar K.V uint32_t flags; 1519a4c0e22SMarcel Apfelbaum bool disable_modern; 15266d1c4c1SMaxime Coquelin bool ignore_backend_features; 1539a4c0e22SMarcel Apfelbaum OnOffAuto disable_legacy; 15422733245SLongpeng /* Transitional device id */ 15522733245SLongpeng uint16_t trans_devid; 1569fe1ebebSAneesh Kumar K.V uint32_t class_code; 1579fe1ebebSAneesh Kumar K.V uint32_t nvectors; 158dfb8e184SMichael S. Tsirkin uint32_t dfselect; 159dfb8e184SMichael S. Tsirkin uint32_t gfselect; 160dfb8e184SMichael S. Tsirkin uint32_t guest_features[2]; 161a6df8adfSJason Wang VirtIOPCIQueue vqs[VIRTIO_QUEUE_MAX]; 162dfb8e184SMichael S. Tsirkin 1637d37d351SJan Kiszka VirtIOIRQFD *vector_irqfd; 1642d620f59SMichael S. Tsirkin int nvqs_with_notifiers; 165085bccb7SKONRAD Frederic VirtioBusState bus; 166085bccb7SKONRAD Frederic }; 1679fe1ebebSAneesh Kumar K.V 1689a4c0e22SMarcel Apfelbaum static inline bool virtio_pci_modern(VirtIOPCIProxy *proxy) 1699a4c0e22SMarcel Apfelbaum { 170dd56040dSDr. David Alan Gilbert return !proxy->disable_modern; 1719a4c0e22SMarcel Apfelbaum } 1729a4c0e22SMarcel Apfelbaum 1739a4c0e22SMarcel Apfelbaum static inline bool virtio_pci_legacy(VirtIOPCIProxy *proxy) 1749a4c0e22SMarcel Apfelbaum { 175dd56040dSDr. David Alan Gilbert return proxy->disable_legacy == ON_OFF_AUTO_OFF; 1769a4c0e22SMarcel Apfelbaum } 1779a4c0e22SMarcel Apfelbaum 178dd56040dSDr. David Alan Gilbert static inline void virtio_pci_force_virtio_1(VirtIOPCIProxy *proxy) 1799a4c0e22SMarcel Apfelbaum { 180dd56040dSDr. David Alan Gilbert proxy->disable_modern = false; 181dd56040dSDr. David Alan Gilbert proxy->disable_legacy = ON_OFF_AUTO_ON; 1829a4c0e22SMarcel Apfelbaum } 183bc7b90a0SKONRAD Frederic 184d1b4259fSMaxime Coquelin static inline void virtio_pci_disable_modern(VirtIOPCIProxy *proxy) 185d1b4259fSMaxime Coquelin { 186dd56040dSDr. David Alan Gilbert proxy->disable_modern = true; 187d1b4259fSMaxime Coquelin } 188d1b4259fSMaxime Coquelin 18922733245SLongpeng uint16_t virtio_pci_get_trans_devid(uint16_t device_id); 19022733245SLongpeng uint16_t virtio_pci_get_class_id(uint16_t device_id); 19122733245SLongpeng 192bc7b90a0SKONRAD Frederic /* 193f958c8aaSGerd Hoffmann * virtio-input-pci: This extends VirtioPCIProxy. 194f958c8aaSGerd Hoffmann */ 195f958c8aaSGerd Hoffmann #define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci" 196710e2d90SGerd Hoffmann 197befeac45SMichael S. Tsirkin /* Virtio ABI version, if we increment this, we break the guest driver. */ 198befeac45SMichael S. Tsirkin #define VIRTIO_PCI_ABI_VERSION 0 199befeac45SMichael S. Tsirkin 200a4ee4c8bSEduardo Habkost /* Input for virtio_pci_types_register() */ 201a4ee4c8bSEduardo Habkost typedef struct VirtioPCIDeviceTypeInfo { 202a4ee4c8bSEduardo Habkost /* 203a4ee4c8bSEduardo Habkost * Common base class for the subclasses below. 204a4ee4c8bSEduardo Habkost * 205a4ee4c8bSEduardo Habkost * Required only if transitional_name or non_transitional_name is set. 206a4ee4c8bSEduardo Habkost * 207a4ee4c8bSEduardo Habkost * We need a separate base type instead of making all types 208a4ee4c8bSEduardo Habkost * inherit from generic_name for two reasons: 209a4ee4c8bSEduardo Habkost * 1) generic_name implements INTERFACE_PCIE_DEVICE, but 210a4ee4c8bSEduardo Habkost * transitional_name does not. 211a4ee4c8bSEduardo Habkost * 2) generic_name has the "disable-legacy" and "disable-modern" 212a4ee4c8bSEduardo Habkost * properties, transitional_name and non_transitional name don't. 213a4ee4c8bSEduardo Habkost */ 214a4ee4c8bSEduardo Habkost const char *base_name; 215a4ee4c8bSEduardo Habkost /* 216a4ee4c8bSEduardo Habkost * Generic device type. Optional. 217a4ee4c8bSEduardo Habkost * 218a4ee4c8bSEduardo Habkost * Supports both transitional and non-transitional modes, 219a4ee4c8bSEduardo Habkost * using the disable-legacy and disable-modern properties. 220a4ee4c8bSEduardo Habkost * If disable-legacy=auto, (non-)transitional mode is selected 221a4ee4c8bSEduardo Habkost * depending on the bus where the device is plugged. 222a4ee4c8bSEduardo Habkost * 223a4ee4c8bSEduardo Habkost * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE, 224a4ee4c8bSEduardo Habkost * but PCI Express is supported only in non-transitional mode. 225a4ee4c8bSEduardo Habkost * 226a4ee4c8bSEduardo Habkost * The only type implemented by QEMU 3.1 and older. 227a4ee4c8bSEduardo Habkost */ 228a4ee4c8bSEduardo Habkost const char *generic_name; 229a4ee4c8bSEduardo Habkost /* 230a4ee4c8bSEduardo Habkost * The transitional device type. Optional. 231a4ee4c8bSEduardo Habkost * 232a4ee4c8bSEduardo Habkost * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE. 233a4ee4c8bSEduardo Habkost */ 234a4ee4c8bSEduardo Habkost const char *transitional_name; 235a4ee4c8bSEduardo Habkost /* 236a4ee4c8bSEduardo Habkost * The non-transitional device type. Optional. 237a4ee4c8bSEduardo Habkost * 238a4ee4c8bSEduardo Habkost * Implements INTERFACE_CONVENTIONAL_PCI_DEVICE only. 239a4ee4c8bSEduardo Habkost */ 240a4ee4c8bSEduardo Habkost const char *non_transitional_name; 241a4ee4c8bSEduardo Habkost 242a4ee4c8bSEduardo Habkost /* Parent type. If NULL, TYPE_VIRTIO_PCI is used */ 243a4ee4c8bSEduardo Habkost const char *parent; 244a4ee4c8bSEduardo Habkost 245a4ee4c8bSEduardo Habkost /* Same as TypeInfo fields: */ 246a4ee4c8bSEduardo Habkost size_t instance_size; 2478ea90ee6SGerd Hoffmann size_t class_size; 248a4ee4c8bSEduardo Habkost void (*instance_init)(Object *obj); 249*837053a7SPhilippe Mathieu-Daudé void (*instance_finalize)(Object *obj); 250a4ee4c8bSEduardo Habkost void (*class_init)(ObjectClass *klass, void *data); 2511e33b513SDavid Hildenbrand InterfaceInfo *interfaces; 252a4ee4c8bSEduardo Habkost } VirtioPCIDeviceTypeInfo; 253a4ee4c8bSEduardo Habkost 254a4ee4c8bSEduardo Habkost /* Register virtio-pci type(s). @t must be static. */ 255a4ee4c8bSEduardo Habkost void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t); 256a4ee4c8bSEduardo Habkost 2571436f32aSStefan Hajnoczi /** 2581436f32aSStefan Hajnoczi * virtio_pci_optimal_num_queues: 2591436f32aSStefan Hajnoczi * @fixed_queues: number of queues that are always present 2601436f32aSStefan Hajnoczi * 2611436f32aSStefan Hajnoczi * Returns: The optimal number of queues for a multi-queue device, excluding 2621436f32aSStefan Hajnoczi * @fixed_queues. 2631436f32aSStefan Hajnoczi */ 2641436f32aSStefan Hajnoczi unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues); 26516805428SCindy Lu void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, VirtQueue *vq, 26616805428SCindy Lu int n, bool assign, 26716805428SCindy Lu bool with_irqfd); 268605a16a7SDr. David Alan Gilbert 269605a16a7SDr. David Alan Gilbert int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy, uint8_t bar, uint64_t offset, 270605a16a7SDr. David Alan Gilbert uint64_t length, uint8_t id); 271605a16a7SDr. David Alan Gilbert 2729fe1ebebSAneesh Kumar K.V #endif 273