Skip to contentSkip to conversation

RFC: Introduce new selftests framework

PATCH In review Michal Wajdeczko 5 participants 19 messages Mar 13, 2023 – Mar 21, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover RFC: Introduce new selftests framework

Existing solution to support live testing has many limitations. With proposed here framework we can live test every new bound device and use same style for both mock and live tests of Xe, GT or GuC.

Also Kunit parsing works better (which might be needed by our CI).

And with little more effort, patches pending, we can add support for rerunning tests on selected device or filtering tests to be executed.

This new framework can coexist with any other default kunit tests and any currently available live tests implemented as separate module.

Sample output from mock run:

[ ] Starting KUnit Kernel (1/1)...
[ ] ============================================================ [ ] ================= device_mock (2 subtests) ================= [ ] [PASSED] test_basic
[ ] [PASSED] test_to_gt
[ ] =================== [PASSED] device_mock =================== [ ] =================== gt_mock (2 subtests) =================== [ ] [PASSED] test_basic
[ ] [PASSED] test_to_xe
[ ] ===================== [PASSED] gt_mock ===================== [ ] =================== guc_mock (1 subtest) =================== [ ] [PASSED] test_basic
[ ] ==================== [PASSED] guc_mock ===================== [ ] ================= guc_ct_mock (1 subtest) ================== [ ] [PASSED] test_basic
[ ] =================== [PASSED] guc_ct_mock =================== [ ] ============================================================ [ ] Testing complete. Ran 6 tests: passed: 6

Sample output from live run with "dut_filter=*"

[ ] ============================================================ [ ] ================= device_live (2 subtests) ================= [ ] [PASSED] test_to_gt
[ ] [PASSED] test_guc_submission_enabled
[ ] =================== [PASSED] device_live =================== [ ] =================== gt_live (2 subtests) =================== [ ] [PASSED] test_gt_info
[ ] [PASSED] test_gt_is_not_media_type
[ ] ===================== [PASSED] gt_live ===================== [ ] ================== guc_live (2 subtests) =================== [ ] [PASSED] test_guc_fw
[ ] [SKIPPED] test_guc_rejects_unknown_action
[ ] ==================== [PASSED] guc_live ===================== [ ] =================== ct_live (2 subtests) =================== [ ] [PASSED] test_ct_not_broken
[ ] [PASSED] test_ct_rejects_unknown_action
[ ] ===================== [PASSED] ct_live ===================== [ ] ============================================================ [ ] Testing complete. Ran 8 tests: passed: 7, skipped: 1

Sample raw output from live run with "anonymous_dut=1"

[ ] [drm] Initialized xe 1.1.0 20201103 for 0000:00:02.0 on minor 0

[ ]     # device_live: DUT 0000:00:02.0
[ ]     # Subtest: device_live
[ ]     1..2
[ ]     ok 1 - test_to_gt
[ ]     ok 2 - test_guc_submission_enabled

[ ] # device_live: pass:2 fail:0 skip:0 total:2
[ ] # Totals: pass:2 fail:0 skip:0 total:2
...

Sample raw output from live run with "anonymous_dut=0"

[ ] # device_live: # failed to initialize (-25)
[ ] not ok 1 - device_live
...

Cc Mauro Carvalho Chehab <[email protected]>

Michal Wajdeczko (5):
drm/xe: Introduce new selftests framework
drm/xe: Initial selftests for xe_device
drm/xe: Initial selftests for xe_gt
drm/xe: Initial selftests for xe_guc
drm/xe: Initial selftests for xe_guc_ct

 drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++
 drivers/gpu/drm/xe/Makefile                   |  11 ++
 .../selftests/live/xe_device_live_selftest.c  |  39 ++++++
 .../xe/selftests/live/xe_gt_live_selftest.c   |  40 ++++++
 .../selftests/live/xe_guc_ct_live_selftest.c  |  52 ++++++++
 .../xe/selftests/live/xe_guc_live_selftest.c  |  54 ++++++++
 .../selftests/mock/xe_device_mock_selftest.c  |  41 ++++++
 .../xe/selftests/mock/xe_gt_mock_selftest.c   |  40 ++++++
 .../selftests/mock/xe_guc_ct_mock_selftest.c  |  31 +++++
 .../xe/selftests/mock/xe_guc_mock_selftest.c  |  30 +++++
 drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++
 .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++
 .../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++
 .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++
 .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++
 .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++
 .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++
 drivers/gpu/drm/xe/xe_device.c                |   6 +
 drivers/gpu/drm/xe/xe_gt.c                    |   6 +
 drivers/gpu/drm/xe/xe_guc.c                   |   6 +
 drivers/gpu/drm/xe/xe_guc_ct.c                |   6 +
 drivers/gpu/drm/xe/xe_module.c                |   2 +
 22 files changed, 763 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_device_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_gt_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_guc_ct_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_guc_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_device_mock_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_gt_mock_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_guc_ct_mock_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_guc_mock_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/5 drm/xe: Introduce new selftests framework

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

---
 drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++
 drivers/gpu/drm/xe/Makefile                   |  11 ++
 drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++
 .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++
 .../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++
 .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++
 .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++
 .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++
 .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++
 drivers/gpu/drm/xe/xe_module.c                |   2 +
 10 files changed, 412 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c
drivers/gpu/drm/xe/Kconfig.debug +22 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE
 
 	  If in doubt, say "N".
 
+config DRM_XE_SELFTESTS_MOCK
+	bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS
+	depends on DRM_XE && KUNIT
+	default KUNIT_ALL_TESTS
+	help
+		Choose this option to allow the driver to perform mock
+		selftests based on the Kunit framework.
+
+		Recommended for driver developers only.
+		If in doubt, say "N".
+
+config DRM_XE_SELFTESTS_LIVE
+	bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK
+	depends on DRM_XE && KUNIT && EXPERT
+	default n
+	help
+		Choose this option to allow the driver to perform live
+		selftests based on the Kunit framework.
+
+		Recommended for driver developers only.
+		If in doubt, say "N".
+
 config DRM_XE_KUNIT_TEST
         tristate "KUnit tests for the drm xe driver" if !KUNIT_ALL_TESTS
 	depends on DRM_XE && KUNIT && DEBUG_FS
drivers/gpu/drm/xe/Makefile +11 -0
index b94e65c91101..b193af2d9930 100644
@@ -211,6 +211,17 @@ ifeq ($(CONFIG_DRM_FBDEV_EMULATION),y)
 	xe-$(CONFIG_DRM_XE_DISPLAY) += display/intel_fbdev.o
 endif
 
+xe-$(CONFIG_DRM_XE_SELFTESTS_MOCK) += \
+	selftests/xe_selftests_mock.o \
+
+xe-$(CONFIG_DRM_XE_SELFTESTS_LIVE) += \
+	selftests/xe_selftests_auto.o \
+	selftests/xe_selftests_executor.o \
+	selftests/xe_selftests_params.o \
+
+xe-$(CONFIG_KUNIT) += \
+	selftests/xe_selftests_helpers.o \
+
 obj-$(CONFIG_DRM_XE) += xe.o
 obj-$(CONFIG_DRM_XE_KUNIT_TEST) += tests/
 
drivers/gpu/drm/xe/selftests/xe_selftests.h new file +35 -0
new file mode 100644
index 000000000000..6b13b998e789
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTESTS_H_
+#define _XE_SELFTESTS_H_
+
+struct kunit_suite;
+struct kunit;
+
+int xe_selftests_run(const char *dev_name);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int init_xe_live_selftest_suite(struct kunit_suite *suite);
+#endif
+
+int init_xe_device_selftest(struct kunit *test);
+void exit_xe_device_selftest(struct kunit *test);
+
+int init_xe_gt_selftest(struct kunit *test);
+void exit_xe_gt_selftest(struct kunit *test);
+
+int init_xe_guc_selftest(struct kunit *test);
+void exit_xe_guc_selftest(struct kunit *test);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int xe_selftests_module_init(void);
+void xe_selftests_module_exit(void);
+#else
+static inline int xe_selftests_module_init(void) { return 0; }
+static inline void xe_selftests_module_exit(void) { }
+#endif
+
+#endif /* _XE_SELFTESTS_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +46 -0

46 lines not on this page · raw message

drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +118 -0

118 lines not on this page · raw message

drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c new file +101 -0

101 lines not on this page · raw message

drivers/gpu/drm/xe/selftests/xe_selftests_internal.h new file +22 -0
new file mode 100644
index 000000000000..841c1a497eed
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTEST_INTERNAL_H_
+#define _XE_SELFTEST_INTERNAL_H_
+
+#include <linux/types.h>
+
+struct xe_device;
+
+extern char *dut_filter;
+extern bool anonymous_dut;
+
+struct xe_device *get_xe_dut(void);
+void put_xe_dut(struct xe_device *xe);
+
+struct xe_device *get_xe_mock(void);
+void put_xe_mock(struct xe_device *xe);
+
+#endif /* _XE_SELFTEST_INTERNAL_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_mock.c new file +32 -0
new file mode 100644
index 000000000000..c1515e451e99
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include "xe_device_types.h"
+
+#define MOCK_DEV_NAME "MOCK:DEBUG.0"
+static struct device __mock_device = {
+	.init_name = MOCK_DEV_NAME,
+};
+
+struct xe_device *get_xe_mock(void)
+{
+	struct xe_device *xe = kzalloc(sizeof(*xe), GFP_KERNEL);
+
+	if (!xe)
+		return ERR_PTR(-ENOMEM);
+
+	xe->drm.dev = &__mock_device;
+
+	xe->gt[0].info.id = 0;
+	xe->gt[0].info.type = XE_GT_TYPE_MAIN;
+	xe->gt[0].xe = xe;
+
+	return xe;
+}
+
+void put_xe_mock(struct xe_device *xe)
+{
+	kfree(xe);
+}
drivers/gpu/drm/xe/selftests/xe_selftests_params.c new file +23 -0
new file mode 100644
index 000000000000..e6e28cc91901
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/module.h>
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+
+char *dut_filter;
+module_param_named(dut_filter, dut_filter, charp, 0644);
+MODULE_PARM_DESC(dut_filter,
+		 "The pattern of the names of devices on which we want to run tests automatically. "
+		 "If <none> is specified (default) then no devices will be tested automatically. "
+		 "If '*' is specified then all new bound devices will be tested.\n");
+
+bool anonymous_dut;
+module_param_named(anonymous_dut, anonymous_dut, bool, 0600);
+MODULE_PARM_DESC(anonymous_dut,
+		 "Enables selftests to be run without providing explicit DUT name (like during "
+		 "selftests execution triggered by the Kunit module notifier). Default is off.\n");
+
+#endif
drivers/gpu/drm/xe/xe_module.c +2 -0
index 6860586ce7f8..f76d69f7f5ad 100644
@@ -13,6 +13,7 @@
 #include "xe_module.h"
 #include "xe_pci.h"
 #include "xe_sched_job.h"
+#include "selftests/xe_selftests.h"
 
 bool enable_guc = true;
 module_param_named_unsafe(enable_guc, enable_guc, bool, 0444);
