53ac725d22e059e3d94130108474ed7e2453d209
[deb_dpdk.git] / lib / librte_eal / linuxapp / eal / eal_vfio.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <sys/ioctl.h>
38
39 #include <rte_log.h>
40 #include <rte_memory.h>
41 #include <rte_eal_memconfig.h>
42
43 #include "eal_filesystem.h"
44 #include "eal_vfio.h"
45 #include "eal_private.h"
46
47 #ifdef VFIO_PRESENT
48
49 /* per-process VFIO config */
50 static struct vfio_config vfio_cfg;
51
52 static int vfio_type1_dma_map(int);
53 static int vfio_spapr_dma_map(int);
54 static int vfio_noiommu_dma_map(int);
55
56 /* IOMMU types we support */
57 static const struct vfio_iommu_type iommu_types[] = {
58         /* x86 IOMMU, otherwise known as type 1 */
59         { RTE_VFIO_TYPE1, "Type 1", &vfio_type1_dma_map},
60         /* ppc64 IOMMU, otherwise known as spapr */
61         { RTE_VFIO_SPAPR, "sPAPR", &vfio_spapr_dma_map},
62         /* IOMMU-less mode */
63         { RTE_VFIO_NOIOMMU, "No-IOMMU", &vfio_noiommu_dma_map},
64 };
65
66 int
67 vfio_get_group_fd(int iommu_group_no)
68 {
69         int i;
70         int vfio_group_fd;
71         int group_idx = -1;
72         char filename[PATH_MAX];
73
74         /* check if we already have the group descriptor open */
75         for (i = 0; i < VFIO_MAX_GROUPS; i++)
76                 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
77                         return vfio_cfg.vfio_groups[i].fd;
78
79         /* Lets see first if there is room for a new group */
80         if (vfio_cfg.vfio_active_groups == VFIO_MAX_GROUPS) {
81                 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
82                 return -1;
83         }
84
85         /* Now lets get an index for the new group */
86         for (i = 0; i < VFIO_MAX_GROUPS; i++)
87                 if (vfio_cfg.vfio_groups[i].group_no == -1) {
88                         group_idx = i;
89                         break;
90                 }
91
92         /* This should not happen */
93         if (group_idx == -1) {
94                 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
95                 return -1;
96         }
97         /* if primary, try to open the group */
98         if (internal_config.process_type == RTE_PROC_PRIMARY) {
99                 /* try regular group format */
100                 snprintf(filename, sizeof(filename),
101                                  VFIO_GROUP_FMT, iommu_group_no);
102                 vfio_group_fd = open(filename, O_RDWR);
103                 if (vfio_group_fd < 0) {
104                         /* if file not found, it's not an error */
105                         if (errno != ENOENT) {
106                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
107                                                 strerror(errno));
108                                 return -1;
109                         }
110
111                         /* special case: try no-IOMMU path as well */
112                         snprintf(filename, sizeof(filename),
113                                         VFIO_NOIOMMU_GROUP_FMT, iommu_group_no);
114                         vfio_group_fd = open(filename, O_RDWR);
115                         if (vfio_group_fd < 0) {
116                                 if (errno != ENOENT) {
117                                         RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
118                                                         strerror(errno));
119                                         return -1;
120                                 }
121                                 return 0;
122                         }
123                         /* noiommu group found */
124                 }
125
126                 vfio_cfg.vfio_groups[group_idx].group_no = iommu_group_no;
127                 vfio_cfg.vfio_groups[group_idx].fd = vfio_group_fd;
128                 vfio_cfg.vfio_active_groups++;
129                 return vfio_group_fd;
130         }
131         /* if we're in a secondary process, request group fd from the primary
132          * process via our socket
133          */
134         else {
135                 int socket_fd, ret;
136
137                 socket_fd = vfio_mp_sync_connect_to_primary();
138
139                 if (socket_fd < 0) {
140                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
141                         return -1;
142                 }
143                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
144                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
145                         close(socket_fd);
146                         return -1;
147                 }
148                 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
149                         RTE_LOG(ERR, EAL, "  cannot send group number!\n");
150                         close(socket_fd);
151                         return -1;
152                 }
153                 ret = vfio_mp_sync_receive_request(socket_fd);
154                 switch (ret) {
155                 case SOCKET_NO_FD:
156                         close(socket_fd);
157                         return 0;
158                 case SOCKET_OK:
159                         vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
160                         /* if we got the fd, return it */
161                         if (vfio_group_fd > 0) {
162                                 close(socket_fd);
163                                 return vfio_group_fd;
164                         }
165                         /* fall-through on error */
166                 default:
167                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
168                         close(socket_fd);
169                         return -1;
170                 }
171         }
172         return -1;
173 }
174
175
176 static int
177 get_vfio_group_idx(int vfio_group_fd)
178 {
179         int i;
180         for (i = 0; i < VFIO_MAX_GROUPS; i++)
181                 if (vfio_cfg.vfio_groups[i].fd == vfio_group_fd)
182                         return i;
183         return -1;
184 }
185
186 static void
187 vfio_group_device_get(int vfio_group_fd)
188 {
189         int i;
190
191         i = get_vfio_group_idx(vfio_group_fd);
192         if (i < 0 || i > VFIO_MAX_GROUPS)
193                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
194         else
195                 vfio_cfg.vfio_groups[i].devices++;
196 }
197
198 static void
199 vfio_group_device_put(int vfio_group_fd)
200 {
201         int i;
202
203         i = get_vfio_group_idx(vfio_group_fd);
204         if (i < 0 || i > VFIO_MAX_GROUPS)
205                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
206         else
207                 vfio_cfg.vfio_groups[i].devices--;
208 }
209
210 static int
211 vfio_group_device_count(int vfio_group_fd)
212 {
213         int i;
214
215         i = get_vfio_group_idx(vfio_group_fd);
216         if (i < 0 || i > VFIO_MAX_GROUPS) {
217                 RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
218                 return -1;
219         }
220
221         return vfio_cfg.vfio_groups[i].devices;
222 }
223
224 int
225 clear_group(int vfio_group_fd)
226 {
227         int i;
228         int socket_fd, ret;
229
230         if (internal_config.process_type == RTE_PROC_PRIMARY) {
231
232                 i = get_vfio_group_idx(vfio_group_fd);
233                 if (i < 0)
234                         return -1;
235                 vfio_cfg.vfio_groups[i].group_no = -1;
236                 vfio_cfg.vfio_groups[i].fd = -1;
237                 vfio_cfg.vfio_groups[i].devices = 0;
238                 vfio_cfg.vfio_active_groups--;
239                 return 0;
240         }
241
242         /* This is just for SECONDARY processes */
243         socket_fd = vfio_mp_sync_connect_to_primary();
244
245         if (socket_fd < 0) {
246                 RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
247                 return -1;
248         }
249
250         if (vfio_mp_sync_send_request(socket_fd, SOCKET_CLR_GROUP) < 0) {
251                 RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
252                 close(socket_fd);
253                 return -1;
254         }
255
256         if (vfio_mp_sync_send_request(socket_fd, vfio_group_fd) < 0) {
257                 RTE_LOG(ERR, EAL, "  cannot send group fd!\n");
258                 close(socket_fd);
259                 return -1;
260         }
261
262         ret = vfio_mp_sync_receive_request(socket_fd);
263         switch (ret) {
264         case SOCKET_NO_FD:
265                 RTE_LOG(ERR, EAL, "  BAD VFIO group fd!\n");
266                 close(socket_fd);
267                 break;
268         case SOCKET_OK:
269                 close(socket_fd);
270                 return 0;
271         case SOCKET_ERR:
272                 RTE_LOG(ERR, EAL, "  Socket error\n");
273                 close(socket_fd);
274                 break;
275         default:
276                 RTE_LOG(ERR, EAL, "  UNKNOWN reply, %d\n", ret);
277                 close(socket_fd);
278         }
279         return -1;
280 }
281
282 int
283 vfio_setup_device(const char *sysfs_base, const char *dev_addr,
284                 int *vfio_dev_fd, struct vfio_device_info *device_info)
285 {
286         struct vfio_group_status group_status = {
287                         .argsz = sizeof(group_status)
288         };
289         int vfio_group_fd;
290         int iommu_group_no;
291         int ret;
292
293         /* get group number */
294         ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
295         if (ret == 0) {
296                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
297                         dev_addr);
298                 return 1;
299         }
300
301         /* if negative, something failed */
302         if (ret < 0)
303                 return -1;
304
305         /* get the actual group fd */
306         vfio_group_fd = vfio_get_group_fd(iommu_group_no);
307         if (vfio_group_fd < 0)
308                 return -1;
309
310         /* if group_fd == 0, that means the device isn't managed by VFIO */
311         if (vfio_group_fd == 0) {
312                 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
313                                 dev_addr);
314                 return 1;
315         }
316
317         /*
318          * at this point, we know that this group is viable (meaning, all devices
319          * are either bound to VFIO or not bound to anything)
320          */
321
322         /* check if the group is viable */
323         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
324         if (ret) {
325                 RTE_LOG(ERR, EAL, "  %s cannot get group status, "
326                                 "error %i (%s)\n", dev_addr, errno, strerror(errno));
327                 close(vfio_group_fd);
328                 clear_group(vfio_group_fd);
329                 return -1;
330         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
331                 RTE_LOG(ERR, EAL, "  %s VFIO group is not viable!\n", dev_addr);
332                 close(vfio_group_fd);
333                 clear_group(vfio_group_fd);
334                 return -1;
335         }
336
337         /* check if group does not have a container yet */
338         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
339
340                 /* add group to a container */
341                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
342                                 &vfio_cfg.vfio_container_fd);
343                 if (ret) {
344                         RTE_LOG(ERR, EAL, "  %s cannot add VFIO group to container, "
345                                         "error %i (%s)\n", dev_addr, errno, strerror(errno));
346                         close(vfio_group_fd);
347                         clear_group(vfio_group_fd);
348                         return -1;
349                 }
350
351                 /*
352                  * pick an IOMMU type and set up DMA mappings for container
353                  *
354                  * needs to be done only once, only when first group is
355                  * assigned to a container and only in primary process.
356                  * Note this can happen several times with the hotplug
357                  * functionality.
358                  */
359                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
360                                 vfio_cfg.vfio_active_groups == 1) {
361                         /* select an IOMMU type which we will be using */
362                         const struct vfio_iommu_type *t =
363                                 vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
364                         if (!t) {
365                                 RTE_LOG(ERR, EAL,
366                                         "  %s failed to select IOMMU type\n",
367                                         dev_addr);
368                                 close(vfio_group_fd);
369                                 clear_group(vfio_group_fd);
370                                 return -1;
371                         }
372                         ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
373                         if (ret) {
374                                 RTE_LOG(ERR, EAL,
375                                         "  %s DMA remapping failed, error %i (%s)\n",
376                                         dev_addr, errno, strerror(errno));
377                                 close(vfio_group_fd);
378                                 clear_group(vfio_group_fd);
379                                 return -1;
380                         }
381                 }
382         }
383
384         /* get a file descriptor for the device */
385         *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
386         if (*vfio_dev_fd < 0) {
387                 /* if we cannot get a device fd, this implies a problem with
388                  * the VFIO group or the container not having IOMMU configured.
389                  */
390
391                 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
392                                 dev_addr);
393                 close(vfio_group_fd);
394                 clear_group(vfio_group_fd);
395                 return -1;
396         }
397
398         /* test and setup the device */
399         ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
400         if (ret) {
401                 RTE_LOG(ERR, EAL, "  %s cannot get device info, "
402                                 "error %i (%s)\n", dev_addr, errno,
403                                 strerror(errno));
404                 close(*vfio_dev_fd);
405                 close(vfio_group_fd);
406                 clear_group(vfio_group_fd);
407                 return -1;
408         }
409         vfio_group_device_get(vfio_group_fd);
410
411         return 0;
412 }
413
414 int
415 vfio_release_device(const char *sysfs_base, const char *dev_addr,
416                     int vfio_dev_fd)
417 {
418         struct vfio_group_status group_status = {
419                         .argsz = sizeof(group_status)
420         };
421         int vfio_group_fd;
422         int iommu_group_no;
423         int ret;
424
425         /* get group number */
426         ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
427         if (ret <= 0) {
428                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver\n",
429                         dev_addr);
430                 /* This is an error at this point. */
431                 return -1;
432         }
433
434         /* get the actual group fd */
435         vfio_group_fd = vfio_get_group_fd(iommu_group_no);
436         if (vfio_group_fd <= 0) {
437                 RTE_LOG(INFO, EAL, "vfio_get_group_fd failed for %s\n",
438                                    dev_addr);
439                 return -1;
440         }
441
442         /* At this point we got an active group. Closing it will make the
443          * container detachment. If this is the last active group, VFIO kernel
444          * code will unset the container and the IOMMU mappings.
445          */
446
447         /* Closing a device */
448         if (close(vfio_dev_fd) < 0) {
449                 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
450                                    dev_addr);
451                 return -1;
452         }
453
454         /* An VFIO group can have several devices attached. Just when there is
455          * no devices remaining should the group be closed.
456          */
457         vfio_group_device_put(vfio_group_fd);
458         if (!vfio_group_device_count(vfio_group_fd)) {
459
460                 if (close(vfio_group_fd) < 0) {
461                         RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
462                                 dev_addr);
463                         return -1;
464                 }
465
466                 if (clear_group(vfio_group_fd) < 0) {
467                         RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
468                                            dev_addr);
469                         return -1;
470                 }
471         }
472
473         return 0;
474 }
475
476 int
477 vfio_enable(const char *modname)
478 {
479         /* initialize group list */
480         int i;
481         int vfio_available;
482
483         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
484                 vfio_cfg.vfio_groups[i].fd = -1;
485                 vfio_cfg.vfio_groups[i].group_no = -1;
486                 vfio_cfg.vfio_groups[i].devices = 0;
487         }
488
489         /* inform the user that we are probing for VFIO */
490         RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
491
492         /* check if vfio-pci module is loaded */
493         vfio_available = rte_eal_check_module(modname);
494
495         /* return error directly */
496         if (vfio_available == -1) {
497                 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
498                 return -1;
499         }
500
501         /* return 0 if VFIO modules not loaded */
502         if (vfio_available == 0) {
503                 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
504                         "skipping VFIO support...\n");
505                 return 0;
506         }
507
508         vfio_cfg.vfio_container_fd = vfio_get_container_fd();
509
510         /* check if we have VFIO driver enabled */
511         if (vfio_cfg.vfio_container_fd != -1) {
512                 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
513                 vfio_cfg.vfio_enabled = 1;
514         } else {
515                 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
516         }
517
518         return 0;
519 }
520
521 int
522 vfio_is_enabled(const char *modname)
523 {
524         const int mod_available = rte_eal_check_module(modname);
525         return vfio_cfg.vfio_enabled && mod_available;
526 }
527
528 const struct vfio_iommu_type *
529 vfio_set_iommu_type(int vfio_container_fd)
530 {
531         unsigned idx;
532         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
533                 const struct vfio_iommu_type *t = &iommu_types[idx];
534
535                 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
536                                 t->type_id);
537                 if (!ret) {
538                         RTE_LOG(NOTICE, EAL, "  using IOMMU type %d (%s)\n",
539                                         t->type_id, t->name);
540                         return t;
541                 }
542                 /* not an error, there may be more supported IOMMU types */
543                 RTE_LOG(DEBUG, EAL, "  set IOMMU type %d (%s) failed, "
544                                 "error %i (%s)\n", t->type_id, t->name, errno,
545                                 strerror(errno));
546         }
547         /* if we didn't find a suitable IOMMU type, fail */
548         return NULL;
549 }
550
551 int
552 vfio_has_supported_extensions(int vfio_container_fd)
553 {
554         int ret;
555         unsigned idx, n_extensions = 0;
556         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
557                 const struct vfio_iommu_type *t = &iommu_types[idx];
558
559                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
560                                 t->type_id);
561                 if (ret < 0) {
562                         RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
563                                 "error %i (%s)\n", errno,
564                                 strerror(errno));
565                         close(vfio_container_fd);
566                         return -1;
567                 } else if (ret == 1) {
568                         /* we found a supported extension */
569                         n_extensions++;
570                 }
571                 RTE_LOG(DEBUG, EAL, "  IOMMU type %d (%s) is %s\n",
572                                 t->type_id, t->name,
573                                 ret ? "supported" : "not supported");
574         }
575
576         /* if we didn't find any supported IOMMU types, fail */
577         if (!n_extensions) {
578                 close(vfio_container_fd);
579                 return -1;
580         }
581
582         return 0;
583 }
584
585 int
586 vfio_get_container_fd(void)
587 {
588         int ret, vfio_container_fd;
589
590         /* if we're in a primary process, try to open the container */
591         if (internal_config.process_type == RTE_PROC_PRIMARY) {
592                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
593                 if (vfio_container_fd < 0) {
594                         RTE_LOG(ERR, EAL, "  cannot open VFIO container, "
595                                         "error %i (%s)\n", errno, strerror(errno));
596                         return -1;
597                 }
598
599                 /* check VFIO API version */
600                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
601                 if (ret != VFIO_API_VERSION) {
602                         if (ret < 0)
603                                 RTE_LOG(ERR, EAL, "  could not get VFIO API version, "
604                                                 "error %i (%s)\n", errno, strerror(errno));
605                         else
606                                 RTE_LOG(ERR, EAL, "  unsupported VFIO API version!\n");
607                         close(vfio_container_fd);
608                         return -1;
609                 }
610
611                 ret = vfio_has_supported_extensions(vfio_container_fd);
612                 if (ret) {
613                         RTE_LOG(ERR, EAL, "  no supported IOMMU "
614                                         "extensions found!\n");
615                         return -1;
616                 }
617
618                 return vfio_container_fd;
619         } else {
620                 /*
621                  * if we're in a secondary process, request container fd from the
622                  * primary process via our socket
623                  */
624                 int socket_fd;
625
626                 socket_fd = vfio_mp_sync_connect_to_primary();
627                 if (socket_fd < 0) {
628                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
629                         return -1;
630                 }
631                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
632                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
633                         close(socket_fd);
634                         return -1;
635                 }
636                 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
637                 if (vfio_container_fd < 0) {
638                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
639                         close(socket_fd);
640                         return -1;
641                 }
642                 close(socket_fd);
643                 return vfio_container_fd;
644         }
645
646         return -1;
647 }
648
649 int
650 vfio_get_group_no(const char *sysfs_base,
651                 const char *dev_addr, int *iommu_group_no)
652 {
653         char linkname[PATH_MAX];
654         char filename[PATH_MAX];
655         char *tok[16], *group_tok, *end;
656         int ret;
657
658         memset(linkname, 0, sizeof(linkname));
659         memset(filename, 0, sizeof(filename));
660
661         /* try to find out IOMMU group for this device */
662         snprintf(linkname, sizeof(linkname),
663                          "%s/%s/iommu_group", sysfs_base, dev_addr);
664
665         ret = readlink(linkname, filename, sizeof(filename));
666
667         /* if the link doesn't exist, no VFIO for us */
668         if (ret < 0)
669                 return 0;
670
671         ret = rte_strsplit(filename, sizeof(filename),
672                         tok, RTE_DIM(tok), '/');
673
674         if (ret <= 0) {
675                 RTE_LOG(ERR, EAL, "  %s cannot get IOMMU group\n", dev_addr);
676                 return -1;
677         }
678
679         /* IOMMU group is always the last token */
680         errno = 0;
681         group_tok = tok[ret - 1];
682         end = group_tok;
683         *iommu_group_no = strtol(group_tok, &end, 10);
684         if ((end != group_tok && *end != '\0') || errno != 0) {
685                 RTE_LOG(ERR, EAL, "  %s error parsing IOMMU number!\n", dev_addr);
686                 return -1;
687         }
688
689         return 1;
690 }
691
692 static int
693 vfio_type1_dma_map(int vfio_container_fd)
694 {
695         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
696         int i, ret;
697
698         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
699         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
700                 struct vfio_iommu_type1_dma_map dma_map;
701
702                 if (ms[i].addr == NULL)
703                         break;
704
705                 memset(&dma_map, 0, sizeof(dma_map));
706                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
707                 dma_map.vaddr = ms[i].addr_64;
708                 dma_map.size = ms[i].len;
709                 dma_map.iova = ms[i].phys_addr;
710                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
711
712                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
713
714                 if (ret) {
715                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, "
716                                           "error %i (%s)\n", errno,
717                                           strerror(errno));
718                         return -1;
719                 }
720         }
721
722         return 0;
723 }
724
725 static int
726 vfio_spapr_dma_map(int vfio_container_fd)
727 {
728         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
729         int i, ret;
730
731         struct vfio_iommu_spapr_register_memory reg = {
732                 .argsz = sizeof(reg),
733                 .flags = 0
734         };
735         struct vfio_iommu_spapr_tce_info info = {
736                 .argsz = sizeof(info),
737         };
738         struct vfio_iommu_spapr_tce_create create = {
739                 .argsz = sizeof(create),
740         };
741         struct vfio_iommu_spapr_tce_remove remove = {
742                 .argsz = sizeof(remove),
743         };
744
745         /* query spapr iommu info */
746         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
747         if (ret) {
748                 RTE_LOG(ERR, EAL, "  cannot get iommu info, "
749                                 "error %i (%s)\n", errno, strerror(errno));
750                 return -1;
751         }
752
753         /* remove default DMA of 32 bit window */
754         remove.start_addr = info.dma32_window_start;
755         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
756         if (ret) {
757                 RTE_LOG(ERR, EAL, "  cannot remove default DMA window, "
758                                 "error %i (%s)\n", errno, strerror(errno));
759                 return -1;
760         }
761
762         /* calculate window size based on number of hugepages configured */
763         create.window_size = rte_eal_get_physmem_size();
764         create.page_shift = __builtin_ctzll(ms->hugepage_sz);
765         create.levels = 2;
766
767         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
768         if (ret) {
769                 RTE_LOG(ERR, EAL, "  cannot create new DMA window, "
770                                 "error %i (%s)\n", errno, strerror(errno));
771                 return -1;
772         }
773
774         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
775         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
776                 struct vfio_iommu_type1_dma_map dma_map;
777
778                 if (ms[i].addr == NULL)
779                         break;
780
781                 reg.vaddr = (uintptr_t) ms[i].addr;
782                 reg.size = ms[i].len;
783                 ret = ioctl(vfio_container_fd,
784                         VFIO_IOMMU_SPAPR_REGISTER_MEMORY, &reg);
785                 if (ret) {
786                         RTE_LOG(ERR, EAL, "  cannot register vaddr for IOMMU, "
787                                 "error %i (%s)\n", errno, strerror(errno));
788                         return -1;
789                 }
790
791                 memset(&dma_map, 0, sizeof(dma_map));
792                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
793                 dma_map.vaddr = ms[i].addr_64;
794                 dma_map.size = ms[i].len;
795                 dma_map.iova = ms[i].phys_addr;
796                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
797                                  VFIO_DMA_MAP_FLAG_WRITE;
798
799                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
800
801                 if (ret) {
802                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, "
803                                 "error %i (%s)\n", errno, strerror(errno));
804                         return -1;
805                 }
806
807         }
808
809         return 0;
810 }
811
812 static int
813 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
814 {
815         /* No-IOMMU mode does not need DMA mapping */
816         return 0;
817 }
818
819 #endif