xref: /openbmc/qemu/include/hw/pci/pci_device.h (revision b88cfee9)
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 
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,
47 };
48 typedef enum PCIReqIDType PCIReqIDType;
49 
50 struct PCIReqIDCache {
51     PCIDevice *dev;
52     PCIReqIDType type;
53 };
54 typedef struct PCIReqIDCache PCIReqIDCache;
55 
56 struct PCIDevice {
57     DeviceState qdev;
58     bool partially_hotplugged;
59     bool enabled;
60 
61     /* PCI config space */
62     uint8_t *config;
63 
64     /*
65      * Used to enable config checks on load. Note that writable bits are
66      * never checked even if set in cmask.
67      */
68     uint8_t *cmask;
69 
70     /* Used to implement R/W bytes */
71     uint8_t *wmask;
72 
73     /* Used to implement RW1C(Write 1 to Clear) bytes */
74     uint8_t *w1cmask;
75 
76     /* Used to allocate config space for capabilities. */
77     uint8_t *used;
78 
79     /* the following fields are read only */
80     int32_t devfn;
81     /*
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.
85      */
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;
92 
93     /* do not access the following fields */
94     PCIConfigReadFunc *config_read;
95     PCIConfigWriteFunc *config_write;
96 
97     /* Legacy PCI VGA regions */
98     MemoryRegion *vga_regions[QEMU_PCI_VGA_NUM_REGIONS];
99     bool has_vga;
100 
101     /* Current IRQ levels.  Used internally by the generic PCI code.  */
102     uint8_t irq_state;
103 
104     /* Capability bits */
105     uint32_t cap_present;
106 
107     /* Offset of MSI-X capability in config space */
108     uint8_t msix_cap;
109 
110     /* MSI-X entries */
111     int msix_entries_nr;
112 
113     /* Space to store MSIX table & pending bit array */
114     uint8_t *msix_table;
115     uint8_t *msix_pba;
116 
117     /* May be used by INTx or MSI during interrupt notification */
118     void *irq_opaque;
119 
120     MSITriggerFunc *msi_trigger;
121     MSIPrepareMessageFunc *msi_prepare_message;
122     MSIxPrepareMessageFunc *msix_prepare_message;
123 
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;
135 
136     /* Offset of MSI capability in config space */
137     uint8_t msi_cap;
138 
139     /* PCI Express */
140     PCIExpressDevice exp;
141 
142     /* SHPC */
143     SHPCDevice *shpc;
144 
145     /* Location of option rom */
146     char *romfile;
147     uint32_t romsize;
148     bool has_rom;
149     MemoryRegion rom;
150     uint32_t rom_bar;
151 
152     /* INTx routing notifier */
153     PCIINTxRoutingNotifier intx_routing_notifier;
154 
155     /* MSI-X notifiers */
156     MSIVectorUseNotifier msix_vector_use_notifier;
157     MSIVectorReleaseNotifier msix_vector_release_notifier;
158     MSIVectorPollNotifier msix_vector_poll_notifier;
159 
160     /* ID of standby device in net_failover pair */
161     char *failover_pair_id;
162     uint32_t acpi_index;
163 };
164 
165 static inline int pci_intx(PCIDevice *pci_dev)
166 {
167     return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
168 }
169 
170 static inline int pci_is_cxl(const PCIDevice *d)
171 {
172     return d->cap_present & QEMU_PCIE_CAP_CXL;
173 }
174 
175 static inline int pci_is_express(const PCIDevice *d)
176 {
177     return d->cap_present & QEMU_PCI_CAP_EXPRESS;
178 }
179 
180 static inline int pci_is_express_downstream_port(const PCIDevice *d)
181 {
182     uint8_t type;
183 
184     if (!pci_is_express(d) || !d->exp.exp_cap) {
185         return 0;
186     }
187 
188     type = pcie_cap_get_type(d);
189 
190     return type == PCI_EXP_TYPE_DOWNSTREAM || type == PCI_EXP_TYPE_ROOT_PORT;
191 }
192 
193 static inline int pci_is_vf(const PCIDevice *d)
194 {
195     return d->exp.sriov_vf.pf != NULL;
196 }
197 
198 static inline uint32_t pci_config_size(const PCIDevice *d)
199 {
200     return pci_is_express(d) ? PCIE_CONFIG_SPACE_SIZE : PCI_CONFIG_SPACE_SIZE;
201 }
202 
203 static inline uint16_t pci_get_bdf(PCIDevice *dev)
204 {
205     return PCI_BUILD_BDF(pci_bus_num(pci_get_bus(dev)), dev->devfn);
206 }
207 
208 static inline void pci_set_power(PCIDevice *pci_dev, bool state)
209 {
210     /*
211      * Don't change the enabled state of VFs when powering on/off the device.
212      *
213      * When powering on, VFs must not be enabled immediately but they must
214      * wait until the guest configures SR-IOV.
215      * When powering off, their corresponding PFs will be reset and disable
216      * VFs.
217      */
218     if (!pci_is_vf(pci_dev)) {
219         pci_set_enabled(pci_dev, state);
220     }
221 }
222 
223 uint16_t pci_requester_id(PCIDevice *dev);
224 
225 /* DMA access functions */
226 static inline AddressSpace *pci_get_address_space(PCIDevice *dev)
227 {
228     return &dev->bus_master_as;
229 }
230 
231 /**
232  * pci_dma_rw: Read from or write to an address space from PCI device.
233  *
234  * Return a MemTxResult indicating whether the operation succeeded
235  * or failed (eg unassigned memory, device rejected the transaction,
236  * IOMMU fault).
237  *
238  * @dev: #PCIDevice doing the memory access
239  * @addr: address within the #PCIDevice address space
240  * @buf: buffer with the data transferred
241  * @len: the number of bytes to read or write
242  * @dir: indicates the transfer direction
243  */
244 static inline MemTxResult pci_dma_rw(PCIDevice *dev, dma_addr_t addr,
245                                      void *buf, dma_addr_t len,
246                                      DMADirection dir, MemTxAttrs attrs)
247 {
248     return dma_memory_rw(pci_get_address_space(dev), addr, buf, len,
249                          dir, attrs);
250 }
251 
252 /**
253  * pci_dma_read: Read from an address space from PCI device.
254  *
255  * Return a MemTxResult indicating whether the operation succeeded
256  * or failed (eg unassigned memory, device rejected the transaction,
257  * IOMMU fault).  Called within RCU critical section.
258  *
259  * @dev: #PCIDevice doing the memory access
260  * @addr: address within the #PCIDevice address space
261  * @buf: buffer with the data transferred
262  * @len: length of the data transferred
263  */
264 static inline MemTxResult pci_dma_read(PCIDevice *dev, dma_addr_t addr,
265                                        void *buf, dma_addr_t len)
266 {
267     return pci_dma_rw(dev, addr, buf, len,
268                       DMA_DIRECTION_TO_DEVICE, MEMTXATTRS_UNSPECIFIED);
269 }
270 
271 /**
272  * pci_dma_write: Write to address space from PCI device.
273  *
274  * Return a MemTxResult indicating whether the operation succeeded
275  * or failed (eg unassigned memory, device rejected the transaction,
276  * IOMMU fault).
277  *
278  * @dev: #PCIDevice doing the memory access
279  * @addr: address within the #PCIDevice address space
280  * @buf: buffer with the data transferred
281  * @len: the number of bytes to write
282  */
283 static inline MemTxResult pci_dma_write(PCIDevice *dev, dma_addr_t addr,
284                                         const void *buf, dma_addr_t len)
285 {
286     return pci_dma_rw(dev, addr, (void *) buf, len,
287                       DMA_DIRECTION_FROM_DEVICE, MEMTXATTRS_UNSPECIFIED);
288 }
289 
290 #define PCI_DMA_DEFINE_LDST(_l, _s, _bits) \
291     static inline MemTxResult ld##_l##_pci_dma(PCIDevice *dev, \
292                                                dma_addr_t addr, \
293                                                uint##_bits##_t *val, \
294                                                MemTxAttrs attrs) \
295     { \
296         return ld##_l##_dma(pci_get_address_space(dev), addr, val, attrs); \
297     } \
298     static inline MemTxResult st##_s##_pci_dma(PCIDevice *dev, \
299                                                dma_addr_t addr, \
300                                                uint##_bits##_t val, \
301                                                MemTxAttrs attrs) \
302     { \
303         return st##_s##_dma(pci_get_address_space(dev), addr, val, attrs); \
304     }
305 
306 PCI_DMA_DEFINE_LDST(ub, b, 8);
307 PCI_DMA_DEFINE_LDST(uw_le, w_le, 16)
308 PCI_DMA_DEFINE_LDST(l_le, l_le, 32);
309 PCI_DMA_DEFINE_LDST(q_le, q_le, 64);
310 PCI_DMA_DEFINE_LDST(uw_be, w_be, 16)
311 PCI_DMA_DEFINE_LDST(l_be, l_be, 32);
312 PCI_DMA_DEFINE_LDST(q_be, q_be, 64);
313 
314 #undef PCI_DMA_DEFINE_LDST
315 
316 /**
317  * pci_dma_map: Map device PCI address space range into host virtual address
318  * @dev: #PCIDevice to be accessed
319  * @addr: address within that device's address space
320  * @plen: pointer to length of buffer; updated on return to indicate
321  *        if only a subset of the requested range has been mapped
322  * @dir: indicates the transfer direction
323  *
324  * Return: A host pointer, or %NULL if the resources needed to
325  *         perform the mapping are exhausted (in that case *@plen
326  *         is set to zero).
327  */
328 static inline void *pci_dma_map(PCIDevice *dev, dma_addr_t addr,
329                                 dma_addr_t *plen, DMADirection dir)
330 {
331     return dma_memory_map(pci_get_address_space(dev), addr, plen, dir,
332                           MEMTXATTRS_UNSPECIFIED);
333 }
334 
335 static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len,
336                                  DMADirection dir, dma_addr_t access_len)
337 {
338     dma_memory_unmap(pci_get_address_space(dev), buffer, len, dir, access_len);
339 }
340 
341 static inline void pci_dma_sglist_init(QEMUSGList *qsg, PCIDevice *dev,
342                                        int alloc_hint)
343 {
344     qemu_sglist_init(qsg, DEVICE(dev), alloc_hint, pci_get_address_space(dev));
345 }
346 
347 extern const VMStateDescription vmstate_pci_device;
348 
349 #define VMSTATE_PCI_DEVICE(_field, _state) {                         \
350     .name       = (stringify(_field)),                               \
351     .size       = sizeof(PCIDevice),                                 \
352     .vmsd       = &vmstate_pci_device,                               \
353     .flags      = VMS_STRUCT,                                        \
354     .offset     = vmstate_offset_value(_state, _field, PCIDevice),   \
355 }
356 
357 #define VMSTATE_PCI_DEVICE_POINTER(_field, _state) {                 \
358     .name       = (stringify(_field)),                               \
359     .size       = sizeof(PCIDevice),                                 \
360     .vmsd       = &vmstate_pci_device,                               \
361     .flags      = VMS_STRUCT | VMS_POINTER,                          \
362     .offset     = vmstate_offset_pointer(_state, _field, PCIDevice), \
363 }
364 
365 #endif
366