vfio: export some internal functions
[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 /*
14  * determine if VFIO is present on the system
15  */
16 #if !defined(VFIO_PRESENT) && defined(RTE_EAL_VFIO)
17 #include <linux/version.h>
18 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
19 #define VFIO_PRESENT
20 #endif /* kernel version >= 3.6.0 */
21 #endif /* RTE_EAL_VFIO */
22
23 #ifdef VFIO_PRESENT
24
25 #include <linux/vfio.h>
26
27 #define VFIO_DIR "/dev/vfio"
28 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
29 #define VFIO_GROUP_FMT "/dev/vfio/%u"
30 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
31 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
32 #define VFIO_GET_REGION_IDX(x) (x >> 40)
33 #define VFIO_NOIOMMU_MODE      \
34         "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
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 /**
48  * Setup vfio_cfg for the device identified by its address.
49  * It discovers the configured I/O MMU groups or sets a new one for the device.
50  * If a new groups is assigned, the DMA mapping is performed.
51  *
52  * This function is only relevant to linux and will return
53  * an error on BSD.
54  *
55  * @param sysfs_base
56  *   sysfs path prefix.
57  *
58  * @param dev_addr
59  *   device location.
60  *
61  * @param vfio_dev_fd
62  *   VFIO fd.
63  *
64  * @param device_info
65  *   Device information.
66  *
67  * @return
68  *   0 on success.
69  *   <0 on failure.
70  *   >1 if the device cannot be managed this way.
71  */
72 int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
73                 int *vfio_dev_fd, struct vfio_device_info *device_info);
74
75 /**
76  * Release a device mapped to a VFIO-managed I/O MMU group.
77  *
78  * This function is only relevant to linux and will return
79  * an error on BSD.
80  *
81  * @param sysfs_base
82  *   sysfs path prefix.
83  *
84  * @param dev_addr
85  *   device location.
86  *
87  * @param fd
88  *   VFIO fd.
89  *
90  * @return
91  *   0 on success.
92  *   <0 on failure.
93  */
94 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
95
96 /**
97  * Enable a VFIO-related kmod.
98  *
99  * This function is only relevant to linux and will return
100  * an error on BSD.
101  *
102  * @param modname
103  *   kernel module name.
104  *
105  * @return
106  *   0 on success.
107  *   <0 on failure.
108  */
109 int rte_vfio_enable(const char *modname);
110
111 /**
112  * Check whether a VFIO-related kmod is enabled.
113  *
114  * This function is only relevant to linux and will return
115  * an error on BSD.
116  *
117  * @param modname
118  *   kernel module name.
119  *
120  * @return
121  *   !0 if true.
122  *   0 otherwise.
123  */
124 int rte_vfio_is_enabled(const char *modname);
125
126 /**
127  * Whether VFIO NOIOMMU mode is enabled.
128  *
129  * This function is only relevant to linux and will return
130  * an error on BSD.
131  *
132  * @return
133  *   !0 if true.
134  *   0 otherwise.
135  */
136 int rte_vfio_noiommu_is_enabled(void);
137
138 /**
139  * Remove group fd from internal VFIO group fd array/
140  *
141  * This function is only relevant to linux and will return
142  * an error on BSD.
143  *
144  * @param vfio_group_fd
145  *   VFIO Grouup FD.
146  *
147  * @return
148  *   0 on success.
149  *   <0 on failure.
150  */
151 int
152 rte_vfio_clear_group(int vfio_group_fd);
153
154 /**
155  * Map memory region for use with VFIO.
156  *
157  * @note requires at least one device to be attached at the time of mapping.
158  *
159  * @param vaddr
160  *   Starting virtual address of memory to be mapped.
161  *
162  * @param iova
163  *   Starting IOVA address of memory to be mapped.
164  *
165  * @param len
166  *   Length of memory segment being mapped.
167  *
168  * @return
169  *   0 if success.
170  *   -1 on error.
171  */
172 int  __rte_experimental
173 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len);
174
175
176 /**
177  * Unmap memory region from VFIO.
178  *
179  * @param vaddr
180  *   Starting virtual address of memory to be unmapped.
181  *
182  * @param iova
183  *   Starting IOVA address of memory to be unmapped.
184  *
185  * @param len
186  *   Length of memory segment being unmapped.
187  *
188  * @return
189  *   0 if success.
190  *   -1 on error.
191  */
192
193 int __rte_experimental
194 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len);
195 /**
196  * Parse IOMMU group number for a device
197  *
198  * This function is only relevant to linux and will return
199  * an error on BSD.
200  *
201  * @param sysfs_base
202  *   sysfs path prefix.
203  *
204  * @param dev_addr
205  *   device location.
206  *
207  * @param iommu_group_num
208  *   iommu group number
209  *
210  * @return
211  *  >0 on success
212  *   0 for non-existent group or VFIO
213  *  <0 for errors
214  */
215 int __rte_experimental
216 rte_vfio_get_group_num(const char *sysfs_base,
217                       const char *dev_addr, int *iommu_group_num);
218
219 /**
220  * Open VFIO container fd or get an existing one
221  *
222  * This function is only relevant to linux and will return
223  * an error on BSD.
224  *
225  * @return
226  *  > 0 container fd
227  *  < 0 for errors
228  */
229 int __rte_experimental
230 rte_vfio_get_container_fd(void);
231
232 /**
233  * Open VFIO group fd or get an existing one
234  *
235  * This function is only relevant to linux and will return
236  * an error on BSD.
237  *
238  * @param iommu_group_num
239  *   iommu group number
240  *
241  * @return
242  *  > 0 group fd
243  *  < 0 for errors
244  */
245 int __rte_experimental
246 rte_vfio_get_group_fd(int iommu_group_num);
247
248 #ifdef __cplusplus
249 }
250 #endif
251
252 #endif /* VFIO_PRESENT */
253
254 #endif /* _RTE_VFIO_H_ */