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