xref: /linux/drivers/gpu/drm/xe/tests/xe_lmtt_test.c (revision 41c177cf354126a22443b5c80cec9fdd313e67e1)
1*f1a5d808SMichal Wajdeczko // SPDX-License-Identifier: GPL-2.0 AND MIT
2*f1a5d808SMichal Wajdeczko /*
3*f1a5d808SMichal Wajdeczko  * Copyright © 2023 Intel Corporation
4*f1a5d808SMichal Wajdeczko  */
5*f1a5d808SMichal Wajdeczko 
6*f1a5d808SMichal Wajdeczko #include <kunit/test.h>
7*f1a5d808SMichal Wajdeczko 
8*f1a5d808SMichal Wajdeczko static const struct lmtt_ops_param {
9*f1a5d808SMichal Wajdeczko 	const char *desc;
10*f1a5d808SMichal Wajdeczko 	const struct xe_lmtt_ops *ops;
11*f1a5d808SMichal Wajdeczko } lmtt_ops_params[] = {
12*f1a5d808SMichal Wajdeczko 	{ "2-level", &lmtt_2l_ops, },
13*f1a5d808SMichal Wajdeczko 	{ "multi-level", &lmtt_ml_ops, },
14*f1a5d808SMichal Wajdeczko };
15*f1a5d808SMichal Wajdeczko 
lmtt_ops_param_get_desc(const struct lmtt_ops_param * p,char * desc)16*f1a5d808SMichal Wajdeczko static void lmtt_ops_param_get_desc(const struct lmtt_ops_param *p, char *desc)
17*f1a5d808SMichal Wajdeczko {
18*f1a5d808SMichal Wajdeczko 	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s", p->desc);
19*f1a5d808SMichal Wajdeczko }
20*f1a5d808SMichal Wajdeczko 
21*f1a5d808SMichal Wajdeczko KUNIT_ARRAY_PARAM(lmtt_ops, lmtt_ops_params, lmtt_ops_param_get_desc);
22*f1a5d808SMichal Wajdeczko 
test_ops(struct kunit * test)23*f1a5d808SMichal Wajdeczko static void test_ops(struct kunit *test)
24*f1a5d808SMichal Wajdeczko {
25*f1a5d808SMichal Wajdeczko 	const struct lmtt_ops_param *p = test->param_value;
26*f1a5d808SMichal Wajdeczko 	const struct xe_lmtt_ops *ops = p->ops;
27*f1a5d808SMichal Wajdeczko 	unsigned int n;
28*f1a5d808SMichal Wajdeczko 
29*f1a5d808SMichal Wajdeczko 	KUNIT_ASSERT_NOT_NULL(test, ops->lmtt_root_pd_level);
30*f1a5d808SMichal Wajdeczko 	KUNIT_ASSERT_NOT_NULL(test, ops->lmtt_pte_num);
31*f1a5d808SMichal Wajdeczko 	KUNIT_ASSERT_NOT_NULL(test, ops->lmtt_pte_size);
32*f1a5d808SMichal Wajdeczko 	KUNIT_ASSERT_NOT_NULL(test, ops->lmtt_pte_shift);
33*f1a5d808SMichal Wajdeczko 	KUNIT_ASSERT_NOT_NULL(test, ops->lmtt_pte_index);
34*f1a5d808SMichal Wajdeczko 	KUNIT_ASSERT_NOT_NULL(test, ops->lmtt_pte_encode);
35*f1a5d808SMichal Wajdeczko 
36*f1a5d808SMichal Wajdeczko 	KUNIT_EXPECT_NE(test, ops->lmtt_root_pd_level(), 0);
37*f1a5d808SMichal Wajdeczko 
38*f1a5d808SMichal Wajdeczko 	for (n = 0; n <= ops->lmtt_root_pd_level(); n++) {
39*f1a5d808SMichal Wajdeczko 		KUNIT_EXPECT_NE_MSG(test, ops->lmtt_pte_num(n), 0,
40*f1a5d808SMichal Wajdeczko 				    "level=%u", n);
41*f1a5d808SMichal Wajdeczko 		KUNIT_EXPECT_NE_MSG(test, ops->lmtt_pte_size(n), 0,
42*f1a5d808SMichal Wajdeczko 				    "level=%u", n);
43*f1a5d808SMichal Wajdeczko 		KUNIT_EXPECT_NE_MSG(test, ops->lmtt_pte_encode(0, n), LMTT_PTE_INVALID,
44*f1a5d808SMichal Wajdeczko 				    "level=%u", n);
45*f1a5d808SMichal Wajdeczko 	}
46*f1a5d808SMichal Wajdeczko 
47*f1a5d808SMichal Wajdeczko 	for (n = 0; n < ops->lmtt_root_pd_level(); n++) {
48*f1a5d808SMichal Wajdeczko 		u64 addr = BIT_ULL(ops->lmtt_pte_shift(n));
49*f1a5d808SMichal Wajdeczko 
50*f1a5d808SMichal Wajdeczko 		KUNIT_EXPECT_NE_MSG(test, ops->lmtt_pte_shift(n), 0,
51*f1a5d808SMichal Wajdeczko 				    "level=%u", n);
52*f1a5d808SMichal Wajdeczko 		KUNIT_EXPECT_EQ_MSG(test, ops->lmtt_pte_index(addr - 1, n), 0,
53*f1a5d808SMichal Wajdeczko 				    "addr=%#llx level=%u", addr, n);
54*f1a5d808SMichal Wajdeczko 		KUNIT_EXPECT_EQ_MSG(test, ops->lmtt_pte_index(addr + 1, n), 1,
55*f1a5d808SMichal Wajdeczko 				    "addr=%#llx level=%u", addr, n);
56*f1a5d808SMichal Wajdeczko 		KUNIT_EXPECT_EQ_MSG(test, ops->lmtt_pte_index(addr * 2 - 1, n), 1,
57*f1a5d808SMichal Wajdeczko 				    "addr=%#llx level=%u", addr, n);
58*f1a5d808SMichal Wajdeczko 		KUNIT_EXPECT_EQ_MSG(test, ops->lmtt_pte_index(addr * 2, n), 2,
59*f1a5d808SMichal Wajdeczko 				    "addr=%#llx level=%u", addr, n);
60*f1a5d808SMichal Wajdeczko 	}
61*f1a5d808SMichal Wajdeczko }
62*f1a5d808SMichal Wajdeczko 
63*f1a5d808SMichal Wajdeczko static struct kunit_case lmtt_test_cases[] = {
64*f1a5d808SMichal Wajdeczko 	KUNIT_CASE_PARAM(test_ops, lmtt_ops_gen_params),
65*f1a5d808SMichal Wajdeczko 	{}
66*f1a5d808SMichal Wajdeczko };
67*f1a5d808SMichal Wajdeczko 
68*f1a5d808SMichal Wajdeczko static struct kunit_suite lmtt_suite = {
69*f1a5d808SMichal Wajdeczko 	.name = "lmtt",
70*f1a5d808SMichal Wajdeczko 	.test_cases = lmtt_test_cases,
71*f1a5d808SMichal Wajdeczko };
72*f1a5d808SMichal Wajdeczko 
73*f1a5d808SMichal Wajdeczko kunit_test_suites(&lmtt_suite);
74