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>
46 #include <rte_memory.h>
48 #include "eal_private.h"
49 #include "eal_filesystem.h"
56 * PCI probing under linux
58 * This code is used to simulate a PCI probe by parsing information in sysfs.
59 * When a registered device matches a driver, it is then initialized with
60 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
63 extern struct rte_pci_bus rte_pci_bus;
66 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
72 if (!filename || !dri_name)
75 count = readlink(filename, path, PATH_MAX);
76 if (count >= PATH_MAX)
79 /* For device does not have a driver */
85 name = strrchr(path, '/');
87 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
96 rte_pci_map_device(struct rte_pci_device *dev)
100 /* try mapping the NIC resources using VFIO if it exists */
104 if (pci_vfio_is_enabled())
105 ret = pci_vfio_map_resource(dev);
108 case RTE_KDRV_IGB_UIO:
109 case RTE_KDRV_UIO_GENERIC:
110 if (rte_eal_using_phys_addrs()) {
111 /* map resources for devices that use uio */
112 ret = pci_uio_map_resource(dev);
117 " Not managed by a supported kernel driver, skipped\n");
125 /* Unmap pci device */
127 rte_pci_unmap_device(struct rte_pci_device *dev)
129 /* try unmapping the NIC resources using VFIO if it exists */
133 if (pci_vfio_is_enabled())
134 pci_vfio_unmap_resource(dev);
137 case RTE_KDRV_IGB_UIO:
138 case RTE_KDRV_UIO_GENERIC:
139 /* unmap resources for devices that use uio */
140 pci_uio_unmap_resource(dev);
144 " Not managed by a supported kernel driver, skipped\n");
150 pci_find_max_end_va(void)
152 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
153 const struct rte_memseg *last = seg;
156 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
157 if (seg->addr == NULL)
160 if (seg->addr > last->addr)
164 return RTE_PTR_ADD(last->addr, last->len);
167 /* parse one line of the "resource" sysfs file (note that the 'line'
168 * string is modified)
171 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
172 uint64_t *end_addr, uint64_t *flags)
174 union pci_resource_info {
180 char *ptrs[PCI_RESOURCE_FMT_NVAL];
183 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
185 "%s(): bad resource format\n", __func__);
189 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
190 *end_addr = strtoull(res_info.end_addr, NULL, 16);
191 *flags = strtoull(res_info.flags, NULL, 16);
194 "%s(): bad resource format\n", __func__);
201 /* parse the "resource" sysfs file */
203 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
208 uint64_t phys_addr, end_addr, flags;
210 f = fopen(filename, "r");
212 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
216 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
218 if (fgets(buf, sizeof(buf), f) == NULL) {
220 "%s(): cannot read resource\n", __func__);
223 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
224 &end_addr, &flags) < 0)
227 if (flags & IORESOURCE_MEM) {
228 dev->mem_resource[i].phys_addr = phys_addr;
229 dev->mem_resource[i].len = end_addr - phys_addr + 1;
230 /* not mapped for now */
231 dev->mem_resource[i].addr = NULL;
242 /* Scan one pci sysfs entry, and fill the devices list from it. */
244 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
246 char filename[PATH_MAX];
248 struct rte_pci_device *dev;
249 char driver[PATH_MAX];
252 dev = malloc(sizeof(*dev));
256 memset(dev, 0, sizeof(*dev));
260 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
261 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
265 dev->id.vendor_id = (uint16_t)tmp;
268 snprintf(filename, sizeof(filename), "%s/device", dirname);
269 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
273 dev->id.device_id = (uint16_t)tmp;
275 /* get subsystem_vendor id */
276 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
278 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
282 dev->id.subsystem_vendor_id = (uint16_t)tmp;
284 /* get subsystem_device id */
285 snprintf(filename, sizeof(filename), "%s/subsystem_device",
287 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
291 dev->id.subsystem_device_id = (uint16_t)tmp;
294 snprintf(filename, sizeof(filename), "%s/class",
296 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
300 /* the least 24 bits are valid: class, subclass, program interface */
301 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
305 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
306 if (!access(filename, F_OK) &&
307 eal_parse_sysfs_value(filename, &tmp) == 0)
308 dev->max_vfs = (uint16_t)tmp;
310 /* for non igb_uio driver, need kernel version >= 3.8 */
311 snprintf(filename, sizeof(filename),
312 "%s/sriov_numvfs", dirname);
313 if (!access(filename, F_OK) &&
314 eal_parse_sysfs_value(filename, &tmp) == 0)
315 dev->max_vfs = (uint16_t)tmp;
318 /* get numa node, default to 0 if not present */
319 snprintf(filename, sizeof(filename), "%s/numa_node",
322 if (access(filename, F_OK) != -1) {
323 if (eal_parse_sysfs_value(filename, &tmp) == 0)
324 dev->device.numa_node = tmp;
326 dev->device.numa_node = -1;
328 dev->device.numa_node = 0;
333 /* parse resources */
334 snprintf(filename, sizeof(filename), "%s/resource", dirname);
335 if (pci_parse_sysfs_resource(filename, dev) < 0) {
336 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
342 snprintf(filename, sizeof(filename), "%s/driver", dirname);
343 ret = pci_get_kernel_driver_by_path(filename, driver);
345 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
351 if (!strcmp(driver, "vfio-pci"))
352 dev->kdrv = RTE_KDRV_VFIO;
353 else if (!strcmp(driver, "igb_uio"))
354 dev->kdrv = RTE_KDRV_IGB_UIO;
355 else if (!strcmp(driver, "uio_pci_generic"))
356 dev->kdrv = RTE_KDRV_UIO_GENERIC;
358 dev->kdrv = RTE_KDRV_UNKNOWN;
360 dev->kdrv = RTE_KDRV_NONE;
362 /* device is valid, add in list (sorted) */
363 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
364 rte_pci_add_device(dev);
366 struct rte_pci_device *dev2;
369 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
370 ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
375 rte_pci_insert_device(dev2, dev);
376 } else { /* already registered */
377 dev2->kdrv = dev->kdrv;
378 dev2->max_vfs = dev->max_vfs;
380 memmove(dev2->mem_resource, dev->mem_resource,
381 sizeof(dev->mem_resource));
387 rte_pci_add_device(dev);
394 pci_update_device(const struct rte_pci_addr *addr)
396 char filename[PATH_MAX];
398 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
399 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
402 return pci_scan_one(filename, addr);
406 * split up a pci address into its constituent parts.
409 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
411 /* first split on ':' */
419 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
422 char *buf_copy = strndup(buf, bufsize);
423 if (buf_copy == NULL)
426 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
429 /* final split is on '.' between devid and function */
430 splitaddr.function = strchr(splitaddr.devid,'.');
431 if (splitaddr.function == NULL)
433 *splitaddr.function++ = '\0';
435 /* now convert to int values */
437 addr->domain = strtoul(splitaddr.domain, NULL, 16);
438 addr->bus = strtoul(splitaddr.bus, NULL, 16);
439 addr->devid = strtoul(splitaddr.devid, NULL, 16);
440 addr->function = strtoul(splitaddr.function, NULL, 10);
444 free(buf_copy); /* free the copy made with strdup */
452 * Scan the content of the PCI bus, and the devices in the devices
460 char dirname[PATH_MAX];
461 struct rte_pci_addr addr;
463 /* for debug purposes, PCI can be disabled */
464 if (!rte_eal_has_pci())
468 if (!pci_vfio_is_enabled())
469 RTE_LOG(DEBUG, EAL, "VFIO PCI modules not loaded\n");
472 dir = opendir(rte_pci_get_sysfs_path());
474 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
475 __func__, strerror(errno));
479 while ((e = readdir(dir)) != NULL) {
480 if (e->d_name[0] == '.')
483 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
486 snprintf(dirname, sizeof(dirname), "%s/%s",
487 rte_pci_get_sysfs_path(), e->d_name);
489 if (pci_scan_one(dirname, &addr) < 0)
501 * Is pci device bound to any kdrv
504 pci_one_device_is_bound(void)
506 struct rte_pci_device *dev = NULL;
509 FOREACH_DEVICE_ON_PCIBUS(dev) {
510 if (dev->kdrv == RTE_KDRV_UNKNOWN ||
511 dev->kdrv == RTE_KDRV_NONE) {
522 * Any one of the device bound to uio
525 pci_one_device_bound_uio(void)
527 struct rte_pci_device *dev = NULL;
528 struct rte_devargs *devargs;
531 FOREACH_DEVICE_ON_PCIBUS(dev) {
532 devargs = dev->device.devargs;
535 switch (rte_pci_bus.bus.conf.scan_mode) {
536 case RTE_BUS_SCAN_WHITELIST:
537 if (devargs && devargs->policy == RTE_DEV_WHITELISTED)
540 case RTE_BUS_SCAN_UNDEFINED:
541 case RTE_BUS_SCAN_BLACKLIST:
542 if (devargs == NULL ||
543 devargs->policy != RTE_DEV_BLACKLISTED)
551 if (dev->kdrv == RTE_KDRV_IGB_UIO ||
552 dev->kdrv == RTE_KDRV_UIO_GENERIC) {
560 * Any one of the device has iova as va
563 pci_one_device_has_iova_va(void)
565 struct rte_pci_device *dev = NULL;
566 struct rte_pci_driver *drv = NULL;
568 FOREACH_DRIVER_ON_PCIBUS(drv) {
569 if (drv && drv->drv_flags & RTE_PCI_DRV_IOVA_AS_VA) {
570 FOREACH_DEVICE_ON_PCIBUS(dev) {
571 if (dev->kdrv == RTE_KDRV_VFIO &&
572 rte_pci_match(drv, dev))
580 #if defined(RTE_ARCH_X86)
582 pci_one_device_iommu_support_va(struct rte_pci_device *dev)
584 #define VTD_CAP_MGAW_SHIFT 16
585 #define VTD_CAP_MGAW_MASK (0x3fULL << VTD_CAP_MGAW_SHIFT)
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;
617 if (!rte_eal_check_dma_mask(mgaw))
623 #elif defined(RTE_ARCH_PPC_64)
625 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
631 pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
638 * All devices IOMMUs support VA as IOVA
641 pci_devices_iommu_support_va(void)
643 struct rte_pci_device *dev = NULL;
644 struct rte_pci_driver *drv = NULL;
645 int iommu_dma_mask_check_done = 0;
647 FOREACH_DRIVER_ON_PCIBUS(drv) {
648 FOREACH_DEVICE_ON_PCIBUS(dev) {
649 if (!rte_pci_match(drv, dev))
651 if (!iommu_dma_mask_check_done) {
652 if (!pci_one_device_iommu_support_va(dev))
654 iommu_dma_mask_check_done = 1;
662 * Get iommu class of PCI devices on the bus.
665 rte_pci_get_iommu_class(void)
668 bool is_vfio_noiommu_enabled = true;
673 is_bound = pci_one_device_is_bound();
677 has_iova_va = pci_one_device_has_iova_va();
678 is_bound_uio = pci_one_device_bound_uio();
679 iommu_no_va = !pci_devices_iommu_support_va();
681 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
685 if (has_iova_va && !is_bound_uio && !is_vfio_noiommu_enabled &&
690 RTE_LOG(WARNING, EAL, "Some devices want iova as va but pa will be used because.. ");
691 if (is_vfio_noiommu_enabled)
692 RTE_LOG(WARNING, EAL, "vfio-noiommu mode configured\n");
694 RTE_LOG(WARNING, EAL, "few device bound to UIO\n");
696 RTE_LOG(WARNING, EAL, "IOMMU does not support IOVA as VA\n");
702 /* Read PCI config space. */
703 int rte_pci_read_config(const struct rte_pci_device *device,
704 void *buf, size_t len, off_t offset)
706 const struct rte_intr_handle *intr_handle = &device->intr_handle;
708 switch (intr_handle->type) {
709 case RTE_INTR_HANDLE_UIO:
710 case RTE_INTR_HANDLE_UIO_INTX:
711 return pci_uio_read_config(intr_handle, buf, len, offset);
714 case RTE_INTR_HANDLE_VFIO_MSIX:
715 case RTE_INTR_HANDLE_VFIO_MSI:
716 case RTE_INTR_HANDLE_VFIO_LEGACY:
717 return pci_vfio_read_config(intr_handle, buf, len, offset);
721 "Unknown handle type of fd %d\n",
727 /* Write PCI config space. */
728 int rte_pci_write_config(const struct rte_pci_device *device,
729 const void *buf, size_t len, off_t offset)
731 const struct rte_intr_handle *intr_handle = &device->intr_handle;
733 switch (intr_handle->type) {
734 case RTE_INTR_HANDLE_UIO:
735 case RTE_INTR_HANDLE_UIO_INTX:
736 return pci_uio_write_config(intr_handle, buf, len, offset);
739 case RTE_INTR_HANDLE_VFIO_MSIX:
740 case RTE_INTR_HANDLE_VFIO_MSI:
741 case RTE_INTR_HANDLE_VFIO_LEGACY:
742 return pci_vfio_write_config(intr_handle, buf, len, offset);
746 "Unknown handle type of fd %d\n",
752 #if defined(RTE_ARCH_X86)
754 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
755 struct rte_pci_ioport *p)
764 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
765 dev->addr.domain, dev->addr.bus,
766 dev->addr.devid, dev->addr.function);
768 fp = fopen("/proc/ioports", "r");
770 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
774 while (getdelim(&line, &linesz, '\n', fp) > 0) {
779 n = strcspn(ptr, ":");
783 while (*left && isspace(*left))
786 if (!strncmp(left, pci_id, strlen(pci_id))) {
789 while (*ptr && isspace(*ptr))
792 sscanf(ptr, "%04hx-%04hx", &start, &end);
805 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
812 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
813 struct rte_pci_ioport *p)
820 if (pci_vfio_is_enabled())
821 ret = pci_vfio_ioport_map(dev, bar, p);
824 case RTE_KDRV_IGB_UIO:
825 ret = pci_uio_ioport_map(dev, bar, p);
827 case RTE_KDRV_UIO_GENERIC:
828 #if defined(RTE_ARCH_X86)
829 ret = pci_ioport_map(dev, bar, p);
831 ret = pci_uio_ioport_map(dev, bar, p);
835 #if defined(RTE_ARCH_X86)
836 ret = pci_ioport_map(dev, bar, p);
850 rte_pci_ioport_read(struct rte_pci_ioport *p,
851 void *data, size_t len, off_t offset)
853 switch (p->dev->kdrv) {
856 pci_vfio_ioport_read(p, data, len, offset);
859 case RTE_KDRV_IGB_UIO:
860 pci_uio_ioport_read(p, data, len, offset);
862 case RTE_KDRV_UIO_GENERIC:
863 pci_uio_ioport_read(p, data, len, offset);
866 #if defined(RTE_ARCH_X86)
867 pci_uio_ioport_read(p, data, len, offset);
876 rte_pci_ioport_write(struct rte_pci_ioport *p,
877 const void *data, size_t len, off_t offset)
879 switch (p->dev->kdrv) {
882 pci_vfio_ioport_write(p, data, len, offset);
885 case RTE_KDRV_IGB_UIO:
886 pci_uio_ioport_write(p, data, len, offset);
888 case RTE_KDRV_UIO_GENERIC:
889 pci_uio_ioport_write(p, data, len, offset);
892 #if defined(RTE_ARCH_X86)
893 pci_uio_ioport_write(p, data, len, offset);
902 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
906 switch (p->dev->kdrv) {
909 if (pci_vfio_is_enabled())
910 ret = pci_vfio_ioport_unmap(p);
913 case RTE_KDRV_IGB_UIO:
914 ret = pci_uio_ioport_unmap(p);
916 case RTE_KDRV_UIO_GENERIC:
917 #if defined(RTE_ARCH_X86)
920 ret = pci_uio_ioport_unmap(p);
924 #if defined(RTE_ARCH_X86)