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>
44 #include <rte_eal_memconfig.h>
45 #include <rte_malloc.h>
46 #include <eal_private.h>
48 #include "eal_filesystem.h"
49 #include "eal_pci_init.h"
54 * PCI probing under linux (VFIO version)
56 * This code tries to determine if the PCI device is bound to VFIO driver,
57 * and initialize it (map BARs, set up interrupts) if that's the case.
59 * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
64 #define PAGE_SIZE (sysconf(_SC_PAGESIZE))
65 #define PAGE_MASK (~(PAGE_SIZE - 1))
67 static struct rte_tailq_elem rte_vfio_tailq = {
68 .name = "VFIO_RESOURCE_LIST",
70 EAL_REGISTER_TAILQ(rte_vfio_tailq)
72 #define VFIO_DIR "/dev/vfio"
73 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
74 #define VFIO_GROUP_FMT "/dev/vfio/%u"
75 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
76 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
77 #define VFIO_GET_REGION_IDX(x) (x >> 40)
79 /* per-process VFIO config */
80 static struct vfio_config vfio_cfg;
82 /* DMA mapping function prototype.
83 * Takes VFIO container fd as a parameter.
84 * Returns 0 on success, -1 on error.
86 typedef int (*vfio_dma_func_t)(int);
88 struct vfio_iommu_type {
91 vfio_dma_func_t dma_map_func;
94 static int vfio_type1_dma_map(int);
95 static int vfio_noiommu_dma_map(int);
97 /* IOMMU types we support */
98 static const struct vfio_iommu_type iommu_types[] = {
99 /* x86 IOMMU, otherwise known as type 1 */
100 { RTE_VFIO_TYPE1, "Type 1", &vfio_type1_dma_map},
101 /* IOMMU-less mode */
102 { RTE_VFIO_NOIOMMU, "No-IOMMU", &vfio_noiommu_dma_map},
106 vfio_type1_dma_map(int vfio_container_fd)
108 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
111 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
112 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
113 struct vfio_iommu_type1_dma_map dma_map;
115 if (ms[i].addr == NULL)
118 memset(&dma_map, 0, sizeof(dma_map));
119 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
120 dma_map.vaddr = ms[i].addr_64;
121 dma_map.size = ms[i].len;
122 dma_map.iova = ms[i].phys_addr;
123 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
125 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
128 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
129 "error %i (%s)\n", errno, strerror(errno));
138 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
140 /* No-IOMMU mode does not need DMA mapping */
145 pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
146 void *buf, size_t len, off_t offs)
148 return pread64(intr_handle->vfio_dev_fd, buf, len,
149 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
153 pci_vfio_write_config(const struct rte_intr_handle *intr_handle,
154 const void *buf, size_t len, off_t offs)
156 return pwrite64(intr_handle->vfio_dev_fd, buf, len,
157 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
160 /* get PCI BAR number where MSI-X interrupts are */
162 pci_vfio_get_msix_bar(int fd, int *msix_bar, uint32_t *msix_table_offset,
163 uint32_t *msix_table_size)
168 uint8_t cap_id, cap_offset;
170 /* read PCI capability pointer from config space */
171 ret = pread64(fd, ®, sizeof(reg),
172 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
173 PCI_CAPABILITY_LIST);
174 if (ret != sizeof(reg)) {
175 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
180 /* we need first byte */
181 cap_offset = reg & 0xFF;
185 /* read PCI capability ID */
186 ret = pread64(fd, ®, sizeof(reg),
187 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
189 if (ret != sizeof(reg)) {
190 RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
195 /* we need first byte */
198 /* if we haven't reached MSI-X, check next capability */
199 if (cap_id != PCI_CAP_ID_MSIX) {
200 ret = pread64(fd, ®, sizeof(reg),
201 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
203 if (ret != sizeof(reg)) {
204 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
209 /* we need second byte */
210 cap_offset = (reg & 0xFF00) >> 8;
214 /* else, read table offset */
216 /* table offset resides in the next 4 bytes */
217 ret = pread64(fd, ®, sizeof(reg),
218 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
220 if (ret != sizeof(reg)) {
221 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
226 ret = pread64(fd, &flags, sizeof(flags),
227 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
229 if (ret != sizeof(flags)) {
230 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
235 *msix_bar = reg & RTE_PCI_MSIX_TABLE_BIR;
236 *msix_table_offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
237 *msix_table_size = 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
245 /* set PCI bus mastering */
247 pci_vfio_set_bus_master(int dev_fd)
252 ret = pread64(dev_fd, ®, sizeof(reg),
253 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
255 if (ret != sizeof(reg)) {
256 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
260 /* set the master bit */
261 reg |= PCI_COMMAND_MASTER;
263 ret = pwrite64(dev_fd, ®, sizeof(reg),
264 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
267 if (ret != sizeof(reg)) {
268 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
275 /* pick IOMMU type. returns a pointer to vfio_iommu_type or NULL for error */
276 static const struct vfio_iommu_type *
277 pci_vfio_set_iommu_type(int vfio_container_fd) {
279 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
280 const struct vfio_iommu_type *t = &iommu_types[idx];
282 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
285 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
286 t->type_id, t->name);
289 /* not an error, there may be more supported IOMMU types */
290 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
291 "error %i (%s)\n", t->type_id, t->name, errno,
294 /* if we didn't find a suitable IOMMU type, fail */
298 /* check if we have any supported extensions */
300 pci_vfio_has_supported_extensions(int vfio_container_fd) {
302 unsigned idx, n_extensions = 0;
303 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
304 const struct vfio_iommu_type *t = &iommu_types[idx];
306 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
309 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
310 "error %i (%s)\n", errno,
312 close(vfio_container_fd);
314 } else if (ret == 1) {
315 /* we found a supported extension */
318 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
320 ret ? "supported" : "not supported");
323 /* if we didn't find any supported IOMMU types, fail */
325 close(vfio_container_fd);
332 /* set up interrupt support (but not enable interrupts) */
334 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
336 int i, ret, intr_idx;
338 /* default to invalid index */
339 intr_idx = VFIO_PCI_NUM_IRQS;
341 /* get interrupt type from internal config (MSI-X by default, can be
342 * overriden from the command line
344 switch (internal_config.vfio_intr_mode) {
345 case RTE_INTR_MODE_MSIX:
346 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
348 case RTE_INTR_MODE_MSI:
349 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
351 case RTE_INTR_MODE_LEGACY:
352 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
354 /* don't do anything if we want to automatically determine interrupt type */
355 case RTE_INTR_MODE_NONE:
358 RTE_LOG(ERR, EAL, " unknown default interrupt type!\n");
362 /* start from MSI-X interrupt type */
363 for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
364 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
367 /* skip interrupt modes we don't want */
368 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE &&
374 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
376 RTE_LOG(ERR, EAL, " cannot get IRQ info, "
377 "error %i (%s)\n", errno, strerror(errno));
381 /* if this vector cannot be used with eventfd, fail if we explicitly
382 * specified interrupt type, otherwise continue */
383 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
384 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE) {
386 " interrupt vector does not support eventfd!\n");
392 /* set up an eventfd for interrupts */
393 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
395 RTE_LOG(ERR, EAL, " cannot set up eventfd, "
396 "error %i (%s)\n", errno, strerror(errno));
400 dev->intr_handle.fd = fd;
401 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
404 case VFIO_PCI_MSIX_IRQ_INDEX:
405 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSIX;
406 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
408 case VFIO_PCI_MSI_IRQ_INDEX:
409 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSI;
410 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
412 case VFIO_PCI_INTX_IRQ_INDEX:
413 internal_config.vfio_intr_mode = RTE_INTR_MODE_LEGACY;
414 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
417 RTE_LOG(ERR, EAL, " unknown interrupt type!\n");
424 /* if we're here, we haven't found a suitable interrupt vector */
428 /* open container fd or get an existing one */
430 pci_vfio_get_container_fd(void)
432 int ret, vfio_container_fd;
434 /* if we're in a primary process, try to open the container */
435 if (internal_config.process_type == RTE_PROC_PRIMARY) {
436 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
437 if (vfio_container_fd < 0) {
438 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
439 "error %i (%s)\n", errno, strerror(errno));
443 /* check VFIO API version */
444 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
445 if (ret != VFIO_API_VERSION) {
447 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
448 "error %i (%s)\n", errno, strerror(errno));
450 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
451 close(vfio_container_fd);
455 ret = pci_vfio_has_supported_extensions(vfio_container_fd);
457 RTE_LOG(ERR, EAL, " no supported IOMMU "
458 "extensions found!\n");
462 return vfio_container_fd;
465 * if we're in a secondary process, request container fd from the
466 * primary process via our socket
470 socket_fd = vfio_mp_sync_connect_to_primary();
472 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
475 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
476 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
480 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
481 if (vfio_container_fd < 0) {
482 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
487 return vfio_container_fd;
493 /* open group fd or get an existing one */
495 pci_vfio_get_group_fd(int iommu_group_no)
499 char filename[PATH_MAX];
501 /* check if we already have the group descriptor open */
502 for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
503 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
504 return vfio_cfg.vfio_groups[i].fd;
506 /* if primary, try to open the group */
507 if (internal_config.process_type == RTE_PROC_PRIMARY) {
508 /* try regular group format */
509 snprintf(filename, sizeof(filename),
510 VFIO_GROUP_FMT, iommu_group_no);
511 vfio_group_fd = open(filename, O_RDWR);
512 if (vfio_group_fd < 0) {
513 /* if file not found, it's not an error */
514 if (errno != ENOENT) {
515 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
520 /* special case: try no-IOMMU path as well */
521 snprintf(filename, sizeof(filename),
522 VFIO_NOIOMMU_GROUP_FMT, iommu_group_no);
523 vfio_group_fd = open(filename, O_RDWR);
524 if (vfio_group_fd < 0) {
525 if (errno != ENOENT) {
526 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
532 /* noiommu group found */
535 /* if the fd is valid, create a new group for it */
536 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
537 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
538 close(vfio_group_fd);
541 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
542 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
543 return vfio_group_fd;
545 /* if we're in a secondary process, request group fd from the primary
546 * process via our socket
551 socket_fd = vfio_mp_sync_connect_to_primary();
554 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
557 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
558 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
562 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
563 RTE_LOG(ERR, EAL, " cannot send group number!\n");
567 ret = vfio_mp_sync_receive_request(socket_fd);
573 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
574 /* if we got the fd, return it */
575 if (vfio_group_fd > 0) {
577 return vfio_group_fd;
579 /* fall-through on error */
581 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
589 /* parse IOMMU group number for a PCI device
590 * returns 1 on success, -1 for errors, 0 for non-existent group
593 pci_vfio_get_group_no(const char *pci_addr, int *iommu_group_no)
595 char linkname[PATH_MAX];
596 char filename[PATH_MAX];
597 char *tok[16], *group_tok, *end;
600 memset(linkname, 0, sizeof(linkname));
601 memset(filename, 0, sizeof(filename));
603 /* try to find out IOMMU group for this device */
604 snprintf(linkname, sizeof(linkname),
605 "%s/%s/iommu_group", pci_get_sysfs_path(), pci_addr);
607 ret = readlink(linkname, filename, sizeof(filename));
609 /* if the link doesn't exist, no VFIO for us */
613 ret = rte_strsplit(filename, sizeof(filename),
614 tok, RTE_DIM(tok), '/');
617 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", pci_addr);
621 /* IOMMU group is always the last token */
623 group_tok = tok[ret - 1];
625 *iommu_group_no = strtol(group_tok, &end, 10);
626 if ((end != group_tok && *end != '\0') || errno != 0) {
627 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", pci_addr);
635 clear_current_group(void)
637 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
638 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
643 * map the PCI resources of a PCI device in virtual memory (VFIO version).
644 * primary and secondary processes follow almost exactly the same path
647 pci_vfio_map_resource(struct rte_pci_device *dev)
649 struct vfio_group_status group_status = {
650 .argsz = sizeof(group_status)
652 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
653 int vfio_group_fd, vfio_dev_fd;
655 char pci_addr[PATH_MAX] = {0};
656 struct rte_pci_addr *loc = &dev->addr;
657 int i, ret, msix_bar;
658 struct mapped_pci_resource *vfio_res = NULL;
659 struct mapped_pci_res_list *vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
661 struct pci_map *maps;
662 uint32_t msix_table_offset = 0;
663 uint32_t msix_table_size = 0;
666 dev->intr_handle.fd = -1;
667 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
669 /* store PCI address string */
670 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
671 loc->domain, loc->bus, loc->devid, loc->function);
673 /* get group number */
674 ret = pci_vfio_get_group_no(pci_addr, &iommu_group_no);
676 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
681 /* if negative, something failed */
685 /* get the actual group fd */
686 vfio_group_fd = pci_vfio_get_group_fd(iommu_group_no);
687 if (vfio_group_fd < 0)
691 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
692 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
694 /* if group_fd == 0, that means the device isn't managed by VFIO */
695 if (vfio_group_fd == 0) {
696 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
698 /* we store 0 as group fd to distinguish between existing but
699 * unbound VFIO groups, and groups that don't exist at all.
701 vfio_cfg.vfio_group_idx++;
706 * at this point, we know at least one port on this device is bound to VFIO,
707 * so we can proceed to try and set this particular port up
710 /* check if the group is viable */
711 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
713 RTE_LOG(ERR, EAL, " %s cannot get group status, "
714 "error %i (%s)\n", pci_addr, errno, strerror(errno));
715 close(vfio_group_fd);
716 clear_current_group();
718 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
719 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", pci_addr);
720 close(vfio_group_fd);
721 clear_current_group();
726 * at this point, we know that this group is viable (meaning, all devices
727 * are either bound to VFIO or not bound to anything)
730 /* check if group does not have a container yet */
731 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
733 /* add group to a container */
734 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
735 &vfio_cfg.vfio_container_fd);
737 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
738 "error %i (%s)\n", pci_addr, errno, strerror(errno));
739 close(vfio_group_fd);
740 clear_current_group();
744 * at this point we know that this group has been successfully
745 * initialized, so we increment vfio_group_idx to indicate that we can
748 vfio_cfg.vfio_group_idx++;
752 * pick an IOMMU type and set up DMA mappings for container
754 * needs to be done only once, only when at least one group is assigned to
755 * a container and only in primary process
757 if (internal_config.process_type == RTE_PROC_PRIMARY &&
758 vfio_cfg.vfio_container_has_dma == 0) {
759 /* select an IOMMU type which we will be using */
760 const struct vfio_iommu_type *t =
761 pci_vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
763 RTE_LOG(ERR, EAL, " %s failed to select IOMMU type\n", pci_addr);
766 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
768 RTE_LOG(ERR, EAL, " %s DMA remapping failed, "
769 "error %i (%s)\n", pci_addr, errno, strerror(errno));
772 vfio_cfg.vfio_container_has_dma = 1;
775 /* get a file descriptor for the device */
776 vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, pci_addr);
777 if (vfio_dev_fd < 0) {
778 /* if we cannot get a device fd, this simply means that this
779 * particular port is not bound to VFIO
781 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
786 /* test and setup the device */
787 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info);
789 RTE_LOG(ERR, EAL, " %s cannot get device info, "
790 "error %i (%s)\n", pci_addr, errno, strerror(errno));
795 /* get MSI-X BAR, if any (we have to know where it is because we can't
796 * easily mmap it when using VFIO) */
798 ret = pci_vfio_get_msix_bar(vfio_dev_fd, &msix_bar,
799 &msix_table_offset, &msix_table_size);
801 RTE_LOG(ERR, EAL, " %s cannot get MSI-X BAR number!\n", pci_addr);
806 /* if we're in a primary process, allocate vfio_res and get region info */
807 if (internal_config.process_type == RTE_PROC_PRIMARY) {
808 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
809 if (vfio_res == NULL) {
811 "%s(): cannot store uio mmap details\n", __func__);
815 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
817 /* get number of registers (up to BAR5) */
818 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
819 VFIO_PCI_BAR5_REGION_INDEX + 1);
821 /* if we're in a secondary process, just find our tailq entry */
822 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
823 if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
827 /* if we haven't found our tailq entry, something's wrong */
828 if (vfio_res == NULL) {
829 RTE_LOG(ERR, EAL, " %s cannot find TAILQ entry for PCI device!\n",
837 maps = vfio_res->maps;
839 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
840 struct vfio_region_info reg = { .argsz = sizeof(reg) };
843 unsigned long offset, size;
848 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®);
851 RTE_LOG(ERR, EAL, " %s cannot get device region info "
852 "error %i (%s)\n", pci_addr, errno, strerror(errno));
854 if (internal_config.process_type == RTE_PROC_PRIMARY)
859 /* chk for io port region */
860 ret = pread64(vfio_dev_fd, &ioport_bar, sizeof(ioport_bar),
861 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX)
862 + PCI_BASE_ADDRESS_0 + i*4);
864 if (ret != sizeof(ioport_bar)) {
866 "Cannot read command (%x) from config space!\n",
867 PCI_BASE_ADDRESS_0 + i*4);
871 if (ioport_bar & PCI_BASE_ADDRESS_SPACE_IO) {
873 "Ignore mapping IO port bar(%d) addr: %x\n",
878 /* skip non-mmapable BARs */
879 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
884 * VFIO will not let us map the MSI-X table,
885 * but we can map around it.
887 uint32_t table_start = msix_table_offset;
888 uint32_t table_end = table_start + msix_table_size;
889 table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
890 table_start &= PAGE_MASK;
892 if (table_start == 0 && table_end >= reg.size) {
893 /* Cannot map this BAR */
894 RTE_LOG(DEBUG, EAL, "Skipping BAR %d\n", i);
897 memreg[0].offset = reg.offset;
898 memreg[0].size = table_start;
899 memreg[1].offset = table_end;
900 memreg[1].size = reg.size - table_end;
903 "Trying to map BAR %d that contains the MSI-X "
904 "table. Trying offsets: "
905 "0x%04lx:0x%04lx, 0x%04lx:0x%04lx\n", i,
906 memreg[0].offset, memreg[0].size,
907 memreg[1].offset, memreg[1].size);
910 memreg[0].offset = reg.offset;
911 memreg[0].size = reg.size;
914 /* try to figure out an address */
915 if (internal_config.process_type == RTE_PROC_PRIMARY) {
916 /* try mapping somewhere close to the end of hugepages */
917 if (pci_map_addr == NULL)
918 pci_map_addr = pci_find_max_end_va();
920 bar_addr = pci_map_addr;
921 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg.size);
923 bar_addr = maps[i].addr;
926 /* reserve the address using an inaccessible mapping */
927 bar_addr = mmap(bar_addr, reg.size, 0, MAP_PRIVATE |
928 MAP_ANONYMOUS, -1, 0);
929 if (bar_addr != MAP_FAILED) {
930 void *map_addr = NULL;
931 if (memreg[0].size) {
932 /* actual map of first part */
933 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
939 /* if there's a second part, try to map it */
940 if (map_addr != MAP_FAILED
941 && memreg[1].offset && memreg[1].size) {
942 void *second_addr = RTE_PTR_ADD(bar_addr, memreg[1].offset);
943 map_addr = pci_map_resource(second_addr,
944 vfio_dev_fd, memreg[1].offset,
949 if (map_addr == MAP_FAILED || !map_addr) {
950 munmap(bar_addr, reg.size);
951 bar_addr = MAP_FAILED;
955 if (bar_addr == MAP_FAILED ||
956 (internal_config.process_type == RTE_PROC_SECONDARY &&
957 bar_addr != maps[i].addr)) {
958 RTE_LOG(ERR, EAL, " %s mapping BAR%i failed: %s\n", pci_addr, i,
961 if (internal_config.process_type == RTE_PROC_PRIMARY)
966 maps[i].addr = bar_addr;
967 maps[i].offset = reg.offset;
968 maps[i].size = reg.size;
969 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
970 dev->mem_resource[i].addr = bar_addr;
973 /* if secondary process, do not set up interrupts */
974 if (internal_config.process_type == RTE_PROC_PRIMARY) {
975 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
976 RTE_LOG(ERR, EAL, " %s error setting up interrupts!\n", pci_addr);
982 /* set bus mastering for the device */
983 if (pci_vfio_set_bus_master(vfio_dev_fd)) {
984 RTE_LOG(ERR, EAL, " %s cannot set up bus mastering!\n", pci_addr);
990 /* Reset the device */
991 ioctl(vfio_dev_fd, VFIO_DEVICE_RESET);
994 if (internal_config.process_type == RTE_PROC_PRIMARY)
995 TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
1001 pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
1002 struct rte_pci_ioport *p)
1004 if (bar < VFIO_PCI_BAR0_REGION_INDEX ||
1005 bar > VFIO_PCI_BAR5_REGION_INDEX) {
1006 RTE_LOG(ERR, EAL, "invalid bar (%d)!\n", bar);
1011 p->base = VFIO_GET_REGION_ADDR(bar);
1016 pci_vfio_ioport_read(struct rte_pci_ioport *p,
1017 void *data, size_t len, off_t offset)
1019 const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
1021 if (pread64(intr_handle->vfio_dev_fd, data,
1022 len, p->base + offset) <= 0)
1024 "Can't read from PCI bar (%" PRIu64 ") : offset (%x)\n",
1025 VFIO_GET_REGION_IDX(p->base), (int)offset);
1029 pci_vfio_ioport_write(struct rte_pci_ioport *p,
1030 const void *data, size_t len, off_t offset)
1032 const struct rte_intr_handle *intr_handle = &p->dev->intr_handle;
1034 if (pwrite64(intr_handle->vfio_dev_fd, data,
1035 len, p->base + offset) <= 0)
1037 "Can't write to PCI bar (%" PRIu64 ") : offset (%x)\n",
1038 VFIO_GET_REGION_IDX(p->base), (int)offset);
1042 pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
1049 pci_vfio_enable(void)
1051 /* initialize group list */
1055 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
1056 vfio_cfg.vfio_groups[i].fd = -1;
1057 vfio_cfg.vfio_groups[i].group_no = -1;
1060 /* inform the user that we are probing for VFIO */
1061 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
1063 /* check if vfio-pci module is loaded */
1064 vfio_available = rte_eal_check_module("vfio_pci");
1066 /* return error directly */
1067 if (vfio_available == -1) {
1068 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
1072 /* return 0 if VFIO modules not loaded */
1073 if (vfio_available == 0) {
1074 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
1075 "skipping VFIO support...\n");
1079 vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
1081 /* check if we have VFIO driver enabled */
1082 if (vfio_cfg.vfio_container_fd != -1) {
1083 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
1084 vfio_cfg.vfio_enabled = 1;
1086 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
1093 pci_vfio_is_enabled(void)
1095 return vfio_cfg.vfio_enabled;