1// SPDX-License-Identifier: GPL-2.0
2
3#include <kunit/test.h>
4
5#define MAX_PHYS_REGIONS 16
6#define INVALID_VALUE (~0ull)
7
8struct ne_phys_regions_test {
9 u64 paddr;
10 u64 size;
11 int expect_rc;
12 unsigned long expect_num;
13 u64 expect_last_paddr;
14 u64 expect_last_size;
15} phys_regions_test_cases[] = {
16 /*
17 * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
18 * Expected result:
19 * Failed, start address is not 2M-aligned
20 *
21 * Now the instance of struct ne_phys_contig_mem_regions is:
22 * num = 0
23 * regions = {}
24 */
25 {0x1000, 0x200000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
26
27 /*
28 * Add the region from 0x200000 to (0x200000 + 0x1000 - 1):
29 * Expected result:
30 * Failed, size is not 2M-aligned
31 *
32 * Now the instance of struct ne_phys_contig_mem_regions is:
33 * num = 0
34 * regions = {}
35 */
36 {0x200000, 0x1000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
37
38 /*
39 * Add the region from 0x200000 to (0x200000 + 0x200000 - 1):
40 * Expected result:
41 * Successful
42 *
43 * Now the instance of struct ne_phys_contig_mem_regions is:
44 * num = 1
45 * regions = {
46 * {start=0x200000, end=0x3fffff}, // len=0x200000
47 * }
48 */
49 {0x200000, 0x200000, 0, 1, 0x200000, 0x200000},
50
51 /*
52 * Add the region from 0x0 to (0x0 + 0x200000 - 1):
53 * Expected result:
54 * Successful
55 *
56 * Now the instance of struct ne_phys_contig_mem_regions is:
57 * num = 2
58 * regions = {
59 * {start=0x200000, end=0x3fffff}, // len=0x200000
60 * {start=0x0, end=0x1fffff}, // len=0x200000
61 * }
62 */
63 {0x0, 0x200000, 0, 2, 0x0, 0x200000},
64
65 /*
66 * Add the region from 0x600000 to (0x600000 + 0x400000 - 1):
67 * Expected result:
68 * Successful
69 *
70 * Now the instance of struct ne_phys_contig_mem_regions is:
71 * num = 3
72 * regions = {
73 * {start=0x200000, end=0x3fffff}, // len=0x200000
74 * {start=0x0, end=0x1fffff}, // len=0x200000
75 * {start=0x600000, end=0x9fffff}, // len=0x400000
76 * }
77 */
78 {0x600000, 0x400000, 0, 3, 0x600000, 0x400000},
79
80 /*
81 * Add the region from 0xa00000 to (0xa00000 + 0x400000 - 1):
82 * Expected result:
83 * Successful, merging case!
84 *
85 * Now the instance of struct ne_phys_contig_mem_regions is:
86 * num = 3
87 * regions = {
88 * {start=0x200000, end=0x3fffff}, // len=0x200000
89 * {start=0x0, end=0x1fffff}, // len=0x200000
90 * {start=0x600000, end=0xdfffff}, // len=0x800000
91 * }
92 */
93 {0xa00000, 0x400000, 0, 3, 0x600000, 0x800000},
94
95 /*
96 * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
97 * Expected result:
98 * Failed, start address is not 2M-aligned
99 *
100 * Now the instance of struct ne_phys_contig_mem_regions is:
101 * num = 3
102 * regions = {
103 * {start=0x200000, end=0x3fffff}, // len=0x200000
104 * {start=0x0, end=0x1fffff}, // len=0x200000
105 * {start=0x600000, end=0xdfffff}, // len=0x800000
106 * }
107 */
108 {0x1000, 0x200000, -EINVAL, 3, 0x600000, 0x800000},
109};
110
111static void ne_misc_dev_test_merge_phys_contig_memory_regions(struct kunit *test)
112{
113 struct ne_phys_contig_mem_regions phys_contig_mem_regions = {};
114 int rc = 0;
115 int i = 0;
116
117 phys_contig_mem_regions.regions = kunit_kcalloc(test, MAX_PHYS_REGIONS,
118 size: sizeof(*phys_contig_mem_regions.regions),
119 GFP_KERNEL);
120 KUNIT_ASSERT_TRUE(test, phys_contig_mem_regions.regions);
121
122 for (i = 0; i < ARRAY_SIZE(phys_regions_test_cases); i++) {
123 struct ne_phys_regions_test *test_case = &phys_regions_test_cases[i];
124 unsigned long num = 0;
125
126 rc = ne_merge_phys_contig_memory_regions(phys_contig_regions: &phys_contig_mem_regions,
127 page_paddr: test_case->paddr, page_size: test_case->size);
128 KUNIT_EXPECT_EQ(test, rc, test_case->expect_rc);
129 KUNIT_EXPECT_EQ(test, phys_contig_mem_regions.num, test_case->expect_num);
130
131 if (test_case->expect_last_paddr == INVALID_VALUE)
132 continue;
133
134 num = phys_contig_mem_regions.num;
135 KUNIT_EXPECT_EQ(test, phys_contig_mem_regions.regions[num - 1].start,
136 test_case->expect_last_paddr);
137 KUNIT_EXPECT_EQ(test, range_len(&phys_contig_mem_regions.regions[num - 1]),
138 test_case->expect_last_size);
139 }
140
141 kunit_kfree(test, ptr: phys_contig_mem_regions.regions);
142}
143
144static struct kunit_case ne_misc_dev_test_cases[] = {
145 KUNIT_CASE(ne_misc_dev_test_merge_phys_contig_memory_regions),
146 {}
147};
148
149static struct kunit_suite ne_misc_dev_test_suite = {
150 .name = "ne_misc_dev_test",
151 .test_cases = ne_misc_dev_test_cases,
152};
153
154kunit_test_suite(ne_misc_dev_test_suite);
155

source code of linux/drivers/virt/nitro_enclaves/ne_misc_dev_test.c