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