xref: /dpdk/drivers/bus/pci/linux/pci_uio.c (revision c752998b5e2eb5c827ffbecc5bd03ea28b14314f)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33 
34 #include <string.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <dirent.h>
38 #include <inttypes.h>
39 #include <sys/stat.h>
40 #include <sys/mman.h>
41 #include <sys/sysmacros.h>
42 #include <linux/pci_regs.h>
43 
44 #if defined(RTE_ARCH_X86)
45 #include <sys/io.h>
46 #endif
47 
48 #include <rte_log.h>
49 #include <rte_pci.h>
50 #include <rte_bus_pci.h>
51 #include <rte_eal_memconfig.h>
52 #include <rte_common.h>
53 #include <rte_malloc.h>
54 
55 #include "eal_filesystem.h"
56 #include "pci_init.h"
57 
58 void *pci_map_addr = NULL;
59 
60 #define OFF_MAX              ((uint64_t)(off_t)-1)
61 
62 int
63 pci_uio_read_config(const struct rte_intr_handle *intr_handle,
64 		    void *buf, size_t len, off_t offset)
65 {
66 	return pread(intr_handle->uio_cfg_fd, buf, len, offset);
67 }
68 
69 int
70 pci_uio_write_config(const struct rte_intr_handle *intr_handle,
71 		     const void *buf, size_t len, off_t offset)
72 {
73 	return pwrite(intr_handle->uio_cfg_fd, buf, len, offset);
74 }
75 
76 static int
77 pci_uio_set_bus_master(int dev_fd)
78 {
79 	uint16_t reg;
80 	int ret;
81 
82 	ret = pread(dev_fd, &reg, sizeof(reg), PCI_COMMAND);
83 	if (ret != sizeof(reg)) {
84 		RTE_LOG(ERR, EAL,
85 			"Cannot read command from PCI config space!\n");
86 		return -1;
87 	}
88 
89 	/* return if bus mastering is already on */
90 	if (reg & PCI_COMMAND_MASTER)
91 		return 0;
92 
93 	reg |= PCI_COMMAND_MASTER;
94 
95 	ret = pwrite(dev_fd, &reg, sizeof(reg), PCI_COMMAND);
96 	if (ret != sizeof(reg)) {
97 		RTE_LOG(ERR, EAL,
98 			"Cannot write command to PCI config space!\n");
99 		return -1;
100 	}
101 
102 	return 0;
103 }
104 
105 static int
106 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
107 {
108 	FILE *f;
109 	char filename[PATH_MAX];
110 	int ret;
111 	unsigned major, minor;
112 	dev_t dev;
113 
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);
117 
118 	f = fopen(filename, "r");
119 	if (f == NULL) {
120 		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
121 			__func__);
122 		return -1;
123 	}
124 
125 	ret = fscanf(f, "%u:%u", &major, &minor);
126 	if (ret != 2) {
127 		RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
128 			__func__);
129 		fclose(f);
130 		return -1;
131 	}
132 	fclose(f);
133 
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);
138 	if (ret != 0) {
139 		RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
140 			__func__, strerror(errno));
141 		return -1;
142 	}
143 
144 	return ret;
145 }
146 
147 /*
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
151  * invalid.
152  */
153 static int
154 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
155 			   unsigned int buflen, int create)
156 {
157 	struct rte_pci_addr *loc = &dev->addr;
158 	int uio_num = -1;
159 	struct dirent *e;
160 	DIR *dir;
161 	char dirname[PATH_MAX];
162 
163 	/* depending on kernel version, uio can be located in uio/uioX
164 	 * or uio:uioX */
165 
166 	snprintf(dirname, sizeof(dirname),
167 			"%s/" PCI_PRI_FMT "/uio", pci_get_sysfs_path(),
168 			loc->domain, loc->bus, loc->devid, loc->function);
169 
170 	dir = opendir(dirname);
171 	if (dir == NULL) {
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);
177 
178 		if (dir == NULL) {
179 			RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
180 			return -1;
181 		}
182 	}
183 
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;
190 		char *endptr;
191 
192 		if (strncmp(e->d_name, "uio", 3) != 0)
193 			continue;
194 
195 		/* first try uio%d */
196 		errno = 0;
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);
200 			break;
201 		}
202 
203 		/* then try uio:uio%d */
204 		errno = 0;
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);
208 			break;
209 		}
210 	}
211 	closedir(dir);
212 
213 	/* No uio resource found */
214 	if (e == NULL)
215 		return -1;
216 
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);
221 
222 	return uio_num;
223 }
224 
225 void
226 pci_uio_free_resource(struct rte_pci_device *dev,
227 		struct mapped_pci_resource *uio_res)
228 {
229 	rte_free(uio_res);
230 
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;
234 	}
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;
239 	}
240 }
241 
242 int
243 pci_uio_alloc_resource(struct rte_pci_device *dev,
244 		struct mapped_pci_resource **uio_res)
245 {
246 	char dirname[PATH_MAX];
247 	char cfgname[PATH_MAX];
248 	char devname[PATH_MAX]; /* contains the /dev/uioX */
249 	int uio_num;
250 	struct rte_pci_addr *loc;
251 
252 	loc = &dev->addr;
253 
254 	/* find uio resource */
255 	uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 1);
256 	if (uio_num < 0) {
257 		RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
258 				"skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
259 		return 1;
260 	}
261 	snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
262 
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));
268 		goto error;
269 	}
270 
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));
277 		goto error;
278 	}
279 
280 	if (dev->kdrv == RTE_KDRV_IGB_UIO)
281 		dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
282 	else {
283 		dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
284 
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");
288 			goto error;
289 		}
290 	}
291 
292 	/* allocate the mapping details for secondary processes*/
293 	*uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
294 	if (*uio_res == NULL) {
295 		RTE_LOG(ERR, EAL,
296 			"%s(): cannot store uio mmap details\n", __func__);
297 		goto error;
298 	}
299 
300 	snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
301 	memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
302 
303 	return 0;
304 
305 error:
306 	pci_uio_free_resource(dev, *uio_res);
307 	return -1;
308 }
309 
310 int
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)
313 {
314 	int fd;
315 	char devname[PATH_MAX];
316 	void *mapaddr;
317 	struct rte_pci_addr *loc;
318 	struct pci_map *maps;
319 
320 	loc = &dev->addr;
321 	maps = uio_res->maps;
322 
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);
329 
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",
334 				strerror(errno));
335 		return -1;
336 	}
337 
338 	/*
339 	 * open resource file, to mmap it
340 	 */
341 	fd = open(devname, O_RDWR);
342 	if (fd < 0) {
343 		RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
344 				devname, strerror(errno));
345 		goto error;
346 	}
347 
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();
351 
352 	mapaddr = pci_map_resource(pci_map_addr, fd, 0,
353 			(size_t)dev->mem_resource[res_idx].len, 0);
354 	close(fd);
355 	if (mapaddr == MAP_FAILED)
356 		goto error;
357 
358 	pci_map_addr = RTE_PTR_ADD(mapaddr,
359 			(size_t)dev->mem_resource[res_idx].len);
360 
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;
367 
368 	return 0;
369 
370 error:
371 	rte_free(maps[map_idx].path);
372 	return -1;
373 }
374 
375 #if defined(RTE_ARCH_X86)
376 int
377 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
378 		   struct rte_pci_ioport *p)
379 {
380 	char dirname[PATH_MAX];
381 	char filename[PATH_MAX];
382 	int uio_num;
383 	unsigned long start;
384 
385 	uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname), 0);
386 	if (uio_num < 0)
387 		return -1;
388 
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",
394 			__func__);
395 		return -1;
396 	}
397 	/* ensure we don't get anything funny here, read/write will cast to
398 	 * uin16_t */
399 	if (start > UINT16_MAX)
400 		return -1;
401 
402 	/* FIXME only for primary process ? */
403 	if (dev->intr_handle.type == RTE_INTR_HANDLE_UNKNOWN) {
404 
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));
410 			return -1;
411 		}
412 		dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
413 	}
414 
415 	RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx\n", start);
416 
417 	p->base = start;
418 	p->len = 0;
419 	return 0;
420 }
421 #else
422 int
423 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
424 		   struct rte_pci_ioport *p)
425 {
426 	FILE *f;
427 	char buf[BUFSIZ];
428 	char filename[PATH_MAX];
429 	uint64_t phys_addr, end_addr, flags;
430 	int fd, i;
431 	void *addr;
432 
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");
438 	if (f == NULL) {
439 		RTE_LOG(ERR, EAL, "Cannot open sysfs resource: %s\n",
440 			strerror(errno));
441 		return -1;
442 	}
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");
446 			goto error;
447 		}
448 	}
449 	if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
450 			&end_addr, &flags) < 0)
451 		goto error;
452 	if ((flags & IORESOURCE_IO) == 0) {
453 		RTE_LOG(ERR, EAL, "BAR %d is not an IO resource\n", bar);
454 		goto error;
455 	}
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);
459 
460 	/* mmap the pci resource */
461 	fd = open(filename, O_RDWR);
462 	if (fd < 0) {
463 		RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
464 			strerror(errno));
465 		goto error;
466 	}
467 	addr = mmap(NULL, end_addr + 1, PROT_READ | PROT_WRITE,
468 		MAP_SHARED, fd, 0);
469 	close(fd);
470 	if (addr == MAP_FAILED) {
471 		RTE_LOG(ERR, EAL, "Cannot mmap IO port resource: %s\n",
472 			strerror(errno));
473 		goto error;
474 	}
475 
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);
480 	fclose(f);
481 
482 	return 0;
483 
484 error:
485 	fclose(f);
486 	return -1;
487 }
488 #endif
489 
490 void
491 pci_uio_ioport_read(struct rte_pci_ioport *p,
492 		    void *data, size_t len, off_t offset)
493 {
494 	uint8_t *d;
495 	int size;
496 	uintptr_t reg = p->base + offset;
497 
498 	for (d = data; len > 0; d += size, reg += size, len -= size) {
499 		if (len >= 4) {
500 			size = 4;
501 #if defined(RTE_ARCH_X86)
502 			*(uint32_t *)d = inl(reg);
503 #else
504 			*(uint32_t *)d = *(volatile uint32_t *)reg;
505 #endif
506 		} else if (len >= 2) {
507 			size = 2;
508 #if defined(RTE_ARCH_X86)
509 			*(uint16_t *)d = inw(reg);
510 #else
511 			*(uint16_t *)d = *(volatile uint16_t *)reg;
512 #endif
513 		} else {
514 			size = 1;
515 #if defined(RTE_ARCH_X86)
516 			*d = inb(reg);
517 #else
518 			*d = *(volatile uint8_t *)reg;
519 #endif
520 		}
521 	}
522 }
523 
524 void
525 pci_uio_ioport_write(struct rte_pci_ioport *p,
526 		     const void *data, size_t len, off_t offset)
527 {
528 	const uint8_t *s;
529 	int size;
530 	uintptr_t reg = p->base + offset;
531 
532 	for (s = data; len > 0; s += size, reg += size, len -= size) {
533 		if (len >= 4) {
534 			size = 4;
535 #if defined(RTE_ARCH_X86)
536 			outl_p(*(const uint32_t *)s, reg);
537 #else
538 			*(volatile uint32_t *)reg = *(const uint32_t *)s;
539 #endif
540 		} else if (len >= 2) {
541 			size = 2;
542 #if defined(RTE_ARCH_X86)
543 			outw_p(*(const uint16_t *)s, reg);
544 #else
545 			*(volatile uint16_t *)reg = *(const uint16_t *)s;
546 #endif
547 		} else {
548 			size = 1;
549 #if defined(RTE_ARCH_X86)
550 			outb_p(*s, reg);
551 #else
552 			*(volatile uint8_t *)reg = *s;
553 #endif
554 		}
555 	}
556 }
557 
558 int
559 pci_uio_ioport_unmap(struct rte_pci_ioport *p)
560 {
561 #if defined(RTE_ARCH_X86)
562 	RTE_SET_USED(p);
563 	/* FIXME close intr fd ? */
564 	return 0;
565 #else
566 	return munmap((void *)(uintptr_t)p->base, p->len);
567 #endif
568 }
569