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.
21 * determine if VFIO is present on the system
23 #if !defined(VFIO_PRESENT) && defined(RTE_EAL_VFIO)
24 #include <linux/version.h>
25 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
27 #endif /* kernel version >= 3.6.0 */
28 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)
29 #define HAVE_VFIO_DEV_REQ_INTERFACE
30 #endif /* kernel version >= 4.0.0 */
31 #endif /* RTE_EAL_VFIO */
35 #include <linux/vfio.h>
37 #define VFIO_DIR "/dev/vfio"
38 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
39 #define VFIO_GROUP_FMT "/dev/vfio/%u"
40 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
41 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
42 #define VFIO_GET_REGION_IDX(x) (x >> 40)
43 #define VFIO_NOIOMMU_MODE \
44 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
46 /* NOIOMMU is defined from kernel version 4.5 onwards */
47 #ifdef VFIO_NOIOMMU_IOMMU
48 #define RTE_VFIO_NOIOMMU VFIO_NOIOMMU_IOMMU
50 #define RTE_VFIO_NOIOMMU 8
54 * capabilities are only supported on kernel 4.6+. there were also some API
55 * changes as well, so add a macro to get cap offset.
57 #ifdef VFIO_REGION_INFO_FLAG_CAPS
58 #define RTE_VFIO_INFO_FLAG_CAPS VFIO_REGION_INFO_FLAG_CAPS
59 #define VFIO_CAP_OFFSET(x) (x->cap_offset)
61 #define RTE_VFIO_INFO_FLAG_CAPS (1 << 3)
62 #define VFIO_CAP_OFFSET(x) (x->resv)
63 struct vfio_info_cap_header {
70 /* kernels 4.16+ can map BAR containing MSI-X table */
71 #ifdef VFIO_REGION_INFO_CAP_MSIX_MAPPABLE
72 #define RTE_VFIO_CAP_MSIX_MAPPABLE VFIO_REGION_INFO_CAP_MSIX_MAPPABLE
74 #define RTE_VFIO_CAP_MSIX_MAPPABLE 3
77 #else /* not VFIO_PRESENT */
79 /* we don't need an actual definition, only pointer is used */
80 struct vfio_device_info;
82 #endif /* VFIO_PRESENT */
84 #define RTE_VFIO_DEFAULT_CONTAINER_FD (-1)
87 * Setup vfio_cfg for the device identified by its address.
88 * It discovers the configured I/O MMU groups or sets a new one for the device.
89 * If a new groups is assigned, the DMA mapping is performed.
91 * This function is only relevant to linux and will return
104 * Device information.
109 * >1 if the device cannot be managed this way.
111 int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
112 int *vfio_dev_fd, struct vfio_device_info *device_info);
115 * Release a device mapped to a VFIO-managed I/O MMU group.
117 * This function is only relevant to linux and will return
133 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
136 * Enable a VFIO-related kmod.
138 * This function is only relevant to linux and will return
142 * kernel module name.
148 int rte_vfio_enable(const char *modname);
151 * Check whether a VFIO-related kmod is enabled.
153 * This function is only relevant to linux and will return
157 * kernel module name.
163 int rte_vfio_is_enabled(const char *modname);
166 * Whether VFIO NOIOMMU mode is enabled.
168 * This function is only relevant to linux and will return
176 int rte_vfio_noiommu_is_enabled(void);
179 * Remove group fd from internal VFIO group fd array/
181 * This function is only relevant to linux and will return
184 * @param vfio_group_fd
192 rte_vfio_clear_group(int vfio_group_fd);
195 * Parse IOMMU group number for a device
197 * This function is only relevant to linux and will return
206 * @param iommu_group_num
211 * 0 for non-existent group or VFIO
215 rte_vfio_get_group_num(const char *sysfs_base,
216 const char *dev_addr, int *iommu_group_num);
219 * Open a new VFIO container fd
221 * This function is only relevant to linux and will return
229 rte_vfio_get_container_fd(void);
232 * Open VFIO group fd or get an existing one
234 * This function is only relevant to linux and will return
237 * @param iommu_group_num
245 rte_vfio_get_group_fd(int iommu_group_num);
248 * Create a new container for device binding.
250 * @note Any newly allocated DPDK memory will not be mapped into these
251 * containers by default, user needs to manage DMA mappings for
252 * any container created by this API.
254 * @note When creating containers using this API, the container will only be
255 * available in the process that has created it. Sharing containers and
256 * devices between multiple processes is not supported.
259 * the container fd if successful
263 rte_vfio_container_create(void);
266 * Destroy the container, unbind all vfio groups within it.
268 * @param container_fd
269 * the container fd to destroy
276 rte_vfio_container_destroy(int container_fd);
279 * Bind a IOMMU group to a container.
281 * @param container_fd
284 * @param iommu_group_num
285 * the iommu group number to bind to container
288 * group fd if successful
292 rte_vfio_container_group_bind(int container_fd, int iommu_group_num);
295 * Unbind a IOMMU group from a container.
297 * @param container_fd
298 * the container fd of container
300 * @param iommu_group_num
301 * the iommu group number to delete from container
308 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num);
311 * Perform DMA mapping for devices in a container.
313 * @param container_fd
314 * the specified container fd. Use RTE_VFIO_DEFAULT_CONTAINER_FD to
315 * use the default container.
318 * Starting virtual address of memory to be mapped.
321 * Starting IOVA address of memory to be mapped.
324 * Length of memory segment being mapped.
331 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr,
332 uint64_t iova, uint64_t len);
335 * Perform DMA unmapping for devices in a container.
337 * @param container_fd
338 * the specified container fd. Use RTE_VFIO_DEFAULT_CONTAINER_FD to
339 * use the default container.
342 * Starting virtual address of memory to be unmapped.
345 * Starting IOVA address of memory to be unmapped.
348 * Length of memory segment being unmapped.
355 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr,
356 uint64_t iova, uint64_t len);
362 #endif /* _RTE_VFIO_H_ */