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 #include "qom/object.h" 21 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 DECLARE_OBJ_CHECKERS(VirtioPCIBusState, VirtioPCIBusClass, 30 VIRTIO_PCI_BUS, TYPE_VIRTIO_PCI_BUS) 31 32 enum { 33 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, 34 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, 35 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, 36 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, 37 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, 38 VIRTIO_PCI_FLAG_ATS_BIT, 39 VIRTIO_PCI_FLAG_INIT_DEVERR_BIT, 40 VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, 41 VIRTIO_PCI_FLAG_INIT_PM_BIT, 42 VIRTIO_PCI_FLAG_INIT_FLR_BIT, 43 VIRTIO_PCI_FLAG_AER_BIT, 44 VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT, 45 VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET_BIT, 46 }; 47 48 /* Need to activate work-arounds for buggy guests at vmstate load. */ 49 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \ 50 (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT) 51 52 /* Performance improves when virtqueue kick processing is decoupled from the 53 * vcpu thread using ioeventfd for some devices. */ 54 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT) 55 56 /* virtio version flags */ 57 #define VIRTIO_PCI_FLAG_DISABLE_PCIE (1 << VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT) 58 59 /* have pio notification for modern device ? */ 60 #define VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY \ 61 (1 << VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT) 62 63 /* page per vq flag to be used by split drivers within guests */ 64 #define VIRTIO_PCI_FLAG_PAGE_PER_VQ \ 65 (1 << VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT) 66 67 /* address space translation service */ 68 #define VIRTIO_PCI_FLAG_ATS (1 << VIRTIO_PCI_FLAG_ATS_BIT) 69 70 /* Init error enabling flags */ 71 #define VIRTIO_PCI_FLAG_INIT_DEVERR (1 << VIRTIO_PCI_FLAG_INIT_DEVERR_BIT) 72 73 /* Init Link Control register */ 74 #define VIRTIO_PCI_FLAG_INIT_LNKCTL (1 << VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT) 75 76 /* Init Power Management */ 77 #define VIRTIO_PCI_FLAG_INIT_PM (1 << VIRTIO_PCI_FLAG_INIT_PM_BIT) 78 79 /* Init The No_Soft_Reset bit of Power Management */ 80 #define VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET \ 81 (1 << VIRTIO_PCI_FLAG_PM_NO_SOFT_RESET_BIT) 82 83 /* Init Function Level Reset capability */ 84 #define VIRTIO_PCI_FLAG_INIT_FLR (1 << VIRTIO_PCI_FLAG_INIT_FLR_BIT) 85 86 /* Advanced Error Reporting capability */ 87 #define VIRTIO_PCI_FLAG_AER (1 << VIRTIO_PCI_FLAG_AER_BIT) 88 89 /* Page Aligned Address space Translation Service */ 90 #define VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED \ 91 (1 << VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT) 92 93 typedef struct { 94 MSIMessage msg; 95 int virq; 96 unsigned int users; 97 } VirtIOIRQFD; 98 99 /* 100 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. 101 */ 102 #define TYPE_VIRTIO_PCI "virtio-pci" 103 OBJECT_DECLARE_TYPE(VirtIOPCIProxy, VirtioPCIClass, VIRTIO_PCI) 104 105 struct VirtioPCIClass { 106 PCIDeviceClass parent_class; 107 DeviceRealize parent_dc_realize; 108 void (*realize)(VirtIOPCIProxy *vpci_dev, Error **errp); 109 }; 110 111 typedef struct VirtIOPCIRegion { 112 MemoryRegion mr; 113 uint32_t offset; 114 uint32_t size; 115 uint32_t type; 116 } VirtIOPCIRegion; 117 118 typedef struct VirtIOPCIQueue { 119 uint16_t num; 120 bool enabled; 121 /* 122 * No need to migrate the reset status, because it is always 0 123 * when the migration starts. 124 */ 125 bool reset; 126 uint32_t desc[2]; 127 uint32_t avail[2]; 128 uint32_t used[2]; 129 } VirtIOPCIQueue; 130 131 struct VirtIOPCIProxy { 132 PCIDevice pci_dev; 133 MemoryRegion bar; 134 union { 135 struct { 136 VirtIOPCIRegion common; 137 VirtIOPCIRegion isr; 138 VirtIOPCIRegion device; 139 VirtIOPCIRegion notify; 140 VirtIOPCIRegion notify_pio; 141 }; 142 VirtIOPCIRegion regs[5]; 143 }; 144 MemoryRegion modern_bar; 145 MemoryRegion io_bar; 146 /* address space for VirtIOPCIRegions */ 147 AddressSpace modern_cfg_mem_as; 148 AddressSpace modern_cfg_io_as; 149 uint32_t legacy_io_bar_idx; 150 uint32_t msix_bar_idx; 151 uint32_t modern_io_bar_idx; 152 uint32_t modern_mem_bar_idx; 153 int config_cap; 154 uint16_t last_pcie_cap_offset; 155 uint32_t flags; 156 bool disable_modern; 157 bool ignore_backend_features; 158 OnOffAuto disable_legacy; 159 /* Transitional device id */ 160 uint16_t trans_devid; 161 uint32_t class_code; 162 uint32_t nvectors; 163 uint32_t dfselect; 164 uint32_t gfselect; 165 uint32_t guest_features[2]; 166 VirtIOPCIQueue vqs[VIRTIO_QUEUE_MAX]; 167 168 VirtIOIRQFD *vector_irqfd; 169 int nvqs_with_notifiers; 170 VirtioBusState bus; 171 }; 172 173 static inline bool virtio_pci_modern(VirtIOPCIProxy *proxy) 174 { 175 return !proxy->disable_modern; 176 } 177 178 static inline bool virtio_pci_legacy(VirtIOPCIProxy *proxy) 179 { 180 return proxy->disable_legacy == ON_OFF_AUTO_OFF; 181 } 182 183 static inline void virtio_pci_force_virtio_1(VirtIOPCIProxy *proxy) 184 { 185 proxy->disable_modern = false; 186 proxy->disable_legacy = ON_OFF_AUTO_ON; 187 } 188 189 static inline void virtio_pci_disable_modern(VirtIOPCIProxy *proxy) 190 { 191 proxy->disable_modern = true; 192 } 193 194 uint16_t virtio_pci_get_trans_devid(uint16_t device_id); 195 uint16_t virtio_pci_get_class_id(uint16_t device_id); 196 197 /* 198 * virtio-input-pci: This extends VirtioPCIProxy. 199 */ 200 #define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci" 201 202 /* Virtio ABI version, if we increment this, we break the guest driver. */ 203 #define VIRTIO_PCI_ABI_VERSION 0 204 205 /* Input for virtio_pci_types_register() */ 206 typedef struct VirtioPCIDeviceTypeInfo { 207 /* 208 * Common base class for the subclasses below. 209 * 210 * Required only if transitional_name or non_transitional_name is set. 211 * 212 * We need a separate base type instead of making all types 213 * inherit from generic_name for two reasons: 214 * 1) generic_name implements INTERFACE_PCIE_DEVICE, but 215 * transitional_name does not. 216 * 2) generic_name has the "disable-legacy" and "disable-modern" 217 * properties, transitional_name and non_transitional name don't. 218 */ 219 const char *base_name; 220 /* 221 * Generic device type. Optional. 222 * 223 * Supports both transitional and non-transitional modes, 224 * using the disable-legacy and disable-modern properties. 225 * If disable-legacy=auto, (non-)transitional mode is selected 226 * depending on the bus where the device is plugged. 227 * 228 * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE, 229 * but PCI Express is supported only in non-transitional mode. 230 * 231 * The only type implemented by QEMU 3.1 and older. 232 */ 233 const char *generic_name; 234 /* 235 * The transitional device type. Optional. 236 * 237 * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE. 238 */ 239 const char *transitional_name; 240 /* 241 * The non-transitional device type. Optional. 242 * 243 * Implements INTERFACE_CONVENTIONAL_PCI_DEVICE only. 244 */ 245 const char *non_transitional_name; 246 247 /* Parent type. If NULL, TYPE_VIRTIO_PCI is used */ 248 const char *parent; 249 250 /* Same as TypeInfo fields: */ 251 size_t instance_size; 252 size_t class_size; 253 void (*instance_init)(Object *obj); 254 void (*instance_finalize)(Object *obj); 255 void (*class_init)(ObjectClass *klass, const void *data); 256 const InterfaceInfo *interfaces; 257 } VirtioPCIDeviceTypeInfo; 258 259 /* Register virtio-pci type(s). @t must be static. */ 260 void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t); 261 262 /** 263 * virtio_pci_optimal_num_queues: 264 * @fixed_queues: number of queues that are always present 265 * 266 * Returns: The optimal number of queues for a multi-queue device, excluding 267 * @fixed_queues. 268 */ 269 unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues); 270 void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, VirtQueue *vq, 271 int n, bool assign, 272 bool with_irqfd); 273 274 int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy, uint8_t bar, uint64_t offset, 275 uint64_t length, uint8_t id); 276 277 #endif 278