Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
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      * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
0018      *   Expected result:
0019      *       Failed, start address is not 2M-aligned
0020      *
0021      * Now the instance of struct ne_phys_contig_mem_regions is:
0022      *   num = 0
0023      *   regions = {}
0024      */
0025     {0x1000, 0x200000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
0026 
0027     /*
0028      * Add the region from 0x200000 to (0x200000 + 0x1000 - 1):
0029      *   Expected result:
0030      *       Failed, size is not 2M-aligned
0031      *
0032      * Now the instance of struct ne_phys_contig_mem_regions is:
0033      *   num = 0
0034      *   regions = {}
0035      */
0036     {0x200000, 0x1000, -EINVAL, 0, INVALID_VALUE, INVALID_VALUE},
0037 
0038     /*
0039      * Add the region from 0x200000 to (0x200000 + 0x200000 - 1):
0040      *   Expected result:
0041      *       Successful
0042      *
0043      * Now the instance of struct ne_phys_contig_mem_regions is:
0044      *   num = 1
0045      *   regions = {
0046      *       {start=0x200000, end=0x3fffff}, // len=0x200000
0047      *   }
0048      */
0049     {0x200000, 0x200000, 0, 1, 0x200000, 0x200000},
0050 
0051     /*
0052      * Add the region from 0x0 to (0x0 + 0x200000 - 1):
0053      *   Expected result:
0054      *       Successful
0055      *
0056      * Now the instance of struct ne_phys_contig_mem_regions is:
0057      *   num = 2
0058      *   regions = {
0059      *       {start=0x200000, end=0x3fffff}, // len=0x200000
0060      *       {start=0x0,      end=0x1fffff}, // len=0x200000
0061      *   }
0062      */
0063     {0x0, 0x200000, 0, 2, 0x0, 0x200000},
0064 
0065     /*
0066      * Add the region from 0x600000 to (0x600000 + 0x400000 - 1):
0067      *   Expected result:
0068      *       Successful
0069      *
0070      * Now the instance of struct ne_phys_contig_mem_regions is:
0071      *   num = 3
0072      *   regions = {
0073      *       {start=0x200000, end=0x3fffff}, // len=0x200000
0074      *       {start=0x0,      end=0x1fffff}, // len=0x200000
0075      *       {start=0x600000, end=0x9fffff}, // len=0x400000
0076      *   }
0077      */
0078     {0x600000, 0x400000, 0, 3, 0x600000, 0x400000},
0079 
0080     /*
0081      * Add the region from 0xa00000 to (0xa00000 + 0x400000 - 1):
0082      *   Expected result:
0083      *       Successful, merging case!
0084      *
0085      * Now the instance of struct ne_phys_contig_mem_regions is:
0086      *   num = 3
0087      *   regions = {
0088      *       {start=0x200000, end=0x3fffff}, // len=0x200000
0089      *       {start=0x0,      end=0x1fffff}, // len=0x200000
0090      *       {start=0x600000, end=0xdfffff}, // len=0x800000
0091      *   }
0092      */
0093     {0xa00000, 0x400000, 0, 3, 0x600000, 0x800000},
0094 
0095     /*
0096      * Add the region from 0x1000 to (0x1000 + 0x200000 - 1):
0097      *   Expected result:
0098      *       Failed, start address is not 2M-aligned
0099      *
0100      * Now the instance of struct ne_phys_contig_mem_regions is:
0101      *   num = 3
0102      *   regions = {
0103      *       {start=0x200000, end=0x3fffff}, // len=0x200000
0104      *       {start=0x0,      end=0x1fffff}, // len=0x200000
0105      *       {start=0x600000, end=0xdfffff}, // len=0x800000
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);