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.
20 * determine if VFIO is present on the system
22 #if !defined(VFIO_PRESENT) && defined(RTE_EAL_VFIO)
23 #include <linux/version.h>
24 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
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 */
34 #include <linux/vfio.h>
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"
45 /* NOIOMMU is defined from kernel version 4.5 onwards */
46 #ifdef VFIO_NOIOMMU_IOMMU
47 #define RTE_VFIO_NOIOMMU VFIO_NOIOMMU_IOMMU
49 #define RTE_VFIO_NOIOMMU 8
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.
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)
60 #define RTE_VFIO_INFO_FLAG_CAPS (1 << 3)
61 #define VFIO_CAP_OFFSET(x) (x->resv)
62 struct vfio_info_cap_header {
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
73 #define RTE_VFIO_CAP_MSIX_MAPPABLE 3
76 #else /* not VFIO_PRESENT */
78 /* we don't need an actual definition, only pointer is used */
79 struct vfio_device_info;
81 #endif /* VFIO_PRESENT */
84 * Setup vfio_cfg for the device identified by its address.
85 * It discovers the configured I/O MMU groups or sets a new one for the device.
86 * If a new groups is assigned, the DMA mapping is performed.
88 * This function is only relevant to linux and will return
101 * Device information.
106 * >1 if the device cannot be managed this way.
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);
112 * Release a device mapped to a VFIO-managed I/O MMU group.
114 * This function is only relevant to linux and will return
130 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
133 * Enable a VFIO-related kmod.
135 * This function is only relevant to linux and will return
139 * kernel module name.
145 int rte_vfio_enable(const char *modname);
148 * Check whether a VFIO-related kmod is enabled.
150 * This function is only relevant to linux and will return
154 * kernel module name.
160 int rte_vfio_is_enabled(const char *modname);
163 * Whether VFIO NOIOMMU mode is enabled.
165 * This function is only relevant to linux and will return
172 int rte_vfio_noiommu_is_enabled(void);
175 * Remove group fd from internal VFIO group fd array/
177 * This function is only relevant to linux and will return
180 * @param vfio_group_fd
188 rte_vfio_clear_group(int vfio_group_fd);
191 * Map memory region for use with VFIO.
193 * @note Require at least one device to be attached at the time of
194 * mapping. DMA maps done via this API will only apply to default
195 * container and will not apply to any of the containers created
196 * via rte_vfio_container_create().
199 * Starting virtual address of memory to be mapped.
202 * Starting IOVA address of memory to be mapped.
205 * Length of memory segment being mapped.
212 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len);
216 * Unmap memory region from VFIO.
219 * Starting virtual address of memory to be unmapped.
222 * Starting IOVA address of memory to be unmapped.
225 * Length of memory segment being unmapped.
233 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len);
235 * Parse IOMMU group number for a device
237 * This function is only relevant to linux and will return
246 * @param iommu_group_num
251 * 0 for non-existent group or VFIO
255 rte_vfio_get_group_num(const char *sysfs_base,
256 const char *dev_addr, int *iommu_group_num);
259 * Open a new VFIO container fd
261 * This function is only relevant to linux and will return
269 rte_vfio_get_container_fd(void);
272 * Open VFIO group fd or get an existing one
274 * This function is only relevant to linux and will return
277 * @param iommu_group_num
285 rte_vfio_get_group_fd(int iommu_group_num);
288 * Create a new container for device binding.
290 * @note Any newly allocated DPDK memory will not be mapped into these
291 * containers by default, user needs to manage DMA mappings for
292 * any container created by this API.
295 * the container fd if successful
299 rte_vfio_container_create(void);
302 * Destroy the container, unbind all vfio groups within it.
304 * @param container_fd
305 * the container fd to destroy
312 rte_vfio_container_destroy(int container_fd);
315 * Bind a IOMMU group to a container.
317 * @param container_fd
320 * @param iommu_group_num
321 * the iommu group number to bind to container
324 * group fd if successful
328 rte_vfio_container_group_bind(int container_fd, int iommu_group_num);
331 * Unbind a IOMMU group from a container.
333 * @param container_fd
334 * the container fd of container
336 * @param iommu_group_num
337 * the iommu group number to delete from container
344 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num);
347 * Perform DMA mapping for devices in a container.
349 * @param container_fd
350 * the specified container fd
353 * Starting virtual address of memory to be mapped.
356 * Starting IOVA address of memory to be mapped.
359 * Length of memory segment being mapped.
366 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr,
367 uint64_t iova, uint64_t len);
370 * Perform DMA unmapping for devices in a container.
372 * @param container_fd
373 * the specified container fd
376 * Starting virtual address of memory to be unmapped.
379 * Starting IOVA address of memory to be unmapped.
382 * Length of memory segment being unmapped.
389 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr,
390 uint64_t iova, uint64_t len);
396 #endif /* _RTE_VFIO_H_ */