1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_bus_pci.h>
12 #include <rte_eal_memconfig.h>
13 #include <rte_malloc.h>
14 #include <rte_devargs.h>
15 #include <rte_memcpy.h>
18 #include "eal_private.h"
19 #include "eal_filesystem.h"
26 * PCI probing under linux
28 * This code is used to simulate a PCI probe by parsing information in sysfs.
29 * When a registered device matches a driver, it is then initialized with
30 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
33 extern struct rte_pci_bus rte_pci_bus;
36 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
42 if (!filename || !dri_name)
45 count = readlink(filename, path, PATH_MAX);
46 if (count >= PATH_MAX)
49 /* For device does not have a driver */
55 name = strrchr(path, '/');
57 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
66 rte_pci_map_device(struct rte_pci_device *dev)
70 /* try mapping the NIC resources using VFIO if it exists */
74 if (pci_vfio_is_enabled())
75 ret = pci_vfio_map_resource(dev);
78 case RTE_KDRV_IGB_UIO:
79 case RTE_KDRV_UIO_GENERIC:
80 if (rte_eal_using_phys_addrs()) {
81 /* map resources for devices that use uio */
82 ret = pci_uio_map_resource(dev);
87 " Not managed by a supported kernel driver, skipped\n");
95 /* Unmap pci device */
97 rte_pci_unmap_device(struct rte_pci_device *dev)
99 /* try unmapping the NIC resources using VFIO if it exists */
103 if (pci_vfio_is_enabled())
104 pci_vfio_unmap_resource(dev);
107 case RTE_KDRV_IGB_UIO:
108 case RTE_KDRV_UIO_GENERIC:
109 /* unmap resources for devices that use uio */
110 pci_uio_unmap_resource(dev);
114 " Not managed by a supported kernel driver, skipped\n");
120 pci_find_max_end_va(void)
122 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
123 const struct rte_memseg *last = seg;
126 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
127 if (seg->addr == NULL)
130 if (seg->addr > last->addr)
134 return RTE_PTR_ADD(last->addr, last->len);
137 /* parse one line of the "resource" sysfs file (note that the 'line'
138 * string is modified)
141 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
142 uint64_t *end_addr, uint64_t *flags)
144 union pci_resource_info {
150 char *ptrs[PCI_RESOURCE_FMT_NVAL];
153 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
155 "%s(): bad resource format\n", __func__);
159 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
160 *end_addr = strtoull(res_info.end_addr, NULL, 16);
161 *flags = strtoull(res_info.flags, NULL, 16);
164 "%s(): bad resource format\n", __func__);
171 /* parse the "resource" sysfs file */
173 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
178 uint64_t phys_addr, end_addr, flags;
180 f = fopen(filename, "r");
182 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
186 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
188 if (fgets(buf, sizeof(buf), f) == NULL) {
190 "%s(): cannot read resource\n", __func__);
193 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
194 &end_addr, &flags) < 0)
197 if (flags & IORESOURCE_MEM) {
198 dev->mem_resource[i].phys_addr = phys_addr;
199 dev->mem_resource[i].len = end_addr - phys_addr + 1;
200 /* not mapped for now */
201 dev->mem_resource[i].addr = NULL;
212 /* Scan one pci sysfs entry, and fill the devices list from it. */
214 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
216 char filename[PATH_MAX];
218 struct rte_pci_device *dev;
219 char driver[PATH_MAX];
222 dev = malloc(sizeof(*dev));
226 memset(dev, 0, sizeof(*dev));
230 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
231 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
235 dev->id.vendor_id = (uint16_t)tmp;
238 snprintf(filename, sizeof(filename), "%s/device", dirname);
239 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
243 dev->id.device_id = (uint16_t)tmp;
245 /* get subsystem_vendor id */
246 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
248 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
252 dev->id.subsystem_vendor_id = (uint16_t)tmp;
254 /* get subsystem_device id */
255 snprintf(filename, sizeof(filename), "%s/subsystem_device",
257 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
261 dev->id.subsystem_device_id = (uint16_t)tmp;
264 snprintf(filename, sizeof(filename), "%s/class",
266 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
270 /* the least 24 bits are valid: class, subclass, program interface */
271 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
275 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
276 if (!access(filename, F_OK) &&
277 eal_parse_sysfs_value(filename, &tmp) == 0)
278 dev->max_vfs = (uint16_t)tmp;
280 /* for non igb_uio driver, need kernel version >= 3.8 */
281 snprintf(filename, sizeof(filename),
282 "%s/sriov_numvfs", dirname);
283 if (!access(filename, F_OK) &&
284 eal_parse_sysfs_value(filename, &tmp) == 0)
285 dev->max_vfs = (uint16_t)tmp;
288 /* get numa node, default to 0 if not present */
289 snprintf(filename, sizeof(filename), "%s/numa_node",
292 if (access(filename, F_OK) != -1) {
293 if (eal_parse_sysfs_value(filename, &tmp) == 0)
294 dev->device.numa_node = tmp;
296 dev->device.numa_node = -1;
298 dev->device.numa_node = 0;
303 /* parse resources */
304 snprintf(filename, sizeof(filename), "%s/resource", dirname);
305 if (pci_parse_sysfs_resource(filename, dev) < 0) {
306 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
312 snprintf(filename, sizeof(filename), "%s/driver", dirname);
313 ret = pci_get_kernel_driver_by_path(filename, driver);
315 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
321 if (!strcmp(driver, "vfio-pci"))
322 dev->kdrv = RTE_KDRV_VFIO;
323 else if (!strcmp(driver, "igb_uio"))
324 dev->kdrv = RTE_KDRV_IGB_UIO;
325 else if (!strcmp(driver, "uio_pci_generic"))
326 dev->kdrv = RTE_KDRV_UIO_GENERIC;
328 dev->kdrv = RTE_KDRV_UNKNOWN;
330 dev->kdrv = RTE_KDRV_NONE;
332 /* device is valid, add in list (sorted) */
333 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
334 rte_pci_add_device(dev);
336 struct rte_pci_device *dev2;
339 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
340 ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
345 rte_pci_insert_device(dev2, dev);
346 } else { /* already registered */
347 dev2->kdrv = dev->kdrv;
348 dev2->max_vfs = dev->max_vfs;
350 memmove(dev2->mem_resource, dev->mem_resource,
351 sizeof(dev->mem_resource));
357 rte_pci_add_device(dev);
364 pci_update_device(const struct rte_pci_addr *addr)
366 char filename[PATH_MAX];
368 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
369 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
372 return pci_scan_one(filename, addr);
376 * split up a pci address into its constituent parts.
379 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
381 /* first split on ':' */
389 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
392 char *buf_copy = strndup(buf, bufsize);
393 if (buf_copy == NULL)
396 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
399 /* final split is on '.' between devid and function */
400 splitaddr.function = strchr(splitaddr.devid,'.');
401 if (splitaddr.function == NULL)
403 *splitaddr.function++ = '\0';
405 /* now convert to int values */
407 addr->domain = strtoul(splitaddr.domain, NULL, 16);
408 addr->bus = strtoul(splitaddr.bus, NULL, 16);
409 addr->devid = strtoul(splitaddr.devid, NULL, 16);
410 addr->function = strtoul(splitaddr.function, NULL, 10);
414 free(buf_copy); /* free the copy made with strdup */
422 * Scan the content of the PCI bus, and the devices in the devices
430 char dirname[PATH_MAX];
431 struct rte_pci_addr addr;
433 /* for debug purposes, PCI can be disabled */
434 if (!rte_eal_has_pci())
438 if (!pci_vfio_is_enabled())
439 RTE_LOG(DEBUG, EAL, "VFIO PCI modules not loaded\n");
442 dir = opendir(rte_pci_get_sysfs_path());
444 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
445 __func__, strerror(errno));
449 while ((e = readdir(dir)) != NULL) {
450 if (e->d_name[0] == '.')
453 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
456 snprintf(dirname, sizeof(dirname), "%s/%s",
457 rte_pci_get_sysfs_path(), e->d_name);
459 if (pci_scan_one(dirname, &addr) < 0)
471 * Is pci device bound to any kdrv
474 pci_one_device_is_bound(void)
476 struct rte_pci_device *dev = NULL;
479 FOREACH_DEVICE_ON_PCIBUS(dev) {
480 if (dev->kdrv == RTE_KDRV_UNKNOWN ||
481 dev->kdrv == RTE_KDRV_NONE) {
492 * Any one of the device bound to uio
495 pci_one_device_bound_uio(void)
497 struct rte_pci_device *dev = NULL;
498 struct rte_devargs *devargs;
501 FOREACH_DEVICE_ON_PCIBUS(dev) {
502 devargs = dev->device.devargs;
505 switch (rte_pci_bus.bus.conf.scan_mode) {
506 case RTE_BUS_SCAN_WHITELIST:
507 if (devargs && devargs->policy == RTE_DEV_WHITELISTED)
510 case RTE_BUS_SCAN_UNDEFINED:
511 case RTE_BUS_SCAN_BLACKLIST:
512 if (devargs == NULL ||
513 devargs->policy != RTE_DEV_BLACKLISTED)
521 if (dev->kdrv == RTE_KDRV_IGB_UIO ||
522 dev->kdrv == RTE_KDRV_UIO_GENERIC) {
530 * Any one of the device has iova as va
533 pci_one_device_has_iova_va(void)
535 struct rte_pci_device *dev = NULL;
536 struct rte_pci_driver *drv = NULL;
538 FOREACH_DRIVER_ON_PCIBUS(drv) {
539 if (drv && drv->drv_flags & RTE_PCI_DRV_IOVA_AS_VA) {
540 FOREACH_DEVICE_ON_PCIBUS(dev) {
541 if (dev->kdrv == RTE_KDRV_VFIO &&
542 rte_pci_match(drv, dev))
550 #if defined(RTE_ARCH_X86)
552 pci_one_device_iommu_support_va(struct rte_pci_device *dev)
554 #define VTD_CAP_MGAW_SHIFT 16
555 #define VTD_CAP_MGAW_MASK (0x3fULL << VTD_CAP_MGAW_SHIFT)
556 #define X86_VA_WIDTH 47 /* From Documentation/x86/x86_64/mm.txt */
557 struct rte_pci_addr *addr = &dev->addr;
558 char filename[PATH_MAX];
560 uint64_t mgaw, vtd_cap_reg = 0;
562 snprintf(filename, sizeof(filename),
563 "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
564 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
566 if (access(filename, F_OK) == -1) {
567 /* We don't have an Intel IOMMU, assume VA supported*/
571 /* We have an intel IOMMU */
572 fp = fopen(filename, "r");
574 RTE_LOG(ERR, EAL, "%s(): can't open %s\n", __func__, filename);
578 if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
579 RTE_LOG(ERR, EAL, "%s(): can't read %s\n", __func__, filename);
586 mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
587 if (mgaw < X86_VA_WIDTH)
592 #elif defined(RTE_ARCH_PPC_64)
594 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
600 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
607 * All devices IOMMUs support VA as IOVA
610 pci_devices_iommu_support_va(void)
612 struct rte_pci_device *dev = NULL;
613 struct rte_pci_driver *drv = NULL;
615 FOREACH_DRIVER_ON_PCIBUS(drv) {
616 FOREACH_DEVICE_ON_PCIBUS(dev) {
617 if (!rte_pci_match(drv, dev))
619 if (!pci_one_device_iommu_support_va(dev))
627 * Get iommu class of PCI devices on the bus.
630 rte_pci_get_iommu_class(void)
633 bool is_vfio_noiommu_enabled = true;
638 is_bound = pci_one_device_is_bound();
642 has_iova_va = pci_one_device_has_iova_va();
643 is_bound_uio = pci_one_device_bound_uio();
644 iommu_no_va = !pci_devices_iommu_support_va();
646 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
650 if (has_iova_va && !is_bound_uio && !is_vfio_noiommu_enabled &&
655 RTE_LOG(WARNING, EAL, "Some devices want iova as va but pa will be used because.. ");
656 if (is_vfio_noiommu_enabled)
657 RTE_LOG(WARNING, EAL, "vfio-noiommu mode configured\n");
659 RTE_LOG(WARNING, EAL, "few device bound to UIO\n");
661 RTE_LOG(WARNING, EAL, "IOMMU does not support IOVA as VA\n");
667 /* Read PCI config space. */
668 int rte_pci_read_config(const struct rte_pci_device *device,
669 void *buf, size_t len, off_t offset)
671 const struct rte_intr_handle *intr_handle = &device->intr_handle;
673 switch (intr_handle->type) {
674 case RTE_INTR_HANDLE_UIO:
675 case RTE_INTR_HANDLE_UIO_INTX:
676 return pci_uio_read_config(intr_handle, buf, len, offset);
679 case RTE_INTR_HANDLE_VFIO_MSIX:
680 case RTE_INTR_HANDLE_VFIO_MSI:
681 case RTE_INTR_HANDLE_VFIO_LEGACY:
682 return pci_vfio_read_config(intr_handle, buf, len, offset);
686 "Unknown handle type of fd %d\n",
692 /* Write PCI config space. */
693 int rte_pci_write_config(const struct rte_pci_device *device,
694 const void *buf, size_t len, off_t offset)
696 const struct rte_intr_handle *intr_handle = &device->intr_handle;
698 switch (intr_handle->type) {
699 case RTE_INTR_HANDLE_UIO:
700 case RTE_INTR_HANDLE_UIO_INTX:
701 return pci_uio_write_config(intr_handle, buf, len, offset);
704 case RTE_INTR_HANDLE_VFIO_MSIX:
705 case RTE_INTR_HANDLE_VFIO_MSI:
706 case RTE_INTR_HANDLE_VFIO_LEGACY:
707 return pci_vfio_write_config(intr_handle, buf, len, offset);
711 "Unknown handle type of fd %d\n",
717 #if defined(RTE_ARCH_X86)
719 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
720 struct rte_pci_ioport *p)
729 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
730 dev->addr.domain, dev->addr.bus,
731 dev->addr.devid, dev->addr.function);
733 fp = fopen("/proc/ioports", "r");
735 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
739 while (getdelim(&line, &linesz, '\n', fp) > 0) {
744 n = strcspn(ptr, ":");
748 while (*left && isspace(*left))
751 if (!strncmp(left, pci_id, strlen(pci_id))) {
754 while (*ptr && isspace(*ptr))
757 sscanf(ptr, "%04hx-%04hx", &start, &end);
770 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
777 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
778 struct rte_pci_ioport *p)
785 if (pci_vfio_is_enabled())
786 ret = pci_vfio_ioport_map(dev, bar, p);
789 case RTE_KDRV_IGB_UIO:
790 ret = pci_uio_ioport_map(dev, bar, p);
792 case RTE_KDRV_UIO_GENERIC:
793 #if defined(RTE_ARCH_X86)
794 ret = pci_ioport_map(dev, bar, p);
796 ret = pci_uio_ioport_map(dev, bar, p);
800 #if defined(RTE_ARCH_X86)
801 ret = pci_ioport_map(dev, bar, p);
815 rte_pci_ioport_read(struct rte_pci_ioport *p,
816 void *data, size_t len, off_t offset)
818 switch (p->dev->kdrv) {
821 pci_vfio_ioport_read(p, data, len, offset);
824 case RTE_KDRV_IGB_UIO:
825 pci_uio_ioport_read(p, data, len, offset);
827 case RTE_KDRV_UIO_GENERIC:
828 pci_uio_ioport_read(p, data, len, offset);
831 #if defined(RTE_ARCH_X86)
832 pci_uio_ioport_read(p, data, len, offset);
841 rte_pci_ioport_write(struct rte_pci_ioport *p,
842 const void *data, size_t len, off_t offset)
844 switch (p->dev->kdrv) {
847 pci_vfio_ioport_write(p, data, len, offset);
850 case RTE_KDRV_IGB_UIO:
851 pci_uio_ioport_write(p, data, len, offset);
853 case RTE_KDRV_UIO_GENERIC:
854 pci_uio_ioport_write(p, data, len, offset);
857 #if defined(RTE_ARCH_X86)
858 pci_uio_ioport_write(p, data, len, offset);
867 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
871 switch (p->dev->kdrv) {
874 if (pci_vfio_is_enabled())
875 ret = pci_vfio_ioport_unmap(p);
878 case RTE_KDRV_IGB_UIO:
879 ret = pci_uio_ioport_unmap(p);
881 case RTE_KDRV_UIO_GENERIC:
882 #if defined(RTE_ARCH_X86)
885 ret = pci_uio_ioport_unmap(p);
889 #if defined(RTE_ARCH_X86)