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

drm/xe/tests: Add helpers for use in live tests

Instead of iterating over available Xe devices within a testcase,
without being able to distinguish potential failures from different
devices on system with many Xe devices, introduce helpers that will
allow to treat each Xe device as a parameter for the testcase like:

static void bar(struct kunit *test)
{
struct xe_device *xe = test->priv;
...
}

struct kunit_case foo_live_tests[] = {
KUNIT_CASE_PARAM(bar, xe_pci_live_device_gen_param),
{}
};

struct kunit_suite foo_suite = {
.name = "foo_live",
.test_cases = foo_live_tests,
.init = xe_kunit_helper_xe_device_live_test_init,
};

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Reviewed-by: Jonathan Cavitt <jonathan.cavitt@intel.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20240720142528.530-3-michal.wajdeczko@intel.com

+73
+39
drivers/gpu/drm/xe/tests/xe_kunit_helpers.c
··· 12 12 13 13 #include "tests/xe_kunit_helpers.h" 14 14 #include "tests/xe_pci_test.h" 15 + #include "xe_device.h" 15 16 #include "xe_device_types.h" 17 + #include "xe_pm.h" 16 18 17 19 /** 18 20 * xe_kunit_helper_alloc_xe_device - Allocate a &xe_device for a KUnit test. ··· 90 88 return 0; 91 89 } 92 90 EXPORT_SYMBOL_IF_KUNIT(xe_kunit_helper_xe_device_test_init); 91 + 92 + KUNIT_DEFINE_ACTION_WRAPPER(put_xe_pm_runtime, xe_pm_runtime_put, struct xe_device *); 93 + 94 + /** 95 + * xe_kunit_helper_xe_device_live_test_init - Prepare a &xe_device for 96 + * use in a live KUnit test. 97 + * @test: the &kunit where live &xe_device will be used 98 + * 99 + * This function expects pointer to the &xe_device in the &test.param_value, 100 + * like it is prepared by the &xe_pci_live_device_gen_param and stores that 101 + * pointer as &kunit.priv to allow the test code to access it. 102 + * 103 + * This function makes sure that device is not wedged and then resumes it 104 + * to avoid waking up the device inside the test. It uses deferred cleanup 105 + * action to release a runtime_pm reference. 106 + * 107 + * This function can be used as custom implementation of &kunit_suite.init. 108 + * 109 + * This function uses KUNIT_ASSERT to detect any failures. 110 + * 111 + * Return: Always 0. 112 + */ 113 + int xe_kunit_helper_xe_device_live_test_init(struct kunit *test) 114 + { 115 + struct xe_device *xe = xe_device_const_cast(test->param_value); 116 + 117 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, xe); 118 + kunit_info(test, "running on %s device\n", xe->info.platform_name); 119 + 120 + KUNIT_ASSERT_FALSE(test, xe_device_wedged(xe)); 121 + xe_pm_runtime_get(xe); 122 + KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test, put_xe_pm_runtime, xe)); 123 + 124 + test->priv = xe; 125 + return 0; 126 + } 127 + EXPORT_SYMBOL_IF_KUNIT(xe_kunit_helper_xe_device_live_test_init);
+2
drivers/gpu/drm/xe/tests/xe_kunit_helpers.h
··· 14 14 struct device *dev); 15 15 int xe_kunit_helper_xe_device_test_init(struct kunit *test); 16 16 17 + int xe_kunit_helper_xe_device_live_test_init(struct kunit *test); 18 + 17 19 #endif
+30
drivers/gpu/drm/xe/tests/xe_pci.c
··· 167 167 return 0; 168 168 } 169 169 EXPORT_SYMBOL_IF_KUNIT(xe_pci_fake_device_init); 170 + 171 + /** 172 + * xe_pci_live_device_gen_param - Helper to iterate Xe devices as KUnit parameters 173 + * @prev: the previously returned value, or NULL for the first iteration 174 + * @desc: the buffer for a parameter name 175 + * 176 + * Iterates over the available Xe devices on the system. Uses the device name 177 + * as the parameter name. 178 + * 179 + * To be used only as a parameter generator function in &KUNIT_CASE_PARAM. 180 + * 181 + * Return: pointer to the next &struct xe_device ready to be used as a parameter 182 + * or NULL if there are no more Xe devices on the system. 183 + */ 184 + const void *xe_pci_live_device_gen_param(const void *prev, char *desc) 185 + { 186 + const struct xe_device *xe = prev; 187 + struct device *dev = xe ? xe->drm.dev : NULL; 188 + struct device *next; 189 + 190 + next = driver_find_next_device(&xe_pci_driver.driver, dev); 191 + if (dev) 192 + put_device(dev); 193 + if (!next) 194 + return NULL; 195 + 196 + snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s", dev_name(next)); 197 + return pdev_to_xe_device(to_pci_dev(next)); 198 + } 199 + EXPORT_SYMBOL_IF_KUNIT(xe_pci_live_device_gen_param);
+2
drivers/gpu/drm/xe/tests/xe_pci_test.h
··· 35 35 36 36 int xe_pci_fake_device_init(struct xe_device *xe); 37 37 38 + const void *xe_pci_live_device_gen_param(const void *prev, char *desc); 39 + 38 40 #endif