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_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
77 /* per-process VFIO config */
78 static struct vfio_config vfio_cfg;
81 pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
82 void *buf, size_t len, off_t offs)
84 return pread64(intr_handle->vfio_dev_fd, buf, len,
85 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
89 pci_vfio_write_config(const struct rte_intr_handle *intr_handle,
90 const void *buf, size_t len, off_t offs)
92 return pwrite64(intr_handle->vfio_dev_fd, buf, len,
93 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
96 /* get PCI BAR number where MSI-X interrupts are */
98 pci_vfio_get_msix_bar(int fd, int *msix_bar, uint32_t *msix_table_offset,
99 uint32_t *msix_table_size)
104 uint8_t cap_id, cap_offset;
106 /* read PCI capability pointer from config space */
107 ret = pread64(fd, ®, sizeof(reg),
108 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
109 PCI_CAPABILITY_LIST);
110 if (ret != sizeof(reg)) {
111 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
116 /* we need first byte */
117 cap_offset = reg & 0xFF;
121 /* read PCI capability ID */
122 ret = pread64(fd, ®, sizeof(reg),
123 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
125 if (ret != sizeof(reg)) {
126 RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
131 /* we need first byte */
134 /* if we haven't reached MSI-X, check next capability */
135 if (cap_id != PCI_CAP_ID_MSIX) {
136 ret = pread64(fd, ®, sizeof(reg),
137 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
139 if (ret != sizeof(reg)) {
140 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
145 /* we need second byte */
146 cap_offset = (reg & 0xFF00) >> 8;
150 /* else, read table offset */
152 /* table offset resides in the next 4 bytes */
153 ret = pread64(fd, ®, sizeof(reg),
154 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
156 if (ret != sizeof(reg)) {
157 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
162 ret = pread64(fd, &flags, sizeof(flags),
163 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
165 if (ret != sizeof(flags)) {
166 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
171 *msix_bar = reg & RTE_PCI_MSIX_TABLE_BIR;
172 *msix_table_offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
173 *msix_table_size = 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
181 /* set PCI bus mastering */
183 pci_vfio_set_bus_master(int dev_fd)
188 ret = pread64(dev_fd, ®, sizeof(reg),
189 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
191 if (ret != sizeof(reg)) {
192 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
196 /* set the master bit */
197 reg |= PCI_COMMAND_MASTER;
199 ret = pwrite64(dev_fd, ®, sizeof(reg),
200 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
203 if (ret != sizeof(reg)) {
204 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
211 /* set up DMA mappings */
213 pci_vfio_setup_dma_maps(int vfio_container_fd)
215 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
218 ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
221 RTE_LOG(ERR, EAL, " cannot set IOMMU type, "
222 "error %i (%s)\n", errno, strerror(errno));
226 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
227 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
228 struct vfio_iommu_type1_dma_map dma_map;
230 if (ms[i].addr == NULL)
233 memset(&dma_map, 0, sizeof(dma_map));
234 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
235 dma_map.vaddr = ms[i].addr_64;
236 dma_map.size = ms[i].len;
237 dma_map.iova = ms[i].phys_addr;
238 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
240 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
243 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
244 "error %i (%s)\n", errno, strerror(errno));
252 /* set up interrupt support (but not enable interrupts) */
254 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
256 int i, ret, intr_idx;
258 /* default to invalid index */
259 intr_idx = VFIO_PCI_NUM_IRQS;
261 /* get interrupt type from internal config (MSI-X by default, can be
262 * overriden from the command line
264 switch (internal_config.vfio_intr_mode) {
265 case RTE_INTR_MODE_MSIX:
266 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
268 case RTE_INTR_MODE_MSI:
269 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
271 case RTE_INTR_MODE_LEGACY:
272 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
274 /* don't do anything if we want to automatically determine interrupt type */
275 case RTE_INTR_MODE_NONE:
278 RTE_LOG(ERR, EAL, " unknown default interrupt type!\n");
282 /* start from MSI-X interrupt type */
283 for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
284 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
287 /* skip interrupt modes we don't want */
288 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE &&
294 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
296 RTE_LOG(ERR, EAL, " cannot get IRQ info, "
297 "error %i (%s)\n", errno, strerror(errno));
301 /* if this vector cannot be used with eventfd, fail if we explicitly
302 * specified interrupt type, otherwise continue */
303 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
304 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE) {
306 " interrupt vector does not support eventfd!\n");
312 /* set up an eventfd for interrupts */
313 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
315 RTE_LOG(ERR, EAL, " cannot set up eventfd, "
316 "error %i (%s)\n", errno, strerror(errno));
320 dev->intr_handle.fd = fd;
321 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
324 case VFIO_PCI_MSIX_IRQ_INDEX:
325 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSIX;
326 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
328 case VFIO_PCI_MSI_IRQ_INDEX:
329 internal_config.vfio_intr_mode = RTE_INTR_MODE_MSI;
330 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
332 case VFIO_PCI_INTX_IRQ_INDEX:
333 internal_config.vfio_intr_mode = RTE_INTR_MODE_LEGACY;
334 dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
337 RTE_LOG(ERR, EAL, " unknown interrupt type!\n");
344 /* if we're here, we haven't found a suitable interrupt vector */
348 /* open container fd or get an existing one */
350 pci_vfio_get_container_fd(void)
352 int ret, vfio_container_fd;
354 /* if we're in a primary process, try to open the container */
355 if (internal_config.process_type == RTE_PROC_PRIMARY) {
356 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
357 if (vfio_container_fd < 0) {
358 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
359 "error %i (%s)\n", errno, strerror(errno));
363 /* check VFIO API version */
364 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
365 if (ret != VFIO_API_VERSION) {
367 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
368 "error %i (%s)\n", errno, strerror(errno));
370 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
371 close(vfio_container_fd);
375 /* check if we support IOMMU type 1 */
376 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
379 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
380 "error %i (%s)\n", errno,
383 RTE_LOG(ERR, EAL, " unsupported IOMMU type "
384 "detected in VFIO\n");
385 close(vfio_container_fd);
389 return vfio_container_fd;
392 * if we're in a secondary process, request container fd from the
393 * primary process via our socket
397 socket_fd = vfio_mp_sync_connect_to_primary();
399 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
402 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
403 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
407 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
408 if (vfio_container_fd < 0) {
409 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
414 return vfio_container_fd;
420 /* open group fd or get an existing one */
422 pci_vfio_get_group_fd(int iommu_group_no)
426 char filename[PATH_MAX];
428 /* check if we already have the group descriptor open */
429 for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
430 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
431 return vfio_cfg.vfio_groups[i].fd;
433 /* if primary, try to open the group */
434 if (internal_config.process_type == RTE_PROC_PRIMARY) {
435 snprintf(filename, sizeof(filename),
436 VFIO_GROUP_FMT, iommu_group_no);
437 vfio_group_fd = open(filename, O_RDWR);
438 if (vfio_group_fd < 0) {
439 /* if file not found, it's not an error */
440 if (errno != ENOENT) {
441 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
448 /* if the fd is valid, create a new group for it */
449 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
450 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
453 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
454 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
455 return vfio_group_fd;
457 /* if we're in a secondary process, request group fd from the primary
458 * process via our socket
463 socket_fd = vfio_mp_sync_connect_to_primary();
466 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
469 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
470 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
474 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
475 RTE_LOG(ERR, EAL, " cannot send group number!\n");
479 ret = vfio_mp_sync_receive_request(socket_fd);
485 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
486 /* if we got the fd, return it */
487 if (vfio_group_fd > 0) {
489 return vfio_group_fd;
491 /* fall-through on error */
493 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
501 /* parse IOMMU group number for a PCI device
502 * returns -1 for errors, 0 for non-existent group */
504 pci_vfio_get_group_no(const char *pci_addr)
506 char linkname[PATH_MAX];
507 char filename[PATH_MAX];
508 char *tok[16], *group_tok, *end;
509 int ret, iommu_group_no;
511 memset(linkname, 0, sizeof(linkname));
512 memset(filename, 0, sizeof(filename));
514 /* try to find out IOMMU group for this device */
515 snprintf(linkname, sizeof(linkname),
516 SYSFS_PCI_DEVICES "/%s/iommu_group", pci_addr);
518 ret = readlink(linkname, filename, sizeof(filename));
520 /* if the link doesn't exist, no VFIO for us */
524 ret = rte_strsplit(filename, sizeof(filename),
525 tok, RTE_DIM(tok), '/');
528 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", pci_addr);
532 /* IOMMU group is always the last token */
534 group_tok = tok[ret - 1];
536 iommu_group_no = strtol(group_tok, &end, 10);
537 if ((end != group_tok && *end != '\0') || errno != 0) {
538 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", pci_addr);
542 return iommu_group_no;
546 clear_current_group(void)
548 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
549 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
554 * map the PCI resources of a PCI device in virtual memory (VFIO version).
555 * primary and secondary processes follow almost exactly the same path
558 pci_vfio_map_resource(struct rte_pci_device *dev)
560 struct vfio_group_status group_status = {
561 .argsz = sizeof(group_status)
563 struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
564 int vfio_group_fd, vfio_dev_fd;
566 char pci_addr[PATH_MAX] = {0};
567 struct rte_pci_addr *loc = &dev->addr;
568 int i, ret, msix_bar;
569 struct mapped_pci_resource *vfio_res = NULL;
570 struct mapped_pci_res_list *vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
572 struct pci_map *maps;
573 uint32_t msix_table_offset = 0;
574 uint32_t msix_table_size = 0;
576 dev->intr_handle.fd = -1;
577 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
579 /* store PCI address string */
580 snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
581 loc->domain, loc->bus, loc->devid, loc->function);
583 /* get group number */
584 iommu_group_no = pci_vfio_get_group_no(pci_addr);
586 /* if 0, group doesn't exist */
587 if (iommu_group_no == 0) {
588 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
592 /* if negative, something failed */
593 else if (iommu_group_no < 0)
596 /* get the actual group fd */
597 vfio_group_fd = pci_vfio_get_group_fd(iommu_group_no);
598 if (vfio_group_fd < 0)
602 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
603 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
605 /* if group_fd == 0, that means the device isn't managed by VFIO */
606 if (vfio_group_fd == 0) {
607 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
609 /* we store 0 as group fd to distinguish between existing but
610 * unbound VFIO groups, and groups that don't exist at all.
612 vfio_cfg.vfio_group_idx++;
617 * at this point, we know at least one port on this device is bound to VFIO,
618 * so we can proceed to try and set this particular port up
621 /* check if the group is viable */
622 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
624 RTE_LOG(ERR, EAL, " %s cannot get group status, "
625 "error %i (%s)\n", pci_addr, errno, strerror(errno));
626 close(vfio_group_fd);
627 clear_current_group();
629 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
630 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", pci_addr);
631 close(vfio_group_fd);
632 clear_current_group();
637 * at this point, we know that this group is viable (meaning, all devices
638 * are either bound to VFIO or not bound to anything)
641 /* check if group does not have a container yet */
642 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
644 /* add group to a container */
645 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
646 &vfio_cfg.vfio_container_fd);
648 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
649 "error %i (%s)\n", pci_addr, errno, strerror(errno));
650 close(vfio_group_fd);
651 clear_current_group();
655 * at this point we know that this group has been successfully
656 * initialized, so we increment vfio_group_idx to indicate that we can
659 vfio_cfg.vfio_group_idx++;
663 * set up DMA mappings for container
665 * needs to be done only once, only when at least one group is assigned to
666 * a container and only in primary process
668 if (internal_config.process_type == RTE_PROC_PRIMARY &&
669 vfio_cfg.vfio_container_has_dma == 0) {
670 ret = pci_vfio_setup_dma_maps(vfio_cfg.vfio_container_fd);
672 RTE_LOG(ERR, EAL, " %s DMA remapping failed, "
673 "error %i (%s)\n", pci_addr, errno, strerror(errno));
676 vfio_cfg.vfio_container_has_dma = 1;
679 /* get a file descriptor for the device */
680 vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, pci_addr);
681 if (vfio_dev_fd < 0) {
682 /* if we cannot get a device fd, this simply means that this
683 * particular port is not bound to VFIO
685 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
690 /* test and setup the device */
691 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info);
693 RTE_LOG(ERR, EAL, " %s cannot get device info, "
694 "error %i (%s)\n", pci_addr, errno, strerror(errno));
699 /* get MSI-X BAR, if any (we have to know where it is because we can't
700 * easily mmap it when using VFIO) */
702 ret = pci_vfio_get_msix_bar(vfio_dev_fd, &msix_bar,
703 &msix_table_offset, &msix_table_size);
705 RTE_LOG(ERR, EAL, " %s cannot get MSI-X BAR number!\n", pci_addr);
710 /* if we're in a primary process, allocate vfio_res and get region info */
711 if (internal_config.process_type == RTE_PROC_PRIMARY) {
712 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
713 if (vfio_res == NULL) {
715 "%s(): cannot store uio mmap details\n", __func__);
719 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
721 /* get number of registers (up to BAR5) */
722 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
723 VFIO_PCI_BAR5_REGION_INDEX + 1);
725 /* if we're in a secondary process, just find our tailq entry */
726 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
727 if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
731 /* if we haven't found our tailq entry, something's wrong */
732 if (vfio_res == NULL) {
733 RTE_LOG(ERR, EAL, " %s cannot find TAILQ entry for PCI device!\n",
741 maps = vfio_res->maps;
743 for (i = 0; i < (int) vfio_res->nb_maps; i++) {
744 struct vfio_region_info reg = { .argsz = sizeof(reg) };
747 unsigned long offset, size;
752 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®);
755 RTE_LOG(ERR, EAL, " %s cannot get device region info "
756 "error %i (%s)\n", pci_addr, errno, strerror(errno));
758 if (internal_config.process_type == RTE_PROC_PRIMARY)
763 /* skip non-mmapable BARs */
764 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
769 * VFIO will not let us map the MSI-X table,
770 * but we can map around it.
772 uint32_t table_start = msix_table_offset;
773 uint32_t table_end = table_start + msix_table_size;
774 table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
775 table_start &= PAGE_MASK;
777 if (table_start == 0 && table_end >= reg.size) {
778 /* Cannot map this BAR */
779 RTE_LOG(DEBUG, EAL, "Skipping BAR %d\n", i);
782 memreg[0].offset = reg.offset;
783 memreg[0].size = table_start;
784 memreg[1].offset = table_end;
785 memreg[1].size = reg.size - table_end;
788 "Trying to map BAR %d that contains the MSI-X "
789 "table. Trying offsets: "
790 "0x%04lx:0x%04lx, 0x%04lx:0x%04lx\n", i,
791 memreg[0].offset, memreg[0].size,
792 memreg[1].offset, memreg[1].size);
795 memreg[0].offset = reg.offset;
796 memreg[0].size = reg.size;
799 /* try to figure out an address */
800 if (internal_config.process_type == RTE_PROC_PRIMARY) {
801 /* try mapping somewhere close to the end of hugepages */
802 if (pci_map_addr == NULL)
803 pci_map_addr = pci_find_max_end_va();
805 bar_addr = pci_map_addr;
806 pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg.size);
808 bar_addr = maps[i].addr;
811 /* reserve the address using an inaccessible mapping */
812 bar_addr = mmap(bar_addr, reg.size, 0, MAP_PRIVATE |
813 MAP_ANONYMOUS, -1, 0);
814 if (bar_addr != MAP_FAILED) {
815 void *map_addr = NULL;
816 if (memreg[0].size) {
817 /* actual map of first part */
818 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
824 /* if there's a second part, try to map it */
825 if (map_addr != MAP_FAILED
826 && memreg[1].offset && memreg[1].size) {
827 void *second_addr = RTE_PTR_ADD(bar_addr, memreg[1].offset);
828 map_addr = pci_map_resource(second_addr,
829 vfio_dev_fd, memreg[1].offset,
834 if (map_addr == MAP_FAILED || !map_addr) {
835 munmap(bar_addr, reg.size);
836 bar_addr = MAP_FAILED;
840 if (bar_addr == MAP_FAILED ||
841 (internal_config.process_type == RTE_PROC_SECONDARY &&
842 bar_addr != maps[i].addr)) {
843 RTE_LOG(ERR, EAL, " %s mapping BAR%i failed: %s\n", pci_addr, i,
846 if (internal_config.process_type == RTE_PROC_PRIMARY)
851 maps[i].addr = bar_addr;
852 maps[i].offset = reg.offset;
853 maps[i].size = reg.size;
854 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
855 dev->mem_resource[i].addr = bar_addr;
858 /* if secondary process, do not set up interrupts */
859 if (internal_config.process_type == RTE_PROC_PRIMARY) {
860 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
861 RTE_LOG(ERR, EAL, " %s error setting up interrupts!\n", pci_addr);
867 /* set bus mastering for the device */
868 if (pci_vfio_set_bus_master(vfio_dev_fd)) {
869 RTE_LOG(ERR, EAL, " %s cannot set up bus mastering!\n", pci_addr);
875 /* Reset the device */
876 ioctl(vfio_dev_fd, VFIO_DEVICE_RESET);
879 if (internal_config.process_type == RTE_PROC_PRIMARY)
880 TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
886 pci_vfio_enable(void)
888 /* initialize group list */
890 int module_vfio_type1;
892 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
893 vfio_cfg.vfio_groups[i].fd = -1;
894 vfio_cfg.vfio_groups[i].group_no = -1;
897 module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
899 /* return error directly */
900 if (module_vfio_type1 == -1) {
901 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
905 /* return 0 if VFIO modules not loaded */
906 if (module_vfio_type1 == 0) {
907 RTE_LOG(INFO, EAL, "VFIO modules not all loaded, "
908 "skip VFIO support...\n");
912 vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
914 /* check if we have VFIO driver enabled */
915 if (vfio_cfg.vfio_container_fd != -1)
916 vfio_cfg.vfio_enabled = 1;
918 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
924 pci_vfio_is_enabled(void)
926 return vfio_cfg.vfio_enabled;