mirror of
https://github.com/torvalds/linux.git
synced 2026-06-04 20:46:48 +02:00
vfio: selftests: Encapsulate IOMMU mode
Encapsulate the "IOMMU mode" a test should use behind a new struct. In the future this will be used to support other types of IOMMUs besides VFIO_TYPE1_IOMMU, and allow users to select the mode on the command line. No functional change intended. Acked-by: Shuah Khan <skhan@linuxfoundation.org> Signed-off-by: David Matlack <dmatlack@google.com> Link: https://lore.kernel.org/r/20250822212518.4156428-25-dmatlack@google.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
This commit is contained in:
parent
118e073ef6
commit
5df9bd6205
|
|
@ -47,6 +47,12 @@
|
|||
VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
struct vfio_iommu_mode {
|
||||
const char *name;
|
||||
const char *container_path;
|
||||
unsigned long iommu_type;
|
||||
};
|
||||
|
||||
struct vfio_pci_bar {
|
||||
struct vfio_region_info info;
|
||||
void *vaddr;
|
||||
|
|
@ -144,6 +150,8 @@ struct vfio_pci_driver {
|
|||
|
||||
struct vfio_pci_device {
|
||||
int fd;
|
||||
|
||||
const struct vfio_iommu_mode *iommu_mode;
|
||||
int group_fd;
|
||||
int container_fd;
|
||||
|
||||
|
|
@ -177,7 +185,9 @@ struct vfio_pci_device {
|
|||
const char *vfio_selftests_get_bdf(int *argc, char *argv[]);
|
||||
const char *vfio_pci_get_cdev_path(const char *bdf);
|
||||
|
||||
struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type);
|
||||
extern const char *default_iommu_mode;
|
||||
|
||||
struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode);
|
||||
void vfio_pci_device_cleanup(struct vfio_pci_device *device);
|
||||
void vfio_pci_device_reset(struct vfio_pci_device *device);
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@
|
|||
#include "../../../kselftest.h"
|
||||
#include <vfio_util.h>
|
||||
|
||||
#define VFIO_DEV_PATH "/dev/vfio/vfio"
|
||||
#define PCI_SYSFS_PATH "/sys/bus/pci/devices"
|
||||
|
||||
#define ioctl_assert(_fd, _op, _arg) do { \
|
||||
|
|
@ -261,10 +260,11 @@ static unsigned int vfio_pci_get_group_from_dev(const char *bdf)
|
|||
|
||||
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(VFIO_DEV_PATH, O_RDWR);
|
||||
VFIO_ASSERT_GE(device->container_fd, 0, "open(%s) failed\n", VFIO_DEV_PATH);
|
||||
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);
|
||||
|
|
@ -290,8 +290,9 @@ 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, unsigned long iommu_type)
|
||||
static void vfio_pci_iommu_setup(struct vfio_pci_device *device)
|
||||
{
|
||||
unsigned long iommu_type = device->iommu_mode->iommu_type;
|
||||
int ret;
|
||||
|
||||
INIT_LIST_HEAD(&device->dma_regions);
|
||||
|
|
@ -363,16 +364,45 @@ const char *vfio_pci_get_cdev_path(const char *bdf)
|
|||
return cdev_path;
|
||||
}
|
||||
|
||||
struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type)
|
||||
static const struct vfio_iommu_mode iommu_modes[] = {
|
||||
{
|
||||
.name = "vfio_type1_iommu",
|
||||
.container_path = "/dev/vfio/vfio",
|
||||
.iommu_type = VFIO_TYPE1_IOMMU,
|
||||
},
|
||||
};
|
||||
|
||||
const char *default_iommu_mode = "vfio_type1_iommu";
|
||||
|
||||
static const struct vfio_iommu_mode *lookup_iommu_mode(const char *iommu_mode)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!iommu_mode)
|
||||
iommu_mode = default_iommu_mode;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(iommu_modes); i++) {
|
||||
if (strcmp(iommu_mode, iommu_modes[i].name))
|
||||
continue;
|
||||
|
||||
return &iommu_modes[i];
|
||||
}
|
||||
|
||||
VFIO_FAIL("Unrecognized IOMMU mode: %s\n", iommu_mode);
|
||||
}
|
||||
|
||||
struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode)
|
||||
{
|
||||
struct vfio_pci_device *device;
|
||||
|
||||
device = calloc(1, sizeof(*device));
|
||||
VFIO_ASSERT_NOT_NULL(device);
|
||||
|
||||
device->iommu_mode = lookup_iommu_mode(iommu_mode);
|
||||
|
||||
vfio_pci_container_setup(device);
|
||||
vfio_pci_group_setup(device, bdf);
|
||||
vfio_pci_iommu_setup(device, iommu_type);
|
||||
vfio_pci_iommu_setup(device);
|
||||
vfio_pci_device_setup(device, bdf);
|
||||
|
||||
vfio_pci_driver_probe(device);
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, anonymous_hugetlb_1gb) {
|
|||
|
||||
FIXTURE_SETUP(vfio_dma_mapping_test)
|
||||
{
|
||||
self->device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU);
|
||||
self->device = vfio_pci_device_init(device_bdf, default_iommu_mode);
|
||||
}
|
||||
|
||||
FIXTURE_TEARDOWN(vfio_dma_mapping_test)
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ FIXTURE(vfio_pci_device_test) {
|
|||
|
||||
FIXTURE_SETUP(vfio_pci_device_test)
|
||||
{
|
||||
self->device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU);
|
||||
self->device = vfio_pci_device_init(device_bdf, default_iommu_mode);
|
||||
}
|
||||
|
||||
FIXTURE_TEARDOWN(vfio_pci_device_test)
|
||||
|
|
@ -116,7 +116,7 @@ FIXTURE_VARIANT_ADD(vfio_pci_irq_test, msix) {
|
|||
|
||||
FIXTURE_SETUP(vfio_pci_irq_test)
|
||||
{
|
||||
self->device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU);
|
||||
self->device = vfio_pci_device_init(device_bdf, default_iommu_mode);
|
||||
}
|
||||
|
||||
FIXTURE_TEARDOWN(vfio_pci_irq_test)
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ FIXTURE_SETUP(vfio_pci_driver_test)
|
|||
{
|
||||
struct vfio_pci_driver *driver;
|
||||
|
||||
self->device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU);
|
||||
self->device = vfio_pci_device_init(device_bdf, default_iommu_mode);
|
||||
|
||||
driver = &self->device->driver;
|
||||
|
||||
|
|
@ -222,7 +222,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
device_bdf = vfio_selftests_get_bdf(&argc, argv);
|
||||
|
||||
device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU);
|
||||
device = vfio_pci_device_init(device_bdf, default_iommu_mode);
|
||||
if (!device->driver.ops) {
|
||||
fprintf(stderr, "No driver found for device %s\n", device_bdf);
|
||||
return KSFT_SKIP;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user