mem: replace memseg with memseg lists
[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_list *msl __rte_unused,
912                 const struct rte_memseg *ms, void *arg)
913 {
914         int *vfio_container_fd = arg;
915
916         return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
917                         ms->len, 1);
918 }
919
920 static int
921 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
922                 uint64_t len, int do_map)
923 {
924         struct vfio_iommu_type1_dma_map dma_map;
925         struct vfio_iommu_type1_dma_unmap dma_unmap;
926         int ret;
927
928         if (do_map != 0) {
929                 memset(&dma_map, 0, sizeof(dma_map));
930                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
931                 dma_map.vaddr = vaddr;
932                 dma_map.size = len;
933                 dma_map.iova = iova;
934                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
935                                 VFIO_DMA_MAP_FLAG_WRITE;
936
937                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
938                 if (ret) {
939                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, error %i (%s)\n",
940                                 errno, strerror(errno));
941                                 return -1;
942                 }
943         } else {
944                 memset(&dma_unmap, 0, sizeof(dma_unmap));
945                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
946                 dma_unmap.size = len;
947                 dma_unmap.iova = iova;
948
949                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
950                                 &dma_unmap);
951                 if (ret) {
952                         RTE_LOG(ERR, EAL, "  cannot clear DMA remapping, error %i (%s)\n",
953                                         errno, strerror(errno));
954                         return -1;
955                 }
956         }
957
958         return 0;
959 }
960
961 static int
962 vfio_type1_dma_map(int vfio_container_fd)
963 {
964         return rte_memseg_walk(type1_map, &vfio_container_fd);
965 }
966
967 static int
968 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
969                 uint64_t len, int do_map)
970 {
971         struct vfio_iommu_type1_dma_map dma_map;
972         struct vfio_iommu_type1_dma_unmap dma_unmap;
973         int ret;
974
975         if (do_map != 0) {
976                 memset(&dma_map, 0, sizeof(dma_map));
977                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
978                 dma_map.vaddr = vaddr;
979                 dma_map.size = len;
980                 dma_map.iova = iova;
981                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
982                                 VFIO_DMA_MAP_FLAG_WRITE;
983
984                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
985                 if (ret) {
986                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, error %i (%s)\n",
987                                 errno, strerror(errno));
988                                 return -1;
989                 }
990
991         } else {
992                 struct vfio_iommu_spapr_register_memory reg = {
993                         .argsz = sizeof(reg),
994                         .flags = 0
995                 };
996                 reg.vaddr = (uintptr_t) vaddr;
997                 reg.size = len;
998
999                 ret = ioctl(vfio_container_fd,
1000                                 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, &reg);
1001                 if (ret) {
1002                         RTE_LOG(ERR, EAL, "  cannot unregister vaddr for IOMMU, error %i (%s)\n",
1003                                         errno, strerror(errno));
1004                         return -1;
1005                 }
1006
1007                 memset(&dma_unmap, 0, sizeof(dma_unmap));
1008                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1009                 dma_unmap.size = len;
1010                 dma_unmap.iova = iova;
1011
1012                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1013                                 &dma_unmap);
1014                 if (ret) {
1015                         RTE_LOG(ERR, EAL, "  cannot clear DMA remapping, error %i (%s)\n",
1016                                         errno, strerror(errno));
1017                         return -1;
1018                 }
1019         }
1020
1021         return 0;
1022 }
1023
1024 static int
1025 vfio_spapr_map_walk(const struct rte_memseg_list *msl __rte_unused,
1026                 const struct rte_memseg *ms, void *arg)
1027 {
1028         int *vfio_container_fd = arg;
1029
1030         return vfio_spapr_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1031                         ms->len, 1);
1032 }
1033
1034 struct spapr_walk_param {
1035         uint64_t window_size;
1036         uint64_t hugepage_sz;
1037 };
1038 static int
1039 vfio_spapr_window_size_walk(const struct rte_memseg_list *msl __rte_unused,
1040                 const struct rte_memseg *ms, void *arg)
1041 {
1042         struct spapr_walk_param *param = arg;
1043         uint64_t max = ms->iova + ms->len;
1044
1045         if (max > param->window_size) {
1046                 param->hugepage_sz = ms->hugepage_sz;
1047                 param->window_size = max;
1048         }
1049
1050         return 0;
1051 }
1052
1053 static int
1054 vfio_spapr_create_new_dma_window(int vfio_container_fd,
1055                 struct vfio_iommu_spapr_tce_create *create) {
1056         struct vfio_iommu_spapr_tce_remove remove = {
1057                 .argsz = sizeof(remove),
1058         };
1059         struct vfio_iommu_spapr_tce_info info = {
1060                 .argsz = sizeof(info),
1061         };
1062         int ret;
1063
1064         /* query spapr iommu info */
1065         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1066         if (ret) {
1067                 RTE_LOG(ERR, EAL, "  cannot get iommu info, "
1068                                 "error %i (%s)\n", errno, strerror(errno));
1069                 return -1;
1070         }
1071
1072         /* remove default DMA of 32 bit window */
1073         remove.start_addr = info.dma32_window_start;
1074         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1075         if (ret) {
1076                 RTE_LOG(ERR, EAL, "  cannot remove default DMA window, "
1077                                 "error %i (%s)\n", errno, strerror(errno));
1078                 return -1;
1079         }
1080
1081         /* create new DMA window */
1082         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, create);
1083         if (ret) {
1084                 RTE_LOG(ERR, EAL, "  cannot create new DMA window, "
1085                                 "error %i (%s)\n", errno, strerror(errno));
1086                 return -1;
1087         }
1088
1089         if (create->start_addr != 0) {
1090                 RTE_LOG(ERR, EAL, "  DMA window start address != 0\n");
1091                 return -1;
1092         }
1093
1094         return 0;
1095 }
1096
1097 static int
1098 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1099                 uint64_t len, int do_map)
1100 {
1101         struct spapr_walk_param param;
1102         struct vfio_iommu_spapr_tce_create create = {
1103                 .argsz = sizeof(create),
1104         };
1105         int i, ret = 0;
1106
1107         rte_spinlock_lock(&user_mem_maps.lock);
1108
1109         /* check if window size needs to be adjusted */
1110         memset(&param, 0, sizeof(param));
1111
1112         if (rte_memseg_walk(vfio_spapr_window_size_walk, &param) < 0) {
1113                 RTE_LOG(ERR, EAL, "Could not get window size\n");
1114                 ret = -1;
1115                 goto out;
1116         }
1117
1118         /* also check user maps */
1119         for (i = 0; i < user_mem_maps.n_maps; i++) {
1120                 uint64_t max = user_mem_maps.maps[i].iova +
1121                                 user_mem_maps.maps[i].len;
1122                 create.window_size = RTE_MAX(create.window_size, max);
1123         }
1124
1125         /* sPAPR requires window size to be a power of 2 */
1126         create.window_size = rte_align64pow2(param.window_size);
1127         create.page_shift = __builtin_ctzll(param.hugepage_sz);
1128         create.levels = 1;
1129
1130         if (do_map) {
1131                 /* re-create window and remap the entire memory */
1132                 if (iova > create.window_size) {
1133                         if (vfio_spapr_create_new_dma_window(vfio_container_fd,
1134                                         &create) < 0) {
1135                                 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1136                                 ret = -1;
1137                                 goto out;
1138                         }
1139                         if (rte_memseg_walk(vfio_spapr_map_walk,
1140                                         &vfio_container_fd) < 0) {
1141                                 RTE_LOG(ERR, EAL, "Could not recreate DMA maps\n");
1142                                 ret = -1;
1143                                 goto out;
1144                         }
1145                         /* remap all user maps */
1146                         for (i = 0; i < user_mem_maps.n_maps; i++) {
1147                                 struct user_mem_map *map =
1148                                                 &user_mem_maps.maps[i];
1149                                 if (vfio_spapr_dma_do_map(vfio_container_fd,
1150                                                 map->addr, map->iova, map->len,
1151                                                 1)) {
1152                                         RTE_LOG(ERR, EAL, "Could not recreate user DMA maps\n");
1153                                         ret = -1;
1154                                         goto out;
1155                                 }
1156                         }
1157                 }
1158
1159                 /* now that we've remapped all of the memory that was present
1160                  * before, map the segment that we were requested to map.
1161                  */
1162                 if (vfio_spapr_dma_do_map(vfio_container_fd,
1163                                 vaddr, iova, len, 1) < 0) {
1164                         RTE_LOG(ERR, EAL, "Could not map segment\n");
1165                         ret = -1;
1166                         goto out;
1167                 }
1168         } else {
1169                 /* for unmap, check if iova within DMA window */
1170                 if (iova > create.window_size) {
1171                         RTE_LOG(ERR, EAL, "iova beyond DMA window for unmap");
1172                         ret = -1;
1173                         goto out;
1174                 }
1175
1176                 vfio_spapr_dma_do_map(vfio_container_fd, vaddr, iova, len, 0);
1177         }
1178 out:
1179         rte_spinlock_unlock(&user_mem_maps.lock);
1180         return ret;
1181 }
1182
1183 static int
1184 vfio_spapr_dma_map(int vfio_container_fd)
1185 {
1186         struct vfio_iommu_spapr_tce_create create = {
1187                 .argsz = sizeof(create),
1188         };
1189         struct spapr_walk_param param;
1190
1191         memset(&param, 0, sizeof(param));
1192
1193         /* create DMA window from 0 to max(phys_addr + len) */
1194         rte_memseg_walk(vfio_spapr_window_size_walk, &param);
1195
1196         /* sPAPR requires window size to be a power of 2 */
1197         create.window_size = rte_align64pow2(param.window_size);
1198         create.page_shift = __builtin_ctzll(param.hugepage_sz);
1199         create.levels = 1;
1200
1201         if (vfio_spapr_create_new_dma_window(vfio_container_fd, &create) < 0) {
1202                 RTE_LOG(ERR, EAL, "Could not create new DMA window\n");
1203                 return -1;
1204         }
1205
1206         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
1207         if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1208                 return -1;
1209
1210         return 0;
1211 }
1212
1213 static int
1214 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1215 {
1216         /* No-IOMMU mode does not need DMA mapping */
1217         return 0;
1218 }
1219
1220 static int
1221 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1222                          uint64_t __rte_unused vaddr,
1223                          uint64_t __rte_unused iova, uint64_t __rte_unused len,
1224                          int __rte_unused do_map)
1225 {
1226         /* No-IOMMU mode does not need DMA mapping */
1227         return 0;
1228 }
1229
1230 static int
1231 vfio_dma_mem_map(uint64_t vaddr, uint64_t iova, uint64_t len, int do_map)
1232 {
1233         const struct vfio_iommu_type *t = vfio_cfg.vfio_iommu_type;
1234
1235         if (!t) {
1236                 RTE_LOG(ERR, EAL, "  VFIO support not initialized\n");
1237                 rte_errno = ENODEV;
1238                 return -1;
1239         }
1240
1241         if (!t->dma_user_map_func) {
1242                 RTE_LOG(ERR, EAL,
1243                         "  VFIO custom DMA region maping not supported by IOMMU %s\n",
1244                         t->name);
1245                 rte_errno = ENOTSUP;
1246                 return -1;
1247         }
1248
1249         return t->dma_user_map_func(vfio_cfg.vfio_container_fd, vaddr, iova,
1250                         len, do_map);
1251 }
1252
1253 int __rte_experimental
1254 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len)
1255 {
1256         struct user_mem_map *new_map;
1257         int ret = 0;
1258
1259         if (len == 0) {
1260                 rte_errno = EINVAL;
1261                 return -1;
1262         }
1263
1264         rte_spinlock_lock(&user_mem_maps.lock);
1265         if (user_mem_maps.n_maps == VFIO_MAX_USER_MEM_MAPS) {
1266                 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1267                 rte_errno = ENOMEM;
1268                 ret = -1;
1269                 goto out;
1270         }
1271         /* map the entry */
1272         if (vfio_dma_mem_map(vaddr, iova, len, 1)) {
1273                 /* technically, this will fail if there are currently no devices
1274                  * plugged in, even if a device were added later, this mapping
1275                  * might have succeeded. however, since we cannot verify if this
1276                  * is a valid mapping without having a device attached, consider
1277                  * this to be unsupported, because we can't just store any old
1278                  * mapping and pollute list of active mappings willy-nilly.
1279                  */
1280                 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1281                 ret = -1;
1282                 goto out;
1283         }
1284         /* create new user mem map entry */
1285         new_map = &user_mem_maps.maps[user_mem_maps.n_maps++];
1286         new_map->addr = vaddr;
1287         new_map->iova = iova;
1288         new_map->len = len;
1289
1290         compact_user_maps();
1291 out:
1292         rte_spinlock_unlock(&user_mem_maps.lock);
1293         return ret;
1294 }
1295
1296 int __rte_experimental
1297 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len)
1298 {
1299         struct user_mem_map *map, *new_map = NULL;
1300         int ret = 0;
1301
1302         if (len == 0) {
1303                 rte_errno = EINVAL;
1304                 return -1;
1305         }
1306
1307         rte_spinlock_lock(&user_mem_maps.lock);
1308
1309         /* find our mapping */
1310         map = find_user_mem_map(vaddr, iova, len);
1311         if (!map) {
1312                 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1313                 rte_errno = EINVAL;
1314                 ret = -1;
1315                 goto out;
1316         }
1317         if (map->addr != vaddr || map->iova != iova || map->len != len) {
1318                 /* we're partially unmapping a previously mapped region, so we
1319                  * need to split entry into two.
1320                  */
1321                 if (user_mem_maps.n_maps == VFIO_MAX_USER_MEM_MAPS) {
1322                         RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1323                         rte_errno = ENOMEM;
1324                         ret = -1;
1325                         goto out;
1326                 }
1327                 new_map = &user_mem_maps.maps[user_mem_maps.n_maps++];
1328         }
1329
1330         /* unmap the entry */
1331         if (vfio_dma_mem_map(vaddr, iova, len, 0)) {
1332                 /* there may not be any devices plugged in, so unmapping will
1333                  * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
1334                  * stop us from removing the mapping, as the assumption is we
1335                  * won't be needing this memory any more and thus will want to
1336                  * prevent it from being remapped again on hotplug. so, only
1337                  * fail if we indeed failed to unmap (e.g. if the mapping was
1338                  * within our mapped range but had invalid alignment).
1339                  */
1340                 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
1341                         RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
1342                         ret = -1;
1343                         goto out;
1344                 } else {
1345                         RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
1346                 }
1347         }
1348         /* remove map from the list of active mappings */
1349         if (new_map != NULL) {
1350                 adjust_map(map, new_map, vaddr, len);
1351
1352                 /* if we've created a new map by splitting, sort everything */
1353                 if (!is_null_map(new_map)) {
1354                         compact_user_maps();
1355                 } else {
1356                         /* we've created a new mapping, but it was unused */
1357                         user_mem_maps.n_maps--;
1358                 }
1359         } else {
1360                 memset(map, 0, sizeof(*map));
1361                 compact_user_maps();
1362                 user_mem_maps.n_maps--;
1363         }
1364
1365 out:
1366         rte_spinlock_unlock(&user_mem_maps.lock);
1367         return ret;
1368 }
1369
1370 int
1371 rte_vfio_noiommu_is_enabled(void)
1372 {
1373         int fd;
1374         ssize_t cnt;
1375         char c;
1376
1377         fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
1378         if (fd < 0) {
1379                 if (errno != ENOENT) {
1380                         RTE_LOG(ERR, EAL, "  cannot open vfio noiommu file %i (%s)\n",
1381                                         errno, strerror(errno));
1382                         return -1;
1383                 }
1384                 /*
1385                  * else the file does not exists
1386                  * i.e. noiommu is not enabled
1387                  */
1388                 return 0;
1389         }
1390
1391         cnt = read(fd, &c, 1);
1392         close(fd);
1393         if (cnt != 1) {
1394                 RTE_LOG(ERR, EAL, "  unable to read from vfio noiommu "
1395                                 "file %i (%s)\n", errno, strerror(errno));
1396                 return -1;
1397         }
1398
1399         return c == 'Y';
1400 }
1401
1402 #else
1403
1404 int __rte_experimental
1405 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
1406                   __rte_unused uint64_t len)
1407 {
1408         return -1;
1409 }
1410
1411 int __rte_experimental
1412 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
1413                     __rte_unused uint64_t len)
1414 {
1415         return -1;
1416 }
1417
1418 #endif