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