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, 465d98e188SJiqian 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 835d98e188SJiqian Chen /* Init The No_Soft_Reset bit of Power Management */ 845d98e188SJiqian Chen #define VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET \ 855d98e188SJiqian Chen (1 << VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET_BIT) 865d98e188SJiqian 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; 15055fa4be6SGao Shiyuan /* address space for VirtIOPCIRegions */ 15155fa4be6SGao Shiyuan AddressSpace modern_cfg_mem_as; 15255fa4be6SGao Shiyuan AddressSpace modern_cfg_io_as; 1537a25126dSChen Fan uint32_t legacy_io_bar_idx; 1547a25126dSChen Fan uint32_t msix_bar_idx; 1557a25126dSChen Fan uint32_t modern_io_bar_idx; 1567a25126dSChen Fan uint32_t modern_mem_bar_idx; 157ada434cdSMichael S. Tsirkin int config_cap; 158*3f9cfaa9SAkihiko Odaki uint16_t last_pcie_cap_offset; 1599fe1ebebSAneesh Kumar K.V uint32_t flags; 1609a4c0e22SMarcel Apfelbaum bool disable_modern; 16166d1c4c1SMaxime Coquelin bool ignore_backend_features; 1629a4c0e22SMarcel Apfelbaum OnOffAuto disable_legacy; 16322733245SLongpeng /* Transitional device id */ 16422733245SLongpeng uint16_t trans_devid; 1659fe1ebebSAneesh Kumar K.V uint32_t class_code; 1669fe1ebebSAneesh Kumar K.V uint32_t nvectors; 167dfb8e184SMichael S. Tsirkin uint32_t dfselect; 168dfb8e184SMichael S. Tsirkin uint32_t gfselect; 169dfb8e184SMichael S. Tsirkin uint32_t guest_features[2]; 170a6df8adfSJason Wang VirtIOPCIQueue vqs[VIRTIO_QUEUE_MAX]; 171dfb8e184SMichael S. Tsirkin 1727d37d351SJan Kiszka VirtIOIRQFD *vector_irqfd; 1732d620f59SMichael S. Tsirkin int nvqs_with_notifiers; 174085bccb7SKONRAD Frederic VirtioBusState bus; 175085bccb7SKONRAD Frederic }; 1769fe1ebebSAneesh Kumar K.V 1779a4c0e22SMarcel Apfelbaum static inline bool virtio_pci_modern(VirtIOPCIProxy *proxy) 1789a4c0e22SMarcel Apfelbaum { 179dd56040dSDr. David Alan Gilbert return !proxy->disable_modern; 1809a4c0e22SMarcel Apfelbaum } 1819a4c0e22SMarcel Apfelbaum 1829a4c0e22SMarcel Apfelbaum static inline bool virtio_pci_legacy(VirtIOPCIProxy *proxy) 1839a4c0e22SMarcel Apfelbaum { 184dd56040dSDr. David Alan Gilbert return proxy->disable_legacy == ON_OFF_AUTO_OFF; 1859a4c0e22SMarcel Apfelbaum } 1869a4c0e22SMarcel Apfelbaum 187dd56040dSDr. David Alan Gilbert static inline void virtio_pci_force_virtio_1(VirtIOPCIProxy *proxy) 1889a4c0e22SMarcel Apfelbaum { 189dd56040dSDr. David Alan Gilbert proxy->disable_modern = false; 190dd56040dSDr. David Alan Gilbert proxy->disable_legacy = ON_OFF_AUTO_ON; 1919a4c0e22SMarcel Apfelbaum } 192bc7b90a0SKONRAD Frederic 193d1b4259fSMaxime Coquelin static inline void virtio_pci_disable_modern(VirtIOPCIProxy *proxy) 194d1b4259fSMaxime Coquelin { 195dd56040dSDr. David Alan Gilbert proxy->disable_modern = true; 196d1b4259fSMaxime Coquelin } 197d1b4259fSMaxime Coquelin 19822733245SLongpeng uint16_t virtio_pci_get_trans_devid(uint16_t device_id); 19922733245SLongpeng uint16_t virtio_pci_get_class_id(uint16_t device_id); 20022733245SLongpeng 201bc7b90a0SKONRAD Frederic /* 202f958c8aaSGerd Hoffmann * virtio-input-pci: This extends VirtioPCIProxy. 203f958c8aaSGerd Hoffmann */ 204f958c8aaSGerd Hoffmann #define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci" 205710e2d90SGerd Hoffmann 206befeac45SMichael S. Tsirkin /* Virtio ABI version, if we increment this, we break the guest driver. */ 207befeac45SMichael S. Tsirkin #define VIRTIO_PCI_ABI_VERSION 0 208befeac45SMichael S. Tsirkin 209a4ee4c8bSEduardo Habkost /* Input for virtio_pci_types_register() */ 210a4ee4c8bSEduardo Habkost typedef struct VirtioPCIDeviceTypeInfo { 211a4ee4c8bSEduardo Habkost /* 212a4ee4c8bSEduardo Habkost * Common base class for the subclasses below. 213a4ee4c8bSEduardo Habkost * 214a4ee4c8bSEduardo Habkost * Required only if transitional_name or non_transitional_name is set. 215a4ee4c8bSEduardo Habkost * 216a4ee4c8bSEduardo Habkost * We need a separate base type instead of making all types 217a4ee4c8bSEduardo Habkost * inherit from generic_name for two reasons: 218a4ee4c8bSEduardo Habkost * 1) generic_name implements INTERFACE_PCIE_DEVICE, but 219a4ee4c8bSEduardo Habkost * transitional_name does not. 220a4ee4c8bSEduardo Habkost * 2) generic_name has the "disable-legacy" and "disable-modern" 221a4ee4c8bSEduardo Habkost * properties, transitional_name and non_transitional name don't. 222a4ee4c8bSEduardo Habkost */ 223a4ee4c8bSEduardo Habkost const char *base_name; 224a4ee4c8bSEduardo Habkost /* 225a4ee4c8bSEduardo Habkost * Generic device type. Optional. 226a4ee4c8bSEduardo Habkost * 227a4ee4c8bSEduardo Habkost * Supports both transitional and non-transitional modes, 228a4ee4c8bSEduardo Habkost * using the disable-legacy and disable-modern properties. 229a4ee4c8bSEduardo Habkost * If disable-legacy=auto, (non-)transitional mode is selected 230a4ee4c8bSEduardo Habkost * depending on the bus where the device is plugged. 231a4ee4c8bSEduardo Habkost * 232a4ee4c8bSEduardo Habkost * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE, 233a4ee4c8bSEduardo Habkost * but PCI Express is supported only in non-transitional mode. 234a4ee4c8bSEduardo Habkost * 235a4ee4c8bSEduardo Habkost * The only type implemented by QEMU 3.1 and older. 236a4ee4c8bSEduardo Habkost */ 237a4ee4c8bSEduardo Habkost const char *generic_name; 238a4ee4c8bSEduardo Habkost /* 239a4ee4c8bSEduardo Habkost * The transitional device type. Optional. 240a4ee4c8bSEduardo Habkost * 241a4ee4c8bSEduardo Habkost * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE. 242a4ee4c8bSEduardo Habkost */ 243a4ee4c8bSEduardo Habkost const char *transitional_name; 244a4ee4c8bSEduardo Habkost /* 245a4ee4c8bSEduardo Habkost * The non-transitional device type. Optional. 246a4ee4c8bSEduardo Habkost * 247a4ee4c8bSEduardo Habkost * Implements INTERFACE_CONVENTIONAL_PCI_DEVICE only. 248a4ee4c8bSEduardo Habkost */ 249a4ee4c8bSEduardo Habkost const char *non_transitional_name; 250a4ee4c8bSEduardo Habkost 251a4ee4c8bSEduardo Habkost /* Parent type. If NULL, TYPE_VIRTIO_PCI is used */ 252a4ee4c8bSEduardo Habkost const char *parent; 253a4ee4c8bSEduardo Habkost 254a4ee4c8bSEduardo Habkost /* Same as TypeInfo fields: */ 255a4ee4c8bSEduardo Habkost size_t instance_size; 2568ea90ee6SGerd Hoffmann size_t class_size; 257a4ee4c8bSEduardo Habkost void (*instance_init)(Object *obj); 258837053a7SPhilippe Mathieu-Daudé void (*instance_finalize)(Object *obj); 25912d1a768SPhilippe Mathieu-Daudé void (*class_init)(ObjectClass *klass, const void *data); 260231bf6ddSPhilippe Mathieu-Daudé const InterfaceInfo *interfaces; 261a4ee4c8bSEduardo Habkost } VirtioPCIDeviceTypeInfo; 262a4ee4c8bSEduardo Habkost 263a4ee4c8bSEduardo Habkost /* Register virtio-pci type(s). @t must be static. */ 264a4ee4c8bSEduardo Habkost void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t); 265a4ee4c8bSEduardo Habkost 2661436f32aSStefan Hajnoczi /** 2671436f32aSStefan Hajnoczi * virtio_pci_optimal_num_queues: 2681436f32aSStefan Hajnoczi * @fixed_queues: number of queues that are always present 2691436f32aSStefan Hajnoczi * 2701436f32aSStefan Hajnoczi * Returns: The optimal number of queues for a multi-queue device, excluding 2711436f32aSStefan Hajnoczi * @fixed_queues. 2721436f32aSStefan Hajnoczi */ 2731436f32aSStefan Hajnoczi unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues); 27416805428SCindy Lu void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, VirtQueue *vq, 27516805428SCindy Lu int n, bool assign, 27616805428SCindy Lu bool with_irqfd); 277605a16a7SDr. David Alan Gilbert 278605a16a7SDr. David Alan Gilbert int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy, uint8_t bar, uint64_t offset, 279605a16a7SDr. David Alan Gilbert uint64_t length, uint8_t id); 280605a16a7SDr. David Alan Gilbert 2819fe1ebebSAneesh Kumar K.V #endif 282