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.
36 #include <linux/pci_regs.h>
37 #include <sys/eventfd.h>
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
45 #include <rte_eal_memconfig.h>
46 #include <rte_malloc.h>
49 #include "eal_filesystem.h"
50 #include "eal_pci_init.h"
52 #include "eal_private.h"
56 * PCI probing under linux (VFIO version)
58 * This code tries to determine if the PCI device is bound to VFIO driver,
59 * and initialize it (map BARs, set up interrupts) if that's the case.
61 * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
66 #define PAGE_SIZE (sysconf(_SC_PAGESIZE))
67 #define PAGE_MASK (~(PAGE_SIZE - 1))
69 static struct rte_tailq_elem rte_vfio_tailq = {
70 .name = "VFIO_RESOURCE_LIST",
72 EAL_REGISTER_TAILQ(rte_vfio_tailq)
75 pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
76 void *buf, size_t len, off_t offs)
78 return pread64(intr_handle->vfio_dev_fd, buf, len,
79 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
83 pci_vfio_write_config(const struct rte_intr_handle *intr_handle,
84 const void *buf, size_t len, off_t offs)
86 return pwrite64(intr_handle->vfio_dev_fd, buf, len,
87 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
90 /* get PCI BAR number where MSI-X interrupts are */
92 pci_vfio_get_msix_bar(int fd, struct pci_msix_table *msix_table)
97 uint8_t cap_id, cap_offset;
99 /* read PCI capability pointer from config space */
100 ret = pread64(fd, ®, sizeof(reg),
101 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
102 PCI_CAPABILITY_LIST);
103 if (ret != sizeof(reg)) {
104 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
109 /* we need first byte */
110 cap_offset = reg & 0xFF;
114 /* read PCI capability ID */
115 ret = pread64(fd, ®, sizeof(reg),
116 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
118 if (ret != sizeof(reg)) {
119 RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
124 /* we need first byte */
127 /* if we haven't reached MSI-X, check next capability */
128 if (cap_id != PCI_CAP_ID_MSIX) {
129 ret = pread64(fd, ®, sizeof(reg),
130 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
132 if (ret != sizeof(reg)) {
133 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
138 /* we need second byte */
139 cap_offset = (reg & 0xFF00) >> 8;
143 /* else, read table offset */
145 /* table offset resides in the next 4 bytes */
146 ret = pread64(fd, ®, sizeof(reg),
147 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
149 if (ret != sizeof(reg)) {
150 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
155 ret = pread64(fd, &flags, sizeof(flags),
156 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
158 if (ret != sizeof(flags)) {
159 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
164 msix_table->bar_index = reg & RTE_PCI_MSIX_TABLE_BIR;
165 msix_table->offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
167 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
175 /* set PCI bus mastering */
177 pci_vfio_set_bus_master(int dev_fd, bool op)
182 ret = pread64(dev_fd, ®, sizeof(reg),
183 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
185 if (ret != sizeof(reg)) {
186 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
191 /* set the master bit */
192 reg |= PCI_COMMAND_MASTER;
194 reg &= ~(PCI_COMMAND_MASTER);
196 ret = pwrite64(dev_fd, ®, sizeof(reg),
197 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
200 if (ret != sizeof(reg)) {
201 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
208 /* set up interrupt support (but not enable interrupts) */
210 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
212 int i, ret, intr_idx;
214 /* default to invalid index */
215 intr_idx = VFIO_PCI_NUM_IRQS;
217 /* get interrupt type from internal config (MSI-X by default, can be
218 * overridden from the command line
220 switch (internal_config.vfio_intr_mode) {
221 case RTE_INTR_MODE_MSIX:
222 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
224 case RTE_INTR_MODE_MSI:
225 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
227 case RTE_INTR_MODE_LEGACY:
228 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
230 /* don't do anything if we want to automatically determine interrupt type */
231 case RTE_INTR_MODE_NONE:
234 RTE_LOG(ERR, EAL, " unknown default interrupt type!\n");
238 /* start from MSI-X interrupt type */
239 for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
240 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
243 /* skip interrupt modes we don't want */
244 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE &&
250 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
252 RTE_LOG(ERR, EAL, " cannot get IRQ info, "
253 "error %i (%s)\n", errno, strerror(errno));
257 /* if this vector cannot be used with eventfd, fail if we explicitly
258 * specified interrupt type, otherwise continue */
259 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
260 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE) {
262 " interrupt vector does not support eventfd!\n");
268 /* set up an eventfd for interrupts */
269 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
271 RTE_LOG(ERR, EAL, " cannot set up eventfd, "
272 "error %i (%s)\n", errno, strerror(errno));
276 dev->intr_handle.fd = fd;
277 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
280 case VFIO_PCI_MSIX_IRQ_INDEX:
281 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSIX;
282 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
284 case VFIO_PCI_MSI_IRQ_INDEX:
285 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSI;
286 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
288 case VFIO_PCI_INTX_IRQ_INDEX:
289 internal_config.vfio_intr_mode = RTE_INTR_MODE_LEGACY;
290 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
293 RTE_LOG(ERR, EAL, " unknown interrupt type!\n");
300 /* if we're here, we haven't found a suitable interrupt vector */
305 pci_vfio_is_ioport_bar(int vfio_dev_fd, int bar_index)
310 ret = pread64(vfio_dev_fd, &ioport_bar, sizeof(ioport_bar),
311 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX)
312 + PCI_BASE_ADDRESS_0 + bar_index*4);
313 if (ret != sizeof(ioport_bar)) {
314 RTE_LOG(ERR, EAL, "Cannot read command (%x) from config space!\n",
315 PCI_BASE_ADDRESS_0 + bar_index*4);
319 return (ioport_bar & PCI_BASE_ADDRESS_SPACE_IO) != 0;
323 pci_vfio_setup_device(struct rte_pci_device *dev, int vfio_dev_fd)
325 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
326 RTE_LOG(ERR, EAL, "Error setting up interrupts!\n");
330 /* set bus mastering for the device */
331 if (pci_vfio_set_bus_master(vfio_dev_fd, true)) {
332 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
336 /* Reset the device */
337 ioctl(vfio_dev_fd, VFIO_DEVICE_RESET);
343 pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res,
344 int bar_index, int additional_flags)
347 unsigned long offset, size;
350 struct pci_msix_table *msix_table = &vfio_res->msix_table;
351 struct pci_map *bar = &vfio_res->maps[bar_index];
357 if (msix_table->bar_index == bar_index) {
359 * VFIO will not let us map the MSI-X table,
360 * but we can map around it.
362 uint32_t table_start = msix_table->offset;
363 uint32_t table_end = table_start + msix_table->size;
364 table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
365 table_start &= PAGE_MASK;
367 if (table_start == 0 && table_end >= bar->size) {
368 /* Cannot map this BAR */
369 RTE_LOG(DEBUG, EAL, "Skipping BAR%d\n", bar_index);
375 memreg[0].offset = bar->offset;
376 memreg[0].size = table_start;
377 memreg[1].offset = bar->offset + table_end;
378 memreg[1].size = bar->size - table_end;
381 "Trying to map BAR%d that contains the MSI-X "
382 "table. Trying offsets: "
383 "0x%04lx:0x%04lx, 0x%04lx:0x%04lx\n", bar_index,
384 memreg[0].offset, memreg[0].size,
385 memreg[1].offset, memreg[1].size);
387 memreg[0].offset = bar->offset;
388 memreg[0].size = bar->size;
391 /* reserve the address using an inaccessible mapping */
392 bar_addr = mmap(bar->addr, bar->size, 0, MAP_PRIVATE |
393 MAP_ANONYMOUS | additional_flags, -1, 0);
394 if (bar_addr != MAP_FAILED) {
395 void *map_addr = NULL;
396 if (memreg[0].size) {
397 /* actual map of first part */
398 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
404 /* if there's a second part, try to map it */
405 if (map_addr != MAP_FAILED
406 && memreg[1].offset && memreg[1].size) {
407 void *second_addr = RTE_PTR_ADD(bar_addr,
409 (uintptr_t)bar->offset);
410 map_addr = pci_map_resource(second_addr,
417 if (map_addr == MAP_FAILED || !map_addr) {
418 munmap(bar_addr, bar->size);
419 bar_addr = MAP_FAILED;
420 RTE_LOG(ERR, EAL, "Failed to map pci BAR%d\n",
426 "Failed to create inaccessible mapping for BAR%d\n",
431 bar->addr = bar_addr;
436 pci_vfio_map_resource_primary(struct rte_pci_device *dev)
438 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
439 char pci_addr[PATH_MAX] = {0};
441 struct rte_pci_addr *loc = &dev->addr;
443 struct mapped_pci_resource *vfio_res = NULL;
444 struct mapped_pci_res_list *vfio_res_list =
445 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
447 struct pci_map *maps;
449 dev->intr_handle.fd = -1;
450 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
452 /* store PCI address string */
453 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
454 loc->domain, loc->bus, loc->devid, loc->function);
456 ret = vfio_setup_device(pci_get_sysfs_path(), pci_addr,
457 &vfio_dev_fd, &device_info);
461 /* allocate vfio_res and get region info */
462 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
463 if (vfio_res == NULL) {
465 "%s(): cannot store uio mmap details\n", __func__);
466 goto err_vfio_dev_fd;
468 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
470 /* get number of registers (up to BAR5) */
471 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
472 VFIO_PCI_BAR5_REGION_INDEX + 1);
475 maps = vfio_res->maps;
477 vfio_res->msix_table.bar_index = -1;
478 /* get MSI-X BAR, if any (we have to know where it is because we can't
479 * easily mmap it when using VFIO)
481 ret = pci_vfio_get_msix_bar(vfio_dev_fd, &vfio_res->msix_table);
483 RTE_LOG(ERR, EAL, " %s cannot get MSI-X BAR number!\n",
485 goto err_vfio_dev_fd;
488 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
489 struct vfio_region_info reg = { .argsz = sizeof(reg) };
494 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®);
496 RTE_LOG(ERR, EAL, " %s cannot get device region info "
497 "error %i (%s)\n", pci_addr, errno, strerror(errno));
501 /* chk for io port region */
502 ret = pci_vfio_is_ioport_bar(vfio_dev_fd, i);
506 RTE_LOG(INFO, EAL, "Ignore mapping IO port bar(%d)\n",
511 /* skip non-mmapable BARs */
512 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
515 /* try mapping somewhere close to the end of hugepages */
516 if (pci_map_addr == NULL)
517 pci_map_addr = pci_find_max_end_va();
519 bar_addr = pci_map_addr;
520 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg.size);
522 maps[i].addr = bar_addr;
523 maps[i].offset = reg.offset;
524 maps[i].size = reg.size;
525 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
527 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, 0);
529 RTE_LOG(ERR, EAL, " %s mapping BAR%i failed: %s\n",
530 pci_addr, i, strerror(errno));
534 dev->mem_resource[i].addr = maps[i].addr;
537 if (pci_vfio_setup_device(dev, vfio_dev_fd) < 0) {
538 RTE_LOG(ERR, EAL, " %s setup device failed\n", pci_addr);
542 TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
553 pci_vfio_map_resource_secondary(struct rte_pci_device *dev)
555 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
556 char pci_addr[PATH_MAX] = {0};
558 struct rte_pci_addr *loc = &dev->addr;
560 struct mapped_pci_resource *vfio_res = NULL;
561 struct mapped_pci_res_list *vfio_res_list =
562 RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
564 struct pci_map *maps;
566 dev->intr_handle.fd = -1;
567 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
569 /* store PCI address string */
570 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
571 loc->domain, loc->bus, loc->devid, loc->function);
573 ret = vfio_setup_device(pci_get_sysfs_path(), pci_addr,
574 &vfio_dev_fd, &device_info);
578 /* if we're in a secondary process, just find our tailq entry */
579 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
580 if (rte_eal_compare_pci_addr(&vfio_res->pci_addr,
585 /* if we haven't found our tailq entry, something's wrong */
586 if (vfio_res == NULL) {
587 RTE_LOG(ERR, EAL, " %s cannot find TAILQ entry for PCI device!\n",
589 goto err_vfio_dev_fd;
593 maps = vfio_res->maps;
595 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
596 ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, MAP_FIXED);
598 RTE_LOG(ERR, EAL, " %s mapping BAR%i failed: %s\n",
599 pci_addr, i, strerror(errno));
600 goto err_vfio_dev_fd;
603 dev->mem_resource[i].addr = maps[i].addr;
613 * map the PCI resources of a PCI device in virtual memory (VFIO version).
614 * primary and secondary processes follow almost exactly the same path
617 pci_vfio_map_resource(struct rte_pci_device *dev)
619 if (internal_config.process_type == RTE_PROC_PRIMARY)
620 return pci_vfio_map_resource_primary(dev);
622 return pci_vfio_map_resource_secondary(dev);
626 pci_vfio_unmap_resource(struct rte_pci_device *dev)
628 char pci_addr[PATH_MAX] = {0};
629 struct rte_pci_addr *loc = &dev->addr;
631 struct mapped_pci_resource *vfio_res = NULL;
632 struct mapped_pci_res_list *vfio_res_list;
634 struct pci_map *maps;
636 /* store PCI address string */
637 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
638 loc->domain, loc->bus, loc->devid, loc->function);
641 if (close(dev->intr_handle.fd) < 0) {
642 RTE_LOG(INFO, EAL, "Error when closing eventfd file descriptor for %s\n",
647 if (pci_vfio_set_bus_master(dev->intr_handle.vfio_dev_fd, false)) {
648 RTE_LOG(ERR, EAL, " %s cannot unset bus mastering for PCI device!\n",
653 ret = vfio_release_device(pci_get_sysfs_path(), pci_addr,
654 dev->intr_handle.vfio_dev_fd);
657 "%s(): cannot release device\n", __func__);
661 vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
663 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
664 if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
668 /* if we haven't found our tailq entry, something's wrong */
669 if (vfio_res == NULL) {
670 RTE_LOG(ERR, EAL, " %s cannot find TAILQ entry for PCI device!\n",
676 maps = vfio_res->maps;
678 RTE_LOG(INFO, EAL, "Releasing pci mapped resource for %s\n",
680 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
683 * We do not need to be aware of MSI-X table BAR mappings as
684 * when mapping. Just using current maps array is enough
687 RTE_LOG(INFO, EAL, "Calling pci_unmap_resource for %s at %p\n",
688 pci_addr, maps[i].addr);
689 pci_unmap_resource(maps[i].addr, maps[i].size);
693 TAILQ_REMOVE(vfio_res_list, vfio_res, next);
699 pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
700 struct rte_pci_ioport *p)
702 if (bar < VFIO_PCI_BAR0_REGION_INDEX ||
703 bar > VFIO_PCI_BAR5_REGION_INDEX) {
704 RTE_LOG(ERR, EAL, "invalid bar (%d)!\n", bar);
709 p->base = VFIO_GET_REGION_ADDR(bar);
714 pci_vfio_ioport_read(struct rte_pci_ioport *p,
715 void *data, size_t len, off_t offset)
717 const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
719 if (pread64(intr_handle->vfio_dev_fd, data,
720 len, p->base + offset) <= 0)
722 "Can't read from PCI bar (%" PRIu64 ") : offset (%x)\n",
723 VFIO_GET_REGION_IDX(p->base), (int)offset);
727 pci_vfio_ioport_write(struct rte_pci_ioport *p,
728 const void *data, size_t len, off_t offset)
730 const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
732 if (pwrite64(intr_handle->vfio_dev_fd, data,
733 len, p->base + offset) <= 0)
735 "Can't write to PCI bar (%" PRIu64 ") : offset (%x)\n",
736 VFIO_GET_REGION_IDX(p->base), (int)offset);
740 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
747 pci_vfio_is_enabled(void)
749 return vfio_is_enabled("vfio_pci");