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