New upstream version 18.11-rc1
[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 <stdint.h>
23 #include <linux/vfio.h>
24
25 #define RTE_VFIO_TYPE1 VFIO_TYPE1_IOMMU
26
27 #ifndef VFIO_SPAPR_TCE_v2_IOMMU
28 #define RTE_VFIO_SPAPR 7
29 #define VFIO_IOMMU_SPAPR_REGISTER_MEMORY _IO(VFIO_TYPE, VFIO_BASE + 17)
30 #define VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY _IO(VFIO_TYPE, VFIO_BASE + 18)
31 #define VFIO_IOMMU_SPAPR_TCE_CREATE _IO(VFIO_TYPE, VFIO_BASE + 19)
32 #define VFIO_IOMMU_SPAPR_TCE_REMOVE _IO(VFIO_TYPE, VFIO_BASE + 20)
33
34 struct vfio_iommu_spapr_register_memory {
35         uint32_t argsz;
36         uint32_t flags;
37         uint64_t vaddr;
38         uint64_t size;
39 };
40
41 struct vfio_iommu_spapr_tce_create {
42         uint32_t argsz;
43         uint32_t flags;
44         /* in */
45         uint32_t page_shift;
46         uint32_t __resv1;
47         uint64_t window_size;
48         uint32_t levels;
49         uint32_t __resv2;
50         /* out */
51         uint64_t start_addr;
52 };
53
54 struct vfio_iommu_spapr_tce_remove {
55         uint32_t argsz;
56         uint32_t flags;
57         /* in */
58         uint64_t start_addr;
59 };
60
61 struct vfio_iommu_spapr_tce_ddw_info {
62         uint64_t pgsizes;
63         uint32_t max_dynamic_windows_supported;
64         uint32_t levels;
65 };
66
67 /* SPAPR_v2 is not present, but SPAPR might be */
68 #ifndef VFIO_SPAPR_TCE_IOMMU
69 #define VFIO_IOMMU_SPAPR_TCE_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12)
70
71 struct vfio_iommu_spapr_tce_info {
72         uint32_t argsz;
73         uint32_t flags;
74         uint32_t dma32_window_start;
75         uint32_t dma32_window_size;
76         struct vfio_iommu_spapr_tce_ddw_info ddw;
77 };
78 #endif /* VFIO_SPAPR_TCE_IOMMU */
79
80 #else /* VFIO_SPAPR_TCE_v2_IOMMU */
81 #define RTE_VFIO_SPAPR VFIO_SPAPR_TCE_v2_IOMMU
82 #endif
83
84 #define VFIO_MAX_GROUPS RTE_MAX_VFIO_GROUPS
85 #define VFIO_MAX_CONTAINERS RTE_MAX_VFIO_CONTAINERS
86
87 /*
88  * we don't need to store device fd's anywhere since they can be obtained from
89  * the group fd via an ioctl() call.
90  */
91 struct vfio_group {
92         int group_num;
93         int fd;
94         int devices;
95 };
96
97 /* DMA mapping function prototype.
98  * Takes VFIO container fd as a parameter.
99  * Returns 0 on success, -1 on error.
100  * */
101 typedef int (*vfio_dma_func_t)(int);
102
103 /* Custom memory region DMA mapping function prototype.
104  * Takes VFIO container fd, virtual address, phisical address, length and
105  * operation type (0 to unmap 1 for map) as a parameters.
106  * Returns 0 on success, -1 on error.
107  **/
108 typedef int (*vfio_dma_user_func_t)(int fd, uint64_t vaddr, uint64_t iova,
109                 uint64_t len, int do_map);
110
111 struct vfio_iommu_type {
112         int type_id;
113         const char *name;
114         vfio_dma_user_func_t dma_user_map_func;
115         vfio_dma_func_t dma_map_func;
116 };
117
118 /* get the vfio container that devices are bound to by default */
119 int vfio_get_default_container_fd(void);
120
121 /* pick IOMMU type. returns a pointer to vfio_iommu_type or NULL for error */
122 const struct vfio_iommu_type *
123 vfio_set_iommu_type(int vfio_container_fd);
124
125 /* check if we have any supported extensions */
126 int
127 vfio_has_supported_extensions(int vfio_container_fd);
128
129 int vfio_mp_sync_setup(void);
130
131 #define EAL_VFIO_MP "eal_vfio_mp_sync"
132
133 #define SOCKET_REQ_CONTAINER 0x100
134 #define SOCKET_REQ_GROUP 0x200
135 #define SOCKET_REQ_DEFAULT_CONTAINER 0x400
136 #define SOCKET_OK 0x0
137 #define SOCKET_NO_FD 0x1
138 #define SOCKET_ERR 0xFF
139
140 struct vfio_mp_param {
141         int req;
142         int result;
143         int group_num;
144 };
145
146 #endif /* VFIO_PRESENT */
147
148 #endif /* EAL_VFIO_H_ */