New upstream version 18.08
[deb_dpdk.git] / lib / librte_eal / common / include / rte_vfio.h
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 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 /*
18  * determine if VFIO is present on the system
19  */
20 #if !defined(VFIO_PRESENT) && defined(RTE_EAL_VFIO)
21 #include <linux/version.h>
22 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
23 #define VFIO_PRESENT
24 #endif /* kernel version >= 3.6.0 */
25 #endif /* RTE_EAL_VFIO */
26
27 #ifdef VFIO_PRESENT
28
29 #include <linux/vfio.h>
30
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"
39
40 /* NOIOMMU is defined from kernel version 4.5 onwards */
41 #ifdef VFIO_NOIOMMU_IOMMU
42 #define RTE_VFIO_NOIOMMU VFIO_NOIOMMU_IOMMU
43 #else
44 #define RTE_VFIO_NOIOMMU 8
45 #endif
46
47 #else /* not VFIO_PRESENT */
48
49 /* we don't need an actual definition, only pointer is used */
50 struct vfio_device_info;
51
52 #endif /* VFIO_PRESENT */
53
54 /**
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.
58  *
59  * This function is only relevant to linux and will return
60  * an error on BSD.
61  *
62  * @param sysfs_base
63  *   sysfs path prefix.
64  *
65  * @param dev_addr
66  *   device location.
67  *
68  * @param vfio_dev_fd
69  *   VFIO fd.
70  *
71  * @param device_info
72  *   Device information.
73  *
74  * @return
75  *   0 on success.
76  *   <0 on failure.
77  *   >1 if the device cannot be managed this way.
78  */
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);
81
82 /**
83  * Release a device mapped to a VFIO-managed I/O MMU group.
84  *
85  * This function is only relevant to linux and will return
86  * an error on BSD.
87  *
88  * @param sysfs_base
89  *   sysfs path prefix.
90  *
91  * @param dev_addr
92  *   device location.
93  *
94  * @param fd
95  *   VFIO fd.
96  *
97  * @return
98  *   0 on success.
99  *   <0 on failure.
100  */
101 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
102
103 /**
104  * Enable a VFIO-related kmod.
105  *
106  * This function is only relevant to linux and will return
107  * an error on BSD.
108  *
109  * @param modname
110  *   kernel module name.
111  *
112  * @return
113  *   0 on success.
114  *   <0 on failure.
115  */
116 int rte_vfio_enable(const char *modname);
117
118 /**
119  * Check whether a VFIO-related kmod is enabled.
120  *
121  * This function is only relevant to linux and will return
122  * an error on BSD.
123  *
124  * @param modname
125  *   kernel module name.
126  *
127  * @return
128  *   !0 if true.
129  *   0 otherwise.
130  */
131 int rte_vfio_is_enabled(const char *modname);
132
133 /**
134  * Whether VFIO NOIOMMU mode is enabled.
135  *
136  * This function is only relevant to linux and will return
137  * an error on BSD.
138  *
139  * @return
140  *   !0 if true.
141  *   0 otherwise.
142  */
143 int rte_vfio_noiommu_is_enabled(void);
144
145 /**
146  * Remove group fd from internal VFIO group fd array/
147  *
148  * This function is only relevant to linux and will return
149  * an error on BSD.
150  *
151  * @param vfio_group_fd
152  *   VFIO Grouup FD.
153  *
154  * @return
155  *   0 on success.
156  *   <0 on failure.
157  */
158 int
159 rte_vfio_clear_group(int vfio_group_fd);
160
161 /**
162  * Map memory region for use with VFIO.
163  *
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().
168  *
169  * @param vaddr
170  *   Starting virtual address of memory to be mapped.
171  *
172  * @param iova
173  *   Starting IOVA address of memory to be mapped.
174  *
175  * @param len
176  *   Length of memory segment being mapped.
177  *
178  * @return
179  *   0 if success.
180  *   -1 on error.
181  */
182 int
183 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len);
184
185
186 /**
187  * Unmap memory region from VFIO.
188  *
189  * @param vaddr
190  *   Starting virtual address of memory to be unmapped.
191  *
192  * @param iova
193  *   Starting IOVA address of memory to be unmapped.
194  *
195  * @param len
196  *   Length of memory segment being unmapped.
197  *
198  * @return
199  *   0 if success.
200  *   -1 on error.
201  */
202
203 int
204 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len);
205 /**
206  * Parse IOMMU group number for a device
207  *
208  * This function is only relevant to linux and will return
209  * an error on BSD.
210  *
211  * @param sysfs_base
212  *   sysfs path prefix.
213  *
214  * @param dev_addr
215  *   device location.
216  *
217  * @param iommu_group_num
218  *   iommu group number
219  *
220  * @return
221  *  >0 on success
222  *   0 for non-existent group or VFIO
223  *  <0 for errors
224  */
225 int
226 rte_vfio_get_group_num(const char *sysfs_base,
227                       const char *dev_addr, int *iommu_group_num);
228
229 /**
230  * Open VFIO container fd or get an existing one
231  *
232  * This function is only relevant to linux and will return
233  * an error on BSD.
234  *
235  * @return
236  *  > 0 container fd
237  *  < 0 for errors
238  */
239 int
240 rte_vfio_get_container_fd(void);
241
242 /**
243  * Open VFIO group fd or get an existing one
244  *
245  * This function is only relevant to linux and will return
246  * an error on BSD.
247  *
248  * @param iommu_group_num
249  *   iommu group number
250  *
251  * @return
252  *  > 0 group fd
253  *  < 0 for errors
254  */
255 int
256 rte_vfio_get_group_fd(int iommu_group_num);
257
258 /**
259  * Create a new container for device binding.
260  *
261  * @note Any newly allocated DPDK memory will not be mapped into these
262  *       containers by default, user needs to manage DMA mappings for
263  *       any container created by this API.
264  *
265  * @return
266  *   the container fd if successful
267  *   <0 if failed
268  */
269 int
270 rte_vfio_container_create(void);
271
272 /**
273  * Destroy the container, unbind all vfio groups within it.
274  *
275  * @param container_fd
276  *   the container fd to destroy
277  *
278  * @return
279  *    0 if successful
280  *   <0 if failed
281  */
282 int
283 rte_vfio_container_destroy(int container_fd);
284
285 /**
286  * Bind a IOMMU group to a container.
287  *
288  * @param container_fd
289  *   the container's fd
290  *
291  * @param iommu_group_num
292  *   the iommu group number to bind to container
293  *
294  * @return
295  *   group fd if successful
296  *   <0 if failed
297  */
298 int
299 rte_vfio_container_group_bind(int container_fd, int iommu_group_num);
300
301 /**
302  * Unbind a IOMMU group from a container.
303  *
304  * @param container_fd
305  *   the container fd of container
306  *
307  * @param iommu_group_num
308  *   the iommu group number to delete from container
309  *
310  * @return
311  *    0 if successful
312  *   <0 if failed
313  */
314 int
315 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num);
316
317 /**
318  * Perform DMA mapping for devices in a container.
319  *
320  * @param container_fd
321  *   the specified container fd
322  *
323  * @param vaddr
324  *   Starting virtual address of memory to be mapped.
325  *
326  * @param iova
327  *   Starting IOVA address of memory to be mapped.
328  *
329  * @param len
330  *   Length of memory segment being mapped.
331  *
332  * @return
333  *    0 if successful
334  *   <0 if failed
335  */
336 int
337 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr,
338                 uint64_t iova, uint64_t len);
339
340 /**
341  * Perform DMA unmapping for devices in a container.
342  *
343  * @param container_fd
344  *   the specified container fd
345  *
346  * @param vaddr
347  *   Starting virtual address of memory to be unmapped.
348  *
349  * @param iova
350  *   Starting IOVA address of memory to be unmapped.
351  *
352  * @param len
353  *   Length of memory segment being unmapped.
354  *
355  * @return
356  *    0 if successful
357  *   <0 if failed
358  */
359 int
360 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr,
361                 uint64_t iova, uint64_t len);
362
363 #ifdef __cplusplus
364 }
365 #endif
366
367 #endif /* _RTE_VFIO_H_ */