doc: add VFIO API in doxygen
[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 /**
41  * Setup vfio_cfg for the device identified by its address.
42  * It discovers the configured I/O MMU groups or sets a new one for the device.
43  * If a new groups is assigned, the DMA mapping is performed.
44  *
45  * This function is only relevant to linux and will return
46  * an error on BSD.
47  *
48  * @param sysfs_base
49  *   sysfs path prefix.
50  *
51  * @param dev_addr
52  *   device location.
53  *
54  * @param vfio_dev_fd
55  *   VFIO fd.
56  *
57  * @param device_info
58  *   Device information.
59  *
60  * @return
61  *   0 on success.
62  *   <0 on failure.
63  *   >1 if the device cannot be managed this way.
64  */
65 int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
66                 int *vfio_dev_fd, struct vfio_device_info *device_info);
67
68 /**
69  * Release a device mapped to a VFIO-managed I/O MMU group.
70  *
71  * This function is only relevant to linux and will return
72  * an error on BSD.
73  *
74  * @param sysfs_base
75  *   sysfs path prefix.
76  *
77  * @param dev_addr
78  *   device location.
79  *
80  * @param fd
81  *   VFIO fd.
82  *
83  * @return
84  *   0 on success.
85  *   <0 on failure.
86  */
87 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
88
89 /**
90  * Enable a VFIO-related kmod.
91  *
92  * This function is only relevant to linux and will return
93  * an error on BSD.
94  *
95  * @param modname
96  *   kernel module name.
97  *
98  * @return
99  *   0 on success.
100  *   <0 on failure.
101  */
102 int rte_vfio_enable(const char *modname);
103
104 /**
105  * Check whether a VFIO-related kmod is enabled.
106  *
107  * This function is only relevant to linux and will return
108  * an error on BSD.
109  *
110  * @param modname
111  *   kernel module name.
112  *
113  * @return
114  *   !0 if true.
115  *   0 otherwise.
116  */
117 int rte_vfio_is_enabled(const char *modname);
118
119 /**
120  * Whether VFIO NOIOMMU mode is enabled.
121  *
122  * This function is only relevant to linux and will return
123  * an error on BSD.
124  *
125  * @return
126  *   !0 if true.
127  *   0 otherwise.
128  */
129 int rte_vfio_noiommu_is_enabled(void);
130
131 /* remove group fd from internal VFIO group fd array */
132 int
133 rte_vfio_clear_group(int vfio_group_fd);
134
135 /**
136  * Map memory region for use with VFIO.
137  *
138  * @note requires at least one device to be attached at the time of mapping.
139  *
140  * @param vaddr
141  *   Starting virtual address of memory to be mapped.
142  *
143  * @param iova
144  *   Starting IOVA address of memory to be mapped.
145  *
146  * @param len
147  *   Length of memory segment being mapped.
148  *
149  * @return
150  *   0 if success.
151  *   -1 on error.
152  */
153 int  __rte_experimental
154 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len);
155
156
157 /**
158  * Unmap memory region from VFIO.
159  *
160  * @param vaddr
161  *   Starting virtual address of memory to be unmapped.
162  *
163  * @param iova
164  *   Starting IOVA address of memory to be unmapped.
165  *
166  * @param len
167  *   Length of memory segment being unmapped.
168  *
169  * @return
170  *   0 if success.
171  *   -1 on error.
172  */
173 int __rte_experimental
174 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len);
175
176 #ifdef __cplusplus
177 }
178 #endif
179
180 #endif /* VFIO_PRESENT */
181
182 #endif /* _RTE_VFIO_H_ */