/*
- * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Copyright (c) 2018 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-/*
- * pci.c: Linux user space PCI bus management.
- *
- * Copyright (c) 2008 Eliot Dresselhaus
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
#include <unistd.h>
#include <sys/types.h>
#include <vlib/linux/vfio.h>
#include <vlib/physmem.h>
+#ifndef VFIO_NOIOMMU_IOMMU
+#define VFIO_NOIOMMU_IOMMU 8
+#endif
+
linux_vfio_main_t vfio_main;
-static int
-map_regions (vlib_main_t * vm, int fd)
+clib_error_t *
+vfio_map_physmem_page (vlib_main_t * vm, void *addr)
{
- vlib_physmem_main_t *vpm = &physmem_main;
- vlib_physmem_region_t *pr;
+ vlib_physmem_main_t *vpm = &vm->physmem_main;
+ linux_vfio_main_t *lvm = &vfio_main;
struct vfio_iommu_type1_dma_map dm = { 0 };
- int i;
+ uword log2_page_size = vpm->pmalloc_main->log2_page_sz;
+ uword physmem_start = pointer_to_uword (vpm->pmalloc_main->base);
- dm.argsz = sizeof (struct vfio_iommu_type1_dma_map);
- dm.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
+ if (lvm->container_fd == -1)
+ return clib_error_return (0, "No cointainer fd");
- /* *INDENT-OFF* */
- pool_foreach (pr, vpm->regions,
- {
- vec_foreach_index (i, pr->page_table)
- {
- int rv;
- dm.vaddr = pointer_to_uword (pr->mem) + (i << pr->log2_page_size);
- dm.size = 1 << pr->log2_page_size;
- dm.iova = pr->page_table[i];
- if ((rv = ioctl (fd, VFIO_IOMMU_MAP_DMA, &dm)))
- return rv;
- }
- });
- /* *INDENT-ON* */
- return 0;
-}
+ u32 page_index = vlib_physmem_get_page_index (vm, addr);
-static clib_error_t *
-scan_vfio_fd (void *arg, u8 * path_name, u8 * file_name)
-{
- linux_vfio_main_t *lvm = &vfio_main;
- const char fn[] = "/dev/vfio/vfio";
- char buff[sizeof (fn)] = { 0 };
- int fd;
- u8 *path = format (0, "%v%c", path_name, 0);
-
- if (readlink ((char *) path, buff, sizeof (fn)) + 1 != sizeof (fn))
- goto done;
+ if (clib_bitmap_get (lvm->physmem_pages_mapped, page_index))
+ {
+ vlib_log_debug (lvm->log_default, "map DMA va:%p page:%u already "
+ "mapped", addr, page_index);
+ return 0;
+ }
- if (strncmp (fn, buff, sizeof (fn)))
- goto done;
+ dm.argsz = sizeof (struct vfio_iommu_type1_dma_map);
+ dm.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
+ dm.vaddr = physmem_start + (page_index << log2_page_size);
+ dm.size = 1ULL << log2_page_size;
+ dm.iova = dm.vaddr;
+ vlib_log_debug (lvm->log_default, "map DMA page:%u va:0x%lx iova:%lx "
+ "size:0x%lx", page_index, dm.vaddr, dm.iova, dm.size);
- fd = atoi ((char *) file_name);
- if (fd != lvm->container_fd)
- lvm->ext_container_fd = atoi ((char *) file_name);
+ if (ioctl (lvm->container_fd, VFIO_IOMMU_MAP_DMA, &dm) == -1)
+ {
+ vlib_log_err (lvm->log_default, "map DMA page:%u va:0x%lx iova:%lx "
+ "size:0x%lx failed, error %s (errno %d)", page_index,
+ dm.vaddr, dm.iova, dm.size, strerror (errno), errno);
+ return clib_error_return_unix (0, "physmem DMA map failed");
+ }
-done:
- vec_free (path);
+ lvm->physmem_pages_mapped = clib_bitmap_set (lvm->physmem_pages_mapped,
+ page_index, 1);
return 0;
}
-void
-linux_vfio_dma_map_regions (vlib_main_t * vm)
-{
- linux_vfio_main_t *lvm = &vfio_main;
-
- if (lvm->container_fd != -1)
- map_regions (vm, lvm->container_fd);
-
- if (lvm->ext_container_fd == -1)
- foreach_directory_file ("/proc/self/fd", scan_vfio_fd, 0, 0);
-
- if (lvm->ext_container_fd != -1)
- map_regions (vm, lvm->ext_container_fd);
-}
-
static linux_pci_vfio_iommu_group_t *
get_vfio_iommu_group (int group)
{
}
static clib_error_t *
-open_vfio_iommu_group (int group)
+open_vfio_iommu_group (int group, int is_noiommu)
{
linux_vfio_main_t *lvm = &vfio_main;
linux_pci_vfio_iommu_group_t *g;
u8 *s = 0;
int fd;
+ if (lvm->container_fd == -1)
+ {
+ if ((fd = open ("/dev/vfio/vfio", O_RDWR)) == -1)
+ return clib_error_return_unix (0, "failed to open VFIO container");
+
+ if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
+ {
+ close (fd);
+ return clib_error_return_unix (0, "incompatible VFIO version");
+ }
+
+ lvm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
+ lvm->container_fd = fd;
+ }
+
g = get_vfio_iommu_group (group);
if (g)
{
g->refcnt++;
return 0;
}
- s = format (s, "/dev/vfio/%u%c", group, 0);
+ s = format (s, "/dev/vfio/%s%u%c", is_noiommu ? "noiommu-" : "", group, 0);
fd = open ((char *) s, O_RDWR);
if (fd < 0)
return clib_error_return_unix (0, "open '%s'", s);
if (lvm->iommu_mode == 0)
{
- if (ioctl (lvm->container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) < 0)
+ if (is_noiommu)
+ lvm->iommu_mode = VFIO_NOIOMMU_IOMMU;
+ else
+ lvm->iommu_mode = VFIO_TYPE1_IOMMU;
+
+ if (ioctl (lvm->container_fd, VFIO_SET_IOMMU, lvm->iommu_mode) < 0)
{
err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
"'/dev/vfio/vfio'");
goto error;
}
- lvm->iommu_mode = VFIO_TYPE1_IOMMU;
}
}
clib_error_t *
-linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp)
+linux_vfio_group_get_device_fd (vlib_pci_addr_t * addr, int *fdp,
+ int *is_noiommu)
{
clib_error_t *err = 0;
linux_pci_vfio_iommu_group_t *g;
u8 *tmpstr;
int fd;
+ *is_noiommu = 0;
s = format (s, "/sys/bus/pci/devices/%U/iommu_group", format_vlib_pci_addr,
addr);
tmpstr = clib_sysfs_link_to_name ((char *) s);
}
vec_reset_length (s);
- if ((err = open_vfio_iommu_group (iommu_group)))
+ s = format (s, "/sys/bus/pci/devices/%U/iommu_group/name",
+ format_vlib_pci_addr, addr);
+ err = clib_sysfs_read ((char *) s, "%s", &tmpstr);
+ if (err == 0)
+ {
+ if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
+ *is_noiommu = 1;
+
+ vec_free (tmpstr);
+ }
+ else
+ clib_error_free (err);
+ vec_reset_length (s);
+ if ((err = open_vfio_iommu_group (iommu_group, *is_noiommu)))
return err;
g = get_vfio_iommu_group (iommu_group);
linux_vfio_init (vlib_main_t * vm)
{
linux_vfio_main_t *lvm = &vfio_main;
- int fd;
-
- lvm->ext_container_fd = -1;
- fd = open ("/dev/vfio/vfio", O_RDWR);
-
- /* check if iommu is available */
- if (fd != -1)
- {
- if (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
- {
- close (fd);
- fd = -1;
- }
- else if (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 1)
- lvm->flags |= LINUX_VFIO_F_HAVE_IOMMU;
- }
+ lvm->log_default = vlib_log_register_class ("vfio", 0);
+ lvm->container_fd = -1;
- lvm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
- lvm->container_fd = fd;
return 0;
}