pci_device.h 11 KB

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