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