Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include <string.h>
6 : : #include <dirent.h>
7 : :
8 : : #include <rte_log.h>
9 : : #include <rte_pci.h>
10 : : #include <rte_bus_pci.h>
11 : : #include <rte_malloc.h>
12 : : #include <rte_devargs.h>
13 : : #include <rte_memcpy.h>
14 : : #include <rte_vfio.h>
15 : :
16 : : #include <eal_export.h>
17 : : #include "eal_filesystem.h"
18 : :
19 : : #include "private.h"
20 : : #include "pci_init.h"
21 : :
22 : : /**
23 : : * @file
24 : : * PCI probing using Linux sysfs.
25 : : */
26 : :
27 : : static int
28 : 7955 : pci_get_kernel_driver_by_path(const char *filename, char *dri_name,
29 : : size_t len)
30 : : {
31 : : int count;
32 : : char path[PATH_MAX];
33 : : char *name;
34 : :
35 [ + - ]: 7955 : if (!filename || !dri_name)
36 : : return -1;
37 : :
38 : 7955 : count = readlink(filename, path, PATH_MAX);
39 [ + - ]: 7955 : if (count >= PATH_MAX)
40 : : return -1;
41 : :
42 : : /* For device does not have a driver */
43 [ + + ]: 7955 : if (count < 0)
44 : : return 1;
45 : :
46 : 7215 : path[count] = '\0';
47 : :
48 : 7215 : name = strrchr(path, '/');
49 [ + - ]: 7215 : if (name) {
50 : 7215 : strlcpy(dri_name, name + 1, len);
51 : 7215 : return 0;
52 : : }
53 : :
54 : : return -1;
55 : : }
56 : :
57 : : /* Map pci device */
58 : : RTE_EXPORT_SYMBOL(rte_pci_map_device)
59 : : int
60 : 183 : rte_pci_map_device(struct rte_pci_device *dev)
61 : : {
62 : : int ret = -1;
63 : :
64 : : /* try mapping the NIC resources using VFIO if it exists */
65 [ - - + ]: 183 : switch (dev->kdrv) {
66 : 0 : case RTE_PCI_KDRV_VFIO:
67 [ # # ]: 0 : if (pci_vfio_is_enabled())
68 : 0 : ret = pci_vfio_map_resource(dev);
69 : : break;
70 : 0 : case RTE_PCI_KDRV_IGB_UIO:
71 : : case RTE_PCI_KDRV_UIO_GENERIC:
72 [ # # ]: 0 : if (rte_eal_using_phys_addrs()) {
73 : : /* map resources for devices that use uio */
74 : 0 : ret = pci_uio_map_resource(dev);
75 : : }
76 : : break;
77 : 183 : default:
78 : 183 : PCI_LOG(DEBUG, " Not managed by a supported kernel driver, skipped");
79 : : ret = 1;
80 : 183 : break;
81 : : }
82 : :
83 : 183 : return ret;
84 : : }
85 : :
86 : : /* Unmap pci device */
87 : : RTE_EXPORT_SYMBOL(rte_pci_unmap_device)
88 : : void
89 : 0 : rte_pci_unmap_device(struct rte_pci_device *dev)
90 : : {
91 : : /* try unmapping the NIC resources using VFIO if it exists */
92 [ # # # ]: 0 : switch (dev->kdrv) {
93 : 0 : case RTE_PCI_KDRV_VFIO:
94 [ # # ]: 0 : if (pci_vfio_is_enabled())
95 : 0 : pci_vfio_unmap_resource(dev);
96 : : break;
97 : 0 : case RTE_PCI_KDRV_IGB_UIO:
98 : : case RTE_PCI_KDRV_UIO_GENERIC:
99 : : /* unmap resources for devices that use uio */
100 : 0 : pci_uio_unmap_resource(dev);
101 : 0 : break;
102 : 0 : default:
103 : 0 : PCI_LOG(DEBUG, " Not managed by a supported kernel driver, skipped");
104 : 0 : break;
105 : : }
106 : 0 : }
107 : :
108 : : static int
109 : 0 : find_max_end_va(const struct rte_memseg_list *msl, void *arg)
110 : : {
111 : 0 : size_t sz = msl->len;
112 : 0 : void *end_va = RTE_PTR_ADD(msl->base_va, sz);
113 : : void **max_va = arg;
114 : :
115 [ # # ]: 0 : if (*max_va < end_va)
116 : 0 : *max_va = end_va;
117 : 0 : return 0;
118 : : }
119 : :
120 : : void *
121 : 0 : pci_find_max_end_va(void)
122 : : {
123 : 0 : void *va = NULL;
124 : :
125 : 0 : rte_memseg_list_walk(find_max_end_va, &va);
126 : 0 : return va;
127 : : }
128 : :
129 : :
130 : : /* parse one line of the "resource" sysfs file (note that the 'line'
131 : : * string is modified)
132 : : */
133 : : int
134 : 47730 : pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
135 : : uint64_t *end_addr, uint64_t *flags)
136 : : {
137 : : union pci_resource_info {
138 : : struct {
139 : : char *phys_addr;
140 : : char *end_addr;
141 : : char *flags;
142 : : };
143 : : char *ptrs[PCI_RESOURCE_FMT_NVAL];
144 : : } res_info;
145 : :
146 [ - + ]: 47730 : if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
147 : 0 : PCI_LOG(ERR, "%s(): bad resource format", __func__);
148 : 0 : return -1;
149 : : }
150 : 47730 : errno = 0;
151 : 47730 : *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
152 : 47730 : *end_addr = strtoull(res_info.end_addr, NULL, 16);
153 : 47730 : *flags = strtoull(res_info.flags, NULL, 16);
154 [ - + ]: 47730 : if (errno != 0) {
155 : 0 : PCI_LOG(ERR, "%s(): bad resource format", __func__);
156 : 0 : return -1;
157 : : }
158 : :
159 : : return 0;
160 : : }
161 : :
162 : : /* parse the "resource" sysfs file */
163 : : static int
164 : 7955 : pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
165 : : {
166 : : FILE *f;
167 : : char buf[BUFSIZ];
168 : : int i;
169 : : uint64_t phys_addr, end_addr, flags;
170 : :
171 : 7955 : f = fopen(filename, "r");
172 [ - + ]: 7955 : if (f == NULL) {
173 : 0 : PCI_LOG(ERR, "Cannot open sysfs resource");
174 : 0 : return -1;
175 : : }
176 : :
177 [ + + ]: 55685 : for (i = 0; i<PCI_MAX_RESOURCE; i++) {
178 : :
179 [ - + ]: 47730 : if (fgets(buf, sizeof(buf), f) == NULL) {
180 : 0 : PCI_LOG(ERR, "%s(): cannot read resource", __func__);
181 : 0 : goto error;
182 : : }
183 [ - + ]: 47730 : if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
184 : : &end_addr, &flags) < 0)
185 : 0 : goto error;
186 : :
187 [ + + ]: 47730 : if (flags & IORESOURCE_MEM) {
188 : 1665 : dev->mem_resource[i].phys_addr = phys_addr;
189 : 1665 : dev->mem_resource[i].len = end_addr - phys_addr + 1;
190 : : /* not mapped for now */
191 : 1665 : dev->mem_resource[i].addr = NULL;
192 : : }
193 : : }
194 : 7955 : fclose(f);
195 : 7955 : return 0;
196 : :
197 : 0 : error:
198 : 0 : fclose(f);
199 : 0 : return -1;
200 : : }
201 : :
202 : : /* Scan one pci sysfs entry, and fill the devices list from it. */
203 : : static int
204 : 7955 : pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
205 : : {
206 : : char filename[PATH_MAX];
207 : : unsigned long tmp;
208 : : struct rte_pci_device_internal *pdev;
209 : : struct rte_pci_device *dev;
210 : : char driver[PATH_MAX];
211 : : int ret;
212 : :
213 : 7955 : pdev = malloc(sizeof(*pdev));
214 [ - + ]: 7955 : if (pdev == NULL) {
215 : 0 : PCI_LOG(ERR, "Cannot allocate memory for internal pci device");
216 : 0 : return -1;
217 : : }
218 : :
219 : : memset(pdev, 0, sizeof(*pdev));
220 : 7955 : dev = &pdev->device;
221 : 7955 : dev->addr = *addr;
222 : :
223 : : /* get vendor id */
224 : : snprintf(filename, sizeof(filename), "%s/vendor", dirname);
225 [ - + ]: 7955 : if (eal_parse_sysfs_value(filename, &tmp) < 0) {
226 : 0 : pci_free(pdev);
227 : 0 : return -1;
228 : : }
229 : 7955 : dev->id.vendor_id = (uint16_t)tmp;
230 : :
231 : : /* get device id */
232 : : snprintf(filename, sizeof(filename), "%s/device", dirname);
233 [ - + ]: 7955 : if (eal_parse_sysfs_value(filename, &tmp) < 0) {
234 : 0 : pci_free(pdev);
235 : 0 : return -1;
236 : : }
237 : 7955 : dev->id.device_id = (uint16_t)tmp;
238 : :
239 : : /* get subsystem_vendor id */
240 : : snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
241 : : dirname);
242 [ - + ]: 7955 : if (eal_parse_sysfs_value(filename, &tmp) < 0) {
243 : 0 : pci_free(pdev);
244 : 0 : return -1;
245 : : }
246 : 7955 : dev->id.subsystem_vendor_id = (uint16_t)tmp;
247 : :
248 : : /* get subsystem_device id */
249 : : snprintf(filename, sizeof(filename), "%s/subsystem_device",
250 : : dirname);
251 [ - + ]: 7955 : if (eal_parse_sysfs_value(filename, &tmp) < 0) {
252 : 0 : pci_free(pdev);
253 : 0 : return -1;
254 : : }
255 : 7955 : dev->id.subsystem_device_id = (uint16_t)tmp;
256 : :
257 : : /* get class_id */
258 : : snprintf(filename, sizeof(filename), "%s/class",
259 : : dirname);
260 [ - + ]: 7955 : if (eal_parse_sysfs_value(filename, &tmp) < 0) {
261 : 0 : pci_free(pdev);
262 : 0 : return -1;
263 : : }
264 : : /* the least 24 bits are valid: class, subclass, program interface */
265 : 7955 : dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
266 : :
267 : : /* get max_vfs */
268 : 7955 : dev->max_vfs = 0;
269 : : snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
270 [ - + - - ]: 7955 : if (!access(filename, F_OK) &&
271 : 0 : eal_parse_sysfs_value(filename, &tmp) == 0)
272 : 0 : dev->max_vfs = (uint16_t)tmp;
273 : : else {
274 : : /* for non igb_uio driver, need kernel version >= 3.8 */
275 : : snprintf(filename, sizeof(filename),
276 : : "%s/sriov_numvfs", dirname);
277 [ - + - - ]: 7955 : if (!access(filename, F_OK) &&
278 : 0 : eal_parse_sysfs_value(filename, &tmp) == 0)
279 : 0 : dev->max_vfs = (uint16_t)tmp;
280 : : }
281 : :
282 : : /* get numa node, default to 0 if not present */
283 : : snprintf(filename, sizeof(filename), "%s/numa_node", dirname);
284 : :
285 [ + - + - ]: 15910 : if (access(filename, F_OK) == 0 &&
286 : 7955 : eal_parse_sysfs_value(filename, &tmp) == 0)
287 : 7955 : dev->device.numa_node = tmp;
288 : : else
289 : 0 : dev->device.numa_node = SOCKET_ID_ANY;
290 : :
291 : 7955 : pci_common_set(dev);
292 : :
293 : : /* parse resources */
294 : : snprintf(filename, sizeof(filename), "%s/resource", dirname);
295 [ - + ]: 7955 : if (pci_parse_sysfs_resource(filename, dev) < 0) {
296 : 0 : PCI_LOG(ERR, "%s(): cannot parse resource", __func__);
297 : 0 : pci_free(pdev);
298 : 0 : return -1;
299 : : }
300 : :
301 : : /* parse driver */
302 : : snprintf(filename, sizeof(filename), "%s/driver", dirname);
303 : 7955 : ret = pci_get_kernel_driver_by_path(filename, driver, sizeof(driver));
304 [ - + ]: 7955 : if (ret < 0) {
305 : 0 : PCI_LOG(ERR, "Fail to get kernel driver");
306 : 0 : pci_free(pdev);
307 : 0 : return -1;
308 : : }
309 : :
310 [ + + ]: 7955 : if (!ret) {
311 [ - + ]: 7215 : if (!strcmp(driver, "vfio-pci"))
312 : 0 : dev->kdrv = RTE_PCI_KDRV_VFIO;
313 [ - + ]: 7215 : else if (!strcmp(driver, "igb_uio"))
314 : 0 : dev->kdrv = RTE_PCI_KDRV_IGB_UIO;
315 [ - + ]: 7215 : else if (!strcmp(driver, "uio_pci_generic"))
316 : 0 : dev->kdrv = RTE_PCI_KDRV_UIO_GENERIC;
317 : : else
318 : 7215 : dev->kdrv = RTE_PCI_KDRV_UNKNOWN;
319 : : } else {
320 : 740 : pci_free(pdev);
321 : 740 : return 0;
322 : : }
323 : : /* device is valid, add in list (sorted) */
324 [ + + ]: 7215 : if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
325 : 186 : rte_bus_add_device(&rte_pci_bus, &dev->device);
326 : : } else {
327 : : struct rte_pci_device *dev2;
328 : :
329 [ + - ]: 68615 : RTE_BUS_FOREACH_DEV(dev2, &rte_pci_bus) {
330 : 68615 : ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
331 [ + + ]: 68615 : if (ret > 0)
332 : : continue;
333 : :
334 [ + - ]: 7029 : if (ret < 0) {
335 : 7029 : rte_bus_insert_device(&rte_pci_bus, &dev2->device, &dev->device);
336 : : } else { /* already registered */
337 [ # # ]: 0 : if (!rte_dev_is_probed(&dev2->device)) {
338 : 0 : dev2->kdrv = dev->kdrv;
339 : 0 : dev2->max_vfs = dev->max_vfs;
340 : 0 : dev2->id = dev->id;
341 : 0 : pci_common_set(dev2);
342 : 0 : memmove(dev2->mem_resource,
343 : 0 : dev->mem_resource,
344 : : sizeof(dev->mem_resource));
345 : : } else {
346 : : /**
347 : : * If device is plugged and driver is
348 : : * probed already, (This happens when
349 : : * we call rte_dev_probe which will
350 : : * scan all device on the bus) we don't
351 : : * need to do anything here unless...
352 : : **/
353 [ # # ]: 0 : if (dev2->kdrv != dev->kdrv ||
354 : 0 : dev2->max_vfs != dev->max_vfs ||
355 [ # # ]: 0 : memcmp(&dev2->id, &dev->id, sizeof(dev2->id)))
356 : : /*
357 : : * This should not happens.
358 : : * But it is still possible if
359 : : * we unbind a device from
360 : : * vfio or uio before hotplug
361 : : * remove and rebind it with
362 : : * a different configure.
363 : : * So we just print out the
364 : : * error as an alarm.
365 : : */
366 : 0 : PCI_LOG(ERR, "Unexpected device scan at %s!",
367 : : filename);
368 : 0 : else if (dev2->device.devargs !=
369 [ # # ]: 0 : dev->device.devargs) {
370 : 0 : rte_devargs_remove(dev2->device.devargs);
371 : 0 : pci_common_set(dev2);
372 : : }
373 : : }
374 : 0 : pci_free(pdev);
375 : : }
376 : 7029 : return 0;
377 : : }
378 : :
379 : 0 : rte_bus_add_device(&rte_pci_bus, &dev->device);
380 : : }
381 : :
382 : : return 0;
383 : : }
384 : :
385 : : /*
386 : : * split up a pci address into its constituent parts.
387 : : */
388 : : static int
389 : 8299 : parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
390 : : {
391 : : /* first split on ':' */
392 : : union splitaddr {
393 : : struct {
394 : : char *domain;
395 : : char *bus;
396 : : char *devid;
397 : : char *function;
398 : : };
399 : : char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
400 : : } splitaddr;
401 : :
402 : 8299 : char *buf_copy = strndup(buf, bufsize);
403 [ + - ]: 8299 : if (buf_copy == NULL)
404 : : return -1;
405 : :
406 [ - + ]: 8299 : if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
407 : : != PCI_FMT_NVAL - 1)
408 : 0 : goto error;
409 : : /* final split is on '.' between devid and function */
410 : 8299 : splitaddr.function = strchr(splitaddr.devid,'.');
411 [ - + ]: 8299 : if (splitaddr.function == NULL)
412 : 0 : goto error;
413 : 8299 : *splitaddr.function++ = '\0';
414 : :
415 : : /* now convert to int values */
416 : 8299 : errno = 0;
417 : 8299 : addr->domain = strtoul(splitaddr.domain, NULL, 16);
418 : 8299 : addr->bus = strtoul(splitaddr.bus, NULL, 16);
419 : 8299 : addr->devid = strtoul(splitaddr.devid, NULL, 16);
420 : 8299 : addr->function = strtoul(splitaddr.function, NULL, 10);
421 [ - + ]: 8299 : if (errno != 0)
422 : 0 : goto error;
423 : :
424 : 8299 : free(buf_copy); /* free the copy made with strdup */
425 : 8299 : return 0;
426 : 0 : error:
427 : 0 : free(buf_copy);
428 : 0 : return -1;
429 : : }
430 : :
431 : : /*
432 : : * Scan the content of the PCI bus, and the devices in the devices
433 : : * list
434 : : */
435 : : int
436 : 225 : rte_pci_scan(void)
437 : : {
438 : : struct dirent *e;
439 : : DIR *dir;
440 : : char dirname[PATH_MAX];
441 : : struct rte_pci_addr addr;
442 : :
443 : : /* for debug purposes, PCI can be disabled */
444 [ + + ]: 225 : if (!rte_eal_has_pci())
445 : : return 0;
446 : :
447 : 193 : dir = opendir(rte_pci_get_sysfs_path());
448 [ - + ]: 193 : if (dir == NULL) {
449 : 0 : PCI_LOG(ERR, "%s(): opendir failed: %s", __func__, strerror(errno));
450 : 0 : return -1;
451 : : }
452 : :
453 [ + + ]: 8878 : while ((e = readdir(dir)) != NULL) {
454 [ + + ]: 8685 : if (e->d_name[0] == '.')
455 : 386 : continue;
456 : :
457 [ - + ]: 8299 : if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
458 : 0 : continue;
459 : :
460 [ + + ]: 8299 : if (rte_bus_device_is_ignored(&rte_pci_bus, e->d_name))
461 : 344 : continue;
462 : :
463 : 7955 : snprintf(dirname, sizeof(dirname), "%s/%s",
464 : : rte_pci_get_sysfs_path(), e->d_name);
465 : :
466 [ - + ]: 7955 : if (pci_scan_one(dirname, &addr) < 0)
467 : 0 : goto error;
468 : : }
469 : 193 : closedir(dir);
470 : 193 : return 0;
471 : :
472 : : error:
473 : 0 : closedir(dir);
474 : 0 : return -1;
475 : : }
476 : :
477 : : #if defined(RTE_ARCH_X86)
478 : : bool
479 : 186 : pci_device_iommu_support_va(const struct rte_pci_device *dev)
480 : : {
481 : : #define VTD_CAP_MGAW_SHIFT 16
482 : : #define VTD_CAP_MGAW_MASK (0x3fULL << VTD_CAP_MGAW_SHIFT)
483 : : const struct rte_pci_addr *addr = &dev->addr;
484 : : char filename[PATH_MAX];
485 : : FILE *fp;
486 : 186 : uint64_t mgaw, vtd_cap_reg = 0;
487 : :
488 : 186 : snprintf(filename, sizeof(filename),
489 : : "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
490 : 186 : rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
491 : 186 : addr->function);
492 : :
493 : 186 : fp = fopen(filename, "r");
494 [ + - ]: 186 : if (fp == NULL) {
495 : : /* We don't have an Intel IOMMU, assume VA supported */
496 [ - + ]: 186 : if (errno == ENOENT)
497 : : return true;
498 : :
499 : 0 : PCI_LOG(ERR, "%s(): can't open %s: %s",
500 : : __func__, filename, strerror(errno));
501 : 0 : return false;
502 : : }
503 : :
504 : : /* We have an Intel IOMMU */
505 [ # # ]: 0 : if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
506 : 0 : PCI_LOG(ERR, "%s(): can't read %s", __func__, filename);
507 : 0 : fclose(fp);
508 : 0 : return false;
509 : : }
510 : :
511 : 0 : fclose(fp);
512 : :
513 : 0 : mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
514 : :
515 : : /*
516 : : * Assuming there is no limitation by now. We can not know at this point
517 : : * because the memory has not been initialized yet. Setting the dma mask
518 : : * will force a check once memory initialization is done. We can not do
519 : : * a fallback to IOVA PA now, but if the dma check fails, the error
520 : : * message should advice for using '--iova-mode pa' if IOVA VA is the
521 : : * current mode.
522 : : */
523 : 0 : rte_mem_set_dma_mask(mgaw);
524 : 0 : return true;
525 : : }
526 : : #elif defined(RTE_ARCH_PPC_64)
527 : : bool
528 : : pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
529 : : {
530 : : /*
531 : : * All POWER systems support an IOMMU, but only IOMMUv2 supports
532 : : * IOVA = VA in DPDK. Check contents of /proc/cpuinfo to find the
533 : : * system.
534 : : *
535 : : * Platform | Model | IOMMU | VA? | Comment
536 : : * ---------+-------+---------+-----+---------------------------------
537 : : * PowerNV | N/A | IOMMUv2 | Yes | OpenPOWER (Bare Metal)
538 : : * pSeries | ~qemu | IOMMUv2 | Yes | PowerVM Logical Partition (LPAR)
539 : : * pSeries | qemu | IOMMUv1 | No | QEMU Virtual Machine
540 : : */
541 : :
542 : : char *line = NULL;
543 : : size_t len = 0;
544 : : char filename[PATH_MAX] = "/proc/cpuinfo";
545 : : FILE *fp = fopen(filename, "r");
546 : : bool pseries = false, powernv = false, qemu = false;
547 : : bool ret = false;
548 : :
549 : : if (fp == NULL) {
550 : : PCI_LOG(ERR, "%s(): can't open %s: %s",
551 : : __func__, filename, strerror(errno));
552 : : return ret;
553 : : }
554 : :
555 : : /* Check the "platform" and "model" fields */
556 : : while (getline(&line, &len, fp) != -1) {
557 : : if (strstr(line, "platform") != NULL) {
558 : : if (strstr(line, "PowerNV") != NULL) {
559 : : PCI_LOG(DEBUG, "Running on a PowerNV platform");
560 : : powernv = true;
561 : : } else if (strstr(line, "pSeries") != NULL) {
562 : : PCI_LOG(DEBUG, "Running on a pSeries platform");
563 : : pseries = true;
564 : : }
565 : : } else if (strstr(line, "model") != NULL) {
566 : : if (strstr(line, "qemu") != NULL) {
567 : : PCI_LOG(DEBUG, "Found qemu emulation");
568 : : qemu = true;
569 : : }
570 : : }
571 : : }
572 : :
573 : : free(line);
574 : : fclose(fp);
575 : :
576 : : if (powernv || (pseries && !qemu))
577 : : ret = true;
578 : : return ret;
579 : : }
580 : : #else
581 : : bool
582 : : pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
583 : : {
584 : : return true;
585 : : }
586 : : #endif
587 : :
588 : : enum rte_iova_mode
589 : 183 : pci_device_iova_mode(const struct rte_pci_driver *pdrv,
590 : : const struct rte_pci_device *pdev)
591 : : {
592 : : enum rte_iova_mode iova_mode = RTE_IOVA_DC;
593 : :
594 [ - + - ]: 183 : switch (pdev->kdrv) {
595 : 0 : case RTE_PCI_KDRV_VFIO: {
596 : : static int is_vfio_noiommu_enabled = -1;
597 : :
598 [ # # ]: 0 : if (is_vfio_noiommu_enabled == -1) {
599 [ # # ]: 0 : if (rte_vfio_noiommu_is_enabled() == 1)
600 : 0 : is_vfio_noiommu_enabled = 1;
601 : : else
602 : 0 : is_vfio_noiommu_enabled = 0;
603 : : }
604 [ # # ]: 0 : if (is_vfio_noiommu_enabled != 0)
605 : : iova_mode = RTE_IOVA_PA;
606 [ # # ]: 0 : else if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0)
607 : : iova_mode = RTE_IOVA_VA;
608 : : break;
609 : : }
610 : :
611 : : case RTE_PCI_KDRV_IGB_UIO:
612 : : case RTE_PCI_KDRV_UIO_GENERIC:
613 : : iova_mode = RTE_IOVA_PA;
614 : : break;
615 : :
616 : 183 : default:
617 [ - + ]: 183 : if ((pdrv->drv_flags & RTE_PCI_DRV_NEED_IOVA_AS_VA) != 0)
618 : : iova_mode = RTE_IOVA_VA;
619 : : break;
620 : : }
621 : 183 : return iova_mode;
622 : : }
623 : :
624 : : /* Read PCI config space. */
625 : : RTE_EXPORT_SYMBOL(rte_pci_read_config)
626 : 0 : int rte_pci_read_config(const struct rte_pci_device *device,
627 : : void *buf, size_t len, off_t offset)
628 : : {
629 : 0 : char devname[RTE_DEV_NAME_MAX_LEN] = "";
630 : 0 : const struct rte_intr_handle *intr_handle = device->intr_handle;
631 : :
632 [ # # # ]: 0 : switch (device->kdrv) {
633 : 0 : case RTE_PCI_KDRV_IGB_UIO:
634 : : case RTE_PCI_KDRV_UIO_GENERIC:
635 : 0 : return pci_uio_read_config(intr_handle, buf, len, offset);
636 : 0 : case RTE_PCI_KDRV_VFIO:
637 : 0 : return pci_vfio_read_config(device, buf, len, offset);
638 : 0 : default:
639 : 0 : rte_pci_device_name(&device->addr, devname,
640 : : RTE_DEV_NAME_MAX_LEN);
641 : 0 : PCI_LOG(ERR, "Unknown driver type for %s", devname);
642 : 0 : return -1;
643 : : }
644 : : }
645 : :
646 : : /* Write PCI config space. */
647 : : RTE_EXPORT_SYMBOL(rte_pci_write_config)
648 : 0 : int rte_pci_write_config(const struct rte_pci_device *device,
649 : : const void *buf, size_t len, off_t offset)
650 : : {
651 : 0 : char devname[RTE_DEV_NAME_MAX_LEN] = "";
652 : 0 : const struct rte_intr_handle *intr_handle = device->intr_handle;
653 : :
654 [ # # # ]: 0 : switch (device->kdrv) {
655 : 0 : case RTE_PCI_KDRV_IGB_UIO:
656 : : case RTE_PCI_KDRV_UIO_GENERIC:
657 : 0 : return pci_uio_write_config(intr_handle, buf, len, offset);
658 : 0 : case RTE_PCI_KDRV_VFIO:
659 : 0 : return pci_vfio_write_config(device, buf, len, offset);
660 : 0 : default:
661 : 0 : rte_pci_device_name(&device->addr, devname,
662 : : RTE_DEV_NAME_MAX_LEN);
663 : 0 : PCI_LOG(ERR, "Unknown driver type for %s", devname);
664 : 0 : return -1;
665 : : }
666 : : }
667 : :
668 : : /* Read PCI MMIO space. */
669 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_mmio_read, 23.07)
670 : 0 : int rte_pci_mmio_read(const struct rte_pci_device *device, int bar,
671 : : void *buf, size_t len, off_t offset)
672 : : {
673 : 0 : char devname[RTE_DEV_NAME_MAX_LEN] = "";
674 : :
675 [ # # # ]: 0 : switch (device->kdrv) {
676 : 0 : case RTE_PCI_KDRV_IGB_UIO:
677 : : case RTE_PCI_KDRV_UIO_GENERIC:
678 : 0 : return pci_uio_mmio_read(device, bar, buf, len, offset);
679 : 0 : case RTE_PCI_KDRV_VFIO:
680 : 0 : return pci_vfio_mmio_read(device, bar, buf, len, offset);
681 : 0 : default:
682 : 0 : rte_pci_device_name(&device->addr, devname,
683 : : RTE_DEV_NAME_MAX_LEN);
684 : 0 : PCI_LOG(ERR, "Unknown driver type for %s", devname);
685 : 0 : return -1;
686 : : }
687 : : }
688 : :
689 : : /* Write PCI MMIO space. */
690 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pci_mmio_write, 23.07)
691 : 0 : int rte_pci_mmio_write(const struct rte_pci_device *device, int bar,
692 : : const void *buf, size_t len, off_t offset)
693 : : {
694 : 0 : char devname[RTE_DEV_NAME_MAX_LEN] = "";
695 : :
696 [ # # # ]: 0 : switch (device->kdrv) {
697 : 0 : case RTE_PCI_KDRV_IGB_UIO:
698 : : case RTE_PCI_KDRV_UIO_GENERIC:
699 : 0 : return pci_uio_mmio_write(device, bar, buf, len, offset);
700 : 0 : case RTE_PCI_KDRV_VFIO:
701 : 0 : return pci_vfio_mmio_write(device, bar, buf, len, offset);
702 : 0 : default:
703 : 0 : rte_pci_device_name(&device->addr, devname,
704 : : RTE_DEV_NAME_MAX_LEN);
705 : 0 : PCI_LOG(ERR, "Unknown driver type for %s", devname);
706 : 0 : return -1;
707 : : }
708 : : }
709 : :
710 : : RTE_EXPORT_SYMBOL(rte_pci_ioport_map)
711 : : int
712 : 0 : rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
713 : : struct rte_pci_ioport *p)
714 : : {
715 : : int ret = -1;
716 : :
717 [ # # # ]: 0 : switch (dev->kdrv) {
718 : 0 : case RTE_PCI_KDRV_VFIO:
719 [ # # ]: 0 : if (pci_vfio_is_enabled())
720 : 0 : ret = pci_vfio_ioport_map(dev, bar, p);
721 : : break;
722 : 0 : case RTE_PCI_KDRV_IGB_UIO:
723 : : case RTE_PCI_KDRV_UIO_GENERIC:
724 : 0 : ret = pci_uio_ioport_map(dev, bar, p);
725 : 0 : break;
726 : : default:
727 : : break;
728 : : }
729 : :
730 [ # # ]: 0 : if (!ret)
731 : 0 : p->dev = dev;
732 : :
733 : 0 : return ret;
734 : : }
735 : :
736 : : RTE_EXPORT_SYMBOL(rte_pci_ioport_read)
737 : : void
738 : 0 : rte_pci_ioport_read(struct rte_pci_ioport *p,
739 : : void *data, size_t len, off_t offset)
740 : : {
741 [ # # # ]: 0 : switch (p->dev->kdrv) {
742 : 0 : case RTE_PCI_KDRV_VFIO:
743 : 0 : pci_vfio_ioport_read(p, data, len, offset);
744 : 0 : break;
745 : 0 : case RTE_PCI_KDRV_IGB_UIO:
746 : : case RTE_PCI_KDRV_UIO_GENERIC:
747 : 0 : pci_uio_ioport_read(p, data, len, offset);
748 : 0 : break;
749 : : default:
750 : : break;
751 : : }
752 : 0 : }
753 : :
754 : : RTE_EXPORT_SYMBOL(rte_pci_ioport_write)
755 : : void
756 : 0 : rte_pci_ioport_write(struct rte_pci_ioport *p,
757 : : const void *data, size_t len, off_t offset)
758 : : {
759 [ # # # ]: 0 : switch (p->dev->kdrv) {
760 : 0 : case RTE_PCI_KDRV_VFIO:
761 : 0 : pci_vfio_ioport_write(p, data, len, offset);
762 : 0 : break;
763 : 0 : case RTE_PCI_KDRV_IGB_UIO:
764 : : case RTE_PCI_KDRV_UIO_GENERIC:
765 : 0 : pci_uio_ioport_write(p, data, len, offset);
766 : 0 : break;
767 : : default:
768 : : break;
769 : : }
770 : 0 : }
771 : :
772 : : RTE_EXPORT_SYMBOL(rte_pci_ioport_unmap)
773 : : int
774 : 0 : rte_pci_ioport_unmap(struct rte_pci_ioport *p)
775 : : {
776 : : int ret = -1;
777 : :
778 [ # # # ]: 0 : switch (p->dev->kdrv) {
779 : 0 : case RTE_PCI_KDRV_VFIO:
780 [ # # ]: 0 : if (pci_vfio_is_enabled())
781 : 0 : ret = pci_vfio_ioport_unmap(p);
782 : : break;
783 : 0 : case RTE_PCI_KDRV_IGB_UIO:
784 : : case RTE_PCI_KDRV_UIO_GENERIC:
785 : 0 : ret = pci_uio_ioport_unmap(p);
786 : 0 : break;
787 : : default:
788 : : break;
789 : : }
790 : :
791 : 0 : return ret;
792 : : }
|