eal: use SPDX tags in 6WIND copyrighted files
[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 /**
32  * Setup vfio_cfg for the device identified by its address.
33  * It discovers the configured I/O MMU groups or sets a new one for the device.
34  * If a new groups is assigned, the DMA mapping is performed.
35  *
36  * This function is only relevant to linux and will return
37  * an error on BSD.
38  *
39  * @param sysfs_base
40  *   sysfs path prefix.
41  *
42  * @param dev_addr
43  *   device location.
44  *
45  * @param vfio_dev_fd
46  *   VFIO fd.
47  *
48  * @param device_info
49  *   Device information.
50  *
51  * @return
52  *   0 on success.
53  *   <0 on failure.
54  *   >1 if the device cannot be managed this way.
55  */
56 int rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
57                 int *vfio_dev_fd, struct vfio_device_info *device_info);
58
59 /**
60  * Release a device mapped to a VFIO-managed I/O MMU group.
61  *
62  * This function is only relevant to linux and will return
63  * an error on BSD.
64  *
65  * @param sysfs_base
66  *   sysfs path prefix.
67  *
68  * @param dev_addr
69  *   device location.
70  *
71  * @param fd
72  *   VFIO fd.
73  *
74  * @return
75  *   0 on success.
76  *   <0 on failure.
77  */
78 int rte_vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
79
80 /**
81  * Enable a VFIO-related kmod.
82  *
83  * This function is only relevant to linux and will return
84  * an error on BSD.
85  *
86  * @param modname
87  *   kernel module name.
88  *
89  * @return
90  *   0 on success.
91  *   <0 on failure.
92  */
93 int rte_vfio_enable(const char *modname);
94
95 /**
96  * Check whether a VFIO-related kmod is enabled.
97  *
98  * This function is only relevant to linux and will return
99  * an error on BSD.
100  *
101  * @param modname
102  *   kernel module name.
103  *
104  * @return
105  *   !0 if true.
106  *   0 otherwise.
107  */
108 int rte_vfio_is_enabled(const char *modname);
109
110 /**
111  * Whether VFIO NOIOMMU mode is enabled.
112  *
113  * This function is only relevant to linux and will return
114  * an error on BSD.
115  *
116  * @return
117  *   !0 if true.
118  *   0 otherwise.
119  */
120 int rte_vfio_noiommu_is_enabled(void);
121
122 /* remove group fd from internal VFIO group fd array */
123 int
124 rte_vfio_clear_group(int vfio_group_fd);
125
126 #endif /* VFIO_PRESENT */
127
128 #endif /* _RTE_VFIO_H_ */