ne_misc_dev_test.c 4.4 KB

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