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