dpdk: move DPDK vfio hack to dpdk plugin
[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 = dm.vaddr;
77           if ((rv = ioctl (fd, VFIO_IOMMU_MAP_DMA, &dm)))
78             return rv;
79         }
80     });
81   /* *INDENT-ON* */
82   return 0;
83 }
84
85 void
86 linux_vfio_dma_map_regions (vlib_main_t * vm)
87 {
88   linux_vfio_main_t *lvm = &vfio_main;
89
90   if (lvm->container_fd != -1)
91     map_regions (vm, lvm->container_fd);
92 }
93
94 static linux_pci_vfio_iommu_group_t *
95 get_vfio_iommu_group (int group)
96 {
97   linux_vfio_main_t *lvm = &vfio_main;
98   uword *p;
99
100   p = hash_get (lvm->iommu_pool_index_by_group, group);
101
102   return p ? pool_elt_at_index (lvm->iommu_groups, p[0]) : 0;
103 }
104
105 static clib_error_t *
106 open_vfio_iommu_group (int group)
107 {
108   linux_vfio_main_t *lvm = &vfio_main;
109   linux_pci_vfio_iommu_group_t *g;
110   clib_error_t *err = 0;
111   struct vfio_group_status group_status;
112   u8 *s = 0;
113   int fd;
114
115   g = get_vfio_iommu_group (group);
116   if (g)
117     {
118       g->refcnt++;
119       return 0;
120     }
121   s = format (s, "/dev/vfio/%u%c", group, 0);
122   fd = open ((char *) s, O_RDWR);
123   if (fd < 0)
124     return clib_error_return_unix (0, "open '%s'", s);
125
126   group_status.argsz = sizeof (group_status);
127   if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
128     {
129       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
130                                     s);
131       goto error;
132     }
133
134   if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
135     {
136       err = clib_error_return (0, "iommu group %d is not viable (not all "
137                                "devices in this group bound to vfio-pci)",
138                                group);
139       goto error;
140     }
141
142   if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lvm->container_fd) < 0)
143     {
144       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
145                                     s);
146       goto error;
147     }
148
149   if (lvm->iommu_mode == 0)
150     {
151       if (ioctl (lvm->container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) < 0)
152         {
153           err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
154                                         "'/dev/vfio/vfio'");
155           goto error;
156         }
157       lvm->iommu_mode = VFIO_TYPE1_IOMMU;
158     }
159
160
161   pool_get (lvm->iommu_groups, g);
162   g->fd = fd;
163   g->refcnt = 1;
164   hash_set (lvm->iommu_pool_index_by_group, group, g - lvm->iommu_groups);
165   vec_free (s);
166   return 0;
167 error:
168   close (fd);
169   return err;
170 }
171
172 clib_error_t *
173 linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp)
174 {
175   clib_error_t *err = 0;
176   linux_pci_vfio_iommu_group_t *g;
177   u8 *s = 0;
178   int iommu_group;
179   u8 *tmpstr;
180   int fd;
181
182   s = format (s, "/sys/bus/pci/devices/%U/iommu_group", format_vlib_pci_addr,
183               addr);
184   tmpstr = clib_sysfs_link_to_name ((char *) s);
185   if (tmpstr)
186     {
187       iommu_group = atoi ((char *) tmpstr);
188       vec_free (tmpstr);
189     }
190   else
191     {
192       err = clib_error_return (0, "Cannot find IOMMU group for PCI device ",
193                                "'%U'", format_vlib_pci_addr, addr);
194       goto error;
195     }
196   vec_reset_length (s);
197
198   if ((err = open_vfio_iommu_group (iommu_group)))
199     return err;
200
201   g = get_vfio_iommu_group (iommu_group);
202
203   s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
204   if ((fd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
205     {
206       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
207                                     format_vlib_pci_addr, addr);
208       goto error;
209     }
210   vec_reset_length (s);
211
212   *fdp = fd;
213
214 error:
215   vec_free (s);
216   return err;
217 }
218
219 clib_error_t *
220 linux_vfio_init (vlib_main_t * vm)
221 {
222   linux_vfio_main_t *lvm = &vfio_main;
223   int fd;
224
225   fd = open ("/dev/vfio/vfio", O_RDWR);
226
227   /* check if iommu is available */
228   if (fd != -1)
229     {
230       if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
231         {
232           close (fd);
233           fd = -1;
234         }
235       else if (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 1)
236         lvm->flags |= LINUX_VFIO_F_HAVE_IOMMU;
237     }
238
239   lvm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
240   lvm->container_fd = fd;
241   return 0;
242 }
243
244 /*
245  * fd.io coding-style-patch-verification: ON
246  *
247  * Local Variables:
248  * eval: (c-set-style "gnu")
249  * End:
250  */