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_bus_pci.h>
51 #include <rte_eal_memconfig.h>
52 #include <rte_common.h>
53 #include <rte_malloc.h>
55 #include "eal_filesystem.h"
58 void *pci_map_addr = NULL;
60 #define OFF_MAX ((uint64_t)(off_t)-1)
63 pci_uio_read_config(const struct rte_intr_handle *intr_handle,
64 void *buf, size_t len, off_t offset)
66 return pread(intr_handle->uio_cfg_fd, buf, len, offset);
70 pci_uio_write_config(const struct rte_intr_handle *intr_handle,
71 const void *buf, size_t len, off_t offset)
73 return pwrite(intr_handle->uio_cfg_fd, buf, len, offset);
77 pci_uio_set_bus_master(int dev_fd)
82 ret = pread(dev_fd, ®, sizeof(reg), PCI_COMMAND);
83 if (ret != sizeof(reg)) {
85 "Cannot read command from PCI config space!\n");
89 /* return if bus mastering is already on */
90 if (reg & PCI_COMMAND_MASTER)
93 reg |= PCI_COMMAND_MASTER;
95 ret = pwrite(dev_fd, ®, sizeof(reg), PCI_COMMAND);
96 if (ret != sizeof(reg)) {
98 "Cannot write command to PCI config space!\n");
106 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
109 char filename[PATH_MAX];
111 unsigned major, minor;
114 /* get the name of the sysfs file that contains the major and minor
115 * of the uio device and read its content */
116 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
118 f = fopen(filename, "r");
120 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
125 ret = fscanf(f, "%u:%u", &major, &minor);
127 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
134 /* create the char device "mknod /dev/uioX c major minor" */
135 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
136 dev = makedev(major, minor);
137 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
139 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
140 __func__, strerror(errno));
148 * Return the uioX char device used for a pci device. On success, return
149 * the UIO number and fill dstbuf string with the path of the device in
150 * sysfs. On error, return a negative value. In this case dstbuf is
154 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
155 unsigned int buflen, int create)
157 struct rte_pci_addr *loc = &dev->addr;
161 char dirname[PATH_MAX];
163 /* depending on kernel version, uio can be located in uio/uioX
166 snprintf(dirname, sizeof(dirname),
167 "%s/" PCI_PRI_FMT "/uio", pci_get_sysfs_path(),
168 loc->domain, loc->bus, loc->devid, loc->function);
170 dir = opendir(dirname);
172 /* retry with the parent directory */
173 snprintf(dirname, sizeof(dirname),
174 "%s/" PCI_PRI_FMT, pci_get_sysfs_path(),
175 loc->domain, loc->bus, loc->devid, loc->function);
176 dir = opendir(dirname);
179 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
184 /* take the first file starting with "uio" */
185 while ((e = readdir(dir)) != NULL) {
186 /* format could be uio%d ...*/
187 int shortprefix_len = sizeof("uio") - 1;
188 /* ... or uio:uio%d */
189 int longprefix_len = sizeof("uio:uio") - 1;
192 if (strncmp(e->d_name, "uio", 3) != 0)
195 /* first try uio%d */
197 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
198 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
199 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
203 /* then try uio:uio%d */
205 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
206 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
207 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
213 /* No uio resource found */
217 /* create uio device if we've been asked to */
218 if (rte_eal_create_uio_dev() && create &&
219 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
220 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
226 pci_uio_free_resource(struct rte_pci_device *dev,
227 struct mapped_pci_resource *uio_res)
231 if (dev->intr_handle.uio_cfg_fd >= 0) {
232 close(dev->intr_handle.uio_cfg_fd);
233 dev->intr_handle.uio_cfg_fd = -1;
235 if (dev->intr_handle.fd >= 0) {
236 close(dev->intr_handle.fd);
237 dev->intr_handle.fd = -1;
238 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
243 pci_uio_alloc_resource(struct rte_pci_device *dev,
244 struct mapped_pci_resource **uio_res)
246 char dirname[PATH_MAX];
247 char cfgname[PATH_MAX];
248 char devname[PATH_MAX]; /* contains the /dev/uioX */
250 struct rte_pci_addr *loc;
254 /* find uio resource */
255 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 1);
257 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
258 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
261 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
263 /* save fd if in primary process */
264 dev->intr_handle.fd = open(devname, O_RDWR);
265 if (dev->intr_handle.fd < 0) {
266 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
267 devname, strerror(errno));
271 snprintf(cfgname, sizeof(cfgname),
272 "/sys/class/uio/uio%u/device/config", uio_num);
273 dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
274 if (dev->intr_handle.uio_cfg_fd < 0) {
275 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
276 cfgname, strerror(errno));
280 if (dev->kdrv == RTE_KDRV_IGB_UIO)
281 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
283 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
285 /* set bus master that is not done by uio_pci_generic */
286 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
287 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
292 /* allocate the mapping details for secondary processes*/
293 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
294 if (*uio_res == NULL) {
296 "%s(): cannot store uio mmap details\n", __func__);
300 snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
301 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
306 pci_uio_free_resource(dev, *uio_res);
311 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
312 struct mapped_pci_resource *uio_res, int map_idx)
315 char devname[PATH_MAX];
317 struct rte_pci_addr *loc;
318 struct pci_map *maps;
321 maps = uio_res->maps;
323 /* update devname for mmap */
324 snprintf(devname, sizeof(devname),
325 "%s/" PCI_PRI_FMT "/resource%d",
326 pci_get_sysfs_path(),
327 loc->domain, loc->bus, loc->devid,
328 loc->function, res_idx);
330 /* allocate memory to keep path */
331 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
332 if (maps[map_idx].path == NULL) {
333 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
339 * open resource file, to mmap it
341 fd = open(devname, O_RDWR);
343 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
344 devname, strerror(errno));
348 /* try mapping somewhere close to the end of hugepages */
349 if (pci_map_addr == NULL)
350 pci_map_addr = pci_find_max_end_va();
352 mapaddr = pci_map_resource(pci_map_addr, fd, 0,
353 (size_t)dev->mem_resource[res_idx].len, 0);
355 if (mapaddr == MAP_FAILED)
358 pci_map_addr = RTE_PTR_ADD(mapaddr,
359 (size_t)dev->mem_resource[res_idx].len);
361 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
362 maps[map_idx].size = dev->mem_resource[res_idx].len;
363 maps[map_idx].addr = mapaddr;
364 maps[map_idx].offset = 0;
365 strcpy(maps[map_idx].path, devname);
366 dev->mem_resource[res_idx].addr = mapaddr;
371 rte_free(maps[map_idx].path);
375 #if defined(RTE_ARCH_X86)
377 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
378 struct rte_pci_ioport *p)
380 char dirname[PATH_MAX];
381 char filename[PATH_MAX];
385 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 0);
389 /* get portio start */
390 snprintf(filename, sizeof(filename),
391 "%s/portio/port%d/start", dirname, bar);
392 if (eal_parse_sysfs_value(filename, &start) < 0) {
393 RTE_LOG(ERR, EAL, "%s(): cannot parse portio start\n",
397 /* ensure we don't get anything funny here, read/write will cast to
399 if (start > UINT16_MAX)
402 /* FIXME only for primary process ? */
403 if (dev->intr_handle.type == RTE_INTR_HANDLE_UNKNOWN) {
405 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
406 dev->intr_handle.fd = open(filename, O_RDWR);
407 if (dev->intr_handle.fd < 0) {
408 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
409 filename, strerror(errno));
412 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
415 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx\n", start);
423 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
424 struct rte_pci_ioport *p)
428 char filename[PATH_MAX];
429 uint64_t phys_addr, end_addr, flags;
433 /* open and read addresses of the corresponding resource in sysfs */
434 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource",
435 pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
436 dev->addr.devid, dev->addr.function);
437 f = fopen(filename, "r");
439 RTE_LOG(ERR, EAL, "Cannot open sysfs resource: %s\n",
443 for (i = 0; i < bar + 1; i++) {
444 if (fgets(buf, sizeof(buf), f) == NULL) {
445 RTE_LOG(ERR, EAL, "Cannot read sysfs resource\n");
449 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
450 &end_addr, &flags) < 0)
452 if ((flags & IORESOURCE_IO) == 0) {
453 RTE_LOG(ERR, EAL, "BAR %d is not an IO resource\n", bar);
456 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource%d",
457 pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
458 dev->addr.devid, dev->addr.function, bar);
460 /* mmap the pci resource */
461 fd = open(filename, O_RDWR);
463 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
467 addr = mmap(NULL, end_addr + 1, PROT_READ | PROT_WRITE,
470 if (addr == MAP_FAILED) {
471 RTE_LOG(ERR, EAL, "Cannot mmap IO port resource: %s\n",
476 /* strangely, the base address is mmap addr + phys_addr */
477 p->base = (uintptr_t)addr + phys_addr;
478 p->len = end_addr + 1;
479 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%"PRIx64"\n", p->base);
491 pci_uio_ioport_read(struct rte_pci_ioport *p,
492 void *data, size_t len, off_t offset)
496 uintptr_t reg = p->base + offset;
498 for (d = data; len > 0; d += size, reg += size, len -= size) {
501 #if defined(RTE_ARCH_X86)
502 *(uint32_t *)d = inl(reg);
504 *(uint32_t *)d = *(volatile uint32_t *)reg;
506 } else if (len >= 2) {
508 #if defined(RTE_ARCH_X86)
509 *(uint16_t *)d = inw(reg);
511 *(uint16_t *)d = *(volatile uint16_t *)reg;
515 #if defined(RTE_ARCH_X86)
518 *d = *(volatile uint8_t *)reg;
525 pci_uio_ioport_write(struct rte_pci_ioport *p,
526 const void *data, size_t len, off_t offset)
530 uintptr_t reg = p->base + offset;
532 for (s = data; len > 0; s += size, reg += size, len -= size) {
535 #if defined(RTE_ARCH_X86)
536 outl_p(*(const uint32_t *)s, reg);
538 *(volatile uint32_t *)reg = *(const uint32_t *)s;
540 } else if (len >= 2) {
542 #if defined(RTE_ARCH_X86)
543 outw_p(*(const uint16_t *)s, reg);
545 *(volatile uint16_t *)reg = *(const uint16_t *)s;
549 #if defined(RTE_ARCH_X86)
552 *(volatile uint8_t *)reg = *s;
559 pci_uio_ioport_unmap(struct rte_pci_ioport *p)
561 #if defined(RTE_ARCH_X86)
563 /* FIXME close intr fd ? */
566 return munmap((void *)(uintptr_t)p->base, p->len);