@@ -45,6 +46,7 @@ struct init_funcs {
 static const struct init_funcs init_funcs[] = {
 	MAKE_INIT_EXIT_FUNCS(hw_fence),
 	MAKE_INIT_EXIT_FUNCS(sched_job),
+	MAKE_INIT_EXIT_FUNCS(selftests),
 };
 
 static int __init xe_init(void)
Signature · 1 line
2.25.1
#2 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

Quoted 226 lines · On Mon, 13 Mar 2023 09:13:12 +0100 Michal Wajdeczko…

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

---
 drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++
 drivers/gpu/drm/xe/Makefile                   |  11 ++
 drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++
 .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++
 .../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++
 .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++
 .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++
 .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++
 .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++
 drivers/gpu/drm/xe/xe_module.c                |   2 +
 10 files changed, 412 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c
drivers/gpu/drm/xe/Kconfig.debug +22 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE
 
 	  If in doubt, say "N".
 
+config DRM_XE_SELFTESTS_MOCK
+	bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS
+	depends on DRM_XE && KUNIT
+	default KUNIT_ALL_TESTS
+	help
+		Choose this option to allow the driver to perform mock
+		selftests based on the Kunit framework.
+
+		Recommended for driver developers only.
+		If in doubt, say "N".
+
+config DRM_XE_SELFTESTS_LIVE
+	bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK
+	depends on DRM_XE && KUNIT && EXPERT
+	default n
+	help
+		Choose this option to allow the driver to perform live
+		selftests based on the Kunit framework.
+
+		Recommended for driver developers only.
+		If in doubt, say "N".
+
 config DRM_XE_KUNIT_TEST
         tristate "KUnit tests for the drm xe driver" if !KUNIT_ALL_TESTS
 	depends on DRM_XE && KUNIT && DEBUG_FS
drivers/gpu/drm/xe/Makefile +11 -0
index b94e65c91101..b193af2d9930 100644
@@ -211,6 +211,17 @@ ifeq ($(CONFIG_DRM_FBDEV_EMULATION),y)
 	xe-$(CONFIG_DRM_XE_DISPLAY) += display/intel_fbdev.o
 endif
 
+xe-$(CONFIG_DRM_XE_SELFTESTS_MOCK) += \
+	selftests/xe_selftests_mock.o \
+
+xe-$(CONFIG_DRM_XE_SELFTESTS_LIVE) += \
+	selftests/xe_selftests_auto.o \
+	selftests/xe_selftests_executor.o \
+	selftests/xe_selftests_params.o \
+
+xe-$(CONFIG_KUNIT) += \
+	selftests/xe_selftests_helpers.o \
+
 obj-$(CONFIG_DRM_XE) += xe.o
 obj-$(CONFIG_DRM_XE_KUNIT_TEST) += tests/
 
drivers/gpu/drm/xe/selftests/xe_selftests.h new file +35 -0
new file mode 100644
index 000000000000..6b13b998e789
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTESTS_H_
+#define _XE_SELFTESTS_H_
+
+struct kunit_suite;
+struct kunit;
+
+int xe_selftests_run(const char *dev_name);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int init_xe_live_selftest_suite(struct kunit_suite *suite);
+#endif
+
+int init_xe_device_selftest(struct kunit *test);
+void exit_xe_device_selftest(struct kunit *test);
+
+int init_xe_gt_selftest(struct kunit *test);
+void exit_xe_gt_selftest(struct kunit *test);
+
+int init_xe_guc_selftest(struct kunit *test);
+void exit_xe_guc_selftest(struct kunit *test);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int xe_selftests_module_init(void);
+void xe_selftests_module_exit(void);
+#else
+static inline int xe_selftests_module_init(void) { return 0; }
+static inline void xe_selftests_module_exit(void) { }
+#endif
+
+#endif /* _XE_SELFTESTS_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +46 -0
new file mode 100644
index 000000000000..ef98f3599eac
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/device.h>
+#include <linux/glob.h>
+#include <linux/pci.h>
+
+#include "xe_drv.h"
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+static int bus_cb(struct notifier_block *nb, unsigned long action, void *data)
+{
+	struct device *dev = data;
+	struct device_driver *drv = dev->driver;
+
+	if (action != BUS_NOTIFY_BOUND_DRIVER)
+		return 0;
+
+	if (strcmp(DRIVER_NAME, drv->name))
+		return 0;
+
+	/* only matching DUT names will be tested */
+	if (!dut_filter || !glob_match(dut_filter, dev_name(dev)))
+		return 0;
+
+	xe_selftests_run(dev_name(dev));
+
+	return 0;
+}
+
+static struct notifier_block nb = {
+	.notifier_call = bus_cb,
+};
+
+int xe_selftests_module_init(void)
+{
+	return bus_register_notifier(&pci_bus_type, &nb);
+}
+
+void xe_selftests_module_exit(void)
+{
+	bus_unregister_notifier(&pci_bus_type, &nb);
+}
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +28 -0
new file mode 100644
index 000000000000..93c3360c9631
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/glob.h>
+#include <kunit/test.h>
+
+#include "xe_device.h"
+#include "xe_drv.h"
+
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+#define DUT_NAME_LEN 32
+static char dut_name[DUT_NAME_LEN];
+
+/* protects @dut_name during live test execution */
+static DEFINE_MUTEX(dut_mutex);
+
+static int match_dev_any(struct device *dev, const void *data)
+{
+	return 1;
+}
+
+static int match_dev_by_name(struct device *dev, const void *data)
+{
+	const char *name = data;

drivers/gpu/drm/xe/selftests/xe_selftests_executor.c line 38

Quoted 10 lines
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +10 -0
+	return !strcmp(dev_name(dev), name);
+}
+
+struct xe_device *get_xe_dut(void)
+{
+	struct device_driver *drv;
+	struct device *dev;
+
+	if (!anonymous_dut && !mutex_is_locked(&dut_mutex))
+		return ERR_PTR(-ENOTTY);

Hmm... I guess EBUSY would fit better here.

Quoted 15 lines
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +15 -0
+
+	drv = driver_find(DRIVER_NAME, &pci_bus_type);
+	dev = driver_find_device(drv, NULL, dut_name,
+				 mutex_is_locked(&dut_mutex) ? match_dev_by_name : match_dev_any);
+	if (!dev)
+		return ERR_PTR(-ENODEV);
+
+	return to_xe_device(dev_get_drvdata(dev));
+}
+
+void put_xe_dut(struct xe_device *xe)
+{
+	put_device(xe->drm.dev);
+}
+

drivers/gpu/drm/xe/selftests/xe_selftests_executor.c line 63

Quoted 10 lines
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +10 -0
+static int run_test_suites(struct xe_device *xe)
+{
+	struct device *dev = xe->drm.dev;
+	struct device_driver *drv = dev->driver;
+	struct module *mod = drv->owner;
+	struct kunit_suite **suites = mod->kunit_suites;
+	int num_suites = mod->num_kunit_suites;
+	int i;
+
+	pr_info("TAP version 14\n");

It doesn't seem nice to hardcode it here. IMO, this should be in sync with lib/kunit/executor.c.

Maybe the way to go would be to add, at kunit/test.h:

#define TAP_VERSION "TAP version 14\n"

And use such macro on both lib/kunit/executor.c and here.

We need to c/c kunit maintainers on such patch.

IMO, a better approach would be to modify lib/kunit/executor.c, adding there a variant of kunit_run_all_tests() to be used with a custom filter list, e. g.:

kunit_run_filtered_tests(struct suite_set *suite_set, (int *)filter_test(struct suite_set *suite_set))

And then calling it at xe_selftests_executor.c.

Quoted 4 lines
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +4 -0
+	pr_info("1..%u\n", num_suites);
+
+	/* reset kunit_suite_counter at the start */
+	__kunit_test_suites_exit(NULL, 0);

drivers/gpu/drm/xe/selftests/xe_selftests_executor.c line 77

Quoted 10 lines
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +10 -0
+
+	for (i = 0; i < num_suites; i++) {
+		kunit_run_tests(suites[i]);
+	}
+
+	/* reset kunit_suite_counter at the end */
+	__kunit_test_suites_exit(NULL, 0);
+
+	return num_suites;
+}

Regards,
Mauro

#3 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

Quoted 233 lines · Mauro Carvalho Chehab
Quoted 230 lines · On Mon, 13 Mar 2023 09:13:12 +0100 Michal Wajdeczko…

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

---
 drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++
 drivers/gpu/drm/xe/Makefile                   |  11 ++
 drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++
 .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++
 .../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++
 .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++
 .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++
 .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++
 .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++
 drivers/gpu/drm/xe/xe_module.c                |   2 +
 10 files changed, 412 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c
drivers/gpu/drm/xe/Kconfig.debug +22 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE
 
 	  If in doubt, say "N".
 
+config DRM_XE_SELFTESTS_MOCK
+	bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS
+	depends on DRM_XE && KUNIT
+	default KUNIT_ALL_TESTS
+	help
+		Choose this option to allow the driver to perform mock
+		selftests based on the Kunit framework.
+
+		Recommended for driver developers only.
+		If in doubt, say "N".
+
+config DRM_XE_SELFTESTS_LIVE
+	bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK
+	depends on DRM_XE && KUNIT && EXPERT
+	default n
+	help
+		Choose this option to allow the driver to perform live
+		selftests based on the Kunit framework.
+
+		Recommended for driver developers only.
+		If in doubt, say "N".
+
 config DRM_XE_KUNIT_TEST
         tristate "KUnit tests for the drm xe driver" if !KUNIT_ALL_TESTS
 	depends on DRM_XE && KUNIT && DEBUG_FS
drivers/gpu/drm/xe/Makefile +11 -0
index b94e65c91101..b193af2d9930 100644
@@ -211,6 +211,17 @@ ifeq ($(CONFIG_DRM_FBDEV_EMULATION),y)
 	xe-$(CONFIG_DRM_XE_DISPLAY) += display/intel_fbdev.o
 endif
 
+xe-$(CONFIG_DRM_XE_SELFTESTS_MOCK) += \
+	selftests/xe_selftests_mock.o \
+
+xe-$(CONFIG_DRM_XE_SELFTESTS_LIVE) += \
+	selftests/xe_selftests_auto.o \
+	selftests/xe_selftests_executor.o \
+	selftests/xe_selftests_params.o \
+
+xe-$(CONFIG_KUNIT) += \
+	selftests/xe_selftests_helpers.o \
+
 obj-$(CONFIG_DRM_XE) += xe.o
 obj-$(CONFIG_DRM_XE_KUNIT_TEST) += tests/
 
drivers/gpu/drm/xe/selftests/xe_selftests.h new file +35 -0
new file mode 100644
index 000000000000..6b13b998e789
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTESTS_H_
+#define _XE_SELFTESTS_H_
+
+struct kunit_suite;
+struct kunit;
+
+int xe_selftests_run(const char *dev_name);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int init_xe_live_selftest_suite(struct kunit_suite *suite);
+#endif
+
+int init_xe_device_selftest(struct kunit *test);
+void exit_xe_device_selftest(struct kunit *test);
+
+int init_xe_gt_selftest(struct kunit *test);
+void exit_xe_gt_selftest(struct kunit *test);
+
+int init_xe_guc_selftest(struct kunit *test);
+void exit_xe_guc_selftest(struct kunit *test);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int xe_selftests_module_init(void);
+void xe_selftests_module_exit(void);
+#else
+static inline int xe_selftests_module_init(void) { return 0; }
+static inline void xe_selftests_module_exit(void) { }
+#endif
+
+#endif /* _XE_SELFTESTS_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +46 -0
new file mode 100644
index 000000000000..ef98f3599eac
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/device.h>
+#include <linux/glob.h>
+#include <linux/pci.h>
+
+#include "xe_drv.h"
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+static int bus_cb(struct notifier_block *nb, unsigned long action, void *data)
+{
+	struct device *dev = data;
+	struct device_driver *drv = dev->driver;
+
+	if (action != BUS_NOTIFY_BOUND_DRIVER)
+		return 0;
+
+	if (strcmp(DRIVER_NAME, drv->name))
+		return 0;
+
+	/* only matching DUT names will be tested */
+	if (!dut_filter || !glob_match(dut_filter, dev_name(dev)))
+		return 0;
+
+	xe_selftests_run(dev_name(dev));
+
+	return 0;
+}
+
+static struct notifier_block nb = {
+	.notifier_call = bus_cb,
+};
+
+int xe_selftests_module_init(void)
+{
+	return bus_register_notifier(&pci_bus_type, &nb);
+}
+
+void xe_selftests_module_exit(void)
+{
+	bus_unregister_notifier(&pci_bus_type, &nb);
+}
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +32 -0
new file mode 100644
index 000000000000..93c3360c9631
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/glob.h>
+#include <kunit/test.h>
+
+#include "xe_device.h"
+#include "xe_drv.h"
+
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+#define DUT_NAME_LEN 32
+static char dut_name[DUT_NAME_LEN];
+
+/* protects @dut_name during live test execution */
+static DEFINE_MUTEX(dut_mutex);
+
+static int match_dev_any(struct device *dev, const void *data)
+{
+	return 1;
+}
+
+static int match_dev_by_name(struct device *dev, const void *data)
+{
+	const char *name = data;
+	return !strcmp(dev_name(dev), name);
+}
+
+struct xe_device *get_xe_dut(void)
Quoted 8 lines

drivers/gpu/drm/xe/selftests/xe_selftests_executor.c line 38

Quoted 6 lines
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +6 -0
+{
+	struct device_driver *drv;
+	struct device *dev;
+
+	if (!anonymous_dut && !mutex_is_locked(&dut_mutex))
+		return ERR_PTR(-ENOTTY);

Hmm... I guess EBUSY would fit better here.

I'm using -EBUSY to indicate that other live test is already running, while here we want to say we don't want to run live test on not explicitly specified device.

and I was planning to teach Kunit executor to handle -ENOTTY as indication that we want to SKIP the whole suite (due to missing prerequisites or something)

Quoted 30 lines
Quoted 16 lines
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +15 -0
+
+	drv = driver_find(DRIVER_NAME, &pci_bus_type);
+	dev = driver_find_device(drv, NULL, dut_name,
+				 mutex_is_locked(&dut_mutex) ? match_dev_by_name : match_dev_any);
+	if (!dev)
+		return ERR_PTR(-ENODEV);
+
+	return to_xe_device(dev_get_drvdata(dev));
+}
+
+void put_xe_dut(struct xe_device *xe)
+{
+	put_device(xe->drm.dev);
+}
+

drivers/gpu/drm/xe/selftests/xe_selftests_executor.c line 63

Quoted 10 lines
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +10 -0
+static int run_test_suites(struct xe_device *xe)
+{
+	struct device *dev = xe->drm.dev;
+	struct device_driver *drv = dev->driver;
+	struct module *mod = drv->owner;
+	struct kunit_suite **suites = mod->kunit_suites;
+	int num_suites = mod->num_kunit_suites;
+	int i;
+
+	pr_info("TAP version 14\n");

It doesn't seem nice to hardcode it here. IMO, this should be in sync with lib/kunit/executor.c.

Quoted 5 lines

Maybe the way to go would be to add, at kunit/test.h:

#define TAP_VERSION "TAP version 14\n"

And use such macro on both lib/kunit/executor.c and here.

problem is that upstream Kunit moved from "TAP 14" to "KTAP 1" so I guess while xe is behind, we would still need local definition

Quoted 10 lines

We need to c/c kunit maintainers on such patch.

IMO, a better approach would be to modify lib/kunit/executor.c, adding there a variant of kunit_run_all_tests() to be used with a custom filter list, e. g.:

kunit_run_filtered_tests(struct suite_set *suite_set, (int *)filter_test(struct suite_set *suite_set))

And then calling it at xe_selftests_executor.c.

true, and I've already something similar locally (*), but in this series I decided to go without any external changes to speed up the decision process ;)

Thanks,
Michal

(*) plan was to export (currently available as built-in feature) function to run suites with optional name-based filter:

int kunit_run_suites(struct kunit_suite **suites, int num_suites,

		     const char *filter, bool dry_run);

static inline int kunit_run_module_suites(struct module *mod, const char *filter, bool dry_run) { .. }

static inline int kunit_run_this_module_suites(const char *filter, bool dry_run)

{
	return kunit_run_module_suites(THIS_MODULE, filter, dry_run);
}
Quoted 18 lines
Quoted 5 lines
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +4 -0
+	pr_info("1..%u\n", num_suites);
+
+	/* reset kunit_suite_counter at the start */
+	__kunit_test_suites_exit(NULL, 0);

drivers/gpu/drm/xe/selftests/xe_selftests_executor.c line 77

Quoted 10 lines
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +10 -0
+
+	for (i = 0; i < num_suites; i++) {
+		kunit_run_tests(suites[i]);
+	}
+
+	/* reset kunit_suite_counter at the end */
+	__kunit_test_suites_exit(NULL, 0);
+
+	return num_suites;
+}

Regards,
Mauro

#4 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

In reply to Michal Wajdeczko
Quoted 55 lines · Michal Wajdeczko

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

---

drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++
drivers/gpu/drm/xe/Makefile                   |  11 ++
drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++
.../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++
.../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++
.../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++
.../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++
.../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++
.../drm/xe/selftests/xe_selftests_params.c    |  23 ++++
drivers/gpu/drm/xe/xe_module.c                |   2 +
10 files changed, 412 insertions(+)

create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c

drivers/gpu/drm/xe/Kconfig.debug +3 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE
 
 	  If in doubt, say "N".
 
+config DRM_XE_SELFTESTS_MOCK
+	bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS
+	depends on DRM_XE && KUNIT

drivers/gpu/drm/xe/Kconfig.debug config DRM_XE_SIMPLE_ERROR_CAPTURE · line 76

Quoted 10 lines
drivers/gpu/drm/xe/Kconfig.debug +10 -0
+	default KUNIT_ALL_TESTS
+	help
+		Choose this option to allow the driver to perform mock
+		selftests based on the Kunit framework.
+
+		Recommended for driver developers only.
+		If in doubt, say "N".
+
+config DRM_XE_SELFTESTS_LIVE
+	bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK

why? I think we should aim to run live tests in *addition* to mock ones. The mock tests are more testing our SW infra and can be used in a true "unit test" sense. While the live ones are testing the integration with the HW and making sure they work together.

I have changes pending to test some WA and RTP infra we added, regardless of the HW. But I don't want to have to choose either one or the other.

Also Cc Thomas as original author of the kunit integration.

We also have some overloaded terms that need better explanation:
mock, selftest, live.

Quoted 97 lines
drivers/gpu/drm/xe/Kconfig.debug +9 -0
+	depends on DRM_XE && KUNIT && EXPERT
+	default n
+	help
+		Choose this option to allow the driver to perform live
+		selftests based on the Kunit framework.
+
+		Recommended for driver developers only.
+		If in doubt, say "N".
+

config DRM_XE_KUNIT_TEST
tristate "KUnit tests for the drm xe driver" if !KUNIT_ALL_TESTS depends on DRM_XE && KUNIT && DEBUG_FS

drivers/gpu/drm/xe/Makefile +0 -0
index b94e65c91101..b193af2d9930 100644
@@ -211,6 +211,17 @@ ifeq ($(CONFIG_DRM_FBDEV_EMULATION),y)
 	xe-$(CONFIG_DRM_XE_DISPLAY) += display/intel_fbdev.o

endif

drivers/gpu/drm/xe/Makefile +11 -0
+xe-$(CONFIG_DRM_XE_SELFTESTS_MOCK) += \
+	selftests/xe_selftests_mock.o \
+
+xe-$(CONFIG_DRM_XE_SELFTESTS_LIVE) += \
+	selftests/xe_selftests_auto.o \
+	selftests/xe_selftests_executor.o \
+	selftests/xe_selftests_params.o \
+
+xe-$(CONFIG_KUNIT) += \
+	selftests/xe_selftests_helpers.o \
+

obj-$(CONFIG_DRM_XE) += xe.o
obj-$(CONFIG_DRM_XE_KUNIT_TEST) += tests/

drivers/gpu/drm/xe/selftests/xe_selftests.h new file +35 -0
new file mode 100644
index 000000000000..6b13b998e789
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTESTS_H_
+#define _XE_SELFTESTS_H_
+
+struct kunit_suite;
+struct kunit;
+
+int xe_selftests_run(const char *dev_name);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int init_xe_live_selftest_suite(struct kunit_suite *suite);
+#endif
+
+int init_xe_device_selftest(struct kunit *test);
+void exit_xe_device_selftest(struct kunit *test);
+
+int init_xe_gt_selftest(struct kunit *test);
+void exit_xe_gt_selftest(struct kunit *test);
+
+int init_xe_guc_selftest(struct kunit *test);
+void exit_xe_guc_selftest(struct kunit *test);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int xe_selftests_module_init(void);
+void xe_selftests_module_exit(void);
+#else
+static inline int xe_selftests_module_init(void) { return 0; }
+static inline void xe_selftests_module_exit(void) { }
+#endif
+
+#endif /* _XE_SELFTESTS_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +16 -0
new file mode 100644
index 000000000000..ef98f3599eac
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/device.h>
+#include <linux/glob.h>
+#include <linux/pci.h>
+
+#include "xe_drv.h"
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+static int bus_cb(struct notifier_block *nb, unsigned long action, void *data)
+{
+	struct device *dev = data;

drivers/gpu/drm/xe/selftests/xe_selftests_auto.c line 26

Quoted 10 lines
drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +10 -0
+	struct device_driver *drv = dev->driver;
+
+	if (action != BUS_NOTIFY_BOUND_DRIVER)
+		return 0;
+
+	if (strcmp(DRIVER_NAME, drv->name))
+		return 0;
+
+	/* only matching DUT names will be tested */
+	if (!dut_filter || !glob_match(dut_filter, dev_name(dev)))

"DUT" name could be avoided and just use what exactly it means. When I see "DUT" I'm wondering... is this the platform name, the pci ID, the pci slot, the device node?

Lucas De Marchi

#5 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

Quoted 63 lines · Lucas De Marchi
Quoted 62 lines · Michal Wajdeczko

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

---

drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++
drivers/gpu/drm/xe/Makefile                   |  11 ++
drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++
.../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++
.../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++
.../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++
.../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++
.../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++
.../drm/xe/selftests/xe_selftests_params.c    |  23 ++++
drivers/gpu/drm/xe/xe_module.c                |   2 +
10 files changed, 412 insertions(+)

create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c

drivers/gpu/drm/xe/Kconfig.debug +10 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE
 
 	  If in doubt, say "N".
 
+config DRM_XE_SELFTESTS_MOCK
+	bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS
+	depends on DRM_XE && KUNIT
+	default KUNIT_ALL_TESTS
+	help
+		Choose this option to allow the driver to perform mock
+		selftests based on the Kunit framework.
+
+		Recommended for driver developers only.
+		If in doubt, say "N".
Quoted 8 lines

drivers/gpu/drm/xe/Kconfig.debug config DRM_XE_SIMPLE_ERROR_CAPTURE · line 76

Quoted 3 lines
drivers/gpu/drm/xe/Kconfig.debug +3 -0
+
+config DRM_XE_SELFTESTS_LIVE
+	bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK

why? I think we should aim to run live tests in *addition* to mock ones. The mock tests are more testing our SW infra and can be used in a true "unit test" sense. While the live ones are testing the integration with the HW and making sure they work together.

on a similar note, I'd avoid grouping tests as "mock". Any test runnable under UML is a mock test and is testing the SW behavior. From a kunit perspective I think it's the live ones, interacting with the real hardware that are the exception and can be grouped as "live" tests.

Lucas De Marchi

#6 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

In reply to Lucas De Marchi
Quoted 64 lines · Lucas De Marchi
Quoted 63 lines · Michal Wajdeczko

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

---
drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++ drivers/gpu/drm/xe/Makefile                   |  11 ++ drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++ .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++ .../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++ .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++ .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++ .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++ .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++ drivers/gpu/drm/xe/xe_module.c                |   2 +

10 files changed, 412 insertions(+)

create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c

drivers/gpu/drm/xe/Kconfig.debug +0 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE
 

      If in doubt, say "N".

drivers/gpu/drm/xe/Kconfig.debug +10 -0
+config DRM_XE_SELFTESTS_MOCK
+    bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS
+    depends on DRM_XE && KUNIT
+    default KUNIT_ALL_TESTS
+    help
+        Choose this option to allow the driver to perform mock
+        selftests based on the Kunit framework.
+
+        Recommended for driver developers only.
+        If in doubt, say "N".
Quoted 8 lines

drivers/gpu/drm/xe/Kconfig.debug config DRM_XE_SIMPLE_ERROR_CAPTURE · line 76

Quoted 3 lines
drivers/gpu/drm/xe/Kconfig.debug +3 -0
+
+config DRM_XE_SELFTESTS_LIVE
+    bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK

why?  I think we should aim to run live tests in *addition* to mock ones.  The mock tests are more testing our SW infra and can be used in a true "unit test" sense. While the live ones are testing the integration with the HW and making sure they work together.

I've added that just to simplify the code logic as I assumed we will be running mock tests only in "true kunit" fashion as part of the build.

So then we could avoid mixing mock/live "kunit suites" in single driver module, especially that as of today there is no nice support to filter out undesired suites from the execution. Thus adding compile time flag to only have one category of selftests at time was looking promising.

So I was using .kunitconfig with
CONFIG_DRM_XE_SELFTESTS_MOCK=y
and normal .config with
CONFIG_DRM_XE_SELFTESTS_LIVE=y

Quoted 4 lines

I have changes pending to test some WA and RTP infra we added, regardless of the HW. But I don't want to have to choose either one or the other.

And I guess you still can do that, just it wont be automatically compiled out, nor you be able to benefit from the helpers, but they will be executed

Quoted 5 lines

Also Cc Thomas as original author of the kunit integration.

We also have some overloaded terms that need better explanation:
mock, selftest, live.

I reused terms from i915, assumed everyone is familiar with that.

"mock selftest" - a kunit test suite to be run on abstract xe device "live selftest" - a kunit test suite to be executed on real xe device

If you have better suggestions, then you're welcomed

Quoted 107 lines
Quoted 107 lines

+    depends on DRM_XE && KUNIT && EXPERT
+    default n
+    help
+        Choose this option to allow the driver to perform live +        selftests based on the Kunit framework.

drivers/gpu/drm/xe/Kconfig.debug +4 -0
+
+        Recommended for driver developers only.
+        If in doubt, say "N".
+

config DRM_XE_KUNIT_TEST
        tristate "KUnit tests for the drm xe driver" if !KUNIT_ALL_TESTS depends on DRM_XE && KUNIT && DEBUG_FS

drivers/gpu/drm/xe/Makefile +0 -0
index b94e65c91101..b193af2d9930 100644
@@ -211,6 +211,17 @@ ifeq ($(CONFIG_DRM_FBDEV_EMULATION),y)

    xe-$(CONFIG_DRM_XE_DISPLAY) += display/intel_fbdev.o endif

drivers/gpu/drm/xe/Makefile +11 -0
+xe-$(CONFIG_DRM_XE_SELFTESTS_MOCK) += \
+    selftests/xe_selftests_mock.o \
+
+xe-$(CONFIG_DRM_XE_SELFTESTS_LIVE) += \
+    selftests/xe_selftests_auto.o \
+    selftests/xe_selftests_executor.o \
+    selftests/xe_selftests_params.o \
+
+xe-$(CONFIG_KUNIT) += \
+    selftests/xe_selftests_helpers.o \
+

obj-$(CONFIG_DRM_XE) += xe.o
obj-$(CONFIG_DRM_XE_KUNIT_TEST) += tests/

drivers/gpu/drm/xe/selftests/xe_selftests.h new file +35 -0
new file mode 100644
index 000000000000..6b13b998e789
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTESTS_H_
+#define _XE_SELFTESTS_H_
+
+struct kunit_suite;
+struct kunit;
+
+int xe_selftests_run(const char *dev_name);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int init_xe_live_selftest_suite(struct kunit_suite *suite);
+#endif
+
+int init_xe_device_selftest(struct kunit *test);
+void exit_xe_device_selftest(struct kunit *test);
+
+int init_xe_gt_selftest(struct kunit *test);
+void exit_xe_gt_selftest(struct kunit *test);
+
+int init_xe_guc_selftest(struct kunit *test);
+void exit_xe_guc_selftest(struct kunit *test);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int xe_selftests_module_init(void);
+void xe_selftests_module_exit(void);
+#else
+static inline int xe_selftests_module_init(void) { return 0; }
+static inline void xe_selftests_module_exit(void) { }
+#endif
+
+#endif /* _XE_SELFTESTS_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +14 -0
new file mode 100644
index 000000000000..ef98f3599eac
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/device.h>
+#include <linux/glob.h>
+#include <linux/pci.h>
+
+#include "xe_drv.h"
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+static int bus_cb(struct notifier_block *nb, unsigned long action,

void *data)

drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +8 -0
+{
+    struct device *dev = data;
+    struct device_driver *drv = dev->driver;
+
+    if (action != BUS_NOTIFY_BOUND_DRIVER)
+        return 0;
+
+    if (strcmp(DRIVER_NAME, drv->name))
Quoted 8 lines
Quoted 4 lines

+        return 0;
+
+    /* only matching DUT names will be tested */
+    if (!dut_filter || !glob_match(dut_filter, dev_name(dev)))

"DUT" name could be avoided and just use what exactly it means. When I see "DUT" I'm wondering... is this the platform name, the pci ID, the pci slot, the device node?

will rename to "dut_name_filter" do the trick ?

or do you want to drop "dut" completely and have "device_name_filter" ? but then we would need to add "selftest" or similar word somewhere to give context and then full modparam name will no longer be that friendly

Thanks,
Michal

#7 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

Quoted 76 lines · Michal Wajdeczko
Quoted 64 lines · Lucas De Marchi
Quoted 63 lines · Michal Wajdeczko

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

---
drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++ drivers/gpu/drm/xe/Makefile                   |  11 ++ drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++ .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++ .../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++ .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++ .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++ .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++ .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++ drivers/gpu/drm/xe/xe_module.c                |   2 +

10 files changed, 412 insertions(+)

create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c

drivers/gpu/drm/xe/Kconfig.debug +0 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE
 

      If in doubt, say "N".

drivers/gpu/drm/xe/Kconfig.debug +10 -0
+config DRM_XE_SELFTESTS_MOCK
+    bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS
+    depends on DRM_XE && KUNIT
+    default KUNIT_ALL_TESTS
+    help
+        Choose this option to allow the driver to perform mock
+        selftests based on the Kunit framework.
+
+        Recommended for driver developers only.
+        If in doubt, say "N".
Quoted 8 lines

drivers/gpu/drm/xe/Kconfig.debug config DRM_XE_SIMPLE_ERROR_CAPTURE · line 76

Quoted 3 lines
drivers/gpu/drm/xe/Kconfig.debug +3 -0
+
+config DRM_XE_SELFTESTS_LIVE
+    bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK

why?  I think we should aim to run live tests in *addition* to mock ones.  The mock tests are more testing our SW infra and can be used in a true "unit test" sense. While the live ones are testing the integration with the HW and making sure they work together.

Quoted 7 lines

I've added that just to simplify the code logic as I assumed we will be running mock tests only in "true kunit" fashion as part of the build.

So then we could avoid mixing mock/live "kunit suites" in single driver module, especially that as of today there is no nice support to filter out undesired suites from the execution. Thus adding compile time flag to only have one category of selftests at time was looking promising.

I'm using this in the patches I'm sending to unit test rtp and wa parts:

	./tools/testing/kunit/kunit.py run --raw_output  \
                --kunitconfig drivers/gpu/drm/xe/.kunitconfig xe_rtp
Quoted 18 lines

So I was using .kunitconfig with
CONFIG_DRM_XE_SELFTESTS_MOCK=y
and normal .config with
CONFIG_DRM_XE_SELFTESTS_LIVE=y

Quoted 4 lines

I have changes pending to test some WA and RTP infra we added, regardless of the HW. But I don't want to have to choose either one or the other.

And I guess you still can do that, just it wont be automatically compiled out, nor you be able to benefit from the helpers, but they will be executed

Quoted 3 lines

Also Cc Thomas as original author of the kunit integration.

Quoted 7 lines
Quoted 2 lines

We also have some overloaded terms that need better explanation:
mock, selftest, live.

I reused terms from i915, assumed everyone is familiar with that.

"mock selftest" - a kunit test suite to be run on abstract xe device "live selftest" - a kunit test suite to be executed on real xe device

Things I like on the current kunit integration we have in xe:

1) The ability to move unit test code out to a separate module
2) Not having a big infra that is xe-only
3) Share terminology with the rest of drm subsystem

