blob: e72f10388ca4b1f32b2606f5f999d42b6b5b0c2f [file] [log] [blame]
Damjan Marion1ba0fa42018-03-04 17:19:08 +01001/*
Damjan Marion20ba1642018-03-26 15:35:33 +02002 * Copyright (c) 2018 Cisco and/or its affiliates.
Damjan Marion1ba0fa42018-03-04 17:19:08 +01003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
Damjan Marion1ba0fa42018-03-04 17:19:08 +010015
16#include <unistd.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20#include <linux/vfio.h>
21#include <sys/ioctl.h>
22
23#include <vppinfra/linux/sysfs.h>
24
25#include <vlib/vlib.h>
26#include <vlib/unix/unix.h>
27#include <vlib/pci/pci.h>
28#include <vlib/linux/vfio.h>
29#include <vlib/physmem.h>
30
Damjan Marion20ba1642018-03-26 15:35:33 +020031#ifndef VFIO_NOIOMMU_IOMMU
32#define VFIO_NOIOMMU_IOMMU 8
33#endif
34
Damjan Marion1ba0fa42018-03-04 17:19:08 +010035linux_vfio_main_t vfio_main;
36
37static int
Damjan Marionb7620fd2018-10-05 16:46:00 +020038vfio_map_regions (vlib_main_t * vm, int fd)
Damjan Marion1ba0fa42018-03-04 17:19:08 +010039{
Damjan Marion88c06212018-03-05 20:08:28 +010040 vlib_physmem_main_t *vpm = &physmem_main;
Damjan Marion7d426532018-09-27 14:54:49 +020041 linux_vfio_main_t *lvm = &vfio_main;
Damjan Marion1ba0fa42018-03-04 17:19:08 +010042 vlib_physmem_region_t *pr;
43 struct vfio_iommu_type1_dma_map dm = { 0 };
44 int i;
45
46 dm.argsz = sizeof (struct vfio_iommu_type1_dma_map);
47 dm.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
48
49 /* *INDENT-OFF* */
50 pool_foreach (pr, vpm->regions,
51 {
52 vec_foreach_index (i, pr->page_table)
53 {
54 int rv;
Haiyang Tana5ab5032018-10-15 06:17:55 -070055 dm.vaddr = pointer_to_uword (pr->mem) + ((u64)i << pr->log2_page_size);
56 dm.size = 1ull << pr->log2_page_size;
Damjan Marion51c52c02018-03-09 16:05:56 +010057 dm.iova = dm.vaddr;
Damjan Marion7d426532018-09-27 14:54:49 +020058 vlib_log_debug (lvm->log_default, "map DMA va:0x%lx iova:%lx "
59 "size:0x%lx", dm.vaddr, dm.iova, dm.size);
60
Damjan Marionb7620fd2018-10-05 16:46:00 +020061 if ((rv = ioctl (fd, VFIO_IOMMU_MAP_DMA, &dm)) &&
62 errno != EINVAL)
Damjan Marion7d426532018-09-27 14:54:49 +020063 {
64 vlib_log_err (lvm->log_default, "map DMA va:0x%lx iova:%lx "
65 "size:0x%lx failed, error %s (errno %d)",
66 dm.vaddr, dm.iova, dm.size, strerror (errno),
67 errno);
68 return rv;
69 }
Damjan Marion1ba0fa42018-03-04 17:19:08 +010070 }
71 });
72 /* *INDENT-ON* */
73 return 0;
74}
75
Damjan Marion1ba0fa42018-03-04 17:19:08 +010076void
77linux_vfio_dma_map_regions (vlib_main_t * vm)
78{
79 linux_vfio_main_t *lvm = &vfio_main;
80
81 if (lvm->container_fd != -1)
Damjan Marionb7620fd2018-10-05 16:46:00 +020082 vfio_map_regions (vm, lvm->container_fd);
Damjan Marion1ba0fa42018-03-04 17:19:08 +010083}
84
85static linux_pci_vfio_iommu_group_t *
86get_vfio_iommu_group (int group)
87{
88 linux_vfio_main_t *lvm = &vfio_main;
89 uword *p;
90
91 p = hash_get (lvm->iommu_pool_index_by_group, group);
92
93 return p ? pool_elt_at_index (lvm->iommu_groups, p[0]) : 0;
94}
95
96static clib_error_t *
Damjan Marion20ba1642018-03-26 15:35:33 +020097open_vfio_iommu_group (int group, int is_noiommu)
Damjan Marion1ba0fa42018-03-04 17:19:08 +010098{
99 linux_vfio_main_t *lvm = &vfio_main;
100 linux_pci_vfio_iommu_group_t *g;
101 clib_error_t *err = 0;
102 struct vfio_group_status group_status;
103 u8 *s = 0;
104 int fd;
105
Damjan Marion23227982018-10-22 13:38:57 +0200106 if (lvm->container_fd == -1)
107 {
108 if ((fd = open ("/dev/vfio/vfio", O_RDWR)) == -1)
109 return clib_error_return_unix (0, "failed to open VFIO container");
110
111 if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
112 {
113 close (fd);
114 return clib_error_return_unix (0, "incompatible VFIO version");
115 }
116
117 lvm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
118 lvm->container_fd = fd;
119 }
120
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100121 g = get_vfio_iommu_group (group);
122 if (g)
123 {
124 g->refcnt++;
125 return 0;
126 }
Damjan Marion20ba1642018-03-26 15:35:33 +0200127 s = format (s, "/dev/vfio/%s%u%c", is_noiommu ? "noiommu-" : "", group, 0);
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100128 fd = open ((char *) s, O_RDWR);
129 if (fd < 0)
130 return clib_error_return_unix (0, "open '%s'", s);
131
132 group_status.argsz = sizeof (group_status);
133 if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
134 {
135 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
136 s);
137 goto error;
138 }
139
140 if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
141 {
142 err = clib_error_return (0, "iommu group %d is not viable (not all "
143 "devices in this group bound to vfio-pci)",
144 group);
145 goto error;
146 }
147
148 if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lvm->container_fd) < 0)
149 {
150 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
151 s);
152 goto error;
153 }
154
155 if (lvm->iommu_mode == 0)
156 {
Damjan Marion20ba1642018-03-26 15:35:33 +0200157 if (is_noiommu)
158 lvm->iommu_mode = VFIO_NOIOMMU_IOMMU;
159 else
160 lvm->iommu_mode = VFIO_TYPE1_IOMMU;
161
162 if (ioctl (lvm->container_fd, VFIO_SET_IOMMU, lvm->iommu_mode) < 0)
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100163 {
164 err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
165 "'/dev/vfio/vfio'");
166 goto error;
167 }
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100168 }
169
170
171 pool_get (lvm->iommu_groups, g);
172 g->fd = fd;
173 g->refcnt = 1;
174 hash_set (lvm->iommu_pool_index_by_group, group, g - lvm->iommu_groups);
175 vec_free (s);
176 return 0;
177error:
178 close (fd);
179 return err;
180}
181
182clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200183linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp,
184 int *is_noiommu)
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100185{
186 clib_error_t *err = 0;
187 linux_pci_vfio_iommu_group_t *g;
188 u8 *s = 0;
189 int iommu_group;
190 u8 *tmpstr;
191 int fd;
192
Damjan Marion23227982018-10-22 13:38:57 +0200193 *is_noiommu = 0;
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100194 s = format (s, "/sys/bus/pci/devices/%U/iommu_group", format_vlib_pci_addr,
195 addr);
196 tmpstr = clib_sysfs_link_to_name ((char *) s);
197 if (tmpstr)
198 {
199 iommu_group = atoi ((char *) tmpstr);
200 vec_free (tmpstr);
201 }
202 else
203 {
204 err = clib_error_return (0, "Cannot find IOMMU group for PCI device ",
205 "'%U'", format_vlib_pci_addr, addr);
206 goto error;
207 }
208 vec_reset_length (s);
209
Damjan Marion23227982018-10-22 13:38:57 +0200210 s = format (s, "/sys/bus/pci/devices/%U/iommu_group/name",
211 format_vlib_pci_addr, addr);
Damjan Marion20ba1642018-03-26 15:35:33 +0200212 err = clib_sysfs_read ((char *) s, "%s", &tmpstr);
213 if (err == 0)
214 {
215 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
Damjan Marion23227982018-10-22 13:38:57 +0200216 *is_noiommu = 1;
217
Damjan Marion20ba1642018-03-26 15:35:33 +0200218 vec_free (tmpstr);
219 }
220 else
221 clib_error_free (err);
222 vec_reset_length (s);
Damjan Marion23227982018-10-22 13:38:57 +0200223 if ((err = open_vfio_iommu_group (iommu_group, *is_noiommu)))
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100224 return err;
225
226 g = get_vfio_iommu_group (iommu_group);
227
228 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
229 if ((fd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
230 {
231 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
232 format_vlib_pci_addr, addr);
233 goto error;
234 }
235 vec_reset_length (s);
236
237 *fdp = fd;
238
239error:
240 vec_free (s);
241 return err;
242}
243
244clib_error_t *
245linux_vfio_init (vlib_main_t * vm)
246{
247 linux_vfio_main_t *lvm = &vfio_main;
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100248
Damjan Marion7d426532018-09-27 14:54:49 +0200249 lvm->log_default = vlib_log_register_class ("vfio", 0);
Damjan Marion23227982018-10-22 13:38:57 +0200250 lvm->container_fd = -1;
Damjan Marion7d426532018-09-27 14:54:49 +0200251
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100252 return 0;
253}
254
255/*
256 * fd.io coding-style-patch-verification: ON
257 *
258 * Local Variables:
259 * eval: (c-set-style "gnu")
260 * End:
261 */