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;
53 vfio_get_group_fd(int iommu_group_no)
57 char filename[PATH_MAX];
59 /* check if we already have the group descriptor open */
60 for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
61 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
62 return vfio_cfg.vfio_groups[i].fd;
64 /* if primary, try to open the group */
65 if (internal_config.process_type == RTE_PROC_PRIMARY) {
66 /* try regular group format */
67 snprintf(filename, sizeof(filename),
68 VFIO_GROUP_FMT, iommu_group_no);
69 vfio_group_fd = open(filename, O_RDWR);
70 if (vfio_group_fd < 0) {
71 /* if file not found, it's not an error */
72 if (errno != ENOENT) {
73 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
78 /* special case: try no-IOMMU path as well */
79 snprintf(filename, sizeof(filename),
80 VFIO_NOIOMMU_GROUP_FMT, iommu_group_no);
81 vfio_group_fd = open(filename, O_RDWR);
82 if (vfio_group_fd < 0) {
83 if (errno != ENOENT) {
84 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
90 /* noiommu group found */
93 /* if the fd is valid, create a new group for it */
94 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
95 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
99 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
100 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
101 return vfio_group_fd;
103 /* if we're in a secondary process, request group fd from the primary
104 * process via our socket
109 socket_fd = vfio_mp_sync_connect_to_primary();
112 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
115 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
116 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
120 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
121 RTE_LOG(ERR, EAL, " cannot send group number!\n");
125 ret = vfio_mp_sync_receive_request(socket_fd);
131 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
132 /* if we got the fd, return it */
133 if (vfio_group_fd > 0) {
135 return vfio_group_fd;
137 /* fall-through on error */
139 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
148 clear_current_group(void)
150 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
151 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
154 int vfio_setup_device(const char *sysfs_base, const char *dev_addr,
155 int *vfio_dev_fd, struct vfio_device_info *device_info)
157 struct vfio_group_status group_status = {
158 .argsz = sizeof(group_status)
164 /* get group number */
165 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
167 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
172 /* if negative, something failed */
176 /* get the actual group fd */
177 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
178 if (vfio_group_fd < 0)
182 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
183 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
185 /* if group_fd == 0, that means the device isn't managed by VFIO */
186 if (vfio_group_fd == 0) {
187 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
189 /* we store 0 as group fd to distinguish between existing but
190 * unbound VFIO groups, and groups that don't exist at all.
192 vfio_cfg.vfio_group_idx++;
197 * at this point, we know that this group is viable (meaning, all devices
198 * are either bound to VFIO or not bound to anything)
201 /* check if the group is viable */
202 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
204 RTE_LOG(ERR, EAL, " %s cannot get group status, "
205 "error %i (%s)\n", dev_addr, errno, strerror(errno));
206 close(vfio_group_fd);
207 clear_current_group();
209 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
210 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", dev_addr);
211 close(vfio_group_fd);
212 clear_current_group();
216 /* check if group does not have a container yet */
217 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
219 /* add group to a container */
220 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
221 &vfio_cfg.vfio_container_fd);
223 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
224 "error %i (%s)\n", dev_addr, errno, strerror(errno));
225 close(vfio_group_fd);
226 clear_current_group();
230 * at this point we know that this group has been successfully
231 * initialized, so we increment vfio_group_idx to indicate that we can
234 vfio_cfg.vfio_group_idx++;
238 * pick an IOMMU type and set up DMA mappings for container
240 * needs to be done only once, only when at least one group is assigned to
241 * a container and only in primary process
243 if (internal_config.process_type == RTE_PROC_PRIMARY &&
244 vfio_cfg.vfio_container_has_dma == 0) {
245 /* select an IOMMU type which we will be using */
246 const struct vfio_iommu_type *t =
247 vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
249 RTE_LOG(ERR, EAL, " %s failed to select IOMMU type\n", dev_addr);
252 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
254 RTE_LOG(ERR, EAL, " %s DMA remapping failed, "
255 "error %i (%s)\n", dev_addr, errno, strerror(errno));
258 vfio_cfg.vfio_container_has_dma = 1;
261 /* get a file descriptor for the device */
262 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
263 if (*vfio_dev_fd < 0) {
264 /* if we cannot get a device fd, this simply means that this
265 * particular port is not bound to VFIO
267 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
272 /* test and setup the device */
273 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
275 RTE_LOG(ERR, EAL, " %s cannot get device info, "
276 "error %i (%s)\n", dev_addr, errno, strerror(errno));
285 vfio_enable(const char *modname)
287 /* initialize group list */
291 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
292 vfio_cfg.vfio_groups[i].fd = -1;
293 vfio_cfg.vfio_groups[i].group_no = -1;
296 /* inform the user that we are probing for VFIO */
297 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
299 /* check if vfio-pci module is loaded */
300 vfio_available = rte_eal_check_module(modname);
302 /* return error directly */
303 if (vfio_available == -1) {
304 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
308 /* return 0 if VFIO modules not loaded */
309 if (vfio_available == 0) {
310 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
311 "skipping VFIO support...\n");
315 vfio_cfg.vfio_container_fd = vfio_get_container_fd();
317 /* check if we have VFIO driver enabled */
318 if (vfio_cfg.vfio_container_fd != -1) {
319 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
320 vfio_cfg.vfio_enabled = 1;
322 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
329 vfio_is_enabled(const char *modname)
331 const int mod_available = rte_eal_check_module(modname);
332 return vfio_cfg.vfio_enabled && mod_available;
335 const struct vfio_iommu_type *
336 vfio_set_iommu_type(int vfio_container_fd) {
338 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
339 const struct vfio_iommu_type *t = &iommu_types[idx];
341 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
344 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
345 t->type_id, t->name);
348 /* not an error, there may be more supported IOMMU types */
349 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
350 "error %i (%s)\n", t->type_id, t->name, errno,
353 /* if we didn't find a suitable IOMMU type, fail */
358 vfio_has_supported_extensions(int vfio_container_fd) {
360 unsigned idx, n_extensions = 0;
361 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
362 const struct vfio_iommu_type *t = &iommu_types[idx];
364 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
367 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
368 "error %i (%s)\n", errno,
370 close(vfio_container_fd);
372 } else if (ret == 1) {
373 /* we found a supported extension */
376 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
378 ret ? "supported" : "not supported");
381 /* if we didn't find any supported IOMMU types, fail */
383 close(vfio_container_fd);
391 vfio_get_container_fd(void)
393 int ret, vfio_container_fd;
395 /* if we're in a primary process, try to open the container */
396 if (internal_config.process_type == RTE_PROC_PRIMARY) {
397 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
398 if (vfio_container_fd < 0) {
399 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
400 "error %i (%s)\n", errno, strerror(errno));
404 /* check VFIO API version */
405 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
406 if (ret != VFIO_API_VERSION) {
408 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
409 "error %i (%s)\n", errno, strerror(errno));
411 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
412 close(vfio_container_fd);
416 ret = vfio_has_supported_extensions(vfio_container_fd);
418 RTE_LOG(ERR, EAL, " no supported IOMMU "
419 "extensions found!\n");
423 return vfio_container_fd;
426 * if we're in a secondary process, request container fd from the
427 * primary process via our socket
431 socket_fd = vfio_mp_sync_connect_to_primary();
433 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
436 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
437 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
441 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
442 if (vfio_container_fd < 0) {
443 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
448 return vfio_container_fd;
455 vfio_get_group_no(const char *sysfs_base,
456 const char *dev_addr, int *iommu_group_no)
458 char linkname[PATH_MAX];
459 char filename[PATH_MAX];
460 char *tok[16], *group_tok, *end;
463 memset(linkname, 0, sizeof(linkname));
464 memset(filename, 0, sizeof(filename));
466 /* try to find out IOMMU group for this device */
467 snprintf(linkname, sizeof(linkname),
468 "%s/%s/iommu_group", sysfs_base, dev_addr);
470 ret = readlink(linkname, filename, sizeof(filename));
472 /* if the link doesn't exist, no VFIO for us */
476 ret = rte_strsplit(filename, sizeof(filename),
477 tok, RTE_DIM(tok), '/');
480 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
484 /* IOMMU group is always the last token */
486 group_tok = tok[ret - 1];
488 *iommu_group_no = strtol(group_tok, &end, 10);
489 if ((end != group_tok && *end != '\0') || errno != 0) {
490 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
498 vfio_type1_dma_map(int vfio_container_fd)
500 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
503 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
504 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
505 struct vfio_iommu_type1_dma_map dma_map;
507 if (ms[i].addr == NULL)
510 memset(&dma_map, 0, sizeof(dma_map));
511 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
512 dma_map.vaddr = ms[i].addr_64;
513 dma_map.size = ms[i].len;
514 dma_map.iova = ms[i].phys_addr;
515 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
517 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
520 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
521 "error %i (%s)\n", errno, strerror(errno));
530 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
532 /* No-IOMMU mode does not need DMA mapping */