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