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
{
12 unsigned long expect_num
;
13 u64 expect_last_paddr
;
15 } phys_regions_test_cases
[] = {
17 * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
19 * Failed, start address is not 2M-aligned
21 * Now the instance of struct ne_phys_contig_mem_regions is:
25 {0x1000, 0x200000, -EINVAL
, 0, INVALID_VALUE
, INVALID_VALUE
},
28 * Add the region from 0x200000 to (0x200000 + 0x1000 - 1):
30 * Failed, size is not 2M-aligned
32 * Now the instance of struct ne_phys_contig_mem_regions is:
36 {0x200000, 0x1000, -EINVAL
, 0, INVALID_VALUE
, INVALID_VALUE
},
39 * Add the region from 0x200000 to (0x200000 + 0x200000 - 1):
43 * Now the instance of struct ne_phys_contig_mem_regions is:
46 * {start=0x200000, end=0x3fffff}, // len=0x200000
49 {0x200000, 0x200000, 0, 1, 0x200000, 0x200000},
52 * Add the region from 0x0 to (0x0 + 0x200000 - 1):
56 * Now the instance of struct ne_phys_contig_mem_regions is:
59 * {start=0x200000, end=0x3fffff}, // len=0x200000
60 * {start=0x0, end=0x1fffff}, // len=0x200000
63 {0x0, 0x200000, 0, 2, 0x0, 0x200000},
66 * Add the region from 0x600000 to (0x600000 + 0x400000 - 1):
70 * Now the instance of struct ne_phys_contig_mem_regions is:
73 * {start=0x200000, end=0x3fffff}, // len=0x200000
74 * {start=0x0, end=0x1fffff}, // len=0x200000
75 * {start=0x600000, end=0x9fffff}, // len=0x400000
78 {0x600000, 0x400000, 0, 3, 0x600000, 0x400000},
81 * Add the region from 0xa00000 to (0xa00000 + 0x400000 - 1):
83 * Successful, merging case!
85 * Now the instance of struct ne_phys_contig_mem_regions is:
88 * {start=0x200000, end=0x3fffff}, // len=0x200000
89 * {start=0x0, end=0x1fffff}, // len=0x200000
90 * {start=0x600000, end=0xdfffff}, // len=0x800000
93 {0xa00000, 0x400000, 0, 3, 0x600000, 0x800000},
96 * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
98 * Failed, start address is not 2M-aligned
100 * Now the instance of struct ne_phys_contig_mem_regions is:
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
= {};
117 phys_contig_mem_regions
.regions
= kunit_kcalloc(test
, MAX_PHYS_REGIONS
,
118 sizeof(*phys_contig_mem_regions
.regions
),
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
)
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
);