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 = &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;
77 if ((rv = ioctl (fd, VFIO_IOMMU_MAP_DMA, &dm)))
86 linux_vfio_dma_map_regions (vlib_main_t * vm)
88 linux_vfio_main_t *lvm = &vfio_main;
90 if (lvm->container_fd != -1)
91 map_regions (vm, lvm->container_fd);
94 static linux_pci_vfio_iommu_group_t *
95 get_vfio_iommu_group (int group)
97 linux_vfio_main_t *lvm = &vfio_main;
100 p = hash_get (lvm->iommu_pool_index_by_group, group);
102 return p ? pool_elt_at_index (lvm->iommu_groups, p[0]) : 0;
105 static clib_error_t *
106 open_vfio_iommu_group (int group)
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;
115 g = get_vfio_iommu_group (group);
121 s = format (s, "/dev/vfio/%u%c", group, 0);
122 fd = open ((char *) s, O_RDWR);
124 return clib_error_return_unix (0, "open '%s'", s);
126 group_status.argsz = sizeof (group_status);
127 if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
129 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
134 if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
136 err = clib_error_return (0, "iommu group %d is not viable (not all "
137 "devices in this group bound to vfio-pci)",
142 if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lvm->container_fd) < 0)
144 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
149 if (lvm->iommu_mode == 0)
151 if (ioctl (lvm->container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) < 0)
153 err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
157 lvm->iommu_mode = VFIO_TYPE1_IOMMU;
161 pool_get (lvm->iommu_groups, g);
164 hash_set (lvm->iommu_pool_index_by_group, group, g - lvm->iommu_groups);
173 linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp)
175 clib_error_t *err = 0;
176 linux_pci_vfio_iommu_group_t *g;
182 s = format (s, "/sys/bus/pci/devices/%U/iommu_group", format_vlib_pci_addr,
184 tmpstr = clib_sysfs_link_to_name ((char *) s);
187 iommu_group = atoi ((char *) tmpstr);
192 err = clib_error_return (0, "Cannot find IOMMU group for PCI device ",
193 "'%U'", format_vlib_pci_addr, addr);
196 vec_reset_length (s);
198 if ((err = open_vfio_iommu_group (iommu_group)))
201 g = get_vfio_iommu_group (iommu_group);
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)
206 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
207 format_vlib_pci_addr, addr);
210 vec_reset_length (s);
220 linux_vfio_init (vlib_main_t * vm)
222 linux_vfio_main_t *lvm = &vfio_main;
225 fd = open ("/dev/vfio/vfio", O_RDWR);
227 /* check if iommu is available */
230 if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
235 else if (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 1)
236 lvm->flags |= LINUX_VFIO_F_HAVE_IOMMU;
239 lvm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
240 lvm->container_fd = fd;
245 * fd.io coding-style-patch-verification: ON
248 * eval: (c-set-style "gnu")