This patch series is basically killing (1) and (3). And for (2) it seems to be heading another direction. Looking at the current available drivers, I think we should be more like the drm tests and the vc4 tests rather than the i915 ones. If we need more helpers we may probably extend the ones in DRM_KUNIT_TEST_HELPERS

I'd love to hear others' opinions though.

Lucas De Marchi

Quoted 127 lines

If you have better suggestions, then you're welcomed

Quoted 107 lines
Quoted 107 lines

+    depends on DRM_XE && KUNIT && EXPERT
+    default n
+    help
+        Choose this option to allow the driver to perform live +        selftests based on the Kunit framework.

drivers/gpu/drm/xe/Kconfig.debug +4 -0
+
+        Recommended for driver developers only.
+        If in doubt, say "N".
+

config DRM_XE_KUNIT_TEST
        tristate "KUnit tests for the drm xe driver" if !KUNIT_ALL_TESTS depends on DRM_XE && KUNIT && DEBUG_FS

drivers/gpu/drm/xe/Makefile +0 -0
index b94e65c91101..b193af2d9930 100644
@@ -211,6 +211,17 @@ ifeq ($(CONFIG_DRM_FBDEV_EMULATION),y)

    xe-$(CONFIG_DRM_XE_DISPLAY) += display/intel_fbdev.o endif

drivers/gpu/drm/xe/Makefile +11 -0
+xe-$(CONFIG_DRM_XE_SELFTESTS_MOCK) += \
+    selftests/xe_selftests_mock.o \
+
+xe-$(CONFIG_DRM_XE_SELFTESTS_LIVE) += \
+    selftests/xe_selftests_auto.o \
+    selftests/xe_selftests_executor.o \
+    selftests/xe_selftests_params.o \
+
+xe-$(CONFIG_KUNIT) += \
+    selftests/xe_selftests_helpers.o \
+

obj-$(CONFIG_DRM_XE) += xe.o
obj-$(CONFIG_DRM_XE_KUNIT_TEST) += tests/

drivers/gpu/drm/xe/selftests/xe_selftests.h new file +35 -0
new file mode 100644
index 000000000000..6b13b998e789
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTESTS_H_
+#define _XE_SELFTESTS_H_
+
+struct kunit_suite;
+struct kunit;
+
+int xe_selftests_run(const char *dev_name);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int init_xe_live_selftest_suite(struct kunit_suite *suite);
+#endif
+
+int init_xe_device_selftest(struct kunit *test);
+void exit_xe_device_selftest(struct kunit *test);
+
+int init_xe_gt_selftest(struct kunit *test);
+void exit_xe_gt_selftest(struct kunit *test);
+
+int init_xe_guc_selftest(struct kunit *test);
+void exit_xe_guc_selftest(struct kunit *test);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int xe_selftests_module_init(void);
+void xe_selftests_module_exit(void);
+#else
+static inline int xe_selftests_module_init(void) { return 0; }
+static inline void xe_selftests_module_exit(void) { }
+#endif
+
+#endif /* _XE_SELFTESTS_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +14 -0
new file mode 100644
index 000000000000..ef98f3599eac
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/device.h>
+#include <linux/glob.h>
+#include <linux/pci.h>
+
+#include "xe_drv.h"
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+static int bus_cb(struct notifier_block *nb, unsigned long action,

void *data)

drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +8 -0
+{
+    struct device *dev = data;
+    struct device_driver *drv = dev->driver;
+
+    if (action != BUS_NOTIFY_BOUND_DRIVER)
+        return 0;
+
+    if (strcmp(DRIVER_NAME, drv->name))
Quoted 8 lines
Quoted 4 lines

+        return 0;
+
+    /* only matching DUT names will be tested */
+    if (!dut_filter || !glob_match(dut_filter, dev_name(dev)))

"DUT" name could be avoided and just use what exactly it means. When I see "DUT" I'm wondering... is this the platform name, the pci ID, the pci slot, the device node?

will rename to "dut_name_filter" do the trick ?

or do you want to drop "dut" completely and have "device_name_filter" ? but then we would need to add "selftest" or similar word somewhere to give context and then full modparam name will no longer be that friendly

Thanks,
Michal

#8 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

Hi,

Quoted 122 lines · Lucas De Marchi
Quoted 76 lines · Michal Wajdeczko
Quoted 64 lines · Lucas De Marchi
Quoted 63 lines · Michal Wajdeczko

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

---
drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++ drivers/gpu/drm/xe/Makefile                   |  11 ++ drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++ .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++ .../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++ .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++ .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++ .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++ .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++ drivers/gpu/drm/xe/xe_module.c                |   2 +

10 files changed, 412 insertions(+)

create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c

drivers/gpu/drm/xe/Kconfig.debug +0 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE
 

      If in doubt, say "N".

drivers/gpu/drm/xe/Kconfig.debug +10 -0
+config DRM_XE_SELFTESTS_MOCK
+    bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS
+    depends on DRM_XE && KUNIT
+    default KUNIT_ALL_TESTS
+    help
+        Choose this option to allow the driver to perform mock
+        selftests based on the Kunit framework.
+
+        Recommended for driver developers only.
+        If in doubt, say "N".
Quoted 8 lines

drivers/gpu/drm/xe/Kconfig.debug config DRM_XE_SIMPLE_ERROR_CAPTURE · line 76

Quoted 3 lines
drivers/gpu/drm/xe/Kconfig.debug +3 -0
+
+config DRM_XE_SELFTESTS_LIVE
+    bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK

why?  I think we should aim to run live tests in *addition* to mock ones.  The mock tests are more testing our SW infra and can be used in a true "unit test" sense. While the live ones are testing the integration with the HW and making sure they work together.

Quoted 7 lines

I've added that just to simplify the code logic as I assumed we will be running mock tests only in "true kunit" fashion as part of the build.

So then we could avoid mixing mock/live "kunit suites" in single driver module, especially that as of today there is no nice support to filter out undesired suites from the execution. Thus adding compile time flag to only have one category of selftests at time was looking promising.

I'm using this in the patches I'm sending to unit test rtp and wa parts:

    ./tools/testing/kunit/kunit.py run --raw_output  \
                --kunitconfig drivers/gpu/drm/xe/.kunitconfig xe_rtp

Quoted 18 lines

So I was using .kunitconfig with
    CONFIG_DRM_XE_SELFTESTS_MOCK=y
and normal .config with
    CONFIG_DRM_XE_SELFTESTS_LIVE=y

Quoted 4 lines

I have changes pending to test some WA and RTP infra we added, regardless of the HW. But I don't want to have to choose either one or the other.

And I guess you still can do that, just it wont be automatically compiled out, nor you be able to benefit from the helpers, but they will be executed

Quoted 3 lines

Also Cc Thomas as original author of the kunit integration.

Quoted 7 lines
Quoted 2 lines

We also have some overloaded terms that need better explanation:
mock, selftest, live.

I reused terms from i915, assumed everyone is familiar with that.

"mock selftest" - a kunit test suite to be run on abstract xe device "live selftest" - a kunit test suite to be executed on real xe device

Things I like on the current kunit integration we have in xe:

    1) The ability to move unit test code out to a separate module
    2) Not having a big infra that is xe-only
    3) Share terminology with the rest of drm subsystem

Quoted 7 lines

This patch series is basically killing (1) and (3). And for (2) it seems to be heading another direction.  Looking at the current available drivers,  I think we should be more like the drm tests and the vc4 tests rather than the i915 ones.  If we need more helpers we may probably extend the ones in DRM_KUNIT_TEST_HELPERS

I'd love to hear others' opinions though.

I basically agree with Lucas here. Some points:

* For mock selftests IMO we don't need anything on top of what we already have, except perhaps consolidate initialization of a mock device as a helper each test can optionally use.  I think, though, that Mauro's suggestion of coalescing multiple mock tests together in a single separate kernel module with multiple test suites to avoid too many separate kernel module might be worth considering if needed.
* For live selftests it seems like where the big disconnect is. I still believe that we should have separate modules, and depending on how the IGT runner end up working have enough information in that module for IGT to execute the tests with the --run-subtest and --dynamic-subtest options.
* Admiddedly the xe_for_each_device() helper is very limited, but couldn't we just make a more elaborate helper that can add functionality like filter out devices if needed? I'm thinking if we enforce a middle layer that tells each test what device it should be testing, things like testing PCI p2p dma between devices will become awkward (but still possible I guess).
* Do we really need to support compiled-in live selftests that run a device on device binding? I guess the use-case isn't fully clear to me.
* And yes the test parameters shouldn't be needing the test->priv, but neither should we need to pass the device here, right?

/Thomas

Quoted 130 lines

Lucas De Marchi

Quoted 127 lines

If you have better suggestions, then you're welcomed

Quoted 107 lines
Quoted 107 lines

+    depends on DRM_XE && KUNIT && EXPERT
+    default n
+    help
+        Choose this option to allow the driver to perform live +        selftests based on the Kunit framework.

drivers/gpu/drm/xe/Kconfig.debug +4 -0
+
+        Recommended for driver developers only.
+        If in doubt, say "N".
+

config DRM_XE_KUNIT_TEST
        tristate "KUnit tests for the drm xe driver" if !KUNIT_ALL_TESTS depends on DRM_XE && KUNIT && DEBUG_FS

drivers/gpu/drm/xe/Makefile +0 -0
index b94e65c91101..b193af2d9930 100644
@@ -211,6 +211,17 @@ ifeq ($(CONFIG_DRM_FBDEV_EMULATION),y)

    xe-$(CONFIG_DRM_XE_DISPLAY) += display/intel_fbdev.o endif

drivers/gpu/drm/xe/Makefile +11 -0
+xe-$(CONFIG_DRM_XE_SELFTESTS_MOCK) += \
+    selftests/xe_selftests_mock.o \
+
+xe-$(CONFIG_DRM_XE_SELFTESTS_LIVE) += \
+    selftests/xe_selftests_auto.o \
+    selftests/xe_selftests_executor.o \
+    selftests/xe_selftests_params.o \
+
+xe-$(CONFIG_KUNIT) += \
+    selftests/xe_selftests_helpers.o \
+

obj-$(CONFIG_DRM_XE) += xe.o
obj-$(CONFIG_DRM_XE_KUNIT_TEST) += tests/

drivers/gpu/drm/xe/selftests/xe_selftests.h new file +35 -0
new file mode 100644
index 000000000000..6b13b998e789
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTESTS_H_
+#define _XE_SELFTESTS_H_
+
+struct kunit_suite;
+struct kunit;
+
+int xe_selftests_run(const char *dev_name);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int init_xe_live_selftest_suite(struct kunit_suite *suite);
+#endif
+
+int init_xe_device_selftest(struct kunit *test);
+void exit_xe_device_selftest(struct kunit *test);
+
+int init_xe_gt_selftest(struct kunit *test);
+void exit_xe_gt_selftest(struct kunit *test);
+
+int init_xe_guc_selftest(struct kunit *test);
+void exit_xe_guc_selftest(struct kunit *test);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int xe_selftests_module_init(void);
+void xe_selftests_module_exit(void);
+#else
+static inline int xe_selftests_module_init(void) { return 0; }
+static inline void xe_selftests_module_exit(void) { }
+#endif
+
+#endif /* _XE_SELFTESTS_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +14 -0
new file mode 100644
index 000000000000..ef98f3599eac
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/device.h>
+#include <linux/glob.h>
+#include <linux/pci.h>
+
+#include "xe_drv.h"
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+static int bus_cb(struct notifier_block *nb, unsigned long action,

void *data)

drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +8 -0
+{
+    struct device *dev = data;
+    struct device_driver *drv = dev->driver;
+
+    if (action != BUS_NOTIFY_BOUND_DRIVER)
+        return 0;
+
+    if (strcmp(DRIVER_NAME, drv->name))
Quoted 8 lines
Quoted 4 lines

+        return 0;
+
+    /* only matching DUT names will be tested */
+    if (!dut_filter || !glob_match(dut_filter, dev_name(dev)))

"DUT" name could be avoided and just use what exactly it means. When I see "DUT" I'm wondering... is this the platform name, the pci ID, the pci slot, the device node?

will rename to "dut_name_filter" do the trick ?

or do you want to drop "dut" completely and have "device_name_filter" ? but then we would need to add "selftest" or similar word somewhere to give context and then full modparam name will no longer be that friendly

Thanks,
Michal

#9 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

Quoted 138 lines · On Fri, 17 Mar 2023 08:20:46 +0100 Thomas Hellström…

Hi,

Quoted 125 lines · Lucas De Marchi
Quoted 79 lines · Michal Wajdeczko
Quoted 67 lines · Lucas De Marchi
Quoted 66 lines · Michal Wajdeczko

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

---
drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++ drivers/gpu/drm/xe/Makefile                   |  11 ++ drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++ .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++ .../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++ .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++ .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++ .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++ .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++ drivers/gpu/drm/xe/xe_module.c                |   2 +

10 files changed, 412 insertions(+)

create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c
create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h
create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c

drivers/gpu/drm/xe/Kconfig.debug +0 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE
 

      If in doubt, say "N".

drivers/gpu/drm/xe/Kconfig.debug +11 -0
+config DRM_XE_SELFTESTS_MOCK
+    bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS
+    depends on DRM_XE && KUNIT
+    default KUNIT_ALL_TESTS
+    help
+        Choose this option to allow the driver to perform mock
+        selftests based on the Kunit framework.
+
+        Recommended for driver developers only.
+        If in doubt, say "N".
+
Quoted 8 lines
Quoted 2 lines

+config DRM_XE_SELFTESTS_LIVE
+    bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK

why?  I think we should aim to run live tests in *addition* to mock ones.  The mock tests are more testing our SW infra and can be used
in a true "unit test" sense. While the live ones are testing the integration with the HW and making sure they work together.

Quoted 7 lines

I've added that just to simplify the code logic as I assumed we will be running mock tests only in "true kunit" fashion as part of the build.

So then we could avoid mixing mock/live "kunit suites" in single driver module, especially that as of today there is no nice support to filter out undesired suites from the execution. Thus adding compile time flag to only have one category of selftests at time was looking promising.

I'm using this in the patches I'm sending to unit test rtp and wa parts:

    ./tools/testing/kunit/kunit.py run --raw_output  \
                --kunitconfig drivers/gpu/drm/xe/.kunitconfig xe_rtp

Quoted 18 lines

So I was using .kunitconfig with
    CONFIG_DRM_XE_SELFTESTS_MOCK=y
and normal .config with
    CONFIG_DRM_XE_SELFTESTS_LIVE=y

Quoted 4 lines

I have changes pending to test some WA and RTP infra we added, regardless of the HW. But I don't want to have to choose either one or the other.

And I guess you still can do that, just it wont be automatically compiled out, nor you be able to benefit from the helpers, but they will be executed

Quoted 3 lines

Also Cc Thomas as original author of the kunit integration.

Quoted 7 lines
Quoted 2 lines

We also have some overloaded terms that need better explanation:
mock, selftest, live.

I reused terms from i915, assumed everyone is familiar with that.

"mock selftest" - a kunit test suite to be run on abstract xe device "live selftest" - a kunit test suite to be executed on real xe device

Things I like on the current kunit integration we have in xe:

    1) The ability to move unit test code out to a separate module
    2) Not having a big infra that is xe-only
    3) Share terminology with the rest of drm subsystem

Quoted 7 lines

This patch series is basically killing (1) and (3). And for (2) it seems to be heading another direction.  Looking at the current available drivers,  I think we should be more like the drm tests and the vc4 tests rather than the i915 ones.  If we need more helpers we may probably extend the ones in DRM_KUNIT_TEST_HELPERS

I'd love to hear others' opinions though.

I basically agree with Lucas here. Some points:

Quoted 7 lines

* For mock selftests IMO we don't need anything on top of what we already have, except perhaps consolidate initialization of a mock device as a helper each test can optionally use.  I think, though, that Mauro's suggestion of coalescing multiple mock tests together in a single separate kernel module with multiple test suites to avoid too many separate kernel module might be worth considering if needed.

IMO there's one important feature here that it is missing upstream:
support for filtering out KUnit tests.

Yet, the patch is adding it inside Xe. The right solution is, IMO, to add support as much as possible inside the KUnit core and having additional variables inside the module(s) implementing KUnit to setup such filters.

This is more important for live selftests, but having this also for mock won't hurt

Quoted 5 lines

* For live selftests it seems like where the big disconnect is. I still believe that we should have separate modules, and depending on how the IGT runner end up working have enough information in that module for IGT to execute the tests with the --run-subtest and --dynamic-subtest options.

KUnit patch series is currently in rev. 5. It sounds almost ready for being merged. In any case, to properly support dynamic subtests, we need filtering capabilities. Once the filters get added, having multiple test suites on the same module or on different modules won't make any difference for IGT.

Regards,
Mauro

#10 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

Quoted 152 lines · Mauro Carvalho Chehab
Quoted 138 lines · On Fri, 17 Mar 2023 08:20:46 +0100 Thomas Hellström…

Hi,

Quoted 125 lines · Lucas De Marchi
Quoted 79 lines · Michal Wajdeczko
Quoted 67 lines · Lucas De Marchi
Quoted 66 lines · Michal Wajdeczko

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

---
drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++ drivers/gpu/drm/xe/Makefile                   |  11 ++ drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++ .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++ .../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++ .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++ .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++ .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++ .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++ drivers/gpu/drm/xe/xe_module.c                |   2 +

10 files changed, 412 insertions(+)

create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c
create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h
create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c

drivers/gpu/drm/xe/Kconfig.debug +0 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE
 

      If in doubt, say "N".

drivers/gpu/drm/xe/Kconfig.debug +11 -0
+config DRM_XE_SELFTESTS_MOCK
+    bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS
+    depends on DRM_XE && KUNIT
+    default KUNIT_ALL_TESTS
+    help
+        Choose this option to allow the driver to perform mock
+        selftests based on the Kunit framework.
+
+        Recommended for driver developers only.
+        If in doubt, say "N".
+
Quoted 8 lines
Quoted 2 lines

+config DRM_XE_SELFTESTS_LIVE
+    bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK

why?  I think we should aim to run live tests in *addition* to mock ones.  The mock tests are more testing our SW infra and can be used
in a true "unit test" sense. While the live ones are testing the integration with the HW and making sure they work together.

Quoted 7 lines

I've added that just to simplify the code logic as I assumed we will be running mock tests only in "true kunit" fashion as part of the build.

So then we could avoid mixing mock/live "kunit suites" in single driver module, especially that as of today there is no nice support to filter out undesired suites from the execution. Thus adding compile time flag to only have one category of selftests at time was looking promising.

I'm using this in the patches I'm sending to unit test rtp and wa parts:

    ./tools/testing/kunit/kunit.py run --raw_output  \
                --kunitconfig drivers/gpu/drm/xe/.kunitconfig xe_rtp

Quoted 18 lines

So I was using .kunitconfig with
    CONFIG_DRM_XE_SELFTESTS_MOCK=y
and normal .config with
    CONFIG_DRM_XE_SELFTESTS_LIVE=y

Quoted 4 lines

I have changes pending to test some WA and RTP infra we added, regardless of the HW. But I don't want to have to choose either one or the other.

And I guess you still can do that, just it wont be automatically compiled out, nor you be able to benefit from the helpers, but they will be executed

Quoted 3 lines

Also Cc Thomas as original author of the kunit integration.

