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.
41 #include <sys/types.h>
47 #include <sys/queue.h>
49 #include <sys/ioctl.h>
50 #include <sys/pciio.h>
51 #include <dev/pci/pcireg.h>
53 #if defined(RTE_ARCH_X86)
54 #include <machine/cpufunc.h>
57 #include <rte_interrupts.h>
60 #include <rte_bus_pci.h>
61 #include <rte_common.h>
62 #include <rte_launch.h>
63 #include <rte_memory.h>
64 #include <rte_memzone.h>
66 #include <rte_eal_memconfig.h>
67 #include <rte_per_lcore.h>
68 #include <rte_lcore.h>
69 #include <rte_malloc.h>
70 #include <rte_string_fns.h>
71 #include <rte_debug.h>
72 #include <rte_devargs.h>
74 #include "eal_filesystem.h"
79 * PCI probing under linux
81 * This code is used to simulate a PCI probe by parsing information in
82 * sysfs. Moreover, when a registered driver matches a device, the
83 * kernel driver currently using it is unloaded and replaced by
84 * igb_uio module, which is a very minimal userland driver for Intel
85 * network card, only providing access to PCI BAR to applications, and
86 * enabling bus master.
89 extern struct rte_pci_bus rte_pci_bus;
93 rte_pci_map_device(struct rte_pci_device *dev)
97 /* try mapping the NIC resources */
99 case RTE_KDRV_NIC_UIO:
100 /* map resources for devices that use uio */
101 ret = pci_uio_map_resource(dev);
105 " Not managed by a supported kernel driver, skipped\n");
113 /* Unmap pci device */
115 rte_pci_unmap_device(struct rte_pci_device *dev)
117 /* try unmapping the NIC resources */
119 case RTE_KDRV_NIC_UIO:
120 /* unmap resources for devices that use uio */
121 pci_uio_unmap_resource(dev);
125 " Not managed by a supported kernel driver, skipped\n");
131 pci_uio_free_resource(struct rte_pci_device *dev,
132 struct mapped_pci_resource *uio_res)
136 if (dev->intr_handle.fd) {
137 close(dev->intr_handle.fd);
138 dev->intr_handle.fd = -1;
139 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
144 pci_uio_alloc_resource(struct rte_pci_device *dev,
145 struct mapped_pci_resource **uio_res)
147 char devname[PATH_MAX]; /* contains the /dev/uioX */
148 struct rte_pci_addr *loc;
152 snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
153 dev->addr.bus, dev->addr.devid, dev->addr.function);
155 if (access(devname, O_RDWR) < 0) {
156 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
157 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
161 /* save fd if in primary process */
162 dev->intr_handle.fd = open(devname, O_RDWR);
163 if (dev->intr_handle.fd < 0) {
164 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
165 devname, strerror(errno));
168 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
170 /* allocate the mapping details for secondary processes*/
171 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
172 if (*uio_res == NULL) {
174 "%s(): cannot store uio mmap details\n", __func__);
178 snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
179 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
184 pci_uio_free_resource(dev, *uio_res);
189 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
190 struct mapped_pci_resource *uio_res, int map_idx)
197 struct pci_map *maps;
199 maps = uio_res->maps;
200 devname = uio_res->path;
201 pagesz = sysconf(_SC_PAGESIZE);
203 /* allocate memory to keep path */
204 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
205 if (maps[map_idx].path == NULL) {
206 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
212 * open resource file, to mmap it
214 fd = open(devname, O_RDWR);
216 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
217 devname, strerror(errno));
221 /* if matching map is found, then use it */
222 offset = res_idx * pagesz;
223 mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
224 (size_t)dev->mem_resource[res_idx].len, 0);
226 if (mapaddr == MAP_FAILED)
229 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
230 maps[map_idx].size = dev->mem_resource[res_idx].len;
231 maps[map_idx].addr = mapaddr;
232 maps[map_idx].offset = offset;
233 strcpy(maps[map_idx].path, devname);
234 dev->mem_resource[res_idx].addr = mapaddr;
239 rte_free(maps[map_idx].path);
244 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
246 struct rte_pci_device *dev;
247 struct pci_bar_io bar;
250 dev = malloc(sizeof(*dev));
255 memset(dev, 0, sizeof(*dev));
256 dev->addr.domain = conf->pc_sel.pc_domain;
257 dev->addr.bus = conf->pc_sel.pc_bus;
258 dev->addr.devid = conf->pc_sel.pc_dev;
259 dev->addr.function = conf->pc_sel.pc_func;
262 dev->id.vendor_id = conf->pc_vendor;
265 dev->id.device_id = conf->pc_device;
267 /* get subsystem_vendor id */
268 dev->id.subsystem_vendor_id = conf->pc_subvendor;
270 /* get subsystem_device id */
271 dev->id.subsystem_device_id = conf->pc_subdevice;
274 dev->id.class_id = (conf->pc_class << 16) |
275 (conf->pc_subclass << 8) |
278 /* TODO: get max_vfs */
281 /* FreeBSD has no NUMA support (yet) */
282 dev->device.numa_node = 0;
286 /* FreeBSD has only one pass through driver */
287 dev->kdrv = RTE_KDRV_NIC_UIO;
289 /* parse resources */
290 switch (conf->pc_hdr & PCIM_HDRTYPE) {
291 case PCIM_HDRTYPE_NORMAL:
292 max = PCIR_MAX_BAR_0;
294 case PCIM_HDRTYPE_BRIDGE:
295 max = PCIR_MAX_BAR_1;
297 case PCIM_HDRTYPE_CARDBUS:
298 max = PCIR_MAX_BAR_2;
304 for (i = 0; i <= max; i++) {
305 bar.pbi_sel = conf->pc_sel;
306 bar.pbi_reg = PCIR_BAR(i);
307 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
310 dev->mem_resource[i].len = bar.pbi_length;
311 if (PCI_BAR_IO(bar.pbi_base)) {
312 dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
315 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
318 /* device is valid, add in list (sorted) */
319 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
320 rte_pci_add_device(dev);
323 struct rte_pci_device *dev2 = NULL;
326 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
327 ret = pci_addr_cmp(&dev->addr, &dev2->addr);
331 rte_pci_insert_device(dev2, dev);
332 } else { /* already registered */
333 dev2->kdrv = dev->kdrv;
334 dev2->max_vfs = dev->max_vfs;
336 memmove(dev2->mem_resource,
338 sizeof(dev->mem_resource));
343 rte_pci_add_device(dev);
354 * Scan the content of the PCI bus, and add the devices in the devices
355 * list. Call pci_scan_one() for each pci entry found.
361 unsigned dev_count = 0;
362 struct pci_conf matches[16];
363 struct pci_conf_io conf_io = {
367 .match_buf_len = sizeof(matches),
368 .matches = &matches[0],
371 /* for debug purposes, PCI can be disabled */
372 if (!rte_eal_has_pci())
375 fd = open("/dev/pci", O_RDONLY);
377 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
383 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
384 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
385 __func__, strerror(errno));
389 for (i = 0; i < conf_io.num_matches; i++)
390 if (pci_scan_one(fd, &matches[i]) < 0)
393 dev_count += conf_io.num_matches;
394 } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
398 RTE_LOG(DEBUG, EAL, "PCI scan found %u devices\n", dev_count);
408 * Get iommu class of PCI devices on the bus.
411 rte_pci_get_iommu_class(void)
413 /* Supports only RTE_KDRV_NIC_UIO */
418 pci_update_device(const struct rte_pci_addr *addr)
421 struct pci_conf matches[2];
422 struct pci_match_conf match = {
424 .pc_domain = addr->domain,
426 .pc_dev = addr->devid,
427 .pc_func = addr->function,
430 struct pci_conf_io conf_io = {
434 .match_buf_len = sizeof(matches),
435 .matches = &matches[0],
438 fd = open("/dev/pci", O_RDONLY);
440 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
444 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
445 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
446 __func__, strerror(errno));
450 if (conf_io.num_matches != 1)
453 if (pci_scan_one(fd, &matches[0]) < 0)
466 /* Read PCI config space. */
467 int rte_pci_read_config(const struct rte_pci_device *dev,
468 void *buf, size_t len, off_t offset)
474 .pc_domain = dev->addr.domain,
475 .pc_bus = dev->addr.bus,
476 .pc_dev = dev->addr.devid,
477 .pc_func = dev->addr.function,
482 fd = open("/dev/pci", O_RDWR);
484 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
489 size = (len >= 4) ? 4 : ((len >= 2) ? 2 : 1);
492 if (ioctl(fd, PCIOCREAD, &pi) < 0)
494 memcpy(buf, &pi.pi_data, size);
496 buf = (char *)buf + size;
510 /* Write PCI config space. */
511 int rte_pci_write_config(const struct rte_pci_device *dev,
512 const void *buf, size_t len, off_t offset)
518 .pc_domain = dev->addr.domain,
519 .pc_bus = dev->addr.bus,
520 .pc_dev = dev->addr.devid,
521 .pc_func = dev->addr.function,
524 .pi_data = *(const uint32_t *)buf,
528 if (len == 3 || len > sizeof(pi.pi_data)) {
529 RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__);
533 memcpy(&pi.pi_data, buf, len);
535 fd = open("/dev/pci", O_RDWR);
537 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
541 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
554 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
555 struct rte_pci_ioport *p)
560 #if defined(RTE_ARCH_X86)
561 case RTE_KDRV_NIC_UIO:
562 if ((uintptr_t) dev->mem_resource[bar].addr <= UINT16_MAX) {
563 p->base = (uintptr_t)dev->mem_resource[bar].addr;
581 pci_uio_ioport_read(struct rte_pci_ioport *p,
582 void *data, size_t len, off_t offset)
584 #if defined(RTE_ARCH_X86)
587 unsigned short reg = p->base + offset;
589 for (d = data; len > 0; d += size, reg += size, len -= size) {
592 *(uint32_t *)d = inl(reg);
593 } else if (len >= 2) {
595 *(uint16_t *)d = inw(reg);
605 RTE_SET_USED(offset);
610 rte_pci_ioport_read(struct rte_pci_ioport *p,
611 void *data, size_t len, off_t offset)
613 switch (p->dev->kdrv) {
614 case RTE_KDRV_NIC_UIO:
615 pci_uio_ioport_read(p, data, len, offset);
623 pci_uio_ioport_write(struct rte_pci_ioport *p,
624 const void *data, size_t len, off_t offset)
626 #if defined(RTE_ARCH_X86)
629 unsigned short reg = p->base + offset;
631 for (s = data; len > 0; s += size, reg += size, len -= size) {
634 outl(reg, *(const uint32_t *)s);
635 } else if (len >= 2) {
637 outw(reg, *(const uint16_t *)s);
647 RTE_SET_USED(offset);
652 rte_pci_ioport_write(struct rte_pci_ioport *p,
653 const void *data, size_t len, off_t offset)
655 switch (p->dev->kdrv) {
656 case RTE_KDRV_NIC_UIO:
657 pci_uio_ioport_write(p, data, len, offset);
665 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
669 switch (p->dev->kdrv) {
670 #if defined(RTE_ARCH_X86)
671 case RTE_KDRV_NIC_UIO: