memtest.c 2.75 KB
Newer Older
1 2 3
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/init.h>
4
#include <linux/memblock.h>
5

6
static u64 patterns[] __initdata = {
7
	/* The first entry has to be 0 to leave memtest with zeroed memory */
8 9 10 11
	0,
	0xffffffffffffffffULL,
	0x5555555555555555ULL,
	0xaaaaaaaaaaaaaaaaULL,
12 13 14 15 16 17 18 19 20 21 22 23 24
	0x1111111111111111ULL,
	0x2222222222222222ULL,
	0x4444444444444444ULL,
	0x8888888888888888ULL,
	0x3333333333333333ULL,
	0x6666666666666666ULL,
	0x9999999999999999ULL,
	0xccccccccccccccccULL,
	0x7777777777777777ULL,
	0xbbbbbbbbbbbbbbbbULL,
	0xddddddddddddddddULL,
	0xeeeeeeeeeeeeeeeeULL,
	0x7a6c7258554e494cULL, /* yeah ;-) */
25
};
26

27
static void __init reserve_bad_mem(u64 pattern, phys_addr_t start_bad, phys_addr_t end_bad)
28
{
29 30
	pr_info("  %016llx bad mem addr %pa - %pa reserved\n",
		cpu_to_be64(pattern), &start_bad, &end_bad);
31
	memblock_reserve(start_bad, end_bad - start_bad);
32 33
}

34
static void __init memtest(u64 pattern, phys_addr_t start_phys, phys_addr_t size)
35
{
36
	u64 *p, *start, *end;
37 38
	phys_addr_t start_bad, last_bad;
	phys_addr_t start_phys_aligned;
39
	const size_t incr = sizeof(pattern);
40 41 42

	start_phys_aligned = ALIGN(start_phys, incr);
	start = __va(start_phys_aligned);
43
	end = start + (size - (start_phys_aligned - start_phys)) / incr;
44 45 46
	start_bad = 0;
	last_bad = 0;

47 48
	for (p = start; p < end; p++)
		*p = pattern;
49

50 51
	for (p = start; p < end; p++, start_phys_aligned += incr) {
		if (*p == pattern)
52 53 54 55
			continue;
		if (start_phys_aligned == last_bad + incr) {
			last_bad += incr;
			continue;
56
		}
57 58 59
		if (start_bad)
			reserve_bad_mem(pattern, start_bad, last_bad + incr);
		start_bad = last_bad = start_phys_aligned;
60
	}
61 62
	if (start_bad)
		reserve_bad_mem(pattern, start_bad, last_bad + incr);
63 64
}

65
static void __init do_one_pass(u64 pattern, phys_addr_t start, phys_addr_t end)
66
{
67 68 69
	u64 i;
	phys_addr_t this_start, this_end;

70 71
	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &this_start,
				&this_end, NULL) {
72 73
		this_start = clamp(this_start, start, end);
		this_end = clamp(this_end, start, end);
74
		if (this_start < this_end) {
75 76
			pr_info("  %pa - %pa pattern %016llx\n",
				&this_start, &this_end, cpu_to_be64(pattern));
77 78
			memtest(pattern, this_start, this_end - this_start);
		}
79 80 81
	}
}

82
/* default is disabled */
83
static unsigned int memtest_pattern __initdata;
84 85 86

static int __init parse_memtest(char *arg)
{
87 88
	int ret = 0;

89
	if (arg)
90
		ret = kstrtouint(arg, 0, &memtest_pattern);
91 92 93
	else
		memtest_pattern = ARRAY_SIZE(patterns);

94
	return ret;
95 96 97 98
}

early_param("memtest", parse_memtest);

99
void __init early_memtest(phys_addr_t start, phys_addr_t end)
100
{
101
	unsigned int i;
102
	unsigned int idx = 0;
103 104 105 106

	if (!memtest_pattern)
		return;

107
	pr_info("early_memtest: # of tests: %u\n", memtest_pattern);
108
	for (i = memtest_pattern-1; i < UINT_MAX; --i) {
109 110 111
		idx = i % ARRAY_SIZE(patterns);
		do_one_pass(patterns[idx], start, end);
	}
112
}