blob: 3232397783aef87ae73850399fc3f115b3220d71 [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>
9
10#include <asm/e820.h>
11
Andreas Herrmann6d741712009-02-25 11:27:27 +010012static u64 patterns[] __initdata = {
13 0,
14 0xffffffffffffffffULL,
15 0x5555555555555555ULL,
16 0xaaaaaaaaaaaaaaaaULL,
17};
Andreas Herrmann40823f72009-02-25 11:26:18 +010018
Andreas Herrmann570c9e62009-02-25 11:28:58 +010019static void __init reserve_bad_mem(u64 pattern, u64 start_bad, u64 end_bad)
Andreas Herrmann7dad1692009-02-25 11:28:07 +010020{
Andreas Herrmann570c9e62009-02-25 11:28:58 +010021 printk(KERN_INFO " %016llx bad mem addr %010llx - %010llx reserved\n",
22 (unsigned long long) pattern,
23 (unsigned long long) start_bad,
24 (unsigned long long) end_bad);
Andreas Herrmann7dad1692009-02-25 11:28:07 +010025 reserve_early(start_bad, end_bad, "BAD RAM");
26}
27
Andreas Herrmann570c9e62009-02-25 11:28:58 +010028static void __init memtest(u64 pattern, u64 start_phys, u64 size)
Yinghai Lu1f067162008-07-15 00:02:28 -070029{
Andreas Herrmann570c9e62009-02-25 11:28:58 +010030 u64 i, count;
Andreas Herrmann6d741712009-02-25 11:27:27 +010031 u64 *start;
Andreas Herrmann570c9e62009-02-25 11:28:58 +010032 u64 start_bad, last_bad;
33 u64 start_phys_aligned;
34 size_t incr;
Yinghai Lu1f067162008-07-15 00:02:28 -070035
Andreas Herrmann6d741712009-02-25 11:27:27 +010036 incr = sizeof(pattern);
Yinghai Lu1f067162008-07-15 00:02:28 -070037 start_phys_aligned = ALIGN(start_phys, incr);
38 count = (size - (start_phys_aligned - start_phys))/incr;
39 start = __va(start_phys_aligned);
40 start_bad = 0;
41 last_bad = 0;
42
43 for (i = 0; i < count; i++)
Andreas Herrmann6d741712009-02-25 11:27:27 +010044 start[i] = pattern;
Yinghai Lu1f067162008-07-15 00:02:28 -070045 for (i = 0; i < count; i++, start++, start_phys_aligned += incr) {
Andreas Herrmann7dad1692009-02-25 11:28:07 +010046 if (*start == pattern)
47 continue;
48 if (start_phys_aligned == last_bad + incr) {
49 last_bad += incr;
50 continue;
Yinghai Lu1f067162008-07-15 00:02:28 -070051 }
Andreas Herrmann7dad1692009-02-25 11:28:07 +010052 if (start_bad)
53 reserve_bad_mem(pattern, start_bad, last_bad + incr);
54 start_bad = last_bad = start_phys_aligned;
Yinghai Lu1f067162008-07-15 00:02:28 -070055 }
Andreas Herrmann7dad1692009-02-25 11:28:07 +010056 if (start_bad)
57 reserve_bad_mem(pattern, start_bad, last_bad + incr);
Yinghai Lu1f067162008-07-15 00:02:28 -070058}
59
60/* default is disabled */
61static int memtest_pattern __initdata;
62
63static int __init parse_memtest(char *arg)
64{
65 if (arg)
66 memtest_pattern = simple_strtoul(arg, NULL, 0);
67 return 0;
68}
69
70early_param("memtest", parse_memtest);
71
72void __init early_memtest(unsigned long start, unsigned long end)
73{
74 u64 t_start, t_size;
Andreas Herrmann6d741712009-02-25 11:27:27 +010075 unsigned int i;
76 u64 pattern;
Yinghai Lu1f067162008-07-15 00:02:28 -070077
78 if (!memtest_pattern)
79 return;
80
Andreas Herrmann570c9e62009-02-25 11:28:58 +010081 printk(KERN_INFO "early_memtest: # of tests: %d\n", memtest_pattern);
Andreas Herrmann6d741712009-02-25 11:27:27 +010082 for (i = 0; i < memtest_pattern; i++) {
83 unsigned int idx = i % ARRAY_SIZE(patterns);
84 pattern = patterns[idx];
Yinghai Lu1f067162008-07-15 00:02:28 -070085 t_start = start;
86 t_size = 0;
87 while (t_start < end) {
88 t_start = find_e820_area_size(t_start, &t_size, 1);
89
90 /* done ? */
91 if (t_start >= end)
92 break;
93 if (t_start + t_size > end)
94 t_size = end - t_start;
95
Andreas Herrmann570c9e62009-02-25 11:28:58 +010096 printk(KERN_INFO " %010llx - %010llx pattern %016llx\n",
97 (unsigned long long) t_start,
98 (unsigned long long) t_start + t_size,
99 (unsigned long long) cpu_to_be64(pattern));
100 memtest(pattern, t_start, t_size);
Yinghai Lu1f067162008-07-15 00:02:28 -0700101
102 t_start += t_size;
103 }
104 }
Yinghai Lu1f067162008-07-15 00:02:28 -0700105}