vfio: fix partial unmap
[dpdk.git] / lib / eal / linux / eal_vfio.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 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_memcfg.h"
19 #include "eal_vfio.h"
20 #include "eal_private.h"
21 #include "eal_internal_cfg.h"
22
23 #ifdef VFIO_PRESENT
24
25 #define VFIO_MEM_EVENT_CLB_NAME "vfio_mem_event_clb"
26
27 /* hot plug/unplug of VFIO groups may cause all DMA maps to be dropped. we can
28  * recreate the mappings for DPDK segments, but we cannot do so for memory that
29  * was registered by the user themselves, so we need to store the user mappings
30  * somewhere, to recreate them later.
31  */
32 #define VFIO_MAX_USER_MEM_MAPS 256
33 struct user_mem_map {
34         uint64_t addr;  /**< start VA */
35         uint64_t iova;  /**< start IOVA */
36         uint64_t len;   /**< total length of the mapping */
37         uint64_t chunk; /**< this mapping can be split in chunks of this size */
38 };
39
40 struct user_mem_maps {
41         rte_spinlock_recursive_t lock;
42         int n_maps;
43         struct user_mem_map maps[VFIO_MAX_USER_MEM_MAPS];
44 };
45
46 struct vfio_config {
47         int vfio_enabled;
48         int vfio_container_fd;
49         int vfio_active_groups;
50         const struct vfio_iommu_type *vfio_iommu_type;
51         struct vfio_group vfio_groups[VFIO_MAX_GROUPS];
52         struct user_mem_maps mem_maps;
53 };
54
55 /* per-process VFIO config */
56 static struct vfio_config vfio_cfgs[VFIO_MAX_CONTAINERS];
57 static struct vfio_config *default_vfio_cfg = &vfio_cfgs[0];
58
59 static int vfio_type1_dma_map(int);
60 static int vfio_type1_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
61 static int vfio_spapr_dma_map(int);
62 static int vfio_spapr_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
63 static int vfio_noiommu_dma_map(int);
64 static int vfio_noiommu_dma_mem_map(int, uint64_t, uint64_t, uint64_t, int);
65 static int vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr,
66                 uint64_t iova, uint64_t len, int do_map);
67
68 /* IOMMU types we support */
69 static const struct vfio_iommu_type iommu_types[] = {
70         /* x86 IOMMU, otherwise known as type 1 */
71         {
72                 .type_id = RTE_VFIO_TYPE1,
73                 .name = "Type 1",
74                 .partial_unmap = false,
75                 .dma_map_func = &vfio_type1_dma_map,
76                 .dma_user_map_func = &vfio_type1_dma_mem_map
77         },
78         /* ppc64 IOMMU, otherwise known as spapr */
79         {
80                 .type_id = RTE_VFIO_SPAPR,
81                 .name = "sPAPR",
82                 .partial_unmap = true,
83                 .dma_map_func = &vfio_spapr_dma_map,
84                 .dma_user_map_func = &vfio_spapr_dma_mem_map
85         },
86         /* IOMMU-less mode */
87         {
88                 .type_id = RTE_VFIO_NOIOMMU,
89                 .name = "No-IOMMU",
90                 .partial_unmap = true,
91                 .dma_map_func = &vfio_noiommu_dma_map,
92                 .dma_user_map_func = &vfio_noiommu_dma_mem_map
93         },
94 };
95
96 static int
97 is_null_map(const struct user_mem_map *map)
98 {
99         return map->addr == 0 && map->iova == 0 &&
100                         map->len == 0 && map->chunk == 0;
101 }
102
103 /* we may need to merge user mem maps together in case of user mapping/unmapping
104  * chunks of memory, so we'll need a comparator function to sort segments.
105  */
106 static int
107 user_mem_map_cmp(const void *a, const void *b)
108 {
109         const struct user_mem_map *umm_a = a;
110         const struct user_mem_map *umm_b = b;
111
112         /* move null entries to end */
113         if (is_null_map(umm_a))
114                 return 1;
115         if (is_null_map(umm_b))
116                 return -1;
117
118         /* sort by iova first */
119         if (umm_a->iova < umm_b->iova)
120                 return -1;
121         if (umm_a->iova > umm_b->iova)
122                 return 1;
123
124         if (umm_a->addr < umm_b->addr)
125                 return -1;
126         if (umm_a->addr > umm_b->addr)
127                 return 1;
128
129         if (umm_a->len < umm_b->len)
130                 return -1;
131         if (umm_a->len > umm_b->len)
132                 return 1;
133
134         if (umm_a->chunk < umm_b->chunk)
135                 return -1;
136         if (umm_a->chunk > umm_b->chunk)
137                 return 1;
138
139         return 0;
140 }
141
142 /*
143  * Take in an address range and list of current mappings, and produce a list of
144  * mappings that will be kept.
145  */
146 static int
147 process_maps(struct user_mem_map *src, size_t src_len,
148                 struct user_mem_map newmap[2], uint64_t vaddr, uint64_t len)
149 {
150         struct user_mem_map *src_first = &src[0];
151         struct user_mem_map *src_last = &src[src_len - 1];
152         struct user_mem_map *dst_first = &newmap[0];
153         /* we can get at most two new segments */
154         struct user_mem_map *dst_last = &newmap[1];
155         uint64_t first_off = vaddr - src_first->addr;
156         uint64_t last_off = (src_last->addr + src_last->len) - (vaddr + len);
157         int newmap_len = 0;
158
159         if (first_off != 0) {
160                 dst_first->addr = src_first->addr;
161                 dst_first->iova = src_first->iova;
162                 dst_first->len = first_off;
163                 dst_first->chunk = src_first->chunk;
164
165                 newmap_len++;
166         }
167         if (last_off != 0) {
168                 /* if we had start offset, we have two segments */
169                 struct user_mem_map *last =
170                                 first_off == 0 ? dst_first : dst_last;
171                 last->addr = (src_last->addr + src_last->len) - last_off;
172                 last->iova = (src_last->iova + src_last->len) - last_off;
173                 last->len = last_off;
174                 last->chunk = src_last->chunk;
175
176                 newmap_len++;
177         }
178         return newmap_len;
179 }
180
181 /* erase certain maps from the list */
182 static void
183 delete_maps(struct user_mem_maps *user_mem_maps, struct user_mem_map *del_maps,
184                 size_t n_del)
185 {
186         int i;
187         size_t j;
188
189         for (i = 0, j = 0; i < VFIO_MAX_USER_MEM_MAPS && j < n_del; i++) {
190                 struct user_mem_map *left = &user_mem_maps->maps[i];
191                 struct user_mem_map *right = &del_maps[j];
192
193                 if (user_mem_map_cmp(left, right) == 0) {
194                         memset(left, 0, sizeof(*left));
195                         j++;
196                         user_mem_maps->n_maps--;
197                 }
198         }
199 }
200
201 static void
202 copy_maps(struct user_mem_maps *user_mem_maps, struct user_mem_map *add_maps,
203                 size_t n_add)
204 {
205         int i;
206         size_t j;
207
208         for (i = 0, j = 0; i < VFIO_MAX_USER_MEM_MAPS && j < n_add; i++) {
209                 struct user_mem_map *left = &user_mem_maps->maps[i];
210                 struct user_mem_map *right = &add_maps[j];
211
212                 /* insert into empty space */
213                 if (is_null_map(left)) {
214                         memcpy(left, right, sizeof(*left));
215                         j++;
216                         user_mem_maps->n_maps++;
217                 }
218         }
219 }
220
221 /* try merging two maps into one, return 1 if succeeded */
222 static int
223 merge_map(struct user_mem_map *left, struct user_mem_map *right)
224 {
225         /* merge the same maps into one */
226         if (memcmp(left, right, sizeof(struct user_mem_map)) == 0)
227                 goto out;
228
229         if (left->addr + left->len != right->addr)
230                 return 0;
231         if (left->iova + left->len != right->iova)
232                 return 0;
233         if (left->chunk != right->chunk)
234                 return 0;
235         left->len += right->len;
236
237 out:
238         memset(right, 0, sizeof(*right));
239
240         return 1;
241 }
242
243 static bool
244 addr_is_chunk_aligned(struct user_mem_map *maps, size_t n_maps,
245                 uint64_t vaddr, uint64_t iova)
246 {
247         unsigned int i;
248
249         for (i = 0; i < n_maps; i++) {
250                 struct user_mem_map *map = &maps[i];
251                 uint64_t map_va_end = map->addr + map->len;
252                 uint64_t map_iova_end = map->iova + map->len;
253                 uint64_t map_va_off = vaddr - map->addr;
254                 uint64_t map_iova_off = iova - map->iova;
255
256                 /* we include end of the segment in comparison as well */
257                 bool addr_in_map = (vaddr >= map->addr) && (vaddr <= map_va_end);
258                 bool iova_in_map = (iova >= map->iova) && (iova <= map_iova_end);
259                 /* chunk may not be power of two, so use modulo */
260                 bool addr_is_aligned = (map_va_off % map->chunk) == 0;
261                 bool iova_is_aligned = (map_iova_off % map->chunk) == 0;
262
263                 if (addr_in_map && iova_in_map &&
264                                 addr_is_aligned && iova_is_aligned)
265                         return true;
266         }
267         return false;
268 }
269
270 static int
271 find_user_mem_maps(struct user_mem_maps *user_mem_maps, uint64_t addr,
272                 uint64_t iova, uint64_t len, struct user_mem_map *dst,
273                 size_t dst_len)
274 {
275         uint64_t va_end = addr + len;
276         uint64_t iova_end = iova + len;
277         bool found = false;
278         size_t j;
279         int i, ret;
280
281         for (i = 0, j = 0; i < user_mem_maps->n_maps; i++) {
282                 struct user_mem_map *map = &user_mem_maps->maps[i];
283                 uint64_t map_va_end = map->addr + map->len;
284                 uint64_t map_iova_end = map->iova + map->len;
285
286                 bool start_addr_in_map = (addr >= map->addr) &&
287                                 (addr < map_va_end);
288                 bool end_addr_in_map = (va_end > map->addr) &&
289                                 (va_end <= map_va_end);
290                 bool start_iova_in_map = (iova >= map->iova) &&
291                                 (iova < map_iova_end);
292                 bool end_iova_in_map = (iova_end > map->iova) &&
293                                 (iova_end <= map_iova_end);
294
295                 /* do we have space in temporary map? */
296                 if (j == dst_len) {
297                         ret = -ENOSPC;
298                         goto err;
299                 }
300                 /* check if current map is start of our segment */
301                 if (!found && start_addr_in_map && start_iova_in_map)
302                         found = true;
303                 /* if we have previously found a segment, add it to the map */
304                 if (found) {
305                         /* copy the segment into our temporary map */
306                         memcpy(&dst[j++], map, sizeof(*map));
307
308                         /* if we match end of segment, quit */
309                         if (end_addr_in_map && end_iova_in_map)
310                                 return j;
311                 }
312         }
313         /* we didn't find anything */
314         ret = -ENOENT;
315 err:
316         memset(dst, 0, sizeof(*dst) * dst_len);
317         return ret;
318 }
319
320 /* this will sort all user maps, and merge/compact any adjacent maps */
321 static void
322 compact_user_maps(struct user_mem_maps *user_mem_maps)
323 {
324         int i;
325
326         qsort(user_mem_maps->maps, VFIO_MAX_USER_MEM_MAPS,
327                         sizeof(user_mem_maps->maps[0]), user_mem_map_cmp);
328
329         /* we'll go over the list backwards when merging */
330         for (i = VFIO_MAX_USER_MEM_MAPS - 2; i >= 0; i--) {
331                 struct user_mem_map *l, *r;
332
333                 l = &user_mem_maps->maps[i];
334                 r = &user_mem_maps->maps[i + 1];
335
336                 if (is_null_map(l) || is_null_map(r))
337                         continue;
338
339                 /* try and merge the maps */
340                 if (merge_map(l, r))
341                         user_mem_maps->n_maps--;
342         }
343
344         /* the entries are still sorted, but now they have holes in them, so
345          * sort the list again.
346          */
347         qsort(user_mem_maps->maps, VFIO_MAX_USER_MEM_MAPS,
348                         sizeof(user_mem_maps->maps[0]), user_mem_map_cmp);
349 }
350
351 static int
352 vfio_open_group_fd(int iommu_group_num)
353 {
354         int vfio_group_fd;
355         char filename[PATH_MAX];
356         struct rte_mp_msg mp_req, *mp_rep;
357         struct rte_mp_reply mp_reply = {0};
358         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
359         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
360         const struct internal_config *internal_conf =
361                 eal_get_internal_configuration();
362
363         /* if primary, try to open the group */
364         if (internal_conf->process_type == RTE_PROC_PRIMARY) {
365                 /* try regular group format */
366                 snprintf(filename, sizeof(filename),
367                                  VFIO_GROUP_FMT, iommu_group_num);
368                 vfio_group_fd = open(filename, O_RDWR);
369                 if (vfio_group_fd < 0) {
370                         /* if file not found, it's not an error */
371                         if (errno != ENOENT) {
372                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
373                                                 filename, strerror(errno));
374                                 return -1;
375                         }
376
377                         /* special case: try no-IOMMU path as well */
378                         snprintf(filename, sizeof(filename),
379                                         VFIO_NOIOMMU_GROUP_FMT,
380                                         iommu_group_num);
381                         vfio_group_fd = open(filename, O_RDWR);
382                         if (vfio_group_fd < 0) {
383                                 if (errno != ENOENT) {
384                                         RTE_LOG(ERR, EAL,
385                                                 "Cannot open %s: %s\n",
386                                                 filename, strerror(errno));
387                                         return -1;
388                                 }
389                                 return -ENOENT;
390                         }
391                         /* noiommu group found */
392                 }
393
394                 return vfio_group_fd;
395         }
396         /* if we're in a secondary process, request group fd from the primary
397          * process via mp channel.
398          */
399         p->req = SOCKET_REQ_GROUP;
400         p->group_num = iommu_group_num;
401         strcpy(mp_req.name, EAL_VFIO_MP);
402         mp_req.len_param = sizeof(*p);
403         mp_req.num_fds = 0;
404
405         vfio_group_fd = -1;
406         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
407             mp_reply.nb_received == 1) {
408                 mp_rep = &mp_reply.msgs[0];
409                 p = (struct vfio_mp_param *)mp_rep->param;
410                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
411                         vfio_group_fd = mp_rep->fds[0];
412                 } else if (p->result == SOCKET_NO_FD) {
413                         RTE_LOG(ERR, EAL, "Bad VFIO group fd\n");
414                         vfio_group_fd = -ENOENT;
415                 }
416         }
417
418         free(mp_reply.msgs);
419         if (vfio_group_fd < 0 && vfio_group_fd != -ENOENT)
420                 RTE_LOG(ERR, EAL, "Cannot request VFIO group fd\n");
421         return vfio_group_fd;
422 }
423
424 static struct vfio_config *
425 get_vfio_cfg_by_group_num(int iommu_group_num)
426 {
427         struct vfio_config *vfio_cfg;
428         int i, j;
429
430         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
431                 vfio_cfg = &vfio_cfgs[i];
432                 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
433                         if (vfio_cfg->vfio_groups[j].group_num ==
434                                         iommu_group_num)
435                                 return vfio_cfg;
436                 }
437         }
438
439         return NULL;
440 }
441
442 static int
443 vfio_get_group_fd(struct vfio_config *vfio_cfg,
444                 int iommu_group_num)
445 {
446         int i;
447         int vfio_group_fd;
448         struct vfio_group *cur_grp;
449
450         /* check if we already have the group descriptor open */
451         for (i = 0; i < VFIO_MAX_GROUPS; i++)
452                 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num)
453                         return vfio_cfg->vfio_groups[i].fd;
454
455         /* Lets see first if there is room for a new group */
456         if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
457                 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
458                 return -1;
459         }
460
461         /* Now lets get an index for the new group */
462         for (i = 0; i < VFIO_MAX_GROUPS; i++)
463                 if (vfio_cfg->vfio_groups[i].group_num == -1) {
464                         cur_grp = &vfio_cfg->vfio_groups[i];
465                         break;
466                 }
467
468         /* This should not happen */
469         if (i == VFIO_MAX_GROUPS) {
470                 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
471                 return -1;
472         }
473
474         vfio_group_fd = vfio_open_group_fd(iommu_group_num);
475         if (vfio_group_fd < 0) {
476                 RTE_LOG(ERR, EAL, "Failed to open VFIO group %d\n",
477                         iommu_group_num);
478                 return vfio_group_fd;
479         }
480
481         cur_grp->group_num = iommu_group_num;
482         cur_grp->fd = vfio_group_fd;
483         vfio_cfg->vfio_active_groups++;
484
485         return vfio_group_fd;
486 }
487
488 static struct vfio_config *
489 get_vfio_cfg_by_group_fd(int vfio_group_fd)
490 {
491         struct vfio_config *vfio_cfg;
492         int i, j;
493
494         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
495                 vfio_cfg = &vfio_cfgs[i];
496                 for (j = 0; j < VFIO_MAX_GROUPS; j++)
497                         if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
498                                 return vfio_cfg;
499         }
500
501         return NULL;
502 }
503
504 static struct vfio_config *
505 get_vfio_cfg_by_container_fd(int container_fd)
506 {
507         int i;
508
509         if (container_fd == RTE_VFIO_DEFAULT_CONTAINER_FD)
510                 return default_vfio_cfg;
511
512         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
513                 if (vfio_cfgs[i].vfio_container_fd == container_fd)
514                         return &vfio_cfgs[i];
515         }
516
517         return NULL;
518 }
519
520 int
521 rte_vfio_get_group_fd(int iommu_group_num)
522 {
523         struct vfio_config *vfio_cfg;
524
525         /* get the vfio_config it belongs to */
526         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
527         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
528
529         return vfio_get_group_fd(vfio_cfg, iommu_group_num);
530 }
531
532 static int
533 get_vfio_group_idx(int vfio_group_fd)
534 {
535         struct vfio_config *vfio_cfg;
536         int i, j;
537
538         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
539                 vfio_cfg = &vfio_cfgs[i];
540                 for (j = 0; j < VFIO_MAX_GROUPS; j++)
541                         if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
542                                 return j;
543         }
544
545         return -1;
546 }
547
548 static void
549 vfio_group_device_get(int vfio_group_fd)
550 {
551         struct vfio_config *vfio_cfg;
552         int i;
553
554         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
555         if (vfio_cfg == NULL) {
556                 RTE_LOG(ERR, EAL, "Invalid VFIO group fd!\n");
557                 return;
558         }
559
560         i = get_vfio_group_idx(vfio_group_fd);
561         if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
562                 RTE_LOG(ERR, EAL, "Wrong VFIO group index (%d)\n", i);
563         else
564                 vfio_cfg->vfio_groups[i].devices++;
565 }
566
567 static void
568 vfio_group_device_put(int vfio_group_fd)
569 {
570         struct vfio_config *vfio_cfg;
571         int i;
572
573         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
574         if (vfio_cfg == NULL) {
575                 RTE_LOG(ERR, EAL, "Invalid VFIO group fd!\n");
576                 return;
577         }
578
579         i = get_vfio_group_idx(vfio_group_fd);
580         if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
581                 RTE_LOG(ERR, EAL, "Wrong VFIO group index (%d)\n", i);
582         else
583                 vfio_cfg->vfio_groups[i].devices--;
584 }
585
586 static int
587 vfio_group_device_count(int vfio_group_fd)
588 {
589         struct vfio_config *vfio_cfg;
590         int i;
591
592         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
593         if (vfio_cfg == NULL) {
594                 RTE_LOG(ERR, EAL, "Invalid VFIO group fd!\n");
595                 return -1;
596         }
597
598         i = get_vfio_group_idx(vfio_group_fd);
599         if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
600                 RTE_LOG(ERR, EAL, "Wrong VFIO group index (%d)\n", i);
601                 return -1;
602         }
603
604         return vfio_cfg->vfio_groups[i].devices;
605 }
606
607 static void
608 vfio_mem_event_callback(enum rte_mem_event type, const void *addr, size_t len,
609                 void *arg __rte_unused)
610 {
611         struct rte_memseg_list *msl;
612         struct rte_memseg *ms;
613         size_t cur_len = 0;
614
615         msl = rte_mem_virt2memseg_list(addr);
616
617         /* for IOVA as VA mode, no need to care for IOVA addresses */
618         if (rte_eal_iova_mode() == RTE_IOVA_VA && msl->external == 0) {
619                 uint64_t vfio_va = (uint64_t)(uintptr_t)addr;
620                 uint64_t page_sz = msl->page_sz;
621
622                 /* Maintain granularity of DMA map/unmap to memseg size */
623                 for (; cur_len < len; cur_len += page_sz) {
624                         if (type == RTE_MEM_EVENT_ALLOC)
625                                 vfio_dma_mem_map(default_vfio_cfg, vfio_va,
626                                                  vfio_va, page_sz, 1);
627                         else
628                                 vfio_dma_mem_map(default_vfio_cfg, vfio_va,
629                                                  vfio_va, page_sz, 0);
630                         vfio_va += page_sz;
631                 }
632
633                 return;
634         }
635
636         /* memsegs are contiguous in memory */
637         ms = rte_mem_virt2memseg(addr, msl);
638         while (cur_len < len) {
639                 /* some memory segments may have invalid IOVA */
640                 if (ms->iova == RTE_BAD_IOVA) {
641                         RTE_LOG(DEBUG, EAL,
642                                 "Memory segment at %p has bad IOVA, skipping\n",
643                                 ms->addr);
644                         goto next;
645                 }
646                 if (type == RTE_MEM_EVENT_ALLOC)
647                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
648                                         ms->iova, ms->len, 1);
649                 else
650                         vfio_dma_mem_map(default_vfio_cfg, ms->addr_64,
651                                         ms->iova, ms->len, 0);
652 next:
653                 cur_len += ms->len;
654                 ++ms;
655         }
656 }
657
658 static int
659 vfio_sync_default_container(void)
660 {
661         struct rte_mp_msg mp_req, *mp_rep;
662         struct rte_mp_reply mp_reply = {0};
663         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
664         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
665         int iommu_type_id;
666         unsigned int i;
667
668         /* cannot be called from primary */
669         if (rte_eal_process_type() != RTE_PROC_SECONDARY)
670                 return -1;
671
672         /* default container fd should have been opened in rte_vfio_enable() */
673         if (!default_vfio_cfg->vfio_enabled ||
674                         default_vfio_cfg->vfio_container_fd < 0) {
675                 RTE_LOG(ERR, EAL, "VFIO support is not initialized\n");
676                 return -1;
677         }
678
679         /* find default container's IOMMU type */
680         p->req = SOCKET_REQ_IOMMU_TYPE;
681         strcpy(mp_req.name, EAL_VFIO_MP);
682         mp_req.len_param = sizeof(*p);
683         mp_req.num_fds = 0;
684
685         iommu_type_id = -1;
686         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
687                         mp_reply.nb_received == 1) {
688                 mp_rep = &mp_reply.msgs[0];
689                 p = (struct vfio_mp_param *)mp_rep->param;
690                 if (p->result == SOCKET_OK)
691                         iommu_type_id = p->iommu_type_id;
692         }
693         free(mp_reply.msgs);
694         if (iommu_type_id < 0) {
695                 RTE_LOG(ERR, EAL,
696                         "Could not get IOMMU type for default container\n");
697                 return -1;
698         }
699
700         /* we now have an fd for default container, as well as its IOMMU type.
701          * now, set up default VFIO container config to match.
702          */
703         for (i = 0; i < RTE_DIM(iommu_types); i++) {
704                 const struct vfio_iommu_type *t = &iommu_types[i];
705                 if (t->type_id != iommu_type_id)
706                         continue;
707
708                 /* we found our IOMMU type */
709                 default_vfio_cfg->vfio_iommu_type = t;
710
711                 return 0;
712         }
713         RTE_LOG(ERR, EAL, "Could not find IOMMU type id (%i)\n",
714                         iommu_type_id);
715         return -1;
716 }
717
718 int
719 rte_vfio_clear_group(int vfio_group_fd)
720 {
721         int i;
722         struct vfio_config *vfio_cfg;
723
724         vfio_cfg = get_vfio_cfg_by_group_fd(vfio_group_fd);
725         if (vfio_cfg == NULL) {
726                 RTE_LOG(ERR, EAL, "Invalid VFIO group fd!\n");
727                 return -1;
728         }
729
730         i = get_vfio_group_idx(vfio_group_fd);
731         if (i < 0)
732                 return -1;
733         vfio_cfg->vfio_groups[i].group_num = -1;
734         vfio_cfg->vfio_groups[i].fd = -1;
735         vfio_cfg->vfio_groups[i].devices = 0;
736         vfio_cfg->vfio_active_groups--;
737
738         return 0;
739 }
740
741 int
742 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
743                 int *vfio_dev_fd, struct vfio_device_info *device_info)
744 {
745         struct vfio_group_status group_status = {
746                         .argsz = sizeof(group_status)
747         };
748         struct vfio_config *vfio_cfg;
749         struct user_mem_maps *user_mem_maps;
750         int vfio_container_fd;
751         int vfio_group_fd;
752         int iommu_group_num;
753         rte_uuid_t vf_token;
754         int i, ret;
755         const struct internal_config *internal_conf =
756                 eal_get_internal_configuration();
757
758         /* get group number */
759         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
760         if (ret == 0) {
761                 RTE_LOG(NOTICE, EAL,
762                                 "%s not managed by VFIO driver, skipping\n",
763                                 dev_addr);
764                 return 1;
765         }
766
767         /* if negative, something failed */
768         if (ret < 0)
769                 return -1;
770
771         /* get the actual group fd */
772         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
773         if (vfio_group_fd < 0 && vfio_group_fd != -ENOENT)
774                 return -1;
775
776         /*
777          * if vfio_group_fd == -ENOENT, that means the device
778          * isn't managed by VFIO
779          */
780         if (vfio_group_fd == -ENOENT) {
781                 RTE_LOG(NOTICE, EAL,
782                                 "%s not managed by VFIO driver, skipping\n",
783                                 dev_addr);
784                 return 1;
785         }
786
787         /*
788          * at this point, we know that this group is viable (meaning, all devices
789          * are either bound to VFIO or not bound to anything)
790          */
791
792         /* check if the group is viable */
793         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
794         if (ret) {
795                 RTE_LOG(ERR, EAL, "%s cannot get VFIO group status, "
796                         "error %i (%s)\n", dev_addr, errno, strerror(errno));
797                 close(vfio_group_fd);
798                 rte_vfio_clear_group(vfio_group_fd);
799                 return -1;
800         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
801                 RTE_LOG(ERR, EAL, "%s VFIO group is not viable! "
802                         "Not all devices in IOMMU group bound to VFIO or unbound\n",
803                         dev_addr);
804                 close(vfio_group_fd);
805                 rte_vfio_clear_group(vfio_group_fd);
806                 return -1;
807         }
808
809         /* get the vfio_config it belongs to */
810         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
811         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
812         vfio_container_fd = vfio_cfg->vfio_container_fd;
813         user_mem_maps = &vfio_cfg->mem_maps;
814
815         /* check if group does not have a container yet */
816         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
817
818                 /* add group to a container */
819                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
820                                 &vfio_container_fd);
821                 if (ret) {
822                         RTE_LOG(ERR, EAL,
823                                 "%s cannot add VFIO group to container, error "
824                                 "%i (%s)\n", dev_addr, errno, strerror(errno));
825                         close(vfio_group_fd);
826                         rte_vfio_clear_group(vfio_group_fd);
827                         return -1;
828                 }
829
830                 /*
831                  * pick an IOMMU type and set up DMA mappings for container
832                  *
833                  * needs to be done only once, only when first group is
834                  * assigned to a container and only in primary process.
835                  * Note this can happen several times with the hotplug
836                  * functionality.
837                  */
838                 if (internal_conf->process_type == RTE_PROC_PRIMARY &&
839                                 vfio_cfg->vfio_active_groups == 1 &&
840                                 vfio_group_device_count(vfio_group_fd) == 0) {
841                         const struct vfio_iommu_type *t;
842
843                         /* select an IOMMU type which we will be using */
844                         t = vfio_set_iommu_type(vfio_container_fd);
845                         if (!t) {
846                                 RTE_LOG(ERR, EAL,
847                                         "%s failed to select IOMMU type\n",
848                                         dev_addr);
849                                 close(vfio_group_fd);
850                                 rte_vfio_clear_group(vfio_group_fd);
851                                 return -1;
852                         }
853                         /* lock memory hotplug before mapping and release it
854                          * after registering callback, to prevent races
855                          */
856                         rte_mcfg_mem_read_lock();
857                         if (vfio_cfg == default_vfio_cfg)
858                                 ret = t->dma_map_func(vfio_container_fd);
859                         else
860                                 ret = 0;
861                         if (ret) {
862                                 RTE_LOG(ERR, EAL,
863                                         "%s DMA remapping failed, error "
864                                         "%i (%s)\n",
865                                         dev_addr, errno, strerror(errno));
866                                 close(vfio_group_fd);
867                                 rte_vfio_clear_group(vfio_group_fd);
868                                 rte_mcfg_mem_read_unlock();
869                                 return -1;
870                         }
871
872                         vfio_cfg->vfio_iommu_type = t;
873
874                         /* re-map all user-mapped segments */
875                         rte_spinlock_recursive_lock(&user_mem_maps->lock);
876
877                         /* this IOMMU type may not support DMA mapping, but
878                          * if we have mappings in the list - that means we have
879                          * previously mapped something successfully, so we can
880                          * be sure that DMA mapping is supported.
881                          */
882                         for (i = 0; i < user_mem_maps->n_maps; i++) {
883                                 struct user_mem_map *map;
884                                 map = &user_mem_maps->maps[i];
885
886                                 ret = t->dma_user_map_func(
887                                                 vfio_container_fd,
888                                                 map->addr, map->iova, map->len,
889                                                 1);
890                                 if (ret) {
891                                         RTE_LOG(ERR, EAL, "Couldn't map user memory for DMA: "
892                                                         "va: 0x%" PRIx64 " "
893                                                         "iova: 0x%" PRIx64 " "
894                                                         "len: 0x%" PRIu64 "\n",
895                                                         map->addr, map->iova,
896                                                         map->len);
897                                         rte_spinlock_recursive_unlock(
898                                                         &user_mem_maps->lock);
899                                         rte_mcfg_mem_read_unlock();
900                                         return -1;
901                                 }
902                         }
903                         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
904
905                         /* register callback for mem events */
906                         if (vfio_cfg == default_vfio_cfg)
907                                 ret = rte_mem_event_callback_register(
908                                         VFIO_MEM_EVENT_CLB_NAME,
909                                         vfio_mem_event_callback, NULL);
910                         else
911                                 ret = 0;
912                         /* unlock memory hotplug */
913                         rte_mcfg_mem_read_unlock();
914
915                         if (ret && rte_errno != ENOTSUP) {
916                                 RTE_LOG(ERR, EAL, "Could not install memory event callback for VFIO\n");
917                                 return -1;
918                         }
919                         if (ret)
920                                 RTE_LOG(DEBUG, EAL, "Memory event callbacks not supported\n");
921                         else
922                                 RTE_LOG(DEBUG, EAL, "Installed memory event callback for VFIO\n");
923                 }
924         } else if (rte_eal_process_type() != RTE_PROC_PRIMARY &&
925                         vfio_cfg == default_vfio_cfg &&
926                         vfio_cfg->vfio_iommu_type == NULL) {
927                 /* if we're not a primary process, we do not set up the VFIO
928                  * container because it's already been set up by the primary
929                  * process. instead, we simply ask the primary about VFIO type
930                  * we are using, and set the VFIO config up appropriately.
931                  */
932                 ret = vfio_sync_default_container();
933                 if (ret < 0) {
934                         RTE_LOG(ERR, EAL, "Could not sync default VFIO container\n");
935                         close(vfio_group_fd);
936                         rte_vfio_clear_group(vfio_group_fd);
937                         return -1;
938                 }
939                 /* we have successfully initialized VFIO, notify user */
940                 const struct vfio_iommu_type *t =
941                                 default_vfio_cfg->vfio_iommu_type;
942                 RTE_LOG(INFO, EAL, "Using IOMMU type %d (%s)\n",
943                                 t->type_id, t->name);
944         }
945
946         rte_eal_vfio_get_vf_token(vf_token);
947
948         /* get a file descriptor for the device with VF token firstly */
949         if (!rte_uuid_is_null(vf_token)) {
950                 char vf_token_str[RTE_UUID_STRLEN];
951                 char dev[PATH_MAX];
952
953                 rte_uuid_unparse(vf_token, vf_token_str, sizeof(vf_token_str));
954                 snprintf(dev, sizeof(dev),
955                          "%s vf_token=%s", dev_addr, vf_token_str);
956
957                 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD,
958                                      dev);
959                 if (*vfio_dev_fd >= 0)
960                         goto dev_get_info;
961         }
962
963         /* get a file descriptor for the device */
964         *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
965         if (*vfio_dev_fd < 0) {
966                 /* if we cannot get a device fd, this implies a problem with
967                  * the VFIO group or the container not having IOMMU configured.
968                  */
969
970                 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
971                                 dev_addr);
972                 close(vfio_group_fd);
973                 rte_vfio_clear_group(vfio_group_fd);
974                 return -1;
975         }
976
977         /* test and setup the device */
978 dev_get_info:
979         ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
980         if (ret) {
981                 RTE_LOG(ERR, EAL, "%s cannot get device info, "
982                                 "error %i (%s)\n", dev_addr, errno,
983                                 strerror(errno));
984                 close(*vfio_dev_fd);
985                 close(vfio_group_fd);
986                 rte_vfio_clear_group(vfio_group_fd);
987                 return -1;
988         }
989         vfio_group_device_get(vfio_group_fd);
990
991         return 0;
992 }
993
994 int
995 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
996                     int vfio_dev_fd)
997 {
998         struct vfio_config *vfio_cfg;
999         int vfio_group_fd;
1000         int iommu_group_num;
1001         int ret;
1002
1003         /* we don't want any DMA mapping messages to come while we're detaching
1004          * VFIO device, because this might be the last device and we might need
1005          * to unregister the callback.
1006          */
1007         rte_mcfg_mem_read_lock();
1008
1009         /* get group number */
1010         ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_num);
1011         if (ret <= 0) {
1012                 RTE_LOG(WARNING, EAL, "%s not managed by VFIO driver\n",
1013                         dev_addr);
1014                 /* This is an error at this point. */
1015                 ret = -1;
1016                 goto out;
1017         }
1018
1019         /* get the actual group fd */
1020         vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
1021         if (vfio_group_fd < 0) {
1022                 RTE_LOG(INFO, EAL, "rte_vfio_get_group_fd failed for %s\n",
1023                                    dev_addr);
1024                 ret = vfio_group_fd;
1025                 goto out;
1026         }
1027
1028         /* get the vfio_config it belongs to */
1029         vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
1030         vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
1031
1032         /* At this point we got an active group. Closing it will make the
1033          * container detachment. If this is the last active group, VFIO kernel
1034          * code will unset the container and the IOMMU mappings.
1035          */
1036
1037         /* Closing a device */
1038         if (close(vfio_dev_fd) < 0) {
1039                 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
1040                                    dev_addr);
1041                 ret = -1;
1042                 goto out;
1043         }
1044
1045         /* An VFIO group can have several devices attached. Just when there is
1046          * no devices remaining should the group be closed.
1047          */
1048         vfio_group_device_put(vfio_group_fd);
1049         if (!vfio_group_device_count(vfio_group_fd)) {
1050
1051                 if (close(vfio_group_fd) < 0) {
1052                         RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
1053                                 dev_addr);
1054                         ret = -1;
1055                         goto out;
1056                 }
1057
1058                 if (rte_vfio_clear_group(vfio_group_fd) < 0) {
1059                         RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
1060                                            dev_addr);
1061                         ret = -1;
1062                         goto out;
1063                 }
1064         }
1065
1066         /* if there are no active device groups, unregister the callback to
1067          * avoid spurious attempts to map/unmap memory from VFIO.
1068          */
1069         if (vfio_cfg == default_vfio_cfg && vfio_cfg->vfio_active_groups == 0 &&
1070                         rte_eal_process_type() != RTE_PROC_SECONDARY)
1071                 rte_mem_event_callback_unregister(VFIO_MEM_EVENT_CLB_NAME,
1072                                 NULL);
1073
1074         /* success */
1075         ret = 0;
1076
1077 out:
1078         rte_mcfg_mem_read_unlock();
1079         return ret;
1080 }
1081
1082 int
1083 rte_vfio_enable(const char *modname)
1084 {
1085         /* initialize group list */
1086         int i, j;
1087         int vfio_available;
1088         const struct internal_config *internal_conf =
1089                 eal_get_internal_configuration();
1090
1091         rte_spinlock_recursive_t lock = RTE_SPINLOCK_RECURSIVE_INITIALIZER;
1092
1093         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
1094                 vfio_cfgs[i].vfio_container_fd = -1;
1095                 vfio_cfgs[i].vfio_active_groups = 0;
1096                 vfio_cfgs[i].vfio_iommu_type = NULL;
1097                 vfio_cfgs[i].mem_maps.lock = lock;
1098
1099                 for (j = 0; j < VFIO_MAX_GROUPS; j++) {
1100                         vfio_cfgs[i].vfio_groups[j].fd = -1;
1101                         vfio_cfgs[i].vfio_groups[j].group_num = -1;
1102                         vfio_cfgs[i].vfio_groups[j].devices = 0;
1103                 }
1104         }
1105
1106         RTE_LOG(DEBUG, EAL, "Probing VFIO support...\n");
1107
1108         /* check if vfio module is loaded */
1109         vfio_available = rte_eal_check_module(modname);
1110
1111         /* return error directly */
1112         if (vfio_available == -1) {
1113                 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
1114                 return -1;
1115         }
1116
1117         /* return 0 if VFIO modules not loaded */
1118         if (vfio_available == 0) {
1119                 RTE_LOG(DEBUG, EAL,
1120                         "VFIO modules not loaded, skipping VFIO support...\n");
1121                 return 0;
1122         }
1123
1124         if (internal_conf->process_type == RTE_PROC_PRIMARY) {
1125                 /* open a new container */
1126                 default_vfio_cfg->vfio_container_fd =
1127                                 rte_vfio_get_container_fd();
1128         } else {
1129                 /* get the default container from the primary process */
1130                 default_vfio_cfg->vfio_container_fd =
1131                                 vfio_get_default_container_fd();
1132         }
1133
1134         /* check if we have VFIO driver enabled */
1135         if (default_vfio_cfg->vfio_container_fd != -1) {
1136                 RTE_LOG(INFO, EAL, "VFIO support initialized\n");
1137                 default_vfio_cfg->vfio_enabled = 1;
1138         } else {
1139                 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
1140         }
1141
1142         return 0;
1143 }
1144
1145 int
1146 rte_vfio_is_enabled(const char *modname)
1147 {
1148         const int mod_available = rte_eal_check_module(modname) > 0;
1149         return default_vfio_cfg->vfio_enabled && mod_available;
1150 }
1151
1152 int
1153 vfio_get_default_container_fd(void)
1154 {
1155         struct rte_mp_msg mp_req, *mp_rep;
1156         struct rte_mp_reply mp_reply = {0};
1157         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
1158         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
1159         int container_fd;
1160         const struct internal_config *internal_conf =
1161                 eal_get_internal_configuration();
1162
1163         if (default_vfio_cfg->vfio_enabled)
1164                 return default_vfio_cfg->vfio_container_fd;
1165
1166         if (internal_conf->process_type == RTE_PROC_PRIMARY) {
1167                 /* if we were secondary process we would try requesting
1168                  * container fd from the primary, but we're the primary
1169                  * process so just exit here
1170                  */
1171                 return -1;
1172         }
1173
1174         p->req = SOCKET_REQ_DEFAULT_CONTAINER;
1175         strcpy(mp_req.name, EAL_VFIO_MP);
1176         mp_req.len_param = sizeof(*p);
1177         mp_req.num_fds = 0;
1178
1179         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1180             mp_reply.nb_received == 1) {
1181                 mp_rep = &mp_reply.msgs[0];
1182                 p = (struct vfio_mp_param *)mp_rep->param;
1183                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1184                         container_fd = mp_rep->fds[0];
1185                         free(mp_reply.msgs);
1186                         return container_fd;
1187                 }
1188         }
1189
1190         free(mp_reply.msgs);
1191         RTE_LOG(ERR, EAL, "Cannot request default VFIO container fd\n");
1192         return -1;
1193 }
1194
1195 int
1196 vfio_get_iommu_type(void)
1197 {
1198         if (default_vfio_cfg->vfio_iommu_type == NULL)
1199                 return -1;
1200
1201         return default_vfio_cfg->vfio_iommu_type->type_id;
1202 }
1203
1204 const struct vfio_iommu_type *
1205 vfio_set_iommu_type(int vfio_container_fd)
1206 {
1207         unsigned idx;
1208         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
1209                 const struct vfio_iommu_type *t = &iommu_types[idx];
1210
1211                 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
1212                                 t->type_id);
1213                 if (!ret) {
1214                         RTE_LOG(INFO, EAL, "Using IOMMU type %d (%s)\n",
1215                                         t->type_id, t->name);
1216                         return t;
1217                 }
1218                 /* not an error, there may be more supported IOMMU types */
1219                 RTE_LOG(DEBUG, EAL, "Set IOMMU type %d (%s) failed, error "
1220                                 "%i (%s)\n", t->type_id, t->name, errno,
1221                                 strerror(errno));
1222         }
1223         /* if we didn't find a suitable IOMMU type, fail */
1224         return NULL;
1225 }
1226
1227 int
1228 vfio_has_supported_extensions(int vfio_container_fd)
1229 {
1230         int ret;
1231         unsigned idx, n_extensions = 0;
1232         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
1233                 const struct vfio_iommu_type *t = &iommu_types[idx];
1234
1235                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
1236                                 t->type_id);
1237                 if (ret < 0) {
1238                         RTE_LOG(ERR, EAL, "Could not get IOMMU type, error "
1239                                         "%i (%s)\n", errno, strerror(errno));
1240                         close(vfio_container_fd);
1241                         return -1;
1242                 } else if (ret == 1) {
1243                         /* we found a supported extension */
1244                         n_extensions++;
1245                 }
1246                 RTE_LOG(DEBUG, EAL, "IOMMU type %d (%s) is %s\n",
1247                                 t->type_id, t->name,
1248                                 ret ? "supported" : "not supported");
1249         }
1250
1251         /* if we didn't find any supported IOMMU types, fail */
1252         if (!n_extensions) {
1253                 close(vfio_container_fd);
1254                 return -1;
1255         }
1256
1257         return 0;
1258 }
1259
1260 int
1261 rte_vfio_get_container_fd(void)
1262 {
1263         int ret, vfio_container_fd;
1264         struct rte_mp_msg mp_req, *mp_rep;
1265         struct rte_mp_reply mp_reply = {0};
1266         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
1267         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
1268         const struct internal_config *internal_conf =
1269                 eal_get_internal_configuration();
1270
1271
1272         /* if we're in a primary process, try to open the container */
1273         if (internal_conf->process_type == RTE_PROC_PRIMARY) {
1274                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
1275                 if (vfio_container_fd < 0) {
1276                         RTE_LOG(ERR, EAL,
1277                                         "Cannot open VFIO container %s, error "
1278                                         "%i (%s)\n", VFIO_CONTAINER_PATH,
1279                                         errno, strerror(errno));
1280                         return -1;
1281                 }
1282
1283                 /* check VFIO API version */
1284                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
1285                 if (ret != VFIO_API_VERSION) {
1286                         if (ret < 0)
1287                                 RTE_LOG(ERR, EAL,
1288                                         "Could not get VFIO API version, error "
1289                                         "%i (%s)\n", errno, strerror(errno));
1290                         else
1291                                 RTE_LOG(ERR, EAL, "Unsupported VFIO API version!\n");
1292                         close(vfio_container_fd);
1293                         return -1;
1294                 }
1295
1296                 ret = vfio_has_supported_extensions(vfio_container_fd);
1297                 if (ret) {
1298                         RTE_LOG(ERR, EAL,
1299                                 "No supported IOMMU extensions found!\n");
1300                         return -1;
1301                 }
1302
1303                 return vfio_container_fd;
1304         }
1305         /*
1306          * if we're in a secondary process, request container fd from the
1307          * primary process via mp channel
1308          */
1309         p->req = SOCKET_REQ_CONTAINER;
1310         strcpy(mp_req.name, EAL_VFIO_MP);
1311         mp_req.len_param = sizeof(*p);
1312         mp_req.num_fds = 0;
1313
1314         vfio_container_fd = -1;
1315         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
1316             mp_reply.nb_received == 1) {
1317                 mp_rep = &mp_reply.msgs[0];
1318                 p = (struct vfio_mp_param *)mp_rep->param;
1319                 if (p->result == SOCKET_OK && mp_rep->num_fds == 1) {
1320                         vfio_container_fd = mp_rep->fds[0];
1321                         free(mp_reply.msgs);
1322                         return vfio_container_fd;
1323                 }
1324         }
1325
1326         free(mp_reply.msgs);
1327         RTE_LOG(ERR, EAL, "Cannot request VFIO container fd\n");
1328         return -1;
1329 }
1330
1331 int
1332 rte_vfio_get_group_num(const char *sysfs_base,
1333                 const char *dev_addr, int *iommu_group_num)
1334 {
1335         char linkname[PATH_MAX];
1336         char filename[PATH_MAX];
1337         char *tok[16], *group_tok, *end;
1338         int ret;
1339
1340         memset(linkname, 0, sizeof(linkname));
1341         memset(filename, 0, sizeof(filename));
1342
1343         /* try to find out IOMMU group for this device */
1344         snprintf(linkname, sizeof(linkname),
1345                          "%s/%s/iommu_group", sysfs_base, dev_addr);
1346
1347         ret = readlink(linkname, filename, sizeof(filename));
1348
1349         /* if the link doesn't exist, no VFIO for us */
1350         if (ret < 0)
1351                 return 0;
1352
1353         ret = rte_strsplit(filename, sizeof(filename),
1354                         tok, RTE_DIM(tok), '/');
1355
1356         if (ret <= 0) {
1357                 RTE_LOG(ERR, EAL, "%s cannot get IOMMU group\n", dev_addr);
1358                 return -1;
1359         }
1360
1361         /* IOMMU group is always the last token */
1362         errno = 0;
1363         group_tok = tok[ret - 1];
1364         end = group_tok;
1365         *iommu_group_num = strtol(group_tok, &end, 10);
1366         if ((end != group_tok && *end != '\0') || errno != 0) {
1367                 RTE_LOG(ERR, EAL, "%s error parsing IOMMU number!\n", dev_addr);
1368                 return -1;
1369         }
1370
1371         return 1;
1372 }
1373
1374 static int
1375 type1_map_contig(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1376                 size_t len, void *arg)
1377 {
1378         int *vfio_container_fd = arg;
1379
1380         if (msl->external)
1381                 return 0;
1382
1383         return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1384                         len, 1);
1385 }
1386
1387 static int
1388 type1_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1389                 void *arg)
1390 {
1391         int *vfio_container_fd = arg;
1392
1393         /* skip external memory that isn't a heap */
1394         if (msl->external && !msl->heap)
1395                 return 0;
1396
1397         /* skip any segments with invalid IOVA addresses */
1398         if (ms->iova == RTE_BAD_IOVA)
1399                 return 0;
1400
1401         /* if IOVA mode is VA, we've already mapped the internal segments */
1402         if (!msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
1403                 return 0;
1404
1405         return vfio_type1_dma_mem_map(*vfio_container_fd, ms->addr_64, ms->iova,
1406                         ms->len, 1);
1407 }
1408
1409 static int
1410 vfio_type1_dma_mem_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1411                 uint64_t len, int do_map)
1412 {
1413         struct vfio_iommu_type1_dma_map dma_map;
1414         struct vfio_iommu_type1_dma_unmap dma_unmap;
1415         int ret;
1416
1417         if (do_map != 0) {
1418                 memset(&dma_map, 0, sizeof(dma_map));
1419                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1420                 dma_map.vaddr = vaddr;
1421                 dma_map.size = len;
1422                 dma_map.iova = iova;
1423                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1424                                 VFIO_DMA_MAP_FLAG_WRITE;
1425
1426                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1427                 if (ret) {
1428                         /**
1429                          * In case the mapping was already done EEXIST will be
1430                          * returned from kernel.
1431                          */
1432                         if (errno == EEXIST) {
1433                                 RTE_LOG(DEBUG, EAL,
1434                                         "Memory segment is already mapped, skipping");
1435                         } else {
1436                                 RTE_LOG(ERR, EAL,
1437                                         "Cannot set up DMA remapping, error "
1438                                         "%i (%s)\n", errno, strerror(errno));
1439                                 return -1;
1440                         }
1441                 }
1442         } else {
1443                 memset(&dma_unmap, 0, sizeof(dma_unmap));
1444                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1445                 dma_unmap.size = len;
1446                 dma_unmap.iova = iova;
1447
1448                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1449                                 &dma_unmap);
1450                 if (ret) {
1451                         RTE_LOG(ERR, EAL, "Cannot clear DMA remapping, error "
1452                                         "%i (%s)\n", errno, strerror(errno));
1453                         return -1;
1454                 } else if (dma_unmap.size != len) {
1455                         RTE_LOG(ERR, EAL, "Unexpected size %"PRIu64
1456                                 " of DMA remapping cleared instead of %"PRIu64"\n",
1457                                 (uint64_t)dma_unmap.size, len);
1458                         rte_errno = EIO;
1459                         return -1;
1460                 }
1461         }
1462
1463         return 0;
1464 }
1465
1466 static int
1467 vfio_type1_dma_map(int vfio_container_fd)
1468 {
1469         if (rte_eal_iova_mode() == RTE_IOVA_VA) {
1470                 /* with IOVA as VA mode, we can get away with mapping contiguous
1471                  * chunks rather than going page-by-page.
1472                  */
1473                 int ret = rte_memseg_contig_walk(type1_map_contig,
1474                                 &vfio_container_fd);
1475                 if (ret)
1476                         return ret;
1477                 /* we have to continue the walk because we've skipped the
1478                  * external segments during the config walk.
1479                  */
1480         }
1481         return rte_memseg_walk(type1_map, &vfio_container_fd);
1482 }
1483
1484 /* Track the size of the statically allocated DMA window for SPAPR */
1485 uint64_t spapr_dma_win_len;
1486 uint64_t spapr_dma_win_page_sz;
1487
1488 static int
1489 vfio_spapr_dma_do_map(int vfio_container_fd, uint64_t vaddr, uint64_t iova,
1490                 uint64_t len, int do_map)
1491 {
1492         struct vfio_iommu_spapr_register_memory reg = {
1493                 .argsz = sizeof(reg),
1494                 .vaddr = (uintptr_t) vaddr,
1495                 .size = len,
1496                 .flags = 0
1497         };
1498         int ret;
1499
1500         if (do_map != 0) {
1501                 struct vfio_iommu_type1_dma_map dma_map;
1502
1503                 if (iova + len > spapr_dma_win_len) {
1504                         RTE_LOG(ERR, EAL, "DMA map attempt outside DMA window\n");
1505                         return -1;
1506                 }
1507
1508                 ret = ioctl(vfio_container_fd,
1509                                 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, &reg);
1510                 if (ret) {
1511                         RTE_LOG(ERR, EAL,
1512                                 "Cannot register vaddr for IOMMU, error "
1513                                 "%i (%s)\n", errno, strerror(errno));
1514                         return -1;
1515                 }
1516
1517                 memset(&dma_map, 0, sizeof(dma_map));
1518                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
1519                 dma_map.vaddr = vaddr;
1520                 dma_map.size = len;
1521                 dma_map.iova = iova;
1522                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
1523                                 VFIO_DMA_MAP_FLAG_WRITE;
1524
1525                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
1526                 if (ret) {
1527                         RTE_LOG(ERR, EAL, "Cannot map vaddr for IOMMU, error "
1528                                         "%i (%s)\n", errno, strerror(errno));
1529                         return -1;
1530                 }
1531
1532         } else {
1533                 struct vfio_iommu_type1_dma_map dma_unmap;
1534
1535                 memset(&dma_unmap, 0, sizeof(dma_unmap));
1536                 dma_unmap.argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
1537                 dma_unmap.size = len;
1538                 dma_unmap.iova = iova;
1539
1540                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_UNMAP_DMA,
1541                                 &dma_unmap);
1542                 if (ret) {
1543                         RTE_LOG(ERR, EAL, "Cannot unmap vaddr for IOMMU, error "
1544                                         "%i (%s)\n", errno, strerror(errno));
1545                         return -1;
1546                 }
1547
1548                 ret = ioctl(vfio_container_fd,
1549                                 VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, &reg);
1550                 if (ret) {
1551                         RTE_LOG(ERR, EAL,
1552                                 "Cannot unregister vaddr for IOMMU, error "
1553                                 "%i (%s)\n", errno, strerror(errno));
1554                         return -1;
1555                 }
1556         }
1557
1558         return ret;
1559 }
1560
1561 static int
1562 vfio_spapr_map_walk(const struct rte_memseg_list *msl,
1563                 const struct rte_memseg *ms, void *arg)
1564 {
1565         int *vfio_container_fd = arg;
1566
1567         /* skip external memory that isn't a heap */
1568         if (msl->external && !msl->heap)
1569                 return 0;
1570
1571         /* skip any segments with invalid IOVA addresses */
1572         if (ms->iova == RTE_BAD_IOVA)
1573                 return 0;
1574
1575         return vfio_spapr_dma_do_map(*vfio_container_fd,
1576                 ms->addr_64, ms->iova, ms->len, 1);
1577 }
1578
1579 struct spapr_size_walk_param {
1580         uint64_t max_va;
1581         uint64_t page_sz;
1582         bool is_user_managed;
1583 };
1584
1585 /*
1586  * In order to set the DMA window size required for the SPAPR IOMMU
1587  * we need to walk the existing virtual memory allocations as well as
1588  * find the hugepage size used.
1589  */
1590 static int
1591 vfio_spapr_size_walk(const struct rte_memseg_list *msl, void *arg)
1592 {
1593         struct spapr_size_walk_param *param = arg;
1594         uint64_t max = (uint64_t) msl->base_va + (uint64_t) msl->len;
1595
1596         if (msl->external && !msl->heap) {
1597                 /* ignore user managed external memory */
1598                 param->is_user_managed = true;
1599                 return 0;
1600         }
1601
1602         if (max > param->max_va) {
1603                 param->page_sz = msl->page_sz;
1604                 param->max_va = max;
1605         }
1606
1607         return 0;
1608 }
1609
1610 /*
1611  * Find the highest memory address used in physical or virtual address
1612  * space and use that as the top of the DMA window.
1613  */
1614 static int
1615 find_highest_mem_addr(struct spapr_size_walk_param *param)
1616 {
1617         /* find the maximum IOVA address for setting the DMA window size */
1618         if (rte_eal_iova_mode() == RTE_IOVA_PA) {
1619                 static const char proc_iomem[] = "/proc/iomem";
1620                 static const char str_sysram[] = "System RAM";
1621                 uint64_t start, end, max = 0;
1622                 char *line = NULL;
1623                 char *dash, *space;
1624                 size_t line_len;
1625
1626                 /*
1627                  * Example "System RAM" in /proc/iomem:
1628                  * 00000000-1fffffffff : System RAM
1629                  * 200000000000-201fffffffff : System RAM
1630                  */
1631                 FILE *fd = fopen(proc_iomem, "r");
1632                 if (fd == NULL) {
1633                         RTE_LOG(ERR, EAL, "Cannot open %s\n", proc_iomem);
1634                         return -1;
1635                 }
1636                 /* Scan /proc/iomem for the highest PA in the system */
1637                 while (getline(&line, &line_len, fd) != -1) {
1638                         if (strstr(line, str_sysram) == NULL)
1639                                 continue;
1640
1641                         space = strstr(line, " ");
1642                         dash = strstr(line, "-");
1643
1644                         /* Validate the format of the memory string */
1645                         if (space == NULL || dash == NULL || space < dash) {
1646                                 RTE_LOG(ERR, EAL, "Can't parse line \"%s\" in file %s\n",
1647                                         line, proc_iomem);
1648                                 continue;
1649                         }
1650
1651                         start = strtoull(line, NULL, 16);
1652                         end   = strtoull(dash + 1, NULL, 16);
1653                         RTE_LOG(DEBUG, EAL, "Found system RAM from 0x%" PRIx64
1654                                 " to 0x%" PRIx64 "\n", start, end);
1655                         if (end > max)
1656                                 max = end;
1657                 }
1658                 free(line);
1659                 fclose(fd);
1660
1661                 if (max == 0) {
1662                         RTE_LOG(ERR, EAL, "Failed to find valid \"System RAM\" "
1663                                 "entry in file %s\n", proc_iomem);
1664                         return -1;
1665                 }
1666
1667                 spapr_dma_win_len = rte_align64pow2(max + 1);
1668                 return 0;
1669         } else if (rte_eal_iova_mode() == RTE_IOVA_VA) {
1670                 RTE_LOG(DEBUG, EAL, "Highest VA address in memseg list is 0x%"
1671                         PRIx64 "\n", param->max_va);
1672                 spapr_dma_win_len = rte_align64pow2(param->max_va);
1673                 return 0;
1674         }
1675
1676         spapr_dma_win_len = 0;
1677         RTE_LOG(ERR, EAL, "Unsupported IOVA mode\n");
1678         return -1;
1679 }
1680
1681
1682 /*
1683  * The SPAPRv2 IOMMU supports 2 DMA windows with starting
1684  * address at 0 or 1<<59.  By default, a DMA window is set
1685  * at address 0, 2GB long, with a 4KB page.  For DPDK we
1686  * must remove the default window and setup a new DMA window
1687  * based on the hugepage size and memory requirements of
1688  * the application before we can map memory for DMA.
1689  */
1690 static int
1691 spapr_dma_win_size(void)
1692 {
1693         struct spapr_size_walk_param param;
1694
1695         /* only create DMA window once */
1696         if (spapr_dma_win_len > 0)
1697                 return 0;
1698
1699         /* walk the memseg list to find the page size/max VA address */
1700         memset(&param, 0, sizeof(param));
1701         if (rte_memseg_list_walk(vfio_spapr_size_walk, &param) < 0) {
1702                 RTE_LOG(ERR, EAL, "Failed to walk memseg list for DMA window size\n");
1703                 return -1;
1704         }
1705
1706         /* we can't be sure if DMA window covers external memory */
1707         if (param.is_user_managed)
1708                 RTE_LOG(WARNING, EAL, "Detected user managed external memory which may not be managed by the IOMMU\n");
1709
1710         /* check physical/virtual memory size */
1711         if (find_highest_mem_addr(&param) < 0)
1712                 return -1;
1713         RTE_LOG(DEBUG, EAL, "Setting DMA window size to 0x%" PRIx64 "\n",
1714                 spapr_dma_win_len);
1715         spapr_dma_win_page_sz = param.page_sz;
1716         rte_mem_set_dma_mask(__builtin_ctzll(spapr_dma_win_len));
1717         return 0;
1718 }
1719
1720 static int
1721 vfio_spapr_create_dma_window(int vfio_container_fd)
1722 {
1723         struct vfio_iommu_spapr_tce_create create = {
1724                 .argsz = sizeof(create), };
1725         struct vfio_iommu_spapr_tce_remove remove = {
1726                 .argsz = sizeof(remove), };
1727         struct vfio_iommu_spapr_tce_info info = {
1728                 .argsz = sizeof(info), };
1729         int ret;
1730
1731         ret = spapr_dma_win_size();
1732         if (ret < 0)
1733                 return ret;
1734
1735         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1736         if (ret) {
1737                 RTE_LOG(ERR, EAL, "Cannot get IOMMU info, error %i (%s)\n",
1738                         errno, strerror(errno));
1739                 return -1;
1740         }
1741
1742         /*
1743          * sPAPR v1/v2 IOMMU always has a default 1G DMA window set.  The window
1744          * can't be changed for v1 but it can be changed for v2. Since DPDK only
1745          * supports v2, remove the default DMA window so it can be resized.
1746          */
1747         remove.start_addr = info.dma32_window_start;
1748         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
1749         if (ret)
1750                 return -1;
1751
1752         /* create a new DMA window (start address is not selectable) */
1753         create.window_size = spapr_dma_win_len;
1754         create.page_shift  = __builtin_ctzll(spapr_dma_win_page_sz);
1755         create.levels = 1;
1756         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
1757 #ifdef VFIO_IOMMU_SPAPR_INFO_DDW
1758         /*
1759          * The vfio_iommu_spapr_tce_info structure was modified in
1760          * Linux kernel 4.2.0 to add support for the
1761          * vfio_iommu_spapr_tce_ddw_info structure needed to try
1762          * multiple table levels.  Skip the attempt if running with
1763          * an older kernel.
1764          */
1765         if (ret) {
1766                 /* if at first we don't succeed, try more levels */
1767                 uint32_t levels;
1768
1769                 for (levels = create.levels + 1;
1770                         ret && levels <= info.ddw.levels; levels++) {
1771                         create.levels = levels;
1772                         ret = ioctl(vfio_container_fd,
1773                                 VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
1774                 }
1775         }
1776 #endif /* VFIO_IOMMU_SPAPR_INFO_DDW */
1777         if (ret) {
1778                 RTE_LOG(ERR, EAL, "Cannot create new DMA window, error "
1779                                 "%i (%s)\n", errno, strerror(errno));
1780                 RTE_LOG(ERR, EAL,
1781                         "Consider using a larger hugepage size if supported by the system\n");
1782                 return -1;
1783         }
1784
1785         /* verify the start address  */
1786         if (create.start_addr != 0) {
1787                 RTE_LOG(ERR, EAL, "Received unsupported start address 0x%"
1788                         PRIx64 "\n", (uint64_t)create.start_addr);
1789                 return -1;
1790         }
1791         return ret;
1792 }
1793
1794 static int
1795 vfio_spapr_dma_mem_map(int vfio_container_fd, uint64_t vaddr,
1796                 uint64_t iova, uint64_t len, int do_map)
1797 {
1798         int ret = 0;
1799
1800         if (do_map) {
1801                 if (vfio_spapr_dma_do_map(vfio_container_fd,
1802                         vaddr, iova, len, 1)) {
1803                         RTE_LOG(ERR, EAL, "Failed to map DMA\n");
1804                         ret = -1;
1805                 }
1806         } else {
1807                 if (vfio_spapr_dma_do_map(vfio_container_fd,
1808                         vaddr, iova, len, 0)) {
1809                         RTE_LOG(ERR, EAL, "Failed to unmap DMA\n");
1810                         ret = -1;
1811                 }
1812         }
1813
1814         return ret;
1815 }
1816
1817 static int
1818 vfio_spapr_dma_map(int vfio_container_fd)
1819 {
1820         if (vfio_spapr_create_dma_window(vfio_container_fd) < 0) {
1821                 RTE_LOG(ERR, EAL, "Could not create new DMA window!\n");
1822                 return -1;
1823         }
1824
1825         /* map all existing DPDK segments for DMA */
1826         if (rte_memseg_walk(vfio_spapr_map_walk, &vfio_container_fd) < 0)
1827                 return -1;
1828
1829         return 0;
1830 }
1831
1832 static int
1833 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
1834 {
1835         /* No-IOMMU mode does not need DMA mapping */
1836         return 0;
1837 }
1838
1839 static int
1840 vfio_noiommu_dma_mem_map(int __rte_unused vfio_container_fd,
1841                          uint64_t __rte_unused vaddr,
1842                          uint64_t __rte_unused iova, uint64_t __rte_unused len,
1843                          int __rte_unused do_map)
1844 {
1845         /* No-IOMMU mode does not need DMA mapping */
1846         return 0;
1847 }
1848
1849 static int
1850 vfio_dma_mem_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1851                 uint64_t len, int do_map)
1852 {
1853         const struct vfio_iommu_type *t = vfio_cfg->vfio_iommu_type;
1854
1855         if (!t) {
1856                 RTE_LOG(ERR, EAL, "VFIO support not initialized\n");
1857                 rte_errno = ENODEV;
1858                 return -1;
1859         }
1860
1861         if (!t->dma_user_map_func) {
1862                 RTE_LOG(ERR, EAL,
1863                         "VFIO custom DMA region mapping not supported by IOMMU %s\n",
1864                         t->name);
1865                 rte_errno = ENOTSUP;
1866                 return -1;
1867         }
1868
1869         return t->dma_user_map_func(vfio_cfg->vfio_container_fd, vaddr, iova,
1870                         len, do_map);
1871 }
1872
1873 static int
1874 container_dma_map(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1875                 uint64_t len)
1876 {
1877         struct user_mem_map *new_map;
1878         struct user_mem_maps *user_mem_maps;
1879         bool has_partial_unmap;
1880         int ret = 0;
1881
1882         user_mem_maps = &vfio_cfg->mem_maps;
1883         rte_spinlock_recursive_lock(&user_mem_maps->lock);
1884         if (user_mem_maps->n_maps == VFIO_MAX_USER_MEM_MAPS) {
1885                 RTE_LOG(ERR, EAL, "No more space for user mem maps\n");
1886                 rte_errno = ENOMEM;
1887                 ret = -1;
1888                 goto out;
1889         }
1890         /* map the entry */
1891         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 1)) {
1892                 /* technically, this will fail if there are currently no devices
1893                  * plugged in, even if a device were added later, this mapping
1894                  * might have succeeded. however, since we cannot verify if this
1895                  * is a valid mapping without having a device attached, consider
1896                  * this to be unsupported, because we can't just store any old
1897                  * mapping and pollute list of active mappings willy-nilly.
1898                  */
1899                 RTE_LOG(ERR, EAL, "Couldn't map new region for DMA\n");
1900                 ret = -1;
1901                 goto out;
1902         }
1903         /* do we have partial unmap support? */
1904         has_partial_unmap = vfio_cfg->vfio_iommu_type->partial_unmap;
1905
1906         /* create new user mem map entry */
1907         new_map = &user_mem_maps->maps[user_mem_maps->n_maps++];
1908         new_map->addr = vaddr;
1909         new_map->iova = iova;
1910         new_map->len = len;
1911         /* for IOMMU types supporting partial unmap, we don't need chunking */
1912         new_map->chunk = has_partial_unmap ? 0 : len;
1913
1914         compact_user_maps(user_mem_maps);
1915 out:
1916         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
1917         return ret;
1918 }
1919
1920 static int
1921 container_dma_unmap(struct vfio_config *vfio_cfg, uint64_t vaddr, uint64_t iova,
1922                 uint64_t len)
1923 {
1924         struct user_mem_map orig_maps[VFIO_MAX_USER_MEM_MAPS];
1925         struct user_mem_map new_maps[2]; /* can be at most 2 */
1926         struct user_mem_maps *user_mem_maps;
1927         int n_orig, n_new, newlen, ret = 0;
1928         bool has_partial_unmap;
1929
1930         user_mem_maps = &vfio_cfg->mem_maps;
1931         rte_spinlock_recursive_lock(&user_mem_maps->lock);
1932
1933         /*
1934          * Previously, we had adjacent mappings entirely contained within one
1935          * mapping entry. Since we now store original mapping length in some
1936          * cases, this is no longer the case, so unmapping can potentially go
1937          * over multiple segments and split them in any number of ways.
1938          *
1939          * To complicate things further, some IOMMU types support arbitrary
1940          * partial unmapping, while others will only support unmapping along the
1941          * chunk size, so there are a lot of cases we need to handle. To make
1942          * things easier code wise, instead of trying to adjust existing
1943          * mappings, let's just rebuild them using information we have.
1944          */
1945
1946         /*
1947          * first thing to do is check if there exists a mapping that includes
1948          * the start and the end of our requested unmap. We need to collect all
1949          * maps that include our unmapped region.
1950          */
1951         n_orig = find_user_mem_maps(user_mem_maps, vaddr, iova, len,
1952                         orig_maps, RTE_DIM(orig_maps));
1953         /* did we find anything? */
1954         if (n_orig < 0) {
1955                 RTE_LOG(ERR, EAL, "Couldn't find previously mapped region\n");
1956                 rte_errno = EINVAL;
1957                 ret = -1;
1958                 goto out;
1959         }
1960
1961         /* do we have partial unmap capability? */
1962         has_partial_unmap = vfio_cfg->vfio_iommu_type->partial_unmap;
1963
1964         /*
1965          * if we don't support partial unmap, we must check if start and end of
1966          * current unmap region are chunk-aligned.
1967          */
1968         if (!has_partial_unmap) {
1969                 bool start_aligned, end_aligned;
1970
1971                 start_aligned = addr_is_chunk_aligned(orig_maps, n_orig,
1972                                 vaddr, iova);
1973                 end_aligned = addr_is_chunk_aligned(orig_maps, n_orig,
1974                                 vaddr + len, iova + len);
1975
1976                 if (!start_aligned || !end_aligned) {
1977                         RTE_LOG(DEBUG, EAL, "DMA partial unmap unsupported\n");
1978                         rte_errno = ENOTSUP;
1979                         ret = -1;
1980                         goto out;
1981                 }
1982         }
1983
1984         /*
1985          * now we know we can potentially unmap the region, but we still have to
1986          * figure out if there is enough space in our list to store remaining
1987          * maps. for this, we will figure out how many segments we are going to
1988          * remove, and how many new segments we are going to create.
1989          */
1990         n_new = process_maps(orig_maps, n_orig, new_maps, vaddr, len);
1991
1992         /* can we store the new maps in our list? */
1993         newlen = (user_mem_maps->n_maps - n_orig) + n_new;
1994         if (newlen >= VFIO_MAX_USER_MEM_MAPS) {
1995                 RTE_LOG(ERR, EAL, "Not enough space to store partial mapping\n");
1996                 rte_errno = ENOMEM;
1997                 ret = -1;
1998                 goto out;
1999         }
2000
2001         /* unmap the entry */
2002         if (vfio_dma_mem_map(vfio_cfg, vaddr, iova, len, 0)) {
2003                 /* there may not be any devices plugged in, so unmapping will
2004                  * fail with ENODEV/ENOTSUP rte_errno values, but that doesn't
2005                  * stop us from removing the mapping, as the assumption is we
2006                  * won't be needing this memory any more and thus will want to
2007                  * prevent it from being remapped again on hotplug. so, only
2008                  * fail if we indeed failed to unmap (e.g. if the mapping was
2009                  * within our mapped range but had invalid alignment).
2010                  */
2011                 if (rte_errno != ENODEV && rte_errno != ENOTSUP) {
2012                         RTE_LOG(ERR, EAL, "Couldn't unmap region for DMA\n");
2013                         ret = -1;
2014                         goto out;
2015                 } else {
2016                         RTE_LOG(DEBUG, EAL, "DMA unmapping failed, but removing mappings anyway\n");
2017                 }
2018         }
2019
2020         /* we have unmapped the region, so now update the maps */
2021         delete_maps(user_mem_maps, orig_maps, n_orig);
2022         copy_maps(user_mem_maps, new_maps, n_new);
2023         compact_user_maps(user_mem_maps);
2024 out:
2025         rte_spinlock_recursive_unlock(&user_mem_maps->lock);
2026         return ret;
2027 }
2028
2029 int
2030 rte_vfio_noiommu_is_enabled(void)
2031 {
2032         int fd;
2033         ssize_t cnt;
2034         char c;
2035
2036         fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
2037         if (fd < 0) {
2038                 if (errno != ENOENT) {
2039                         RTE_LOG(ERR, EAL, "Cannot open VFIO noiommu file "
2040                                         "%i (%s)\n", errno, strerror(errno));
2041                         return -1;
2042                 }
2043                 /*
2044                  * else the file does not exists
2045                  * i.e. noiommu is not enabled
2046                  */
2047                 return 0;
2048         }
2049
2050         cnt = read(fd, &c, 1);
2051         close(fd);
2052         if (cnt != 1) {
2053                 RTE_LOG(ERR, EAL, "Unable to read from VFIO noiommu file "
2054                                 "%i (%s)\n", errno, strerror(errno));
2055                 return -1;
2056         }
2057
2058         return c == 'Y';
2059 }
2060
2061 int
2062 rte_vfio_container_create(void)
2063 {
2064         int i;
2065
2066         /* Find an empty slot to store new vfio config */
2067         for (i = 1; i < VFIO_MAX_CONTAINERS; i++) {
2068                 if (vfio_cfgs[i].vfio_container_fd == -1)
2069                         break;
2070         }
2071
2072         if (i == VFIO_MAX_CONTAINERS) {
2073                 RTE_LOG(ERR, EAL, "Exceed max VFIO container limit\n");
2074                 return -1;
2075         }
2076
2077         vfio_cfgs[i].vfio_container_fd = rte_vfio_get_container_fd();
2078         if (vfio_cfgs[i].vfio_container_fd < 0) {
2079                 RTE_LOG(NOTICE, EAL, "Fail to create a new VFIO container\n");
2080                 return -1;
2081         }
2082
2083         return vfio_cfgs[i].vfio_container_fd;
2084 }
2085
2086 int
2087 rte_vfio_container_destroy(int container_fd)
2088 {
2089         struct vfio_config *vfio_cfg;
2090         int i;
2091
2092         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2093         if (vfio_cfg == NULL) {
2094                 RTE_LOG(ERR, EAL, "Invalid VFIO container fd\n");
2095                 return -1;
2096         }
2097
2098         for (i = 0; i < VFIO_MAX_GROUPS; i++)
2099                 if (vfio_cfg->vfio_groups[i].group_num != -1)
2100                         rte_vfio_container_group_unbind(container_fd,
2101                                 vfio_cfg->vfio_groups[i].group_num);
2102
2103         close(container_fd);
2104         vfio_cfg->vfio_container_fd = -1;
2105         vfio_cfg->vfio_active_groups = 0;
2106         vfio_cfg->vfio_iommu_type = NULL;
2107
2108         return 0;
2109 }
2110
2111 int
2112 rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
2113 {
2114         struct vfio_config *vfio_cfg;
2115
2116         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2117         if (vfio_cfg == NULL) {
2118                 RTE_LOG(ERR, EAL, "Invalid VFIO container fd\n");
2119                 return -1;
2120         }
2121
2122         return vfio_get_group_fd(vfio_cfg, iommu_group_num);
2123 }
2124
2125 int
2126 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num)
2127 {
2128         struct vfio_config *vfio_cfg;
2129         struct vfio_group *cur_grp = NULL;
2130         int i;
2131
2132         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2133         if (vfio_cfg == NULL) {
2134                 RTE_LOG(ERR, EAL, "Invalid VFIO container fd\n");
2135                 return -1;
2136         }
2137
2138         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
2139                 if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num) {
2140                         cur_grp = &vfio_cfg->vfio_groups[i];
2141                         break;
2142                 }
2143         }
2144
2145         /* This should not happen */
2146         if (i == VFIO_MAX_GROUPS || cur_grp == NULL) {
2147                 RTE_LOG(ERR, EAL, "Specified VFIO group number not found\n");
2148                 return -1;
2149         }
2150
2151         if (cur_grp->fd >= 0 && close(cur_grp->fd) < 0) {
2152                 RTE_LOG(ERR, EAL,
2153                         "Error when closing vfio_group_fd for iommu_group_num "
2154                         "%d\n", iommu_group_num);
2155                 return -1;
2156         }
2157         cur_grp->group_num = -1;
2158         cur_grp->fd = -1;
2159         cur_grp->devices = 0;
2160         vfio_cfg->vfio_active_groups--;
2161
2162         return 0;
2163 }
2164
2165 int
2166 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova,
2167                 uint64_t len)
2168 {
2169         struct vfio_config *vfio_cfg;
2170
2171         if (len == 0) {
2172                 rte_errno = EINVAL;
2173                 return -1;
2174         }
2175
2176         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2177         if (vfio_cfg == NULL) {
2178                 RTE_LOG(ERR, EAL, "Invalid VFIO container fd\n");
2179                 return -1;
2180         }
2181
2182         return container_dma_map(vfio_cfg, vaddr, iova, len);
2183 }
2184
2185 int
2186 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova,
2187                 uint64_t len)
2188 {
2189         struct vfio_config *vfio_cfg;
2190
2191         if (len == 0) {
2192                 rte_errno = EINVAL;
2193                 return -1;
2194         }
2195
2196         vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
2197         if (vfio_cfg == NULL) {
2198                 RTE_LOG(ERR, EAL, "Invalid VFIO container fd\n");
2199                 return -1;
2200         }
2201
2202         return container_dma_unmap(vfio_cfg, vaddr, iova, len);
2203 }
2204
2205 #else
2206
2207 int
2208 rte_vfio_setup_device(__rte_unused const char *sysfs_base,
2209                 __rte_unused const char *dev_addr,
2210                 __rte_unused int *vfio_dev_fd,
2211                 __rte_unused struct vfio_device_info *device_info)
2212 {
2213         return -1;
2214 }
2215
2216 int
2217 rte_vfio_release_device(__rte_unused const char *sysfs_base,
2218                 __rte_unused const char *dev_addr, __rte_unused int fd)
2219 {
2220         return -1;
2221 }
2222
2223 int
2224 rte_vfio_enable(__rte_unused const char *modname)
2225 {
2226         return -1;
2227 }
2228
2229 int
2230 rte_vfio_is_enabled(__rte_unused const char *modname)
2231 {
2232         return -1;
2233 }
2234
2235 int
2236 rte_vfio_noiommu_is_enabled(void)
2237 {
2238         return -1;
2239 }
2240
2241 int
2242 rte_vfio_clear_group(__rte_unused int vfio_group_fd)
2243 {
2244         return -1;
2245 }
2246
2247 int
2248 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
2249                 __rte_unused const char *dev_addr,
2250                 __rte_unused int *iommu_group_num)
2251 {
2252         return -1;
2253 }
2254
2255 int
2256 rte_vfio_get_container_fd(void)
2257 {
2258         return -1;
2259 }
2260
2261 int
2262 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
2263 {
2264         return -1;
2265 }
2266
2267 int
2268 rte_vfio_container_create(void)
2269 {
2270         return -1;
2271 }
2272
2273 int
2274 rte_vfio_container_destroy(__rte_unused int container_fd)
2275 {
2276         return -1;
2277 }
2278
2279 int
2280 rte_vfio_container_group_bind(__rte_unused int container_fd,
2281                 __rte_unused int iommu_group_num)
2282 {
2283         return -1;
2284 }
2285
2286 int
2287 rte_vfio_container_group_unbind(__rte_unused int container_fd,
2288                 __rte_unused int iommu_group_num)
2289 {
2290         return -1;
2291 }
2292
2293 int
2294 rte_vfio_container_dma_map(__rte_unused int container_fd,
2295                 __rte_unused uint64_t vaddr,
2296                 __rte_unused uint64_t iova,
2297                 __rte_unused uint64_t len)
2298 {
2299         return -1;
2300 }
2301
2302 int
2303 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
2304                 __rte_unused uint64_t vaddr,
2305                 __rte_unused uint64_t iova,
2306                 __rte_unused uint64_t len)
2307 {
2308         return -1;
2309 }
2310
2311 #endif /* VFIO_PRESENT */