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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * pci.c: Linux user space PCI bus management.
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
41 #include <sys/types.h>
44 #include <linux/vfio.h>
45 #include <sys/ioctl.h>
47 #include <vppinfra/linux/sysfs.h>
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>
55 linux_vfio_main_t vfio_main;
58 map_regions (vlib_main_t * vm, int fd)
60 vlib_physmem_main_t *vpm = &vm->physmem_main;
61 vlib_physmem_region_t *pr;
62 struct vfio_iommu_type1_dma_map dm = { 0 };
65 dm.argsz = sizeof (struct vfio_iommu_type1_dma_map);
66 dm.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
69 pool_foreach (pr, vpm->regions,
71 vec_foreach_index (i, pr->page_table)
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)))
86 scan_vfio_fd (void *arg, u8 * path_name, u8 * file_name)
88 linux_vfio_main_t *lvm = &vfio_main;
89 const char fn[] = "/dev/vfio/vfio";
90 char buff[sizeof (fn)] = { 0 };
92 u8 *path = format (0, "%v%c", path_name, 0);
94 if (readlink ((char *) path, buff, sizeof (fn)) + 1 != sizeof (fn))
97 if (strncmp (fn, buff, sizeof (fn)))
100 fd = atoi ((char *) file_name);
101 if (fd != lvm->container_fd)
102 lvm->ext_container_fd = atoi ((char *) file_name);
110 linux_vfio_dma_map_regions (vlib_main_t * vm)
112 linux_vfio_main_t *lvm = &vfio_main;
114 if (lvm->container_fd != -1)
115 map_regions (vm, lvm->container_fd);
117 if (lvm->ext_container_fd == -1)
118 foreach_directory_file ("/proc/self/fd", scan_vfio_fd, 0, 0);
120 if (lvm->ext_container_fd != -1)
121 map_regions (vm, lvm->ext_container_fd);
124 static linux_pci_vfio_iommu_group_t *
125 get_vfio_iommu_group (int group)
127 linux_vfio_main_t *lvm = &vfio_main;
130 p = hash_get (lvm->iommu_pool_index_by_group, group);
132 return p ? pool_elt_at_index (lvm->iommu_groups, p[0]) : 0;
135 static clib_error_t *
136 open_vfio_iommu_group (int group)
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;
145 g = get_vfio_iommu_group (group);
151 s = format (s, "/dev/vfio/%u%c", group, 0);
152 fd = open ((char *) s, O_RDWR);
154 return clib_error_return_unix (0, "open '%s'", s);
156 group_status.argsz = sizeof (group_status);
157 if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
159 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
164 if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
166 err = clib_error_return (0, "iommu group %d is not viable (not all "
167 "devices in this group bound to vfio-pci)",
172 if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lvm->container_fd) < 0)
174 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
179 if (lvm->iommu_mode == 0)
181 if (ioctl (lvm->container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) < 0)
183 err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
187 lvm->iommu_mode = VFIO_TYPE1_IOMMU;
191 pool_get (lvm->iommu_groups, g);
194 hash_set (lvm->iommu_pool_index_by_group, group, g - lvm->iommu_groups);
203 linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp)
205 clib_error_t *err = 0;
206 linux_pci_vfio_iommu_group_t *g;
212 s = format (s, "/sys/bus/pci/devices/%U/iommu_group", format_vlib_pci_addr,
214 tmpstr = clib_sysfs_link_to_name ((char *) s);
217 iommu_group = atoi ((char *) tmpstr);
222 err = clib_error_return (0, "Cannot find IOMMU group for PCI device ",
223 "'%U'", format_vlib_pci_addr, addr);
226 vec_reset_length (s);
228 if ((err = open_vfio_iommu_group (iommu_group)))
231 g = get_vfio_iommu_group (iommu_group);
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)
236 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
237 format_vlib_pci_addr, addr);
240 vec_reset_length (s);
250 linux_vfio_init (vlib_main_t * vm)
252 linux_vfio_main_t *lvm = &vfio_main;
255 lvm->ext_container_fd = -1;
257 fd = open ("/dev/vfio/vfio", O_RDWR);
259 /* check if iommu is available */
262 if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
267 else if (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 1)
268 lvm->flags |= LINUX_VFIO_F_HAVE_IOMMU;
271 lvm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
272 lvm->container_fd = fd;
277 * fd.io coding-style-patch-verification: ON
280 * eval: (c-set-style "gnu")