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