DPDK  18.11.11
rte_vfio.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 6WIND S.A.
3  */
4 
5 #ifndef _RTE_VFIO_H_
6 #define _RTE_VFIO_H_
7 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 #include <stdint.h>
18 
19 /*
20  * determine if VFIO is present on the system
21  */
22 #if !defined(VFIO_PRESENT) && defined(RTE_EAL_VFIO)
23 #include <linux/version.h>
24 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
25 #define VFIO_PRESENT
26 #endif /* kernel version >= 3.6.0 */
27 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)
28 #define HAVE_VFIO_DEV_REQ_INTERFACE
29 #endif /* kernel version >= 4.0.0 */
30 #endif /* RTE_EAL_VFIO */
31 
32 #ifdef VFIO_PRESENT
33 
34 #include <linux/vfio.h>
35 
36 #define VFIO_DIR "/dev/vfio"
37 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
38 #define VFIO_GROUP_FMT "/dev/vfio/%u"
39 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
40 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
41 #define VFIO_GET_REGION_IDX(x) (x >> 40)
42 #define VFIO_NOIOMMU_MODE \
43  "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
44 
45 /* NOIOMMU is defined from kernel version 4.5 onwards */
46 #ifdef VFIO_NOIOMMU_IOMMU
47 #define RTE_VFIO_NOIOMMU VFIO_NOIOMMU_IOMMU
48 #else
49 #define RTE_VFIO_NOIOMMU 8
50 #endif
51 
52 /*
53  * capabilities are only supported on kernel 4.6+. there were also some API
54  * changes as well, so add a macro to get cap offset.
55  */
56 #ifdef VFIO_REGION_INFO_FLAG_CAPS
57 #define RTE_VFIO_INFO_FLAG_CAPS VFIO_REGION_INFO_FLAG_CAPS
58 #define VFIO_CAP_OFFSET(x) (x->cap_offset)
59 #else
60 #define RTE_VFIO_INFO_FLAG_CAPS (1 << 3)
61 #define VFIO_CAP_OFFSET(x) (x->resv)
62 struct vfio_info_cap_header {
63  uint16_t id;
64  uint16_t version;
65  uint32_t next;
66 };
67 #endif
68 
69 /* kernels 4.16+ can map BAR containing MSI-X table */
70 #ifdef VFIO_REGION_INFO_CAP_MSIX_MAPPABLE
71 #define RTE_VFIO_CAP_MSIX_MAPPABLE VFIO_REGION_INFO_CAP_MSIX_MAPPABLE
72 #else
73 #define RTE_VFIO_CAP_MSIX_MAPPABLE 3
74 #endif
75 
76 #else /* not VFIO_PRESENT */
77 
78 /* we don't need an actual definition, only pointer is used */
79 struct vfio_device_info;
80 
81 #endif /* VFIO_PRESENT */
82 
108 int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
109  int *vfio_dev_fd, struct vfio_device_info *device_info);
110 
130 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
131 
145 int rte_vfio_enable(const char *modname);
146 
160 int rte_vfio_is_enabled(const char *modname);
161 
173 
187 int
188 rte_vfio_clear_group(int vfio_group_fd);
189 
211 int
212 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len);
213 
214 
232 int
233 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len);
254 int
255 rte_vfio_get_group_num(const char *sysfs_base,
256  const char *dev_addr, int *iommu_group_num);
257 
268 int
270 
284 int
285 rte_vfio_get_group_fd(int iommu_group_num);
286 
302 int
304 
315 int
316 rte_vfio_container_destroy(int container_fd);
317 
331 int
332 rte_vfio_container_group_bind(int container_fd, int iommu_group_num);
333 
347 int
348 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num);
349 
369 int
370 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr,
371  uint64_t iova, uint64_t len);
372 
392 int
393 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr,
394  uint64_t iova, uint64_t len);
395 
396 #ifdef __cplusplus
397 }
398 #endif
399 
400 #endif /* _RTE_VFIO_H_ */
int rte_vfio_enable(const char *modname)
int rte_vfio_noiommu_is_enabled(void)
int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd)
int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr, int *vfio_dev_fd, struct vfio_device_info *device_info)
int rte_vfio_get_group_num(const char *sysfs_base, const char *dev_addr, int *iommu_group_num)
int rte_vfio_clear_group(int vfio_group_fd)
int rte_vfio_container_destroy(int container_fd)
int rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len)
int rte_vfio_get_group_fd(int iommu_group_num)
int rte_vfio_is_enabled(const char *modname)
int rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
int rte_vfio_container_create(void)
int rte_vfio_container_dma_map(int container_fd, uint64_t vaddr, uint64_t iova, uint64_t len)
int rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len)
int rte_vfio_get_container_fd(void)
int rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr, uint64_t iova, uint64_t len)
int rte_vfio_container_group_unbind(int container_fd, int iommu_group_num)