vfio: allow DMA map to the default container
[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 #define RTE_VFIO_DEFAULT_CONTAINER_FD (-1)
84
85 /**
86  * Setup vfio_cfg for the device identified by its address.
87  * It discovers the configured I/O MMU groups or sets a new one for the device.
88  * If a new groups is assigned, the DMA mapping is performed.
89  *
90  * This function is only relevant to linux and will return
91  * an error on BSD.
92  *
93  * @param sysfs_base
94  *   sysfs path prefix.
95  *
96  * @param dev_addr
97  *   device location.
98  *
99  * @param vfio_dev_fd
100  *   VFIO fd.
101  *
102  * @param device_info
103  *   Device information.
104  *
105  * @return
106  *   0 on success.
107  *   <0 on failure.
108  *   >1 if the device cannot be managed this way.
109  */
110 int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
111                 int *vfio_dev_fd, struct vfio_device_info *device_info);
112
113 /**
114  * Release a device mapped to a VFIO-managed I/O MMU group.
115  *
116  * This function is only relevant to linux and will return
117  * an error on BSD.
118  *
119  * @param sysfs_base
120  *   sysfs path prefix.
121  *
122  * @param dev_addr
123  *   device location.
124  *
125  * @param fd
126  *   VFIO fd.
127  *
128  * @return
129  *   0 on success.
130  *   <0 on failure.
131  */
132 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
133
134 /**
135  * Enable a VFIO-related kmod.
136  *
137  * This function is only relevant to linux and will return
138  * an error on BSD.
139  *
140  * @param modname
141  *   kernel module name.
142  *
143  * @return
144  *   0 on success.
145  *   <0 on failure.
146  */
147 int rte_vfio_enable(const char *modname);
148
149 /**
150  * Check whether a VFIO-related kmod is enabled.
151  *
152  * This function is only relevant to linux and will return
153  * an error on BSD.
154  *
155  * @param modname
156  *   kernel module name.
157  *
158  * @return
159  *   !0 if true.
160  *   0 otherwise.
161  */
162 int rte_vfio_is_enabled(const char *modname);
163
164 /**
165  * Whether VFIO NOIOMMU mode is enabled.
166  *
167  * This function is only relevant to linux and will return
168  * an error on BSD.
169  *
170  * @return
171  *   !0 if true.
172  *   0 otherwise.
173  */
174 int rte_vfio_noiommu_is_enabled(void);
175
176 /**
177  * Remove group fd from internal VFIO group fd array/
178  *
179  * This function is only relevant to linux and will return
180  * an error on BSD.
181  *
182  * @param vfio_group_fd
183  *   VFIO Grouup FD.
184  *
185  * @return
186  *   0 on success.
187  *   <0 on failure.
188  */
189 int
190 rte_vfio_clear_group(int vfio_group_fd);
191
192 /**
193  * Map memory region for use with VFIO.
194  *
195  * @note Require at least one device to be attached at the time of
196  *       mapping. DMA maps done via this API will only apply to default
197  *       container and will not apply to any of the containers created
198  *       via rte_vfio_container_create().
199  *
200  * @param vaddr
201  *   Starting virtual address of memory to be mapped.
202  *
203  * @param iova
204  *   Starting IOVA address of memory to be mapped.
205  *
206  * @param len
207  *   Length of memory segment being mapped.
208  *
209  * @return
210  *   0 if success.
211  *   -1 on error.
212  */
213 int
214 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len);
215
216
217 /**
218  * Unmap memory region from VFIO.
219  *
220  * @param vaddr
221  *   Starting virtual address of memory to be unmapped.
222  *
223  * @param iova
224  *   Starting IOVA address of memory to be unmapped.
225  *
226  * @param len
227  *   Length of memory segment being unmapped.
228  *
229  * @return
230  *   0 if success.
231  *   -1 on error.
232  */
233
234 int
235 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len);
236 /**
237  * Parse IOMMU group number for a device
238  *
239  * This function is only relevant to linux and will return
240  * an error on BSD.
241  *
242  * @param sysfs_base
243  *   sysfs path prefix.
244  *
245  * @param dev_addr
246  *   device location.
247  *
248  * @param iommu_group_num
249  *   iommu group number
250  *
251  * @return
252  *  >0 on success
253  *   0 for non-existent group or VFIO
254  *  <0 for errors
255  */
256 int
257 rte_vfio_get_group_num(const char *sysfs_base,
258                       const char *dev_addr, int *iommu_group_num);
259
260 /**
261  * Open a new VFIO container fd
262  *
263  * This function is only relevant to linux and will return
264  * an error on BSD.
265  *
266  * @return
267  *  > 0 container fd
268  *  < 0 for errors
269  */
270 int
271 rte_vfio_get_container_fd(void);
272
273 /**
274  * Open VFIO group fd or get an existing one
275  *
276  * This function is only relevant to linux and will return
277  * an error on BSD.
278  *
279  * @param iommu_group_num
280  *   iommu group number
281  *
282  * @return
283  *  > 0 group fd
284  *  < 0 for errors
285  */
286 int
287 rte_vfio_get_group_fd(int iommu_group_num);
288
289 /**
290  * Create a new container for device binding.
291  *
292  * @note Any newly allocated DPDK memory will not be mapped into these
293  *       containers by default, user needs to manage DMA mappings for
294  *       any container created by this API.
295  *
296  * @note When creating containers using this API, the container will only be
297  *       available in the process that has created it. Sharing containers and
298  *       devices between multiple processes is not supported.
299  *
300  * @return
301  *   the container fd if successful
302  *   <0 if failed
303  */
304 int
305 rte_vfio_container_create(void);
306
307 /**
308  * Destroy the container, unbind all vfio groups within it.
309  *
310  * @param container_fd
311  *   the container fd to destroy
312  *
313  * @return
314  *    0 if successful
315  *   <0 if failed
316  */
317 int
318 rte_vfio_container_destroy(int container_fd);
319
320 /**
321  * Bind a IOMMU group to a container.
322  *
323  * @param container_fd
324  *   the container's fd
325  *
326  * @param iommu_group_num
327  *   the iommu group number to bind to container
328  *
329  * @return
330  *   group fd if successful
331  *   <0 if failed
332  */
333 int
334 rte_vfio_container_group_bind(int container_fd, int iommu_group_num);
335
336 /**
337  * Unbind a IOMMU group from a container.
338  *
339  * @param container_fd
340  *   the container fd of container
341  *
342  * @param iommu_group_num
343  *   the iommu group number to delete from container
344  *
345  * @return
346  *    0 if successful
347  *   <0 if failed
348  */
349 int
350 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num);
351
352 /**
353  * Perform DMA mapping for devices in a container.
354  *
355  * @param container_fd
356  *   the specified container fd. Use RTE_VFIO_DEFAULT_CONTAINER_FD to
357  *   use the default container.
358  *
359  * @param vaddr
360  *   Starting virtual address of memory to be mapped.
361  *
362  * @param iova
363  *   Starting IOVA address of memory to be mapped.
364  *
365  * @param len
366  *   Length of memory segment being mapped.
367  *
368  * @return
369  *    0 if successful
370  *   <0 if failed
371  */
372 int
373 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr,
374                 uint64_t iova, uint64_t len);
375
376 /**
377  * Perform DMA unmapping for devices in a container.
378  *
379  * @param container_fd
380  *   the specified container fd. Use RTE_VFIO_DEFAULT_CONTAINER_FD to
381  *   use the default container.
382  *
383  * @param vaddr
384  *   Starting virtual address of memory to be unmapped.
385  *
386  * @param iova
387  *   Starting IOVA address of memory to be unmapped.
388  *
389  * @param len
390  *   Length of memory segment being unmapped.
391  *
392  * @return
393  *    0 if successful
394  *   <0 if failed
395  */
396 int
397 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr,
398                 uint64_t iova, uint64_t len);
399
400 #ifdef __cplusplus
401 }
402 #endif
403
404 #endif /* _RTE_VFIO_H_ */