blob: f53ace709ccd82d207bae22679887c1da1e3b694 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yinghai Lu1f067162008-07-15 00:02:28 -07002#include <linux/kernel.h>
Yinghai Lu1f067162008-07-15 00:02:28 -07003#include <linux/types.h>
Yinghai Lu1f067162008-07-15 00:02:28 -07004#include <linux/init.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -07005#include <linux/memblock.h>
Yinghai Lu1f067162008-07-15 00:02:28 -07006
Andreas Herrmann6d741712009-02-25 11:27:27 +01007static u64 patterns[] __initdata = {
Alexander Holler20bf0622013-02-16 16:38:17 +01008 /* The first entry has to be 0 to leave memtest with zeroed memory */
Andreas Herrmann6d741712009-02-25 11:27:27 +01009 0,
10 0xffffffffffffffffULL,
11 0x5555555555555555ULL,
12 0xaaaaaaaaaaaaaaaaULL,
Andreas Herrmann63823122009-02-25 11:31:49 +010013 0x1111111111111111ULL,
14 0x2222222222222222ULL,
15 0x4444444444444444ULL,
16 0x8888888888888888ULL,
17 0x3333333333333333ULL,
18 0x6666666666666666ULL,
19 0x9999999999999999ULL,
20 0xccccccccccccccccULL,
21 0x7777777777777777ULL,
22 0xbbbbbbbbbbbbbbbbULL,
23 0xddddddddddddddddULL,
24 0xeeeeeeeeeeeeeeeeULL,
25 0x7a6c7258554e494cULL, /* yeah ;-) */
Andreas Herrmann6d741712009-02-25 11:27:27 +010026};
Andreas Herrmann40823f72009-02-25 11:26:18 +010027
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070028static void __init reserve_bad_mem(u64 pattern, phys_addr_t start_bad, phys_addr_t end_bad)
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010029{
Vladimir Murzinf373baf2015-09-08 15:00:19 -070030 pr_info(" %016llx bad mem addr %pa - %pa reserved\n",
31 cpu_to_be64(pattern), &start_bad, &end_bad);
Tejun Heo24aa0782011-07-12 11:16:06 +020032 memblock_reserve(start_bad, end_bad - start_bad);
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010033}
34
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070035static void __init memtest(u64 pattern, phys_addr_t start_phys, phys_addr_t size)
Yinghai Lu1f067162008-07-15 00:02:28 -070036{
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020037 u64 *p, *start, *end;
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070038 phys_addr_t start_bad, last_bad;
39 phys_addr_t start_phys_aligned;
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020040 const size_t incr = sizeof(pattern);
Yinghai Lu1f067162008-07-15 00:02:28 -070041
Yinghai Lu1f067162008-07-15 00:02:28 -070042 start_phys_aligned = ALIGN(start_phys, incr);
Yinghai Lu1f067162008-07-15 00:02:28 -070043 start = __va(start_phys_aligned);
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020044 end = start + (size - (start_phys_aligned - start_phys)) / incr;
Yinghai Lu1f067162008-07-15 00:02:28 -070045 start_bad = 0;
46 last_bad = 0;
47
Andreas Herrmannc9690992009-06-08 19:09:39 +020048 for (p = start; p < end; p++)
49 *p = pattern;
Thomas Gleixner9866b7e2009-06-11 16:25:01 +020050
Andreas Herrmannc9690992009-06-08 19:09:39 +020051 for (p = start; p < end; p++, start_phys_aligned += incr) {
52 if (*p == pattern)
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010053 continue;
54 if (start_phys_aligned == last_bad + incr) {
55 last_bad += incr;
56 continue;
Yinghai Lu1f067162008-07-15 00:02:28 -070057 }
Andreas Herrmann7dad169e2009-02-25 11:28:07 +010058 if (start_bad)
59 reserve_bad_mem(pattern, start_bad, last_bad + incr);
60 start_bad = last_bad = start_phys_aligned;
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);
Yinghai Lu1f067162008-07-15 00:02:28 -070064}
65
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070066static void __init do_one_pass(u64 pattern, phys_addr_t start, phys_addr_t end)
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +010067{
Tejun Heo8d89ac82011-07-12 11:16:00 +020068 u64 i;
69 phys_addr_t this_start, this_end;
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +010070
Tony Luckfc6daaf2015-06-24 16:58:09 -070071 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &this_start,
72 &this_end, NULL) {
Vladimir Murzin7f70bae2015-04-14 15:48:30 -070073 this_start = clamp(this_start, start, end);
74 this_end = clamp(this_end, start, end);
Tejun Heo8d89ac82011-07-12 11:16:00 +020075 if (this_start < this_end) {
Vladimir Murzinf373baf2015-09-08 15:00:19 -070076 pr_info(" %pa - %pa pattern %016llx\n",
77 &this_start, &this_end, cpu_to_be64(pattern));
Tejun Heo8d89ac82011-07-12 11:16:00 +020078 memtest(pattern, this_start, this_end - this_start);
79 }
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +010080 }
81}
82
Yinghai Lu1f067162008-07-15 00:02:28 -070083/* default is disabled */
Vladimir Murzin06f80592015-09-08 15:00:16 -070084static unsigned int memtest_pattern __initdata;
Yinghai Lu1f067162008-07-15 00:02:28 -070085
86static int __init parse_memtest(char *arg)
87{
Vladimir Murzin06f80592015-09-08 15:00:16 -070088 int ret = 0;
89
Yinghai Lu1f067162008-07-15 00:02:28 -070090 if (arg)
Vladimir Murzin06f80592015-09-08 15:00:16 -070091 ret = kstrtouint(arg, 0, &memtest_pattern);
Yinghai Lud1a8e772009-03-06 03:12:50 -080092 else
93 memtest_pattern = ARRAY_SIZE(patterns);
94
Vladimir Murzin06f80592015-09-08 15:00:16 -070095 return ret;
Yinghai Lu1f067162008-07-15 00:02:28 -070096}
97
98early_param("memtest", parse_memtest);
99
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700100void __init early_memtest(phys_addr_t start, phys_addr_t end)
Yinghai Lu1f067162008-07-15 00:02:28 -0700101{
Andreas Herrmann6d741712009-02-25 11:27:27 +0100102 unsigned int i;
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +0100103 unsigned int idx = 0;
Yinghai Lu1f067162008-07-15 00:02:28 -0700104
105 if (!memtest_pattern)
106 return;
107
Vladimir Murzinf373baf2015-09-08 15:00:19 -0700108 pr_info("early_memtest: # of tests: %u\n", memtest_pattern);
Alexander Holler20bf0622013-02-16 16:38:17 +0100109 for (i = memtest_pattern-1; i < UINT_MAX; --i) {
Andreas Herrmannbfb4dc02009-02-25 11:30:04 +0100110 idx = i % ARRAY_SIZE(patterns);
111 do_one_pass(patterns[idx], start, end);
112 }
Yinghai Lu1f067162008-07-15 00:02:28 -0700113}