physmem: keep only one physmem_main
[vpp.git] / src / vlib / linux / vfio.c
1 /*
2  * Copyright (c) 2016 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  * pci.c: Linux user space PCI bus management.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <linux/vfio.h>
45 #include <sys/ioctl.h>
46
47 #include <vppinfra/linux/sysfs.h>
48
49 #include <vlib/vlib.h>
50 #include <vlib/unix/unix.h>
51 #include <vlib/pci/pci.h>
52 #include <vlib/linux/vfio.h>
53 #include <vlib/physmem.h>
54
55 linux_vfio_main_t vfio_main;
56
57 static int
58 map_regions (vlib_main_t * vm, int fd)
59 {
60   vlib_physmem_main_t *vpm = &physmem_main;
61   vlib_physmem_region_t *pr;
62   struct vfio_iommu_type1_dma_map dm = { 0 };
63   int i;
64
65   dm.argsz = sizeof (struct vfio_iommu_type1_dma_map);
66   dm.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
67
68   /* *INDENT-OFF* */
69   pool_foreach (pr, vpm->regions,
70     {
71       vec_foreach_index (i, pr->page_table)
72         {
73           int rv;
74           dm.vaddr = pointer_to_uword (pr->mem) + (i << pr->log2_page_size);
75           dm.size = 1 << pr->log2_page_size;
76           dm.iova = pr->page_table[i];
77           if ((rv = ioctl (fd, VFIO_IOMMU_MAP_DMA, &dm)))
78             return rv;
79         }
80     });
81   /* *INDENT-ON* */
82   return 0;
83 }
84
85 static clib_error_t *
86 scan_vfio_fd (void *arg, u8 * path_name, u8 * file_name)
87 {
88   linux_vfio_main_t *lvm = &vfio_main;
89   const char fn[] = "/dev/vfio/vfio";
90   char buff[sizeof (fn)] = { 0 };
91   int fd;
92   u8 *path = format (0, "%v%c", path_name, 0);
93
94   if (readlink ((char *) path, buff, sizeof (fn)) + 1 != sizeof (fn))
95     goto done;
96
97   if (strncmp (fn, buff, sizeof (fn)))
98     goto done;
99
100   fd = atoi ((char *) file_name);
101   if (fd != lvm->container_fd)
102     lvm->ext_container_fd = atoi ((char *) file_name);
103
104 done:
105   vec_free (path);
106   return 0;
107 }
108
109 void
110 linux_vfio_dma_map_regions (vlib_main_t * vm)
111 {
112   linux_vfio_main_t *lvm = &vfio_main;
113
114   if (lvm->container_fd != -1)
115     map_regions (vm, lvm->container_fd);
116
117   if (lvm->ext_container_fd == -1)
118     foreach_directory_file ("/proc/self/fd", scan_vfio_fd, 0, 0);
119
120   if (lvm->ext_container_fd != -1)
121     map_regions (vm, lvm->ext_container_fd);
122 }
123
124 static linux_pci_vfio_iommu_group_t *
125 get_vfio_iommu_group (int group)
126 {
127   linux_vfio_main_t *lvm = &vfio_main;
128   uword *p;
129
130   p = hash_get (lvm->iommu_pool_index_by_group, group);
131
132   return p ? pool_elt_at_index (lvm->iommu_groups, p[0]) : 0;
133 }
134
135 static clib_error_t *
136 open_vfio_iommu_group (int group)
137 {
138   linux_vfio_main_t *lvm = &vfio_main;
139   linux_pci_vfio_iommu_group_t *g;
140   clib_error_t *err = 0;
141   struct vfio_group_status group_status;
142   u8 *s = 0;
143   int fd;
144
145   g = get_vfio_iommu_group (group);
146   if (g)
147     {
148       g->refcnt++;
149       return 0;
150     }
151   s = format (s, "/dev/vfio/%u%c", group, 0);
152   fd = open ((char *) s, O_RDWR);
153   if (fd < 0)
154     return clib_error_return_unix (0, "open '%s'", s);
155
156   group_status.argsz = sizeof (group_status);
157   if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
158     {
159       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
160                                     s);
161       goto error;
162     }
163
164   if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
165     {
166       err = clib_error_return (0, "iommu group %d is not viable (not all "
167                                "devices in this group bound to vfio-pci)",
168                                group);
169       goto error;
170     }
171
172   if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lvm->container_fd) < 0)
173     {
174       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
175                                     s);
176       goto error;
177     }
178
179   if (lvm->iommu_mode == 0)
180     {
181       if (ioctl (lvm->container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) < 0)
182         {
183           err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
184                                         "'/dev/vfio/vfio'");
185           goto error;
186         }
187       lvm->iommu_mode = VFIO_TYPE1_IOMMU;
188     }
189
190
191   pool_get (lvm->iommu_groups, g);
192   g->fd = fd;
193   g->refcnt = 1;
194   hash_set (lvm->iommu_pool_index_by_group, group, g - lvm->iommu_groups);
195   vec_free (s);
196   return 0;
197 error:
198   close (fd);
199   return err;
200 }
201
202 clib_error_t *
203 linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp)
204 {
205   clib_error_t *err = 0;
206   linux_pci_vfio_iommu_group_t *g;
207   u8 *s = 0;
208   int iommu_group;
209   u8 *tmpstr;
210   int fd;
211
212   s = format (s, "/sys/bus/pci/devices/%U/iommu_group", format_vlib_pci_addr,
213               addr);
214   tmpstr = clib_sysfs_link_to_name ((char *) s);
215   if (tmpstr)
216     {
217       iommu_group = atoi ((char *) tmpstr);
218       vec_free (tmpstr);
219     }
220   else
221     {
222       err = clib_error_return (0, "Cannot find IOMMU group for PCI device ",
223                                "'%U'", format_vlib_pci_addr, addr);
224       goto error;
225     }
226   vec_reset_length (s);
227
228   if ((err = open_vfio_iommu_group (iommu_group)))
229     return err;
230
231   g = get_vfio_iommu_group (iommu_group);
232
233   s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
234   if ((fd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
235     {
236       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
237                                     format_vlib_pci_addr, addr);
238       goto error;
239     }
240   vec_reset_length (s);
241
242   *fdp = fd;
243
244 error:
245   vec_free (s);
246   return err;
247 }
248
249 clib_error_t *
250 linux_vfio_init (vlib_main_t * vm)
251 {
252   linux_vfio_main_t *lvm = &vfio_main;
253   int fd;
254
255   lvm->ext_container_fd = -1;
256
257   fd = open ("/dev/vfio/vfio", O_RDWR);
258
259   /* check if iommu is available */
260   if (fd != -1)
261     {
262       if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
263         {
264           close (fd);
265           fd = -1;
266         }
267       else if (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 1)
268         lvm->flags |= LINUX_VFIO_F_HAVE_IOMMU;
269     }
270
271   lvm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
272   lvm->container_fd = fd;
273   return 0;
274 }
275
276 /*
277  * fd.io coding-style-patch-verification: ON
278  *
279  * Local Variables:
280  * eval: (c-set-style "gnu")
281  * End:
282  */