blob: 1997d934b13b001f1b1decf9ca898847fe6fb6be [file] [log] [blame]
Yinghai Lu1f067162008-07-15 00:02:28 -07001#include <linux/kernel.h>
2#include <linux/errno.h>
3#include <linux/string.h>
4#include <linux/types.h>
5#include <linux/mm.h>
6#include <linux/smp.h>
7#include <linux/init.h>
8#include <linux/pfn.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -07009#include <linux/memblock.h>
Yinghai Lu1f067162008-07-15 00:02:28 -070010
Andreas Herrmann6d741712009-02-25 11:27:27 +010011static u64 patterns[] __initdata = {
Alexander Holler20bf0622013-02-16 16:38:17 +010012 /* The first entry has to be 0 to leave memtest with zeroed memory */
Andreas Herrmann6d741712009-02-25 11:27:27 +010013 0,
14 0xffffffffffffffffULL,
15 0x5555555555555555ULL,
16 0xaaaaaaaaaaaaaaaaULL,
Andreas Herrmann63823122009-02-25 11:31:49 +010017 0x1111111111111111ULL,
18 0x2222222222222222ULL,
19 0x4444444444444444ULL,
20 0x8888888888888888ULL,
21 0x3333333333333333ULL,
22 0x6666666666666666ULL,
23 0x9999999999999999ULL,
24 0xccccccccccccccccULL,
25 0x7777777777777777ULL,
26 0xbbbbbbbbbbbbbbbbULL,
27 0xddddddddddddddddULL,
28 0xeeeeeeeeeeeeeeeeULL,
29 0x7a6c7258554e494cULL, /* yeah ;-) */
Andreas Herrmann6d741712009-02-25 11:27:27 +010030};
Andreas Herrmann40823f72009-02-25 11:26:18 +010031
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070032static void __init reserve_bad_mem(u64 pattern, phys_addr_t start_bad, phys_addr_t end_bad)
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010033{
Andreas Herrmann570c9e62009-02-25 11:28:58 +010034 printk(KERN_INFO " %016llx bad mem addr %010llx - %010llx reserved\n",
35 (unsigned long long) pattern,
36 (unsigned long long) start_bad,
37 (unsigned long long) end_bad);
Tejun Heo24aa0782011-07-12 11:16:06 +020038 memblock_reserve(start_bad, end_bad - start_bad);
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010039}
40
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070041static void __init memtest(u64 pattern, phys_addr_t start_phys, phys_addr_t size)
Yinghai Lu1f067162008-07-15 00:02:28 -070042{
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020043 u64 *p, *start, *end;
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070044 phys_addr_t start_bad, last_bad;
45 phys_addr_t start_phys_aligned;
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020046 const size_t incr = sizeof(pattern);
Yinghai Lu1f067162008-07-15 00:02:28 -070047
Yinghai Lu1f067162008-07-15 00:02:28 -070048 start_phys_aligned = ALIGN(start_phys, incr);
Yinghai Lu1f067162008-07-15 00:02:28 -070049 start = __va(start_phys_aligned);
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020050 end = start + (size - (start_phys_aligned - start_phys)) / incr;
Yinghai Lu1f067162008-07-15 00:02:28 -070051 start_bad = 0;
52 last_bad = 0;
53
Andreas Herrmannc9690992009-06-08 19:09:39 +020054 for (p = start; p < end; p++)
55 *p = pattern;
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020056
Andreas Herrmannc9690992009-06-08 19:09:39 +020057 for (p = start; p < end; p++, start_phys_aligned += incr) {
58 if (*p == pattern)
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010059 continue;
60 if (start_phys_aligned == last_bad + incr) {
61 last_bad += incr;
62 continue;
Yinghai Lu1f067162008-07-15 00:02:28 -070063 }
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010064 if (start_bad)
65 reserve_bad_mem(pattern, start_bad, last_bad + incr);
66 start_bad = last_bad = start_phys_aligned;
Yinghai Lu1f067162008-07-15 00:02:28 -070067 }
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010068 if (start_bad)
69 reserve_bad_mem(pattern, start_bad, last_bad + incr);
Yinghai Lu1f067162008-07-15 00:02:28 -070070}
71
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070072static void __init do_one_pass(u64 pattern, phys_addr_t start, phys_addr_t end)
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +010073{
Tejun Heo8d89ac82011-07-12 11:16:00 +020074 u64 i;
75 phys_addr_t this_start, this_end;
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +010076
Grygorii Strashko9a28f9d2014-01-21 15:50:53 -080077 for_each_free_mem_range(i, NUMA_NO_NODE, &this_start, &this_end, NULL) {
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070078 this_start = clamp(this_start, start, end);
79 this_end = clamp(this_end, start, end);
Tejun Heo8d89ac82011-07-12 11:16:00 +020080 if (this_start < this_end) {
81 printk(KERN_INFO " %010llx - %010llx pattern %016llx\n",
82 (unsigned long long)this_start,
83 (unsigned long long)this_end,
84 (unsigned long long)cpu_to_be64(pattern));
85 memtest(pattern, this_start, this_end - this_start);
86 }
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +010087 }
88}
89
Yinghai Lu1f067162008-07-15 00:02:28 -070090/* default is disabled */
91static int memtest_pattern __initdata;
92
93static int __init parse_memtest(char *arg)
94{
95 if (arg)
96 memtest_pattern = simple_strtoul(arg, NULL, 0);
Yinghai Lud1a8e772009-03-06 03:12:50 -080097 else
98 memtest_pattern = ARRAY_SIZE(patterns);
99
Yinghai Lu1f067162008-07-15 00:02:28 -0700100 return 0;
101}
102
103early_param("memtest", parse_memtest);
104
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700105void __init early_memtest(phys_addr_t start, phys_addr_t end)
Yinghai Lu1f067162008-07-15 00:02:28 -0700106{
Andreas Herrmann6d741712009-02-25 11:27:27 +0100107 unsigned int i;
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +0100108 unsigned int idx = 0;
Yinghai Lu1f067162008-07-15 00:02:28 -0700109
110 if (!memtest_pattern)
111 return;
112
Andreas Herrmann570c9e62009-02-25 11:28:58 +0100113 printk(KERN_INFO "early_memtest: # of tests: %d\n", memtest_pattern);
Alexander Holler20bf0622013-02-16 16:38:17 +0100114 for (i = memtest_pattern-1; i < UINT_MAX; --i) {
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +0100115 idx = i % ARRAY_SIZE(patterns);
116 do_one_pass(patterns[idx], start, end);
117 }
Yinghai Lu1f067162008-07-15 00:02:28 -0700118}