xref: /dpdk/drivers/bus/pci/private.h (revision 849f773b7645216954022a47e466043a23125af9)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 6WIND S.A.
3  */
4 
5 #ifndef _PCI_PRIVATE_H_
6 #define _PCI_PRIVATE_H_
7 
8 #include <stdbool.h>
9 #include <stdio.h>
10 
11 #include <bus_driver.h>
12 #include <bus_pci_driver.h>
13 #include <rte_log.h>
14 #include <rte_os_shim.h>
15 #include <rte_pci.h>
16 
17 extern int pci_bus_logtype;
18 #define RTE_LOGTYPE_PCI_BUS pci_bus_logtype
19 #define PCI_LOG(level, ...) \
20 	RTE_LOG_LINE(level, PCI_BUS, "" __VA_ARGS__)
21 
22 #define RTE_MAX_PCI_REGIONS    9
23 
24 /*
25  * Convert struct rte_pci_device to struct rte_pci_device_internal
26  */
27 #define RTE_PCI_DEVICE_INTERNAL(ptr) \
28 	container_of(ptr, struct rte_pci_device_internal, device)
29 #define RTE_PCI_DEVICE_INTERNAL_CONST(ptr) \
30 	container_of(ptr, const struct rte_pci_device_internal, device)
31 
32 /**
33  * Structure describing the PCI bus
34  */
35 struct rte_pci_bus {
36 	struct rte_bus bus;               /**< Inherit the generic class */
37 	RTE_TAILQ_HEAD(, rte_pci_device) device_list; /**< List of PCI devices */
38 	RTE_TAILQ_HEAD(, rte_pci_driver) driver_list; /**< List of PCI drivers */
39 };
40 
41 extern struct rte_pci_bus rte_pci_bus;
42 
43 /* PCI Bus iterators */
44 #define FOREACH_DEVICE_ON_PCIBUS(p)	\
45 	RTE_TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
46 
47 #define FOREACH_DRIVER_ON_PCIBUS(p)	\
48 	RTE_TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
49 
50 struct rte_pci_driver;
51 struct rte_pci_device;
52 
53 struct rte_pci_region {
54 	uint64_t size;
55 	uint64_t offset;
56 };
57 
58 struct rte_pci_device_internal {
59 	struct rte_pci_device device;
60 	/* PCI regions provided by e.g. VFIO. */
61 	struct rte_pci_region region[RTE_MAX_PCI_REGIONS];
62 };
63 
64 /**
65  * Scan the content of the PCI bus, and the devices in the devices
66  * list
67  *
68  * @return
69  *  0 on success, negative on error
70  */
71 int rte_pci_scan(void);
72 
73 /**
74  * Set common internal information for a PCI device.
75  */
76 void
77 pci_common_set(struct rte_pci_device *dev);
78 
79 /**
80  * Free a PCI device.
81  */
82 void
83 pci_free(struct rte_pci_device_internal *pdev);
84 
85 /**
86  * Validate whether a device with given PCI address should be ignored or not.
87  *
88  * @param pci_addr
89  *	PCI address of device to be validated
90  * @return
91  *	true: if device is to be ignored,
92  *	false: if device is to be scanned,
93  */
94 bool rte_pci_ignore_device(const struct rte_pci_addr *pci_addr);
95 
96 /**
97  * Add a PCI device to the PCI Bus (append to PCI Device list). This function
98  * also updates the bus references of the PCI Device (and the generic device
99  * object embedded within.
100  *
101  * @param pci_dev
102  *	PCI device to add
103  * @return void
104  */
105 void rte_pci_add_device(struct rte_pci_device *pci_dev);
106 
107 /**
108  * Insert a PCI device in the PCI Bus at a particular location in the device
109  * list. It also updates the PCI Bus reference of the new devices to be
110  * inserted.
111  *
112  * @param exist_pci_dev
113  *	Existing PCI device in PCI Bus
114  * @param new_pci_dev
115  *	PCI device to be added before exist_pci_dev
116  * @return void
117  */
118 void rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
119 		struct rte_pci_device *new_pci_dev);
120 
121 /**
122  * A structure describing a PCI mapping.
123  */
124 struct pci_map {
125 	void *addr;
126 	char *path;
127 	uint64_t offset;
128 	uint64_t size;
129 	uint64_t phaddr;
130 	uint32_t nr_areas;
131 	struct vfio_region_sparse_mmap_area *areas;
132 };
133 
134 struct pci_msix_table {
135 	int bar_index;
136 	uint32_t offset;
137 	uint32_t size;
138 };
139 
140 /**
141  * A structure describing a mapped PCI resource.
142  * For multi-process we need to reproduce all PCI mappings in secondary
143  * processes, so save them in a tailq.
144  */
145 struct mapped_pci_resource {
146 	TAILQ_ENTRY(mapped_pci_resource) next;
147 
148 	struct rte_pci_addr pci_addr;
149 	char path[PATH_MAX];
150 	int nb_maps;
151 	struct pci_map maps[PCI_MAX_RESOURCE];
152 	struct pci_msix_table msix_table;
153 };
154 
155 /** mapped pci device list */
156 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
157 
158 /**
159  * Map a particular resource from a file.
160  *
161  * @param requested_addr
162  *      The starting address for the new mapping range.
163  * @param fd
164  *      The file descriptor.
165  * @param offset
166  *      The offset for the mapping range.
167  * @param size
168  *      The size for the mapping range.
169  * @param additional_flags
170  *      The additional rte_mem_map() flags for the mapping range.
171  * @return
172  *   - On success, the function returns a pointer to the mapped area.
173  *   - On error, NULL is returned.
174  */
175 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
176 		size_t size, int additional_flags);
177 
178 /**
179  * Unmap a particular resource.
180  *
181  * @param requested_addr
182  *      The address for the unmapping range.
183  * @param size
184  *      The size for the unmapping range.
185  */
186 void pci_unmap_resource(void *requested_addr, size_t size);
187 
188 /**
189  * Map the PCI resource of a PCI device in virtual memory
190  *
191  * This function is private to EAL.
192  *
193  * @return
194  *   0 on success, negative on error
195  */
196 int pci_uio_map_resource(struct rte_pci_device *dev);
197 
198 /**
199  * Unmap the PCI resource of a PCI device
200  *
201  * This function is private to EAL.
202  */
203 void pci_uio_unmap_resource(struct rte_pci_device *dev);
204 
205 /**
206  * Allocate uio resource for PCI device
207  *
208  * This function is private to EAL.
209  *
210  * @param dev
211  *   PCI device to allocate uio resource
212  * @param uio_res
213  *   Pointer to uio resource.
214  *   If the function returns 0, the pointer will be filled.
215  * @return
216  *   0 on success, negative on error
217  */
218 int pci_uio_alloc_resource(struct rte_pci_device *dev,
219 		struct mapped_pci_resource **uio_res);
220 
221 /**
222  * Free uio resource for PCI device
223  *
224  * This function is private to EAL.
225  *
226  * @param dev
227  *   PCI device to free uio resource
228  * @param uio_res
229  *   Pointer to uio resource.
230  */
231 void pci_uio_free_resource(struct rte_pci_device *dev,
232 		struct mapped_pci_resource *uio_res);
233 
234 /**
235  * Remap the PCI resource of a PCI device in anonymous virtual memory.
236  *
237  * @param dev
238  *   Point to the struct rte pci device.
239  * @return
240  *   - On success, zero.
241  *   - On failure, a negative value.
242  */
243 int
244 pci_uio_remap_resource(struct rte_pci_device *dev);
245 
246 /**
247  * Map device memory to uio resource
248  *
249  * This function is private to EAL.
250  *
251  * @param dev
252  *   PCI device that has memory information.
253  * @param res_idx
254  *   Memory resource index of the PCI device.
255  * @param uio_res
256  *  uio resource that will keep mapping information.
257  * @param map_idx
258  *   Mapping information index of the uio resource.
259  * @return
260  *   0 on success, negative on error
261  */
262 int pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
263 		struct mapped_pci_resource *uio_res, int map_idx);
264 
265 /*
266  * Match the PCI Driver and Device using the ID Table
267  *
268  * @param pci_drv
269  *      PCI driver from which ID table would be extracted
270  * @param pci_dev
271  *      PCI device to match against the driver
272  * @return
273  *      1 for successful match
274  *      0 for unsuccessful match
275  */
276 int
277 rte_pci_match(const struct rte_pci_driver *pci_drv,
278 	      const struct rte_pci_device *pci_dev);
279 
280 /**
281  * OS specific callbacks for rte_pci_get_iommu_class
282  *
283  */
284 bool
285 pci_device_iommu_support_va(const struct rte_pci_device *dev);
286 
287 enum rte_iova_mode
288 pci_device_iova_mode(const struct rte_pci_driver *pci_drv,
289 		     const struct rte_pci_device *pci_dev);
290 
291 /**
292  * Get iommu class of PCI devices on the bus.
293  * And return their preferred iova mapping mode.
294  *
295  * @return
296  *   - enum rte_iova_mode.
297  */
298 enum rte_iova_mode
299 rte_pci_get_iommu_class(void);
300 
301 /*
302  * Iterate over internal devices,
303  * matching any device against the provided
304  * string.
305  *
306  * @param start
307  *   Iteration starting point.
308  *
309  * @param str
310  *   Device string to match against.
311  *
312  * @param it
313  *   (unused) iterator structure.
314  *
315  * @return
316  *   A pointer to the next matching device if any.
317  *   NULL otherwise.
318  */
319 void *
320 rte_pci_dev_iterate(const void *start,
321 		    const char *str,
322 		    const struct rte_dev_iterator *it);
323 
324 /*
325  * Parse device arguments and update name.
326  *
327  * @param da
328  *   device arguments to parse.
329  *
330  * @return
331  *   0 on success.
332  *   -EINVAL: kvargs string is invalid and cannot be parsed.
333  *   -ENODEV: no key matching a device ID is found in the kv list.
334  */
335 int
336 rte_pci_devargs_parse(struct rte_devargs *da);
337 
338 #endif /* _PCI_PRIVATE_H_ */
339