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.
37 #include <sys/ioctl.h>
40 #include <rte_memory.h>
41 #include <rte_eal_memconfig.h>
43 #include "eal_filesystem.h"
48 const struct vfio_iommu_type *
49 vfio_set_iommu_type(int vfio_container_fd) {
51 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
52 const struct vfio_iommu_type *t = &iommu_types[idx];
54 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
57 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
61 /* not an error, there may be more supported IOMMU types */
62 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
63 "error %i (%s)\n", t->type_id, t->name, errno,
66 /* if we didn't find a suitable IOMMU type, fail */
71 vfio_has_supported_extensions(int vfio_container_fd) {
73 unsigned idx, n_extensions = 0;
74 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
75 const struct vfio_iommu_type *t = &iommu_types[idx];
77 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
80 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
81 "error %i (%s)\n", errno,
83 close(vfio_container_fd);
85 } else if (ret == 1) {
86 /* we found a supported extension */
89 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
91 ret ? "supported" : "not supported");
94 /* if we didn't find any supported IOMMU types, fail */
96 close(vfio_container_fd);
104 vfio_get_container_fd(void)
106 int ret, vfio_container_fd;
108 /* if we're in a primary process, try to open the container */
109 if (internal_config.process_type == RTE_PROC_PRIMARY) {
110 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
111 if (vfio_container_fd < 0) {
112 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
113 "error %i (%s)\n", errno, strerror(errno));
117 /* check VFIO API version */
118 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
119 if (ret != VFIO_API_VERSION) {
121 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
122 "error %i (%s)\n", errno, strerror(errno));
124 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
125 close(vfio_container_fd);
129 ret = vfio_has_supported_extensions(vfio_container_fd);
131 RTE_LOG(ERR, EAL, " no supported IOMMU "
132 "extensions found!\n");
136 return vfio_container_fd;
139 * if we're in a secondary process, request container fd from the
140 * primary process via our socket
144 socket_fd = vfio_mp_sync_connect_to_primary();
146 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
149 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
150 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
154 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
155 if (vfio_container_fd < 0) {
156 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
161 return vfio_container_fd;
168 vfio_get_group_no(const char *sysfs_base,
169 const char *dev_addr, int *iommu_group_no)
171 char linkname[PATH_MAX];
172 char filename[PATH_MAX];
173 char *tok[16], *group_tok, *end;
176 memset(linkname, 0, sizeof(linkname));
177 memset(filename, 0, sizeof(filename));
179 /* try to find out IOMMU group for this device */
180 snprintf(linkname, sizeof(linkname),
181 "%s/%s/iommu_group", sysfs_base, dev_addr);
183 ret = readlink(linkname, filename, sizeof(filename));
185 /* if the link doesn't exist, no VFIO for us */
189 ret = rte_strsplit(filename, sizeof(filename),
190 tok, RTE_DIM(tok), '/');
193 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
197 /* IOMMU group is always the last token */
199 group_tok = tok[ret - 1];
201 *iommu_group_no = strtol(group_tok, &end, 10);
202 if ((end != group_tok && *end != '\0') || errno != 0) {
203 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
211 vfio_type1_dma_map(int vfio_container_fd)
213 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
216 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
217 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
218 struct vfio_iommu_type1_dma_map dma_map;
220 if (ms[i].addr == NULL)
223 memset(&dma_map, 0, sizeof(dma_map));
224 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
225 dma_map.vaddr = ms[i].addr_64;
226 dma_map.size = ms[i].len;
227 dma_map.iova = ms[i].phys_addr;
228 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
230 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
233 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
234 "error %i (%s)\n", errno, strerror(errno));
243 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
245 /* No-IOMMU mode does not need DMA mapping */