Merge tag 'x86_urgent_for_v6.12' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / virt / nitro_enclaves / ne_misc_dev_test.c
blob74df43b925be3a3143ef60298fa64d22163e156f
1 // SPDX-License-Identifier: GPL-2.0
3 #include <kunit/test.h>
5 #define MAX_PHYS_REGIONS 16
6 #define INVALID_VALUE (~0ull)
8 struct 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[] = {
17 * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
18 * Expected result:
19 * Failed, start address is not 2M-aligned
21 * Now the instance of struct ne_phys_contig_mem_regions is:
22 * num = 0
23 * regions = {}
25 {0x1000, 0x200000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
28 * Add the region from 0x200000 to (0x200000 + 0x1000 - 1):
29 * Expected result:
30 * Failed, size is not 2M-aligned
32 * Now the instance of struct ne_phys_contig_mem_regions is:
33 * num = 0
34 * regions = {}
36 {0x200000, 0x1000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
39 * Add the region from 0x200000 to (0x200000 + 0x200000 - 1):
40 * Expected result:
41 * Successful
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 * }
49 {0x200000, 0x200000, 0, 1, 0x200000, 0x200000},
52 * Add the region from 0x0 to (0x0 + 0x200000 - 1):
53 * Expected result:
54 * Successful
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 * }
63 {0x0, 0x200000, 0, 2, 0x0, 0x200000},
66 * Add the region from 0x600000 to (0x600000 + 0x400000 - 1):
67 * Expected result:
68 * Successful
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 * }
78 {0x600000, 0x400000, 0, 3, 0x600000, 0x400000},
81 * Add the region from 0xa00000 to (0xa00000 + 0x400000 - 1):
82 * Expected result:
83 * Successful, merging case!
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 * }
93 {0xa00000, 0x400000, 0, 3, 0x600000, 0x800000},
96 * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
97 * Expected result:
98 * Failed, start address is not 2M-aligned
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
108 {0x1000, 0x200000, -EINVAL, 3, 0x600000, 0x800000},
111 static void ne_misc_dev_test_merge_phys_contig_memory_regions(struct kunit *test)
113 struct ne_phys_contig_mem_regions phys_contig_mem_regions = {};
114 int rc = 0;
115 int i = 0;
117 phys_contig_mem_regions.regions = kunit_kcalloc(test, MAX_PHYS_REGIONS,
118 sizeof(*phys_contig_mem_regions.regions),
119 GFP_KERNEL);
120 KUNIT_ASSERT_TRUE(test, phys_contig_mem_regions.regions);
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;
126 rc = ne_merge_phys_contig_memory_regions(&phys_contig_mem_regions,
127 test_case->paddr, 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);
131 if (test_case->expect_last_paddr == INVALID_VALUE)
132 continue;
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);
141 kunit_kfree(test, phys_contig_mem_regions.regions);
144 static struct kunit_case ne_misc_dev_test_cases[] = {
145 KUNIT_CASE(ne_misc_dev_test_merge_phys_contig_memory_regions),
149 static struct kunit_suite ne_misc_dev_test_suite = {
150 .name = "ne_misc_dev_test",
151 .test_cases = ne_misc_dev_test_cases,
154 kunit_test_suite(ne_misc_dev_test_suite);