Quoted 7 lines
Quoted 2 lines

We also have some overloaded terms that need better explanation:
mock, selftest, live.

I reused terms from i915, assumed everyone is familiar with that.

"mock selftest" - a kunit test suite to be run on abstract xe device "live selftest" - a kunit test suite to be executed on real xe device

Things I like on the current kunit integration we have in xe:

    1) The ability to move unit test code out to a separate module
    2) Not having a big infra that is xe-only
    3) Share terminology with the rest of drm subsystem

Quoted 7 lines

This patch series is basically killing (1) and (3). And for (2) it seems to be heading another direction.  Looking at the current available drivers,  I think we should be more like the drm tests and the vc4 tests rather than the i915 ones.  If we need more helpers we may probably extend the ones in DRM_KUNIT_TEST_HELPERS

I'd love to hear others' opinions though.

I basically agree with Lucas here. Some points:

Quoted 7 lines

* For mock selftests IMO we don't need anything on top of what we already have, except perhaps consolidate initialization of a mock device as a helper each test can optionally use.  I think, though, that Mauro's suggestion of coalescing multiple mock tests together in a single separate kernel module with multiple test suites to avoid too many separate kernel module might be worth considering if needed.

IMO there's one important feature here that it is missing upstream:
support for filtering out KUnit tests.

Quoted 7 lines

Yet, the patch is adding it inside Xe. The right solution is, IMO, to add support as much as possible inside the KUnit core and having additional variables inside the module(s) implementing KUnit to setup such filters.

This is more important for live selftests, but having this also for mock won't hurt

I'm always filtering the tests I run with:

	./tools/testing/kunit/kunit.py run  \
		--kunitconfig drivers/gpu/drm/xe/.kunitconfig \
		xe_rtp

so it only runs the xe_rtp tests I added. What am I missing? Missing the filter for the device, I'd agree we'd need to add, but I don't understand what support for filtering kunit tests you 2 are talking about. From the script's help:

positional arguments:
filter_glob Filter which KUnit test suites/tests run
at boot-time, e.g. list* or list*.*del_test

there is also this that might be useful:

--run_isolated {suite,test} If set, boot the kernel for each
individual suite/test. This is can be useful for debugging a on-hermetic test, one that might pass/fail based on what ran before it.

... particularly if we integrate running the kunit tests, even for "live" tests under qemu. That has a higher setup cost, but could save us some headache with tests crashing the kernel.

linux-modules recently started using kdevops
(https://github.com/mcgrof/kdevops) for testing. I didn't have time yet to give it a deeper look yet, but it's something we could think about. Adding Luis here. Maybe he has more input on this. Or a demo recorded he can point to.

thanks
Lucas De Marchi

Quoted 15 lines
Quoted 6 lines

* For live selftests it seems like where the big disconnect is. I still believe that we should have separate modules, and depending on how the IGT runner end up working have enough information in that module for IGT to execute the tests with the --run-subtest and --dynamic-subtest options.

KUnit patch series is currently in rev. 5. It sounds almost ready for being merged. In any case, to properly support dynamic subtests, we need filtering capabilities. Once the filters get added, having multiple test suites on the same module or on different modules won't make any difference for IGT.

Regards,
Mauro

#11 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

Quoted 5 lines · Lucas De Marchi

linux-modules recently started using kdevops
(https://github.com/mcgrof/kdevops) for testing. I didn't have time yet to give it a deeper look yet, but it's something we could think about. Adding Luis here. Maybe he has more input on this. Or a demo recorded he can point to.

I have live demo scheduled next week March 29 at 9am PST. It will cover mostly what is *already* supported, but I can give hints as to how to extend things further. Although initial support for kernel selftests is present, adding kunit tests is pretty trivial.

PCI-passthrough is also supported, it generates dynamic kconfig content based on your target system. Ie, it will scrape your lspci output and then convert things into kconfig like language so you can pick and choose what you want to PCI-passthrough onto a guest. So if you wanted to test kunit-live and optionally kunit-mock both could be done. I'd just make each a new make target.

make kunit-drm-mock-xe
make kunit-drm-live-xe

For instance could be targets.

The PCI passthrough documentation:

https://github.com/mcgrof/kdevops/blob/master/docs/libvirt-pcie-passthrough.md

If anyone wants to attend next week's demo let me know. Since it will be focused on what we support already it will cover mostly fstests, blktests, selftests, and CXL support.

Luis

#12 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

In reply to Michal Wajdeczko

Hi, Michal,

Some comments below. I don't have time for an in-depth review today, but As a general comment, I don't think we should have multiple infrastructures for live Kunit selftests, so if we're adopting this, we should port the existing ones over.

Quoted 7 lines · Michal Wajdeczko

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

Could this be passed directly as a parameter instead? I think the test->priv parameter is intended for and very useful to pass parameters used by code to provoke test-specific behaviour (see for example our dma-buf kunit test). That would BTW be a good live test to port over to see if it could be made to work with this new framework.

Quoted 6 lines

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

Quoted 6 lines

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

What would it take with this infrastructure to be able to run both mock- and live without recompiling on an already loaded module? I think that's very convenient.

Quoted 5 lines

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

Thanks,
thomas

Quoted 539 lines

---
 drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++ drivers/gpu/drm/xe/Makefile                   |  11 ++ drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++ .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++ .../drm/xe/selftests/xe_selftests_executor.c  | 118
++++++++++++++++++
 .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++ .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++ .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++ .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++ drivers/gpu/drm/xe/xe_module.c                |   2 +

 10 files changed, 412 insertions(+)

 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c

drivers/gpu/drm/xe/Kconfig.debug +0 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE


          If in doubt, say "N".

+config DRM_XE_SELFTESTS_MOCK
+       bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS +       depends on DRM_XE && KUNIT
+       default KUNIT_ALL_TESTS
+       help
+               Choose this option to allow the driver to perform mock
+               selftests based on the Kunit framework.

drivers/gpu/drm/xe/Kconfig.debug +10 -0
+
+               Recommended for driver developers only.
+               If in doubt, say "N".
+
+config DRM_XE_SELFTESTS_LIVE
+       bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK
+       depends on DRM_XE && KUNIT && EXPERT
+       default n
+       help
+               Choose this option to allow the driver to perform

live
+               selftests based on the Kunit framework.

drivers/gpu/drm/xe/Kconfig.debug +4 -0
+
+               Recommended for driver developers only.
+               If in doubt, say "N".
+

 config DRM_XE_KUNIT_TEST
         tristate "KUnit tests for the drm xe driver" if !KUNIT_ALL_TESTS
        depends on DRM_XE && KUNIT && DEBUG_FS

drivers/gpu/drm/xe/Makefile +0 -0
index b94e65c91101..b193af2d9930 100644
@@ -211,6 +211,17 @@ ifeq ($(CONFIG_DRM_FBDEV_EMULATION),y)

        xe-$(CONFIG_DRM_XE_DISPLAY) += display/intel_fbdev.o endif

drivers/gpu/drm/xe/Makefile +11 -0
+xe-$(CONFIG_DRM_XE_SELFTESTS_MOCK) += \
+       selftests/xe_selftests_mock.o \
+
+xe-$(CONFIG_DRM_XE_SELFTESTS_LIVE) += \
+       selftests/xe_selftests_auto.o \
+       selftests/xe_selftests_executor.o \
+       selftests/xe_selftests_params.o \
+
+xe-$(CONFIG_KUNIT) += \
+       selftests/xe_selftests_helpers.o \
+

 obj-$(CONFIG_DRM_XE) += xe.o
 obj-$(CONFIG_DRM_XE_KUNIT_TEST) += tests/

drivers/gpu/drm/xe/selftests/xe_selftests.h new file +35 -0
new file mode 100644
index 000000000000..6b13b998e789
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTESTS_H_
+#define _XE_SELFTESTS_H_
+
+struct kunit_suite;
+struct kunit;
+
+int xe_selftests_run(const char *dev_name);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int init_xe_live_selftest_suite(struct kunit_suite *suite);
+#endif
+
+int init_xe_device_selftest(struct kunit *test);
+void exit_xe_device_selftest(struct kunit *test);
+
+int init_xe_gt_selftest(struct kunit *test);
+void exit_xe_gt_selftest(struct kunit *test);
+
+int init_xe_guc_selftest(struct kunit *test);
+void exit_xe_guc_selftest(struct kunit *test);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int xe_selftests_module_init(void);
+void xe_selftests_module_exit(void);
+#else
+static inline int xe_selftests_module_init(void) { return 0; }
+static inline void xe_selftests_module_exit(void) { }
+#endif
+
+#endif /* _XE_SELFTESTS_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +14 -0
new file mode 100644
index 000000000000..ef98f3599eac
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/device.h>
+#include <linux/glob.h>
+#include <linux/pci.h>
+
+#include "xe_drv.h"
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+static int bus_cb(struct notifier_block *nb, unsigned long action,

void *data)

drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +32 -0
+{
+       struct device *dev = data;
+       struct device_driver *drv = dev->driver;
+
+       if (action != BUS_NOTIFY_BOUND_DRIVER)
+               return 0;
+
+       if (strcmp(DRIVER_NAME, drv->name))
+               return 0;
+
+       /* only matching DUT names will be tested */
+       if (!dut_filter || !glob_match(dut_filter, dev_name(dev)))
+               return 0;
+
+       xe_selftests_run(dev_name(dev));
+
+       return 0;
+}
+
+static struct notifier_block nb = {
+       .notifier_call = bus_cb,
+};
+
+int xe_selftests_module_init(void)
+{
+       return bus_register_notifier(&pci_bus_type, &nb);
+}
+
+void xe_selftests_module_exit(void)
+{
+       bus_unregister_notifier(&pci_bus_type, &nb);
+}
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +42 -0
new file mode 100644
index 000000000000..93c3360c9631
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/glob.h>
+#include <kunit/test.h>
+
+#include "xe_device.h"
+#include "xe_drv.h"
+
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+#define DUT_NAME_LEN 32
+static char dut_name[DUT_NAME_LEN];
+
+/* protects @dut_name during live test execution */
+static DEFINE_MUTEX(dut_mutex);
+
+static int match_dev_any(struct device *dev, const void *data)
+{
+       return 1;
+}
+
+static int match_dev_by_name(struct device *dev, const void *data)
+{
+       const char *name = data;
+       return !strcmp(dev_name(dev), name);
+}
+
+struct xe_device *get_xe_dut(void)
+{
+       struct device_driver *drv;
+       struct device *dev;
+
+       if (!anonymous_dut && !mutex_is_locked(&dut_mutex))
+               return ERR_PTR(-ENOTTY);
+
+       drv = driver_find(DRIVER_NAME, &pci_bus_type);
+       dev = driver_find_device(drv, NULL, dut_name,
+                                mutex_is_locked(&dut_mutex) ?

match_dev_by_name : match_dev_any);
+       if (!dev)
+               return ERR_PTR(-ENODEV);

drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +74 -0
+
+       return to_xe_device(dev_get_drvdata(dev));
+}
+
+void put_xe_dut(struct xe_device *xe)
+{
+       put_device(xe->drm.dev);
+}
+
+static int run_test_suites(struct xe_device *xe)
+{
+       struct device *dev = xe->drm.dev;
+       struct device_driver *drv = dev->driver;
+       struct module *mod = drv->owner;
+       struct kunit_suite **suites = mod->kunit_suites;
+       int num_suites = mod->num_kunit_suites;
+       int i;
+
+       pr_info("TAP version 14\n");
+       pr_info("1..%u\n", num_suites);
+
+       /* reset kunit_suite_counter at the start */
+       __kunit_test_suites_exit(NULL, 0);
+
+       for (i = 0; i < num_suites; i++) {
+               kunit_run_tests(suites[i]);
+       }
+
+       /* reset kunit_suite_counter at the end */
+       __kunit_test_suites_exit(NULL, 0);
+
+       return num_suites;
+}
+
+/**
+ * xe_selftests_run - Run selftests on selected device
+ * @dev_name: The device name on which selftests should be run
+ *
+ * This function starts Kunit suites with live selftests on selected
+ * device (DUT).
+ *
+ * Return: 0 on success,
+ *   -EINVAL if no device name was provided,
+ *   -EBUSY if tests are already running,
+ *   -ENODEV if device was not found
+ *   or any other negative error code on failure.
+ */
+int xe_selftests_run(const char *dev_name)
+{
+       struct xe_device *xe;
+       int ret;
+
+       if (!dev_name)
+               return -EINVAL;
+       if (!mutex_trylock(&dut_mutex))
+               return -EBUSY;
+       strscpy(dut_name, dev_name, sizeof(dut_name));
+
+       xe = get_xe_dut();
+       if (IS_ERR(xe)) {
+               ret = PTR_ERR(xe);
+               goto cleanup;
+       }
+
+       ret = run_test_suites(xe);
+
+       put_xe_dut(xe);
+
+cleanup:
+       strscpy(dut_name, "", sizeof(dut_name));
+       mutex_unlock(&dut_mutex);
+
+       return ret;
+}
drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c new file +101 -0
new file mode 100644
index 000000000000..837d28871686
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/mutex.h>
+
+#include <kunit/test.h>
+
+#include "xe_device.h"
+#include "selftests/xe_selftests_internal.h"
+
+static struct xe_device *__get_xe(void)
+{
+       struct xe_device *xe = ERR_PTR(-ENOTTY);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_MOCK)
+       xe = get_xe_mock();
+#elif IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+       xe = get_xe_dut();
+#endif
+       return xe;
+}
+
+static void __put_xe(struct xe_device *xe)
+{
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_MOCK)
+       put_xe_mock(xe);
+#elif IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+       put_xe_dut(xe);
+#endif
+}
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int init_xe_live_selftest_suite(struct kunit_suite *suite)
+{
+       struct xe_device *xe = get_xe_dut();
+
+       if (IS_ERR(xe))
+               return PTR_ERR(xe);
+
+       kunit_info(suite, "DUT %s", dev_name(xe->drm.dev));
+       put_xe_dut(xe);
+       return 0;
+}
+#endif
+
+int init_xe_device_selftest(struct kunit *test)
+{
+       struct xe_device *xe = __get_xe();
+
+       if (IS_ERR(xe))
+               return PTR_ERR(xe);
+
+       test->priv = xe;
+       return 0;
+}
+
+void exit_xe_device_selftest(struct kunit *test)
+{
+       struct xe_device *xe = test->priv;
+
+       __put_xe(xe);
+}
+
+int init_xe_gt_selftest(struct kunit *test)
+{
+       struct xe_device *xe = __get_xe();
+
+       if (IS_ERR(xe))
+               return PTR_ERR(xe);
+
+       test->priv = to_gt(xe);
+       return 0;
+}
+
+void exit_xe_gt_selftest(struct kunit *test)
+{
+       struct xe_gt *gt = test->priv;
+
+       __put_xe(gt->xe);
+}
+
+int init_xe_guc_selftest(struct kunit *test)
+{
+       struct xe_device *xe = __get_xe();
+
+       if (IS_ERR(xe))
+               return PTR_ERR(xe);
+
+       test->priv = &to_gt(xe)->uc.guc;
+       return 0;
+}
+
+void exit_xe_guc_selftest(struct kunit *test)
+{
+       struct xe_guc *guc = test->priv;
+       struct xe_gt *gt = container_of(guc, struct xe_gt, uc.guc);
+
+       __put_xe(gt->xe);
+}
drivers/gpu/drm/xe/selftests/xe_selftests_internal.h new file +22 -0
new file mode 100644
index 000000000000..841c1a497eed
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTEST_INTERNAL_H_
+#define _XE_SELFTEST_INTERNAL_H_
+
+#include <linux/types.h>
+
+struct xe_device;
+
+extern char *dut_filter;
+extern bool anonymous_dut;
+
+struct xe_device *get_xe_dut(void);
+void put_xe_dut(struct xe_device *xe);
+
+struct xe_device *get_xe_mock(void);
+void put_xe_mock(struct xe_device *xe);
+
+#endif /* _XE_SELFTEST_INTERNAL_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_mock.c new file +32 -0
new file mode 100644
index 000000000000..c1515e451e99
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include "xe_device_types.h"
+
+#define MOCK_DEV_NAME "MOCK:DEBUG.0"
+static struct device __mock_device = {
+       .init_name = MOCK_DEV_NAME,
+};
+
+struct xe_device *get_xe_mock(void)
+{
+       struct xe_device *xe = kzalloc(sizeof(*xe), GFP_KERNEL);
+
+       if (!xe)
+               return ERR_PTR(-ENOMEM);
+
+       xe->drm.dev = &__mock_device;
+
+       xe->gt[0].info.id = 0;
+       xe->gt[0].info.type = XE_GT_TYPE_MAIN;
+       xe->gt[0].xe = xe;
+
+       return xe;
+}
+
+void put_xe_mock(struct xe_device *xe)
+{
+       kfree(xe);
+}
drivers/gpu/drm/xe/selftests/xe_selftests_params.c new file +13 -0
new file mode 100644
index 000000000000..e6e28cc91901
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/module.h>
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+
+char *dut_filter;
+module_param_named(dut_filter, dut_filter, charp, 0644);
+MODULE_PARM_DESC(dut_filter,
+                "The pattern of the names of devices on which we

want to run tests automatically. "
+                "If <none> is specified (default) then no devices will be tested automatically. "
+                "If '*' is specified then all new bound devices will be tested.\n");

