766d0042deb8a14d43550bb98d2d29c52646bac4
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_vfio.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. 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 Intel Corporation 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 EAL_VFIO_H_
35 #define EAL_VFIO_H_
36
37 /*
38  * determine if VFIO is present on the system
39  */
40 #ifdef RTE_EAL_VFIO
41 #include <linux/version.h>
42 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
43 #include <linux/vfio.h>
44
45 #define RTE_VFIO_TYPE1 VFIO_TYPE1_IOMMU
46
47 #ifndef VFIO_SPAPR_TCE_v2_IOMMU
48 #define RTE_VFIO_SPAPR 7
49 #define VFIO_IOMMU_SPAPR_REGISTER_MEMORY _IO(VFIO_TYPE, VFIO_BASE + 17)
50 #define VFIO_IOMMU_SPAPR_TCE_CREATE _IO(VFIO_TYPE, VFIO_BASE + 19)
51 #define VFIO_IOMMU_SPAPR_TCE_REMOVE _IO(VFIO_TYPE, VFIO_BASE + 20)
52
53 struct vfio_iommu_spapr_register_memory {
54         uint32_t argsz;
55         uint32_t flags;
56         uint64_t vaddr;
57         uint64_t size;
58 };
59
60 struct vfio_iommu_spapr_tce_create {
61         uint32_t argsz;
62         uint32_t flags;
63         /* in */
64         uint32_t page_shift;
65         uint32_t __resv1;
66         uint64_t window_size;
67         uint32_t levels;
68         uint32_t __resv2;
69         /* out */
70         uint64_t start_addr;
71 };
72
73 struct vfio_iommu_spapr_tce_remove {
74         uint32_t argsz;
75         uint32_t flags;
76         /* in */
77         uint64_t start_addr;
78 };
79
80 struct vfio_iommu_spapr_tce_ddw_info {
81         uint64_t pgsizes;
82         uint32_t max_dynamic_windows_supported;
83         uint32_t levels;
84 };
85
86 /* SPAPR_v2 is not present, but SPAPR might be */
87 #ifndef VFIO_SPAPR_TCE_IOMMU
88 #define VFIO_IOMMU_SPAPR_TCE_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12)
89
90 struct vfio_iommu_spapr_tce_info {
91         uint32_t argsz;
92         uint32_t flags;
93         uint32_t dma32_window_start;
94         uint32_t dma32_window_size;
95         struct vfio_iommu_spapr_tce_ddw_info ddw;
96 };
97 #endif /* VFIO_SPAPR_TCE_IOMMU */
98
99 #else /* VFIO_SPAPR_TCE_v2_IOMMU */
100 #define RTE_VFIO_SPAPR VFIO_SPAPR_TCE_v2_IOMMU
101 #endif
102
103 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
104 #define RTE_VFIO_NOIOMMU 8
105 #else
106 #define RTE_VFIO_NOIOMMU VFIO_NOIOMMU_IOMMU
107 #endif
108
109 #define VFIO_MAX_GROUPS 64
110
111 /*
112  * Function prototypes for VFIO multiprocess sync functions
113  */
114 int vfio_mp_sync_send_request(int socket, int req);
115 int vfio_mp_sync_receive_request(int socket);
116 int vfio_mp_sync_send_fd(int socket, int fd);
117 int vfio_mp_sync_receive_fd(int socket);
118 int vfio_mp_sync_connect_to_primary(void);
119
120 /*
121  * we don't need to store device fd's anywhere since they can be obtained from
122  * the group fd via an ioctl() call.
123  */
124 struct vfio_group {
125         int group_no;
126         int fd;
127         int devices;
128 };
129
130 struct vfio_config {
131         int vfio_enabled;
132         int vfio_container_fd;
133         int vfio_active_groups;
134         struct vfio_group vfio_groups[VFIO_MAX_GROUPS];
135 };
136
137 /* DMA mapping function prototype.
138  * Takes VFIO container fd as a parameter.
139  * Returns 0 on success, -1 on error.
140  * */
141 typedef int (*vfio_dma_func_t)(int);
142
143 struct vfio_iommu_type {
144         int type_id;
145         const char *name;
146         vfio_dma_func_t dma_map_func;
147 };
148
149 /* pick IOMMU type. returns a pointer to vfio_iommu_type or NULL for error */
150 const struct vfio_iommu_type *
151 vfio_set_iommu_type(int vfio_container_fd);
152
153 /* check if we have any supported extensions */
154 int
155 vfio_has_supported_extensions(int vfio_container_fd);
156
157 /* open container fd or get an existing one */
158 int
159 vfio_get_container_fd(void);
160
161 /* parse IOMMU group number for a device
162  * returns 1 on success, -1 for errors, 0 for non-existent group
163  */
164 int
165 vfio_get_group_no(const char *sysfs_base,
166                 const char *dev_addr, int *iommu_group_no);
167
168 /* open group fd or get an existing one */
169 int
170 vfio_get_group_fd(int iommu_group_no);
171
172 /* remove group fd from internal VFIO group fd array */
173 int
174 clear_group(int vfio_group_fd);
175
176 int vfio_mp_sync_setup(void);
177
178 #define SOCKET_REQ_CONTAINER 0x100
179 #define SOCKET_REQ_GROUP 0x200
180 #define SOCKET_CLR_GROUP 0x300
181 #define SOCKET_OK 0x0
182 #define SOCKET_NO_FD 0x1
183 #define SOCKET_ERR 0xFF
184
185 #define VFIO_PRESENT
186 #endif /* kernel version */
187 #endif /* RTE_EAL_VFIO */
188
189 #endif /* EAL_VFIO_H_ */