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