drivers/gpu/drm/xe/selftests/xe_selftests_params.c new file +5 -0
+
+bool anonymous_dut;
+module_param_named(anonymous_dut, anonymous_dut, bool, 0600);
+MODULE_PARM_DESC(anonymous_dut,
+                "Enables selftests to be run without providing

explicit DUT name (like during "
+                "selftests execution triggered by the Kunit module notifier). Default is off.\n");

drivers/gpu/drm/xe/selftests/xe_selftests_params.c new file +2 -0
+
+#endif
drivers/gpu/drm/xe/xe_module.c +1 -0
index 6860586ce7f8..f76d69f7f5ad 100644
@@ -13,6 +13,7 @@
  #include "xe_module.h"
  #include "xe_pci.h"
  #include "xe_sched_job.h"
+#include "selftests/xe_selftests.h"


 bool enable_guc = true; module_param_named_unsafe(enable_guc, enable_guc, bool, 0444);

drivers/gpu/drm/xe/xe_module.c +1 -0
@@ -45,6 +46,7 @@ struct init_funcs {
  static const struct init_funcs init_funcs[] = {
         MAKE_INIT_EXIT_FUNCS(hw_fence),
         MAKE_INIT_EXIT_FUNCS(sched_job),
+       MAKE_INIT_EXIT_FUNCS(selftests),
  };


 static int __init xe_init(void)

#13 lore raw

Re: [Intel-xe] [PATCH 1/5] drm/xe: Introduce new selftests framework

Quoted 11 lines · Thomas Hellström

Hi, Michal,

Some comments below. I don't have time for an in-depth review today, but As a general comment, I don't think we should have multiple infrastructures for live Kunit selftests, so if we're adopting this, we should port the existing ones over.

Quoted 3 lines · Michal Wajdeczko

Our initial attempt to run Kunit based tests on live devices is hard to use and will not work well with multiple devices attached.

Quoted 8 lines
Quoted 4 lines

Instead of relying on separate test module and duplicating support for multiple live devices in every test entry point, create new set of helpers and use test->priv to pass reference of the device under test (DUT) directly to every test function.

Could this be passed directly as a parameter instead? I think the test->priv parameter is intended for and very useful to pass parameters used by code to provoke test-specific behaviour (see for

parameters are available as test->param_value, see [1]

[1] https://elixir.bootlin.com/linux/latest/source/include/kunit/test.h#L207

Quoted 2 lines

example our dma-buf kunit test). That would BTW be a good live test to port over to see if it could be made to work with this new framework.

done, see attached patch

Quoted 11 lines
Quoted 11 lines

The same use pattern is supported for both mock and live selftests.

By using bus_notifier we can start testing each new bound device separatelly, subject to filtering using dut_filter modparam.

All test cases can be included directly from .c file, allowing test code to access and use any private functions.

Quoted 7 lines
Quoted 3 lines

As Kunit will attempt to run all suites on module load, including our new live test suites, we may want to allow to execute them on any available device. This is controlled by anonymous_dut modparam.

What would it take with this infrastructure to be able to run both mock- and live without recompiling on an already loaded module? I think that's very convenient.

I had that locally, so doable, but note that without full support for the suite or test filtering it will potentially rerun everything on any device probe:

1. MOCK+LIVE(dev1) by kunit executor on module load
2. MOCK+LIVE(dev1) by new framework on device1 probe
3. MOCK+LIVE(dev2) by new framework on device2 probe .. MOCK+LIVE(devN) by new framework on deviceN probe

but if you still find that convenient (rather than running pure pure kunit tests using separate kunit build) then we can relax this limitation

Thanks,
Michal

Quoted 544 lines
Quoted 8 lines

Support for either mock or live selftests, only one at time, is controlled by new Kconfig options.

Thanks,
thomas

Quoted 531 lines

---
 drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++ drivers/gpu/drm/xe/Makefile                   |  11 ++ drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++ .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++ .../drm/xe/selftests/xe_selftests_executor.c  | 118
++++++++++++++++++
 .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++ .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++ .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++ .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++ drivers/gpu/drm/xe/xe_module.c                |   2 +

 10 files changed, 412 insertions(+)

 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c

drivers/gpu/drm/xe/Kconfig.debug +0 -0
index 93b284cdd0a2..aeba43977dca 100644
@@ -61,6 +61,28 @@ config DRM_XE_SIMPLE_ERROR_CAPTURE


          If in doubt, say "N".

+config DRM_XE_SELFTESTS_MOCK
+       bool "Mock selftests for Xe" if !KUNIT_ALL_TESTS +       depends on DRM_XE && KUNIT
+       default KUNIT_ALL_TESTS
+       help
+               Choose this option to allow the driver to perform mock
+               selftests based on the Kunit framework.

drivers/gpu/drm/xe/Kconfig.debug +10 -0
+
+               Recommended for driver developers only.
+               If in doubt, say "N".
+
+config DRM_XE_SELFTESTS_LIVE
+       bool "Live selftests for Xe" if !DRM_XE_SELFTESTS_MOCK
+       depends on DRM_XE && KUNIT && EXPERT
+       default n
+       help
+               Choose this option to allow the driver to perform

live
+               selftests based on the Kunit framework.

drivers/gpu/drm/xe/Kconfig.debug +4 -0
+
+               Recommended for driver developers only.
+               If in doubt, say "N".
+

 config DRM_XE_KUNIT_TEST
         tristate "KUnit tests for the drm xe driver" if !KUNIT_ALL_TESTS
        depends on DRM_XE && KUNIT && DEBUG_FS

drivers/gpu/drm/xe/Makefile +0 -0
index b94e65c91101..b193af2d9930 100644
@@ -211,6 +211,17 @@ ifeq ($(CONFIG_DRM_FBDEV_EMULATION),y)

        xe-$(CONFIG_DRM_XE_DISPLAY) += display/intel_fbdev.o endif

drivers/gpu/drm/xe/Makefile +11 -0
+xe-$(CONFIG_DRM_XE_SELFTESTS_MOCK) += \
+       selftests/xe_selftests_mock.o \
+
+xe-$(CONFIG_DRM_XE_SELFTESTS_LIVE) += \
+       selftests/xe_selftests_auto.o \
+       selftests/xe_selftests_executor.o \
+       selftests/xe_selftests_params.o \
+
+xe-$(CONFIG_KUNIT) += \
+       selftests/xe_selftests_helpers.o \
+

 obj-$(CONFIG_DRM_XE) += xe.o
 obj-$(CONFIG_DRM_XE_KUNIT_TEST) += tests/

drivers/gpu/drm/xe/selftests/xe_selftests.h new file +35 -0
new file mode 100644
index 000000000000..6b13b998e789
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTESTS_H_
+#define _XE_SELFTESTS_H_
+
+struct kunit_suite;
+struct kunit;
+
+int xe_selftests_run(const char *dev_name);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int init_xe_live_selftest_suite(struct kunit_suite *suite);
+#endif
+
+int init_xe_device_selftest(struct kunit *test);
+void exit_xe_device_selftest(struct kunit *test);
+
+int init_xe_gt_selftest(struct kunit *test);
+void exit_xe_gt_selftest(struct kunit *test);
+
+int init_xe_guc_selftest(struct kunit *test);
+void exit_xe_guc_selftest(struct kunit *test);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int xe_selftests_module_init(void);
+void xe_selftests_module_exit(void);
+#else
+static inline int xe_selftests_module_init(void) { return 0; }
+static inline void xe_selftests_module_exit(void) { }
+#endif
+
+#endif /* _XE_SELFTESTS_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +14 -0
new file mode 100644
index 000000000000..ef98f3599eac
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/device.h>
+#include <linux/glob.h>
+#include <linux/pci.h>
+
+#include "xe_drv.h"
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+static int bus_cb(struct notifier_block *nb, unsigned long action,

void *data)

drivers/gpu/drm/xe/selftests/xe_selftests_auto.c new file +32 -0
+{
+       struct device *dev = data;
+       struct device_driver *drv = dev->driver;
+
+       if (action != BUS_NOTIFY_BOUND_DRIVER)
+               return 0;
+
+       if (strcmp(DRIVER_NAME, drv->name))
+               return 0;
+
+       /* only matching DUT names will be tested */
+       if (!dut_filter || !glob_match(dut_filter, dev_name(dev)))
+               return 0;
+
+       xe_selftests_run(dev_name(dev));
+
+       return 0;
+}
+
+static struct notifier_block nb = {
+       .notifier_call = bus_cb,
+};
+
+int xe_selftests_module_init(void)
+{
+       return bus_register_notifier(&pci_bus_type, &nb);
+}
+
+void xe_selftests_module_exit(void)
+{
+       bus_unregister_notifier(&pci_bus_type, &nb);
+}
drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +42 -0
new file mode 100644
index 000000000000..93c3360c9631
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/glob.h>
+#include <kunit/test.h>
+
+#include "xe_device.h"
+#include "xe_drv.h"
+
+#include "selftests/xe_selftests.h"
+#include "selftests/xe_selftests_internal.h"
+
+#define DUT_NAME_LEN 32
+static char dut_name[DUT_NAME_LEN];
+
+/* protects @dut_name during live test execution */
+static DEFINE_MUTEX(dut_mutex);
+
+static int match_dev_any(struct device *dev, const void *data)
+{
+       return 1;
+}
+
+static int match_dev_by_name(struct device *dev, const void *data)
+{
+       const char *name = data;
+       return !strcmp(dev_name(dev), name);
+}
+
+struct xe_device *get_xe_dut(void)
+{
+       struct device_driver *drv;
+       struct device *dev;
+
+       if (!anonymous_dut && !mutex_is_locked(&dut_mutex))
+               return ERR_PTR(-ENOTTY);
+
+       drv = driver_find(DRIVER_NAME, &pci_bus_type);
+       dev = driver_find_device(drv, NULL, dut_name,
+                                mutex_is_locked(&dut_mutex) ?

match_dev_by_name : match_dev_any);
+       if (!dev)
+               return ERR_PTR(-ENODEV);

drivers/gpu/drm/xe/selftests/xe_selftests_executor.c new file +74 -0
+
+       return to_xe_device(dev_get_drvdata(dev));
+}
+
+void put_xe_dut(struct xe_device *xe)
+{
+       put_device(xe->drm.dev);
+}
+
+static int run_test_suites(struct xe_device *xe)
+{
+       struct device *dev = xe->drm.dev;
+       struct device_driver *drv = dev->driver;
+       struct module *mod = drv->owner;
+       struct kunit_suite **suites = mod->kunit_suites;
+       int num_suites = mod->num_kunit_suites;
+       int i;
+
+       pr_info("TAP version 14\n");
+       pr_info("1..%u\n", num_suites);
+
+       /* reset kunit_suite_counter at the start */
+       __kunit_test_suites_exit(NULL, 0);
+
+       for (i = 0; i < num_suites; i++) {
+               kunit_run_tests(suites[i]);
+       }
+
+       /* reset kunit_suite_counter at the end */
+       __kunit_test_suites_exit(NULL, 0);
+
+       return num_suites;
+}
+
+/**
+ * xe_selftests_run - Run selftests on selected device
+ * @dev_name: The device name on which selftests should be run
+ *
+ * This function starts Kunit suites with live selftests on selected
+ * device (DUT).
+ *
+ * Return: 0 on success,
+ *   -EINVAL if no device name was provided,
+ *   -EBUSY if tests are already running,
+ *   -ENODEV if device was not found
+ *   or any other negative error code on failure.
+ */
+int xe_selftests_run(const char *dev_name)
+{
+       struct xe_device *xe;
+       int ret;
+
+       if (!dev_name)
+               return -EINVAL;
+       if (!mutex_trylock(&dut_mutex))
+               return -EBUSY;
+       strscpy(dut_name, dev_name, sizeof(dut_name));
+
+       xe = get_xe_dut();
+       if (IS_ERR(xe)) {
+               ret = PTR_ERR(xe);
+               goto cleanup;
+       }
+
+       ret = run_test_suites(xe);
+
+       put_xe_dut(xe);
+
+cleanup:
+       strscpy(dut_name, "", sizeof(dut_name));
+       mutex_unlock(&dut_mutex);
+
+       return ret;
+}
drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c new file +101 -0
new file mode 100644
index 000000000000..837d28871686
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/mutex.h>
+
+#include <kunit/test.h>
+
+#include "xe_device.h"
+#include "selftests/xe_selftests_internal.h"
+
+static struct xe_device *__get_xe(void)
+{
+       struct xe_device *xe = ERR_PTR(-ENOTTY);
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_MOCK)
+       xe = get_xe_mock();
+#elif IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+       xe = get_xe_dut();
+#endif
+       return xe;
+}
+
+static void __put_xe(struct xe_device *xe)
+{
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_MOCK)
+       put_xe_mock(xe);
+#elif IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+       put_xe_dut(xe);
+#endif
+}
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+int init_xe_live_selftest_suite(struct kunit_suite *suite)
+{
+       struct xe_device *xe = get_xe_dut();
+
+       if (IS_ERR(xe))
+               return PTR_ERR(xe);
+
+       kunit_info(suite, "DUT %s", dev_name(xe->drm.dev));
+       put_xe_dut(xe);
+       return 0;
+}
+#endif
+
+int init_xe_device_selftest(struct kunit *test)
+{
+       struct xe_device *xe = __get_xe();
+
+       if (IS_ERR(xe))
+               return PTR_ERR(xe);
+
+       test->priv = xe;
+       return 0;
+}
+
+void exit_xe_device_selftest(struct kunit *test)
+{
+       struct xe_device *xe = test->priv;
+
+       __put_xe(xe);
+}
+
+int init_xe_gt_selftest(struct kunit *test)
+{
+       struct xe_device *xe = __get_xe();
+
+       if (IS_ERR(xe))
+               return PTR_ERR(xe);
+
+       test->priv = to_gt(xe);
+       return 0;
+}
+
+void exit_xe_gt_selftest(struct kunit *test)
+{
+       struct xe_gt *gt = test->priv;
+
+       __put_xe(gt->xe);
+}
+
+int init_xe_guc_selftest(struct kunit *test)
+{
+       struct xe_device *xe = __get_xe();
+
+       if (IS_ERR(xe))
+               return PTR_ERR(xe);
+
+       test->priv = &to_gt(xe)->uc.guc;
+       return 0;
+}
+
+void exit_xe_guc_selftest(struct kunit *test)
+{
+       struct xe_guc *guc = test->priv;
+       struct xe_gt *gt = container_of(guc, struct xe_gt, uc.guc);
+
+       __put_xe(gt->xe);
+}
drivers/gpu/drm/xe/selftests/xe_selftests_internal.h new file +22 -0
new file mode 100644
index 000000000000..841c1a497eed
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_SELFTEST_INTERNAL_H_
+#define _XE_SELFTEST_INTERNAL_H_
+
+#include <linux/types.h>
+
+struct xe_device;
+
+extern char *dut_filter;
+extern bool anonymous_dut;
+
+struct xe_device *get_xe_dut(void);
+void put_xe_dut(struct xe_device *xe);
+
+struct xe_device *get_xe_mock(void);
+void put_xe_mock(struct xe_device *xe);
+
+#endif /* _XE_SELFTEST_INTERNAL_H_ */
drivers/gpu/drm/xe/selftests/xe_selftests_mock.c new file +32 -0
new file mode 100644
index 000000000000..c1515e451e99
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include "xe_device_types.h"
+
+#define MOCK_DEV_NAME "MOCK:DEBUG.0"
+static struct device __mock_device = {
+       .init_name = MOCK_DEV_NAME,
+};
+
+struct xe_device *get_xe_mock(void)
+{
+       struct xe_device *xe = kzalloc(sizeof(*xe), GFP_KERNEL);
+
+       if (!xe)
+               return ERR_PTR(-ENOMEM);
+
+       xe->drm.dev = &__mock_device;
+
+       xe->gt[0].info.id = 0;
+       xe->gt[0].info.type = XE_GT_TYPE_MAIN;
+       xe->gt[0].xe = xe;
+
+       return xe;
+}
+
+void put_xe_mock(struct xe_device *xe)
+{
+       kfree(xe);
+}
drivers/gpu/drm/xe/selftests/xe_selftests_params.c new file +13 -0
new file mode 100644
index 000000000000..e6e28cc91901
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/module.h>
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+
+char *dut_filter;
+module_param_named(dut_filter, dut_filter, charp, 0644);
+MODULE_PARM_DESC(dut_filter,
+                "The pattern of the names of devices on which we

want to run tests automatically. "
+                "If <none> is specified (default) then no devices will be tested automatically. "
+                "If '*' is specified then all new bound devices will be tested.\n");

