1 #ifndef QEMU_PCI_DEVICE_H 2 #define QEMU_PCI_DEVICE_H 3 4 #include "hw/pci/pci.h" 5 #include "hw/pci/pcie.h" 6 7 #define TYPE_PCI_DEVICE "pci-device" 8 typedef struct PCIDeviceClass PCIDeviceClass; 9 DECLARE_OBJ_CHECKERS(PCIDevice, PCIDeviceClass, 10 PCI_DEVICE, TYPE_PCI_DEVICE) 11 12 /* 13 * Implemented by devices that can be plugged on CXL buses. In the spec, this is 14 * actually a "CXL Component, but we name it device to match the PCI naming. 15 */ 16 #define INTERFACE_CXL_DEVICE "cxl-device" 17 18 /* Implemented by devices that can be plugged on PCI Express buses */ 19 #define INTERFACE_PCIE_DEVICE "pci-express-device" 20 21 /* Implemented by devices that can be plugged on Conventional PCI buses */ 22 #define INTERFACE_CONVENTIONAL_PCI_DEVICE "conventional-pci-device" 23 24 struct PCIDeviceClass { 25 DeviceClass parent_class; 26 27 void (*realize)(PCIDevice *dev, Error **errp); 28 PCIUnregisterFunc *exit; 29 PCIConfigReadFunc *config_read; 30 PCIConfigWriteFunc *config_write; 31 32 uint16_t vendor_id; 33 uint16_t device_id; 34 uint8_t revision; 35 uint16_t class_id; 36 uint16_t subsystem_vendor_id; /* only for header type = 0 */ 37 uint16_t subsystem_id; /* only for header type = 0 */ 38 39 const char *romfile; /* rom bar */ 40 41 bool sriov_vf_user_creatable; 42 }; 43 44 enum PCIReqIDType { 45 PCI_REQ_ID_INVALID = 0, 46 PCI_REQ_ID_BDF, 47 PCI_REQ_ID_SECONDARY_BUS, 48 PCI_REQ_ID_MAX, 49 }; 50 typedef enum PCIReqIDType PCIReqIDType; 51 52 struct PCIReqIDCache { 53 PCIDevice *dev; 54 PCIReqIDType type; 55 }; 56 typedef struct PCIReqIDCache PCIReqIDCache; 57 58 struct PCIDevice { 59 DeviceState qdev; 60 bool partially_hotplugged; 61 bool enabled; 62 63 /* PCI config space */ 64 uint8_t *config; 65 66 /* 67 * Used to enable config checks on load. Note that writable bits are 68 * never checked even if set in cmask. 69 */ 70 uint8_t *cmask; 71 72 /* Used to implement R/W bytes */ 73 uint8_t *wmask; 74 75 /* Used to implement RW1C(Write 1 to Clear) bytes */ 76 uint8_t *w1cmask; 77 78 /* Used to allocate config space for capabilities. */ 79 uint8_t *used; 80 81 /* the following fields are read only */ 82 int32_t devfn; 83 /* 84 * Cached device to fetch requester ID from, to avoid the PCI tree 85 * walking every time we invoke PCI request (e.g., MSI). For 86 * conventional PCI root complex, this field is meaningless. 87 */ 88 PCIReqIDCache requester_id_cache; 89 char name[64]; 90 PCIIORegion io_regions[PCI_NUM_REGIONS]; 91 AddressSpace bus_master_as; 92 MemoryRegion bus_master_container_region; 93 MemoryRegion bus_master_enable_region; 94 95 /* do not access the following fields */ 96 PCIConfigReadFunc *config_read; 97 PCIConfigWriteFunc *config_write; 98 99 /* Legacy PCI VGA regions */ 100 MemoryRegion *vga_regions[QEMU_PCI_VGA_NUM_REGIONS]; 101 bool has_vga; 102 103 /* Current IRQ levels. Used internally by the generic PCI code. */ 104 uint8_t irq_state; 105 106 /* Capability bits */ 107 uint32_t cap_present; 108 109 /* Offset of MSI-X capability in config space */ 110 uint8_t msix_cap; 111 112 /* MSI-X entries */ 113 int msix_entries_nr; 114 115 /* Space to store MSIX table & pending bit array */ 116 uint8_t *msix_table; 117 uint8_t *msix_pba; 118 119 /* May be used by INTx or MSI during interrupt notification */ 120 void *irq_opaque; 121 122 MSITriggerFunc *msi_trigger; 123 MSIPrepareMessageFunc *msi_prepare_message; 124 MSIxPrepareMessageFunc *msix_prepare_message; 125 126 /* MemoryRegion container for msix exclusive BAR setup */ 127 MemoryRegion msix_exclusive_bar; 128 /* Memory Regions for MSIX table and pending bit entries. */ 129 MemoryRegion msix_table_mmio; 130 MemoryRegion msix_pba_mmio; 131 /* Reference-count for entries actually in use by driver. */ 132 unsigned *msix_entry_used; 133 /* MSIX function mask set or MSIX disabled */ 134 bool msix_function_masked; 135 /* Version id needed for VMState */ 136 int32_t version_id; 137 138 /* Offset of MSI capability in config space */ 139 uint8_t msi_cap; 140 141 /* PCI Express */ 142 PCIExpressDevice exp; 143 144 /* SHPC */ 145 SHPCDevice *shpc; 146 147 /* Location of option rom */ 148 char *romfile; 149 uint32_t romsize; 150 bool has_rom; 151 MemoryRegion rom; 152 uint32_t rom_bar; 153 154 /* INTx routing notifier */ 155 PCIINTxRoutingNotifier intx_routing_notifier; 156 157 /* MSI-X notifiers */ 158 MSIVectorUseNotifier msix_vector_use_notifier; 159 MSIVectorReleaseNotifier msix_vector_release_notifier; 160 MSIVectorPollNotifier msix_vector_poll_notifier; 161 162 /* ID of standby device in net_failover pair */ 163 char *failover_pair_id; 164 uint32_t acpi_index; 165 166 char *sriov_pf; 167 }; 168 169 static inline int pci_intx(PCIDevice *pci_dev) 170 { 171 return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1; 172 } 173 174 static inline int pci_is_cxl(const PCIDevice *d) 175 { 176 return d->cap_present & QEMU_PCIE_CAP_CXL; 177 } 178 179 static inline int pci_is_express(const PCIDevice *d) 180 { 181 return d->cap_present & QEMU_PCI_CAP_EXPRESS; 182 } 183 184 static inline int pci_is_express_downstream_port(const PCIDevice *d) 185 { 186 uint8_t type; 187 188 if (!pci_is_express(d) || !d->exp.exp_cap) { 189 return 0; 190 } 191 192 type = pcie_cap_get_type(d); 193 194 return type == PCI_EXP_TYPE_DOWNSTREAM || type == PCI_EXP_TYPE_ROOT_PORT; 195 } 196 197 static inline int pci_is_vf(const PCIDevice *d) 198 { 199 return d->sriov_pf || d->exp.sriov_vf.pf != NULL; 200 } 201 202 static inline uint32_t pci_config_size(const PCIDevice *d) 203 { 204 return pci_is_express(d) ? PCIE_CONFIG_SPACE_SIZE : PCI_CONFIG_SPACE_SIZE; 205 } 206 207 static inline uint16_t pci_get_bdf(PCIDevice *dev) 208 { 209 return PCI_BUILD_BDF(pci_bus_num(pci_get_bus(dev)), dev->devfn); 210 } 211 212 static inline void pci_set_power(PCIDevice *pci_dev, bool state) 213 { 214 /* 215 * Don't change the enabled state of VFs when powering on/off the device. 216 * 217 * When powering on, VFs must not be enabled immediately but they must 218 * wait until the guest configures SR-IOV. 219 * When powering off, their corresponding PFs will be reset and disable 220 * VFs. 221 */ 222 if (!pci_is_vf(pci_dev)) { 223 pci_set_enabled(pci_dev, state); 224 } 225 } 226 227 uint16_t pci_requester_id(PCIDevice *dev); 228 229 /* DMA access functions */ 230 static inline AddressSpace *pci_get_address_space(PCIDevice *dev) 231 { 232 return &dev->bus_master_as; 233 } 234 235 /** 236 * pci_dma_rw: Read from or write to an address space from PCI device. 237 * 238 * Return a MemTxResult indicating whether the operation succeeded 239 * or failed (eg unassigned memory, device rejected the transaction, 240 * IOMMU fault). 241 * 242 * @dev: #PCIDevice doing the memory access 243 * @addr: address within the #PCIDevice address space 244 * @buf: buffer with the data transferred 245 * @len: the number of bytes to read or write 246 * @dir: indicates the transfer direction 247 */ 248 static inline MemTxResult pci_dma_rw(PCIDevice *dev, dma_addr_t addr, 249 void *buf, dma_addr_t len, 250 DMADirection dir, MemTxAttrs attrs) 251 { 252 return dma_memory_rw(pci_get_address_space(dev), addr, buf, len, 253 dir, attrs); 254 } 255 256 /** 257 * pci_dma_read: Read from an address space from PCI device. 258 * 259 * Return a MemTxResult indicating whether the operation succeeded 260 * or failed (eg unassigned memory, device rejected the transaction, 261 * IOMMU fault). Called within RCU critical section. 262 * 263 * @dev: #PCIDevice doing the memory access 264 * @addr: address within the #PCIDevice address space 265 * @buf: buffer with the data transferred 266 * @len: length of the data transferred 267 */ 268 static inline MemTxResult pci_dma_read(PCIDevice *dev, dma_addr_t addr, 269 void *buf, dma_addr_t len) 270 { 271 return pci_dma_rw(dev, addr, buf, len, 272 DMA_DIRECTION_TO_DEVICE, MEMTXATTRS_UNSPECIFIED); 273 } 274 275 /** 276 * pci_dma_write: Write to address space from PCI device. 277 * 278 * Return a MemTxResult indicating whether the operation succeeded 279 * or failed (eg unassigned memory, device rejected the transaction, 280 * IOMMU fault). 281 * 282 * @dev: #PCIDevice doing the memory access 283 * @addr: address within the #PCIDevice address space 284 * @buf: buffer with the data transferred 285 * @len: the number of bytes to write 286 */ 287 static inline MemTxResult pci_dma_write(PCIDevice *dev, dma_addr_t addr, 288 const void *buf, dma_addr_t len) 289 { 290 return pci_dma_rw(dev, addr, (void *) buf, len, 291 DMA_DIRECTION_FROM_DEVICE, MEMTXATTRS_UNSPECIFIED); 292 } 293 294 #define PCI_DMA_DEFINE_LDST(_l, _s, _bits) \ 295 static inline MemTxResult ld##_l##_pci_dma(PCIDevice *dev, \ 296 dma_addr_t addr, \ 297 uint##_bits##_t *val, \ 298 MemTxAttrs attrs) \ 299 { \ 300 return ld##_l##_dma(pci_get_address_space(dev), addr, val, attrs); \ 301 } \ 302 static inline MemTxResult st##_s##_pci_dma(PCIDevice *dev, \ 303 dma_addr_t addr, \ 304 uint##_bits##_t val, \ 305 MemTxAttrs attrs) \ 306 { \ 307 return st##_s##_dma(pci_get_address_space(dev), addr, val, attrs); \ 308 } 309 310 PCI_DMA_DEFINE_LDST(ub, b, 8); 311 PCI_DMA_DEFINE_LDST(uw_le, w_le, 16) 312 PCI_DMA_DEFINE_LDST(l_le, l_le, 32); 313 PCI_DMA_DEFINE_LDST(q_le, q_le, 64); 314 PCI_DMA_DEFINE_LDST(uw_be, w_be, 16) 315 PCI_DMA_DEFINE_LDST(l_be, l_be, 32); 316 PCI_DMA_DEFINE_LDST(q_be, q_be, 64); 317 318 #undef PCI_DMA_DEFINE_LDST 319 320 /** 321 * pci_dma_map: Map device PCI address space range into host virtual address 322 * @dev: #PCIDevice to be accessed 323 * @addr: address within that device's address space 324 * @plen: pointer to length of buffer; updated on return to indicate 325 * if only a subset of the requested range has been mapped 326 * @dir: indicates the transfer direction 327 * 328 * Return: A host pointer, or %NULL if the resources needed to 329 * perform the mapping are exhausted (in that case *@plen 330 * is set to zero). 331 */ 332 static inline void *pci_dma_map(PCIDevice *dev, dma_addr_t addr, 333 dma_addr_t *plen, DMADirection dir) 334 { 335 return dma_memory_map(pci_get_address_space(dev), addr, plen, dir, 336 MEMTXATTRS_UNSPECIFIED); 337 } 338 339 static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len, 340 DMADirection dir, dma_addr_t access_len) 341 { 342 dma_memory_unmap(pci_get_address_space(dev), buffer, len, dir, access_len); 343 } 344 345 static inline void pci_dma_sglist_init(QEMUSGList *qsg, PCIDevice *dev, 346 int alloc_hint) 347 { 348 qemu_sglist_init(qsg, DEVICE(dev), alloc_hint, pci_get_address_space(dev)); 349 } 350 351 extern const VMStateDescription vmstate_pci_device; 352 353 #define VMSTATE_PCI_DEVICE(_field, _state) { \ 354 .name = (stringify(_field)), \ 355 .size = sizeof(PCIDevice), \ 356 .vmsd = &vmstate_pci_device, \ 357 .flags = VMS_STRUCT, \ 358 .offset = vmstate_offset_value(_state, _field, PCIDevice), \ 359 } 360 361 #define VMSTATE_PCI_DEVICE_POINTER(_field, _state) { \ 362 .name = (stringify(_field)), \ 363 .size = sizeof(PCIDevice), \ 364 .vmsd = &vmstate_pci_device, \ 365 .flags = VMS_STRUCT | VMS_POINTER, \ 366 .offset = vmstate_offset_pointer(_state, _field, PCIDevice), \ 367 } 368 369 #endif 370