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.
37 #include <sys/ioctl.h>
40 #include <rte_memory.h>
41 #include <rte_eal_memconfig.h>
43 #include "eal_filesystem.h"
45 #include "eal_private.h"
49 /* per-process VFIO config */
50 static struct vfio_config vfio_cfg;
52 static int vfio_type1_dma_map(int);
53 static int vfio_spapr_dma_map(int);
54 static int vfio_noiommu_dma_map(int);
56 /* IOMMU types we support */
57 static const struct vfio_iommu_type iommu_types[] = {
58 /* x86 IOMMU, otherwise known as type 1 */
59 { RTE_VFIO_TYPE1, "Type 1", &vfio_type1_dma_map},
60 /* ppc64 IOMMU, otherwise known as spapr */
61 { RTE_VFIO_SPAPR, "sPAPR", &vfio_spapr_dma_map},
63 { RTE_VFIO_NOIOMMU, "No-IOMMU", &vfio_noiommu_dma_map},
67 vfio_get_group_fd(int iommu_group_no)
72 char filename[PATH_MAX];
74 /* check if we already have the group descriptor open */
75 for (i = 0; i < VFIO_MAX_GROUPS; i++)
76 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
77 return vfio_cfg.vfio_groups[i].fd;
79 /* Lets see first if there is room for a new group */
80 if (vfio_cfg.vfio_active_groups == VFIO_MAX_GROUPS) {
81 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
85 /* Now lets get an index for the new group */
86 for (i = 0; i < VFIO_MAX_GROUPS; i++)
87 if (vfio_cfg.vfio_groups[i].group_no == -1) {
92 /* This should not happen */
93 if (group_idx == -1) {
94 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
97 /* if primary, try to open the group */
98 if (internal_config.process_type == RTE_PROC_PRIMARY) {
99 /* try regular group format */
100 snprintf(filename, sizeof(filename),
101 VFIO_GROUP_FMT, iommu_group_no);
102 vfio_group_fd = open(filename, O_RDWR);
103 if (vfio_group_fd < 0) {
104 /* if file not found, it's not an error */
105 if (errno != ENOENT) {
106 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
111 /* special case: try no-IOMMU path as well */
112 snprintf(filename, sizeof(filename),
113 VFIO_NOIOMMU_GROUP_FMT, iommu_group_no);
114 vfio_group_fd = open(filename, O_RDWR);
115 if (vfio_group_fd < 0) {
116 if (errno != ENOENT) {
117 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
123 /* noiommu group found */
126 vfio_cfg.vfio_groups[group_idx].group_no = iommu_group_no;
127 vfio_cfg.vfio_groups[group_idx].fd = vfio_group_fd;
128 vfio_cfg.vfio_active_groups++;
129 return vfio_group_fd;
131 /* if we're in a secondary process, request group fd from the primary
132 * process via our socket
137 socket_fd = vfio_mp_sync_connect_to_primary();
140 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
143 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
144 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
148 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
149 RTE_LOG(ERR, EAL, " cannot send group number!\n");
153 ret = vfio_mp_sync_receive_request(socket_fd);
159 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
160 /* if we got the fd, return it */
161 if (vfio_group_fd > 0) {
163 return vfio_group_fd;
165 /* fall-through on error */
167 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
177 get_vfio_group_idx(int vfio_group_fd)
180 for (i = 0; i < VFIO_MAX_GROUPS; i++)
181 if (vfio_cfg.vfio_groups[i].fd == vfio_group_fd)
187 vfio_group_device_get(int vfio_group_fd)
191 i = get_vfio_group_idx(vfio_group_fd);
192 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
193 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
195 vfio_cfg.vfio_groups[i].devices++;
199 vfio_group_device_put(int vfio_group_fd)
203 i = get_vfio_group_idx(vfio_group_fd);
204 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
205 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
207 vfio_cfg.vfio_groups[i].devices--;
211 vfio_group_device_count(int vfio_group_fd)
215 i = get_vfio_group_idx(vfio_group_fd);
216 if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
217 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
221 return vfio_cfg.vfio_groups[i].devices;
225 clear_group(int vfio_group_fd)
230 if (internal_config.process_type == RTE_PROC_PRIMARY) {
232 i = get_vfio_group_idx(vfio_group_fd);
235 vfio_cfg.vfio_groups[i].group_no = -1;
236 vfio_cfg.vfio_groups[i].fd = -1;
237 vfio_cfg.vfio_groups[i].devices = 0;
238 vfio_cfg.vfio_active_groups--;
242 /* This is just for SECONDARY processes */
243 socket_fd = vfio_mp_sync_connect_to_primary();
246 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
250 if (vfio_mp_sync_send_request(socket_fd, SOCKET_CLR_GROUP) < 0) {
251 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
256 if (vfio_mp_sync_send_request(socket_fd, vfio_group_fd) < 0) {
257 RTE_LOG(ERR, EAL, " cannot send group fd!\n");
262 ret = vfio_mp_sync_receive_request(socket_fd);
265 RTE_LOG(ERR, EAL, " BAD VFIO group fd!\n");
272 RTE_LOG(ERR, EAL, " Socket error\n");
276 RTE_LOG(ERR, EAL, " UNKNOWN reply, %d\n", ret);
283 vfio_setup_device(const char *sysfs_base, const char *dev_addr,
284 int *vfio_dev_fd, struct vfio_device_info *device_info)
286 struct vfio_group_status group_status = {
287 .argsz = sizeof(group_status)
293 /* get group number */
294 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
296 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
301 /* if negative, something failed */
305 /* get the actual group fd */
306 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
307 if (vfio_group_fd < 0)
310 /* if group_fd == 0, that means the device isn't managed by VFIO */
311 if (vfio_group_fd == 0) {
312 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
318 * at this point, we know that this group is viable (meaning, all devices
319 * are either bound to VFIO or not bound to anything)
322 /* check if the group is viable */
323 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
325 RTE_LOG(ERR, EAL, " %s cannot get group status, "
326 "error %i (%s)\n", dev_addr, errno, strerror(errno));
327 close(vfio_group_fd);
328 clear_group(vfio_group_fd);
330 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
331 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", dev_addr);
332 close(vfio_group_fd);
333 clear_group(vfio_group_fd);
337 /* check if group does not have a container yet */
338 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
340 /* add group to a container */
341 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
342 &vfio_cfg.vfio_container_fd);
344 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
345 "error %i (%s)\n", dev_addr, errno, strerror(errno));
346 close(vfio_group_fd);
347 clear_group(vfio_group_fd);
352 * pick an IOMMU type and set up DMA mappings for container
354 * needs to be done only once, only when first group is
355 * assigned to a container and only in primary process.
356 * Note this can happen several times with the hotplug
359 if (internal_config.process_type == RTE_PROC_PRIMARY &&
360 vfio_cfg.vfio_active_groups == 1) {
361 /* select an IOMMU type which we will be using */
362 const struct vfio_iommu_type *t =
363 vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
366 " %s failed to select IOMMU type\n",
368 close(vfio_group_fd);
369 clear_group(vfio_group_fd);
372 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
375 " %s DMA remapping failed, error %i (%s)\n",
376 dev_addr, errno, strerror(errno));
377 close(vfio_group_fd);
378 clear_group(vfio_group_fd);
384 /* get a file descriptor for the device */
385 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
386 if (*vfio_dev_fd < 0) {
387 /* if we cannot get a device fd, this implies a problem with
388 * the VFIO group or the container not having IOMMU configured.
391 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
393 close(vfio_group_fd);
394 clear_group(vfio_group_fd);
398 /* test and setup the device */
399 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
401 RTE_LOG(ERR, EAL, " %s cannot get device info, "
402 "error %i (%s)\n", dev_addr, errno,
405 close(vfio_group_fd);
406 clear_group(vfio_group_fd);
409 vfio_group_device_get(vfio_group_fd);
415 vfio_release_device(const char *sysfs_base, const char *dev_addr,
418 struct vfio_group_status group_status = {
419 .argsz = sizeof(group_status)
425 /* get group number */
426 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
428 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
430 /* This is an error at this point. */
434 /* get the actual group fd */
435 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
436 if (vfio_group_fd <= 0) {
437 RTE_LOG(INFO, EAL, "vfio_get_group_fd failed for %s\n",
442 /* At this point we got an active group. Closing it will make the
443 * container detachment. If this is the last active group, VFIO kernel
444 * code will unset the container and the IOMMU mappings.
447 /* Closing a device */
448 if (close(vfio_dev_fd) < 0) {
449 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
454 /* An VFIO group can have several devices attached. Just when there is
455 * no devices remaining should the group be closed.
457 vfio_group_device_put(vfio_group_fd);
458 if (!vfio_group_device_count(vfio_group_fd)) {
460 if (close(vfio_group_fd) < 0) {
461 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
466 if (clear_group(vfio_group_fd) < 0) {
467 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
477 vfio_enable(const char *modname)
479 /* initialize group list */
483 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
484 vfio_cfg.vfio_groups[i].fd = -1;
485 vfio_cfg.vfio_groups[i].group_no = -1;
486 vfio_cfg.vfio_groups[i].devices = 0;
489 /* inform the user that we are probing for VFIO */
490 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
492 /* check if vfio-pci module is loaded */
493 vfio_available = rte_eal_check_module(modname);
495 /* return error directly */
496 if (vfio_available == -1) {
497 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
501 /* return 0 if VFIO modules not loaded */
502 if (vfio_available == 0) {
503 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
504 "skipping VFIO support...\n");
508 vfio_cfg.vfio_container_fd = vfio_get_container_fd();
510 /* check if we have VFIO driver enabled */
511 if (vfio_cfg.vfio_container_fd != -1) {
512 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
513 vfio_cfg.vfio_enabled = 1;
515 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
522 vfio_is_enabled(const char *modname)
524 const int mod_available = rte_eal_check_module(modname);
525 return vfio_cfg.vfio_enabled && mod_available;
528 const struct vfio_iommu_type *
529 vfio_set_iommu_type(int vfio_container_fd)
532 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
533 const struct vfio_iommu_type *t = &iommu_types[idx];
535 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
538 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
539 t->type_id, t->name);
542 /* not an error, there may be more supported IOMMU types */
543 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
544 "error %i (%s)\n", t->type_id, t->name, errno,
547 /* if we didn't find a suitable IOMMU type, fail */
552 vfio_has_supported_extensions(int vfio_container_fd)
555 unsigned idx, n_extensions = 0;
556 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
557 const struct vfio_iommu_type *t = &iommu_types[idx];
559 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
562 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
563 "error %i (%s)\n", errno,
565 close(vfio_container_fd);
567 } else if (ret == 1) {
568 /* we found a supported extension */
571 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
573 ret ? "supported" : "not supported");
576 /* if we didn't find any supported IOMMU types, fail */
578 close(vfio_container_fd);
586 vfio_get_container_fd(void)
588 int ret, vfio_container_fd;
590 /* if we're in a primary process, try to open the container */
591 if (internal_config.process_type == RTE_PROC_PRIMARY) {
592 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
593 if (vfio_container_fd < 0) {
594 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
595 "error %i (%s)\n", errno, strerror(errno));
599 /* check VFIO API version */
600 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
601 if (ret != VFIO_API_VERSION) {
603 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
604 "error %i (%s)\n", errno, strerror(errno));
606 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
607 close(vfio_container_fd);
611 ret = vfio_has_supported_extensions(vfio_container_fd);
613 RTE_LOG(ERR, EAL, " no supported IOMMU "
614 "extensions found!\n");
618 return vfio_container_fd;
621 * if we're in a secondary process, request container fd from the
622 * primary process via our socket
626 socket_fd = vfio_mp_sync_connect_to_primary();
628 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
631 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
632 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
636 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
637 if (vfio_container_fd < 0) {
638 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
643 return vfio_container_fd;
650 vfio_get_group_no(const char *sysfs_base,
651 const char *dev_addr, int *iommu_group_no)
653 char linkname[PATH_MAX];
654 char filename[PATH_MAX];
655 char *tok[16], *group_tok, *end;
658 memset(linkname, 0, sizeof(linkname));
659 memset(filename, 0, sizeof(filename));
661 /* try to find out IOMMU group for this device */
662 snprintf(linkname, sizeof(linkname),
663 "%s/%s/iommu_group", sysfs_base, dev_addr);
665 ret = readlink(linkname, filename, sizeof(filename));
667 /* if the link doesn't exist, no VFIO for us */
671 ret = rte_strsplit(filename, sizeof(filename),
672 tok, RTE_DIM(tok), '/');
675 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
679 /* IOMMU group is always the last token */
681 group_tok = tok[ret - 1];
683 *iommu_group_no = strtol(group_tok, &end, 10);
684 if ((end != group_tok && *end != '\0') || errno != 0) {
685 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
693 vfio_type1_dma_map(int vfio_container_fd)
695 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
698 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
699 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
700 struct vfio_iommu_type1_dma_map dma_map;
702 if (ms[i].addr == NULL)
705 memset(&dma_map, 0, sizeof(dma_map));
706 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
707 dma_map.vaddr = ms[i].addr_64;
708 dma_map.size = ms[i].len;
709 dma_map.iova = ms[i].phys_addr;
710 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
712 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
715 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
716 "error %i (%s)\n", errno,
726 vfio_spapr_dma_map(int vfio_container_fd)
728 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
731 struct vfio_iommu_spapr_register_memory reg = {
732 .argsz = sizeof(reg),
735 struct vfio_iommu_spapr_tce_info info = {
736 .argsz = sizeof(info),
738 struct vfio_iommu_spapr_tce_create create = {
739 .argsz = sizeof(create),
741 struct vfio_iommu_spapr_tce_remove remove = {
742 .argsz = sizeof(remove),
745 /* query spapr iommu info */
746 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
748 RTE_LOG(ERR, EAL, " cannot get iommu info, "
749 "error %i (%s)\n", errno, strerror(errno));
753 /* remove default DMA of 32 bit window */
754 remove.start_addr = info.dma32_window_start;
755 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
757 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
758 "error %i (%s)\n", errno, strerror(errno));
762 /* calculate window size based on number of hugepages configured */
763 create.window_size = rte_eal_get_physmem_size();
764 create.page_shift = __builtin_ctzll(ms->hugepage_sz);
767 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
769 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
770 "error %i (%s)\n", errno, strerror(errno));
774 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
775 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
776 struct vfio_iommu_type1_dma_map dma_map;
778 if (ms[i].addr == NULL)
781 reg.vaddr = (uintptr_t) ms[i].addr;
782 reg.size = ms[i].len;
783 ret = ioctl(vfio_container_fd,
784 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®);
786 RTE_LOG(ERR, EAL, " cannot register vaddr for IOMMU, "
787 "error %i (%s)\n", errno, strerror(errno));
791 memset(&dma_map, 0, sizeof(dma_map));
792 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
793 dma_map.vaddr = ms[i].addr_64;
794 dma_map.size = ms[i].len;
795 dma_map.iova = ms[i].phys_addr;
796 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
797 VFIO_DMA_MAP_FLAG_WRITE;
799 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
802 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
803 "error %i (%s)\n", errno, strerror(errno));
813 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
815 /* No-IOMMU mode does not need DMA mapping */