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_active_groups--;
193 /* This is just for SECONDARY processes */
194 socket_fd = vfio_mp_sync_connect_to_primary();
197 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
201 if (vfio_mp_sync_send_request(socket_fd, SOCKET_CLR_GROUP) < 0) {
202 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
207 if (vfio_mp_sync_send_request(socket_fd, vfio_group_fd) < 0) {
208 RTE_LOG(ERR, EAL, " cannot send group fd!\n");
213 ret = vfio_mp_sync_receive_request(socket_fd);
216 RTE_LOG(ERR, EAL, " BAD VFIO group fd!\n");
223 RTE_LOG(ERR, EAL, " Socket error\n");
227 RTE_LOG(ERR, EAL, " UNKNOWN reply, %d\n", ret);
234 vfio_setup_device(const char *sysfs_base, const char *dev_addr,
235 int *vfio_dev_fd, struct vfio_device_info *device_info)
237 struct vfio_group_status group_status = {
238 .argsz = sizeof(group_status)
244 /* get group number */
245 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
247 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
252 /* if negative, something failed */
256 /* get the actual group fd */
257 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
258 if (vfio_group_fd < 0)
261 /* if group_fd == 0, that means the device isn't managed by VFIO */
262 if (vfio_group_fd == 0) {
263 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
269 * at this point, we know that this group is viable (meaning, all devices
270 * are either bound to VFIO or not bound to anything)
273 /* check if the group is viable */
274 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
276 RTE_LOG(ERR, EAL, " %s cannot get group status, "
277 "error %i (%s)\n", dev_addr, errno, strerror(errno));
278 close(vfio_group_fd);
279 clear_group(vfio_group_fd);
281 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
282 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", dev_addr);
283 close(vfio_group_fd);
284 clear_group(vfio_group_fd);
288 /* check if group does not have a container yet */
289 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
291 /* add group to a container */
292 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
293 &vfio_cfg.vfio_container_fd);
295 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
296 "error %i (%s)\n", dev_addr, errno, strerror(errno));
297 close(vfio_group_fd);
298 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 assigned to
307 * a container and only in primary process. Note this can happen several
308 * times with the hotplug functionality.
310 if (internal_config.process_type == RTE_PROC_PRIMARY &&
311 vfio_cfg.vfio_active_groups == 1) {
312 /* select an IOMMU type which we will be using */
313 const struct vfio_iommu_type *t =
314 vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
316 RTE_LOG(ERR, EAL, " %s failed to select IOMMU type\n", dev_addr);
317 close(vfio_group_fd);
318 clear_group(vfio_group_fd);
321 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
323 RTE_LOG(ERR, EAL, " %s DMA remapping failed, "
324 "error %i (%s)\n", dev_addr, errno, strerror(errno));
325 close(vfio_group_fd);
326 clear_group(vfio_group_fd);
331 /* get a file descriptor for the device */
332 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
333 if (*vfio_dev_fd < 0) {
334 /* if we cannot get a device fd, this implies a problem with
335 * the VFIO group or the container not having IOMMU configured.
338 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
340 close(vfio_group_fd);
341 clear_group(vfio_group_fd);
345 /* test and setup the device */
346 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
348 RTE_LOG(ERR, EAL, " %s cannot get device info, "
349 "error %i (%s)\n", dev_addr, errno,
352 close(vfio_group_fd);
353 clear_group(vfio_group_fd);
361 vfio_release_device(const char *sysfs_base, const char *dev_addr,
364 struct vfio_group_status group_status = {
365 .argsz = sizeof(group_status)
371 /* get group number */
372 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
374 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
376 /* This is an error at this point. */
380 /* get the actual group fd */
381 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
382 if (vfio_group_fd <= 0) {
383 RTE_LOG(INFO, EAL, "vfio_get_group_fd failed for %s\n",
388 /* At this point we got an active group. Closing it will make the
389 * container detachment. If this is the last active group, VFIO kernel
390 * code will unset the container and the IOMMU mappings.
393 if (close(vfio_group_fd) < 0)
394 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
397 if (close(vfio_dev_fd) < 0)
398 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
401 if (clear_group(vfio_group_fd) < 0)
402 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
409 vfio_enable(const char *modname)
411 /* initialize group list */
415 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
416 vfio_cfg.vfio_groups[i].fd = -1;
417 vfio_cfg.vfio_groups[i].group_no = -1;
420 /* inform the user that we are probing for VFIO */
421 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
423 /* check if vfio-pci module is loaded */
424 vfio_available = rte_eal_check_module(modname);
426 /* return error directly */
427 if (vfio_available == -1) {
428 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
432 /* return 0 if VFIO modules not loaded */
433 if (vfio_available == 0) {
434 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
435 "skipping VFIO support...\n");
439 vfio_cfg.vfio_container_fd = vfio_get_container_fd();
441 /* check if we have VFIO driver enabled */
442 if (vfio_cfg.vfio_container_fd != -1) {
443 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
444 vfio_cfg.vfio_enabled = 1;
446 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
453 vfio_is_enabled(const char *modname)
455 const int mod_available = rte_eal_check_module(modname);
456 return vfio_cfg.vfio_enabled && mod_available;
459 const struct vfio_iommu_type *
460 vfio_set_iommu_type(int vfio_container_fd)
463 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
464 const struct vfio_iommu_type *t = &iommu_types[idx];
466 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
469 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
470 t->type_id, t->name);
473 /* not an error, there may be more supported IOMMU types */
474 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
475 "error %i (%s)\n", t->type_id, t->name, errno,
478 /* if we didn't find a suitable IOMMU type, fail */
483 vfio_has_supported_extensions(int vfio_container_fd)
486 unsigned idx, n_extensions = 0;
487 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
488 const struct vfio_iommu_type *t = &iommu_types[idx];
490 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
493 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
494 "error %i (%s)\n", errno,
496 close(vfio_container_fd);
498 } else if (ret == 1) {
499 /* we found a supported extension */
502 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
504 ret ? "supported" : "not supported");
507 /* if we didn't find any supported IOMMU types, fail */
509 close(vfio_container_fd);
517 vfio_get_container_fd(void)
519 int ret, vfio_container_fd;
521 /* if we're in a primary process, try to open the container */
522 if (internal_config.process_type == RTE_PROC_PRIMARY) {
523 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
524 if (vfio_container_fd < 0) {
525 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
526 "error %i (%s)\n", errno, strerror(errno));
530 /* check VFIO API version */
531 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
532 if (ret != VFIO_API_VERSION) {
534 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
535 "error %i (%s)\n", errno, strerror(errno));
537 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
538 close(vfio_container_fd);
542 ret = vfio_has_supported_extensions(vfio_container_fd);
544 RTE_LOG(ERR, EAL, " no supported IOMMU "
545 "extensions found!\n");
549 return vfio_container_fd;
552 * if we're in a secondary process, request container fd from the
553 * primary process via our socket
557 socket_fd = vfio_mp_sync_connect_to_primary();
559 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
562 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
563 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
567 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
568 if (vfio_container_fd < 0) {
569 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
574 return vfio_container_fd;
581 vfio_get_group_no(const char *sysfs_base,
582 const char *dev_addr, int *iommu_group_no)
584 char linkname[PATH_MAX];
585 char filename[PATH_MAX];
586 char *tok[16], *group_tok, *end;
589 memset(linkname, 0, sizeof(linkname));
590 memset(filename, 0, sizeof(filename));
592 /* try to find out IOMMU group for this device */
593 snprintf(linkname, sizeof(linkname),
594 "%s/%s/iommu_group", sysfs_base, dev_addr);
596 ret = readlink(linkname, filename, sizeof(filename));
598 /* if the link doesn't exist, no VFIO for us */
602 ret = rte_strsplit(filename, sizeof(filename),
603 tok, RTE_DIM(tok), '/');
606 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
610 /* IOMMU group is always the last token */
612 group_tok = tok[ret - 1];
614 *iommu_group_no = strtol(group_tok, &end, 10);
615 if ((end != group_tok && *end != '\0') || errno != 0) {
616 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
624 vfio_type1_dma_map(int vfio_container_fd)
626 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
629 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
630 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
631 struct vfio_iommu_type1_dma_map dma_map;
633 if (ms[i].addr == NULL)
636 memset(&dma_map, 0, sizeof(dma_map));
637 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
638 dma_map.vaddr = ms[i].addr_64;
639 dma_map.size = ms[i].len;
640 dma_map.iova = ms[i].phys_addr;
641 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
643 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
646 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
647 "error %i (%s)\n", errno,
657 vfio_spapr_dma_map(int vfio_container_fd)
659 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
662 struct vfio_iommu_spapr_register_memory reg = {
663 .argsz = sizeof(reg),
666 struct vfio_iommu_spapr_tce_info info = {
667 .argsz = sizeof(info),
669 struct vfio_iommu_spapr_tce_create create = {
670 .argsz = sizeof(create),
672 struct vfio_iommu_spapr_tce_remove remove = {
673 .argsz = sizeof(remove),
676 /* query spapr iommu info */
677 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
679 RTE_LOG(ERR, EAL, " cannot get iommu info, "
680 "error %i (%s)\n", errno, strerror(errno));
684 /* remove default DMA of 32 bit window */
685 remove.start_addr = info.dma32_window_start;
686 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
688 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
689 "error %i (%s)\n", errno, strerror(errno));
693 /* calculate window size based on number of hugepages configured */
694 create.window_size = rte_eal_get_physmem_size();
695 create.page_shift = __builtin_ctzll(ms->hugepage_sz);
698 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
700 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
701 "error %i (%s)\n", errno, strerror(errno));
705 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
706 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
707 struct vfio_iommu_type1_dma_map dma_map;
709 if (ms[i].addr == NULL)
712 reg.vaddr = (uintptr_t) ms[i].addr;
713 reg.size = ms[i].len;
714 ret = ioctl(vfio_container_fd,
715 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®);
717 RTE_LOG(ERR, EAL, " cannot register vaddr for IOMMU, "
718 "error %i (%s)\n", errno, strerror(errno));
722 memset(&dma_map, 0, sizeof(dma_map));
723 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
724 dma_map.vaddr = ms[i].addr_64;
725 dma_map.size = ms[i].len;
726 dma_map.iova = ms[i].phys_addr;
727 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
728 VFIO_DMA_MAP_FLAG_WRITE;
730 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
733 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
734 "error %i (%s)\n", errno, strerror(errno));
744 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
746 /* No-IOMMU mode does not need DMA mapping */