vfio: allow to map other memory regions
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_vfio.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <inttypes.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <sys/ioctl.h>
10
11 #include <rte_errno.h>
12 #include <rte_log.h>
13 #include <rte_memory.h>
14 #include <rte_eal_memconfig.h>
15 #include <rte_vfio.h>
16
17 #include "eal_filesystem.h"
18 #include "eal_vfio.h"
19 #include "eal_private.h"
20
21 #ifdef VFIO_PRESENT
22
23 /* per-process VFIO config */
24 static struct vfio_config vfio_cfg;
25
26 static int vfio_type1_dma_map(int);
27 static int vfio_type1_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
28 static int vfio_spapr_dma_map(int);
29 static int vfio_spapr_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
30 static int vfio_noiommu_dma_map(int);
31 static int vfio_noiommu_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
32 static int vfio_dma_mem_map(uint64_t vaddr, uint64_t iova, uint64_t len,
33                 int do_map);
34
35 /* IOMMU types we support */
36 static const struct vfio_iommu_type iommu_types[] = {
37         /* x86 IOMMU, otherwise known as type 1 */
38         {
39                 .type_id = RTE_VFIO_TYPE1,
40                 .name = "Type 1",
41                 .dma_map_func = &vfio_type1_dma_map,
42                 .dma_user_map_func = &vfio_type1_dma_mem_map
43         },
44         /* ppc64 IOMMU, otherwise known as spapr */
45         {
46                 .type_id = RTE_VFIO_SPAPR,
47                 .name = "sPAPR",
48                 .dma_map_func = &vfio_spapr_dma_map,
49                 .dma_user_map_func = &vfio_spapr_dma_mem_map
50         },
51         /* IOMMU-less mode */
52         {
53                 .type_id = RTE_VFIO_NOIOMMU,
54                 .name = "No-IOMMU",
55                 .dma_map_func = &vfio_noiommu_dma_map,
56                 .dma_user_map_func = &vfio_noiommu_dma_mem_map
57         },
58 };
59
60 /* hot plug/unplug of VFIO groups may cause all DMA maps to be dropped. we can
61  * recreate the mappings for DPDK segments, but we cannot do so for memory that
62  * was registered by the user themselves, so we need to store the user mappings
63  * somewhere, to recreate them later.
64  */
65 #define VFIO_MAX_USER_MEM_MAPS 256
66 struct user_mem_map {
67         uint64_t addr;
68         uint64_t iova;
69         uint64_t len;
70 };
71 static struct {
72         rte_spinlock_t lock;
73         int n_maps;
74         struct user_mem_map maps[VFIO_MAX_USER_MEM_MAPS];
75 } user_mem_maps = {
76         .lock = RTE_SPINLOCK_INITIALIZER
77 };
78
79 static int
80 is_null_map(const struct user_mem_map *map)
81 {
82         return map->addr == 0 && map->iova == 0 && map->len == 0;
83 }
84
85 /* we may need to merge user mem maps together in case of user mapping/unmapping
86  * chunks of memory, so we'll need a comparator function to sort segments.
87  */
88 static int
89 user_mem_map_cmp(const void *a, const void *b)
90 {
91         const struct user_mem_map *umm_a = a;
92         const struct user_mem_map *umm_b = b;
93
94         /* move null entries to end */
95         if (is_null_map(umm_a))
96                 return 1;
97         if (is_null_map(umm_b))
98                 return -1;
99
100         /* sort by iova first */
101         if (umm_a->iova < umm_b->iova)
102                 return -1;
103         if (umm_a->iova > umm_b->iova)
104                 return 1;
105
106         if (umm_a->addr < umm_b->addr)
107                 return -1;
108         if (umm_a->addr > umm_b->addr)
109                 return 1;
110
111         if (umm_a->len < umm_b->len)
112                 return -1;
113         if (umm_a->len > umm_b->len)
114                 return 1;
115
116         return 0;
117 }
118
119 /* adjust user map entry. this may result in shortening of existing map, or in
120  * splitting existing map in two pieces.
121  */
122 static void
123 adjust_map(struct user_mem_map *src, struct user_mem_map *end,
124                 uint64_t remove_va_start, uint64_t remove_len)
125 {
126         /* if va start is same as start address, we're simply moving start */
127         if (remove_va_start == src->addr) {
128                 src->addr += remove_len;
129                 src->iova += remove_len;
130                 src->len -= remove_len;
131         } else if (remove_va_start + remove_len == src->addr + src->len) {
132                 /* we're shrinking mapping from the end */
133                 src->len -= remove_len;
134         } else {
135                 /* we're blowing a hole in the middle */
136                 struct user_mem_map tmp;
137                 uint64_t total_len = src->len;
138
139                 /* adjust source segment length */
140                 src->len = remove_va_start - src->addr;
141
142                 /* create temporary segment in the middle */
143                 tmp.addr = src->addr + src->len;
144                 tmp.iova = src->iova + src->len;
145                 tmp.len = remove_len;
146
147                 /* populate end segment - this one we will be keeping */
148                 end->addr = tmp.addr + tmp.len;
149                 end->iova = tmp.iova + tmp.len;
150                 end->len = total_len - src->len - tmp.len;
151         }
152 }
153
154 /* try merging two maps into one, return 1 if succeeded */
155 static int
156 merge_map(struct user_mem_map *left, struct user_mem_map *right)
157 {
158         if (left->addr + left->len != right->addr)
159                 return 0;
160         if (left->iova + left->len != right->iova)
161                 return 0;
162
163         left->len += right->len;
164
165         memset(right, 0, sizeof(*right));
166
167         return 1;
168 }
169
170 static struct user_mem_map *
171 find_user_mem_map(uint64_t addr, uint64_t iova, uint64_t len)
172 {
173         uint64_t va_end = addr + len;
174         uint64_t iova_end = iova + len;
175         int i;
176
177         for (i = 0; i < user_mem_maps.n_maps; i++) {
178                 struct user_mem_map *map = &user_mem_maps.maps[i];
179                 uint64_t map_va_end = map->addr + map->len;
180                 uint64_t map_iova_end = map->iova + map->len;
181
182                 /* check start VA */
183                 if (addr < map->addr || addr >= map_va_end)
184                         continue;
185                 /* check if IOVA end is within boundaries */
186                 if (va_end <= map->addr || va_end >= map_va_end)
187                         continue;
188
189                 /* check start PA */
190                 if (iova < map->iova || iova >= map_iova_end)
191                         continue;
192                 /* check if IOVA end is within boundaries */
193                 if (iova_end <= map->iova || iova_end >= map_iova_end)
194                         continue;
195
196                 /* we've found our map */
197                 return map;
198         }
199         return NULL;
200 }
201
202 /* this will sort all user maps, and merge/compact any adjacent maps */
203 static void
204 compact_user_maps(void)
205 {
206         int i, n_merged, cur_idx;
207
208         qsort(user_mem_maps.maps, user_mem_maps.n_maps,
209                         sizeof(user_mem_maps.maps[0]), user_mem_map_cmp);
210
211         /* we'll go over the list backwards when merging */
212         n_merged = 0;
213         for (i = user_mem_maps.n_maps - 2; i >= 0; i--) {
214                 struct user_mem_map *l, *r;
215
216                 l = &user_mem_maps.maps[i];
217                 r = &user_mem_maps.maps[i + 1];
218
219                 if (is_null_map(l) || is_null_map(r))
220                         continue;
221
222                 if (merge_map(l, r))
223                         n_merged++;
224         }
225
226         /* the entries are still sorted, but now they have holes in them, so
227          * walk through the list and remove the holes
228          */
229         if (n_merged > 0) {
230                 cur_idx = 0;
231                 for (i = 0; i < user_mem_maps.n_maps; i++) {
232                         if (!is_null_map(&user_mem_maps.maps[i])) {
233                                 struct user_mem_map *src, *dst;
234
235                                 src = &user_mem_maps.maps[i];
236                                 dst = &user_mem_maps.maps[cur_idx++];
237
238                                 if (src != dst) {
239                                         memcpy(dst, src, sizeof(*src));
240                                         memset(src, 0, sizeof(*src));
241                                 }
242                         }
243                 }
244                 user_mem_maps.n_maps = cur_idx;
245         }
246 }
247
248 int
249 vfio_get_group_fd(int iommu_group_no)
250 {
251         int i;
252         int vfio_group_fd;
253         char filename[PATH_MAX];
254         struct vfio_group *cur_grp;
255
256         /* check if we already have the group descriptor open */
257         for (i = 0; i < VFIO_MAX_GROUPS; i++)
258                 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
259                         return vfio_cfg.vfio_groups[i].fd;
260
261         /* Lets see first if there is room for a new group */
262         if (vfio_cfg.vfio_active_groups == VFIO_MAX_GROUPS) {
263                 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
264                 return -1;
265         }
266
267         /* Now lets get an index for the new group */
268         for (i = 0; i < VFIO_MAX_GROUPS; i++)
269                 if (vfio_cfg.vfio_groups[i].group_no == -1) {
270                         cur_grp = &vfio_cfg.vfio_groups[i];
271                         break;
272                 }
273
274         /* This should not happen */
275         if (i == VFIO_MAX_GROUPS) {
276                 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
277                 return -1;
278         }
279         /* if primary, try to open the group */
280         if (internal_config.process_type == RTE_PROC_PRIMARY) {
281                 /* try regular group format */
282                 snprintf(filename, sizeof(filename),
283                                  VFIO_GROUP_FMT, iommu_group_no);
284                 vfio_group_fd = open(filename, O_RDWR);
285                 if (vfio_group_fd < 0) {
286                         /* if file not found, it's not an error */
287                         if (errno != ENOENT) {
288                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
289                                                 strerror(errno));
290                                 return -1;
291                         }
292
293                         /* special case: try no-IOMMU path as well */
294                         snprintf(filename, sizeof(filename),
295                                         VFIO_NOIOMMU_GROUP_FMT, iommu_group_no);
296                         vfio_group_fd = open(filename, O_RDWR);
297                         if (vfio_group_fd < 0) {
298                                 if (errno != ENOENT) {
299                                         RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
300                                                         strerror(errno));
301                                         return -1;
302                                 }
303                                 return 0;
304                         }
305                         /* noiommu group found */
306                 }
307
308                 cur_grp->group_no = iommu_group_no;
309                 cur_grp->fd = vfio_group_fd;
310                 vfio_cfg.vfio_active_groups++;
311                 return vfio_group_fd;
312         }
313         /* if we're in a secondary process, request group fd from the primary
314          * process via our socket
315          */
316         else {
317                 int socket_fd, ret;
318
319                 socket_fd = vfio_mp_sync_connect_to_primary();
320
321                 if (socket_fd < 0) {
322                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
323                         return -1;
324                 }
325                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
326                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
327                         close(socket_fd);
328                         return -1;
329                 }
330                 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
331                         RTE_LOG(ERR, EAL, "  cannot send group number!\n");
332                         close(socket_fd);
333                         return -1;
334                 }
335                 ret = vfio_mp_sync_receive_request(socket_fd);
336                 switch (ret) {
337                 case SOCKET_NO_FD:
338                         close(socket_fd);
339                         return 0;
340                 case SOCKET_OK:
341                         vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
342                         /* if we got the fd, store it and return it */
343                         if (vfio_group_fd > 0) {
344                                 close(socket_fd);
345                                 cur_grp->group_no = iommu_group_no;
346                                 cur_grp->fd = vfio_group_fd;
347                                 vfio_cfg.vfio_active_groups++;
348                                 return vfio_group_fd;
349                         }
350                         /* fall-through on error */
351                 default:
352                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
353                         close(socket_fd);
354                         return -1;
355                 }
356         }
357         return -1;
358 }
359
360
361 static int
362 get_vfio_group_idx(int vfio_group_fd)
363 {
364         int i;
365         for (i = 0; i < VFIO_MAX_GROUPS; i++)
366                 if (vfio_cfg.vfio_groups[i].fd == vfio_group_fd)
367                         return i;
368         return -1;
369 }
370
371 static void
372 vfio_group_device_get(int vfio_group_fd)
373 {
374         int i;
375
376         i = get_vfio_group_idx(vfio_group_fd);
377         if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
378                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
379         else
380                 vfio_cfg.vfio_groups[i].devices++;
381 }
382
383 static void
384 vfio_group_device_put(int vfio_group_fd)
385 {
386         int i;
387
388         i = get_vfio_group_idx(vfio_group_fd);
389         if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
390                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
391         else
392                 vfio_cfg.vfio_groups[i].devices--;
393 }
394
395 static int
396 vfio_group_device_count(int vfio_group_fd)
397 {
398         int i;
399
400         i = get_vfio_group_idx(vfio_group_fd);
401         if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
402                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
403                 return -1;
404         }
405
406         return vfio_cfg.vfio_groups[i].devices;
407 }
408
409 int
410 rte_vfio_clear_group(int vfio_group_fd)
411 {
412         int i;
413         int socket_fd, ret;
414
415         if (internal_config.process_type == RTE_PROC_PRIMARY) {
416
417                 i = get_vfio_group_idx(vfio_group_fd);
418                 if (i < 0)
419                         return -1;
420                 vfio_cfg.vfio_groups[i].group_no = -1;
421                 vfio_cfg.vfio_groups[i].fd = -1;
422                 vfio_cfg.vfio_groups[i].devices = 0;
423                 vfio_cfg.vfio_active_groups--;
424                 return 0;
425         }
426
427         /* This is just for SECONDARY processes */
428         socket_fd = vfio_mp_sync_connect_to_primary();
429
430         if (socket_fd < 0) {
431                 RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
432                 return -1;
433         }
434
435         if (vfio_mp_sync_send_request(socket_fd, SOCKET_CLR_GROUP) < 0) {
436                 RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
437                 close(socket_fd);
438                 return -1;
439         }
440
441         if (vfio_mp_sync_send_request(socket_fd, vfio_group_fd) < 0) {
442                 RTE_LOG(ERR, EAL, "  cannot send group fd!\n");
443                 close(socket_fd);
444                 return -1;
445         }
446
447         ret = vfio_mp_sync_receive_request(socket_fd);
448         switch (ret) {
449         case SOCKET_NO_FD:
450                 RTE_LOG(ERR, EAL, "  BAD VFIO group fd!\n");
451                 close(socket_fd);
452                 break;
453         case SOCKET_OK:
454                 close(socket_fd);
455                 return 0;
456         case SOCKET_ERR:
457                 RTE_LOG(ERR, EAL, "  Socket error\n");
458                 close(socket_fd);
459                 break;
460         default:
461                 RTE_LOG(ERR, EAL, "  UNKNOWN reply, %d\n", ret);
462                 close(socket_fd);
463         }
464         return -1;
465 }
466
467 int
468 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
469                 int *vfio_dev_fd, struct vfio_device_info *device_info)
470 {
471         struct vfio_group_status group_status = {
472                         .argsz = sizeof(group_status)
473         };
474         int vfio_group_fd;
475         int iommu_group_no;
476         int i, ret;
477
478         /* get group number */
479         ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
480         if (ret == 0) {
481                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
482                         dev_addr);
483                 return 1;
484         }
485
486         /* if negative, something failed */
487         if (ret < 0)
488                 return -1;
489
490         /* get the actual group fd */
491         vfio_group_fd = vfio_get_group_fd(iommu_group_no);
492         if (vfio_group_fd < 0)
493                 return -1;
494
495         /* if group_fd == 0, that means the device isn't managed by VFIO */
496         if (vfio_group_fd == 0) {
497                 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
498                                 dev_addr);
499                 return 1;
500         }
501
502         /*
503          * at this point, we know that this group is viable (meaning, all devices
504          * are either bound to VFIO or not bound to anything)
505          */
506
507         /* check if the group is viable */
508         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
509         if (ret) {
510                 RTE_LOG(ERR, EAL, "  %s cannot get group status, "
511                                 "error %i (%s)\n", dev_addr, errno, strerror(errno));
512                 close(vfio_group_fd);
513                 rte_vfio_clear_group(vfio_group_fd);
514                 return -1;
515         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
516                 RTE_LOG(ERR, EAL, "  %s VFIO group is not viable!\n", dev_addr);
517                 close(vfio_group_fd);
518                 rte_vfio_clear_group(vfio_group_fd);
519                 return -1;
520         }
521
522         /* check if group does not have a container yet */
523         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
524
525                 /* add group to a container */
526                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
527                                 &vfio_cfg.vfio_container_fd);
528                 if (ret) {
529                         RTE_LOG(ERR, EAL, "  %s cannot add VFIO group to container, "
530                                         "error %i (%s)\n", dev_addr, errno, strerror(errno));
531                         close(vfio_group_fd);
532                         rte_vfio_clear_group(vfio_group_fd);
533                         return -1;
534                 }
535
536                 /*
537                  * pick an IOMMU type and set up DMA mappings for container
538                  *
539                  * needs to be done only once, only when first group is
540                  * assigned to a container and only in primary process.
541                  * Note this can happen several times with the hotplug
542                  * functionality.
543                  */
544                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
545                                 vfio_cfg.vfio_active_groups == 1) {
546                         const struct vfio_iommu_type *t;
547
548                         /* select an IOMMU type which we will be using */
549                         t = vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
550                         if (!t) {
551                                 RTE_LOG(ERR, EAL,
552                                         "  %s failed to select IOMMU type\n",
553                                         dev_addr);
554                                 close(vfio_group_fd);
555                                 rte_vfio_clear_group(vfio_group_fd);
556                                 return -1;
557                         }
558                         ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
559                         if (ret) {
560                                 RTE_LOG(ERR, EAL,
561                                         "  %s DMA remapping failed, error %i (%s)\n",
562                                         dev_addr, errno, strerror(errno));
563                                 close(vfio_group_fd);
564                                 rte_vfio_clear_group(vfio_group_fd);
565                                 return -1;
566                         }
567
568                         vfio_cfg.vfio_iommu_type = t;
569
570                         /* re-map all user-mapped segments */
571                         rte_spinlock_lock(&user_mem_maps.lock);
572
573                         /* this IOMMU type may not support DMA mapping, but
574                          * if we have mappings in the list - that means we have
575                          * previously mapped something successfully, so we can
576                          * be sure that DMA mapping is supported.
577                          */
578                         for (i = 0; i < user_mem_maps.n_maps; i++) {
579                                 struct user_mem_map *map;
580                                 map = &user_mem_maps.maps[i];
581
582                                 ret = t->dma_user_map_func(
583                                                 vfio_cfg.vfio_container_fd,
584                                                 map->addr, map->iova, map->len,
585                                                 1);
586                                 if (ret) {
587                                         RTE_LOG(ERR, EAL, "Couldn't map user memory for DMA: "
588                                                         "va: 0x%" PRIx64 " "
589                                                         "iova: 0x%" PRIx64 " "
590                                                         "len: 0x%" PRIu64 "\n",
591                                                         map->addr, map->iova,
592                                                         map->len);
593                                         rte_spinlock_unlock(
594                                                         &user_mem_maps.lock);
595                                         return -1;
596                                 }
597                         }
598                         rte_spinlock_unlock(&user_mem_maps.lock);
599                 }
600         }
601
602         /* get a file descriptor for the device */
603         *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
604         if (*vfio_dev_fd < 0) {
605                 /* if we cannot get a device fd, this implies a problem with
606                  * the VFIO group or the container not having IOMMU configured.
607                  */
608
609                 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
610                                 dev_addr);
611                 close(vfio_group_fd);
612                 rte_vfio_clear_group(vfio_group_fd);
613                 return -1;
614         }
615
616         /* test and setup the device */
617         ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
618         if (ret) {
619                 RTE_LOG(ERR, EAL, "  %s cannot get device info, "
620                                 "error %i (%s)\n", dev_addr, errno,
621                                 strerror(errno));
622                 close(*vfio_dev_fd);
623                 close(vfio_group_fd);
624                 rte_vfio_clear_group(vfio_group_fd);
625                 return -1;
626         }
627         vfio_group_device_get(vfio_group_fd);
628
629         return 0;
630 }
631
632 int
633 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
634                     int vfio_dev_fd)
635 {
636         struct vfio_group_status group_status = {
637                         .argsz = sizeof(group_status)
638         };
639         int vfio_group_fd;
640         int iommu_group_no;
641         int ret;
642
643         /* get group number */
644         ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
645         if (ret <= 0) {
646                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver\n",
647                         dev_addr);
648                 /* This is an error at this point. */
649                 return -1;
650         }
651
652         /* get the actual group fd */
653         vfio_group_fd = vfio_get_group_fd(iommu_group_no);
654         if (vfio_group_fd <= 0) {
655                 RTE_LOG(INFO, EAL, "vfio_get_group_fd failed for %s\n",
656                                    dev_addr);
657                 return -1;
658         }
659
660         /* At this point we got an active group. Closing it will make the
661          * container detachment. If this is the last active group, VFIO kernel
662          * code will unset the container and the IOMMU mappings.
663          */
664
665         /* Closing a device */
666         if (close(vfio_dev_fd) < 0) {
667                 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
668                                    dev_addr);
669                 return -1;
670         }
671
672         /* An VFIO group can have several devices attached. Just when there is
673          * no devices remaining should the group be closed.
674          */
675         vfio_group_device_put(vfio_group_fd);
676         if (!vfio_group_device_count(vfio_group_fd)) {
677
678                 if (close(vfio_group_fd) < 0) {
679                         RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
680                                 dev_addr);
681                         return -1;
682                 }
683
684                 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
685                         RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
686                                            dev_addr);
687                         return -1;
688                 }
689         }
690
691         return 0;
692 }
693
694 int
695 rte_vfio_enable(const char *modname)
696 {
697         /* initialize group list */
698         int i;
699         int vfio_available;
700
701         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
702                 vfio_cfg.vfio_groups[i].fd = -1;
703                 vfio_cfg.vfio_groups[i].group_no = -1;
704                 vfio_cfg.vfio_groups[i].devices = 0;
705         }
706
707         /* inform the user that we are probing for VFIO */
708         RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
709
710         /* check if vfio module is loaded */
711         vfio_available = rte_eal_check_module(modname);
712
713         /* return error directly */
714         if (vfio_available == -1) {
715                 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
716                 return -1;
717         }
718
719         /* return 0 if VFIO modules not loaded */
720         if (vfio_available == 0) {
721                 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
722                         "skipping VFIO support...\n");
723                 return 0;
724         }
725
726         vfio_cfg.vfio_container_fd = vfio_get_container_fd();
727
728         /* check if we have VFIO driver enabled */
729         if (vfio_cfg.vfio_container_fd != -1) {
730                 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
731                 vfio_cfg.vfio_enabled = 1;
732         } else {
733                 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
734         }
735
736         return 0;
737 }
738
739 int
740 rte_vfio_is_enabled(const char *modname)
741 {
742         const int mod_available = rte_eal_check_module(modname) > 0;
743         return vfio_cfg.vfio_enabled && mod_available;
744 }
745
746 const struct vfio_iommu_type *
747 vfio_set_iommu_type(int vfio_container_fd)
748 {
749         unsigned idx;
750         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
751                 const struct vfio_iommu_type *t = &iommu_types[idx];
752
753                 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
754                                 t->type_id);
755                 if (!ret) {
756                         RTE_LOG(NOTICE, EAL, "  using IOMMU type %d (%s)\n",
757                                         t->type_id, t->name);
758                         return t;
759                 }
760                 /* not an error, there may be more supported IOMMU types */
761                 RTE_LOG(DEBUG, EAL, "  set IOMMU type %d (%s) failed, "
762                                 "error %i (%s)\n", t->type_id, t->name, errno,
763                                 strerror(errno));
764         }
765         /* if we didn't find a suitable IOMMU type, fail */
766         return NULL;
767 }
768
769 int
770 vfio_has_supported_extensions(int vfio_container_fd)
771 {
772         int ret;
773         unsigned idx, n_extensions = 0;
774         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
775                 const struct vfio_iommu_type *t = &iommu_types[idx];
776
777                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
778                                 t->type_id);
779                 if (ret < 0) {
780                         RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
781                                 "error %i (%s)\n", errno,
782                                 strerror(errno));
783                         close(vfio_container_fd);
784                         return -1;
785                 } else if (ret == 1) {
786                         /* we found a supported extension */
787                         n_extensions++;
788                 }
789                 RTE_LOG(DEBUG, EAL, "  IOMMU type %d (%s) is %s\n",
790                                 t->type_id, t->name,
791                                 ret ? "supported" : "not supported");
792         }
793
794         /* if we didn't find any supported IOMMU types, fail */
795         if (!n_extensions) {
796                 close(vfio_container_fd);
797                 return -1;
798         }
799
800         return 0;
801 }
802
803 int
804 vfio_get_container_fd(void)
805 {
806         int ret, vfio_container_fd;
807
808         /* if we're in a primary process, try to open the container */
809         if (internal_config.process_type == RTE_PROC_PRIMARY) {
810                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
811                 if (vfio_container_fd < 0) {
812                         RTE_LOG(ERR, EAL, "  cannot open VFIO container, "
813                                         "error %i (%s)\n", errno, strerror(errno));
814                         return -1;
815                 }
816
817                 /* check VFIO API version */
818                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
819                 if (ret != VFIO_API_VERSION) {
820                         if (ret < 0)
821                                 RTE_LOG(ERR, EAL, "  could not get VFIO API version, "
822                                                 "error %i (%s)\n", errno, strerror(errno));
823                         else
824                                 RTE_LOG(ERR, EAL, "  unsupported VFIO API version!\n");
825                         close(vfio_container_fd);
826                         return -1;
827                 }
828
829                 ret = vfio_has_supported_extensions(vfio_container_fd);
830                 if (ret) {
831                         RTE_LOG(ERR, EAL, "  no supported IOMMU "
832                                         "extensions found!\n");
833                         return -1;
834                 }
835
836                 return vfio_container_fd;
837         } else {
838                 /*
839                  * if we're in a secondary process, request container fd from the
840                  * primary process via our socket
841                  */
842                 int socket_fd;
843
844                 socket_fd = vfio_mp_sync_connect_to_primary();
845                 if (socket_fd < 0) {
846                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
847                         return -1;
848                 }
849                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
850                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
851                         close(socket_fd);
852                         return -1;
853                 }
854                 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
855                 if (vfio_container_fd < 0) {
856                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
857                         close(socket_fd);
858                         return -1;
859                 }
860                 close(socket_fd);
861                 return vfio_container_fd;
862         }
863
864         return -1;
865 }
866
867 int
868 vfio_get_group_no(const char *sysfs_base,
869                 const char *dev_addr, int *iommu_group_no)
870 {
871         char linkname[PATH_MAX];
872         char filename[PATH_MAX];
873         char *tok[16], *group_tok, *end;
874         int ret;
875
876         memset(linkname, 0, sizeof(linkname));
877         memset(filename, 0, sizeof(filename));
878
879         /* try to find out IOMMU group for this device */
880         snprintf(linkname, sizeof(linkname),
881                          "%s/%s/iommu_group", sysfs_base, dev_addr);
882
883         ret = readlink(linkname, filename, sizeof(filename));
884
885         /* if the link doesn't exist, no VFIO for us */
886         if (ret < 0)
887                 return 0;
888
889         ret = rte_strsplit(filename, sizeof(filename),
890                         tok, RTE_DIM(tok), '/');
891
892         if (ret <= 0) {
893                 RTE_LOG(ERR, EAL, "  %s cannot get IOMMU group\n", dev_addr);
894                 return -1;
895         }
896
897         /* IOMMU group is always the last token */
898         errno = 0;
899         group_tok = tok[ret - 1];
900         end = group_tok;
901         *iommu_group_no = strtol(group_tok, &end, 10);
902         if ((end != group_tok && *end != '\0') || errno != 0) {
903                 RTE_LOG(ERR, EAL, "  %s error parsing IOMMU number!\n", dev_addr);
904                 return -1;
905         }
906
907         return 1;
908 }
909
910 static int
911 type1_map(const struct rte_memseg *ms, void *arg)
912 {
913         int *vfio_container_fd = arg;
914
915         return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
916                         ms->len, 1);
917 }
918
919 static int
920 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
921                 uint64_t len, int do_map)
922 {
923         struct vfio_iommu_type1_dma_map dma_map;
924         struct vfio_iommu_type1_dma_unmap dma_unmap;
925         int ret;
926
927         if (do_map != 0) {
928                 memset(&dma_map, 0, sizeof(dma_map));
929                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
930                 dma_map.vaddr = vaddr;
931                 dma_map.size = len;
932                 dma_map.iova = iova;
933                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
934                                 VFIO_DMA_MAP_FLAG_WRITE;
935
936                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
937                 if (ret) {
938                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, error %i (%s)\n",
939                                 errno, strerror(errno));
940                                 return -1;
941                 }
942         } else {
943                 memset(&dma_unmap, 0, sizeof(dma_unmap));
944                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
945                 dma_unmap.size = len;
946                 dma_unmap.iova = iova;
947
948                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
949                                 &dma_unmap);
950                 if (ret) {
951                         RTE_LOG(ERR, EAL, "  cannot clear DMA remapping, error %i (%s)\n",
952                                         errno, strerror(errno));
953                         return -1;
954                 }
955         }
956
957         return 0;
958 }
959
960 static int
961 vfio_type1_dma_map(int vfio_container_fd)
962 {
963         return rte_memseg_walk(type1_map, &vfio_container_fd);
964 }
965
966 static int
967 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
968                 uint64_t len, int do_map)
969 {
970         struct vfio_iommu_type1_dma_map dma_map;
971         struct vfio_iommu_type1_dma_unmap dma_unmap;
972         int ret;
973
974         if (do_map != 0) {
975                 memset(&dma_map, 0, sizeof(dma_map));
976                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
977                 dma_map.vaddr = vaddr;
978                 dma_map.size = len;
979                 dma_map.iova = iova;
980                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
981                                 VFIO_DMA_MAP_FLAG_WRITE;
982
983                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
984                 if (ret) {
985                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, error %i (%s)\n",
986                                 errno, strerror(errno));
987                                 return -1;
988                 }
989
990         } else {
991                 struct vfio_iommu_spapr_register_memory reg = {
992                         .argsz = sizeof(reg),
993                         .flags = 0
994                 };
995                 reg.vaddr = (uintptr_t) vaddr;
996                 reg.size = len;
997
998                 ret = ioctl(vfio_container_fd,
999                                 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, &reg);
1000                 if (ret) {
1001                         RTE_LOG(ERR, EAL, "  cannot unregister vaddr for IOMMU, error %i (%s)\n",
1002                                         errno, strerror(errno));
1003                         return -1;
1004                 }
1005
1006                 memset(&dma_unmap, 0, sizeof(dma_unmap));
1007                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1008                 dma_unmap.size = len;
1009                 dma_unmap.iova = iova;
1010
1011                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1012                                 &dma_unmap);
1013                 if (ret) {
1014                         RTE_LOG(ERR, EAL, "  cannot clear DMA remapping, error %i (%s)\n",
1015                                         errno, strerror(errno));
1016                         return -1;
1017                 }
1018         }
1019
1020         return 0;
1021 }
1022
1023 static int
1024 vfio_spapr_map_walk(const struct rte_memseg *ms, void *arg)
1025 {
1026         int *vfio_container_fd = arg;
1027
1028         return vfio_spapr_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1029                         ms->len, 1);
1030 }
1031
1032 struct spapr_walk_param {
1033         uint64_t window_size;
1034         uint64_t hugepage_sz;
1035 };
1036 static int
1037 vfio_spapr_window_size_walk(const struct rte_memseg *ms, void *arg)
1038 {
1039         struct spapr_walk_param *param = arg;
1040         uint64_t max = ms->iova + ms->len;
1041
1042         if (max > param->window_size) {
1043                 param->hugepage_sz = ms->hugepage_sz;
1044                 param->window_size = max;
1045         }
1046
1047         return 0;
1048 }
1049
1050 static int
1051 vfio_spapr_create_new_dma_window(int vfio_container_fd,
1052                 struct vfio_iommu_spapr_tce_create *create) {
1053         struct vfio_iommu_spapr_tce_remove remove = {
1054                 .argsz = sizeof(remove),
1055         };
1056         struct vfio_iommu_spapr_tce_info info = {
1057                 .argsz = sizeof(info),
1058         };
1059         int ret;
1060
1061         /* query spapr iommu info */
1062         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1063         if (ret) {
1064                 RTE_LOG(ERR, EAL, "  cannot get iommu info, "
1065                                 "error %i (%s)\n", errno, strerror(errno));
1066                 return -1;
1067         }
1068
1069         /* remove default DMA of 32 bit window */
1070         remove.start_addr = info.dma32_window_start;
1071         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1072         if (ret) {
1073                 RTE_LOG(ERR, EAL, "  cannot remove default DMA window, "
1074                                 "error %i (%s)\n", errno, strerror(errno));
1075                 return -1;
1076         }
1077
1078         /* create new DMA window */
1079         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1080         if (ret) {
1081                 RTE_LOG(ERR, EAL, "  cannot create new DMA window, "
1082                                 "error %i (%s)\n", errno, strerror(errno));
1083                 return -1;
1084         }
1085
1086         if (create->start_addr != 0) {
1087                 RTE_LOG(ERR, EAL, "  DMA window start address != 0\n");
1088                 return -1;
1089         }
1090
1091         return 0;
1092 }
1093
1094 static int
1095 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1096                 uint64_t len, int do_map)
1097 {
1098         struct spapr_walk_param param;
1099         struct vfio_iommu_spapr_tce_create create = {
1100                 .argsz = sizeof(create),
1101         };
1102         int i, ret = 0;
1103
1104         rte_spinlock_lock(&user_mem_maps.lock);
1105
1106         /* check if window size needs to be adjusted */
1107         memset(&param, 0, sizeof(param));
1108
1109         if (rte_memseg_walk(vfio_spapr_window_size_walk, &param) < 0) {
1110                 RTE_LOG(ERR, EAL, "Could not get window size\n");
1111                 ret = -1;
1112                 goto out;
1113         }
1114
1115         /* also check user maps */
1116         for (i = 0; i < user_mem_maps.n_maps; i++) {
1117                 uint64_t max = user_mem_maps.maps[i].iova +
1118                                 user_mem_maps.maps[i].len;
1119                 create.window_size = RTE_MAX(create.window_size, max);
1120         }
1121
1122         /* sPAPR requires window size to be a power of 2 */
1123         create.window_size = rte_align64pow2(param.window_size);
1124         create.page_shift = __builtin_ctzll(param.hugepage_sz);
1125         create.levels = 1;
1126
1127         if (do_map) {
1128                 /* re-create window and remap the entire memory */
1129                 if (iova > create.window_size) {
1130                         if (vfio_spapr_create_new_dma_window(vfio_container_fd,
1131                                         &create) < 0) {
1132                                 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1133                                 ret = -1;
1134                                 goto out;
1135                         }
1136                         if (rte_memseg_walk(vfio_spapr_map_walk,
1137                                         &vfio_container_fd) < 0) {
1138                                 RTE_LOG(ERR, EAL, "Could not recreate DMA maps\n");
1139                                 ret = -1;
1140                                 goto out;
1141                         }
1142                         /* remap all user maps */
1143                         for (i = 0; i < user_mem_maps.n_maps; i++) {
1144                                 struct user_mem_map *map =
1145                                                 &user_mem_maps.maps[i];
1146                                 if (vfio_spapr_dma_do_map(vfio_container_fd,
1147                                                 map->addr, map->iova, map->len,
1148                                                 1)) {
1149                                         RTE_LOG(ERR, EAL, "Could not recreate user DMA maps\n");
1150                                         ret = -1;
1151                                         goto out;
1152                                 }
1153                         }
1154                 }
1155
1156                 /* now that we've remapped all of the memory that was present
1157                  * before, map the segment that we were requested to map.
1158                  */
1159                 if (vfio_spapr_dma_do_map(vfio_container_fd,
1160                                 vaddr, iova, len, 1) < 0) {
1161                         RTE_LOG(ERR, EAL, "Could not map segment\n");
1162                         ret = -1;
1163                         goto out;
1164                 }
1165         } else {
1166                 /* for unmap, check if iova within DMA window */
1167                 if (iova > create.window_size) {
1168                         RTE_LOG(ERR, EAL, "iova beyond DMA window for unmap");
1169                         ret = -1;
1170                         goto out;
1171                 }
1172
1173                 vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 0);
1174         }
1175 out:
1176         rte_spinlock_unlock(&user_mem_maps.lock);
1177         return ret;
1178 }
1179
1180 static int
1181 vfio_spapr_dma_map(int vfio_container_fd)
1182 {
1183         struct vfio_iommu_spapr_tce_create create = {
1184                 .argsz = sizeof(create),
1185         };
1186         struct spapr_walk_param param;
1187
1188         memset(&param, 0, sizeof(param));
1189
1190         /* create DMA window from 0 to max(phys_addr + len) */
1191         rte_memseg_walk(vfio_spapr_window_size_walk, &param);
1192
1193         /* sPAPR requires window size to be a power of 2 */
1194         create.window_size = rte_align64pow2(param.window_size);
1195         create.page_shift = __builtin_ctzll(param.hugepage_sz);
1196         create.levels = 1;
1197
1198         if (vfio_spapr_create_new_dma_window(vfio_container_fd, &create) < 0) {
1199                 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1200                 return -1;
1201         }
1202
1203         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
1204         if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1205                 return -1;
1206
1207         return 0;
1208 }
1209
1210 static int
1211 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1212 {
1213         /* No-IOMMU mode does not need DMA mapping */
1214         return 0;
1215 }
1216
1217 static int
1218 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1219                          uint64_t __rte_unused vaddr,
1220                          uint64_t __rte_unused iova, uint64_t __rte_unused len,
1221                          int __rte_unused do_map)
1222 {
1223         /* No-IOMMU mode does not need DMA mapping */
1224         return 0;
1225 }
1226
1227 static int
1228 vfio_dma_mem_map(uint64_t vaddr, uint64_t iova, uint64_t len, int do_map)
1229 {
1230         const struct vfio_iommu_type *t = vfio_cfg.vfio_iommu_type;
1231
1232         if (!t) {
1233                 RTE_LOG(ERR, EAL, "  VFIO support not initialized\n");
1234                 rte_errno = ENODEV;
1235                 return -1;
1236         }
1237
1238         if (!t->dma_user_map_func) {
1239                 RTE_LOG(ERR, EAL,
1240                         "  VFIO custom DMA region maping not supported by IOMMU %s\n",
1241                         t->name);
1242                 rte_errno = ENOTSUP;
1243                 return -1;
1244         }
1245
1246         return t->dma_user_map_func(vfio_cfg.vfio_container_fd, vaddr, iova,
1247                         len, do_map);
1248 }
1249
1250 int __rte_experimental
1251 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len)
1252 {
1253         struct user_mem_map *new_map;
1254         int ret = 0;
1255
1256         if (len == 0) {
1257                 rte_errno = EINVAL;
1258                 return -1;
1259         }
1260
1261         rte_spinlock_lock(&user_mem_maps.lock);
1262         if (user_mem_maps.n_maps == VFIO_MAX_USER_MEM_MAPS) {
1263                 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1264                 rte_errno = ENOMEM;
1265                 ret = -1;
1266                 goto out;
1267         }
1268         /* map the entry */
1269         if (vfio_dma_mem_map(vaddr, iova, len, 1)) {
1270                 /* technically, this will fail if there are currently no devices
1271                  * plugged in, even if a device were added later, this mapping
1272                  * might have succeeded. however, since we cannot verify if this
1273                  * is a valid mapping without having a device attached, consider
1274                  * this to be unsupported, because we can't just store any old
1275                  * mapping and pollute list of active mappings willy-nilly.
1276                  */
1277                 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1278                 ret = -1;
1279                 goto out;
1280         }
1281         /* create new user mem map entry */
1282         new_map = &user_mem_maps.maps[user_mem_maps.n_maps++];
1283         new_map->addr = vaddr;
1284         new_map->iova = iova;
1285         new_map->len = len;
1286
1287         compact_user_maps();
1288 out:
1289         rte_spinlock_unlock(&user_mem_maps.lock);
1290         return ret;
1291 }
1292
1293 int __rte_experimental
1294 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len)
1295 {
1296         struct user_mem_map *map, *new_map = NULL;
1297         int ret = 0;
1298
1299         if (len == 0) {
1300                 rte_errno = EINVAL;
1301                 return -1;
1302         }
1303
1304         rte_spinlock_lock(&user_mem_maps.lock);
1305
1306         /* find our mapping */
1307         map = find_user_mem_map(vaddr, iova, len);
1308         if (!map) {
1309                 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1310                 rte_errno = EINVAL;
1311                 ret = -1;
1312                 goto out;
1313         }
1314         if (map->addr != vaddr || map->iova != iova || map->len != len) {
1315                 /* we're partially unmapping a previously mapped region, so we
1316                  * need to split entry into two.
1317                  */
1318                 if (user_mem_maps.n_maps == VFIO_MAX_USER_MEM_MAPS) {
1319                         RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1320                         rte_errno = ENOMEM;
1321                         ret = -1;
1322                         goto out;
1323                 }
1324                 new_map = &user_mem_maps.maps[user_mem_maps.n_maps++];
1325         }
1326
1327         /* unmap the entry */
1328         if (vfio_dma_mem_map(vaddr, iova, len, 0)) {
1329                 /* there may not be any devices plugged in, so unmapping will
1330                  * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
1331                  * stop us from removing the mapping, as the assumption is we
1332                  * won't be needing this memory any more and thus will want to
1333                  * prevent it from being remapped again on hotplug. so, only
1334                  * fail if we indeed failed to unmap (e.g. if the mapping was
1335                  * within our mapped range but had invalid alignment).
1336                  */
1337                 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
1338                         RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
1339                         ret = -1;
1340                         goto out;
1341                 } else {
1342                         RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
1343                 }
1344         }
1345         /* remove map from the list of active mappings */
1346         if (new_map != NULL) {
1347                 adjust_map(map, new_map, vaddr, len);
1348
1349                 /* if we've created a new map by splitting, sort everything */
1350                 if (!is_null_map(new_map)) {
1351                         compact_user_maps();
1352                 } else {
1353                         /* we've created a new mapping, but it was unused */
1354                         user_mem_maps.n_maps--;
1355                 }
1356         } else {
1357                 memset(map, 0, sizeof(*map));
1358                 compact_user_maps();
1359                 user_mem_maps.n_maps--;
1360         }
1361
1362 out:
1363         rte_spinlock_unlock(&user_mem_maps.lock);
1364         return ret;
1365 }
1366
1367 int
1368 rte_vfio_noiommu_is_enabled(void)
1369 {
1370         int fd;
1371         ssize_t cnt;
1372         char c;
1373
1374         fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
1375         if (fd < 0) {
1376                 if (errno != ENOENT) {
1377                         RTE_LOG(ERR, EAL, "  cannot open vfio noiommu file %i (%s)\n",
1378                                         errno, strerror(errno));
1379                         return -1;
1380                 }
1381                 /*
1382                  * else the file does not exists
1383                  * i.e. noiommu is not enabled
1384                  */
1385                 return 0;
1386         }
1387
1388         cnt = read(fd, &c, 1);
1389         close(fd);
1390         if (cnt != 1) {
1391                 RTE_LOG(ERR, EAL, "  unable to read from vfio noiommu "
1392                                 "file %i (%s)\n", errno, strerror(errno));
1393                 return -1;
1394         }
1395
1396         return c == 'Y';
1397 }
1398
1399 #else
1400
1401 int __rte_experimental
1402 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
1403                   __rte_unused uint64_t len)
1404 {
1405         return -1;
1406 }
1407
1408 int __rte_experimental
1409 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
1410                     __rte_unused uint64_t len)
1411 {
1412         return -1;
1413 }
1414
1415 #endif