vfio: allow to map other memory regions
[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  * determine if VFIO is present on the system
10  */
11 #if !defined(VFIO_PRESENT) && defined(RTE_EAL_VFIO)
12 #include <linux/version.h>
13 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
14 #define VFIO_PRESENT
15 #endif /* kernel version >= 3.6.0 */
16 #endif /* RTE_EAL_VFIO */
17
18 #ifdef VFIO_PRESENT
19
20 #include <linux/vfio.h>
21
22 #define VFIO_DIR "/dev/vfio"
23 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
24 #define VFIO_GROUP_FMT "/dev/vfio/%u"
25 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
26 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
27 #define VFIO_GET_REGION_IDX(x) (x >> 40)
28 #define VFIO_NOIOMMU_MODE      \
29         "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /**
36  * Setup vfio_cfg for the device identified by its address.
37  * It discovers the configured I/O MMU groups or sets a new one for the device.
38  * If a new groups is assigned, the DMA mapping is performed.
39  *
40  * This function is only relevant to linux and will return
41  * an error on BSD.
42  *
43  * @param sysfs_base
44  *   sysfs path prefix.
45  *
46  * @param dev_addr
47  *   device location.
48  *
49  * @param vfio_dev_fd
50  *   VFIO fd.
51  *
52  * @param device_info
53  *   Device information.
54  *
55  * @return
56  *   0 on success.
57  *   <0 on failure.
58  *   >1 if the device cannot be managed this way.
59  */
60 int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
61                 int *vfio_dev_fd, struct vfio_device_info *device_info);
62
63 /**
64  * Release a device mapped to a VFIO-managed I/O MMU group.
65  *
66  * This function is only relevant to linux and will return
67  * an error on BSD.
68  *
69  * @param sysfs_base
70  *   sysfs path prefix.
71  *
72  * @param dev_addr
73  *   device location.
74  *
75  * @param fd
76  *   VFIO fd.
77  *
78  * @return
79  *   0 on success.
80  *   <0 on failure.
81  */
82 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
83
84 /**
85  * Enable a VFIO-related kmod.
86  *
87  * This function is only relevant to linux and will return
88  * an error on BSD.
89  *
90  * @param modname
91  *   kernel module name.
92  *
93  * @return
94  *   0 on success.
95  *   <0 on failure.
96  */
97 int rte_vfio_enable(const char *modname);
98
99 /**
100  * Check whether a VFIO-related kmod is enabled.
101  *
102  * This function is only relevant to linux and will return
103  * an error on BSD.
104  *
105  * @param modname
106  *   kernel module name.
107  *
108  * @return
109  *   !0 if true.
110  *   0 otherwise.
111  */
112 int rte_vfio_is_enabled(const char *modname);
113
114 /**
115  * Whether VFIO NOIOMMU mode is enabled.
116  *
117  * This function is only relevant to linux and will return
118  * an error on BSD.
119  *
120  * @return
121  *   !0 if true.
122  *   0 otherwise.
123  */
124 int rte_vfio_noiommu_is_enabled(void);
125
126 /* remove group fd from internal VFIO group fd array */
127 int
128 rte_vfio_clear_group(int vfio_group_fd);
129
130 /**
131  * Map memory region for use with VFIO.
132  *
133  * @note requires at least one device to be attached at the time of mapping.
134  *
135  * @param vaddr
136  *   Starting virtual address of memory to be mapped.
137  *
138  * @param iova
139  *   Starting IOVA address of memory to be mapped.
140  *
141  * @param len
142  *   Length of memory segment being mapped.
143  *
144  * @return
145  *   0 if success.
146  *   -1 on error.
147  */
148 int  __rte_experimental
149 rte_vfio_dma_map(uint64_t vaddr, uint64_t iova, uint64_t len);
150
151
152 /**
153  * Unmap memory region from VFIO.
154  *
155  * @param vaddr
156  *   Starting virtual address of memory to be unmapped.
157  *
158  * @param iova
159  *   Starting IOVA address of memory to be unmapped.
160  *
161  * @param len
162  *   Length of memory segment being unmapped.
163  *
164  * @return
165  *   0 if success.
166  *   -1 on error.
167  */
168 int __rte_experimental
169 rte_vfio_dma_unmap(uint64_t vaddr, uint64_t iova, uint64_t len);
170
171 #ifdef __cplusplus
172 }
173 #endif
174
175 #endif /* VFIO_PRESENT */
176
177 #endif /* _RTE_VFIO_H_ */