xref: /dpdk/lib/eal/include/rte_vfio.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
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 
8 /**
9  * @file
10  * RTE VFIO. This library provides various VFIO related utility functions.
11  */
12 
13 #include <stdbool.h>
14 #include <stdint.h>
15 
16 #include <rte_compat.h>
17 
18 /*
19  * determine if VFIO is present on the system
20  */
21 #if !defined(VFIO_PRESENT) && defined(RTE_EAL_VFIO)
22 #include <linux/version.h>
23 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
24 #define VFIO_PRESENT
25 #endif /* kernel version >= 3.6.0 */
26 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)
27 #define HAVE_VFIO_DEV_REQ_INTERFACE
28 #endif /* kernel version >= 4.0.0 */
29 #endif /* RTE_EAL_VFIO */
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
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 
114 /**
115  * Setup vfio_cfg for the device identified by its address.
116  * It discovers the configured I/O MMU groups or sets a new one for the device.
117  * If a new groups is assigned, the DMA mapping is performed.
118  *
119  * This function is only relevant to linux and will return
120  * an error on BSD.
121  *
122  * @param sysfs_base
123  *   sysfs path prefix.
124  *
125  * @param dev_addr
126  *   device location.
127  *
128  * @param vfio_dev_fd
129  *   VFIO fd.
130  *
131  * @param device_info
132  *   Device information.
133  *
134  * @return
135  *   0 on success.
136  *   <0 on failure.
137  *   >1 if the device cannot be managed this way.
138  */
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 
142 /**
143  * Release a device mapped to a VFIO-managed I/O MMU group.
144  *
145  * This function is only relevant to linux and will return
146  * an error on BSD.
147  *
148  * @param sysfs_base
149  *   sysfs path prefix.
150  *
151  * @param dev_addr
152  *   device location.
153  *
154  * @param fd
155  *   VFIO fd.
156  *
157  * @return
158  *   0 on success.
159  *   <0 on failure.
160  */
161 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
162 
163 /**
164  * Enable a VFIO-related kmod.
165  *
166  * This function is only relevant to linux and will return
167  * an error on BSD.
168  *
169  * @param modname
170  *   kernel module name.
171  *
172  * @return
173  *   0 on success.
174  *   <0 on failure.
175  */
176 int rte_vfio_enable(const char *modname);
177 
178 /**
179  * Check whether a VFIO-related kmod is enabled.
180  *
181  * This function is only relevant to Linux.
182  *
183  * @param modname
184  *   kernel module name.
185  *
186  * @return
187  *   1 if true.
188  *   0 otherwise.
189  */
190 int rte_vfio_is_enabled(const char *modname);
191 
192 /**
193  * Whether VFIO NOIOMMU mode is enabled.
194  *
195  * This function is only relevant to Linux.
196  *
197  * @return
198  *   1 if true.
199  *   0 if false.
200  *   <0 for errors.
201  */
202 int rte_vfio_noiommu_is_enabled(void);
203 
204 /**
205  * Remove group fd from internal VFIO group fd array/
206  *
207  * This function is only relevant to linux and will return
208  * an error on BSD.
209  *
210  * @param vfio_group_fd
211  *   VFIO Group FD.
212  *
213  * @return
214  *   0 on success.
215  *   <0 on failure.
216  */
217 int
218 rte_vfio_clear_group(int vfio_group_fd);
219 
220 /**
221  * Parse IOMMU group number for a device
222  *
223  * This function is only relevant to linux and will return
224  * an error on BSD.
225  *
226  * @param sysfs_base
227  *   sysfs path prefix.
228  *
229  * @param dev_addr
230  *   device location.
231  *
232  * @param iommu_group_num
233  *   iommu group number
234  *
235  * @return
236  *  >0 on success
237  *   0 for non-existent group or VFIO
238  *  <0 for errors
239  */
240 int
241 rte_vfio_get_group_num(const char *sysfs_base,
242 		      const char *dev_addr, int *iommu_group_num);
243 
244 /**
245  * Get device information
246  *
247  * This function is only relevant to Linux and will return an error on BSD.
248  *
249  * @param sysfs_base
250  *   sysfs path prefix.
251  *
252  * @param dev_addr
253  *   device location.
254  *
255  * @param vfio_dev_fd
256  *   VFIO fd.
257  *
258  * @param device_info
259  *   Device information.
260  *
261  * @return
262  *   0 on success.
263  *  <0 on failure.
264  */
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 
270 /**
271  * Open a new VFIO container fd
272  *
273  * This function is only relevant to linux and will return
274  * an error on BSD.
275  *
276  * @return
277  *  > 0 container fd
278  *  < 0 for errors
279  */
280 int
281 rte_vfio_get_container_fd(void);
282 
283 /**
284  * Open VFIO group fd or get an existing one
285  *
286  * This function is only relevant to linux and will return
287  * an error on BSD.
288  *
289  * @param iommu_group_num
290  *   iommu group number
291  *
292  * @return
293  *  > 0 group fd
294  *  < 0 for errors
295  */
296 int
297 rte_vfio_get_group_fd(int iommu_group_num);
298 
299 /**
300  * Create a new container for device binding.
301  *
302  * @note Any newly allocated DPDK memory will not be mapped into these
303  *       containers by default, user needs to manage DMA mappings for
304  *       any container created by this API.
305  *
306  * @note When creating containers using this API, the container will only be
307  *       available in the process that has created it. Sharing containers and
308  *       devices between multiple processes is not supported.
309  *
310  * @return
311  *   the container fd if successful
312  *   <0 if failed
313  */
314 int
315 rte_vfio_container_create(void);
316 
317 /**
318  * Destroy the container, unbind all vfio groups within it.
319  *
320  * @param container_fd
321  *   the container fd to destroy
322  *
323  * @return
324  *    0 if successful
325  *   <0 if failed
326  */
327 int
328 rte_vfio_container_destroy(int container_fd);
329 
330 /**
331  * Bind a IOMMU group to a container.
332  *
333  * @param container_fd
334  *   the container's fd
335  *
336  * @param iommu_group_num
337  *   the iommu group number to bind to container
338  *
339  * @return
340  *   group fd if successful
341  *   <0 if failed
342  */
343 int
344 rte_vfio_container_group_bind(int container_fd, int iommu_group_num);
345 
346 /**
347  * Unbind a IOMMU group from a container.
348  *
349  * @param container_fd
350  *   the container fd of container
351  *
352  * @param iommu_group_num
353  *   the iommu group number to delete from container
354  *
355  * @return
356  *    0 if successful
357  *   <0 if failed
358  */
359 int
360 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num);
361 
362 /**
363  * Perform DMA mapping for devices in a container.
364  *
365  * @param container_fd
366  *   the specified container fd. Use RTE_VFIO_DEFAULT_CONTAINER_FD to
367  *   use the default container.
368  *
369  * @param vaddr
370  *   Starting virtual address of memory to be mapped.
371  *
372  * @param iova
373  *   Starting IOVA address of memory to be mapped.
374  *
375  * @param len
376  *   Length of memory segment being mapped.
377  *
378  * @return
379  *    0 if successful
380  *   <0 if failed
381  */
382 int
383 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr,
384 		uint64_t iova, uint64_t len);
385 
386 /**
387  * Perform DMA unmapping for devices in a container.
388  *
389  * @param container_fd
390  *   the specified container fd. Use RTE_VFIO_DEFAULT_CONTAINER_FD to
391  *   use the default container.
392  *
393  * @param vaddr
394  *   Starting virtual address of memory to be unmapped.
395  *
396  * @param iova
397  *   Starting IOVA address of memory to be unmapped.
398  *
399  * @param len
400  *   Length of memory segment being unmapped.
401  *
402  * @return
403  *    0 if successful
404  *   <0 if failed
405  */
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_ */
415