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 <linux/pci_regs.h>
42 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
48 #include <rte_eal_memconfig.h>
49 #include <rte_common.h>
50 #include <rte_malloc.h>
52 #include "eal_filesystem.h"
53 #include "eal_pci_init.h"
55 void *pci_map_addr = NULL;
57 #define OFF_MAX ((uint64_t)(off_t)-1)
60 pci_uio_read_config(const struct rte_intr_handle *intr_handle,
61 void *buf, size_t len, off_t offset)
63 return pread(intr_handle->uio_cfg_fd, buf, len, offset);
67 pci_uio_write_config(const struct rte_intr_handle *intr_handle,
68 const void *buf, size_t len, off_t offset)
70 return pwrite(intr_handle->uio_cfg_fd, buf, len, offset);
74 pci_uio_set_bus_master(int dev_fd)
79 ret = pread(dev_fd, ®, sizeof(reg), PCI_COMMAND);
80 if (ret != sizeof(reg)) {
82 "Cannot read command from PCI config space!\n");
86 /* return if bus mastering is already on */
87 if (reg & PCI_COMMAND_MASTER)
90 reg |= PCI_COMMAND_MASTER;
92 ret = pwrite(dev_fd, ®, sizeof(reg), PCI_COMMAND);
93 if (ret != sizeof(reg)) {
95 "Cannot write command to PCI config space!\n");
103 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
106 char filename[PATH_MAX];
108 unsigned major, minor;
111 /* get the name of the sysfs file that contains the major and minor
112 * of the uio device and read its content */
113 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
115 f = fopen(filename, "r");
117 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
122 ret = fscanf(f, "%u:%u", &major, &minor);
124 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
131 /* create the char device "mknod /dev/uioX c major minor" */
132 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
133 dev = makedev(major, minor);
134 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
136 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
137 __func__, strerror(errno));
145 * Return the uioX char device used for a pci device. On success, return
146 * the UIO number and fill dstbuf string with the path of the device in
147 * sysfs. On error, return a negative value. In this case dstbuf is
151 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
152 unsigned int buflen, int create)
154 struct rte_pci_addr *loc = &dev->addr;
155 unsigned int uio_num;
158 char dirname[PATH_MAX];
160 /* depending on kernel version, uio can be located in uio/uioX
163 snprintf(dirname, sizeof(dirname),
164 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
165 loc->domain, loc->bus, loc->devid, loc->function);
167 dir = opendir(dirname);
169 /* retry with the parent directory */
170 snprintf(dirname, sizeof(dirname),
171 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
172 loc->domain, loc->bus, loc->devid, loc->function);
173 dir = opendir(dirname);
176 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
181 /* take the first file starting with "uio" */
182 while ((e = readdir(dir)) != NULL) {
183 /* format could be uio%d ...*/
184 int shortprefix_len = sizeof("uio") - 1;
185 /* ... or uio:uio%d */
186 int longprefix_len = sizeof("uio:uio") - 1;
189 if (strncmp(e->d_name, "uio", 3) != 0)
192 /* first try uio%d */
194 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
195 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
196 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
200 /* then try uio:uio%d */
202 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
203 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
204 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
210 /* No uio resource found */
214 /* create uio device if we've been asked to */
215 if (internal_config.create_uio_dev && create &&
216 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
217 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
223 pci_uio_free_resource(struct rte_pci_device *dev,
224 struct mapped_pci_resource *uio_res)
228 if (dev->intr_handle.uio_cfg_fd >= 0) {
229 close(dev->intr_handle.uio_cfg_fd);
230 dev->intr_handle.uio_cfg_fd = -1;
232 if (dev->intr_handle.fd) {
233 close(dev->intr_handle.fd);
234 dev->intr_handle.fd = -1;
235 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
240 pci_uio_alloc_resource(struct rte_pci_device *dev,
241 struct mapped_pci_resource **uio_res)
243 char dirname[PATH_MAX];
244 char cfgname[PATH_MAX];
245 char devname[PATH_MAX]; /* contains the /dev/uioX */
247 struct rte_pci_addr *loc;
251 /* find uio resource */
252 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 1);
254 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
255 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
258 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
260 /* save fd if in primary process */
261 dev->intr_handle.fd = open(devname, O_RDWR);
262 if (dev->intr_handle.fd < 0) {
263 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
264 devname, strerror(errno));
268 snprintf(cfgname, sizeof(cfgname),
269 "/sys/class/uio/uio%u/device/config", uio_num);
270 dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
271 if (dev->intr_handle.uio_cfg_fd < 0) {
272 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
273 cfgname, strerror(errno));
277 if (dev->kdrv == RTE_KDRV_IGB_UIO)
278 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
280 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
282 /* set bus master that is not done by uio_pci_generic */
283 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
284 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
289 /* allocate the mapping details for secondary processes*/
290 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
291 if (*uio_res == NULL) {
293 "%s(): cannot store uio mmap details\n", __func__);
297 snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
298 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
303 pci_uio_free_resource(dev, *uio_res);
308 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
309 struct mapped_pci_resource *uio_res, int map_idx)
312 char devname[PATH_MAX]; /* contains the /dev/uioX */
314 struct rte_pci_addr *loc;
315 struct pci_map *maps;
318 maps = uio_res->maps;
320 /* update devname for mmap */
321 snprintf(devname, sizeof(devname),
322 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d",
323 loc->domain, loc->bus, loc->devid,
324 loc->function, res_idx);
326 /* allocate memory to keep path */
327 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
328 if (maps[map_idx].path == NULL) {
329 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
335 * open resource file, to mmap it
337 fd = open(devname, O_RDWR);
339 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
340 devname, strerror(errno));
344 /* try mapping somewhere close to the end of hugepages */
345 if (pci_map_addr == NULL)
346 pci_map_addr = pci_find_max_end_va();
348 mapaddr = pci_map_resource(pci_map_addr, fd, 0,
349 (size_t)dev->mem_resource[res_idx].len, 0);
351 if (mapaddr == MAP_FAILED)
354 pci_map_addr = RTE_PTR_ADD(mapaddr,
355 (size_t)dev->mem_resource[res_idx].len);
357 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
358 maps[map_idx].size = dev->mem_resource[res_idx].len;
359 maps[map_idx].addr = mapaddr;
360 maps[map_idx].offset = 0;
361 strcpy(maps[map_idx].path, devname);
362 dev->mem_resource[res_idx].addr = mapaddr;
367 rte_free(maps[map_idx].path);
372 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
373 struct rte_pci_ioport *p)
375 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
376 char dirname[PATH_MAX];
377 char filename[PATH_MAX];
381 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 0);
385 /* get portio start */
386 snprintf(filename, sizeof(filename),
387 "%s/portio/port%d/start", dirname, bar);
388 if (eal_parse_sysfs_value(filename, &start) < 0) {
389 RTE_LOG(ERR, EAL, "%s(): cannot parse portio start\n",
393 /* ensure we don't get anything funny here, read/write will cast to
395 if (start > UINT16_MAX)
398 /* FIXME only for primary process ? */
399 if (dev->intr_handle.type == RTE_INTR_HANDLE_UNKNOWN) {
401 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
402 dev->intr_handle.fd = open(filename, O_RDWR);
403 if (dev->intr_handle.fd < 0) {
404 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
405 filename, strerror(errno));
408 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
411 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx\n", start);
424 pci_uio_ioport_read(struct rte_pci_ioport *p,
425 void *data, size_t len, off_t offset)
427 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
430 unsigned short reg = p->base + offset;
432 for (d = data; len > 0; d += size, reg += size, len -= size) {
435 *(uint32_t *)d = inl(reg);
436 } else if (len >= 2) {
438 *(uint16_t *)d = inw(reg);
448 RTE_SET_USED(offset);
453 pci_uio_ioport_write(struct rte_pci_ioport *p,
454 const void *data, size_t len, off_t offset)
456 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
459 unsigned short reg = p->base + offset;
461 for (s = data; len > 0; s += size, reg += size, len -= size) {
464 outl_p(*(const uint32_t *)s, reg);
465 } else if (len >= 2) {
467 outw_p(*(const uint16_t *)s, reg);
477 RTE_SET_USED(offset);
482 pci_uio_ioport_unmap(struct rte_pci_ioport *p)
485 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
486 /* FIXME close intr fd ? */