Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 1 | #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 Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame^] | 9 | #include <linux/memblock.h> |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 10 | |
Andreas Herrmann | 6d74171 | 2009-02-25 11:27:27 +0100 | [diff] [blame] | 11 | static u64 patterns[] __initdata = { |
| 12 | 0, |
| 13 | 0xffffffffffffffffULL, |
| 14 | 0x5555555555555555ULL, |
| 15 | 0xaaaaaaaaaaaaaaaaULL, |
Andreas Herrmann | 6382312 | 2009-02-25 11:31:49 +0100 | [diff] [blame] | 16 | 0x1111111111111111ULL, |
| 17 | 0x2222222222222222ULL, |
| 18 | 0x4444444444444444ULL, |
| 19 | 0x8888888888888888ULL, |
| 20 | 0x3333333333333333ULL, |
| 21 | 0x6666666666666666ULL, |
| 22 | 0x9999999999999999ULL, |
| 23 | 0xccccccccccccccccULL, |
| 24 | 0x7777777777777777ULL, |
| 25 | 0xbbbbbbbbbbbbbbbbULL, |
| 26 | 0xddddddddddddddddULL, |
| 27 | 0xeeeeeeeeeeeeeeeeULL, |
| 28 | 0x7a6c7258554e494cULL, /* yeah ;-) */ |
Andreas Herrmann | 6d74171 | 2009-02-25 11:27:27 +0100 | [diff] [blame] | 29 | }; |
Andreas Herrmann | 40823f7 | 2009-02-25 11:26:18 +0100 | [diff] [blame] | 30 | |
Andreas Herrmann | 570c9e6 | 2009-02-25 11:28:58 +0100 | [diff] [blame] | 31 | static void __init reserve_bad_mem(u64 pattern, u64 start_bad, u64 end_bad) |
Andreas Herrmann | 7dad169e | 2009-02-25 11:28:07 +0100 | [diff] [blame] | 32 | { |
Andreas Herrmann | 570c9e6 | 2009-02-25 11:28:58 +0100 | [diff] [blame] | 33 | printk(KERN_INFO " %016llx bad mem addr %010llx - %010llx reserved\n", |
| 34 | (unsigned long long) pattern, |
| 35 | (unsigned long long) start_bad, |
| 36 | (unsigned long long) end_bad); |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame^] | 37 | memblock_x86_reserve_range(start_bad, end_bad, "BAD RAM"); |
Andreas Herrmann | 7dad169e | 2009-02-25 11:28:07 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Andreas Herrmann | 570c9e6 | 2009-02-25 11:28:58 +0100 | [diff] [blame] | 40 | static void __init memtest(u64 pattern, u64 start_phys, u64 size) |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 41 | { |
Thomas Gleixner | 9866b7e | 2009-06-11 16:25:01 +0200 | [diff] [blame] | 42 | u64 *p, *start, *end; |
Andreas Herrmann | 570c9e6 | 2009-02-25 11:28:58 +0100 | [diff] [blame] | 43 | u64 start_bad, last_bad; |
| 44 | u64 start_phys_aligned; |
Thomas Gleixner | 9866b7e | 2009-06-11 16:25:01 +0200 | [diff] [blame] | 45 | const size_t incr = sizeof(pattern); |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 46 | |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 47 | start_phys_aligned = ALIGN(start_phys, incr); |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 48 | start = __va(start_phys_aligned); |
Thomas Gleixner | 9866b7e | 2009-06-11 16:25:01 +0200 | [diff] [blame] | 49 | end = start + (size - (start_phys_aligned - start_phys)) / incr; |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 50 | start_bad = 0; |
| 51 | last_bad = 0; |
| 52 | |
Andreas Herrmann | c969099 | 2009-06-08 19:09:39 +0200 | [diff] [blame] | 53 | for (p = start; p < end; p++) |
| 54 | *p = pattern; |
Thomas Gleixner | 9866b7e | 2009-06-11 16:25:01 +0200 | [diff] [blame] | 55 | |
Andreas Herrmann | c969099 | 2009-06-08 19:09:39 +0200 | [diff] [blame] | 56 | for (p = start; p < end; p++, start_phys_aligned += incr) { |
| 57 | if (*p == pattern) |
Andreas Herrmann | 7dad169e | 2009-02-25 11:28:07 +0100 | [diff] [blame] | 58 | continue; |
| 59 | if (start_phys_aligned == last_bad + incr) { |
| 60 | last_bad += incr; |
| 61 | continue; |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 62 | } |
Andreas Herrmann | 7dad169e | 2009-02-25 11:28:07 +0100 | [diff] [blame] | 63 | if (start_bad) |
| 64 | reserve_bad_mem(pattern, start_bad, last_bad + incr); |
| 65 | start_bad = last_bad = start_phys_aligned; |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 66 | } |
Andreas Herrmann | 7dad169e | 2009-02-25 11:28:07 +0100 | [diff] [blame] | 67 | if (start_bad) |
| 68 | reserve_bad_mem(pattern, start_bad, last_bad + incr); |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Andreas Herrmann | bfb4dc0 | 2009-02-25 11:30:04 +0100 | [diff] [blame] | 71 | static void __init do_one_pass(u64 pattern, u64 start, u64 end) |
| 72 | { |
| 73 | u64 size = 0; |
| 74 | |
| 75 | while (start < end) { |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame^] | 76 | start = memblock_x86_find_in_range_size(start, &size, 1); |
Andreas Herrmann | bfb4dc0 | 2009-02-25 11:30:04 +0100 | [diff] [blame] | 77 | |
| 78 | /* done ? */ |
| 79 | if (start >= end) |
| 80 | break; |
| 81 | if (start + size > end) |
| 82 | size = end - start; |
| 83 | |
| 84 | printk(KERN_INFO " %010llx - %010llx pattern %016llx\n", |
| 85 | (unsigned long long) start, |
| 86 | (unsigned long long) start + size, |
| 87 | (unsigned long long) cpu_to_be64(pattern)); |
| 88 | memtest(pattern, start, size); |
| 89 | |
| 90 | start += size; |
| 91 | } |
| 92 | } |
| 93 | |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 94 | /* default is disabled */ |
| 95 | static int memtest_pattern __initdata; |
| 96 | |
| 97 | static int __init parse_memtest(char *arg) |
| 98 | { |
| 99 | if (arg) |
| 100 | memtest_pattern = simple_strtoul(arg, NULL, 0); |
Yinghai Lu | d1a8e77 | 2009-03-06 03:12:50 -0800 | [diff] [blame] | 101 | else |
| 102 | memtest_pattern = ARRAY_SIZE(patterns); |
| 103 | |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | early_param("memtest", parse_memtest); |
| 108 | |
| 109 | void __init early_memtest(unsigned long start, unsigned long end) |
| 110 | { |
Andreas Herrmann | 6d74171 | 2009-02-25 11:27:27 +0100 | [diff] [blame] | 111 | unsigned int i; |
Andreas Herrmann | bfb4dc0 | 2009-02-25 11:30:04 +0100 | [diff] [blame] | 112 | unsigned int idx = 0; |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 113 | |
| 114 | if (!memtest_pattern) |
| 115 | return; |
| 116 | |
Andreas Herrmann | 570c9e6 | 2009-02-25 11:28:58 +0100 | [diff] [blame] | 117 | printk(KERN_INFO "early_memtest: # of tests: %d\n", memtest_pattern); |
Andreas Herrmann | 6d74171 | 2009-02-25 11:27:27 +0100 | [diff] [blame] | 118 | for (i = 0; i < memtest_pattern; i++) { |
Andreas Herrmann | bfb4dc0 | 2009-02-25 11:30:04 +0100 | [diff] [blame] | 119 | idx = i % ARRAY_SIZE(patterns); |
| 120 | do_one_pass(patterns[idx], start, end); |
| 121 | } |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 122 | |
Andreas Herrmann | bfb4dc0 | 2009-02-25 11:30:04 +0100 | [diff] [blame] | 123 | if (idx > 0) { |
| 124 | printk(KERN_INFO "early_memtest: wipe out " |
| 125 | "test pattern from memory\n"); |
| 126 | /* additional test with pattern 0 will do this */ |
| 127 | do_one_pass(0, start, end); |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 128 | } |
Yinghai Lu | 1f06716 | 2008-07-15 00:02:28 -0700 | [diff] [blame] | 129 | } |