blob: 6bcf7d89adfc8818d4aeab14261b545f9f2b4fd5 [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{
Vladimir Murzinf373baf2015-09-08 15:00:19 -070034 pr_info(" %016llx bad mem addr %pa - %pa reserved\n",
35 cpu_to_be64(pattern), &start_bad, &end_bad);
Tejun Heo24aa0782011-07-12 11:16:06 +020036 memblock_reserve(start_bad, end_bad - start_bad);
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010037}
38
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070039static void __init memtest(u64 pattern, phys_addr_t start_phys, phys_addr_t size)
Yinghai Lu1f067162008-07-15 00:02:28 -070040{
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020041 u64 *p, *start, *end;
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070042 phys_addr_t start_bad, last_bad;
43 phys_addr_t start_phys_aligned;
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020044 const size_t incr = sizeof(pattern);
Yinghai Lu1f067162008-07-15 00:02:28 -070045
Yinghai Lu1f067162008-07-15 00:02:28 -070046 start_phys_aligned = ALIGN(start_phys, incr);
Yinghai Lu1f067162008-07-15 00:02:28 -070047 start = __va(start_phys_aligned);
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020048 end = start + (size - (start_phys_aligned - start_phys)) / incr;
Yinghai Lu1f067162008-07-15 00:02:28 -070049 start_bad = 0;
50 last_bad = 0;
51
Andreas Herrmannc9690992009-06-08 19:09:39 +020052 for (p = start; p < end; p++)
53 *p = pattern;
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020054
Andreas Herrmannc9690992009-06-08 19:09:39 +020055 for (p = start; p < end; p++, start_phys_aligned += incr) {
56 if (*p == pattern)
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010057 continue;
58 if (start_phys_aligned == last_bad + incr) {
59 last_bad += incr;
60 continue;
Yinghai Lu1f067162008-07-15 00:02:28 -070061 }
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010062 if (start_bad)
63 reserve_bad_mem(pattern, start_bad, last_bad + incr);
64 start_bad = last_bad = start_phys_aligned;
Yinghai Lu1f067162008-07-15 00:02:28 -070065 }
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010066 if (start_bad)
67 reserve_bad_mem(pattern, start_bad, last_bad + incr);
Yinghai Lu1f067162008-07-15 00:02:28 -070068}
69
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070070static void __init do_one_pass(u64 pattern, phys_addr_t start, phys_addr_t end)
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +010071{
Tejun Heo8d89ac82011-07-12 11:16:00 +020072 u64 i;
73 phys_addr_t this_start, this_end;
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +010074
Tony Luckfc6daaf2015-06-24 16:58:09 -070075 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &this_start,
76 &this_end, NULL) {
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070077 this_start = clamp(this_start, start, end);
78 this_end = clamp(this_end, start, end);
Tejun Heo8d89ac82011-07-12 11:16:00 +020079 if (this_start < this_end) {
Vladimir Murzinf373baf2015-09-08 15:00:19 -070080 pr_info(" %pa - %pa pattern %016llx\n",
81 &this_start, &this_end, cpu_to_be64(pattern));
Tejun Heo8d89ac82011-07-12 11:16:00 +020082 memtest(pattern, this_start, this_end - this_start);
83 }
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +010084 }
85}
86
Yinghai Lu1f067162008-07-15 00:02:28 -070087/* default is disabled */
Vladimir Murzin06f80592015-09-08 15:00:16 -070088static unsigned int memtest_pattern __initdata;
Yinghai Lu1f067162008-07-15 00:02:28 -070089
90static int __init parse_memtest(char *arg)
91{
Vladimir Murzin06f80592015-09-08 15:00:16 -070092 int ret = 0;
93
Yinghai Lu1f067162008-07-15 00:02:28 -070094 if (arg)
Vladimir Murzin06f80592015-09-08 15:00:16 -070095 ret = kstrtouint(arg, 0, &memtest_pattern);
Yinghai Lud1a8e772009-03-06 03:12:50 -080096 else
97 memtest_pattern = ARRAY_SIZE(patterns);
98
Vladimir Murzin06f80592015-09-08 15:00:16 -070099 return ret;
Yinghai Lu1f067162008-07-15 00:02:28 -0700100}
101
102early_param("memtest", parse_memtest);
103
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700104void __init early_memtest(phys_addr_t start, phys_addr_t end)
Yinghai Lu1f067162008-07-15 00:02:28 -0700105{
Andreas Herrmann6d741712009-02-25 11:27:27 +0100106 unsigned int i;
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +0100107 unsigned int idx = 0;
Yinghai Lu1f067162008-07-15 00:02:28 -0700108
109 if (!memtest_pattern)
110 return;
111
Vladimir Murzinf373baf2015-09-08 15:00:19 -0700112 pr_info("early_memtest: # of tests: %u\n", memtest_pattern);
Alexander Holler20bf0622013-02-16 16:38:17 +0100113 for (i = memtest_pattern-1; i < UINT_MAX; --i) {
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +0100114 idx = i % ARRAY_SIZE(patterns);
115 do_one_pass(patterns[idx], start, end);
116 }
Yinghai Lu1f067162008-07-15 00:02:28 -0700117}