53eed8bfbe97dfc71da60119125f74487bf6fb4d
[vpp.git] / src / vlib / linux / vfio.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * 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  */
15
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
31 #ifndef VFIO_NOIOMMU_IOMMU
32 #define VFIO_NOIOMMU_IOMMU 8
33 #endif
34
35 linux_vfio_main_t vfio_main;
36
37 static int
38 map_regions (vlib_main_t * vm, int fd)
39 {
40   vlib_physmem_main_t *vpm = &physmem_main;
41   linux_vfio_main_t *lvm = &vfio_main;
42   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;
55           dm.vaddr = pointer_to_uword (pr->mem) + (i << pr->log2_page_size);
56           dm.size = 1 << pr->log2_page_size;
57           dm.iova = dm.vaddr;
58           vlib_log_debug (lvm->log_default, "map DMA va:0x%lx iova:%lx "
59                           "size:0x%lx", dm.vaddr, dm.iova, dm.size);
60
61           if ((rv = ioctl (fd, VFIO_IOMMU_MAP_DMA, &dm)))
62             {
63               vlib_log_err (lvm->log_default, "map DMA va:0x%lx iova:%lx "
64                             "size:0x%lx failed, error %s (errno %d)",
65                             dm.vaddr, dm.iova, dm.size, strerror (errno),
66                             errno);
67               return rv;
68             }
69         }
70     });
71   /* *INDENT-ON* */
72   return 0;
73 }
74
75 void
76 linux_vfio_dma_map_regions (vlib_main_t * vm)
77 {
78   linux_vfio_main_t *lvm = &vfio_main;
79
80   if (lvm->container_fd != -1)
81     map_regions (vm, lvm->container_fd);
82 }
83
84 static linux_pci_vfio_iommu_group_t *
85 get_vfio_iommu_group (int group)
86 {
87   linux_vfio_main_t *lvm = &vfio_main;
88   uword *p;
89
90   p = hash_get (lvm->iommu_pool_index_by_group, group);
91
92   return p ? pool_elt_at_index (lvm->iommu_groups, p[0]) : 0;
93 }
94
95 static clib_error_t *
96 open_vfio_iommu_group (int group, int is_noiommu)
97 {
98   linux_vfio_main_t *lvm = &vfio_main;
99   linux_pci_vfio_iommu_group_t *g;
100   clib_error_t *err = 0;
101   struct vfio_group_status group_status;
102   u8 *s = 0;
103   int fd;
104
105   g = get_vfio_iommu_group (group);
106   if (g)
107     {
108       g->refcnt++;
109       return 0;
110     }
111   s = format (s, "/dev/vfio/%s%u%c", is_noiommu ? "noiommu-" : "", group, 0);
112   fd = open ((char *) s, O_RDWR);
113   if (fd < 0)
114     return clib_error_return_unix (0, "open '%s'", s);
115
116   group_status.argsz = sizeof (group_status);
117   if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
118     {
119       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
120                                     s);
121       goto error;
122     }
123
124   if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
125     {
126       err = clib_error_return (0, "iommu group %d is not viable (not all "
127                                "devices in this group bound to vfio-pci)",
128                                group);
129       goto error;
130     }
131
132   if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lvm->container_fd) < 0)
133     {
134       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
135                                     s);
136       goto error;
137     }
138
139   if (lvm->iommu_mode == 0)
140     {
141       if (is_noiommu)
142         lvm->iommu_mode = VFIO_NOIOMMU_IOMMU;
143       else
144         lvm->iommu_mode = VFIO_TYPE1_IOMMU;
145
146       if (ioctl (lvm->container_fd, VFIO_SET_IOMMU, lvm->iommu_mode) < 0)
147         {
148           err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
149                                         "'/dev/vfio/vfio'");
150           goto error;
151         }
152     }
153
154
155   pool_get (lvm->iommu_groups, g);
156   g->fd = fd;
157   g->refcnt = 1;
158   hash_set (lvm->iommu_pool_index_by_group, group, g - lvm->iommu_groups);
159   vec_free (s);
160   return 0;
161 error:
162   close (fd);
163   return err;
164 }
165
166 clib_error_t *
167 linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp)
168 {
169   clib_error_t *err = 0;
170   linux_pci_vfio_iommu_group_t *g;
171   u8 *s = 0;
172   int iommu_group;
173   u8 *tmpstr;
174   int fd;
175   int is_noiommu = 0;
176
177   s = format (s, "/sys/bus/pci/devices/%U/iommu_group", format_vlib_pci_addr,
178               addr);
179   tmpstr = clib_sysfs_link_to_name ((char *) s);
180   if (tmpstr)
181     {
182       iommu_group = atoi ((char *) tmpstr);
183       vec_free (tmpstr);
184     }
185   else
186     {
187       err = clib_error_return (0, "Cannot find IOMMU group for PCI device ",
188                                "'%U'", format_vlib_pci_addr, addr);
189       goto error;
190     }
191   vec_reset_length (s);
192
193   s =
194     format (s, "/sys/bus/pci/devices/%U/iommu_group/name",
195             format_vlib_pci_addr, addr);
196   err = clib_sysfs_read ((char *) s, "%s", &tmpstr);
197   if (err == 0)
198     {
199       if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
200         is_noiommu = 1;
201       vec_free (tmpstr);
202     }
203   else
204     clib_error_free (err);
205   vec_reset_length (s);
206   if ((err = open_vfio_iommu_group (iommu_group, is_noiommu)))
207     return err;
208
209   g = get_vfio_iommu_group (iommu_group);
210
211   s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
212   if ((fd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
213     {
214       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
215                                     format_vlib_pci_addr, addr);
216       goto error;
217     }
218   vec_reset_length (s);
219
220   *fdp = fd;
221
222 error:
223   vec_free (s);
224   return err;
225 }
226
227 clib_error_t *
228 linux_vfio_init (vlib_main_t * vm)
229 {
230   linux_vfio_main_t *lvm = &vfio_main;
231   int fd;
232
233   lvm->log_default = vlib_log_register_class ("vfio", 0);
234
235   fd = open ("/dev/vfio/vfio", O_RDWR);
236
237   /* check if iommu is available */
238   if (fd != -1)
239     {
240       if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
241         {
242           close (fd);
243           fd = -1;
244         }
245       else
246         {
247           if (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 1)
248             {
249               lvm->flags |= LINUX_VFIO_F_HAVE_IOMMU;
250               vlib_log_info (lvm->log_default, "type 1 IOMMU mode supported");
251             }
252           if (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_NOIOMMU_IOMMU) == 1)
253             {
254               lvm->flags |= LINUX_VFIO_F_HAVE_NOIOMMU;
255               vlib_log_info (lvm->log_default, "NOIOMMU mode supported");
256             }
257         }
258     }
259
260   lvm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
261   lvm->container_fd = fd;
262   return 0;
263 }
264
265 /*
266  * fd.io coding-style-patch-verification: ON
267  *
268  * Local Variables:
269  * eval: (c-set-style "gnu")
270  * End:
271  */