New upstream version 16.11.5
[deb_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 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
46 #define RTE_PCI_MSIX_TABLE_BIR    0x7
47 #define RTE_PCI_MSIX_TABLE_OFFSET 0xfffffff8
48 #define RTE_PCI_MSIX_FLAGS_QSIZE  0x07ff
49 #else
50 #define RTE_PCI_MSIX_TABLE_BIR    PCI_MSIX_TABLE_BIR
51 #define RTE_PCI_MSIX_TABLE_OFFSET PCI_MSIX_TABLE_OFFSET
52 #define RTE_PCI_MSIX_FLAGS_QSIZE  PCI_MSIX_FLAGS_QSIZE
53 #endif
54
55 #define RTE_VFIO_TYPE1 VFIO_TYPE1_IOMMU
56
57 #ifndef VFIO_SPAPR_TCE_v2_IOMMU
58 #define RTE_VFIO_SPAPR 7
59 #define VFIO_IOMMU_SPAPR_REGISTER_MEMORY _IO(VFIO_TYPE, VFIO_BASE + 17)
60 #define VFIO_IOMMU_SPAPR_TCE_CREATE _IO(VFIO_TYPE, VFIO_BASE + 19)
61 #define VFIO_IOMMU_SPAPR_TCE_REMOVE _IO(VFIO_TYPE, VFIO_BASE + 20)
62 struct vfio_iommu_spapr_register_memory {
63         uint32_t argsz;
64         uint32_t flags;
65         uint64_t vaddr;
66         uint64_t size;
67 };
68 struct vfio_iommu_spapr_tce_create {
69         uint32_t argsz;
70         uint32_t page_shift;
71         uint64_t window_size;
72         uint32_t levels;
73 };
74 struct vfio_iommu_spapr_tce_remove {
75         uint32_t argsz;
76         uint64_t start_addr;
77 };
78 #else
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 64
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 };
107
108 struct vfio_config {
109         int vfio_enabled;
110         int vfio_container_fd;
111         int vfio_container_has_dma;
112         int vfio_group_idx;
113         struct vfio_group vfio_groups[VFIO_MAX_GROUPS];
114 };
115
116 #define VFIO_DIR "/dev/vfio"
117 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
118 #define VFIO_GROUP_FMT "/dev/vfio/%u"
119 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
120 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
121 #define VFIO_GET_REGION_IDX(x) (x >> 40)
122
123 /* DMA mapping function prototype.
124  * Takes VFIO container fd as a parameter.
125  * Returns 0 on success, -1 on error.
126  * */
127 typedef int (*vfio_dma_func_t)(int);
128
129 struct vfio_iommu_type {
130         int type_id;
131         const char *name;
132         vfio_dma_func_t dma_map_func;
133 };
134
135 /* pick IOMMU type. returns a pointer to vfio_iommu_type or NULL for error */
136 const struct vfio_iommu_type *
137 vfio_set_iommu_type(int vfio_container_fd);
138
139 /* check if we have any supported extensions */
140 int
141 vfio_has_supported_extensions(int vfio_container_fd);
142
143 /* open container fd or get an existing one */
144 int
145 vfio_get_container_fd(void);
146
147 /* parse IOMMU group number for a device
148  * returns 1 on success, -1 for errors, 0 for non-existent group
149  */
150 int
151 vfio_get_group_no(const char *sysfs_base,
152                 const char *dev_addr, int *iommu_group_no);
153
154 /* open group fd or get an existing one */
155 int
156 vfio_get_group_fd(int iommu_group_no);
157
158 /**
159  * Setup vfio_cfg for the device identified by its address. It discovers
160  * the configured I/O MMU groups or sets a new one for the device. If a new
161  * groups is assigned, the DMA mapping is performed.
162  * Returns 0 on success, a negative value on failure and a positive value in
163  * case the given device cannot be managed this way.
164  */
165 int vfio_setup_device(const char *sysfs_base, const char *dev_addr,
166                 int *vfio_dev_fd, struct vfio_device_info *device_info);
167
168 int vfio_enable(const char *modname);
169 int vfio_is_enabled(const char *modname);
170
171 int pci_vfio_enable(void);
172 int pci_vfio_is_enabled(void);
173
174 int vfio_mp_sync_setup(void);
175
176 #define SOCKET_REQ_CONTAINER 0x100
177 #define SOCKET_REQ_GROUP 0x200
178 #define SOCKET_OK 0x0
179 #define SOCKET_NO_FD 0x1
180 #define SOCKET_ERR 0xFF
181
182 #define VFIO_PRESENT
183 #endif /* kernel version */
184 #endif /* RTE_EAL_VFIO */
185
186 #endif /* EAL_VFIO_H_ */