memtest.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/types.h>
  4. #include <linux/init.h>
  5. #include <linux/memblock.h>
  6. static u64 patterns[] __initdata = {
  7. /* The first entry has to be 0 to leave memtest with zeroed memory */
  8. 0,
  9. 0xffffffffffffffffULL,
  10. 0x5555555555555555ULL,
  11. 0xaaaaaaaaaaaaaaaaULL,
  12. 0x1111111111111111ULL,
  13. 0x2222222222222222ULL,
  14. 0x4444444444444444ULL,
  15. 0x8888888888888888ULL,
  16. 0x3333333333333333ULL,
  17. 0x6666666666666666ULL,
  18. 0x9999999999999999ULL,
  19. 0xccccccccccccccccULL,
  20. 0x7777777777777777ULL,
  21. 0xbbbbbbbbbbbbbbbbULL,
  22. 0xddddddddddddddddULL,
  23. 0xeeeeeeeeeeeeeeeeULL,
  24. 0x7a6c7258554e494cULL, /* yeah ;-) */
  25. };
  26. static void __init reserve_bad_mem(u64 pattern, phys_addr_t start_bad, phys_addr_t end_bad)
  27. {
  28. #ifdef CONFIG_X86
  29. WARN_ONCE(1, "Bad RAM detected. Use memtest86+ to perform a thorough test\n"
  30. "and the memmap= parameter to reserve the bad areas.");
  31. #endif
  32. pr_info(" %016llx bad mem addr %pa - %pa reserved\n",
  33. cpu_to_be64(pattern), &start_bad, &end_bad);
  34. memblock_reserve(start_bad, end_bad - start_bad);
  35. }
  36. static void __init memtest(u64 pattern, phys_addr_t start_phys, phys_addr_t size)
  37. {
  38. u64 *p, *start, *end;
  39. phys_addr_t start_bad, last_bad;
  40. phys_addr_t start_phys_aligned;
  41. const size_t incr = sizeof(pattern);
  42. start_phys_aligned = ALIGN(start_phys, incr);
  43. start = __va(start_phys_aligned);
  44. end = start + (size - (start_phys_aligned - start_phys)) / incr;
  45. start_bad = 0;
  46. last_bad = 0;
  47. for (p = start; p < end; p++)
  48. *p = pattern;
  49. for (p = start; p < end; p++, start_phys_aligned += incr) {
  50. if (*p == pattern)
  51. continue;
  52. if (start_phys_aligned == last_bad + incr) {
  53. last_bad += incr;
  54. continue;
  55. }
  56. if (start_bad)
  57. reserve_bad_mem(pattern, start_bad, last_bad + incr);
  58. start_bad = last_bad = start_phys_aligned;
  59. }
  60. if (start_bad)
  61. reserve_bad_mem(pattern, start_bad, last_bad + incr);
  62. }
  63. static void __init do_one_pass(u64 pattern, phys_addr_t start, phys_addr_t end)
  64. {
  65. u64 i;
  66. phys_addr_t this_start, this_end;
  67. for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &this_start,
  68. &this_end, NULL) {
  69. this_start = clamp(this_start, start, end);
  70. this_end = clamp(this_end, start, end);
  71. if (this_start < this_end) {
  72. pr_info(" %pa - %pa pattern %016llx\n",
  73. &this_start, &this_end, cpu_to_be64(pattern));
  74. memtest(pattern, this_start, this_end - this_start);
  75. }
  76. }
  77. }
  78. /* default is disabled */
  79. static unsigned int memtest_pattern __initdata;
  80. static int __init parse_memtest(char *arg)
  81. {
  82. int ret = 0;
  83. if (arg)
  84. ret = kstrtouint(arg, 0, &memtest_pattern);
  85. else
  86. memtest_pattern = ARRAY_SIZE(patterns);
  87. return ret;
  88. }
  89. early_param("memtest", parse_memtest);
  90. void __init early_memtest(phys_addr_t start, phys_addr_t end)
  91. {
  92. unsigned int i;
  93. unsigned int idx = 0;
  94. if (!memtest_pattern)
  95. return;
  96. pr_info("early_memtest: # of tests: %u\n", memtest_pattern);
  97. for (i = memtest_pattern-1; i < UINT_MAX; --i) {
  98. idx = i % ARRAY_SIZE(patterns);
  99. do_one_pass(patterns[idx], start, end);
  100. }
  101. }