1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 6WIND S.A.
10 * RTE VFIO. This library provides various VFIO related utility functions.
14 * determine if VFIO is present on the system
16 #if !defined(VFIO_PRESENT) && defined(RTE_EAL_VFIO)
17 #include <linux/version.h>
18 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
20 #endif /* kernel version >= 3.6.0 */
21 #endif /* RTE_EAL_VFIO */
25 #include <linux/vfio.h>
27 #define VFIO_DIR "/dev/vfio"
28 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
29 #define VFIO_GROUP_FMT "/dev/vfio/%u"
30 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
31 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
32 #define VFIO_GET_REGION_IDX(x) (x >> 40)
33 #define VFIO_NOIOMMU_MODE \
34 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
40 /* NOIOMMU is defined from kernel version 4.5 onwards */
41 #ifdef VFIO_NOIOMMU_IOMMU
42 #define RTE_VFIO_NOIOMMU VFIO_NOIOMMU_IOMMU
44 #define RTE_VFIO_NOIOMMU 8
48 * Setup vfio_cfg for the device identified by its address.
49 * It discovers the configured I/O MMU groups or sets a new one for the device.
50 * If a new groups is assigned, the DMA mapping is performed.
52 * This function is only relevant to linux and will return
70 * >1 if the device cannot be managed this way.
72 int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
73 int *vfio_dev_fd, struct vfio_device_info *device_info);
76 * Release a device mapped to a VFIO-managed I/O MMU group.
78 * This function is only relevant to linux and will return
94 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
97 * Enable a VFIO-related kmod.
99 * This function is only relevant to linux and will return
103 * kernel module name.
109 int rte_vfio_enable(const char *modname);
112 * Check whether a VFIO-related kmod is enabled.
114 * This function is only relevant to linux and will return
118 * kernel module name.
124 int rte_vfio_is_enabled(const char *modname);
127 * Whether VFIO NOIOMMU mode is enabled.
129 * This function is only relevant to linux and will return
136 int rte_vfio_noiommu_is_enabled(void);
139 * Remove group fd from internal VFIO group fd array/
141 * This function is only relevant to linux and will return
144 * @param vfio_group_fd
152 rte_vfio_clear_group(int vfio_group_fd);
155 * Map memory region for use with VFIO.
157 * @note requires at least one device to be attached at the time of mapping.
160 * Starting virtual address of memory to be mapped.
163 * Starting IOVA address of memory to be mapped.
166 * Length of memory segment being mapped.
172 int __rte_experimental
173 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len);
177 * Unmap memory region from VFIO.
180 * Starting virtual address of memory to be unmapped.
183 * Starting IOVA address of memory to be unmapped.
186 * Length of memory segment being unmapped.
193 int __rte_experimental
194 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len);
196 * Parse IOMMU group number for a device
198 * This function is only relevant to linux and will return
207 * @param iommu_group_num
212 * 0 for non-existent group or VFIO
215 int __rte_experimental
216 rte_vfio_get_group_num(const char *sysfs_base,
217 const char *dev_addr, int *iommu_group_num);
220 * Open VFIO container fd or get an existing one
222 * This function is only relevant to linux and will return
229 int __rte_experimental
230 rte_vfio_get_container_fd(void);
233 * Open VFIO group fd or get an existing one
235 * This function is only relevant to linux and will return
238 * @param iommu_group_num
245 int __rte_experimental
246 rte_vfio_get_group_fd(int iommu_group_num);
252 #endif /* VFIO_PRESENT */
254 #endif /* _RTE_VFIO_H_ */