vfio: allow mapping MSI-X BARs if kernel allows it
[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 #endif /* RTE_EAL_VFIO */
28
29 #ifdef VFIO_PRESENT
30
31 #include <linux/vfio.h>
32
33 #define VFIO_DIR "/dev/vfio"
34 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
35 #define VFIO_GROUP_FMT "/dev/vfio/%u"
36 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
37 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
38 #define VFIO_GET_REGION_IDX(x) (x >> 40)
39 #define VFIO_NOIOMMU_MODE      \
40         "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
41
42 /* NOIOMMU is defined from kernel version 4.5 onwards */
43 #ifdef VFIO_NOIOMMU_IOMMU
44 #define RTE_VFIO_NOIOMMU VFIO_NOIOMMU_IOMMU
45 #else
46 #define RTE_VFIO_NOIOMMU 8
47 #endif
48
49 /*
50  * capabilities are only supported on kernel 4.6+. there were also some API
51  * changes as well, so add a macro to get cap offset.
52  */
53 #ifdef VFIO_REGION_INFO_FLAG_CAPS
54 #define RTE_VFIO_INFO_FLAG_CAPS VFIO_REGION_INFO_FLAG_CAPS
55 #define VFIO_CAP_OFFSET(x) (x->cap_offset)
56 #else
57 #define RTE_VFIO_INFO_FLAG_CAPS (1 << 3)
58 #define VFIO_CAP_OFFSET(x) (x->resv)
59 struct vfio_info_cap_header {
60         uint16_t id;
61         uint16_t version;
62         uint32_t next;
63 };
64 #endif
65
66 /* kernels 4.16+ can map BAR containing MSI-X table */
67 #ifdef VFIO_REGION_INFO_CAP_MSIX_MAPPABLE
68 #define RTE_VFIO_CAP_MSIX_MAPPABLE VFIO_REGION_INFO_CAP_MSIX_MAPPABLE
69 #else
70 #define RTE_VFIO_CAP_MSIX_MAPPABLE 3
71 #endif
72
73 #else /* not VFIO_PRESENT */
74
75 /* we don't need an actual definition, only pointer is used */
76 struct vfio_device_info;
77
78 #endif /* VFIO_PRESENT */
79
80 /**
81  * Setup vfio_cfg for the device identified by its address.
82  * It discovers the configured I/O MMU groups or sets a new one for the device.
83  * If a new groups is assigned, the DMA mapping is performed.
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 vfio_dev_fd
95  *   VFIO fd.
96  *
97  * @param device_info
98  *   Device information.
99  *
100  * @return
101  *   0 on success.
102  *   <0 on failure.
103  *   >1 if the device cannot be managed this way.
104  */
105 int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
106                 int *vfio_dev_fd, struct vfio_device_info *device_info);
107
108 /**
109  * Release a device mapped to a VFIO-managed I/O MMU group.
110  *
111  * This function is only relevant to linux and will return
112  * an error on BSD.
113  *
114  * @param sysfs_base
115  *   sysfs path prefix.
116  *
117  * @param dev_addr
118  *   device location.
119  *
120  * @param fd
121  *   VFIO fd.
122  *
123  * @return
124  *   0 on success.
125  *   <0 on failure.
126  */
127 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
128
129 /**
130  * Enable a VFIO-related kmod.
131  *
132  * This function is only relevant to linux and will return
133  * an error on BSD.
134  *
135  * @param modname
136  *   kernel module name.
137  *
138  * @return
139  *   0 on success.
140  *   <0 on failure.
141  */
142 int rte_vfio_enable(const char *modname);
143
144 /**
145  * Check whether a VFIO-related kmod is enabled.
146  *
147  * This function is only relevant to linux and will return
148  * an error on BSD.
149  *
150  * @param modname
151  *   kernel module name.
152  *
153  * @return
154  *   !0 if true.
155  *   0 otherwise.
156  */
157 int rte_vfio_is_enabled(const char *modname);
158
159 /**
160  * Whether VFIO NOIOMMU mode is enabled.
161  *
162  * This function is only relevant to linux and will return
163  * an error on BSD.
164  *
165  * @return
166  *   !0 if true.
167  *   0 otherwise.
168  */
169 int rte_vfio_noiommu_is_enabled(void);
170
171 /**
172  * Remove group fd from internal VFIO group fd array/
173  *
174  * This function is only relevant to linux and will return
175  * an error on BSD.
176  *
177  * @param vfio_group_fd
178  *   VFIO Grouup FD.
179  *
180  * @return
181  *   0 on success.
182  *   <0 on failure.
183  */
184 int
185 rte_vfio_clear_group(int vfio_group_fd);
186
187 /**
188  * Map memory region for use with VFIO.
189  *
190  * @note Require at least one device to be attached at the time of
191  *       mapping. DMA maps done via this API will only apply to default
192  *       container and will not apply to any of the containers created
193  *       via rte_vfio_container_create().
194  *
195  * @param vaddr
196  *   Starting virtual address of memory to be mapped.
197  *
198  * @param iova
199  *   Starting IOVA address of memory to be mapped.
200  *
201  * @param len
202  *   Length of memory segment being mapped.
203  *
204  * @return
205  *   0 if success.
206  *   -1 on error.
207  */
208 int
209 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len);
210
211
212 /**
213  * Unmap memory region from VFIO.
214  *
215  * @param vaddr
216  *   Starting virtual address of memory to be unmapped.
217  *
218  * @param iova
219  *   Starting IOVA address of memory to be unmapped.
220  *
221  * @param len
222  *   Length of memory segment being unmapped.
223  *
224  * @return
225  *   0 if success.
226  *   -1 on error.
227  */
228
229 int
230 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len);
231 /**
232  * Parse IOMMU group number for a device
233  *
234  * This function is only relevant to linux and will return
235  * an error on BSD.
236  *
237  * @param sysfs_base
238  *   sysfs path prefix.
239  *
240  * @param dev_addr
241  *   device location.
242  *
243  * @param iommu_group_num
244  *   iommu group number
245  *
246  * @return
247  *  >0 on success
248  *   0 for non-existent group or VFIO
249  *  <0 for errors
250  */
251 int
252 rte_vfio_get_group_num(const char *sysfs_base,
253                       const char *dev_addr, int *iommu_group_num);
254
255 /**
256  * Open VFIO container fd or get an existing one
257  *
258  * This function is only relevant to linux and will return
259  * an error on BSD.
260  *
261  * @return
262  *  > 0 container fd
263  *  < 0 for errors
264  */
265 int
266 rte_vfio_get_container_fd(void);
267
268 /**
269  * Open VFIO group fd or get an existing one
270  *
271  * This function is only relevant to linux and will return
272  * an error on BSD.
273  *
274  * @param iommu_group_num
275  *   iommu group number
276  *
277  * @return
278  *  > 0 group fd
279  *  < 0 for errors
280  */
281 int
282 rte_vfio_get_group_fd(int iommu_group_num);
283
284 /**
285  * Create a new container for device binding.
286  *
287  * @note Any newly allocated DPDK memory will not be mapped into these
288  *       containers by default, user needs to manage DMA mappings for
289  *       any container created by this API.
290  *
291  * @return
292  *   the container fd if successful
293  *   <0 if failed
294  */
295 int
296 rte_vfio_container_create(void);
297
298 /**
299  * Destroy the container, unbind all vfio groups within it.
300  *
301  * @param container_fd
302  *   the container fd to destroy
303  *
304  * @return
305  *    0 if successful
306  *   <0 if failed
307  */
308 int
309 rte_vfio_container_destroy(int container_fd);
310
311 /**
312  * Bind a IOMMU group to a container.
313  *
314  * @param container_fd
315  *   the container's fd
316  *
317  * @param iommu_group_num
318  *   the iommu group number to bind to container
319  *
320  * @return
321  *   group fd if successful
322  *   <0 if failed
323  */
324 int
325 rte_vfio_container_group_bind(int container_fd, int iommu_group_num);
326
327 /**
328  * Unbind a IOMMU group from a container.
329  *
330  * @param container_fd
331  *   the container fd of container
332  *
333  * @param iommu_group_num
334  *   the iommu group number to delete from container
335  *
336  * @return
337  *    0 if successful
338  *   <0 if failed
339  */
340 int
341 rte_vfio_container_group_unbind(int container_fd, int iommu_group_num);
342
343 /**
344  * Perform DMA mapping for devices in a container.
345  *
346  * @param container_fd
347  *   the specified container fd
348  *
349  * @param vaddr
350  *   Starting virtual address of memory to be mapped.
351  *
352  * @param iova
353  *   Starting IOVA address of memory to be mapped.
354  *
355  * @param len
356  *   Length of memory segment being mapped.
357  *
358  * @return
359  *    0 if successful
360  *   <0 if failed
361  */
362 int
363 rte_vfio_container_dma_map(int container_fd, uint64_t vaddr,
364                 uint64_t iova, uint64_t len);
365
366 /**
367  * Perform DMA unmapping for devices in a container.
368  *
369  * @param container_fd
370  *   the specified container fd
371  *
372  * @param vaddr
373  *   Starting virtual address of memory to be unmapped.
374  *
375  * @param iova
376  *   Starting IOVA address of memory to be unmapped.
377  *
378  * @param len
379  *   Length of memory segment being unmapped.
380  *
381  * @return
382  *    0 if successful
383  *   <0 if failed
384  */
385 int
386 rte_vfio_container_dma_unmap(int container_fd, uint64_t vaddr,
387                 uint64_t iova, uint64_t len);
388
389 #ifdef __cplusplus
390 }
391 #endif
392
393 #endif /* _RTE_VFIO_H_ */