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");
176 clear_group(int vfio_group_fd)
181 if (internal_config.process_type == RTE_PROC_PRIMARY) {
183 for (i = 0; i < VFIO_MAX_GROUPS; i++)
184 if (vfio_cfg.vfio_groups[i].fd == vfio_group_fd) {
185 vfio_cfg.vfio_groups[i].group_no = -1;
186 vfio_cfg.vfio_groups[i].fd = -1;
187 vfio_cfg.vfio_groups[i].devices = 0;
188 vfio_cfg.vfio_active_groups--;
194 /* This is just for SECONDARY processes */
195 socket_fd = vfio_mp_sync_connect_to_primary();
198 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
202 if (vfio_mp_sync_send_request(socket_fd, SOCKET_CLR_GROUP) < 0) {
203 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
208 if (vfio_mp_sync_send_request(socket_fd, vfio_group_fd) < 0) {
209 RTE_LOG(ERR, EAL, " cannot send group fd!\n");
214 ret = vfio_mp_sync_receive_request(socket_fd);
217 RTE_LOG(ERR, EAL, " BAD VFIO group fd!\n");
224 RTE_LOG(ERR, EAL, " Socket error\n");
228 RTE_LOG(ERR, EAL, " UNKNOWN reply, %d\n", ret);
235 vfio_setup_device(const char *sysfs_base, const char *dev_addr,
236 int *vfio_dev_fd, struct vfio_device_info *device_info)
238 struct vfio_group_status group_status = {
239 .argsz = sizeof(group_status)
245 /* get group number */
246 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
248 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
253 /* if negative, something failed */
257 /* get the actual group fd */
258 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
259 if (vfio_group_fd < 0)
262 /* if group_fd == 0, that means the device isn't managed by VFIO */
263 if (vfio_group_fd == 0) {
264 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
270 * at this point, we know that this group is viable (meaning, all devices
271 * are either bound to VFIO or not bound to anything)
274 /* check if the group is viable */
275 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
277 RTE_LOG(ERR, EAL, " %s cannot get group status, "
278 "error %i (%s)\n", dev_addr, errno, strerror(errno));
279 close(vfio_group_fd);
280 clear_group(vfio_group_fd);
282 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
283 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", dev_addr);
284 close(vfio_group_fd);
285 clear_group(vfio_group_fd);
289 /* check if group does not have a container yet */
290 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
292 /* add group to a container */
293 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
294 &vfio_cfg.vfio_container_fd);
296 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
297 "error %i (%s)\n", dev_addr, errno, strerror(errno));
298 close(vfio_group_fd);
299 clear_group(vfio_group_fd);
304 * pick an IOMMU type and set up DMA mappings for container
306 * needs to be done only once, only when first group is
307 * assigned to a container and only in primary process.
308 * Note this can happen several times with the hotplug
311 if (internal_config.process_type == RTE_PROC_PRIMARY &&
312 vfio_cfg.vfio_active_groups == 1) {
313 /* select an IOMMU type which we will be using */
314 const struct vfio_iommu_type *t =
315 vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
318 " %s failed to select IOMMU type\n",
320 close(vfio_group_fd);
321 clear_group(vfio_group_fd);
324 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
327 " %s DMA remapping failed, error %i (%s)\n",
328 dev_addr, errno, strerror(errno));
329 close(vfio_group_fd);
330 clear_group(vfio_group_fd);
336 /* get a file descriptor for the device */
337 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
338 if (*vfio_dev_fd < 0) {
339 /* if we cannot get a device fd, this implies a problem with
340 * the VFIO group or the container not having IOMMU configured.
343 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
345 close(vfio_group_fd);
346 clear_group(vfio_group_fd);
350 /* test and setup the device */
351 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
353 RTE_LOG(ERR, EAL, " %s cannot get device info, "
354 "error %i (%s)\n", dev_addr, errno,
357 close(vfio_group_fd);
358 clear_group(vfio_group_fd);
361 vfio_cfg.vfio_groups[vfio_group_fd].devices++;
367 vfio_release_device(const char *sysfs_base, const char *dev_addr,
370 struct vfio_group_status group_status = {
371 .argsz = sizeof(group_status)
377 /* get group number */
378 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
380 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
382 /* This is an error at this point. */
386 /* get the actual group fd */
387 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
388 if (vfio_group_fd <= 0) {
389 RTE_LOG(INFO, EAL, "vfio_get_group_fd failed for %s\n",
394 /* At this point we got an active group. Closing it will make the
395 * container detachment. If this is the last active group, VFIO kernel
396 * code will unset the container and the IOMMU mappings.
399 /* Closing a device */
400 if (close(vfio_dev_fd) < 0) {
401 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
406 /* An VFIO group can have several devices attached. Just when there is
407 * no devices remaining should the group be closed.
409 if (--vfio_cfg.vfio_groups[vfio_group_fd].devices == 0) {
411 if (close(vfio_group_fd) < 0) {
412 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
417 if (clear_group(vfio_group_fd) < 0) {
418 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
428 vfio_enable(const char *modname)
430 /* initialize group list */
434 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
435 vfio_cfg.vfio_groups[i].fd = -1;
436 vfio_cfg.vfio_groups[i].group_no = -1;
437 vfio_cfg.vfio_groups[i].devices = 0;
440 /* inform the user that we are probing for VFIO */
441 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
443 /* check if vfio-pci module is loaded */
444 vfio_available = rte_eal_check_module(modname);
446 /* return error directly */
447 if (vfio_available == -1) {
448 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
452 /* return 0 if VFIO modules not loaded */
453 if (vfio_available == 0) {
454 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
455 "skipping VFIO support...\n");
459 vfio_cfg.vfio_container_fd = vfio_get_container_fd();
461 /* check if we have VFIO driver enabled */
462 if (vfio_cfg.vfio_container_fd != -1) {
463 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
464 vfio_cfg.vfio_enabled = 1;
466 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
473 vfio_is_enabled(const char *modname)
475 const int mod_available = rte_eal_check_module(modname);
476 return vfio_cfg.vfio_enabled && mod_available;
479 const struct vfio_iommu_type *
480 vfio_set_iommu_type(int vfio_container_fd)
483 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
484 const struct vfio_iommu_type *t = &iommu_types[idx];
486 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
489 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
490 t->type_id, t->name);
493 /* not an error, there may be more supported IOMMU types */
494 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
495 "error %i (%s)\n", t->type_id, t->name, errno,
498 /* if we didn't find a suitable IOMMU type, fail */
503 vfio_has_supported_extensions(int vfio_container_fd)
506 unsigned idx, n_extensions = 0;
507 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
508 const struct vfio_iommu_type *t = &iommu_types[idx];
510 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
513 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
514 "error %i (%s)\n", errno,
516 close(vfio_container_fd);
518 } else if (ret == 1) {
519 /* we found a supported extension */
522 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
524 ret ? "supported" : "not supported");
527 /* if we didn't find any supported IOMMU types, fail */
529 close(vfio_container_fd);
537 vfio_get_container_fd(void)
539 int ret, vfio_container_fd;
541 /* if we're in a primary process, try to open the container */
542 if (internal_config.process_type == RTE_PROC_PRIMARY) {
543 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
544 if (vfio_container_fd < 0) {
545 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
546 "error %i (%s)\n", errno, strerror(errno));
550 /* check VFIO API version */
551 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
552 if (ret != VFIO_API_VERSION) {
554 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
555 "error %i (%s)\n", errno, strerror(errno));
557 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
558 close(vfio_container_fd);
562 ret = vfio_has_supported_extensions(vfio_container_fd);
564 RTE_LOG(ERR, EAL, " no supported IOMMU "
565 "extensions found!\n");
569 return vfio_container_fd;
572 * if we're in a secondary process, request container fd from the
573 * primary process via our socket
577 socket_fd = vfio_mp_sync_connect_to_primary();
579 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
582 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
583 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
587 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
588 if (vfio_container_fd < 0) {
589 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
594 return vfio_container_fd;
601 vfio_get_group_no(const char *sysfs_base,
602 const char *dev_addr, int *iommu_group_no)
604 char linkname[PATH_MAX];
605 char filename[PATH_MAX];
606 char *tok[16], *group_tok, *end;
609 memset(linkname, 0, sizeof(linkname));
610 memset(filename, 0, sizeof(filename));
612 /* try to find out IOMMU group for this device */
613 snprintf(linkname, sizeof(linkname),
614 "%s/%s/iommu_group", sysfs_base, dev_addr);
616 ret = readlink(linkname, filename, sizeof(filename));
618 /* if the link doesn't exist, no VFIO for us */
622 ret = rte_strsplit(filename, sizeof(filename),
623 tok, RTE_DIM(tok), '/');
626 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
630 /* IOMMU group is always the last token */
632 group_tok = tok[ret - 1];
634 *iommu_group_no = strtol(group_tok, &end, 10);
635 if ((end != group_tok && *end != '\0') || errno != 0) {
636 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
644 vfio_type1_dma_map(int vfio_container_fd)
646 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
649 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
650 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
651 struct vfio_iommu_type1_dma_map dma_map;
653 if (ms[i].addr == NULL)
656 memset(&dma_map, 0, sizeof(dma_map));
657 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
658 dma_map.vaddr = ms[i].addr_64;
659 dma_map.size = ms[i].len;
660 dma_map.iova = ms[i].phys_addr;
661 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
663 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
666 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
667 "error %i (%s)\n", errno,
677 vfio_spapr_dma_map(int vfio_container_fd)
679 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
682 struct vfio_iommu_spapr_register_memory reg = {
683 .argsz = sizeof(reg),
686 struct vfio_iommu_spapr_tce_info info = {
687 .argsz = sizeof(info),
689 struct vfio_iommu_spapr_tce_create create = {
690 .argsz = sizeof(create),
692 struct vfio_iommu_spapr_tce_remove remove = {
693 .argsz = sizeof(remove),
696 /* query spapr iommu info */
697 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
699 RTE_LOG(ERR, EAL, " cannot get iommu info, "
700 "error %i (%s)\n", errno, strerror(errno));
704 /* remove default DMA of 32 bit window */
705 remove.start_addr = info.dma32_window_start;
706 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
708 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
709 "error %i (%s)\n", errno, strerror(errno));
713 /* calculate window size based on number of hugepages configured */
714 create.window_size = rte_eal_get_physmem_size();
715 create.page_shift = __builtin_ctzll(ms->hugepage_sz);
718 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
720 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
721 "error %i (%s)\n", errno, strerror(errno));
725 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
726 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
727 struct vfio_iommu_type1_dma_map dma_map;
729 if (ms[i].addr == NULL)
732 reg.vaddr = (uintptr_t) ms[i].addr;
733 reg.size = ms[i].len;
734 ret = ioctl(vfio_container_fd,
735 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®);
737 RTE_LOG(ERR, EAL, " cannot register vaddr for IOMMU, "
738 "error %i (%s)\n", errno, strerror(errno));
742 memset(&dma_map, 0, sizeof(dma_map));
743 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
744 dma_map.vaddr = ms[i].addr_64;
745 dma_map.size = ms[i].len;
746 dma_map.iova = ms[i].phys_addr;
747 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
748 VFIO_DMA_MAP_FLAG_WRITE;
750 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
753 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
754 "error %i (%s)\n", errno, strerror(errno));
764 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
766 /* No-IOMMU mode does not need DMA mapping */