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