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_common.h>
61 #include <rte_launch.h>
62 #include <rte_memory.h>
63 #include <rte_memzone.h>
65 #include <rte_eal_memconfig.h>
66 #include <rte_per_lcore.h>
67 #include <rte_lcore.h>
68 #include <rte_malloc.h>
69 #include <rte_string_fns.h>
70 #include <rte_debug.h>
71 #include <rte_devargs.h>
73 #include "eal_filesystem.h"
74 #include "eal_private.h"
78 * PCI probing under linux
80 * This code is used to simulate a PCI probe by parsing information in
81 * sysfs. Moreover, when a registered driver matches a device, the
82 * kernel driver currently using it is unloaded and replaced by
83 * igb_uio module, which is a very minimal userland driver for Intel
84 * network card, only providing access to PCI BAR to applications, and
85 * enabling bus master.
88 extern struct rte_pci_bus rte_pci_bus;
92 rte_pci_map_device(struct rte_pci_device *dev)
96 /* try mapping the NIC resources */
98 case RTE_KDRV_NIC_UIO:
99 /* map resources for devices that use uio */
100 ret = pci_uio_map_resource(dev);
104 " Not managed by a supported kernel driver, skipped\n");
112 /* Unmap pci device */
114 rte_pci_unmap_device(struct rte_pci_device *dev)
116 /* try unmapping the NIC resources */
118 case RTE_KDRV_NIC_UIO:
119 /* unmap resources for devices that use uio */
120 pci_uio_unmap_resource(dev);
124 " Not managed by a supported kernel driver, skipped\n");
130 pci_uio_free_resource(struct rte_pci_device *dev,
131 struct mapped_pci_resource *uio_res)
135 if (dev->intr_handle.fd) {
136 close(dev->intr_handle.fd);
137 dev->intr_handle.fd = -1;
138 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
143 pci_uio_alloc_resource(struct rte_pci_device *dev,
144 struct mapped_pci_resource **uio_res)
146 char devname[PATH_MAX]; /* contains the /dev/uioX */
147 struct rte_pci_addr *loc;
151 snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
152 dev->addr.bus, dev->addr.devid, dev->addr.function);
154 if (access(devname, O_RDWR) < 0) {
155 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
156 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
160 /* save fd if in primary process */
161 dev->intr_handle.fd = open(devname, O_RDWR);
162 if (dev->intr_handle.fd < 0) {
163 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
164 devname, strerror(errno));
167 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
169 /* allocate the mapping details for secondary processes*/
170 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
171 if (*uio_res == NULL) {
173 "%s(): cannot store uio mmap details\n", __func__);
177 snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
178 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
183 pci_uio_free_resource(dev, *uio_res);
188 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
189 struct mapped_pci_resource *uio_res, int map_idx)
196 struct pci_map *maps;
198 maps = uio_res->maps;
199 devname = uio_res->path;
200 pagesz = sysconf(_SC_PAGESIZE);
202 /* allocate memory to keep path */
203 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
204 if (maps[map_idx].path == NULL) {
205 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
211 * open resource file, to mmap it
213 fd = open(devname, O_RDWR);
215 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
216 devname, strerror(errno));
220 /* if matching map is found, then use it */
221 offset = res_idx * pagesz;
222 mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
223 (size_t)dev->mem_resource[res_idx].len, 0);
225 if (mapaddr == MAP_FAILED)
228 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
229 maps[map_idx].size = dev->mem_resource[res_idx].len;
230 maps[map_idx].addr = mapaddr;
231 maps[map_idx].offset = offset;
232 strcpy(maps[map_idx].path, devname);
233 dev->mem_resource[res_idx].addr = mapaddr;
238 rte_free(maps[map_idx].path);
243 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
245 struct rte_pci_device *dev;
246 struct pci_bar_io bar;
249 dev = malloc(sizeof(*dev));
254 memset(dev, 0, sizeof(*dev));
255 dev->addr.domain = conf->pc_sel.pc_domain;
256 dev->addr.bus = conf->pc_sel.pc_bus;
257 dev->addr.devid = conf->pc_sel.pc_dev;
258 dev->addr.function = conf->pc_sel.pc_func;
261 dev->id.vendor_id = conf->pc_vendor;
264 dev->id.device_id = conf->pc_device;
266 /* get subsystem_vendor id */
267 dev->id.subsystem_vendor_id = conf->pc_subvendor;
269 /* get subsystem_device id */
270 dev->id.subsystem_device_id = conf->pc_subdevice;
273 dev->id.class_id = (conf->pc_class << 16) |
274 (conf->pc_subclass << 8) |
277 /* TODO: get max_vfs */
280 /* FreeBSD has no NUMA support (yet) */
281 dev->device.numa_node = 0;
285 /* FreeBSD has only one pass through driver */
286 dev->kdrv = RTE_KDRV_NIC_UIO;
288 /* parse resources */
289 switch (conf->pc_hdr & PCIM_HDRTYPE) {
290 case PCIM_HDRTYPE_NORMAL:
291 max = PCIR_MAX_BAR_0;
293 case PCIM_HDRTYPE_BRIDGE:
294 max = PCIR_MAX_BAR_1;
296 case PCIM_HDRTYPE_CARDBUS:
297 max = PCIR_MAX_BAR_2;
303 for (i = 0; i <= max; i++) {
304 bar.pbi_sel = conf->pc_sel;
305 bar.pbi_reg = PCIR_BAR(i);
306 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
309 dev->mem_resource[i].len = bar.pbi_length;
310 if (PCI_BAR_IO(bar.pbi_base)) {
311 dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
314 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
317 /* device is valid, add in list (sorted) */
318 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
319 rte_pci_add_device(dev);
322 struct rte_pci_device *dev2 = NULL;
325 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
326 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
330 rte_pci_insert_device(dev2, dev);
331 } else { /* already registered */
332 dev2->kdrv = dev->kdrv;
333 dev2->max_vfs = dev->max_vfs;
335 memmove(dev2->mem_resource,
337 sizeof(dev->mem_resource));
342 rte_pci_add_device(dev);
353 * Scan the content of the PCI bus, and add the devices in the devices
354 * list. Call pci_scan_one() for each pci entry found.
360 unsigned dev_count = 0;
361 struct pci_conf matches[16];
362 struct pci_conf_io conf_io = {
366 .match_buf_len = sizeof(matches),
367 .matches = &matches[0],
370 /* for debug purposes, PCI can be disabled */
371 if (internal_config.no_pci)
374 fd = open("/dev/pci", O_RDONLY);
376 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
382 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
383 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
384 __func__, strerror(errno));
388 for (i = 0; i < conf_io.num_matches; i++)
389 if (pci_scan_one(fd, &matches[i]) < 0)
392 dev_count += conf_io.num_matches;
393 } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
397 RTE_LOG(ERR, EAL, "PCI scan found %u devices\n", dev_count);
407 pci_update_device(const struct rte_pci_addr *addr)
410 struct pci_conf matches[2];
411 struct pci_match_conf match = {
413 .pc_domain = addr->domain,
415 .pc_dev = addr->devid,
416 .pc_func = addr->function,
419 struct pci_conf_io conf_io = {
423 .match_buf_len = sizeof(matches),
424 .matches = &matches[0],
427 fd = open("/dev/pci", O_RDONLY);
429 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
433 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
434 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
435 __func__, strerror(errno));
439 if (conf_io.num_matches != 1)
442 if (pci_scan_one(fd, &matches[0]) < 0)
455 /* Read PCI config space. */
456 int rte_pci_read_config(const struct rte_pci_device *dev,
457 void *buf, size_t len, off_t offset)
463 .pc_domain = dev->addr.domain,
464 .pc_bus = dev->addr.bus,
465 .pc_dev = dev->addr.devid,
466 .pc_func = dev->addr.function,
471 fd = open("/dev/pci", O_RDWR);
473 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
478 size = (len >= 4) ? 4 : ((len >= 2) ? 2 : 1);
481 if (ioctl(fd, PCIOCREAD, &pi) < 0)
483 memcpy(buf, &pi.pi_data, size);
485 buf = (char *)buf + size;
499 /* Write PCI config space. */
500 int rte_pci_write_config(const struct rte_pci_device *dev,
501 const void *buf, size_t len, off_t offset)
507 .pc_domain = dev->addr.domain,
508 .pc_bus = dev->addr.bus,
509 .pc_dev = dev->addr.devid,
510 .pc_func = dev->addr.function,
513 .pi_data = *(const uint32_t *)buf,
517 if (len == 3 || len > sizeof(pi.pi_data)) {
518 RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__);
522 memcpy(&pi.pi_data, buf, len);
524 fd = open("/dev/pci", O_RDWR);
526 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
530 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
543 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
544 struct rte_pci_ioport *p)
549 #if defined(RTE_ARCH_X86)
550 case RTE_KDRV_NIC_UIO:
551 if ((uintptr_t) dev->mem_resource[bar].addr <= UINT16_MAX) {
552 p->base = (uintptr_t)dev->mem_resource[bar].addr;
570 pci_uio_ioport_read(struct rte_pci_ioport *p,
571 void *data, size_t len, off_t offset)
573 #if defined(RTE_ARCH_X86)
576 unsigned short reg = p->base + offset;
578 for (d = data; len > 0; d += size, reg += size, len -= size) {
581 *(uint32_t *)d = inl(reg);
582 } else if (len >= 2) {
584 *(uint16_t *)d = inw(reg);
594 RTE_SET_USED(offset);
599 rte_pci_ioport_read(struct rte_pci_ioport *p,
600 void *data, size_t len, off_t offset)
602 switch (p->dev->kdrv) {
603 case RTE_KDRV_NIC_UIO:
604 pci_uio_ioport_read(p, data, len, offset);
612 pci_uio_ioport_write(struct rte_pci_ioport *p,
613 const void *data, size_t len, off_t offset)
615 #if defined(RTE_ARCH_X86)
618 unsigned short reg = p->base + offset;
620 for (s = data; len > 0; s += size, reg += size, len -= size) {
623 outl(reg, *(const uint32_t *)s);
624 } else if (len >= 2) {
626 outw(reg, *(const uint16_t *)s);
636 RTE_SET_USED(offset);
641 rte_pci_ioport_write(struct rte_pci_ioport *p,
642 const void *data, size_t len, off_t offset)
644 switch (p->dev->kdrv) {
645 case RTE_KDRV_NIC_UIO:
646 pci_uio_ioport_write(p, data, len, offset);
654 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
658 switch (p->dev->kdrv) {
659 #if defined(RTE_ARCH_X86)
660 case RTE_KDRV_NIC_UIO: