Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

David Matlack and committed by
Alex Williamson
5df9bd62 118e073e

+52 -12
+11 -1
tools/testing/selftests/vfio/lib/include/vfio_util.h
··· 47 47 VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \ 48 48 } while (0) 49 49 50 + struct vfio_iommu_mode { 51 + const char *name; 52 + const char *container_path; 53 + unsigned long iommu_type; 54 + }; 55 + 50 56 struct vfio_pci_bar { 51 57 struct vfio_region_info info; 52 58 void *vaddr; ··· 150 144 151 145 struct vfio_pci_device { 152 146 int fd; 147 + 148 + const struct vfio_iommu_mode *iommu_mode; 153 149 int group_fd; 154 150 int container_fd; 155 151 ··· 185 177 const char *vfio_selftests_get_bdf(int *argc, char *argv[]); 186 178 const char *vfio_pci_get_cdev_path(const char *bdf); 187 179 188 - struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type); 180 + extern const char *default_iommu_mode; 181 + 182 + struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode); 189 183 void vfio_pci_device_cleanup(struct vfio_pci_device *device); 190 184 void vfio_pci_device_reset(struct vfio_pci_device *device); 191 185
+36 -6
tools/testing/selftests/vfio/lib/vfio_pci_device.c
··· 18 18 #include "../../../kselftest.h" 19 19 #include <vfio_util.h> 20 20 21 - #define VFIO_DEV_PATH "/dev/vfio/vfio" 22 21 #define PCI_SYSFS_PATH "/sys/bus/pci/devices" 23 22 24 23 #define ioctl_assert(_fd, _op, _arg) do { \ ··· 260 261 261 262 static void vfio_pci_container_setup(struct vfio_pci_device *device) 262 263 { 264 + const char *path = device->iommu_mode->container_path; 263 265 int version; 264 266 265 - device->container_fd = open(VFIO_DEV_PATH, O_RDWR); 266 - VFIO_ASSERT_GE(device->container_fd, 0, "open(%s) failed\n", VFIO_DEV_PATH); 267 + device->container_fd = open(path, O_RDWR); 268 + VFIO_ASSERT_GE(device->container_fd, 0, "open(%s) failed\n", path); 267 269 268 270 version = ioctl(device->container_fd, VFIO_GET_API_VERSION); 269 271 VFIO_ASSERT_EQ(version, VFIO_API_VERSION); ··· 290 290 ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->container_fd); 291 291 } 292 292 293 - static void vfio_pci_iommu_setup(struct vfio_pci_device *device, unsigned long iommu_type) 293 + static void vfio_pci_iommu_setup(struct vfio_pci_device *device) 294 294 { 295 + unsigned long iommu_type = device->iommu_mode->iommu_type; 295 296 int ret; 296 297 297 298 INIT_LIST_HEAD(&device->dma_regions); ··· 364 363 return cdev_path; 365 364 } 366 365 367 - struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type) 366 + static const struct vfio_iommu_mode iommu_modes[] = { 367 + { 368 + .name = "vfio_type1_iommu", 369 + .container_path = "/dev/vfio/vfio", 370 + .iommu_type = VFIO_TYPE1_IOMMU, 371 + }, 372 + }; 373 + 374 + const char *default_iommu_mode = "vfio_type1_iommu"; 375 + 376 + static const struct vfio_iommu_mode *lookup_iommu_mode(const char *iommu_mode) 377 + { 378 + int i; 379 + 380 + if (!iommu_mode) 381 + iommu_mode = default_iommu_mode; 382 + 383 + for (i = 0; i < ARRAY_SIZE(iommu_modes); i++) { 384 + if (strcmp(iommu_mode, iommu_modes[i].name)) 385 + continue; 386 + 387 + return &iommu_modes[i]; 388 + } 389 + 390 + VFIO_FAIL("Unrecognized IOMMU mode: %s\n", iommu_mode); 391 + } 392 + 393 + struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode) 368 394 { 369 395 struct vfio_pci_device *device; 370 396 371 397 device = calloc(1, sizeof(*device)); 372 398 VFIO_ASSERT_NOT_NULL(device); 373 399 400 + device->iommu_mode = lookup_iommu_mode(iommu_mode); 401 + 374 402 vfio_pci_container_setup(device); 375 403 vfio_pci_group_setup(device, bdf); 376 - vfio_pci_iommu_setup(device, iommu_type); 404 + vfio_pci_iommu_setup(device); 377 405 vfio_pci_device_setup(device, bdf); 378 406 379 407 vfio_pci_driver_probe(device);
+1 -1
tools/testing/selftests/vfio/vfio_dma_mapping_test.c
··· 116 116 117 117 FIXTURE_SETUP(vfio_dma_mapping_test) 118 118 { 119 - self->device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU); 119 + self->device = vfio_pci_device_init(device_bdf, default_iommu_mode); 120 120 } 121 121 122 122 FIXTURE_TEARDOWN(vfio_dma_mapping_test)
+2 -2
tools/testing/selftests/vfio/vfio_pci_device_test.c
··· 28 28 29 29 FIXTURE_SETUP(vfio_pci_device_test) 30 30 { 31 - self->device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU); 31 + self->device = vfio_pci_device_init(device_bdf, default_iommu_mode); 32 32 } 33 33 34 34 FIXTURE_TEARDOWN(vfio_pci_device_test) ··· 116 116 117 117 FIXTURE_SETUP(vfio_pci_irq_test) 118 118 { 119 - self->device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU); 119 + self->device = vfio_pci_device_init(device_bdf, default_iommu_mode); 120 120 } 121 121 122 122 FIXTURE_TEARDOWN(vfio_pci_irq_test)
+2 -2
tools/testing/selftests/vfio/vfio_pci_driver_test.c
··· 60 60 { 61 61 struct vfio_pci_driver *driver; 62 62 63 - self->device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU); 63 + self->device = vfio_pci_device_init(device_bdf, default_iommu_mode); 64 64 65 65 driver = &self->device->driver; 66 66 ··· 222 222 223 223 device_bdf = vfio_selftests_get_bdf(&argc, argv); 224 224 225 - device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU); 225 + device = vfio_pci_device_init(device_bdf, default_iommu_mode); 226 226 if (!device->driver.ops) { 227 227 fprintf(stderr, "No driver found for device %s\n", device_bdf); 228 228 return KSFT_SKIP;