1 /* 2 * Virtio PCI Bindings 3 * 4 * Copyright IBM, Corp. 2007 5 * Copyright (c) 2009 CodeSourcery 6 * 7 * Authors: 8 * Anthony Liguori <aliguori@us.ibm.com> 9 * Paul Brook <paul@codesourcery.com> 10 * 11 * This work is licensed under the terms of the GNU GPL, version 2. See 12 * the COPYING file in the top-level directory. 13 */ 14 15 #ifndef QEMU_VIRTIO_PCI_H 16 #define QEMU_VIRTIO_PCI_H 17 18 #include "hw/pci/msi.h" 19 #include "hw/virtio/virtio-bus.h" 20 21 typedef struct VirtIOPCIProxy VirtIOPCIProxy; 22 23 /* virtio-pci-bus */ 24 25 typedef struct VirtioBusState VirtioPCIBusState; 26 typedef struct VirtioBusClass VirtioPCIBusClass; 27 28 #define TYPE_VIRTIO_PCI_BUS "virtio-pci-bus" 29 #define VIRTIO_PCI_BUS(obj) \ 30 OBJECT_CHECK(VirtioPCIBusState, (obj), TYPE_VIRTIO_PCI_BUS) 31 #define VIRTIO_PCI_BUS_GET_CLASS(obj) \ 32 OBJECT_GET_CLASS(VirtioPCIBusClass, obj, TYPE_VIRTIO_PCI_BUS) 33 #define VIRTIO_PCI_BUS_CLASS(klass) \ 34 OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS) 35 36 enum { 37 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, 38 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, 39 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, 40 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, 41 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, 42 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, 43 VIRTIO_PCI_FLAG_ATS_BIT, 44 VIRTIO_PCI_FLAG_INIT_DEVERR_BIT, 45 VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, 46 VIRTIO_PCI_FLAG_INIT_PM_BIT, 47 }; 48 49 /* Need to activate work-arounds for buggy guests at vmstate load. */ 50 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \ 51 (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT) 52 53 /* Performance improves when virtqueue kick processing is decoupled from the 54 * vcpu thread using ioeventfd for some devices. */ 55 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT) 56 57 /* virtio version flags */ 58 #define VIRTIO_PCI_FLAG_DISABLE_PCIE (1 << VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT) 59 60 /* migrate extra state */ 61 #define VIRTIO_PCI_FLAG_MIGRATE_EXTRA (1 << VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT) 62 63 /* have pio notification for modern device ? */ 64 #define VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY \ 65 (1 << VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT) 66 67 /* page per vq flag to be used by split drivers within guests */ 68 #define VIRTIO_PCI_FLAG_PAGE_PER_VQ \ 69 (1 << VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT) 70 71 /* address space translation service */ 72 #define VIRTIO_PCI_FLAG_ATS (1 << VIRTIO_PCI_FLAG_ATS_BIT) 73 74 /* Init error enabling flags */ 75 #define VIRTIO_PCI_FLAG_INIT_DEVERR (1 << VIRTIO_PCI_FLAG_INIT_DEVERR_BIT) 76 77 /* Init Link Control register */ 78 #define VIRTIO_PCI_FLAG_INIT_LNKCTL (1 << VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT) 79 80 /* Init Power Management */ 81 #define VIRTIO_PCI_FLAG_INIT_PM (1 << VIRTIO_PCI_FLAG_INIT_PM_BIT) 82 83 typedef struct { 84 MSIMessage msg; 85 int virq; 86 unsigned int users; 87 } VirtIOIRQFD; 88 89 /* 90 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. 91 */ 92 #define TYPE_VIRTIO_PCI "virtio-pci" 93 #define VIRTIO_PCI_GET_CLASS(obj) \ 94 OBJECT_GET_CLASS(VirtioPCIClass, obj, TYPE_VIRTIO_PCI) 95 #define VIRTIO_PCI_CLASS(klass) \ 96 OBJECT_CLASS_CHECK(VirtioPCIClass, klass, TYPE_VIRTIO_PCI) 97 #define VIRTIO_PCI(obj) \ 98 OBJECT_CHECK(VirtIOPCIProxy, (obj), TYPE_VIRTIO_PCI) 99 100 typedef struct VirtioPCIClass { 101 PCIDeviceClass parent_class; 102 DeviceRealize parent_dc_realize; 103 void (*realize)(VirtIOPCIProxy *vpci_dev, Error **errp); 104 } VirtioPCIClass; 105 106 typedef struct VirtIOPCIRegion { 107 MemoryRegion mr; 108 uint32_t offset; 109 uint32_t size; 110 uint32_t type; 111 } VirtIOPCIRegion; 112 113 typedef struct VirtIOPCIQueue { 114 uint16_t num; 115 bool enabled; 116 uint32_t desc[2]; 117 uint32_t avail[2]; 118 uint32_t used[2]; 119 } VirtIOPCIQueue; 120 121 struct VirtIOPCIProxy { 122 PCIDevice pci_dev; 123 MemoryRegion bar; 124 union { 125 struct { 126 VirtIOPCIRegion common; 127 VirtIOPCIRegion isr; 128 VirtIOPCIRegion device; 129 VirtIOPCIRegion notify; 130 VirtIOPCIRegion notify_pio; 131 }; 132 VirtIOPCIRegion regs[5]; 133 }; 134 MemoryRegion modern_bar; 135 MemoryRegion io_bar; 136 uint32_t legacy_io_bar_idx; 137 uint32_t msix_bar_idx; 138 uint32_t modern_io_bar_idx; 139 uint32_t modern_mem_bar_idx; 140 int config_cap; 141 uint32_t flags; 142 bool disable_modern; 143 bool ignore_backend_features; 144 OnOffAuto disable_legacy; 145 uint32_t class_code; 146 uint32_t nvectors; 147 uint32_t dfselect; 148 uint32_t gfselect; 149 uint32_t guest_features[2]; 150 VirtIOPCIQueue vqs[VIRTIO_QUEUE_MAX]; 151 152 VirtIOIRQFD *vector_irqfd; 153 int nvqs_with_notifiers; 154 VirtioBusState bus; 155 }; 156 157 static inline bool virtio_pci_modern(VirtIOPCIProxy *proxy) 158 { 159 return !proxy->disable_modern; 160 } 161 162 static inline bool virtio_pci_legacy(VirtIOPCIProxy *proxy) 163 { 164 return proxy->disable_legacy == ON_OFF_AUTO_OFF; 165 } 166 167 static inline void virtio_pci_force_virtio_1(VirtIOPCIProxy *proxy) 168 { 169 proxy->disable_modern = false; 170 proxy->disable_legacy = ON_OFF_AUTO_ON; 171 } 172 173 static inline void virtio_pci_disable_modern(VirtIOPCIProxy *proxy) 174 { 175 proxy->disable_modern = true; 176 } 177 178 /* 179 * virtio-input-pci: This extends VirtioPCIProxy. 180 */ 181 #define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci" 182 183 /* Virtio ABI version, if we increment this, we break the guest driver. */ 184 #define VIRTIO_PCI_ABI_VERSION 0 185 186 /* Input for virtio_pci_types_register() */ 187 typedef struct VirtioPCIDeviceTypeInfo { 188 /* 189 * Common base class for the subclasses below. 190 * 191 * Required only if transitional_name or non_transitional_name is set. 192 * 193 * We need a separate base type instead of making all types 194 * inherit from generic_name for two reasons: 195 * 1) generic_name implements INTERFACE_PCIE_DEVICE, but 196 * transitional_name does not. 197 * 2) generic_name has the "disable-legacy" and "disable-modern" 198 * properties, transitional_name and non_transitional name don't. 199 */ 200 const char *base_name; 201 /* 202 * Generic device type. Optional. 203 * 204 * Supports both transitional and non-transitional modes, 205 * using the disable-legacy and disable-modern properties. 206 * If disable-legacy=auto, (non-)transitional mode is selected 207 * depending on the bus where the device is plugged. 208 * 209 * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE, 210 * but PCI Express is supported only in non-transitional mode. 211 * 212 * The only type implemented by QEMU 3.1 and older. 213 */ 214 const char *generic_name; 215 /* 216 * The transitional device type. Optional. 217 * 218 * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE. 219 */ 220 const char *transitional_name; 221 /* 222 * The non-transitional device type. Optional. 223 * 224 * Implements INTERFACE_CONVENTIONAL_PCI_DEVICE only. 225 */ 226 const char *non_transitional_name; 227 228 /* Parent type. If NULL, TYPE_VIRTIO_PCI is used */ 229 const char *parent; 230 231 /* Same as TypeInfo fields: */ 232 size_t instance_size; 233 size_t class_size; 234 void (*instance_init)(Object *obj); 235 void (*class_init)(ObjectClass *klass, void *data); 236 InterfaceInfo *interfaces; 237 } VirtioPCIDeviceTypeInfo; 238 239 /* Register virtio-pci type(s). @t must be static. */ 240 void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t); 241 242 #endif 243