74f91bad51dcc6dacb8d093d04a224323ffce9ee
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_pci_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 <linux/pci_regs.h>
37 #include <sys/eventfd.h>
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40 #include <sys/mman.h>
41
42 #include <rte_log.h>
43 #include <rte_pci.h>
44 #include <rte_eal_memconfig.h>
45 #include <rte_malloc.h>
46 #include <eal_private.h>
47
48 #include "eal_filesystem.h"
49 #include "eal_pci_init.h"
50 #include "eal_vfio.h"
51
52 /**
53  * @file
54  * PCI probing under linux (VFIO version)
55  *
56  * This code tries to determine if the PCI device is bound to VFIO driver,
57  * and initialize it (map BARs, set up interrupts) if that's the case.
58  *
59  * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
60  */
61
62 #ifdef VFIO_PRESENT
63
64 #define PAGE_SIZE   (sysconf(_SC_PAGESIZE))
65 #define PAGE_MASK   (~(PAGE_SIZE - 1))
66
67 static struct rte_tailq_elem rte_vfio_tailq = {
68         .name = "VFIO_RESOURCE_LIST",
69 };
70 EAL_REGISTER_TAILQ(rte_vfio_tailq)
71
72 #define VFIO_DIR "/dev/vfio"
73 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
74 #define VFIO_GROUP_FMT "/dev/vfio/%u"
75 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
76
77 /* per-process VFIO config */
78 static struct vfio_config vfio_cfg;
79
80 int
81 pci_vfio_read_config(const struct rte_intr_handle *intr_handle,
82                     void *buf, size_t len, off_t offs)
83 {
84         return pread64(intr_handle->vfio_dev_fd, buf, len,
85                VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
86 }
87
88 int
89 pci_vfio_write_config(const struct rte_intr_handle *intr_handle,
90                     const void *buf, size_t len, off_t offs)
91 {
92         return pwrite64(intr_handle->vfio_dev_fd, buf, len,
93                VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) + offs);
94 }
95
96 /* get PCI BAR number where MSI-X interrupts are */
97 static int
98 pci_vfio_get_msix_bar(int fd, int *msix_bar, uint32_t *msix_table_offset,
99                       uint32_t *msix_table_size)
100 {
101         int ret;
102         uint32_t reg;
103         uint16_t flags;
104         uint8_t cap_id, cap_offset;
105
106         /* read PCI capability pointer from config space */
107         ret = pread64(fd, &reg, sizeof(reg),
108                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
109                         PCI_CAPABILITY_LIST);
110         if (ret != sizeof(reg)) {
111                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
112                                 "config space!\n");
113                 return -1;
114         }
115
116         /* we need first byte */
117         cap_offset = reg & 0xFF;
118
119         while (cap_offset) {
120
121                 /* read PCI capability ID */
122                 ret = pread64(fd, &reg, sizeof(reg),
123                                 VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
124                                 cap_offset);
125                 if (ret != sizeof(reg)) {
126                         RTE_LOG(ERR, EAL, "Cannot read capability ID from PCI "
127                                         "config space!\n");
128                         return -1;
129                 }
130
131                 /* we need first byte */
132                 cap_id = reg & 0xFF;
133
134                 /* if we haven't reached MSI-X, check next capability */
135                 if (cap_id != PCI_CAP_ID_MSIX) {
136                         ret = pread64(fd, &reg, sizeof(reg),
137                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
138                                         cap_offset);
139                         if (ret != sizeof(reg)) {
140                                 RTE_LOG(ERR, EAL, "Cannot read capability pointer from PCI "
141                                                 "config space!\n");
142                                 return -1;
143                         }
144
145                         /* we need second byte */
146                         cap_offset = (reg & 0xFF00) >> 8;
147
148                         continue;
149                 }
150                 /* else, read table offset */
151                 else {
152                         /* table offset resides in the next 4 bytes */
153                         ret = pread64(fd, &reg, sizeof(reg),
154                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
155                                         cap_offset + 4);
156                         if (ret != sizeof(reg)) {
157                                 RTE_LOG(ERR, EAL, "Cannot read table offset from PCI config "
158                                                 "space!\n");
159                                 return -1;
160                         }
161
162                         ret = pread64(fd, &flags, sizeof(flags),
163                                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
164                                         cap_offset + 2);
165                         if (ret != sizeof(flags)) {
166                                 RTE_LOG(ERR, EAL, "Cannot read table flags from PCI config "
167                                                 "space!\n");
168                                 return -1;
169                         }
170
171                         *msix_bar = reg & RTE_PCI_MSIX_TABLE_BIR;
172                         *msix_table_offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
173                         *msix_table_size = 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
174
175                         return 0;
176                 }
177         }
178         return 0;
179 }
180
181 /* set PCI bus mastering */
182 static int
183 pci_vfio_set_bus_master(int dev_fd)
184 {
185         uint16_t reg;
186         int ret;
187
188         ret = pread64(dev_fd, &reg, sizeof(reg),
189                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
190                         PCI_COMMAND);
191         if (ret != sizeof(reg)) {
192                 RTE_LOG(ERR, EAL, "Cannot read command from PCI config space!\n");
193                 return -1;
194         }
195
196         /* set the master bit */
197         reg |= PCI_COMMAND_MASTER;
198
199         ret = pwrite64(dev_fd, &reg, sizeof(reg),
200                         VFIO_GET_REGION_ADDR(VFIO_PCI_CONFIG_REGION_INDEX) +
201                         PCI_COMMAND);
202
203         if (ret != sizeof(reg)) {
204                 RTE_LOG(ERR, EAL, "Cannot write command to PCI config space!\n");
205                 return -1;
206         }
207
208         return 0;
209 }
210
211 /* set up DMA mappings */
212 static int
213 pci_vfio_setup_dma_maps(int vfio_container_fd)
214 {
215         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
216         int i, ret;
217
218         ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
219                         VFIO_TYPE1_IOMMU);
220         if (ret) {
221                 RTE_LOG(ERR, EAL, "  cannot set IOMMU type, "
222                                 "error %i (%s)\n", errno, strerror(errno));
223                 return -1;
224         }
225
226         /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
227         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
228                 struct vfio_iommu_type1_dma_map dma_map;
229
230                 if (ms[i].addr == NULL)
231                         break;
232
233                 memset(&dma_map, 0, sizeof(dma_map));
234                 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
235                 dma_map.vaddr = ms[i].addr_64;
236                 dma_map.size = ms[i].len;
237                 dma_map.iova = ms[i].phys_addr;
238                 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
239
240                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
241
242                 if (ret) {
243                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, "
244                                         "error %i (%s)\n", errno, strerror(errno));
245                         return -1;
246                 }
247         }
248
249         return 0;
250 }
251
252 /* set up interrupt support (but not enable interrupts) */
253 static int
254 pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
255 {
256         int i, ret, intr_idx;
257
258         /* default to invalid index */
259         intr_idx = VFIO_PCI_NUM_IRQS;
260
261         /* get interrupt type from internal config (MSI-X by default, can be
262          * overriden from the command line
263          */
264         switch (internal_config.vfio_intr_mode) {
265         case RTE_INTR_MODE_MSIX:
266                 intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
267                 break;
268         case RTE_INTR_MODE_MSI:
269                 intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
270                 break;
271         case RTE_INTR_MODE_LEGACY:
272                 intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
273                 break;
274         /* don't do anything if we want to automatically determine interrupt type */
275         case RTE_INTR_MODE_NONE:
276                 break;
277         default:
278                 RTE_LOG(ERR, EAL, "  unknown default interrupt type!\n");
279                 return -1;
280         }
281
282         /* start from MSI-X interrupt type */
283         for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
284                 struct vfio_irq_info irq = { .argsz = sizeof(irq) };
285                 int fd = -1;
286
287                 /* skip interrupt modes we don't want */
288                 if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE &&
289                                 i != intr_idx)
290                         continue;
291
292                 irq.index = i;
293
294                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
295                 if (ret < 0) {
296                         RTE_LOG(ERR, EAL, "  cannot get IRQ info, "
297                                         "error %i (%s)\n", errno, strerror(errno));
298                         return -1;
299                 }
300
301                 /* if this vector cannot be used with eventfd, fail if we explicitly
302                  * specified interrupt type, otherwise continue */
303                 if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
304                         if (internal_config.vfio_intr_mode != RTE_INTR_MODE_NONE) {
305                                 RTE_LOG(ERR, EAL,
306                                                 "  interrupt vector does not support eventfd!\n");
307                                 return -1;
308                         } else
309                                 continue;
310                 }
311
312                 /* set up an eventfd for interrupts */
313                 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
314                 if (fd < 0) {
315                         RTE_LOG(ERR, EAL, "  cannot set up eventfd, "
316                                         "error %i (%s)\n", errno, strerror(errno));
317                         return -1;
318                 }
319
320                 dev->intr_handle.fd = fd;
321                 dev->intr_handle.vfio_dev_fd = vfio_dev_fd;
322
323                 switch (i) {
324                 case VFIO_PCI_MSIX_IRQ_INDEX:
325                         internal_config.vfio_intr_mode = RTE_INTR_MODE_MSIX;
326                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSIX;
327                         break;
328                 case VFIO_PCI_MSI_IRQ_INDEX:
329                         internal_config.vfio_intr_mode = RTE_INTR_MODE_MSI;
330                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_MSI;
331                         break;
332                 case VFIO_PCI_INTX_IRQ_INDEX:
333                         internal_config.vfio_intr_mode = RTE_INTR_MODE_LEGACY;
334                         dev->intr_handle.type = RTE_INTR_HANDLE_VFIO_LEGACY;
335                         break;
336                 default:
337                         RTE_LOG(ERR, EAL, "  unknown interrupt type!\n");
338                         return -1;
339                 }
340
341                 return 0;
342         }
343
344         /* if we're here, we haven't found a suitable interrupt vector */
345         return -1;
346 }
347
348 /* open container fd or get an existing one */
349 int
350 pci_vfio_get_container_fd(void)
351 {
352         int ret, vfio_container_fd;
353
354         /* if we're in a primary process, try to open the container */
355         if (internal_config.process_type == RTE_PROC_PRIMARY) {
356                 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
357                 if (vfio_container_fd < 0) {
358                         RTE_LOG(ERR, EAL, "  cannot open VFIO container, "
359                                         "error %i (%s)\n", errno, strerror(errno));
360                         return -1;
361                 }
362
363                 /* check VFIO API version */
364                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
365                 if (ret != VFIO_API_VERSION) {
366                         if (ret < 0)
367                                 RTE_LOG(ERR, EAL, "  could not get VFIO API version, "
368                                                 "error %i (%s)\n", errno, strerror(errno));
369                         else
370                                 RTE_LOG(ERR, EAL, "  unsupported VFIO API version!\n");
371                         close(vfio_container_fd);
372                         return -1;
373                 }
374
375                 /* check if we support IOMMU type 1 */
376                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
377                 if (ret != 1) {
378                         if (ret < 0)
379                                 RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
380                                         "error %i (%s)\n", errno,
381                                         strerror(errno));
382                         else
383                                 RTE_LOG(ERR, EAL, "  unsupported IOMMU type "
384                                         "detected in VFIO\n");
385                         close(vfio_container_fd);
386                         return -1;
387                 }
388
389                 return vfio_container_fd;
390         } else {
391                 /*
392                  * if we're in a secondary process, request container fd from the
393                  * primary process via our socket
394                  */
395                 int socket_fd;
396
397                 socket_fd = vfio_mp_sync_connect_to_primary();
398                 if (socket_fd < 0) {
399                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
400                         return -1;
401                 }
402                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
403                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
404                         close(socket_fd);
405                         return -1;
406                 }
407                 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
408                 if (vfio_container_fd < 0) {
409                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
410                         close(socket_fd);
411                         return -1;
412                 }
413                 close(socket_fd);
414                 return vfio_container_fd;
415         }
416
417         return -1;
418 }
419
420 /* open group fd or get an existing one */
421 int
422 pci_vfio_get_group_fd(int iommu_group_no)
423 {
424         int i;
425         int vfio_group_fd;
426         char filename[PATH_MAX];
427
428         /* check if we already have the group descriptor open */
429         for (i = 0; i < vfio_cfg.vfio_group_idx; i++)
430                 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
431                         return vfio_cfg.vfio_groups[i].fd;
432
433         /* if primary, try to open the group */
434         if (internal_config.process_type == RTE_PROC_PRIMARY) {
435                 snprintf(filename, sizeof(filename),
436                                  VFIO_GROUP_FMT, iommu_group_no);
437                 vfio_group_fd = open(filename, O_RDWR);
438                 if (vfio_group_fd < 0) {
439                         /* if file not found, it's not an error */
440                         if (errno != ENOENT) {
441                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
442                                                 strerror(errno));
443                                 return -1;
444                         }
445                         return 0;
446                 }
447
448                 /* if the fd is valid, create a new group for it */
449                 if (vfio_cfg.vfio_group_idx == VFIO_MAX_GROUPS) {
450                         RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
451                         return -1;
452                 }
453                 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
454                 vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
455                 return vfio_group_fd;
456         }
457         /* if we're in a secondary process, request group fd from the primary
458          * process via our socket
459          */
460         else {
461                 int socket_fd, ret;
462
463                 socket_fd = vfio_mp_sync_connect_to_primary();
464
465                 if (socket_fd < 0) {
466                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
467                         return -1;
468                 }
469                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
470                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
471                         close(socket_fd);
472                         return -1;
473                 }
474                 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
475                         RTE_LOG(ERR, EAL, "  cannot send group number!\n");
476                         close(socket_fd);
477                         return -1;
478                 }
479                 ret = vfio_mp_sync_receive_request(socket_fd);
480                 switch (ret) {
481                 case SOCKET_NO_FD:
482                         close(socket_fd);
483                         return 0;
484                 case SOCKET_OK:
485                         vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
486                         /* if we got the fd, return it */
487                         if (vfio_group_fd > 0) {
488                                 close(socket_fd);
489                                 return vfio_group_fd;
490                         }
491                         /* fall-through on error */
492                 default:
493                         RTE_LOG(ERR, EAL, "  cannot get container fd!\n");
494                         close(socket_fd);
495                         return -1;
496                 }
497         }
498         return -1;
499 }
500
501 /* parse IOMMU group number for a PCI device
502  * returns 1 on success, -1 for errors, 0 for non-existent group
503  */
504 static int
505 pci_vfio_get_group_no(const char *pci_addr, int *iommu_group_no)
506 {
507         char linkname[PATH_MAX];
508         char filename[PATH_MAX];
509         char *tok[16], *group_tok, *end;
510         int ret;
511
512         memset(linkname, 0, sizeof(linkname));
513         memset(filename, 0, sizeof(filename));
514
515         /* try to find out IOMMU group for this device */
516         snprintf(linkname, sizeof(linkname),
517                          SYSFS_PCI_DEVICES "/%s/iommu_group", pci_addr);
518
519         ret = readlink(linkname, filename, sizeof(filename));
520
521         /* if the link doesn't exist, no VFIO for us */
522         if (ret < 0)
523                 return 0;
524
525         ret = rte_strsplit(filename, sizeof(filename),
526                         tok, RTE_DIM(tok), '/');
527
528         if (ret <= 0) {
529                 RTE_LOG(ERR, EAL, "  %s cannot get IOMMU group\n", pci_addr);
530                 return -1;
531         }
532
533         /* IOMMU group is always the last token */
534         errno = 0;
535         group_tok = tok[ret - 1];
536         end = group_tok;
537         *iommu_group_no = strtol(group_tok, &end, 10);
538         if ((end != group_tok && *end != '\0') || errno != 0) {
539                 RTE_LOG(ERR, EAL, "  %s error parsing IOMMU number!\n", pci_addr);
540                 return -1;
541         }
542
543         return 1;
544 }
545
546 static void
547 clear_current_group(void)
548 {
549         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = 0;
550         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = -1;
551 }
552
553
554 /*
555  * map the PCI resources of a PCI device in virtual memory (VFIO version).
556  * primary and secondary processes follow almost exactly the same path
557  */
558 int
559 pci_vfio_map_resource(struct rte_pci_device *dev)
560 {
561         struct vfio_group_status group_status = {
562                         .argsz = sizeof(group_status)
563         };
564         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
565         int vfio_group_fd, vfio_dev_fd;
566         int iommu_group_no;
567         char pci_addr[PATH_MAX] = {0};
568         struct rte_pci_addr *loc = &dev->addr;
569         int i, ret, msix_bar;
570         struct mapped_pci_resource *vfio_res = NULL;
571         struct mapped_pci_res_list *vfio_res_list = RTE_TAILQ_CAST(rte_vfio_tailq.head, mapped_pci_res_list);
572
573         struct pci_map *maps;
574         uint32_t msix_table_offset = 0;
575         uint32_t msix_table_size = 0;
576
577         dev->intr_handle.fd = -1;
578         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
579
580         /* store PCI address string */
581         snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
582                         loc->domain, loc->bus, loc->devid, loc->function);
583
584         /* get group number */
585         ret = pci_vfio_get_group_no(pci_addr, &iommu_group_no);
586         if (ret == 0) {
587                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
588                         pci_addr);
589                 return 1;
590         }
591
592         /* if negative, something failed */
593         if (ret < 0)
594                 return -1;
595
596         /* get the actual group fd */
597         vfio_group_fd = pci_vfio_get_group_fd(iommu_group_no);
598         if (vfio_group_fd < 0)
599                 return -1;
600
601         /* store group fd */
602         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].group_no = iommu_group_no;
603         vfio_cfg.vfio_groups[vfio_cfg.vfio_group_idx].fd = vfio_group_fd;
604
605         /* if group_fd == 0, that means the device isn't managed by VFIO */
606         if (vfio_group_fd == 0) {
607                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
608                                 pci_addr);
609                 /* we store 0 as group fd to distinguish between existing but
610                  * unbound VFIO groups, and groups that don't exist at all.
611                  */
612                 vfio_cfg.vfio_group_idx++;
613                 return 1;
614         }
615
616         /*
617          * at this point, we know at least one port on this device is bound to VFIO,
618          * so we can proceed to try and set this particular port up
619          */
620
621         /* check if the group is viable */
622         ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
623         if (ret) {
624                 RTE_LOG(ERR, EAL, "  %s cannot get group status, "
625                                 "error %i (%s)\n", pci_addr, errno, strerror(errno));
626                 close(vfio_group_fd);
627                 clear_current_group();
628                 return -1;
629         } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
630                 RTE_LOG(ERR, EAL, "  %s VFIO group is not viable!\n", pci_addr);
631                 close(vfio_group_fd);
632                 clear_current_group();
633                 return -1;
634         }
635
636         /*
637          * at this point, we know that this group is viable (meaning, all devices
638          * are either bound to VFIO or not bound to anything)
639          */
640
641         /* check if group does not have a container yet */
642         if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
643
644                 /* add group to a container */
645                 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
646                                 &vfio_cfg.vfio_container_fd);
647                 if (ret) {
648                         RTE_LOG(ERR, EAL, "  %s cannot add VFIO group to container, "
649                                         "error %i (%s)\n", pci_addr, errno, strerror(errno));
650                         close(vfio_group_fd);
651                         clear_current_group();
652                         return -1;
653                 }
654                 /*
655                  * at this point we know that this group has been successfully
656                  * initialized, so we increment vfio_group_idx to indicate that we can
657                  * add new groups.
658                  */
659                 vfio_cfg.vfio_group_idx++;
660         }
661
662         /*
663          * set up DMA mappings for container
664          *
665          * needs to be done only once, only when at least one group is assigned to
666          * a container and only in primary process
667          */
668         if (internal_config.process_type == RTE_PROC_PRIMARY &&
669                         vfio_cfg.vfio_container_has_dma == 0) {
670                 ret = pci_vfio_setup_dma_maps(vfio_cfg.vfio_container_fd);
671                 if (ret) {
672                         RTE_LOG(ERR, EAL, "  %s DMA remapping failed, "
673                                         "error %i (%s)\n", pci_addr, errno, strerror(errno));
674                         return -1;
675                 }
676                 vfio_cfg.vfio_container_has_dma = 1;
677         }
678
679         /* get a file descriptor for the device */
680         vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, pci_addr);
681         if (vfio_dev_fd < 0) {
682                 /* if we cannot get a device fd, this simply means that this
683                  * particular port is not bound to VFIO
684                  */
685                 RTE_LOG(WARNING, EAL, "  %s not managed by VFIO driver, skipping\n",
686                                 pci_addr);
687                 return 1;
688         }
689
690         /* test and setup the device */
691         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info);
692         if (ret) {
693                 RTE_LOG(ERR, EAL, "  %s cannot get device info, "
694                                 "error %i (%s)\n", pci_addr, errno, strerror(errno));
695                 close(vfio_dev_fd);
696                 return -1;
697         }
698
699         /* get MSI-X BAR, if any (we have to know where it is because we can't
700          * easily mmap it when using VFIO) */
701         msix_bar = -1;
702         ret = pci_vfio_get_msix_bar(vfio_dev_fd, &msix_bar,
703                                     &msix_table_offset, &msix_table_size);
704         if (ret < 0) {
705                 RTE_LOG(ERR, EAL, "  %s cannot get MSI-X BAR number!\n", pci_addr);
706                 close(vfio_dev_fd);
707                 return -1;
708         }
709
710         /* if we're in a primary process, allocate vfio_res and get region info */
711         if (internal_config.process_type == RTE_PROC_PRIMARY) {
712                 vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
713                 if (vfio_res == NULL) {
714                         RTE_LOG(ERR, EAL,
715                                 "%s(): cannot store uio mmap details\n", __func__);
716                         close(vfio_dev_fd);
717                         return -1;
718                 }
719                 memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
720
721                 /* get number of registers (up to BAR5) */
722                 vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
723                                 VFIO_PCI_BAR5_REGION_INDEX + 1);
724         } else {
725                 /* if we're in a secondary process, just find our tailq entry */
726                 TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
727                         if (memcmp(&vfio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
728                                 continue;
729                         break;
730                 }
731                 /* if we haven't found our tailq entry, something's wrong */
732                 if (vfio_res == NULL) {
733                         RTE_LOG(ERR, EAL, "  %s cannot find TAILQ entry for PCI device!\n",
734                                         pci_addr);
735                         close(vfio_dev_fd);
736                         return -1;
737                 }
738         }
739
740         /* map BARs */
741         maps = vfio_res->maps;
742
743         for (i = 0; i < (int) vfio_res->nb_maps; i++) {
744                 struct vfio_region_info reg = { .argsz = sizeof(reg) };
745                 void *bar_addr;
746                 struct memreg {
747                         unsigned long offset, size;
748                 } memreg[2] = {};
749
750                 reg.index = i;
751
752                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg);
753
754                 if (ret) {
755                         RTE_LOG(ERR, EAL, "  %s cannot get device region info "
756                                         "error %i (%s)\n", pci_addr, errno, strerror(errno));
757                         close(vfio_dev_fd);
758                         if (internal_config.process_type == RTE_PROC_PRIMARY)
759                                 rte_free(vfio_res);
760                         return -1;
761                 }
762
763                 /* skip non-mmapable BARs */
764                 if ((reg.flags & VFIO_REGION_INFO_FLAG_MMAP) == 0)
765                         continue;
766
767                 if (i == msix_bar) {
768                         /*
769                          * VFIO will not let us map the MSI-X table,
770                          * but we can map around it.
771                          */
772                         uint32_t table_start = msix_table_offset;
773                         uint32_t table_end = table_start + msix_table_size;
774                         table_end = (table_end + ~PAGE_MASK) & PAGE_MASK;
775                         table_start &= PAGE_MASK;
776
777                         if (table_start == 0 && table_end >= reg.size) {
778                                 /* Cannot map this BAR */
779                                 RTE_LOG(DEBUG, EAL, "Skipping BAR %d\n", i);
780                                 continue;
781                         } else {
782                                 memreg[0].offset = reg.offset;
783                                 memreg[0].size = table_start;
784                                 memreg[1].offset = table_end;
785                                 memreg[1].size = reg.size - table_end;
786
787                                 RTE_LOG(DEBUG, EAL,
788                                         "Trying to map BAR %d that contains the MSI-X "
789                                         "table. Trying offsets: "
790                                         "0x%04lx:0x%04lx, 0x%04lx:0x%04lx\n", i,
791                                         memreg[0].offset, memreg[0].size,
792                                         memreg[1].offset, memreg[1].size);
793                         }
794                 } else {
795                         memreg[0].offset = reg.offset;
796                         memreg[0].size = reg.size;
797                 }
798
799                 /* try to figure out an address */
800                 if (internal_config.process_type == RTE_PROC_PRIMARY) {
801                         /* try mapping somewhere close to the end of hugepages */
802                         if (pci_map_addr == NULL)
803                                 pci_map_addr = pci_find_max_end_va();
804
805                         bar_addr = pci_map_addr;
806                         pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg.size);
807                 } else {
808                         bar_addr = maps[i].addr;
809                 }
810
811                 /* reserve the address using an inaccessible mapping */
812                 bar_addr = mmap(bar_addr, reg.size, 0, MAP_PRIVATE |
813                                 MAP_ANONYMOUS, -1, 0);
814                 if (bar_addr != MAP_FAILED) {
815                         void *map_addr = NULL;
816                         if (memreg[0].size) {
817                                 /* actual map of first part */
818                                 map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
819                                                             memreg[0].offset,
820                                                             memreg[0].size,
821                                                             MAP_FIXED);
822                         }
823
824                         /* if there's a second part, try to map it */
825                         if (map_addr != MAP_FAILED
826                             && memreg[1].offset && memreg[1].size) {
827                                 void *second_addr = RTE_PTR_ADD(bar_addr, memreg[1].offset);
828                                 map_addr = pci_map_resource(second_addr,
829                                                             vfio_dev_fd, memreg[1].offset,
830                                                             memreg[1].size,
831                                                             MAP_FIXED);
832                         }
833
834                         if (map_addr == MAP_FAILED || !map_addr) {
835                                 munmap(bar_addr, reg.size);
836                                 bar_addr = MAP_FAILED;
837                         }
838                 }
839
840                 if (bar_addr == MAP_FAILED ||
841                                 (internal_config.process_type == RTE_PROC_SECONDARY &&
842                                                 bar_addr != maps[i].addr)) {
843                         RTE_LOG(ERR, EAL, "  %s mapping BAR%i failed: %s\n", pci_addr, i,
844                                         strerror(errno));
845                         close(vfio_dev_fd);
846                         if (internal_config.process_type == RTE_PROC_PRIMARY)
847                                 rte_free(vfio_res);
848                         return -1;
849                 }
850
851                 maps[i].addr = bar_addr;
852                 maps[i].offset = reg.offset;
853                 maps[i].size = reg.size;
854                 maps[i].path = NULL; /* vfio doesn't have per-resource paths */
855                 dev->mem_resource[i].addr = bar_addr;
856         }
857
858         /* if secondary process, do not set up interrupts */
859         if (internal_config.process_type == RTE_PROC_PRIMARY) {
860                 if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
861                         RTE_LOG(ERR, EAL, "  %s error setting up interrupts!\n", pci_addr);
862                         close(vfio_dev_fd);
863                         rte_free(vfio_res);
864                         return -1;
865                 }
866
867                 /* set bus mastering for the device */
868                 if (pci_vfio_set_bus_master(vfio_dev_fd)) {
869                         RTE_LOG(ERR, EAL, "  %s cannot set up bus mastering!\n", pci_addr);
870                         close(vfio_dev_fd);
871                         rte_free(vfio_res);
872                         return -1;
873                 }
874
875                 /* Reset the device */
876                 ioctl(vfio_dev_fd, VFIO_DEVICE_RESET);
877         }
878
879         if (internal_config.process_type == RTE_PROC_PRIMARY)
880                 TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
881
882         return 0;
883 }
884
885 int
886 pci_vfio_enable(void)
887 {
888         /* initialize group list */
889         int i;
890         int module_vfio_type1;
891
892         for (i = 0; i < VFIO_MAX_GROUPS; i++) {
893                 vfio_cfg.vfio_groups[i].fd = -1;
894                 vfio_cfg.vfio_groups[i].group_no = -1;
895         }
896
897         module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
898
899         /* return error directly */
900         if (module_vfio_type1 == -1) {
901                 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
902                 return -1;
903         }
904
905         /* return 0 if VFIO modules not loaded */
906         if (module_vfio_type1 == 0) {
907                 RTE_LOG(INFO, EAL, "VFIO modules not all loaded, "
908                         "skip VFIO support...\n");
909                 return 0;
910         }
911
912         vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
913
914         /* check if we have VFIO driver enabled */
915         if (vfio_cfg.vfio_container_fd != -1)
916                 vfio_cfg.vfio_enabled = 1;
917         else
918                 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
919
920         return 0;
921 }
922
923 int
924 pci_vfio_is_enabled(void)
925 {
926         return vfio_cfg.vfio_enabled;
927 }
928 #endif