drivers/gpu/drm/xe/selftests/xe_selftests_params.c new file +5 -0
+
+bool anonymous_dut;
+module_param_named(anonymous_dut, anonymous_dut, bool, 0600);
+MODULE_PARM_DESC(anonymous_dut,
+                "Enables selftests to be run without providing

explicit DUT name (like during "
+                "selftests execution triggered by the Kunit module notifier). Default is off.\n");

drivers/gpu/drm/xe/selftests/xe_selftests_params.c new file +2 -0
+
+#endif
drivers/gpu/drm/xe/xe_module.c +1 -0
index 6860586ce7f8..f76d69f7f5ad 100644
@@ -13,6 +13,7 @@
  #include "xe_module.h"
  #include "xe_pci.h"
  #include "xe_sched_job.h"
+#include "selftests/xe_selftests.h"


 bool enable_guc = true; module_param_named_unsafe(enable_guc, enable_guc, bool, 0444);

Quoted 9 lines
Quoted 8 lines
drivers/gpu/drm/xe/xe_module.c +1 -0
@@ -45,6 +46,7 @@ struct init_funcs {
  static const struct init_funcs init_funcs[] = {
         MAKE_INIT_EXIT_FUNCS(hw_fence),
         MAKE_INIT_EXIT_FUNCS(sched_job),
+       MAKE_INIT_EXIT_FUNCS(selftests),
  };


 static int __init xe_init(void)

From 79e558f9b949c9683ff3f541414bae8eb85117da Mon Sep 17 00:00:00 2001
From: Michal Wajdeczko <[email protected]>
Date: Tue, 14 Mar 2023 00:23:35 +0100
Subject: [PATCH] drm/xe: Convert dma_buf_test to new live selftest framework

[12:10:23] =============== xe_dma_buf_live (1 subtest) ================ [12:10:23] ===================== test_xe_dma_buf ====================== [12:10:23] [PASSED] mask=0x04 force=no p2p=yes notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x04 force=yes p2p=yes notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x04 force=no p2p=no notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x04 force=yes p2p=no notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x04 force=no p2p=n/a notify=0x0
[12:10:23] [PASSED] mask=0x04 force=yes p2p=n/a notify=0x0
[12:10:23] [PASSED] mask=0x02 force=no p2p=yes notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x02 force=yes p2p=yes notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x02 force=no p2p=no notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x02 force=yes p2p=no notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x02 force=no p2p=n/a notify=0x0
[12:10:23] [PASSED] mask=0x02 force=yes p2p=n/a notify=0x0
[12:10:23] [PASSED] mask=0x06 force=no p2p=yes notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x06 force=yes p2p=yes notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x06 force=no p2p=no notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x06 force=yes p2p=no notify=xe_dma_buf_move_notify [xe] [12:10:23] [PASSED] mask=0x06 force=no p2p=n/a notify=0x0
[12:10:23] [PASSED] mask=0x06 force=yes p2p=n/a notify=0x0
[12:10:23] ================= [PASSED] test_xe_dma_buf =================

[  158.928563]     # xe_dma_buf_live: DUT 0000:00:02.0
[  158.928564]     # Subtest: xe_dma_buf_live
[  158.933507]     1..1
[  158.937774]         # Subtest: test_xe_dma_buf
[  158.940202]         ok 1 - mask=0x04 force=no  p2p=yes notify=xe_dma_buf_move_notify [xe]
[  158.944937]         ok 2 - mask=0x04 force=yes p2p=yes notify=xe_dma_buf_move_notify [xe]
[  158.953323]         ok 3 - mask=0x04 force=no  p2p=no  notify=xe_dma_buf_move_notify [xe]
[  158.961707]         ok 4 - mask=0x04 force=yes p2p=no  notify=xe_dma_buf_move_notify [xe]
[  158.970233]         ok 5 - mask=0x04 force=no  p2p=n/a notify=0x0
[  158.978785]         ok 6 - mask=0x04 force=yes p2p=n/a notify=0x0
[  158.985253]     # test_xe_dma_buf: running xe_test_dmabuf_import_same_driver
[  158.998881]         ok 7 - mask=0x02 force=no  p2p=yes notify=xe_dma_buf_move_notify [xe]
[  158.999041]     # test_xe_dma_buf: running xe_test_dmabuf_import_same_driver
[  159.014636]         ok 8 - mask=0x02 force=yes p2p=yes notify=xe_dma_buf_move_notify [xe]
[  159.014889]     # test_xe_dma_buf: running xe_test_dmabuf_import_same_driver
[  159.030388]         ok 9 - mask=0x02 force=no  p2p=no  notify=xe_dma_buf_move_notify [xe]
[  159.030652]     # test_xe_dma_buf: running xe_test_dmabuf_import_same_driver
[  159.046388]         ok 10 - mask=0x02 force=yes p2p=no  notify=xe_dma_buf_move_notify [xe]
[  159.046598]     # test_xe_dma_buf: running xe_test_dmabuf_import_same_driver
[  159.062069]         ok 11 - mask=0x02 force=no  p2p=n/a notify=0x0
[  159.062233]     # test_xe_dma_buf: running xe_test_dmabuf_import_same_driver
[  159.075709]         ok 12 - mask=0x02 force=yes p2p=n/a notify=0x0
[  159.075962]         ok 13 - mask=0x06 force=no  p2p=yes notify=xe_dma_buf_move_notify [xe]
[  159.082363]         ok 14 - mask=0x06 force=yes p2p=yes notify=xe_dma_buf_move_notify [xe]
[  159.090886]         ok 15 - mask=0x06 force=no  p2p=no  notify=xe_dma_buf_move_notify [xe]
[  159.099301]         ok 16 - mask=0x06 force=yes p2p=no  notify=xe_dma_buf_move_notify [xe]
[  159.107817]         ok 17 - mask=0x06 force=no  p2p=n/a notify=0x0
[  159.116298]         ok 18 - mask=0x06 force=yes p2p=n/a notify=0x0
[  159.122564]     # test_xe_dma_buf: pass:18 fail:0 skip:0 total:18
[  159.128782]     ok 1 - test_xe_dma_buf
---
 drivers/gpu/drm/xe/tests/Makefile          |  2 +-
 drivers/gpu/drm/xe/tests/xe_dma_buf.c      | 54 +++++++++++++---------
 drivers/gpu/drm/xe/tests/xe_dma_buf_test.c | 23 ---------
 drivers/gpu/drm/xe/tests/xe_dma_buf_test.h | 13 ------
 drivers/gpu/drm/xe/xe_dma_buf.c            |  7 ++-
 5 files changed, 35 insertions(+), 64 deletions(-)
 delete mode 100644 drivers/gpu/drm/xe/tests/xe_dma_buf_test.c
 delete mode 100644 drivers/gpu/drm/xe/tests/xe_dma_buf_test.h
drivers/gpu/drm/xe/tests/Makefile +1 -1
index 47056b6459e3..ee241dc6a123 100644
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0
 
-obj-$(CONFIG_DRM_XE_KUNIT_TEST) += xe_bo_test.o xe_dma_buf_test.o \
+obj-$(CONFIG_DRM_XE_KUNIT_TEST) += xe_bo_test.o \
 	xe_migrate_test.o
drivers/gpu/drm/xe/tests/xe_dma_buf.c +31 -23

91 lines not on this page · raw message

drivers/gpu/drm/xe/tests/xe_dma_buf_test.c deleted +0 -23
deleted file mode 100644
index a1adfd1e1605..000000000000
@@ -1,23 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/*
- * Copyright © 2022 Intel Corporation
- */
-
-#include "xe_dma_buf_test.h"
-
-#include <kunit/test.h>
-
-static struct kunit_case xe_dma_buf_tests[] = {
-	KUNIT_CASE(xe_dma_buf_kunit),
-	{}
-};
-
-static struct kunit_suite xe_dma_buf_test_suite = {
-	.name = "xe_dma_buf",
-	.test_cases = xe_dma_buf_tests,
-};
-
-kunit_test_suite(xe_dma_buf_test_suite);
-
-MODULE_AUTHOR("Intel Corporation");
-MODULE_LICENSE("GPL");
drivers/gpu/drm/xe/tests/xe_dma_buf_test.h deleted +0 -13
deleted file mode 100644
index e6b464ddd526..000000000000
@@ -1,13 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 AND MIT */
-/*
- * Copyright © 2023 Intel Corporation
- */
-
-#ifndef _XE_DMA_BUF_TEST_H_
-#define _XE_DMA_BUF_TEST_H_
-
-struct kunit;
-
-void xe_dma_buf_kunit(struct kunit *test);
-
-#endif
drivers/gpu/drm/xe/xe_dma_buf.c +3 -4
index 9b252cc782b7..4d1ed17ce9d7 100644
@@ -243,9 +243,8 @@ struct dma_buf_test_params {
 struct drm_gem_object *xe_gem_prime_import(struct drm_device *dev,
 					   struct dma_buf *dma_buf)
 {
-	XE_TEST_DECLARE(struct dma_buf_test_params *test =
-			to_dma_buf_test_params
-			(xe_cur_kunit_priv(XE_TEST_LIVE_DMA_BUF));)
+	XE_TEST_DECLARE(const struct dma_buf_test_params *test =
+			xe_cur_kunit()->param_value;)
 	const struct dma_buf_attach_ops *attach_ops;
 	struct dma_buf_attachment *attach;
 	struct drm_gem_object *obj;
@@ -301,6 +300,6 @@ struct drm_gem_object *xe_gem_prime_import(struct drm_device *dev,
 	return obj;
 }
 
-#if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
 #include "tests/xe_dma_buf.c"
 #endif
Signature · 1 line
2.25.1
#14 lore raw

PATCH 2/5 drm/xe: Initial selftests for xe_device

In reply to Michal Wajdeczko

Add few very basic mock and live selftests for the xe_device.

---
 .../selftests/live/xe_device_live_selftest.c  | 39 ++++++++++++++++++
 .../selftests/mock/xe_device_mock_selftest.c  | 41 +++++++++++++++++++
 drivers/gpu/drm/xe/xe_device.c                |  6 +++
 3 files changed, 86 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_device_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_device_mock_selftest.c
drivers/gpu/drm/xe/selftests/live/xe_device_live_selftest.c new file +39 -0
new file mode 100644
index 000000000000..05ad2b04528a
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <kunit/test.h>
+
+#include "selftests/xe_selftests.h"
+
+static void test_to_gt(struct kunit *test)
+{
+	struct xe_device *xe = test->priv;
+
+	KUNIT_ASSERT_NOT_NULL(test, to_gt(xe));
+}
+
+static void test_guc_submission_enabled(struct kunit *test)
+{
+	struct xe_device *xe = test->priv;
+
+	KUNIT_EXPECT_TRUE(test, xe_device_guc_submission_enabled(xe));
+}
+
+static struct kunit_case device_live_test_cases[] = {
+	KUNIT_CASE(test_to_gt),
+	KUNIT_CASE(test_guc_submission_enabled),
+	{}
+};
+
+static struct kunit_suite device_live_suite = {
+	.name = "device_live",
+	.test_cases = device_live_test_cases,
+
+	.suite_init = init_xe_live_selftest_suite,
+	.init = init_xe_device_selftest,
+	.exit = exit_xe_device_selftest,
+};
+
+kunit_test_suite(device_live_suite);
drivers/gpu/drm/xe/selftests/mock/xe_device_mock_selftest.c new file +41 -0

41 lines not on this page · raw message

drivers/gpu/drm/xe/xe_device.c +6 -0
index 1553949d12b6..503f985167fd 100644
@@ -426,3 +426,9 @@ void xe_device_mem_access_put(struct xe_device *xe)
 
 	XE_WARN_ON(xe->mem_access.ref < 0);
 }
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_MOCK)
+#include "selftests/mock/xe_device_mock_selftest.c"
+#elif IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+#include "selftests/live/xe_device_live_selftest.c"
+#endif
Signature · 1 line
2.25.1
#15 lore raw

PATCH 3/5 drm/xe: Initial selftests for xe_gt

In reply to Michal Wajdeczko

Add few very basic mock and live selftests for the xe_gt.

---
 .../xe/selftests/live/xe_gt_live_selftest.c   | 40 +++++++++++++++++++
 .../xe/selftests/mock/xe_gt_mock_selftest.c   | 40 +++++++++++++++++++
 drivers/gpu/drm/xe/xe_gt.c                    |  6 +++
 3 files changed, 86 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_gt_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_gt_mock_selftest.c
drivers/gpu/drm/xe/selftests/live/xe_gt_live_selftest.c new file +40 -0
new file mode 100644
index 000000000000..d807ec1d1d8c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <kunit/test.h>
+
+#include "selftests/xe_selftests.h"
+
+static void test_gt_info(struct kunit *test)
+{
+	struct xe_gt *gt = test->priv;
+
+	KUNIT_EXPECT_NE(test, gt->info.type, XE_GT_TYPE_UNINITIALIZED);
+	KUNIT_EXPECT_EQ(test, gt->info.id, 0u);
+}
+
+static void test_gt_is_not_media_type(struct kunit *test)
+{
+	struct xe_gt *gt = test->priv;
+
+	KUNIT_EXPECT_FALSE(test, xe_gt_is_media_type(gt));
+}
+
+static struct kunit_case gt_live_test_cases[] = {
+	KUNIT_CASE(test_gt_info),
+	KUNIT_CASE(test_gt_is_not_media_type),
+	{}
+};
+
+static struct kunit_suite gt_live_suite = {
+	.name = "gt_live",
+	.test_cases = gt_live_test_cases,
+
+	.suite_init = init_xe_live_selftest_suite,
+	.init = init_xe_gt_selftest,
+	.exit = exit_xe_gt_selftest,
+};
+
+kunit_test_suite(gt_live_suite);
drivers/gpu/drm/xe/selftests/mock/xe_gt_mock_selftest.c new file +40 -0
new file mode 100644
index 000000000000..f208aea3d1a0
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright � 2023 Intel Corporation
+ */
+
+#include <kunit/test.h>
+
+#include "selftests/xe_selftests.h"
+
+static void test_basic(struct kunit *test)
+{
+	struct xe_gt *gt = test->priv;
+
+	KUNIT_ASSERT_NOT_NULL(test, gt);
+	KUNIT_EXPECT_EQ(test, gt->info.type, XE_GT_TYPE_MAIN);
+	KUNIT_EXPECT_EQ(test, gt->info.id, 0u);
+}
+
+static void test_to_xe(struct kunit *test)
+{
+	struct xe_gt *gt = test->priv;
+
+	KUNIT_ASSERT_NOT_NULL(test, gt->xe);
+}
+
+static struct kunit_case gt_mock_test_cases[] = {
+	KUNIT_CASE(test_basic),
+	KUNIT_CASE(test_to_xe),
+	{}
+};
+
+static struct kunit_suite gt_mock_suite = {
+	.name = "gt_mock",
+	.test_cases = gt_mock_test_cases,
+
+	.init = init_xe_gt_selftest,
+	.exit = exit_xe_gt_selftest,
+};
+
+kunit_test_suite(gt_mock_suite);
drivers/gpu/drm/xe/xe_gt.c +6 -0
index daa433d0f2f5..83861014acfa 100644
@@ -852,3 +852,9 @@ struct xe_hw_engine *xe_gt_any_hw_engine_by_reset_domain(struct xe_gt *gt,
 
 	return NULL;
 }
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_MOCK)
+#include "selftests/mock/xe_gt_mock_selftest.c"
+#elif IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+#include "selftests/live/xe_gt_live_selftest.c"
+#endif
Signature · 1 line
2.25.1
#16 lore raw

PATCH 4/5 drm/xe: Initial selftests for xe_guc

In reply to Michal Wajdeczko

Add few very basic mock and live selftests for the xe_guc.

---
 .../xe/selftests/live/xe_guc_live_selftest.c  | 54 +++++++++++++++++++
 .../xe/selftests/mock/xe_guc_mock_selftest.c  | 30 +++++++++++
 drivers/gpu/drm/xe/xe_guc.c                   |  6 +++
 3 files changed, 90 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_guc_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_guc_mock_selftest.c
drivers/gpu/drm/xe/selftests/live/xe_guc_live_selftest.c new file +54 -0
new file mode 100644
index 000000000000..eefda6af1cce
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <kunit/test.h>
+
+#include "selftests/xe_selftests.h"
+
+static void test_guc_fw(struct kunit *test)
+{
+	struct xe_guc *guc = test->priv;
+
+	KUNIT_EXPECT_TRUE(test, xe_uc_fw_is_supported(&guc->fw));
+	KUNIT_EXPECT_TRUE(test, xe_uc_fw_is_enabled(&guc->fw));
+	KUNIT_EXPECT_TRUE(test, xe_uc_fw_is_available(&guc->fw));
+	KUNIT_EXPECT_TRUE(test, xe_uc_fw_is_running(&guc->fw));
+}
+
+static int guc_send_unknown_action(struct xe_guc *guc)
+{
+	u32 request[] = {
+		FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) |
+		FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_REQUEST) |
+		FIELD_PREP(GUC_HXG_REQUEST_MSG_0_ACTION, 0xDEAD),
+	};
+
+	return xe_guc_mmio_send(guc, request, ARRAY_SIZE(request));
+}
+
+static void test_guc_rejects_unknown_action(struct kunit *test)
+{
+	struct xe_guc *guc = test->priv;
+
+	kunit_skip(test, "XE_BUG_ON(guc->ct.enabled)");
+	KUNIT_EXPECT_NE(test, 0, guc_send_unknown_action(guc));
+}
+
+static struct kunit_case guc_live_test_cases[] = {
+	KUNIT_CASE(test_guc_fw),
+	KUNIT_CASE(test_guc_rejects_unknown_action),
+	{}
+};
+
+static struct kunit_suite guc_live_suite = {
+	.name = "guc_live",
+	.test_cases = guc_live_test_cases,
+
+	.suite_init = init_xe_live_selftest_suite,
+	.init = init_xe_guc_selftest,
+	.exit = exit_xe_guc_selftest,
+};
+
+kunit_test_suite(guc_live_suite);
drivers/gpu/drm/xe/selftests/mock/xe_guc_mock_selftest.c new file +30 -0
new file mode 100644
index 000000000000..c10c91b5971f
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright � 2023 Intel Corporation
+ */
+
+#include <kunit/test.h>
+
+#include "selftests/xe_selftests.h"
+
+static void test_basic(struct kunit *test)
+{
+	struct xe_guc *guc = test->priv;
+
+	KUNIT_ASSERT_NOT_NULL(test, guc);
+}
+
+static struct kunit_case guc_mock_test_cases[] = {
+	KUNIT_CASE(test_basic),
+	{}
+};
+
+static struct kunit_suite guc_mock_suite = {
+	.name = "guc_mock",
+	.test_cases = guc_mock_test_cases,
+
+	.init = init_xe_guc_selftest,
+	.exit = exit_xe_guc_selftest,
+};
+
+kunit_test_suite(guc_mock_suite);
drivers/gpu/drm/xe/xe_guc.c +6 -0
index 58b9841616e4..86fbac466788 100644
@@ -871,3 +871,9 @@ void xe_guc_print_info(struct xe_guc *guc, struct drm_printer *p)
 	xe_guc_ct_print(&guc->ct, p);
 	xe_guc_submit_print(guc, p);
 }
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_MOCK)
+#include "selftests/mock/xe_guc_mock_selftest.c"
+#elif IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+#include "selftests/live/xe_guc_live_selftest.c"
+#endif
Signature · 1 line
2.25.1
#17 lore raw

