vfio: expose functions
[dpdk.git] / lib / librte_eal / common / include / rte_vfio.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 6WIND S.A. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_VFIO_H_
35 #define _RTE_VFIO_H_
36
37 #include <linux/vfio.h>
38
39 #define VFIO_DIR "/dev/vfio"
40 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
41 #define VFIO_GROUP_FMT "/dev/vfio/%u"
42 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
43 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
44 #define VFIO_GET_REGION_IDX(x) (x >> 40)
45 #define VFIO_NOIOMMU_MODE      \
46         "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
47
48 /**
49  * Setup vfio_cfg for the device identified by its address.
50  * It discovers the configured I/O MMU groups or sets a new one for the device.
51  * If a new groups is assigned, the DMA mapping is performed.
52  *
53  * This function is only relevant to linux and will return
54  * an error on BSD.
55  *
56  * @param sysfs_base
57  *   sysfs path prefix.
58  *
59  * @param dev_addr
60  *   device location.
61  *
62  * @param vfio_dev_fd
63  *   VFIO fd.
64  *
65  * @param device_info
66  *   Device information.
67  *
68  * @return
69  *   0 on success.
70  *   <0 on failure.
71  *   >1 if the device cannot be managed this way.
72  */
73 int vfio_setup_device(const char *sysfs_base, const char *dev_addr,
74                 int *vfio_dev_fd, struct vfio_device_info *device_info);
75
76 /**
77  * Release a device mapped to a VFIO-managed I/O MMU group.
78  *
79  * This function is only relevant to linux and will return
80  * an error on BSD.
81  *
82  * @param sysfs_base
83  *   sysfs path prefix.
84  *
85  * @param dev_addr
86  *   device location.
87  *
88  * @param fd
89  *   VFIO fd.
90  *
91  * @return
92  *   0 on success.
93  *   <0 on failure.
94  */
95 int vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
96
97 /**
98  * Enable a VFIO-related kmod.
99  *
100  * This function is only relevant to linux and will return
101  * an error on BSD.
102  *
103  * @param modname
104  *   kernel module name.
105  *
106  * @return
107  *   0 on success.
108  *   <0 on failure.
109  */
110 int vfio_enable(const char *modname);
111
112 /**
113  * Check whether a VFIO-related kmod is enabled.
114  *
115  * This function is only relevant to linux and will return
116  * an error on BSD.
117  *
118  * @param modname
119  *   kernel module name.
120  *
121  * @return
122  *   !0 if true.
123  *   0 otherwise.
124  */
125 int vfio_is_enabled(const char *modname);
126
127 /**
128  * Whether VFIO NOIOMMU mode is enabled.
129  *
130  * This function is only relevant to linux and will return
131  * an error on BSD.
132  *
133  * @return
134  *   !0 if true.
135  *   0 otherwise.
136  */
137 int vfio_noiommu_is_enabled(void);
138
139 #endif /* _RTE_VFIO_H_ */