4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <rte_bus_pci.h>
41 #include <rte_eal_memconfig.h>
42 #include <rte_malloc.h>
43 #include <rte_devargs.h>
44 #include <rte_memcpy.h>
47 #include "eal_private.h"
48 #include "eal_filesystem.h"
55 * PCI probing under linux
57 * This code is used to simulate a PCI probe by parsing information in sysfs.
58 * When a registered device matches a driver, it is then initialized with
59 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
62 extern struct rte_pci_bus rte_pci_bus;
65 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
71 if (!filename || !dri_name)
74 count = readlink(filename, path, PATH_MAX);
75 if (count >= PATH_MAX)
78 /* For device does not have a driver */
84 name = strrchr(path, '/');
86 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
95 rte_pci_map_device(struct rte_pci_device *dev)
99 /* try mapping the NIC resources using VFIO if it exists */
103 if (pci_vfio_is_enabled())
104 ret = pci_vfio_map_resource(dev);
107 case RTE_KDRV_IGB_UIO:
108 case RTE_KDRV_UIO_GENERIC:
109 if (rte_eal_using_phys_addrs()) {
110 /* map resources for devices that use uio */
111 ret = pci_uio_map_resource(dev);
116 " Not managed by a supported kernel driver, skipped\n");
124 /* Unmap pci device */
126 rte_pci_unmap_device(struct rte_pci_device *dev)
128 /* try unmapping the NIC resources using VFIO if it exists */
132 if (pci_vfio_is_enabled())
133 pci_vfio_unmap_resource(dev);
136 case RTE_KDRV_IGB_UIO:
137 case RTE_KDRV_UIO_GENERIC:
138 /* unmap resources for devices that use uio */
139 pci_uio_unmap_resource(dev);
143 " Not managed by a supported kernel driver, skipped\n");
149 pci_find_max_end_va(void)
151 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
152 const struct rte_memseg *last = seg;
155 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
156 if (seg->addr == NULL)
159 if (seg->addr > last->addr)
163 return RTE_PTR_ADD(last->addr, last->len);
166 /* parse one line of the "resource" sysfs file (note that the 'line'
167 * string is modified)
170 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
171 uint64_t *end_addr, uint64_t *flags)
173 union pci_resource_info {
179 char *ptrs[PCI_RESOURCE_FMT_NVAL];
182 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
184 "%s(): bad resource format\n", __func__);
188 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
189 *end_addr = strtoull(res_info.end_addr, NULL, 16);
190 *flags = strtoull(res_info.flags, NULL, 16);
193 "%s(): bad resource format\n", __func__);
200 /* parse the "resource" sysfs file */
202 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
207 uint64_t phys_addr, end_addr, flags;
209 f = fopen(filename, "r");
211 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
215 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
217 if (fgets(buf, sizeof(buf), f) == NULL) {
219 "%s(): cannot read resource\n", __func__);
222 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
223 &end_addr, &flags) < 0)
226 if (flags & IORESOURCE_MEM) {
227 dev->mem_resource[i].phys_addr = phys_addr;
228 dev->mem_resource[i].len = end_addr - phys_addr + 1;
229 /* not mapped for now */
230 dev->mem_resource[i].addr = NULL;
241 /* Scan one pci sysfs entry, and fill the devices list from it. */
243 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
245 char filename[PATH_MAX];
247 struct rte_pci_device *dev;
248 char driver[PATH_MAX];
251 dev = malloc(sizeof(*dev));
255 memset(dev, 0, sizeof(*dev));
259 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
260 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
264 dev->id.vendor_id = (uint16_t)tmp;
267 snprintf(filename, sizeof(filename), "%s/device", dirname);
268 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
272 dev->id.device_id = (uint16_t)tmp;
274 /* get subsystem_vendor id */
275 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
277 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
281 dev->id.subsystem_vendor_id = (uint16_t)tmp;
283 /* get subsystem_device id */
284 snprintf(filename, sizeof(filename), "%s/subsystem_device",
286 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
290 dev->id.subsystem_device_id = (uint16_t)tmp;
293 snprintf(filename, sizeof(filename), "%s/class",
295 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
299 /* the least 24 bits are valid: class, subclass, program interface */
300 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
304 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
305 if (!access(filename, F_OK) &&
306 eal_parse_sysfs_value(filename, &tmp) == 0)
307 dev->max_vfs = (uint16_t)tmp;
309 /* for non igb_uio driver, need kernel version >= 3.8 */
310 snprintf(filename, sizeof(filename),
311 "%s/sriov_numvfs", dirname);
312 if (!access(filename, F_OK) &&
313 eal_parse_sysfs_value(filename, &tmp) == 0)
314 dev->max_vfs = (uint16_t)tmp;
317 /* get numa node, default to 0 if not present */
318 snprintf(filename, sizeof(filename), "%s/numa_node",
321 if (access(filename, F_OK) != -1) {
322 if (eal_parse_sysfs_value(filename, &tmp) == 0)
323 dev->device.numa_node = tmp;
325 dev->device.numa_node = -1;
327 dev->device.numa_node = 0;
332 /* parse resources */
333 snprintf(filename, sizeof(filename), "%s/resource", dirname);
334 if (pci_parse_sysfs_resource(filename, dev) < 0) {
335 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
341 snprintf(filename, sizeof(filename), "%s/driver", dirname);
342 ret = pci_get_kernel_driver_by_path(filename, driver);
344 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
350 if (!strcmp(driver, "vfio-pci"))
351 dev->kdrv = RTE_KDRV_VFIO;
352 else if (!strcmp(driver, "igb_uio"))
353 dev->kdrv = RTE_KDRV_IGB_UIO;
354 else if (!strcmp(driver, "uio_pci_generic"))
355 dev->kdrv = RTE_KDRV_UIO_GENERIC;
357 dev->kdrv = RTE_KDRV_UNKNOWN;
359 dev->kdrv = RTE_KDRV_NONE;
361 /* device is valid, add in list (sorted) */
362 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
363 rte_pci_add_device(dev);
365 struct rte_pci_device *dev2;
368 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
369 ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
374 rte_pci_insert_device(dev2, dev);
375 } else { /* already registered */
376 dev2->kdrv = dev->kdrv;
377 dev2->max_vfs = dev->max_vfs;
379 memmove(dev2->mem_resource, dev->mem_resource,
380 sizeof(dev->mem_resource));
386 rte_pci_add_device(dev);
393 pci_update_device(const struct rte_pci_addr *addr)
395 char filename[PATH_MAX];
397 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
398 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
401 return pci_scan_one(filename, addr);
405 * split up a pci address into its constituent parts.
408 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
410 /* first split on ':' */
418 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
421 char *buf_copy = strndup(buf, bufsize);
422 if (buf_copy == NULL)
425 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
428 /* final split is on '.' between devid and function */
429 splitaddr.function = strchr(splitaddr.devid,'.');
430 if (splitaddr.function == NULL)
432 *splitaddr.function++ = '\0';
434 /* now convert to int values */
436 addr->domain = strtoul(splitaddr.domain, NULL, 16);
437 addr->bus = strtoul(splitaddr.bus, NULL, 16);
438 addr->devid = strtoul(splitaddr.devid, NULL, 16);
439 addr->function = strtoul(splitaddr.function, NULL, 10);
443 free(buf_copy); /* free the copy made with strdup */
451 * Scan the content of the PCI bus, and the devices in the devices
459 char dirname[PATH_MAX];
460 struct rte_pci_addr addr;
462 /* for debug purposes, PCI can be disabled */
463 if (!rte_eal_has_pci())
467 if (!pci_vfio_is_enabled())
468 RTE_LOG(DEBUG, EAL, "VFIO PCI modules not loaded\n");
471 dir = opendir(rte_pci_get_sysfs_path());
473 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
474 __func__, strerror(errno));
478 while ((e = readdir(dir)) != NULL) {
479 if (e->d_name[0] == '.')
482 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
485 snprintf(dirname, sizeof(dirname), "%s/%s",
486 rte_pci_get_sysfs_path(), e->d_name);
488 if (pci_scan_one(dirname, &addr) < 0)
500 * Is pci device bound to any kdrv
503 pci_one_device_is_bound(void)
505 struct rte_pci_device *dev = NULL;
508 FOREACH_DEVICE_ON_PCIBUS(dev) {
509 if (dev->kdrv == RTE_KDRV_UNKNOWN ||
510 dev->kdrv == RTE_KDRV_NONE) {
521 * Any one of the device bound to uio
524 pci_one_device_bound_uio(void)
526 struct rte_pci_device *dev = NULL;
527 struct rte_devargs *devargs;
530 FOREACH_DEVICE_ON_PCIBUS(dev) {
531 devargs = dev->device.devargs;
534 switch (rte_pci_bus.bus.conf.scan_mode) {
535 case RTE_BUS_SCAN_WHITELIST:
536 if (devargs && devargs->policy == RTE_DEV_WHITELISTED)
539 case RTE_BUS_SCAN_UNDEFINED:
540 case RTE_BUS_SCAN_BLACKLIST:
541 if (devargs == NULL ||
542 devargs->policy != RTE_DEV_BLACKLISTED)
550 if (dev->kdrv == RTE_KDRV_IGB_UIO ||
551 dev->kdrv == RTE_KDRV_UIO_GENERIC) {
559 * Any one of the device has iova as va
562 pci_one_device_has_iova_va(void)
564 struct rte_pci_device *dev = NULL;
565 struct rte_pci_driver *drv = NULL;
567 FOREACH_DRIVER_ON_PCIBUS(drv) {
568 if (drv && drv->drv_flags & RTE_PCI_DRV_IOVA_AS_VA) {
569 FOREACH_DEVICE_ON_PCIBUS(dev) {
570 if (dev->kdrv == RTE_KDRV_VFIO &&
571 rte_pci_match(drv, dev))
579 #if defined(RTE_ARCH_X86)
581 pci_one_device_iommu_support_va(struct rte_pci_device *dev)
583 #define VTD_CAP_MGAW_SHIFT 16
584 #define VTD_CAP_MGAW_MASK (0x3fULL << VTD_CAP_MGAW_SHIFT)
585 #define X86_VA_WIDTH 47 /* From Documentation/x86/x86_64/mm.txt */
586 struct rte_pci_addr *addr = &dev->addr;
587 char filename[PATH_MAX];
589 uint64_t mgaw, vtd_cap_reg = 0;
591 snprintf(filename, sizeof(filename),
592 "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
593 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
595 if (access(filename, F_OK) == -1) {
596 /* We don't have an Intel IOMMU, assume VA supported*/
600 /* We have an intel IOMMU */
601 fp = fopen(filename, "r");
603 RTE_LOG(ERR, EAL, "%s(): can't open %s\n", __func__, filename);
607 if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
608 RTE_LOG(ERR, EAL, "%s(): can't read %s\n", __func__, filename);
615 mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
616 if (mgaw < X86_VA_WIDTH)
621 #elif defined(RTE_ARCH_PPC_64)
623 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
629 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
636 * All devices IOMMUs support VA as IOVA
639 pci_devices_iommu_support_va(void)
641 struct rte_pci_device *dev = NULL;
642 struct rte_pci_driver *drv = NULL;
644 FOREACH_DRIVER_ON_PCIBUS(drv) {
645 FOREACH_DEVICE_ON_PCIBUS(dev) {
646 if (!rte_pci_match(drv, dev))
648 if (!pci_one_device_iommu_support_va(dev))
656 * Get iommu class of PCI devices on the bus.
659 rte_pci_get_iommu_class(void)
662 bool is_vfio_noiommu_enabled = true;
667 is_bound = pci_one_device_is_bound();
671 has_iova_va = pci_one_device_has_iova_va();
672 is_bound_uio = pci_one_device_bound_uio();
673 iommu_no_va = !pci_devices_iommu_support_va();
675 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
679 if (has_iova_va && !is_bound_uio && !is_vfio_noiommu_enabled &&
684 RTE_LOG(WARNING, EAL, "Some devices want iova as va but pa will be used because.. ");
685 if (is_vfio_noiommu_enabled)
686 RTE_LOG(WARNING, EAL, "vfio-noiommu mode configured\n");
688 RTE_LOG(WARNING, EAL, "few device bound to UIO\n");
690 RTE_LOG(WARNING, EAL, "IOMMU does not support IOVA as VA\n");
696 /* Read PCI config space. */
697 int rte_pci_read_config(const struct rte_pci_device *device,
698 void *buf, size_t len, off_t offset)
700 const struct rte_intr_handle *intr_handle = &device->intr_handle;
702 switch (intr_handle->type) {
703 case RTE_INTR_HANDLE_UIO:
704 case RTE_INTR_HANDLE_UIO_INTX:
705 return pci_uio_read_config(intr_handle, buf, len, offset);
708 case RTE_INTR_HANDLE_VFIO_MSIX:
709 case RTE_INTR_HANDLE_VFIO_MSI:
710 case RTE_INTR_HANDLE_VFIO_LEGACY:
711 return pci_vfio_read_config(intr_handle, buf, len, offset);
715 "Unknown handle type of fd %d\n",
721 /* Write PCI config space. */
722 int rte_pci_write_config(const struct rte_pci_device *device,
723 const void *buf, size_t len, off_t offset)
725 const struct rte_intr_handle *intr_handle = &device->intr_handle;
727 switch (intr_handle->type) {
728 case RTE_INTR_HANDLE_UIO:
729 case RTE_INTR_HANDLE_UIO_INTX:
730 return pci_uio_write_config(intr_handle, buf, len, offset);
733 case RTE_INTR_HANDLE_VFIO_MSIX:
734 case RTE_INTR_HANDLE_VFIO_MSI:
735 case RTE_INTR_HANDLE_VFIO_LEGACY:
736 return pci_vfio_write_config(intr_handle, buf, len, offset);
740 "Unknown handle type of fd %d\n",
746 #if defined(RTE_ARCH_X86)
748 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
749 struct rte_pci_ioport *p)
758 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
759 dev->addr.domain, dev->addr.bus,
760 dev->addr.devid, dev->addr.function);
762 fp = fopen("/proc/ioports", "r");
764 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
768 while (getdelim(&line, &linesz, '\n', fp) > 0) {
773 n = strcspn(ptr, ":");
777 while (*left && isspace(*left))
780 if (!strncmp(left, pci_id, strlen(pci_id))) {
783 while (*ptr && isspace(*ptr))
786 sscanf(ptr, "%04hx-%04hx", &start, &end);
799 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
806 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
807 struct rte_pci_ioport *p)
814 if (pci_vfio_is_enabled())
815 ret = pci_vfio_ioport_map(dev, bar, p);
818 case RTE_KDRV_IGB_UIO:
819 ret = pci_uio_ioport_map(dev, bar, p);
821 case RTE_KDRV_UIO_GENERIC:
822 #if defined(RTE_ARCH_X86)
823 ret = pci_ioport_map(dev, bar, p);
825 ret = pci_uio_ioport_map(dev, bar, p);
829 #if defined(RTE_ARCH_X86)
830 ret = pci_ioport_map(dev, bar, p);
844 rte_pci_ioport_read(struct rte_pci_ioport *p,
845 void *data, size_t len, off_t offset)
847 switch (p->dev->kdrv) {
850 pci_vfio_ioport_read(p, data, len, offset);
853 case RTE_KDRV_IGB_UIO:
854 pci_uio_ioport_read(p, data, len, offset);
856 case RTE_KDRV_UIO_GENERIC:
857 pci_uio_ioport_read(p, data, len, offset);
860 #if defined(RTE_ARCH_X86)
861 pci_uio_ioport_read(p, data, len, offset);
870 rte_pci_ioport_write(struct rte_pci_ioport *p,
871 const void *data, size_t len, off_t offset)
873 switch (p->dev->kdrv) {
876 pci_vfio_ioport_write(p, data, len, offset);
879 case RTE_KDRV_IGB_UIO:
880 pci_uio_ioport_write(p, data, len, offset);
882 case RTE_KDRV_UIO_GENERIC:
883 pci_uio_ioport_write(p, data, len, offset);
886 #if defined(RTE_ARCH_X86)
887 pci_uio_ioport_write(p, data, len, offset);
896 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
900 switch (p->dev->kdrv) {
903 if (pci_vfio_is_enabled())
904 ret = pci_vfio_ioport_unmap(p);
907 case RTE_KDRV_IGB_UIO:
908 ret = pci_uio_ioport_unmap(p);
910 case RTE_KDRV_UIO_GENERIC:
911 #if defined(RTE_ARCH_X86)
914 ret = pci_uio_ioport_unmap(p);
918 #if defined(RTE_ARCH_X86)