PATCH 5/5 drm/xe: Initial selftests for xe_guc_ct

In reply to Michal Wajdeczko

Add few very basic mock and live selftests for the xe_guc_ct.

---
 .../selftests/live/xe_guc_ct_live_selftest.c  | 52 +++++++++++++++++++
 .../selftests/mock/xe_guc_ct_mock_selftest.c  | 31 +++++++++++
 drivers/gpu/drm/xe/xe_guc_ct.c                |  6 +++
 3 files changed, 89 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_guc_ct_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_guc_ct_mock_selftest.c
drivers/gpu/drm/xe/selftests/live/xe_guc_ct_live_selftest.c new file +52 -0
new file mode 100644
index 000000000000..73da4c5d6e81
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <kunit/test.h>
+
+#include "selftests/xe_selftests.h"
+
+static void test_ct_not_broken(struct kunit *test)
+{
+	struct xe_guc *guc = test->priv;
+	struct xe_guc_ct *ct = &guc->ct;
+
+	KUNIT_EXPECT_FALSE(test, ct->ctbs.h2g.broken);
+	KUNIT_EXPECT_FALSE(test, ct->ctbs.g2h.broken);
+}
+
+static int guc_ct_send_unknown_action(struct xe_guc_ct *ct)
+{
+	u32 request[] = {
+		FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) |
+		FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_REQUEST) |
+		FIELD_PREP(GUC_HXG_REQUEST_MSG_0_ACTION, 0xDEAD),
+	};
+
+	return xe_guc_ct_send_block(ct, request, ARRAY_SIZE(request));
+}
+
+static void test_ct_rejects_unknown_action(struct kunit *test)
+{
+	struct xe_guc *guc = test->priv;
+
+	KUNIT_EXPECT_NE(test, 0, guc_ct_send_unknown_action(&guc->ct));
+}
+
+static struct kunit_case ct_live_test_cases[] = {
+	KUNIT_CASE(test_ct_not_broken),
+	KUNIT_CASE(test_ct_rejects_unknown_action),
+	{}
+};
+
+static struct kunit_suite ct_live_suite = {
+	.name = "ct_live",
+	.test_cases = ct_live_test_cases,
+
+	.suite_init = init_xe_live_selftest_suite,
+	.init = init_xe_guc_selftest,
+	.exit = exit_xe_guc_selftest,
+};
+
+kunit_test_suite(ct_live_suite);
drivers/gpu/drm/xe/selftests/mock/xe_guc_ct_mock_selftest.c new file +31 -0
new file mode 100644
index 000000000000..db6e08b7ad8b
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright � 2023 Intel Corporation
+ */
+
+#include <kunit/test.h>
+
+#include "selftests/xe_selftests.h"
+
+static void test_basic(struct kunit *test)
+{
+	struct xe_guc *guc = test->priv;
+	struct xe_guc_ct *ct = &guc->ct;
+
+	KUNIT_ASSERT_NOT_NULL(test, ct);
+}
+
+static struct kunit_case guc_ct_mock_test_cases[] = {
+	KUNIT_CASE(test_basic),
+	{}
+};
+
+static struct kunit_suite guc_ct_mock_suite = {
+	.name = "guc_ct_mock",
+	.test_cases = guc_ct_mock_test_cases,
+
+	.init = init_xe_guc_selftest,
+	.exit = exit_xe_guc_selftest,
+};
+
+kunit_test_suite(guc_ct_mock_suite);
drivers/gpu/drm/xe/xe_guc_ct.c +6 -0
index 5e00b75d3ca2..7ed360229087 100644
@@ -1203,3 +1203,9 @@ void xe_guc_ct_selftest(struct xe_guc_ct *ct, struct drm_printer *p)
 	drm_printf(p, "GuC CT selftest done - %s\n", ret ? "FAIL" : "PASS");
 }
 #endif
+
+#if IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_MOCK)
+#include "selftests/mock/xe_guc_ct_mock_selftest.c"
+#elif IS_ENABLED(CONFIG_DRM_XE_SELFTESTS_LIVE)
+#include "selftests/live/xe_guc_ct_live_selftest.c"
+#endif
Signature · 1 line
2.25.1
Reviewed-by on the v1 series
#18 lore raw
In reply to Michal Wajdeczko
Quoted 11 lines · On Mon, 13 Mar 2023 09:13:11 +0100 Michal Wajdeczko…

Existing solution to support live testing has many limitations. With proposed here framework we can live test every new bound device and use same style for both mock and live tests of Xe, GT or GuC.

Also Kunit parsing works better (which might be needed by our CI).

And with little more effort, patches pending, we can add support for rerunning tests on selected device or filtering tests to be executed.

This new framework can coexist with any other default kunit tests and any currently available live tests implemented as separate module.

I did some comments to patch 1/5.

For patches 2 to 5: they LGTM. Feel free to add:

Quoted 111 lines

Sample output from mock run:

[ ] Starting KUnit Kernel (1/1)...
[ ] ============================================================ [ ] ================= device_mock (2 subtests) ================= [ ] [PASSED] test_basic
[ ] [PASSED] test_to_gt
[ ] =================== [PASSED] device_mock =================== [ ] =================== gt_mock (2 subtests) =================== [ ] [PASSED] test_basic
[ ] [PASSED] test_to_xe
[ ] ===================== [PASSED] gt_mock ===================== [ ] =================== guc_mock (1 subtest) =================== [ ] [PASSED] test_basic
[ ] ==================== [PASSED] guc_mock ===================== [ ] ================= guc_ct_mock (1 subtest) ================== [ ] [PASSED] test_basic
[ ] =================== [PASSED] guc_ct_mock =================== [ ] ============================================================ [ ] Testing complete. Ran 6 tests: passed: 6

Sample output from live run with "dut_filter=*"

[ ] ============================================================ [ ] ================= device_live (2 subtests) ================= [ ] [PASSED] test_to_gt
[ ] [PASSED] test_guc_submission_enabled
[ ] =================== [PASSED] device_live =================== [ ] =================== gt_live (2 subtests) =================== [ ] [PASSED] test_gt_info
[ ] [PASSED] test_gt_is_not_media_type
[ ] ===================== [PASSED] gt_live ===================== [ ] ================== guc_live (2 subtests) =================== [ ] [PASSED] test_guc_fw
[ ] [SKIPPED] test_guc_rejects_unknown_action
[ ] ==================== [PASSED] guc_live ===================== [ ] =================== ct_live (2 subtests) =================== [ ] [PASSED] test_ct_not_broken
[ ] [PASSED] test_ct_rejects_unknown_action
[ ] ===================== [PASSED] ct_live ===================== [ ] ============================================================ [ ] Testing complete. Ran 8 tests: passed: 7, skipped: 1

Sample raw output from live run with "anonymous_dut=1"

[ ] [drm] Initialized xe 1.1.0 20201103 for 0000:00:02.0 on minor 0

[ ]     # device_live: DUT 0000:00:02.0
[ ]     # Subtest: device_live
[ ]     1..2
[ ]     ok 1 - test_to_gt
[ ]     ok 2 - test_guc_submission_enabled

[ ] # device_live: pass:2 fail:0 skip:0 total:2
[ ] # Totals: pass:2 fail:0 skip:0 total:2
...

Sample raw output from live run with "anonymous_dut=0"

[ ] # device_live: # failed to initialize (-25)
[ ] not ok 1 - device_live
...

Cc Mauro Carvalho Chehab <[email protected]>

Michal Wajdeczko (5):
drm/xe: Introduce new selftests framework
drm/xe: Initial selftests for xe_device
drm/xe: Initial selftests for xe_gt
drm/xe: Initial selftests for xe_guc
drm/xe: Initial selftests for xe_guc_ct

 drivers/gpu/drm/xe/Kconfig.debug              |  22 ++++
 drivers/gpu/drm/xe/Makefile                   |  11 ++
 .../selftests/live/xe_device_live_selftest.c  |  39 ++++++
 .../xe/selftests/live/xe_gt_live_selftest.c   |  40 ++++++
 .../selftests/live/xe_guc_ct_live_selftest.c  |  52 ++++++++
 .../xe/selftests/live/xe_guc_live_selftest.c  |  54 ++++++++
 .../selftests/mock/xe_device_mock_selftest.c  |  41 ++++++
 .../xe/selftests/mock/xe_gt_mock_selftest.c   |  40 ++++++
 .../selftests/mock/xe_guc_ct_mock_selftest.c  |  31 +++++
 .../xe/selftests/mock/xe_guc_mock_selftest.c  |  30 +++++
 drivers/gpu/drm/xe/selftests/xe_selftests.h   |  35 ++++++
 .../gpu/drm/xe/selftests/xe_selftests_auto.c  |  46 +++++++
 .../drm/xe/selftests/xe_selftests_executor.c  | 118 ++++++++++++++++++
 .../drm/xe/selftests/xe_selftests_helpers.c   | 101 +++++++++++++++
 .../drm/xe/selftests/xe_selftests_internal.h  |  22 ++++
 .../gpu/drm/xe/selftests/xe_selftests_mock.c  |  32 +++++
 .../drm/xe/selftests/xe_selftests_params.c    |  23 ++++
 drivers/gpu/drm/xe/xe_device.c                |   6 +
 drivers/gpu/drm/xe/xe_gt.c                    |   6 +
 drivers/gpu/drm/xe/xe_guc.c                   |   6 +
 drivers/gpu/drm/xe/xe_guc_ct.c                |   6 +
 drivers/gpu/drm/xe/xe_module.c                |   2 +
 22 files changed, 763 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_device_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_gt_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_guc_ct_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/live/xe_guc_live_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_device_mock_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_gt_mock_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_guc_ct_mock_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/mock/xe_guc_mock_selftest.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests.h
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_auto.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_executor.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_helpers.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_internal.h
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_mock.c
 create mode 100644 drivers/gpu/drm/xe/selftests/xe_selftests_params.c