DPDK  24.07.0
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 <stdbool.h>
18 #include <stdint.h>
19 
20 #include <rte_compat.h>
21 
22 /*
23  * determine if VFIO is present on the system
24  */
25 #if !defined(VFIO_PRESENT) && defined(RTE_EAL_VFIO)
26 #include <linux/version.h>
27 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
28 #define VFIO_PRESENT
29 #endif /* kernel version >= 3.6.0 */
30 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)
31 #define HAVE_VFIO_DEV_REQ_INTERFACE
32 #endif /* kernel version >= 4.0.0 */
33 #endif /* RTE_EAL_VFIO */
34 
35 #ifdef VFIO_PRESENT
36 
37 #include <linux/vfio.h>
38 
39 #define VFIO_DIR "/dev/vfio"
40 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
41 #define VFIO_GROUP_FMT "/dev/vfio/%u"
42 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
43 #define VFIO_GET_REGION_IDX(x) (x >> 40)
44 #define VFIO_NOIOMMU_MODE \
45  "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
46 
47 /* NOIOMMU is defined from kernel version 4.5 onwards */
48 #ifdef VFIO_NOIOMMU_IOMMU
49 #define RTE_VFIO_NOIOMMU VFIO_NOIOMMU_IOMMU
50 #else
51 #define RTE_VFIO_NOIOMMU 8
52 #endif
53 
54 /*
55  * capabilities are only supported on kernel 4.6+. there were also some API
56  * changes as well, so add a macro to get cap offset.
57  */
58 #ifdef VFIO_REGION_INFO_FLAG_CAPS
59 #define RTE_VFIO_INFO_FLAG_CAPS VFIO_REGION_INFO_FLAG_CAPS
60 #define VFIO_CAP_OFFSET(x) (x->cap_offset)
61 #else
62 #define RTE_VFIO_INFO_FLAG_CAPS (1 << 3)
63 #define VFIO_CAP_OFFSET(x) (x->resv)
64 struct vfio_info_cap_header {
65  uint16_t id;
66  uint16_t version;
67  uint32_t next;
68 };
69 #endif
70 
71 /* kernels 4.16+ can map BAR containing MSI-X table */
72 #ifdef VFIO_REGION_INFO_CAP_MSIX_MAPPABLE
73 #define RTE_VFIO_CAP_MSIX_MAPPABLE VFIO_REGION_INFO_CAP_MSIX_MAPPABLE
74 #else
75 #define RTE_VFIO_CAP_MSIX_MAPPABLE 3
76 #endif
77 
78 /* VFIO_DEVICE_FEATURE is defined for kernel version 5.7 and newer. */
79 #ifdef VFIO_DEVICE_FEATURE
80 #define RTE_VFIO_DEVICE_FEATURE VFIO_DEVICE_FEATURE
81 #else
82 #define RTE_VFIO_DEVICE_FEATURE _IO(VFIO_TYPE, VFIO_BASE + 17)
83 struct vfio_device_feature {
84  __u32 argsz;
85  __u32 flags;
86 #define VFIO_DEVICE_FEATURE_MASK (0xffff) /* 16-bit feature index */
87 #define VFIO_DEVICE_FEATURE_GET (1 << 16) /* Get feature into data[] */
88 #define VFIO_DEVICE_FEATURE_SET (1 << 17) /* Set feature from data[] */
89 #define VFIO_DEVICE_FEATURE_PROBE (1 << 18) /* Probe feature support */
90  __u8 data[];
91 };
92 #endif
93 
94 #ifdef VFIO_DEVICE_FEATURE_BUS_MASTER
95 #define RTE_VFIO_DEVICE_FEATURE_BUS_MASTER VFIO_DEVICE_FEATURE_BUS_MASTER
96 #else
97 #define RTE_VFIO_DEVICE_FEATURE_BUS_MASTER 10
98 struct vfio_device_feature_bus_master {
99  __u32 op;
100 #define VFIO_DEVICE_FEATURE_CLEAR_MASTER 0 /* Clear Bus Master */
101 #define VFIO_DEVICE_FEATURE_SET_MASTER 1 /* Set Bus Master */
102 };
103 #endif
104 
105 #else /* not VFIO_PRESENT */
106 
107 /* we don't need an actual definition, only pointer is used */
108 struct vfio_device_info;
109 
110 #endif /* VFIO_PRESENT */
111 
112 #define RTE_VFIO_DEFAULT_CONTAINER_FD (-1)
113 
139 int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
140  int *vfio_dev_fd, struct vfio_device_info *device_info);
141 
161 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
162 
176 int rte_vfio_enable(const char *modname);
177 
190 int rte_vfio_is_enabled(const char *modname);
191 
203 
217 int
218 rte_vfio_clear_group(int vfio_group_fd);
219 
240 int
241 rte_vfio_get_group_num(const char *sysfs_base,
242  const char *dev_addr, int *iommu_group_num);
243 
265 __rte_experimental
266 int
267 rte_vfio_get_device_info(const char *sysfs_base, const char *dev_addr,
268  int *vfio_dev_fd, struct vfio_device_info *device_info);
269 
280 int
282 
296 int
297 rte_vfio_get_group_fd(int iommu_group_num);
298 
314 int
316 
327 int
328 rte_vfio_container_destroy(int container_fd);
329 
343 int
344 rte_vfio_container_group_bind(int container_fd, int iommu_group_num);
345 
359 int
360 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num);
361 
382 int
383 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr,
384  uint64_t iova, uint64_t len);
385 
406 int
407 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr,
408  uint64_t iova, uint64_t len);
409 
410 #ifdef __cplusplus
411 }
412 #endif
413 
414 #endif /* _RTE_VFIO_H_ */
int rte_vfio_enable(const char *modname)
__rte_experimental int rte_vfio_get_device_info(const char *sysfs_base, const char *dev_addr, int *vfio_dev_fd, struct vfio_device_info *device_info)
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_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_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)