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);
303 * pick an IOMMU type and set up DMA mappings for container
305 * needs to be done only once, only when first group is
306 * assigned to a container and only in primary process.
307 * Note this can happen several times with the hotplug
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);
317 " %s failed to select IOMMU type\n",
319 close(vfio_group_fd);
320 clear_group(vfio_group_fd);
323 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
326 " %s DMA remapping failed, error %i (%s)\n",
327 dev_addr, errno, strerror(errno));
328 close(vfio_group_fd);
329 clear_group(vfio_group_fd);
335 /* get a file descriptor for the device */
336 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
337 if (*vfio_dev_fd < 0) {
338 /* if we cannot get a device fd, this implies a problem with
339 * the VFIO group or the container not having IOMMU configured.
342 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
344 close(vfio_group_fd);
345 clear_group(vfio_group_fd);
349 /* test and setup the device */
350 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
352 RTE_LOG(ERR, EAL, " %s cannot get device info, "
353 "error %i (%s)\n", dev_addr, errno,
356 close(vfio_group_fd);
357 clear_group(vfio_group_fd);
365 vfio_release_device(const char *sysfs_base, const char *dev_addr,
368 struct vfio_group_status group_status = {
369 .argsz = sizeof(group_status)
375 /* get group number */
376 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
378 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
380 /* This is an error at this point. */
384 /* get the actual group fd */
385 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
386 if (vfio_group_fd <= 0) {
387 RTE_LOG(INFO, EAL, "vfio_get_group_fd failed for %s\n",
392 /* At this point we got an active group. Closing it will make the
393 * container detachment. If this is the last active group, VFIO kernel
394 * code will unset the container and the IOMMU mappings.
397 if (close(vfio_group_fd) < 0)
398 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
401 if (close(vfio_dev_fd) < 0)
402 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
405 if (clear_group(vfio_group_fd) < 0)
406 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
413 vfio_enable(const char *modname)
415 /* initialize group list */
419 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
420 vfio_cfg.vfio_groups[i].fd = -1;
421 vfio_cfg.vfio_groups[i].group_no = -1;
424 /* inform the user that we are probing for VFIO */
425 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
427 /* check if vfio-pci module is loaded */
428 vfio_available = rte_eal_check_module(modname);
430 /* return error directly */
431 if (vfio_available == -1) {
432 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
436 /* return 0 if VFIO modules not loaded */
437 if (vfio_available == 0) {
438 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
439 "skipping VFIO support...\n");
443 vfio_cfg.vfio_container_fd = vfio_get_container_fd();
445 /* check if we have VFIO driver enabled */
446 if (vfio_cfg.vfio_container_fd != -1) {
447 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
448 vfio_cfg.vfio_enabled = 1;
450 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
457 vfio_is_enabled(const char *modname)
459 const int mod_available = rte_eal_check_module(modname);
460 return vfio_cfg.vfio_enabled && mod_available;
463 const struct vfio_iommu_type *
464 vfio_set_iommu_type(int vfio_container_fd)
467 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
468 const struct vfio_iommu_type *t = &iommu_types[idx];
470 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
473 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
474 t->type_id, t->name);
477 /* not an error, there may be more supported IOMMU types */
478 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
479 "error %i (%s)\n", t->type_id, t->name, errno,
482 /* if we didn't find a suitable IOMMU type, fail */
487 vfio_has_supported_extensions(int vfio_container_fd)
490 unsigned idx, n_extensions = 0;
491 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
492 const struct vfio_iommu_type *t = &iommu_types[idx];
494 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
497 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
498 "error %i (%s)\n", errno,
500 close(vfio_container_fd);
502 } else if (ret == 1) {
503 /* we found a supported extension */
506 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
508 ret ? "supported" : "not supported");
511 /* if we didn't find any supported IOMMU types, fail */
513 close(vfio_container_fd);
521 vfio_get_container_fd(void)
523 int ret, vfio_container_fd;
525 /* if we're in a primary process, try to open the container */
526 if (internal_config.process_type == RTE_PROC_PRIMARY) {
527 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
528 if (vfio_container_fd < 0) {
529 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
530 "error %i (%s)\n", errno, strerror(errno));
534 /* check VFIO API version */
535 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
536 if (ret != VFIO_API_VERSION) {
538 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
539 "error %i (%s)\n", errno, strerror(errno));
541 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
542 close(vfio_container_fd);
546 ret = vfio_has_supported_extensions(vfio_container_fd);
548 RTE_LOG(ERR, EAL, " no supported IOMMU "
549 "extensions found!\n");
553 return vfio_container_fd;
556 * if we're in a secondary process, request container fd from the
557 * primary process via our socket
561 socket_fd = vfio_mp_sync_connect_to_primary();
563 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
566 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
567 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
571 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
572 if (vfio_container_fd < 0) {
573 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
578 return vfio_container_fd;
585 vfio_get_group_no(const char *sysfs_base,
586 const char *dev_addr, int *iommu_group_no)
588 char linkname[PATH_MAX];
589 char filename[PATH_MAX];
590 char *tok[16], *group_tok, *end;
593 memset(linkname, 0, sizeof(linkname));
594 memset(filename, 0, sizeof(filename));
596 /* try to find out IOMMU group for this device */
597 snprintf(linkname, sizeof(linkname),
598 "%s/%s/iommu_group", sysfs_base, dev_addr);
600 ret = readlink(linkname, filename, sizeof(filename));
602 /* if the link doesn't exist, no VFIO for us */
606 ret = rte_strsplit(filename, sizeof(filename),
607 tok, RTE_DIM(tok), '/');
610 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
614 /* IOMMU group is always the last token */
616 group_tok = tok[ret - 1];
618 *iommu_group_no = strtol(group_tok, &end, 10);
619 if ((end != group_tok && *end != '\0') || errno != 0) {
620 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
628 vfio_type1_dma_map(int vfio_container_fd)
630 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
633 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
634 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
635 struct vfio_iommu_type1_dma_map dma_map;
637 if (ms[i].addr == NULL)
640 memset(&dma_map, 0, sizeof(dma_map));
641 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
642 dma_map.vaddr = ms[i].addr_64;
643 dma_map.size = ms[i].len;
644 dma_map.iova = ms[i].phys_addr;
645 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
647 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
650 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
651 "error %i (%s)\n", errno,
661 vfio_spapr_dma_map(int vfio_container_fd)
663 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
666 struct vfio_iommu_spapr_register_memory reg = {
667 .argsz = sizeof(reg),
670 struct vfio_iommu_spapr_tce_info info = {
671 .argsz = sizeof(info),
673 struct vfio_iommu_spapr_tce_create create = {
674 .argsz = sizeof(create),
676 struct vfio_iommu_spapr_tce_remove remove = {
677 .argsz = sizeof(remove),
680 /* query spapr iommu info */
681 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
683 RTE_LOG(ERR, EAL, " cannot get iommu info, "
684 "error %i (%s)\n", errno, strerror(errno));
688 /* remove default DMA of 32 bit window */
689 remove.start_addr = info.dma32_window_start;
690 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
692 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
693 "error %i (%s)\n", errno, strerror(errno));
697 /* calculate window size based on number of hugepages configured */
698 create.window_size = rte_eal_get_physmem_size();
699 create.page_shift = __builtin_ctzll(ms->hugepage_sz);
702 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
704 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
705 "error %i (%s)\n", errno, strerror(errno));
709 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
710 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
711 struct vfio_iommu_type1_dma_map dma_map;
713 if (ms[i].addr == NULL)
716 reg.vaddr = (uintptr_t) ms[i].addr;
717 reg.size = ms[i].len;
718 ret = ioctl(vfio_container_fd,
719 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®);
721 RTE_LOG(ERR, EAL, " cannot register vaddr for IOMMU, "
722 "error %i (%s)\n", errno, strerror(errno));
726 memset(&dma_map, 0, sizeof(dma_map));
727 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
728 dma_map.vaddr = ms[i].addr_64;
729 dma_map.size = ms[i].len;
730 dma_map.iova = ms[i].phys_addr;
731 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
732 VFIO_DMA_MAP_FLAG_WRITE;
734 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
737 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
738 "error %i (%s)\n", errno, strerror(errno));
748 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
750 /* No-IOMMU mode does not need DMA mapping */