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