mirror of
https://github.com/torvalds/linux.git
synced 2026-04-18 14:53:58 -04:00
vfio: selftests: Add iommufd mode
Add a new IOMMU mode for using iommufd directly. In this mode userspace opens /dev/iommu and binds it to a device FD acquired through /dev/vfio/devices/vfioX. Acked-by: Shuah Khan <skhan@linuxfoundation.org> Signed-off-by: David Matlack <dmatlack@google.com> Link: https://lore.kernel.org/r/20250822212518.4156428-29-dmatlack@google.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
This commit is contained in:
committed by
Alex Williamson
parent
d1a17495bb
commit
61cbfe5014
@@ -10,10 +10,12 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <uapi/linux/types.h>
|
||||
#include <linux/limits.h>
|
||||
#include <linux/mman.h>
|
||||
#include <linux/types.h>
|
||||
#include <linux/vfio.h>
|
||||
#include <linux/iommufd.h>
|
||||
|
||||
#include "../../../kselftest.h"
|
||||
#include <vfio_util.h>
|
||||
@@ -139,32 +141,80 @@ static void vfio_pci_irq_get(struct vfio_pci_device *device, u32 index,
|
||||
ioctl_assert(device->fd, VFIO_DEVICE_GET_IRQ_INFO, irq_info);
|
||||
}
|
||||
|
||||
void vfio_pci_dma_map(struct vfio_pci_device *device,
|
||||
struct vfio_dma_region *region)
|
||||
static void vfio_iommu_dma_map(struct vfio_pci_device *device,
|
||||
struct vfio_dma_region *region)
|
||||
{
|
||||
struct vfio_iommu_type1_dma_map map = {
|
||||
.argsz = sizeof(map),
|
||||
struct vfio_iommu_type1_dma_map args = {
|
||||
.argsz = sizeof(args),
|
||||
.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
|
||||
.vaddr = (u64)region->vaddr,
|
||||
.iova = region->iova,
|
||||
.size = region->size,
|
||||
};
|
||||
|
||||
ioctl_assert(device->container_fd, VFIO_IOMMU_MAP_DMA, &map);
|
||||
ioctl_assert(device->container_fd, VFIO_IOMMU_MAP_DMA, &args);
|
||||
}
|
||||
|
||||
static void iommufd_dma_map(struct vfio_pci_device *device,
|
||||
struct vfio_dma_region *region)
|
||||
{
|
||||
struct iommu_ioas_map args = {
|
||||
.size = sizeof(args),
|
||||
.flags = IOMMU_IOAS_MAP_READABLE |
|
||||
IOMMU_IOAS_MAP_WRITEABLE |
|
||||
IOMMU_IOAS_MAP_FIXED_IOVA,
|
||||
.user_va = (u64)region->vaddr,
|
||||
.iova = region->iova,
|
||||
.length = region->size,
|
||||
.ioas_id = device->ioas_id,
|
||||
};
|
||||
|
||||
ioctl_assert(device->iommufd, IOMMU_IOAS_MAP, &args);
|
||||
}
|
||||
|
||||
void vfio_pci_dma_map(struct vfio_pci_device *device,
|
||||
struct vfio_dma_region *region)
|
||||
{
|
||||
if (device->iommufd)
|
||||
iommufd_dma_map(device, region);
|
||||
else
|
||||
vfio_iommu_dma_map(device, region);
|
||||
|
||||
list_add(®ion->link, &device->dma_regions);
|
||||
}
|
||||
|
||||
static void vfio_iommu_dma_unmap(struct vfio_pci_device *device,
|
||||
struct vfio_dma_region *region)
|
||||
{
|
||||
struct vfio_iommu_type1_dma_unmap args = {
|
||||
.argsz = sizeof(args),
|
||||
.iova = region->iova,
|
||||
.size = region->size,
|
||||
};
|
||||
|
||||
ioctl_assert(device->container_fd, VFIO_IOMMU_UNMAP_DMA, &args);
|
||||
}
|
||||
|
||||
static void iommufd_dma_unmap(struct vfio_pci_device *device,
|
||||
struct vfio_dma_region *region)
|
||||
{
|
||||
struct iommu_ioas_unmap args = {
|
||||
.size = sizeof(args),
|
||||
.iova = region->iova,
|
||||
.length = region->size,
|
||||
.ioas_id = device->ioas_id,
|
||||
};
|
||||
|
||||
ioctl_assert(device->iommufd, IOMMU_IOAS_UNMAP, &args);
|
||||
}
|
||||
|
||||
void vfio_pci_dma_unmap(struct vfio_pci_device *device,
|
||||
struct vfio_dma_region *region)
|
||||
{
|
||||
struct vfio_iommu_type1_dma_unmap unmap = {
|
||||
.argsz = sizeof(unmap),
|
||||
.iova = region->iova,
|
||||
.size = region->size,
|
||||
};
|
||||
|
||||
ioctl_assert(device->container_fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
|
||||
if (device->iommufd)
|
||||
iommufd_dma_unmap(device, region);
|
||||
else
|
||||
vfio_iommu_dma_unmap(device, region);
|
||||
|
||||
list_del(®ion->link);
|
||||
}
|
||||
@@ -258,18 +308,6 @@ static unsigned int vfio_pci_get_group_from_dev(const char *bdf)
|
||||
return group;
|
||||
}
|
||||
|
||||
static void vfio_pci_container_setup(struct vfio_pci_device *device)
|
||||
{
|
||||
const char *path = device->iommu_mode->container_path;
|
||||
int version;
|
||||
|
||||
device->container_fd = open(path, O_RDWR);
|
||||
VFIO_ASSERT_GE(device->container_fd, 0, "open(%s) failed\n", path);
|
||||
|
||||
version = ioctl(device->container_fd, VFIO_GET_API_VERSION);
|
||||
VFIO_ASSERT_EQ(version, VFIO_API_VERSION);
|
||||
}
|
||||
|
||||
static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf)
|
||||
{
|
||||
struct vfio_group_status group_status = {
|
||||
@@ -290,25 +328,33 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf
|
||||
ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->container_fd);
|
||||
}
|
||||
|
||||
static void vfio_pci_iommu_setup(struct vfio_pci_device *device)
|
||||
static void vfio_pci_container_setup(struct vfio_pci_device *device, const char *bdf)
|
||||
{
|
||||
unsigned long iommu_type = device->iommu_mode->iommu_type;
|
||||
const char *path = device->iommu_mode->container_path;
|
||||
int version;
|
||||
int ret;
|
||||
|
||||
INIT_LIST_HEAD(&device->dma_regions);
|
||||
device->container_fd = open(path, O_RDWR);
|
||||
VFIO_ASSERT_GE(device->container_fd, 0, "open(%s) failed\n", path);
|
||||
|
||||
version = ioctl(device->container_fd, VFIO_GET_API_VERSION);
|
||||
VFIO_ASSERT_EQ(version, VFIO_API_VERSION, "Unsupported version: %d\n", version);
|
||||
|
||||
vfio_pci_group_setup(device, bdf);
|
||||
|
||||
ret = ioctl(device->container_fd, VFIO_CHECK_EXTENSION, iommu_type);
|
||||
VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type);
|
||||
|
||||
ioctl_assert(device->container_fd, VFIO_SET_IOMMU, (void *)iommu_type);
|
||||
}
|
||||
|
||||
static void vfio_pci_device_setup(struct vfio_pci_device *device, const char *bdf)
|
||||
{
|
||||
int i;
|
||||
|
||||
device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf);
|
||||
VFIO_ASSERT_GE(device->fd, 0);
|
||||
}
|
||||
|
||||
static void vfio_pci_device_setup(struct vfio_pci_device *device)
|
||||
{
|
||||
int i;
|
||||
|
||||
device->info.argsz = sizeof(device->info);
|
||||
ioctl_assert(device->fd, VFIO_DEVICE_GET_INFO, &device->info);
|
||||
@@ -386,6 +432,9 @@ static const struct vfio_iommu_mode iommu_modes[] = {
|
||||
.container_path = "/dev/iommu",
|
||||
.iommu_type = VFIO_TYPE1v2_IOMMU,
|
||||
},
|
||||
{
|
||||
.name = "iommufd",
|
||||
},
|
||||
};
|
||||
|
||||
const char *default_iommu_mode = "vfio_type1_iommu";
|
||||
@@ -407,6 +456,57 @@ static const struct vfio_iommu_mode *lookup_iommu_mode(const char *iommu_mode)
|
||||
VFIO_FAIL("Unrecognized IOMMU mode: %s\n", iommu_mode);
|
||||
}
|
||||
|
||||
static void vfio_device_bind_iommufd(int device_fd, int iommufd)
|
||||
{
|
||||
struct vfio_device_bind_iommufd args = {
|
||||
.argsz = sizeof(args),
|
||||
.iommufd = iommufd,
|
||||
};
|
||||
|
||||
ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args);
|
||||
}
|
||||
|
||||
static u32 iommufd_ioas_alloc(int iommufd)
|
||||
{
|
||||
struct iommu_ioas_alloc args = {
|
||||
.size = sizeof(args),
|
||||
};
|
||||
|
||||
ioctl_assert(iommufd, IOMMU_IOAS_ALLOC, &args);
|
||||
return args.out_ioas_id;
|
||||
}
|
||||
|
||||
static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id)
|
||||
{
|
||||
struct vfio_device_attach_iommufd_pt args = {
|
||||
.argsz = sizeof(args),
|
||||
.pt_id = pt_id,
|
||||
};
|
||||
|
||||
ioctl_assert(device_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &args);
|
||||
}
|
||||
|
||||
static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *bdf)
|
||||
{
|
||||
const char *cdev_path = vfio_pci_get_cdev_path(bdf);
|
||||
|
||||
device->fd = open(cdev_path, O_RDWR);
|
||||
VFIO_ASSERT_GE(device->fd, 0);
|
||||
free((void *)cdev_path);
|
||||
|
||||
/*
|
||||
* Require device->iommufd to be >0 so that a simple non-0 check can be
|
||||
* used to check if iommufd is enabled. In practice open() will never
|
||||
* return 0 unless stdin is closed.
|
||||
*/
|
||||
device->iommufd = open("/dev/iommu", O_RDWR);
|
||||
VFIO_ASSERT_GT(device->iommufd, 0);
|
||||
|
||||
vfio_device_bind_iommufd(device->fd, device->iommufd);
|
||||
device->ioas_id = iommufd_ioas_alloc(device->iommufd);
|
||||
vfio_device_attach_iommufd_pt(device->fd, device->ioas_id);
|
||||
}
|
||||
|
||||
struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode)
|
||||
{
|
||||
struct vfio_pci_device *device;
|
||||
@@ -414,13 +514,16 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_
|
||||
device = calloc(1, sizeof(*device));
|
||||
VFIO_ASSERT_NOT_NULL(device);
|
||||
|
||||
INIT_LIST_HEAD(&device->dma_regions);
|
||||
|
||||
device->iommu_mode = lookup_iommu_mode(iommu_mode);
|
||||
|
||||
vfio_pci_container_setup(device);
|
||||
vfio_pci_group_setup(device, bdf);
|
||||
vfio_pci_iommu_setup(device);
|
||||
vfio_pci_device_setup(device, bdf);
|
||||
if (device->iommu_mode->container_path)
|
||||
vfio_pci_container_setup(device, bdf);
|
||||
else
|
||||
vfio_pci_iommufd_setup(device, bdf);
|
||||
|
||||
vfio_pci_device_setup(device);
|
||||
vfio_pci_driver_probe(device);
|
||||
|
||||
return device;
|
||||
@@ -444,8 +547,12 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *device)
|
||||
VFIO_ASSERT_EQ(close(device->msi_eventfds[i]), 0);
|
||||
}
|
||||
|
||||
VFIO_ASSERT_EQ(close(device->group_fd), 0);
|
||||
VFIO_ASSERT_EQ(close(device->container_fd), 0);
|
||||
if (device->iommufd) {
|
||||
VFIO_ASSERT_EQ(close(device->iommufd), 0);
|
||||
} else {
|
||||
VFIO_ASSERT_EQ(close(device->group_fd), 0);
|
||||
VFIO_ASSERT_EQ(close(device->container_fd), 0);
|
||||
}
|
||||
|
||||
free(device);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user