memtest.c 2.7 KB

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