xref: /dpdk/lib/eal/linux/eal_vfio.h (revision 971d2b57972919527e27ed683032a71864a2eb56)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #ifndef EAL_VFIO_H_
6 #define EAL_VFIO_H_
7 
8 #include <rte_common.h>
9 
10 /*
11  * determine if VFIO is present on the system
12  */
13 #if !defined(VFIO_PRESENT) && defined(RTE_EAL_VFIO)
14 #include <linux/version.h>
15 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
16 #define VFIO_PRESENT
17 #else
18 #pragma message("VFIO configured but not supported by this kernel, disabling.")
19 #endif /* kernel version >= 3.6.0 */
20 #endif /* RTE_EAL_VFIO */
21 
22 #ifdef VFIO_PRESENT
23 
24 #include <stdint.h>
25 #include <linux/vfio.h>
26 
27 #define RTE_VFIO_TYPE1 VFIO_TYPE1_IOMMU
28 
29 #ifndef VFIO_SPAPR_TCE_v2_IOMMU
30 #define RTE_VFIO_SPAPR 7
31 #define VFIO_IOMMU_SPAPR_REGISTER_MEMORY _IO(VFIO_TYPE, VFIO_BASE + 17)
32 #define VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY _IO(VFIO_TYPE, VFIO_BASE + 18)
33 #define VFIO_IOMMU_SPAPR_TCE_CREATE _IO(VFIO_TYPE, VFIO_BASE + 19)
34 #define VFIO_IOMMU_SPAPR_TCE_REMOVE _IO(VFIO_TYPE, VFIO_BASE + 20)
35 
36 struct vfio_iommu_spapr_register_memory {
37 	uint32_t argsz;
38 	uint32_t flags;
39 	uint64_t vaddr;
40 	uint64_t size;
41 };
42 
43 struct vfio_iommu_spapr_tce_create {
44 	uint32_t argsz;
45 	uint32_t flags;
46 	/* in */
47 	uint32_t page_shift;
48 	uint32_t __resv1;
49 	uint64_t window_size;
50 	uint32_t levels;
51 	uint32_t __resv2;
52 	/* out */
53 	uint64_t start_addr;
54 };
55 
56 struct vfio_iommu_spapr_tce_remove {
57 	uint32_t argsz;
58 	uint32_t flags;
59 	/* in */
60 	uint64_t start_addr;
61 };
62 
63 struct vfio_iommu_spapr_tce_ddw_info {
64 	uint64_t pgsizes;
65 	uint32_t max_dynamic_windows_supported;
66 	uint32_t levels;
67 };
68 
69 /* SPAPR_v2 is not present, but SPAPR might be */
70 #ifndef VFIO_SPAPR_TCE_IOMMU
71 #define VFIO_IOMMU_SPAPR_TCE_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12)
72 
73 struct vfio_iommu_spapr_tce_info {
74 	uint32_t argsz;
75 	uint32_t flags;
76 	uint32_t dma32_window_start;
77 	uint32_t dma32_window_size;
78 	struct vfio_iommu_spapr_tce_ddw_info ddw;
79 };
80 #endif /* VFIO_SPAPR_TCE_IOMMU */
81 
82 #else /* VFIO_SPAPR_TCE_v2_IOMMU */
83 #define RTE_VFIO_SPAPR VFIO_SPAPR_TCE_v2_IOMMU
84 #endif
85 
86 #define VFIO_MAX_GROUPS RTE_MAX_VFIO_GROUPS
87 #define VFIO_MAX_CONTAINERS RTE_MAX_VFIO_CONTAINERS
88 
89 /*
90  * we don't need to store device fd's anywhere since they can be obtained from
91  * the group fd via an ioctl() call.
92  */
93 struct vfio_group {
94 	int group_num;
95 	int fd;
96 	int devices;
97 };
98 
99 /* DMA mapping function prototype.
100  * Takes VFIO container fd as a parameter.
101  * Returns 0 on success, -1 on error.
102  */
103 typedef int (*vfio_dma_func_t)(int);
104 
105 /* Custom memory region DMA mapping function prototype.
106  * Takes VFIO container fd, virtual address, physical address, length and
107  * operation type (0 to unmap 1 for map) as a parameters.
108  * Returns 0 on success, -1 on error.
109  */
110 typedef int (*vfio_dma_user_func_t)(int fd, uint64_t vaddr, uint64_t iova,
111 		uint64_t len, int do_map);
112 
113 struct vfio_iommu_type {
114 	int type_id;
115 	const char *name;
116 	bool partial_unmap;
117 	vfio_dma_user_func_t dma_user_map_func;
118 	vfio_dma_func_t dma_map_func;
119 };
120 
121 /* get the vfio container that devices are bound to by default */
122 int vfio_get_default_container_fd(void);
123 
124 /* pick IOMMU type. returns a pointer to vfio_iommu_type or NULL for error */
125 const struct vfio_iommu_type *
126 vfio_set_iommu_type(int vfio_container_fd);
127 
128 int
129 vfio_get_iommu_type(void);
130 
131 /* check if we have any supported extensions */
132 int
133 vfio_has_supported_extensions(int vfio_container_fd);
134 
135 int vfio_mp_sync_setup(void);
136 void vfio_mp_sync_cleanup(void);
137 
138 #define EAL_VFIO_MP "eal_vfio_mp_sync"
139 
140 #define SOCKET_REQ_CONTAINER 0x100
141 #define SOCKET_REQ_GROUP 0x200
142 #define SOCKET_REQ_DEFAULT_CONTAINER 0x400
143 #define SOCKET_REQ_IOMMU_TYPE 0x800
144 #define SOCKET_OK 0x0
145 #define SOCKET_NO_FD 0x1
146 #define SOCKET_ERR 0xFF
147 
148 struct vfio_mp_param {
149 	int req;
150 	int result;
151 	union {
152 		int group_num;
153 		int iommu_type_id;
154 	};
155 };
156 
157 #endif /* VFIO_PRESENT */
158 
159 #endif /* EAL_VFIO_H_ */
160