vfio: generalize non PCI-specific functions
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_vfio.c
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 #include <string.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <sys/ioctl.h>
38
39 #include <rte_log.h>
40 #include <rte_memory.h>
41 #include <rte_eal_memconfig.h>
42
43 #include "eal_filesystem.h"
44 #include "eal_vfio.h"
45
46 #ifdef VFIO_PRESENT
47
48 const struct vfio_iommu_type *
49 vfio_set_iommu_type(int vfio_container_fd) {
50         unsigned idx;
51         for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
52                 const struct vfio_iommu_type *t = &iommu_types[idx];
53
54                 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
55                                 t->type_id);
56                 if (!ret) {
57                         RTE_LOG(NOTICE, EAL, "  using IOMMU type %d (%s)\n",
58                                         t->type_id, t->name);
59                         return t;
60                 }
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,
64                                 strerror(errno));
65         }
66         /* if we didn't find a suitable IOMMU type, fail */
67         return NULL;
68 }
69
70 int
71 vfio_has_supported_extensions(int vfio_container_fd) {
72         int ret;
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];
76
77                 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
78                                 t->type_id);
79                 if (ret < 0) {
80                         RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
81                                 "error %i (%s)\n", errno,
82                                 strerror(errno));
83                         close(vfio_container_fd);
84                         return -1;
85                 } else if (ret == 1) {
86                         /* we found a supported extension */
87                         n_extensions++;
88                 }
89                 RTE_LOG(DEBUG, EAL, "  IOMMU type %d (%s) is %s\n",
90                                 t->type_id, t->name,
91                                 ret ? "supported" : "not supported");
92         }
93
94         /* if we didn't find any supported IOMMU types, fail */
95         if (!n_extensions) {
96                 close(vfio_container_fd);
97                 return -1;
98         }
99
100         return 0;
101 }
102
103 int
104 vfio_get_container_fd(void)
105 {
106         int ret, vfio_container_fd;
107
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));
114                         return -1;
115                 }
116
117                 /* check VFIO API version */
118                 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
119                 if (ret != VFIO_API_VERSION) {
120                         if (ret < 0)
121                                 RTE_LOG(ERR, EAL, "  could not get VFIO API version, "
122                                                 "error %i (%s)\n", errno, strerror(errno));
123                         else
124                                 RTE_LOG(ERR, EAL, "  unsupported VFIO API version!\n");
125                         close(vfio_container_fd);
126                         return -1;
127                 }
128
129                 ret = vfio_has_supported_extensions(vfio_container_fd);
130                 if (ret) {
131                         RTE_LOG(ERR, EAL, "  no supported IOMMU "
132                                         "extensions found!\n");
133                         return -1;
134                 }
135
136                 return vfio_container_fd;
137         } else {
138                 /*
139                  * if we're in a secondary process, request container fd from the
140                  * primary process via our socket
141                  */
142                 int socket_fd;
143
144                 socket_fd = vfio_mp_sync_connect_to_primary();
145                 if (socket_fd < 0) {
146                         RTE_LOG(ERR, EAL, "  cannot connect to primary process!\n");
147                         return -1;
148                 }
149                 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
150                         RTE_LOG(ERR, EAL, "  cannot request container fd!\n");
151                         close(socket_fd);
152                         return -1;
153                 }
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");
157                         close(socket_fd);
158                         return -1;
159                 }
160                 close(socket_fd);
161                 return vfio_container_fd;
162         }
163
164         return -1;
165 }
166
167 int
168 vfio_get_group_no(const char *sysfs_base,
169                 const char *dev_addr, int *iommu_group_no)
170 {
171         char linkname[PATH_MAX];
172         char filename[PATH_MAX];
173         char *tok[16], *group_tok, *end;
174         int ret;
175
176         memset(linkname, 0, sizeof(linkname));
177         memset(filename, 0, sizeof(filename));
178
179         /* try to find out IOMMU group for this device */
180         snprintf(linkname, sizeof(linkname),
181                          "%s/%s/iommu_group", sysfs_base, dev_addr);
182
183         ret = readlink(linkname, filename, sizeof(filename));
184
185         /* if the link doesn't exist, no VFIO for us */
186         if (ret < 0)
187                 return 0;
188
189         ret = rte_strsplit(filename, sizeof(filename),
190                         tok, RTE_DIM(tok), '/');
191
192         if (ret <= 0) {
193                 RTE_LOG(ERR, EAL, "  %s cannot get IOMMU group\n", dev_addr);
194                 return -1;
195         }
196
197         /* IOMMU group is always the last token */
198         errno = 0;
199         group_tok = tok[ret - 1];
200         end = group_tok;
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);
204                 return -1;
205         }
206
207         return 1;
208 }
209
210 int
211 vfio_type1_dma_map(int vfio_container_fd)
212 {
213         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
214         int i, ret;
215
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;
219
220                 if (ms[i].addr == NULL)
221                         break;
222
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;
229
230                 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
231
232                 if (ret) {
233                         RTE_LOG(ERR, EAL, "  cannot set up DMA remapping, "
234                                         "error %i (%s)\n", errno, strerror(errno));
235                         return -1;
236                 }
237         }
238
239         return 0;
240 }
241
242 int
243 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
244 {
245         /* No-IOMMU mode does not need DMA mapping */
246         return 0;
247 }
248
249 #endif