blob: 145863d4d343c4138c167228550e4a0a6a9363a4 [file] [log] [blame]
Paul Gortmaker13fe86f42015-08-24 19:34:55 -04001#include <linux/init.h>
Arjan van de Ven6784f7d2008-10-05 11:33:42 -07002#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{
Shuah Khan5abe68e2012-05-06 11:55:08 -060030 ssize_t ret;
31 unsigned long val;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070032
Shuah Khan5abe68e2012-05-06 11:55:08 -060033 ret = kstrtoul(arg, 10, &val);
34 if (ret)
35 return ret;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070036
Shuah Khan5abe68e2012-05-06 11:55:08 -060037 memory_corruption_check = val;
38 return 0;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070039}
40early_param("memory_corruption_check", set_corruption_check);
41
Arjan van de Venb43d1962008-10-05 12:21:32 -070042static __init int set_corruption_check_period(char *arg)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070043{
Shuah Khan5abe68e2012-05-06 11:55:08 -060044 ssize_t ret;
45 unsigned long val;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070046
Shuah Khan5abe68e2012-05-06 11:55:08 -060047 ret = kstrtoul(arg, 10, &val);
48 if (ret)
49 return ret;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070050
Shuah Khan5abe68e2012-05-06 11:55:08 -060051 corruption_check_period = val;
52 return 0;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070053}
54early_param("memory_corruption_check_period", set_corruption_check_period);
55
Arjan van de Venb43d1962008-10-05 12:21:32 -070056static __init int set_corruption_check_size(char *arg)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070057{
58 char *end;
59 unsigned size;
60
61 size = memparse(arg, &end);
62
63 if (*end == '\0')
64 corruption_check_size = size;
65
66 return (size == corruption_check_size) ? 0 : -EINVAL;
67}
68early_param("memory_corruption_check_size", set_corruption_check_size);
69
70
71void __init setup_bios_corruption_check(void)
72{
Tejun Heo8d89ac82011-07-12 11:16:00 +020073 phys_addr_t start, end;
74 u64 i;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -070075
76 if (memory_corruption_check == -1) {
77 memory_corruption_check =
78#ifdef CONFIG_X86_BOOTPARAM_MEMORY_CORRUPTION_CHECK
79 1
80#else
81 0
82#endif
83 ;
84 }
85
86 if (corruption_check_size == 0)
87 memory_corruption_check = 0;
88
89 if (!memory_corruption_check)
90 return;
91
92 corruption_check_size = round_up(corruption_check_size, PAGE_SIZE);
93
Tony Luckfc6daaf2015-06-24 16:58:09 -070094 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
95 NULL) {
Tejun Heo8d89ac82011-07-12 11:16:00 +020096 start = clamp_t(phys_addr_t, round_up(start, PAGE_SIZE),
97 PAGE_SIZE, corruption_check_size);
98 end = clamp_t(phys_addr_t, round_down(end, PAGE_SIZE),
99 PAGE_SIZE, corruption_check_size);
100 if (start >= end)
101 continue;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700102
Tejun Heo24aa0782011-07-12 11:16:06 +0200103 memblock_reserve(start, end - start);
Tejun Heo8d89ac82011-07-12 11:16:00 +0200104 scan_areas[num_scan_areas].addr = start;
105 scan_areas[num_scan_areas].size = end - start;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700106
107 /* Assume we've already mapped this early memory */
Tejun Heo8d89ac82011-07-12 11:16:00 +0200108 memset(__va(start), 0, end - start);
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700109
Tejun Heo8d89ac82011-07-12 11:16:00 +0200110 if (++num_scan_areas >= MAX_SCAN_AREAS)
111 break;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700112 }
113
Naga Chumbalkara7bd1da2011-02-25 20:31:55 +0000114 if (num_scan_areas)
115 printk(KERN_INFO "Scanning %d areas for low memory corruption\n", num_scan_areas);
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700116}
117
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700118
119void check_for_bios_corruption(void)
120{
121 int i;
122 int corruption = 0;
123
124 if (!memory_corruption_check)
125 return;
126
127 for (i = 0; i < num_scan_areas; i++) {
128 unsigned long *addr = __va(scan_areas[i].addr);
129 unsigned long size = scan_areas[i].size;
130
131 for (; size; addr++, size -= sizeof(unsigned long)) {
132 if (!*addr)
133 continue;
134 printk(KERN_ERR "Corrupted low memory at %p (%lx phys) = %08lx\n",
135 addr, __pa(addr), *addr);
136 corruption = 1;
137 *addr = 0;
138 }
139 }
140
Arjan van de Venb43d1962008-10-05 12:21:32 -0700141 WARN_ONCE(corruption, KERN_ERR "Memory corruption detected in low memory\n");
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700142}
143
Arjan van de Ven304e6292008-10-05 12:09:03 -0700144static void check_corruption(struct work_struct *dummy);
145static DECLARE_DELAYED_WORK(bios_check_work, check_corruption);
146
147static void check_corruption(struct work_struct *dummy)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700148{
149 check_for_bios_corruption();
Arjan van de Ven304e6292008-10-05 12:09:03 -0700150 schedule_delayed_work(&bios_check_work,
Naga Chumbalkara7bd1da2011-02-25 20:31:55 +0000151 round_jiffies_relative(corruption_check_period*HZ));
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700152}
153
Arjan van de Ven304e6292008-10-05 12:09:03 -0700154static int start_periodic_check_for_corruption(void)
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700155{
Naga Chumbalkara7bd1da2011-02-25 20:31:55 +0000156 if (!num_scan_areas || !memory_corruption_check || corruption_check_period == 0)
Arjan van de Ven304e6292008-10-05 12:09:03 -0700157 return 0;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700158
159 printk(KERN_INFO "Scanning for low memory corruption every %d seconds\n",
160 corruption_check_period);
161
Arjan van de Ven304e6292008-10-05 12:09:03 -0700162 /* First time we run the checks right away */
163 schedule_delayed_work(&bios_check_work, 0);
164 return 0;
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700165}
Paul Gortmaker13fe86f42015-08-24 19:34:55 -0400166device_initcall(start_periodic_check_for_corruption);
Arjan van de Ven6784f7d2008-10-05 11:33:42 -0700167