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