tests/qapi-schema: Tidy up pylint warnings and advice
[qemu/armbru.git] / include / hw / pci / pci_device.h
blobd3dd0f64b273ad1d192e43afd27c020a414f0a6b
1 #ifndef QEMU_PCI_DEVICE_H
2 #define QEMU_PCI_DEVICE_H
4 #include "hw/pci/pci.h"
5 #include "hw/pci/pcie.h"
7 #define TYPE_PCI_DEVICE "pci-device"
8 typedef struct PCIDeviceClass PCIDeviceClass;
9 DECLARE_OBJ_CHECKERS(PCIDevice, PCIDeviceClass,
10 PCI_DEVICE, TYPE_PCI_DEVICE)
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.
16 #define INTERFACE_CXL_DEVICE "cxl-device"
18 /* Implemented by devices that can be plugged on PCI Express buses */
19 #define INTERFACE_PCIE_DEVICE "pci-express-device"
21 /* Implemented by devices that can be plugged on Conventional PCI buses */
22 #define INTERFACE_CONVENTIONAL_PCI_DEVICE "conventional-pci-device"
24 struct PCIDeviceClass {
25 DeviceClass parent_class;
27 void (*realize)(PCIDevice *dev, Error **errp);
28 PCIUnregisterFunc *exit;
29 PCIConfigReadFunc *config_read;
30 PCIConfigWriteFunc *config_write;
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 */
39 const char *romfile; /* rom bar */
42 enum PCIReqIDType {
43 PCI_REQ_ID_INVALID = 0,
44 PCI_REQ_ID_BDF,
45 PCI_REQ_ID_SECONDARY_BUS,
46 PCI_REQ_ID_MAX,
48 typedef enum PCIReqIDType PCIReqIDType;
50 struct PCIReqIDCache {
51 PCIDevice *dev;
52 PCIReqIDType type;
54 typedef struct PCIReqIDCache PCIReqIDCache;
56 struct PCIDevice {
57 DeviceState qdev;
58 bool partially_hotplugged;
59 bool has_power;
61 /* PCI config space */
62 uint8_t *config;
65 * Used to enable config checks on load. Note that writable bits are
66 * never checked even if set in cmask.
68 uint8_t *cmask;
70 /* Used to implement R/W bytes */
71 uint8_t *wmask;
73 /* Used to implement RW1C(Write 1 to Clear) bytes */
74 uint8_t *w1cmask;
76 /* Used to allocate config space for capabilities. */
77 uint8_t *used;
79 /* the following fields are read only */
80 int32_t devfn;
82 * Cached device to fetch requester ID from, to avoid the PCI tree
83 * walking every time we invoke PCI request (e.g., MSI). For
84 * conventional PCI root complex, this field is meaningless.
86 PCIReqIDCache requester_id_cache;
87 char name[64];
88 PCIIORegion io_regions[PCI_NUM_REGIONS];
89 AddressSpace bus_master_as;
90 MemoryRegion bus_master_container_region;
91 MemoryRegion bus_master_enable_region;
93 /* do not access the following fields */
94 PCIConfigReadFunc *config_read;
95 PCIConfigWriteFunc *config_write;
97 /* Legacy PCI VGA regions */
98 MemoryRegion *vga_regions[QEMU_PCI_VGA_NUM_REGIONS];
99 bool has_vga;
101 /* Current IRQ levels. Used internally by the generic PCI code. */
102 uint8_t irq_state;
104 /* Capability bits */
105 uint32_t cap_present;
107 /* Offset of MSI-X capability in config space */
108 uint8_t msix_cap;
110 /* MSI-X entries */
111 int msix_entries_nr;
113 /* Space to store MSIX table & pending bit array */
114 uint8_t *msix_table;
115 uint8_t *msix_pba;
117 /* May be used by INTx or MSI during interrupt notification */
118 void *irq_opaque;
120 MSITriggerFunc *msi_trigger;
121 MSIPrepareMessageFunc *msi_prepare_message;
122 MSIxPrepareMessageFunc *msix_prepare_message;
124 /* MemoryRegion container for msix exclusive BAR setup */
125 MemoryRegion msix_exclusive_bar;
126 /* Memory Regions for MSIX table and pending bit entries. */
127 MemoryRegion msix_table_mmio;
128 MemoryRegion msix_pba_mmio;
129 /* Reference-count for entries actually in use by driver. */
130 unsigned *msix_entry_used;
131 /* MSIX function mask set or MSIX disabled */
132 bool msix_function_masked;
133 /* Version id needed for VMState */
134 int32_t version_id;
136 /* Offset of MSI capability in config space */
137 uint8_t msi_cap;
139 /* PCI Express */
140 PCIExpressDevice exp;
142 /* SHPC */
143 SHPCDevice *shpc;
145 /* Location of option rom */
146 char *romfile;
147 uint32_t romsize;
148 bool has_rom;
149 MemoryRegion rom;
150 uint32_t rom_bar;
152 /* INTx routing notifier */
153 PCIINTxRoutingNotifier intx_routing_notifier;
155 /* MSI-X notifiers */
156 MSIVectorUseNotifier msix_vector_use_notifier;
157 MSIVectorReleaseNotifier msix_vector_release_notifier;
158 MSIVectorPollNotifier msix_vector_poll_notifier;
160 /* ID of standby device in net_failover pair */
161 char *failover_pair_id;
162 uint32_t acpi_index;
165 static inline int pci_intx(PCIDevice *pci_dev)
167 return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
170 static inline int pci_is_cxl(const PCIDevice *d)
172 return d->cap_present & QEMU_PCIE_CAP_CXL;
175 static inline int pci_is_express(const PCIDevice *d)
177 return d->cap_present & QEMU_PCI_CAP_EXPRESS;
180 static inline int pci_is_express_downstream_port(const PCIDevice *d)
182 uint8_t type;
184 if (!pci_is_express(d) || !d->exp.exp_cap) {
185 return 0;
188 type = pcie_cap_get_type(d);
190 return type == PCI_EXP_TYPE_DOWNSTREAM || type == PCI_EXP_TYPE_ROOT_PORT;
193 static inline int pci_is_vf(const PCIDevice *d)
195 return d->exp.sriov_vf.pf != NULL;
198 static inline uint32_t pci_config_size(const PCIDevice *d)
200 return pci_is_express(d) ? PCIE_CONFIG_SPACE_SIZE : PCI_CONFIG_SPACE_SIZE;
203 static inline uint16_t pci_get_bdf(PCIDevice *dev)
205 return PCI_BUILD_BDF(pci_bus_num(pci_get_bus(dev)), dev->devfn);
208 uint16_t pci_requester_id(PCIDevice *dev);
210 /* DMA access functions */
211 static inline AddressSpace *pci_get_address_space(PCIDevice *dev)
213 return &dev->bus_master_as;
217 * pci_dma_rw: Read from or write to an address space from PCI device.
219 * Return a MemTxResult indicating whether the operation succeeded
220 * or failed (eg unassigned memory, device rejected the transaction,
221 * IOMMU fault).
223 * @dev: #PCIDevice doing the memory access
224 * @addr: address within the #PCIDevice address space
225 * @buf: buffer with the data transferred
226 * @len: the number of bytes to read or write
227 * @dir: indicates the transfer direction
229 static inline MemTxResult pci_dma_rw(PCIDevice *dev, dma_addr_t addr,
230 void *buf, dma_addr_t len,
231 DMADirection dir, MemTxAttrs attrs)
233 return dma_memory_rw(pci_get_address_space(dev), addr, buf, len,
234 dir, attrs);
238 * pci_dma_read: Read from an address space from PCI device.
240 * Return a MemTxResult indicating whether the operation succeeded
241 * or failed (eg unassigned memory, device rejected the transaction,
242 * IOMMU fault). Called within RCU critical section.
244 * @dev: #PCIDevice doing the memory access
245 * @addr: address within the #PCIDevice address space
246 * @buf: buffer with the data transferred
247 * @len: length of the data transferred
249 static inline MemTxResult pci_dma_read(PCIDevice *dev, dma_addr_t addr,
250 void *buf, dma_addr_t len)
252 return pci_dma_rw(dev, addr, buf, len,
253 DMA_DIRECTION_TO_DEVICE, MEMTXATTRS_UNSPECIFIED);
257 * pci_dma_write: Write to address space from PCI device.
259 * Return a MemTxResult indicating whether the operation succeeded
260 * or failed (eg unassigned memory, device rejected the transaction,
261 * IOMMU fault).
263 * @dev: #PCIDevice doing the memory access
264 * @addr: address within the #PCIDevice address space
265 * @buf: buffer with the data transferred
266 * @len: the number of bytes to write
268 static inline MemTxResult pci_dma_write(PCIDevice *dev, dma_addr_t addr,
269 const void *buf, dma_addr_t len)
271 return pci_dma_rw(dev, addr, (void *) buf, len,
272 DMA_DIRECTION_FROM_DEVICE, MEMTXATTRS_UNSPECIFIED);
275 #define PCI_DMA_DEFINE_LDST(_l, _s, _bits) \
276 static inline MemTxResult ld##_l##_pci_dma(PCIDevice *dev, \
277 dma_addr_t addr, \
278 uint##_bits##_t *val, \
279 MemTxAttrs attrs) \
281 return ld##_l##_dma(pci_get_address_space(dev), addr, val, attrs); \
283 static inline MemTxResult st##_s##_pci_dma(PCIDevice *dev, \
284 dma_addr_t addr, \
285 uint##_bits##_t val, \
286 MemTxAttrs attrs) \
288 return st##_s##_dma(pci_get_address_space(dev), addr, val, attrs); \
291 PCI_DMA_DEFINE_LDST(ub, b, 8);
292 PCI_DMA_DEFINE_LDST(uw_le, w_le, 16)
293 PCI_DMA_DEFINE_LDST(l_le, l_le, 32);
294 PCI_DMA_DEFINE_LDST(q_le, q_le, 64);
295 PCI_DMA_DEFINE_LDST(uw_be, w_be, 16)
296 PCI_DMA_DEFINE_LDST(l_be, l_be, 32);
297 PCI_DMA_DEFINE_LDST(q_be, q_be, 64);
299 #undef PCI_DMA_DEFINE_LDST
302 * pci_dma_map: Map device PCI address space range into host virtual address
303 * @dev: #PCIDevice to be accessed
304 * @addr: address within that device's address space
305 * @plen: pointer to length of buffer; updated on return to indicate
306 * if only a subset of the requested range has been mapped
307 * @dir: indicates the transfer direction
309 * Return: A host pointer, or %NULL if the resources needed to
310 * perform the mapping are exhausted (in that case *@plen
311 * is set to zero).
313 static inline void *pci_dma_map(PCIDevice *dev, dma_addr_t addr,
314 dma_addr_t *plen, DMADirection dir)
316 return dma_memory_map(pci_get_address_space(dev), addr, plen, dir,
317 MEMTXATTRS_UNSPECIFIED);
320 static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len,
321 DMADirection dir, dma_addr_t access_len)
323 dma_memory_unmap(pci_get_address_space(dev), buffer, len, dir, access_len);
326 static inline void pci_dma_sglist_init(QEMUSGList *qsg, PCIDevice *dev,
327 int alloc_hint)
329 qemu_sglist_init(qsg, DEVICE(dev), alloc_hint, pci_get_address_space(dev));
332 extern const VMStateDescription vmstate_pci_device;
334 #define VMSTATE_PCI_DEVICE(_field, _state) { \
335 .name = (stringify(_field)), \
336 .size = sizeof(PCIDevice), \
337 .vmsd = &vmstate_pci_device, \
338 .flags = VMS_STRUCT, \
339 .offset = vmstate_offset_value(_state, _field, PCIDevice), \
342 #define VMSTATE_PCI_DEVICE_POINTER(_field, _state) { \
343 .name = (stringify(_field)), \
344 .size = sizeof(PCIDevice), \
345 .vmsd = &vmstate_pci_device, \
346 .flags = VMS_STRUCT | VMS_POINTER, \
347 .offset = vmstate_offset_pointer(_state, _field, PCIDevice), \
350 #endif