vfio: fix device unplug when several devices per group
[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 int
176 clear_group(int vfio_group_fd)
177 {
178         int i;
179         int socket_fd, ret;
180
181         if (internal_config.process_type == RTE_PROC_PRIMARY) {
182
183                 for (i = 0; i < VFIO_MAX_GROUPS; i++)
184                         if (vfio_cfg.vfio_groups[i].fd == vfio_group_fd) {
185                                 vfio_cfg.vfio_groups[i].group_no = -1;
186                                 vfio_cfg.vfio_groups[i].fd = -1;
187                                 vfio_cfg.vfio_groups[i].devices = 0;
188                                 vfio_cfg.vfio_active_groups--;
189                                 return 0;
190                         }
191                 return -1;
192         }
193
194         /* This is just for SECONDARY processes */
195         socket_fd = vfio_mp_sync_connect_to_primary();
196
197         if (socket_fd < 0) {
198                 RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
199                 return -1;
200         }
201
202         if (vfio_mp_sync_send_request(socket_fd, SOCKET_CLR_GROUP) < 0) {
203                 RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
204                 close(socket_fd);
205                 return -1;
206         }
207
208         if (vfio_mp_sync_send_request(socket_fd, vfio_group_fd) < 0) {
209                 RTE_LOG(ERR, EAL, "  cannot send group fd!\n");
210                 close(socket_fd);
211                 return -1;
212         }
213
214         ret = vfio_mp_sync_receive_request(socket_fd);
215         switch (ret) {
216         case SOCKET_NO_FD:
217                 RTE_LOG(ERR, EAL, "  BAD VFIO group fd!\n");
218                 close(socket_fd);
219                 break;
220         case SOCKET_OK:
221                 close(socket_fd);
222                 return 0;
223         case SOCKET_ERR:
224                 RTE_LOG(ERR, EAL, "  Socket error\n");
225                 close(socket_fd);
226                 break;
227         default:
228                 RTE_LOG(ERR, EAL, "  UNKNOWN reply, %d\n", ret);
229                 close(socket_fd);
230         }
231         return -1;
232 }
233
234 int
235 vfio_setup_device(const char *sysfs_base, const char *dev_addr,
236                 int *vfio_dev_fd, struct vfio_device_info *device_info)
237 {
238         struct vfio_group_status group_status = {
239                         .argsz = sizeof(group_status)
240         };
241         int vfio_group_fd;
242         int iommu_group_no;
243         int ret;
244
245         /* get group number */
246         ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
247         if (ret == 0) {
248                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
249                         dev_addr);
250                 return 1;
251         }
252
253         /* if negative, something failed */
254         if (ret < 0)
255                 return -1;
256
257         /* get the actual group fd */
258         vfio_group_fd = vfio_get_group_fd(iommu_group_no);
259         if (vfio_group_fd < 0)
260                 return -1;
261
262         /* if group_fd == 0, that means the device isn't managed by VFIO */
263         if (vfio_group_fd == 0) {
264                 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
265                                 dev_addr);
266                 return 1;
267         }
268
269         /*
270          * at this point, we know that this group is viable (meaning, all devices
271          * are either bound to VFIO or not bound to anything)
272          */
273
274         /* check if the group is viable */
275         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
276         if (ret) {
277                 RTE_LOG(ERR, EAL, "  %s cannot get group status, "
278                                 "error %i (%s)\n", dev_addr, errno, strerror(errno));
279                 close(vfio_group_fd);
280                 clear_group(vfio_group_fd);
281                 return -1;
282         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
283                 RTE_LOG(ERR, EAL, "  %s VFIO group is not viable!\n", dev_addr);
284                 close(vfio_group_fd);
285                 clear_group(vfio_group_fd);
286                 return -1;
287         }
288
289         /* check if group does not have a container yet */
290         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
291
292                 /* add group to a container */
293                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
294                                 &vfio_cfg.vfio_container_fd);
295                 if (ret) {
296                         RTE_LOG(ERR, EAL, "  %s cannot add VFIO group to container, "
297                                         "error %i (%s)\n", dev_addr, errno, strerror(errno));
298                         close(vfio_group_fd);
299                         clear_group(vfio_group_fd);
300                         return -1;
301                 }
302
303                 /*
304                  * pick an IOMMU type and set up DMA mappings for container
305                  *
306                  * needs to be done only once, only when first group is
307                  * assigned to a container and only in primary process.
308                  * Note this can happen several times with the hotplug
309                  * functionality.
310                  */
311                 if (internal_config.process_type == RTE_PROC_PRIMARY &&
312                                 vfio_cfg.vfio_active_groups == 1) {
313                         /* select an IOMMU type which we will be using */
314                         const struct vfio_iommu_type *t =
315                                 vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
316                         if (!t) {
317                                 RTE_LOG(ERR, EAL,
318                                         "  %s failed to select IOMMU type\n",
319                                         dev_addr);
320                                 close(vfio_group_fd);
321                                 clear_group(vfio_group_fd);
322                                 return -1;
323                         }
324                         ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
325                         if (ret) {
326                                 RTE_LOG(ERR, EAL,
327                                         "  %s DMA remapping failed, error %i (%s)\n",
328                                         dev_addr, errno, strerror(errno));
329                                 close(vfio_group_fd);
330                                 clear_group(vfio_group_fd);
331                                 return -1;
332                         }
333                 }
334         }
335
336         /* get a file descriptor for the device */
337         *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
338         if (*vfio_dev_fd < 0) {
339                 /* if we cannot get a device fd, this implies a problem with
340                  * the VFIO group or the container not having IOMMU configured.
341                  */
342
343                 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
344                                 dev_addr);
345                 close(vfio_group_fd);
346                 clear_group(vfio_group_fd);
347                 return -1;
348         }
349
350         /* test and setup the device */
351         ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
352         if (ret) {
353                 RTE_LOG(ERR, EAL, "  %s cannot get device info, "
354                                 "error %i (%s)\n", dev_addr, errno,
355                                 strerror(errno));
356                 close(*vfio_dev_fd);
357                 close(vfio_group_fd);
358                 clear_group(vfio_group_fd);
359                 return -1;
360         }
361         vfio_cfg.vfio_groups[vfio_group_fd].devices++;
362
363         return 0;
364 }
365
366 int
367 vfio_release_device(const char *sysfs_base, const char *dev_addr,
368                     int vfio_dev_fd)
369 {
370         struct vfio_group_status group_status = {
371                         .argsz = sizeof(group_status)
372         };
373         int vfio_group_fd;
374         int iommu_group_no;
375         int ret;
376
377         /* get group number */
378         ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
379         if (ret <= 0) {
380                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver\n",
381                         dev_addr);
382                 /* This is an error at this point. */
383                 return -1;
384         }
385
386         /* get the actual group fd */
387         vfio_group_fd = vfio_get_group_fd(iommu_group_no);
388         if (vfio_group_fd <= 0) {
389                 RTE_LOG(INFO, EAL, "vfio_get_group_fd failed for %s\n",
390                                    dev_addr);
391                 return -1;
392         }
393
394         /* At this point we got an active group. Closing it will make the
395          * container detachment. If this is the last active group, VFIO kernel
396          * code will unset the container and the IOMMU mappings.
397          */
398
399         /* Closing a device */
400         if (close(vfio_dev_fd) < 0) {
401                 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
402                                    dev_addr);
403                 return -1;
404         }
405
406         /* An VFIO group can have several devices attached. Just when there is
407          * no devices remaining should the group be closed.
408          */
409         if (--vfio_cfg.vfio_groups[vfio_group_fd].devices == 0) {
410
411                 if (close(vfio_group_fd) < 0) {
412                         RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
413                                 dev_addr);
414                         return -1;
415                 }
416
417                 if (clear_group(vfio_group_fd) < 0) {
418                         RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
419                                            dev_addr);
420                         return -1;
421                 }
422         }
423
424         return 0;
425 }
426
427 int
428 vfio_enable(const char *modname)
429 {
430         /* initialize group list */
431         int i;
432         int vfio_available;
433
434         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
435                 vfio_cfg.vfio_groups[i].fd = -1;
436                 vfio_cfg.vfio_groups[i].group_no = -1;
437                 vfio_cfg.vfio_groups[i].devices = 0;
438         }
439
440         /* inform the user that we are probing for VFIO */
441         RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
442
443         /* check if vfio-pci module is loaded */
444         vfio_available = rte_eal_check_module(modname);
445
446         /* return error directly */
447         if (vfio_available == -1) {
448                 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
449                 return -1;
450         }
451
452         /* return 0 if VFIO modules not loaded */
453         if (vfio_available == 0) {
454                 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
455                         "skipping VFIO support...\n");
456                 return 0;
457         }
458
459         vfio_cfg.vfio_container_fd = vfio_get_container_fd();
460
461         /* check if we have VFIO driver enabled */
462         if (vfio_cfg.vfio_container_fd != -1) {
463                 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
464                 vfio_cfg.vfio_enabled = 1;
465         } else {
466                 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
467         }
468
469         return 0;
470 }
471
472 int
473 vfio_is_enabled(const char *modname)
474 {
475         const int mod_available = rte_eal_check_module(modname);
476         return vfio_cfg.vfio_enabled && mod_available;
477 }
478
479 const struct vfio_iommu_type *
480 vfio_set_iommu_type(int vfio_container_fd)
481 {
482         unsigned idx;
483         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
484                 const struct vfio_iommu_type *t = &iommu_types[idx];
485
486                 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
487                                 t->type_id);
488                 if (!ret) {
489                         RTE_LOG(NOTICE, EAL, "  using IOMMU type %d (%s)\n",
490                                         t->type_id, t->name);
491                         return t;
492                 }
493                 /* not an error, there may be more supported IOMMU types */
494                 RTE_LOG(DEBUG, EAL, "  set IOMMU type %d (%s) failed, "
495                                 "error %i (%s)\n", t->type_id, t->name, errno,
496                                 strerror(errno));
497         }
498         /* if we didn't find a suitable IOMMU type, fail */
499         return NULL;
500 }
501
502 int
503 vfio_has_supported_extensions(int vfio_container_fd)
504 {
505         int ret;
506         unsigned idx, n_extensions = 0;
507         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
508                 const struct vfio_iommu_type *t = &iommu_types[idx];
509
510                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
511                                 t->type_id);
512                 if (ret < 0) {
513                         RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
514                                 "error %i (%s)\n", errno,
515                                 strerror(errno));
516                         close(vfio_container_fd);
517                         return -1;
518                 } else if (ret == 1) {
519                         /* we found a supported extension */
520                         n_extensions++;
521                 }
522                 RTE_LOG(DEBUG, EAL, "  IOMMU type %d (%s) is %s\n",
523                                 t->type_id, t->name,
524                                 ret ? "supported" : "not supported");
525         }
526
527         /* if we didn't find any supported IOMMU types, fail */
528         if (!n_extensions) {
529                 close(vfio_container_fd);
530                 return -1;
531         }
532
533         return 0;
534 }
535
536 int
537 vfio_get_container_fd(void)
538 {
539         int ret, vfio_container_fd;
540
541         /* if we're in a primary process, try to open the container */
542         if (internal_config.process_type == RTE_PROC_PRIMARY) {
543                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
544                 if (vfio_container_fd < 0) {
545                         RTE_LOG(ERR, EAL, "  cannot open VFIO container, "
546                                         "error %i (%s)\n", errno, strerror(errno));
547                         return -1;
548                 }
549
550                 /* check VFIO API version */
551                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
552                 if (ret != VFIO_API_VERSION) {
553                         if (ret < 0)
554                                 RTE_LOG(ERR, EAL, "  could not get VFIO API version, "
555                                                 "error %i (%s)\n", errno, strerror(errno));
556                         else
557                                 RTE_LOG(ERR, EAL, "  unsupported VFIO API version!\n");
558                         close(vfio_container_fd);
559                         return -1;
560                 }
561
562                 ret = vfio_has_supported_extensions(vfio_container_fd);
563                 if (ret) {
564                         RTE_LOG(ERR, EAL, "  no supported IOMMU "
565                                         "extensions found!\n");
566                         return -1;
567                 }
568
569                 return vfio_container_fd;
570         } else {
571                 /*
572                  * if we're in a secondary process, request container fd from the
573                  * primary process via our socket
574                  */
575                 int socket_fd;
576
577                 socket_fd = vfio_mp_sync_connect_to_primary();
578                 if (socket_fd < 0) {
579                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
580                         return -1;
581                 }
582                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
583                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
584                         close(socket_fd);
585                         return -1;
586                 }
587                 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
588                 if (vfio_container_fd < 0) {
589                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
590                         close(socket_fd);
591                         return -1;
592                 }
593                 close(socket_fd);
594                 return vfio_container_fd;
595         }
596
597         return -1;
598 }
599
600 int
601 vfio_get_group_no(const char *sysfs_base,
602                 const char *dev_addr, int *iommu_group_no)
603 {
604         char linkname[PATH_MAX];
605         char filename[PATH_MAX];
606         char *tok[16], *group_tok, *end;
607         int ret;
608
609         memset(linkname, 0, sizeof(linkname));
610         memset(filename, 0, sizeof(filename));
611
612         /* try to find out IOMMU group for this device */
613         snprintf(linkname, sizeof(linkname),
614                          "%s/%s/iommu_group", sysfs_base, dev_addr);
615
616         ret = readlink(linkname, filename, sizeof(filename));
617
618         /* if the link doesn't exist, no VFIO for us */
619         if (ret < 0)
620                 return 0;
621
622         ret = rte_strsplit(filename, sizeof(filename),
623                         tok, RTE_DIM(tok), '/');
624
625         if (ret <= 0) {
626                 RTE_LOG(ERR, EAL, "  %s cannot get IOMMU group\n", dev_addr);
627                 return -1;
628         }
629
630         /* IOMMU group is always the last token */
631         errno = 0;
632         group_tok = tok[ret - 1];
633         end = group_tok;
634         *iommu_group_no = strtol(group_tok, &end, 10);
635         if ((end != group_tok && *end != '\0') || errno != 0) {
636                 RTE_LOG(ERR, EAL, "  %s error parsing IOMMU number!\n", dev_addr);
637                 return -1;
638         }
639
640         return 1;
641 }
642
643 static int
644 vfio_type1_dma_map(int vfio_container_fd)
645 {
646         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
647         int i, ret;
648
649         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
650         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
651                 struct vfio_iommu_type1_dma_map dma_map;
652
653                 if (ms[i].addr == NULL)
654                         break;
655
656                 memset(&dma_map, 0, sizeof(dma_map));
657                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
658                 dma_map.vaddr = ms[i].addr_64;
659                 dma_map.size = ms[i].len;
660                 dma_map.iova = ms[i].phys_addr;
661                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
662
663                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
664
665                 if (ret) {
666                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, "
667                                           "error %i (%s)\n", errno,
668                                           strerror(errno));
669                         return -1;
670                 }
671         }
672
673         return 0;
674 }
675
676 static int
677 vfio_spapr_dma_map(int vfio_container_fd)
678 {
679         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
680         int i, ret;
681
682         struct vfio_iommu_spapr_register_memory reg = {
683                 .argsz = sizeof(reg),
684                 .flags = 0
685         };
686         struct vfio_iommu_spapr_tce_info info = {
687                 .argsz = sizeof(info),
688         };
689         struct vfio_iommu_spapr_tce_create create = {
690                 .argsz = sizeof(create),
691         };
692         struct vfio_iommu_spapr_tce_remove remove = {
693                 .argsz = sizeof(remove),
694         };
695
696         /* query spapr iommu info */
697         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
698         if (ret) {
699                 RTE_LOG(ERR, EAL, "  cannot get iommu info, "
700                                 "error %i (%s)\n", errno, strerror(errno));
701                 return -1;
702         }
703
704         /* remove default DMA of 32 bit window */
705         remove.start_addr = info.dma32_window_start;
706         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
707         if (ret) {
708                 RTE_LOG(ERR, EAL, "  cannot remove default DMA window, "
709                                 "error %i (%s)\n", errno, strerror(errno));
710                 return -1;
711         }
712
713         /* calculate window size based on number of hugepages configured */
714         create.window_size = rte_eal_get_physmem_size();
715         create.page_shift = __builtin_ctzll(ms->hugepage_sz);
716         create.levels = 2;
717
718         ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
719         if (ret) {
720                 RTE_LOG(ERR, EAL, "  cannot create new DMA window, "
721                                 "error %i (%s)\n", errno, strerror(errno));
722                 return -1;
723         }
724
725         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
726         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
727                 struct vfio_iommu_type1_dma_map dma_map;
728
729                 if (ms[i].addr == NULL)
730                         break;
731
732                 reg.vaddr = (uintptr_t) ms[i].addr;
733                 reg.size = ms[i].len;
734                 ret = ioctl(vfio_container_fd,
735                         VFIO_IOMMU_SPAPR_REGISTER_MEMORY, &reg);
736                 if (ret) {
737                         RTE_LOG(ERR, EAL, "  cannot register vaddr for IOMMU, "
738                                 "error %i (%s)\n", errno, strerror(errno));
739                         return -1;
740                 }
741
742                 memset(&dma_map, 0, sizeof(dma_map));
743                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
744                 dma_map.vaddr = ms[i].addr_64;
745                 dma_map.size = ms[i].len;
746                 dma_map.iova = ms[i].phys_addr;
747                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
748                                  VFIO_DMA_MAP_FLAG_WRITE;
749
750                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
751
752                 if (ret) {
753                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, "
754                                 "error %i (%s)\n", errno, strerror(errno));
755                         return -1;
756                 }
757
758         }
759
760         return 0;
761 }
762
763 static int
764 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
765 {
766         /* No-IOMMU mode does not need DMA mapping */
767         return 0;
768 }
769
770 #endif