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.
18 * determine if VFIO is present on the system
20 #if !defined(VFIO_PRESENT) && defined(RTE_EAL_VFIO)
21 #include <linux/version.h>
22 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
24 #endif /* kernel version >= 3.6.0 */
25 #endif /* RTE_EAL_VFIO */
29 #include <linux/vfio.h>
31 #define VFIO_DIR "/dev/vfio"
32 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
33 #define VFIO_GROUP_FMT "/dev/vfio/%u"
34 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
35 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
36 #define VFIO_GET_REGION_IDX(x) (x >> 40)
37 #define VFIO_NOIOMMU_MODE \
38 "/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
47 #else /* not VFIO_PRESENT */
49 /* we don't need an actual definition, only pointer is used */
50 struct vfio_device_info;
52 #endif /* VFIO_PRESENT */
55 * Setup vfio_cfg for the device identified by its address.
56 * It discovers the configured I/O MMU groups or sets a new one for the device.
57 * If a new groups is assigned, the DMA mapping is performed.
59 * This function is only relevant to linux and will return
77 * >1 if the device cannot be managed this way.
79 int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
80 int *vfio_dev_fd, struct vfio_device_info *device_info);
83 * Release a device mapped to a VFIO-managed I/O MMU group.
85 * This function is only relevant to linux and will return
101 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
104 * Enable a VFIO-related kmod.
106 * This function is only relevant to linux and will return
110 * kernel module name.
116 int rte_vfio_enable(const char *modname);
119 * Check whether a VFIO-related kmod is enabled.
121 * This function is only relevant to linux and will return
125 * kernel module name.
131 int rte_vfio_is_enabled(const char *modname);
134 * Whether VFIO NOIOMMU mode is enabled.
136 * This function is only relevant to linux and will return
143 int rte_vfio_noiommu_is_enabled(void);
146 * Remove group fd from internal VFIO group fd array/
148 * This function is only relevant to linux and will return
151 * @param vfio_group_fd
159 rte_vfio_clear_group(int vfio_group_fd);
162 * Map memory region for use with VFIO.
164 * @note Require at least one device to be attached at the time of
165 * mapping. DMA maps done via this API will only apply to default
166 * container and will not apply to any of the containers created
167 * via rte_vfio_container_create().
170 * Starting virtual address of memory to be mapped.
173 * Starting IOVA address of memory to be mapped.
176 * Length of memory segment being mapped.
182 int __rte_experimental
183 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len);
187 * Unmap memory region from VFIO.
190 * Starting virtual address of memory to be unmapped.
193 * Starting IOVA address of memory to be unmapped.
196 * Length of memory segment being unmapped.
203 int __rte_experimental
204 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len);
206 * Parse IOMMU group number for a device
208 * This function is only relevant to linux and will return
217 * @param iommu_group_num
222 * 0 for non-existent group or VFIO
225 int __rte_experimental
226 rte_vfio_get_group_num(const char *sysfs_base,
227 const char *dev_addr, int *iommu_group_num);
230 * Open VFIO container fd or get an existing one
232 * This function is only relevant to linux and will return
239 int __rte_experimental
240 rte_vfio_get_container_fd(void);
243 * Open VFIO group fd or get an existing one
245 * This function is only relevant to linux and will return
248 * @param iommu_group_num
255 int __rte_experimental
256 rte_vfio_get_group_fd(int iommu_group_num);
260 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
262 * Create a new container for device binding.
264 * @note Any newly allocated DPDK memory will not be mapped into these
265 * containers by default, user needs to manage DMA mappings for
266 * any container created by this API.
269 * the container fd if successful
272 int __rte_experimental
273 rte_vfio_container_create(void);
277 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
279 * Destroy the container, unbind all vfio groups within it.
281 * @param container_fd
282 * the container fd to destroy
288 int __rte_experimental
289 rte_vfio_container_destroy(int container_fd);
293 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
295 * Bind a IOMMU group to a container.
297 * @param container_fd
300 * @param iommu_group_num
301 * the iommu group number to bind to container
304 * group fd if successful
307 int __rte_experimental
308 rte_vfio_container_group_bind(int container_fd, int iommu_group_num);
312 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
314 * Unbind a IOMMU group from a container.
316 * @param container_fd
317 * the container fd of container
319 * @param iommu_group_num
320 * the iommu group number to delete from container
326 int __rte_experimental
327 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num);
331 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
333 * Perform DMA mapping for devices in a container.
335 * @param container_fd
336 * the specified container fd
339 * Starting virtual address of memory to be mapped.
342 * Starting IOVA address of memory to be mapped.
345 * Length of memory segment being mapped.
351 int __rte_experimental
352 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr,
353 uint64_t iova, uint64_t len);
357 * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
359 * Perform DMA unmapping for devices in a container.
361 * @param container_fd
362 * the specified container fd
365 * Starting virtual address of memory to be unmapped.
368 * Starting IOVA address of memory to be unmapped.
371 * Length of memory segment being unmapped.
377 int __rte_experimental
378 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr,
379 uint64_t iova, uint64_t len);
385 #endif /* _RTE_VFIO_H_ */