xref: /linux/drivers/gpu/drm/xe/tests/xe_test.h (revision 2697b79a469b68e3ad3640f55284359c1396278d)
1 /* SPDX-License-Identifier: GPL-2.0 AND MIT */
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #ifndef _XE_TEST_H_
7 #define _XE_TEST_H_
8 
9 #include <linux/types.h>
10 
11 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
12 #include <linux/sched.h>
13 #include <kunit/test.h>
14 
15 /*
16  * Each test that provides a kunit private test structure, place a test id
17  * here and point the kunit->priv to an embedded struct xe_test_priv.
18  */
19 enum xe_test_priv_id {
20 	XE_TEST_LIVE_DMA_BUF,
21 	XE_TEST_LIVE_MIGRATE,
22 };
23 
24 /**
25  * struct xe_test_priv - Base class for test private info
26  * @id: enum xe_test_priv_id to identify the subclass.
27  */
28 struct xe_test_priv {
29 	enum xe_test_priv_id id;
30 };
31 
32 #define XE_TEST_DECLARE(x) x
33 #define XE_TEST_ONLY(x) unlikely(x)
34 #define XE_TEST_EXPORT
35 #define xe_cur_kunit() current->kunit_test
36 
37 /**
38  * xe_cur_kunit_priv - Obtain the struct xe_test_priv pointed to by
39  * current->kunit->priv if it exists and is embedded in the expected subclass.
40  * @id: Id of the expected subclass.
41  *
42  * Return: NULL if the process is not a kunit test, and NULL if the
43  * current kunit->priv pointer is not pointing to an object of the expected
44  * subclass. A pointer to the embedded struct xe_test_priv otherwise.
45  */
46 static inline struct xe_test_priv *
47 xe_cur_kunit_priv(enum xe_test_priv_id id)
48 {
49 	struct xe_test_priv *priv;
50 
51 	if (!xe_cur_kunit())
52 		return NULL;
53 
54 	priv = xe_cur_kunit()->priv;
55 	return priv->id == id ? priv : NULL;
56 }
57 
58 #else /* if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST) */
59 
60 #define XE_TEST_DECLARE(x)
61 #define XE_TEST_ONLY(x) 0
62 #define XE_TEST_EXPORT static
63 #define xe_cur_kunit() NULL
64 #define xe_cur_kunit_priv(_id) NULL
65 
66 #endif
67 #endif
68