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)
71 char filename[PATH_MAX];
73 /* check if we already have the group descriptor open */
74 for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
75 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
76 return vfio_cfg.vfio_groups[i].fd;
78 /* if primary, try to open the group */
79 if (internal_config.process_type == RTE_PROC_PRIMARY) {
80 /* try regular group format */
81 snprintf(filename, sizeof(filename),
82 VFIO_GROUP_FMT, iommu_group_no);
83 vfio_group_fd = open(filename, O_RDWR);
84 if (vfio_group_fd < 0) {
85 /* if file not found, it's not an error */
86 if (errno != ENOENT) {
87 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
92 /* special case: try no-IOMMU path as well */
93 snprintf(filename, sizeof(filename),
94 VFIO_NOIOMMU_GROUP_FMT, iommu_group_no);
95 vfio_group_fd = open(filename, O_RDWR);
96 if (vfio_group_fd < 0) {
97 if (errno != ENOENT) {
98 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
104 /* noiommu group found */
107 /* if the fd is valid, create a new group for it */
108 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
109 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
110 close(vfio_group_fd);
113 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
114 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
115 return vfio_group_fd;
117 /* if we're in a secondary process, request group fd from the primary
118 * process via our socket
123 socket_fd = vfio_mp_sync_connect_to_primary();
126 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
129 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
130 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
134 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
135 RTE_LOG(ERR, EAL, " cannot send group number!\n");
139 ret = vfio_mp_sync_receive_request(socket_fd);
145 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
146 /* if we got the fd, return it */
147 if (vfio_group_fd > 0) {
149 return vfio_group_fd;
151 /* fall-through on error */
153 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
162 clear_current_group(void)
164 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
165 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
168 int vfio_setup_device(const char *sysfs_base, const char *dev_addr,
169 int *vfio_dev_fd, struct vfio_device_info *device_info)
171 struct vfio_group_status group_status = {
172 .argsz = sizeof(group_status)
178 /* get group number */
179 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
181 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
186 /* if negative, something failed */
190 /* get the actual group fd */
191 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
192 if (vfio_group_fd < 0)
196 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
197 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
199 /* if group_fd == 0, that means the device isn't managed by VFIO */
200 if (vfio_group_fd == 0) {
201 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
203 /* we store 0 as group fd to distinguish between existing but
204 * unbound VFIO groups, and groups that don't exist at all.
206 vfio_cfg.vfio_group_idx++;
211 * at this point, we know that this group is viable (meaning, all devices
212 * are either bound to VFIO or not bound to anything)
215 /* check if the group is viable */
216 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
218 RTE_LOG(ERR, EAL, " %s cannot get group status, "
219 "error %i (%s)\n", dev_addr, errno, strerror(errno));
220 close(vfio_group_fd);
221 clear_current_group();
223 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
224 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", dev_addr);
225 close(vfio_group_fd);
226 clear_current_group();
230 /* check if group does not have a container yet */
231 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
233 /* add group to a container */
234 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
235 &vfio_cfg.vfio_container_fd);
237 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
238 "error %i (%s)\n", dev_addr, errno, strerror(errno));
239 close(vfio_group_fd);
240 clear_current_group();
244 * at this point we know that this group has been successfully
245 * initialized, so we increment vfio_group_idx to indicate that we can
248 vfio_cfg.vfio_group_idx++;
252 * pick an IOMMU type and set up DMA mappings for container
254 * needs to be done only once, only when at least one group is assigned to
255 * a container and only in primary process
257 if (internal_config.process_type == RTE_PROC_PRIMARY &&
258 vfio_cfg.vfio_container_has_dma == 0) {
259 /* select an IOMMU type which we will be using */
260 const struct vfio_iommu_type *t =
261 vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
263 RTE_LOG(ERR, EAL, " %s failed to select IOMMU type\n", dev_addr);
266 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
268 RTE_LOG(ERR, EAL, " %s DMA remapping failed, "
269 "error %i (%s)\n", dev_addr, errno, strerror(errno));
272 vfio_cfg.vfio_container_has_dma = 1;
275 /* get a file descriptor for the device */
276 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
277 if (*vfio_dev_fd < 0) {
278 /* if we cannot get a device fd, this simply means that this
279 * particular port is not bound to VFIO
281 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
286 /* test and setup the device */
287 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
289 RTE_LOG(ERR, EAL, " %s cannot get device info, "
290 "error %i (%s)\n", dev_addr, errno, strerror(errno));
299 vfio_enable(const char *modname)
301 /* initialize group list */
305 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
306 vfio_cfg.vfio_groups[i].fd = -1;
307 vfio_cfg.vfio_groups[i].group_no = -1;
310 /* inform the user that we are probing for VFIO */
311 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
313 /* check if vfio-pci module is loaded */
314 vfio_available = rte_eal_check_module(modname);
316 /* return error directly */
317 if (vfio_available == -1) {
318 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
322 /* return 0 if VFIO modules not loaded */
323 if (vfio_available == 0) {
324 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
325 "skipping VFIO support...\n");
329 vfio_cfg.vfio_container_fd = vfio_get_container_fd();
331 /* check if we have VFIO driver enabled */
332 if (vfio_cfg.vfio_container_fd != -1) {
333 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
334 vfio_cfg.vfio_enabled = 1;
336 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
343 vfio_is_enabled(const char *modname)
345 const int mod_available = rte_eal_check_module(modname);
346 return vfio_cfg.vfio_enabled && mod_available;
349 const struct vfio_iommu_type *
350 vfio_set_iommu_type(int vfio_container_fd)
353 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
354 const struct vfio_iommu_type *t = &iommu_types[idx];
356 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
359 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
360 t->type_id, t->name);
363 /* not an error, there may be more supported IOMMU types */
364 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
365 "error %i (%s)\n", t->type_id, t->name, errno,
368 /* if we didn't find a suitable IOMMU type, fail */
373 vfio_has_supported_extensions(int vfio_container_fd)
376 unsigned idx, n_extensions = 0;
377 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
378 const struct vfio_iommu_type *t = &iommu_types[idx];
380 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
383 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
384 "error %i (%s)\n", errno,
386 close(vfio_container_fd);
388 } else if (ret == 1) {
389 /* we found a supported extension */
392 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
394 ret ? "supported" : "not supported");
397 /* if we didn't find any supported IOMMU types, fail */
399 close(vfio_container_fd);
407 vfio_get_container_fd(void)
409 int ret, vfio_container_fd;
411 /* if we're in a primary process, try to open the container */
412 if (internal_config.process_type == RTE_PROC_PRIMARY) {
413 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
414 if (vfio_container_fd < 0) {
415 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
416 "error %i (%s)\n", errno, strerror(errno));
420 /* check VFIO API version */
421 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
422 if (ret != VFIO_API_VERSION) {
424 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
425 "error %i (%s)\n", errno, strerror(errno));
427 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
428 close(vfio_container_fd);
432 ret = vfio_has_supported_extensions(vfio_container_fd);
434 RTE_LOG(ERR, EAL, " no supported IOMMU "
435 "extensions found!\n");
439 return vfio_container_fd;
442 * if we're in a secondary process, request container fd from the
443 * primary process via our socket
447 socket_fd = vfio_mp_sync_connect_to_primary();
449 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
452 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
453 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
457 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
458 if (vfio_container_fd < 0) {
459 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
464 return vfio_container_fd;
471 vfio_get_group_no(const char *sysfs_base,
472 const char *dev_addr, int *iommu_group_no)
474 char linkname[PATH_MAX];
475 char filename[PATH_MAX];
476 char *tok[16], *group_tok, *end;
479 memset(linkname, 0, sizeof(linkname));
480 memset(filename, 0, sizeof(filename));
482 /* try to find out IOMMU group for this device */
483 snprintf(linkname, sizeof(linkname),
484 "%s/%s/iommu_group", sysfs_base, dev_addr);
486 ret = readlink(linkname, filename, sizeof(filename));
488 /* if the link doesn't exist, no VFIO for us */
492 ret = rte_strsplit(filename, sizeof(filename),
493 tok, RTE_DIM(tok), '/');
496 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
500 /* IOMMU group is always the last token */
502 group_tok = tok[ret - 1];
504 *iommu_group_no = strtol(group_tok, &end, 10);
505 if ((end != group_tok && *end != '\0') || errno != 0) {
506 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
514 vfio_type1_dma_map(int vfio_container_fd)
516 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
519 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
520 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
521 struct vfio_iommu_type1_dma_map dma_map;
523 if (ms[i].addr == NULL)
526 memset(&dma_map, 0, sizeof(dma_map));
527 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
528 dma_map.vaddr = ms[i].addr_64;
529 dma_map.size = ms[i].len;
530 dma_map.iova = ms[i].phys_addr;
531 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
533 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
536 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
537 "error %i (%s)\n", errno, strerror(errno));
546 vfio_spapr_dma_map(int vfio_container_fd)
548 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
551 struct vfio_iommu_spapr_register_memory reg = {
552 .argsz = sizeof(reg),
555 struct vfio_iommu_spapr_tce_info info = {
556 .argsz = sizeof(info),
558 struct vfio_iommu_spapr_tce_create create = {
559 .argsz = sizeof(create),
561 struct vfio_iommu_spapr_tce_remove remove = {
562 .argsz = sizeof(remove),
565 /* query spapr iommu info */
566 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
568 RTE_LOG(ERR, EAL, " cannot get iommu info, "
569 "error %i (%s)\n", errno, strerror(errno));
573 /* remove default DMA of 32 bit window */
574 remove.start_addr = info.dma32_window_start;
575 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
577 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
578 "error %i (%s)\n", errno, strerror(errno));
582 /* calculate window size based on number of hugepages configured */
583 create.window_size = rte_eal_get_physmem_size();
584 create.page_shift = __builtin_ctzll(ms->hugepage_sz);
587 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
589 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
590 "error %i (%s)\n", errno, strerror(errno));
594 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
595 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
596 struct vfio_iommu_type1_dma_map dma_map;
598 if (ms[i].addr == NULL)
601 reg.vaddr = (uintptr_t) ms[i].addr;
602 reg.size = ms[i].len;
603 ret = ioctl(vfio_container_fd,
604 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®);
606 RTE_LOG(ERR, EAL, " cannot register vaddr for IOMMU, "
607 "error %i (%s)\n", errno, strerror(errno));
611 memset(&dma_map, 0, sizeof(dma_map));
612 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
613 dma_map.vaddr = ms[i].addr_64;
614 dma_map.size = ms[i].len;
615 dma_map.iova = ms[i].phys_addr;
616 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
617 VFIO_DMA_MAP_FLAG_WRITE;
619 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
622 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
623 "error %i (%s)\n", errno, strerror(errno));
633 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
635 /* No-IOMMU mode does not need DMA mapping */