4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
38 * determine if VFIO is present on the system
41 #include <linux/version.h>
42 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
43 #include <linux/vfio.h>
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
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
55 #define RTE_VFIO_TYPE1 VFIO_TYPE1_IOMMU
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)
63 struct vfio_iommu_spapr_register_memory {
70 struct vfio_iommu_spapr_tce_create {
83 struct vfio_iommu_spapr_tce_remove {
90 struct vfio_iommu_spapr_tce_ddw_info {
92 uint32_t max_dynamic_windows_supported;
96 /* SPAPR_v2 is not present, but SPAPR might be */
97 #ifndef VFIO_SPAPR_TCE_IOMMU
98 #define VFIO_IOMMU_SPAPR_TCE_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12)
100 struct vfio_iommu_spapr_tce_info {
103 uint32_t dma32_window_start;
104 uint32_t dma32_window_size;
105 struct vfio_iommu_spapr_tce_ddw_info ddw;
107 #endif /* VFIO_SPAPR_TCE_IOMMU */
109 #else /* VFIO_SPAPR_TCE_v2_IOMMU */
110 #define RTE_VFIO_SPAPR VFIO_SPAPR_TCE_v2_IOMMU
113 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
114 #define RTE_VFIO_NOIOMMU 8
116 #define RTE_VFIO_NOIOMMU VFIO_NOIOMMU_IOMMU
119 #define VFIO_MAX_GROUPS 64
122 * Function prototypes for VFIO multiprocess sync functions
124 int vfio_mp_sync_send_request(int socket, int req);
125 int vfio_mp_sync_receive_request(int socket);
126 int vfio_mp_sync_send_fd(int socket, int fd);
127 int vfio_mp_sync_receive_fd(int socket);
128 int vfio_mp_sync_connect_to_primary(void);
131 * we don't need to store device fd's anywhere since they can be obtained from
132 * the group fd via an ioctl() call.
142 int vfio_container_fd;
143 int vfio_active_groups;
144 struct vfio_group vfio_groups[VFIO_MAX_GROUPS];
147 #define VFIO_DIR "/dev/vfio"
148 #define VFIO_CONTAINER_PATH "/dev/vfio/vfio"
149 #define VFIO_GROUP_FMT "/dev/vfio/%u"
150 #define VFIO_NOIOMMU_GROUP_FMT "/dev/vfio/noiommu-%u"
151 #define VFIO_GET_REGION_ADDR(x) ((uint64_t) x << 40ULL)
152 #define VFIO_GET_REGION_IDX(x) (x >> 40)
154 /* DMA mapping function prototype.
155 * Takes VFIO container fd as a parameter.
156 * Returns 0 on success, -1 on error.
158 typedef int (*vfio_dma_func_t)(int);
160 struct vfio_iommu_type {
163 vfio_dma_func_t dma_map_func;
166 /* pick IOMMU type. returns a pointer to vfio_iommu_type or NULL for error */
167 const struct vfio_iommu_type *
168 vfio_set_iommu_type(int vfio_container_fd);
170 /* check if we have any supported extensions */
172 vfio_has_supported_extensions(int vfio_container_fd);
174 /* open container fd or get an existing one */
176 vfio_get_container_fd(void);
178 /* parse IOMMU group number for a device
179 * returns 1 on success, -1 for errors, 0 for non-existent group
182 vfio_get_group_no(const char *sysfs_base,
183 const char *dev_addr, int *iommu_group_no);
185 /* open group fd or get an existing one */
187 vfio_get_group_fd(int iommu_group_no);
189 /* remove group fd from internal VFIO group fd array */
191 clear_group(int vfio_group_fd);
194 * Setup vfio_cfg for the device identified by its address. It discovers
195 * the configured I/O MMU groups or sets a new one for the device. If a new
196 * groups is assigned, the DMA mapping is performed.
197 * Returns 0 on success, a negative value on failure and a positive value in
198 * case the given device cannot be managed this way.
200 int vfio_setup_device(const char *sysfs_base, const char *dev_addr,
201 int *vfio_dev_fd, struct vfio_device_info *device_info);
203 int vfio_release_device(const char *sysfs_base, const char *dev_addr, int fd);
205 int vfio_enable(const char *modname);
206 int vfio_is_enabled(const char *modname);
208 int pci_vfio_enable(void);
209 int pci_vfio_is_enabled(void);
211 int vfio_mp_sync_setup(void);
213 #define SOCKET_REQ_CONTAINER 0x100
214 #define SOCKET_REQ_GROUP 0x200
215 #define SOCKET_CLR_GROUP 0x300
216 #define SOCKET_OK 0x0
217 #define SOCKET_NO_FD 0x1
218 #define SOCKET_ERR 0xFF
221 #endif /* kernel version */
222 #endif /* RTE_EAL_VFIO */
224 #endif /* EAL_VFIO_H_ */