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/sysmacros.h>
42 #include <linux/pci_regs.h>
44 #if defined(RTE_ARCH_X86)
50 #include <rte_eal_memconfig.h>
51 #include <rte_common.h>
52 #include <rte_malloc.h>
54 #include "eal_filesystem.h"
55 #include "eal_pci_init.h"
57 void *pci_map_addr = NULL;
59 #define OFF_MAX ((uint64_t)(off_t)-1)
62 pci_uio_read_config(const struct rte_intr_handle *intr_handle,
63 void *buf, size_t len, off_t offset)
65 return pread(intr_handle->uio_cfg_fd, buf, len, offset);
69 pci_uio_write_config(const struct rte_intr_handle *intr_handle,
70 const void *buf, size_t len, off_t offset)
72 return pwrite(intr_handle->uio_cfg_fd, buf, len, offset);
76 pci_uio_set_bus_master(int dev_fd)
81 ret = pread(dev_fd, ®, sizeof(reg), PCI_COMMAND);
82 if (ret != sizeof(reg)) {
84 "Cannot read command from PCI config space!\n");
88 /* return if bus mastering is already on */
89 if (reg & PCI_COMMAND_MASTER)
92 reg |= PCI_COMMAND_MASTER;
94 ret = pwrite(dev_fd, ®, sizeof(reg), PCI_COMMAND);
95 if (ret != sizeof(reg)) {
97 "Cannot write command to PCI config space!\n");
105 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
108 char filename[PATH_MAX];
110 unsigned major, minor;
113 /* get the name of the sysfs file that contains the major and minor
114 * of the uio device and read its content */
115 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
117 f = fopen(filename, "r");
119 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
124 ret = fscanf(f, "%u:%u", &major, &minor);
126 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
133 /* create the char device "mknod /dev/uioX c major minor" */
134 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
135 dev = makedev(major, minor);
136 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
138 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
139 __func__, strerror(errno));
147 * Return the uioX char device used for a pci device. On success, return
148 * the UIO number and fill dstbuf string with the path of the device in
149 * sysfs. On error, return a negative value. In this case dstbuf is
153 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
154 unsigned int buflen, int create)
156 struct rte_pci_addr *loc = &dev->addr;
157 unsigned int uio_num;
160 char dirname[PATH_MAX];
162 /* depending on kernel version, uio can be located in uio/uioX
165 snprintf(dirname, sizeof(dirname),
166 "%s/" PCI_PRI_FMT "/uio", pci_get_sysfs_path(),
167 loc->domain, loc->bus, loc->devid, loc->function);
169 dir = opendir(dirname);
171 /* retry with the parent directory */
172 snprintf(dirname, sizeof(dirname),
173 "%s/" PCI_PRI_FMT, pci_get_sysfs_path(),
174 loc->domain, loc->bus, loc->devid, loc->function);
175 dir = opendir(dirname);
178 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
183 /* take the first file starting with "uio" */
184 while ((e = readdir(dir)) != NULL) {
185 /* format could be uio%d ...*/
186 int shortprefix_len = sizeof("uio") - 1;
187 /* ... or uio:uio%d */
188 int longprefix_len = sizeof("uio:uio") - 1;
191 if (strncmp(e->d_name, "uio", 3) != 0)
194 /* first try uio%d */
196 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
197 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
198 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
202 /* then try uio:uio%d */
204 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
205 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
206 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
212 /* No uio resource found */
216 /* create uio device if we've been asked to */
217 if (internal_config.create_uio_dev && create &&
218 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
219 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
225 pci_uio_free_resource(struct rte_pci_device *dev,
226 struct mapped_pci_resource *uio_res)
230 if (dev->intr_handle.uio_cfg_fd >= 0) {
231 close(dev->intr_handle.uio_cfg_fd);
232 dev->intr_handle.uio_cfg_fd = -1;
234 if (dev->intr_handle.fd >= 0) {
235 close(dev->intr_handle.fd);
236 dev->intr_handle.fd = -1;
237 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
242 pci_uio_alloc_resource(struct rte_pci_device *dev,
243 struct mapped_pci_resource **uio_res)
245 char dirname[PATH_MAX];
246 char cfgname[PATH_MAX];
247 char devname[PATH_MAX]; /* contains the /dev/uioX */
249 struct rte_pci_addr *loc;
253 /* find uio resource */
254 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 1);
256 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
257 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
260 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
262 /* save fd if in primary process */
263 dev->intr_handle.fd = open(devname, O_RDWR);
264 if (dev->intr_handle.fd < 0) {
265 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
266 devname, strerror(errno));
270 snprintf(cfgname, sizeof(cfgname),
271 "/sys/class/uio/uio%u/device/config", uio_num);
272 dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
273 if (dev->intr_handle.uio_cfg_fd < 0) {
274 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
275 cfgname, strerror(errno));
279 if (dev->kdrv == RTE_KDRV_IGB_UIO)
280 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
282 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
284 /* set bus master that is not done by uio_pci_generic */
285 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
286 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
291 /* allocate the mapping details for secondary processes*/
292 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
293 if (*uio_res == NULL) {
295 "%s(): cannot store uio mmap details\n", __func__);
299 snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
300 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
305 pci_uio_free_resource(dev, *uio_res);
310 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
311 struct mapped_pci_resource *uio_res, int map_idx)
314 char devname[PATH_MAX];
316 struct rte_pci_addr *loc;
317 struct pci_map *maps;
320 maps = uio_res->maps;
322 /* update devname for mmap */
323 snprintf(devname, sizeof(devname),
324 "%s/" PCI_PRI_FMT "/resource%d",
325 pci_get_sysfs_path(),
326 loc->domain, loc->bus, loc->devid,
327 loc->function, res_idx);
329 /* allocate memory to keep path */
330 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
331 if (maps[map_idx].path == NULL) {
332 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
338 * open resource file, to mmap it
340 fd = open(devname, O_RDWR);
342 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
343 devname, strerror(errno));
347 /* try mapping somewhere close to the end of hugepages */
348 if (pci_map_addr == NULL)
349 pci_map_addr = pci_find_max_end_va();
351 mapaddr = pci_map_resource(pci_map_addr, fd, 0,
352 (size_t)dev->mem_resource[res_idx].len, 0);
354 if (mapaddr == MAP_FAILED)
357 pci_map_addr = RTE_PTR_ADD(mapaddr,
358 (size_t)dev->mem_resource[res_idx].len);
360 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
361 maps[map_idx].size = dev->mem_resource[res_idx].len;
362 maps[map_idx].addr = mapaddr;
363 maps[map_idx].offset = 0;
364 strcpy(maps[map_idx].path, devname);
365 dev->mem_resource[res_idx].addr = mapaddr;
370 rte_free(maps[map_idx].path);
374 #if defined(RTE_ARCH_X86)
376 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
377 struct rte_pci_ioport *p)
379 char dirname[PATH_MAX];
380 char filename[PATH_MAX];
384 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 0);
388 /* get portio start */
389 snprintf(filename, sizeof(filename),
390 "%s/portio/port%d/start", dirname, bar);
391 if (eal_parse_sysfs_value(filename, &start) < 0) {
392 RTE_LOG(ERR, EAL, "%s(): cannot parse portio start\n",
396 /* ensure we don't get anything funny here, read/write will cast to
398 if (start > UINT16_MAX)
401 /* FIXME only for primary process ? */
402 if (dev->intr_handle.type == RTE_INTR_HANDLE_UNKNOWN) {
404 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
405 dev->intr_handle.fd = open(filename, O_RDWR);
406 if (dev->intr_handle.fd < 0) {
407 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
408 filename, strerror(errno));
411 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
414 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx\n", start);
422 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
423 struct rte_pci_ioport *p)
427 char filename[PATH_MAX];
428 uint64_t phys_addr, end_addr, flags;
432 /* open and read addresses of the corresponding resource in sysfs */
433 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource",
434 pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
435 dev->addr.devid, dev->addr.function);
436 f = fopen(filename, "r");
438 RTE_LOG(ERR, EAL, "Cannot open sysfs resource: %s\n",
442 for (i = 0; i < bar + 1; i++) {
443 if (fgets(buf, sizeof(buf), f) == NULL) {
444 RTE_LOG(ERR, EAL, "Cannot read sysfs resource\n");
448 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
449 &end_addr, &flags) < 0)
451 if ((flags & IORESOURCE_IO) == 0) {
452 RTE_LOG(ERR, EAL, "BAR %d is not an IO resource\n", bar);
455 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource%d",
456 pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
457 dev->addr.devid, dev->addr.function, bar);
459 /* mmap the pci resource */
460 fd = open(filename, O_RDWR);
462 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
466 addr = mmap(NULL, end_addr + 1, PROT_READ | PROT_WRITE,
469 if (addr == MAP_FAILED) {
470 RTE_LOG(ERR, EAL, "Cannot mmap IO port resource: %s\n",
475 /* strangely, the base address is mmap addr + phys_addr */
476 p->base = (uintptr_t)addr + phys_addr;
477 p->len = end_addr + 1;
478 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%"PRIx64"\n", p->base);
490 pci_uio_ioport_read(struct rte_pci_ioport *p,
491 void *data, size_t len, off_t offset)
495 uintptr_t reg = p->base + offset;
497 for (d = data; len > 0; d += size, reg += size, len -= size) {
500 #if defined(RTE_ARCH_X86)
501 *(uint32_t *)d = inl(reg);
503 *(uint32_t *)d = *(volatile uint32_t *)reg;
505 } else if (len >= 2) {
507 #if defined(RTE_ARCH_X86)
508 *(uint16_t *)d = inw(reg);
510 *(uint16_t *)d = *(volatile uint16_t *)reg;
514 #if defined(RTE_ARCH_X86)
517 *d = *(volatile uint8_t *)reg;
524 pci_uio_ioport_write(struct rte_pci_ioport *p,
525 const void *data, size_t len, off_t offset)
529 uintptr_t reg = p->base + offset;
531 for (s = data; len > 0; s += size, reg += size, len -= size) {
534 #if defined(RTE_ARCH_X86)
535 outl_p(*(const uint32_t *)s, reg);
537 *(volatile uint32_t *)reg = *(const uint32_t *)s;
539 } else if (len >= 2) {
541 #if defined(RTE_ARCH_X86)
542 outw_p(*(const uint16_t *)s, reg);
544 *(volatile uint16_t *)reg = *(const uint16_t *)s;
548 #if defined(RTE_ARCH_X86)
551 *(volatile uint8_t *)reg = *s;
558 pci_uio_ioport_unmap(struct rte_pci_ioport *p)
560 #if defined(RTE_ARCH_X86)
562 /* FIXME close intr fd ? */
565 return munmap((void *)(uintptr_t)p->base, p->len);