blob: 5da1269e8ddcbcb55ff840d90dd912502fd582d5 [file] [log] [blame]
Arjan van de Ven6784f7d2008-10-05 11:33:42 -07001#include <linux/module.h>
2#include <linux/sched.h>
Arjan van de Ven304e6292008-10-05 12:09:03 -07003#include <linux/kthread.h>
4#include <linux/workqueue.h>
Yinghai Lu72d7c3b2010-08-25 13:39:17 -07005#include <linux/memblock.h>
6
Arjan van de Ven6784f7d2008-10-05 11:33:42 -07007#include <asm/proto.h>
8
9/*
10 * Some BIOSes seem to corrupt the low 64k of memory during events
11 * like suspend/resume and unplugging an HDMI cable. Reserve all
12 * remaining free memory in that area and fill it with a distinct
13 * pattern.
14 */
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070015#define MAX_SCAN_AREAS 8
16
17static int __read_mostly memory_corruption_check = -1;
18
19static unsigned __read_mostly corruption_check_size = 64*1024;
20static unsigned __read_mostly corruption_check_period = 60; /* seconds */
21
Yinghai Lu72d7c3b2010-08-25 13:39:17 -070022static struct scan_area {
23 u64 addr;
24 u64 size;
25} scan_areas[MAX_SCAN_AREAS];
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070026static int num_scan_areas;
27
Arjan van de Venb43d1962008-10-05 12:21:32 -070028static __init int set_corruption_check(char *arg)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070029{
30 char *end;
31
32 memory_corruption_check = simple_strtol(arg, &end, 10);
33
34 return (*end == 0) ? 0 : -EINVAL;
35}
36early_param("memory_corruption_check", set_corruption_check);
37
Arjan van de Venb43d1962008-10-05 12:21:32 -070038static __init int set_corruption_check_period(char *arg)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070039{
40 char *end;
41
42 corruption_check_period = simple_strtoul(arg, &end, 10);
43
44 return (*end == 0) ? 0 : -EINVAL;
45}
46early_param("memory_corruption_check_period", set_corruption_check_period);
47
Arjan van de Venb43d1962008-10-05 12:21:32 -070048static __init int set_corruption_check_size(char *arg)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070049{
50 char *end;
51 unsigned size;
52
53 size = memparse(arg, &end);
54
55 if (*end == '\0')
56 corruption_check_size = size;
57
58 return (size == corruption_check_size) ? 0 : -EINVAL;
59}
60early_param("memory_corruption_check_size", set_corruption_check_size);
61
62
63void __init setup_bios_corruption_check(void)
64{
Tejun Heo8d89ac82011-07-12 11:16:00 +020065 phys_addr_t start, end;
66 u64 i;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070067
68 if (memory_corruption_check == -1) {
69 memory_corruption_check =
70#ifdef CONFIG_X86_BOOTPARAM_MEMORY_CORRUPTION_CHECK
71 1
72#else
73 0
74#endif
75 ;
76 }
77
78 if (corruption_check_size == 0)
79 memory_corruption_check = 0;
80
81 if (!memory_corruption_check)
82 return;
83
84 corruption_check_size = round_up(corruption_check_size, PAGE_SIZE);
85
Tejun Heo8d89ac82011-07-12 11:16:00 +020086 for_each_free_mem_range(i, MAX_NUMNODES, &start, &end, NULL) {
87 start = clamp_t(phys_addr_t, round_up(start, PAGE_SIZE),
88 PAGE_SIZE, corruption_check_size);
89 end = clamp_t(phys_addr_t, round_down(end, PAGE_SIZE),
90 PAGE_SIZE, corruption_check_size);
91 if (start >= end)
92 continue;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070093
Tejun Heo24aa0782011-07-12 11:16:06 +020094 memblock_reserve(start, end - start);
Tejun Heo8d89ac82011-07-12 11:16:00 +020095 scan_areas[num_scan_areas].addr = start;
96 scan_areas[num_scan_areas].size = end - start;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070097
98 /* Assume we've already mapped this early memory */
Tejun Heo8d89ac82011-07-12 11:16:00 +020099 memset(__va(start), 0, end - start);
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700100
Tejun Heo8d89ac82011-07-12 11:16:00 +0200101 if (++num_scan_areas >= MAX_SCAN_AREAS)
102 break;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700103 }
104
Naga Chumbalkara7bd1da2011-02-25 20:31:55 +0000105 if (num_scan_areas)
106 printk(KERN_INFO "Scanning %d areas for low memory corruption\n", num_scan_areas);
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700107}
108
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700109
110void check_for_bios_corruption(void)
111{
112 int i;
113 int corruption = 0;
114
115 if (!memory_corruption_check)
116 return;
117
118 for (i = 0; i < num_scan_areas; i++) {
119 unsigned long *addr = __va(scan_areas[i].addr);
120 unsigned long size = scan_areas[i].size;
121
122 for (; size; addr++, size -= sizeof(unsigned long)) {
123 if (!*addr)
124 continue;
125 printk(KERN_ERR "Corrupted low memory at %p (%lx phys) = %08lx\n",
126 addr, __pa(addr), *addr);
127 corruption = 1;
128 *addr = 0;
129 }
130 }
131
Arjan van de Venb43d1962008-10-05 12:21:32 -0700132 WARN_ONCE(corruption, KERN_ERR "Memory corruption detected in low memory\n");
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700133}
134
Arjan van de Ven304e6292008-10-05 12:09:03 -0700135static void check_corruption(struct work_struct *dummy);
136static DECLARE_DELAYED_WORK(bios_check_work, check_corruption);
137
138static void check_corruption(struct work_struct *dummy)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700139{
140 check_for_bios_corruption();
Arjan van de Ven304e6292008-10-05 12:09:03 -0700141 schedule_delayed_work(&bios_check_work,
Naga Chumbalkara7bd1da2011-02-25 20:31:55 +0000142 round_jiffies_relative(corruption_check_period*HZ));
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700143}
144
Arjan van de Ven304e6292008-10-05 12:09:03 -0700145static int start_periodic_check_for_corruption(void)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700146{
Naga Chumbalkara7bd1da2011-02-25 20:31:55 +0000147 if (!num_scan_areas || !memory_corruption_check || corruption_check_period == 0)
Arjan van de Ven304e6292008-10-05 12:09:03 -0700148 return 0;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700149
150 printk(KERN_INFO "Scanning for low memory corruption every %d seconds\n",
151 corruption_check_period);
152
Arjan van de Ven304e6292008-10-05 12:09:03 -0700153 /* First time we run the checks right away */
154 schedule_delayed_work(&bios_check_work, 0);
155 return 0;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700156}
Arjan van de Ven304e6292008-10-05 12:09:03 -0700157
158module_init(start_periodic_check_for_corruption);
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700159