0001
0002
0003 #include <kunit/test.h>
0004
0005 #define MAX_PHYS_REGIONS 16
0006 #define INVALID_VALUE (~0ull)
0007
0008 struct ne_phys_regions_test {
0009 u64 paddr;
0010 u64 size;
0011 int expect_rc;
0012 unsigned long expect_num;
0013 u64 expect_last_paddr;
0014 u64 expect_last_size;
0015 } phys_regions_test_cases[] = {
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 {0x1000, 0x200000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 {0x200000, 0x1000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 {0x200000, 0x200000, 0, 1, 0x200000, 0x200000},
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 {0x0, 0x200000, 0, 2, 0x0, 0x200000},
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078 {0x600000, 0x400000, 0, 3, 0x600000, 0x400000},
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093 {0xa00000, 0x400000, 0, 3, 0x600000, 0x800000},
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108 {0x1000, 0x200000, -EINVAL, 3, 0x600000, 0x800000},
0109 };
0110
0111 static void ne_misc_dev_test_merge_phys_contig_memory_regions(struct kunit *test)
0112 {
0113 struct ne_phys_contig_mem_regions phys_contig_mem_regions = {};
0114 int rc = 0;
0115 int i = 0;
0116
0117 phys_contig_mem_regions.regions = kunit_kcalloc(test, MAX_PHYS_REGIONS,
0118 sizeof(*phys_contig_mem_regions.regions),
0119 GFP_KERNEL);
0120 KUNIT_ASSERT_TRUE(test, phys_contig_mem_regions.regions);
0121
0122 for (i = 0; i < ARRAY_SIZE(phys_regions_test_cases); i++) {
0123 struct ne_phys_regions_test *test_case = &phys_regions_test_cases[i];
0124 unsigned long num = 0;
0125
0126 rc = ne_merge_phys_contig_memory_regions(&phys_contig_mem_regions,
0127 test_case->paddr, test_case->size);
0128 KUNIT_EXPECT_EQ(test, rc, test_case->expect_rc);
0129 KUNIT_EXPECT_EQ(test, phys_contig_mem_regions.num, test_case->expect_num);
0130
0131 if (test_case->expect_last_paddr == INVALID_VALUE)
0132 continue;
0133
0134 num = phys_contig_mem_regions.num;
0135 KUNIT_EXPECT_EQ(test, phys_contig_mem_regions.regions[num - 1].start,
0136 test_case->expect_last_paddr);
0137 KUNIT_EXPECT_EQ(test, range_len(&phys_contig_mem_regions.regions[num - 1]),
0138 test_case->expect_last_size);
0139 }
0140
0141 kunit_kfree(test, phys_contig_mem_regions.regions);
0142 }
0143
0144 static struct kunit_case ne_misc_dev_test_cases[] = {
0145 KUNIT_CASE(ne_misc_dev_test_merge_phys_contig_memory_regions),
0146 {}
0147 };
0148
0149 static struct kunit_suite ne_misc_dev_test_suite = {
0150 .name = "ne_misc_dev_test",
0151 .test_cases = ne_misc_dev_test_cases,
0152 };
0153
0154 kunit_test_suite(ne_misc_dev_test_suite);