1 /* linux/arch/arm/plat-s3c/pm-check.c
2 * originally in linux/arch/arm/plat-s3c24xx/pm.c
4 * Copyright (c) 2004-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk
6 * Ben Dooks <ben@simtec.co.uk>
8 * S3C Power Mangament - suspend/resume memory corruptiuon check.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/kernel.h>
17 #include <linux/addr_overlap.h>
18 #include <linux/suspend.h>
19 #include <linux/init.h>
20 #include <linux/crc32.h>
21 #include <linux/highmem.h>
22 #include <linux/ioport.h>
23 #include <asm/memory.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/slab.h>
30 #if CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE < 1
31 #error CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE must be a positive non-zero value
34 /* suspend checking code...
36 * this next area does a set of crc checks over all the installed
37 * memory, so the system can verify if the resume was ok.
39 * CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE defines the block-size for the CRC,
40 * increasing it will mean that the area corrupted will be less easy to spot,
41 * and reducing the size will cause the CRC save area to grow
44 static bool pm_check_use_xor = 1;
45 static bool pm_check_enabled;
46 static bool pm_check_should_panic = 1;
47 static bool pm_check_print_skips;
48 static bool pm_check_print_timings;
49 static int pm_check_chunksize = CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE * 1024;
51 static u32 crc_size; /* size needed for the crc block */
52 static u32 *crcs; /* allocated over suspend/resume */
54 /* number of errors found this resume */
55 static __suspend_volatile_bss u32 crc_err_cnt;
58 typedef u32 *(run_fn_t)(struct resource *ptr, u32 *arg);
60 module_param(pm_check_use_xor, bool, S_IRUGO | S_IWUSR);
61 MODULE_PARM_DESC(pm_check_use_xor, "Use XOR checks instead of CRC");
62 module_param(pm_check_enabled, bool, S_IRUGO | S_IWUSR);
63 MODULE_PARM_DESC(pm_check_enabled,
64 "Enable memory validation on suspend/resume");
65 module_param(pm_check_should_panic, bool, S_IRUGO | S_IWUSR);
66 MODULE_PARM_DESC(pm_check_should_panic, "Panic on CRC errors");
67 module_param(pm_check_print_skips, bool, S_IRUGO | S_IWUSR);
68 MODULE_PARM_DESC(pm_check_print_skips, "Print skipped regions");
69 module_param(pm_check_print_timings, bool, S_IRUGO | S_IWUSR);
70 MODULE_PARM_DESC(pm_check_print_timings,
71 "Print time to compute checks (causes runtime warnings)");
72 module_param(pm_check_chunksize, int, S_IRUGO | S_IWUSR);
73 MODULE_PARM_DESC(pm_check_chunksize,
74 "Size of blocks for CRCs, shold be 1K multiples");
77 * s3c_pm_xor_mem() - XOR all the words in the memory range passed
78 * @val: Initial value to start the XOR from
79 * @ptr: Pointer to the start of the memory range
80 * @len: Length (in bytes) of the memory range
83 static u32 s3c_pm_xor_mem(u32 val, const u32 *ptr, size_t len)
87 len >>= 2; /* get words, not bytes */
88 for (i = 0; i < len; i++, ptr++)
94 * s3c_pm_process_mem() - Process one memory chunk for checksum
95 * @val: Starting point for checksum function
96 * @addr: Pointer to the physical memory range. This must be page-aligned.
97 * @len: Length (in bytes) of the memory range
99 * Loop through the chunk in PAGE_SIZE blocks, ensuring each
100 * page is mapped. Use either crc32 or xor checksum and return
103 static inline u32 s3c_pm_process_mem(u32 val, u32 addr, size_t len)
105 size_t processed = 0;
107 if (unlikely(addr & (PAGE_SIZE - 1))) {
108 printk(KERN_ERR "s3c_pm_check: Unaligned address: 0x%x\n",
112 while (processed < len) {
113 int pfn = __phys_to_pfn(addr + processed);
114 unsigned long left = len - processed;
117 if (left > PAGE_SIZE)
119 virt = kmap_atomic(pfn_to_page(pfn));
121 printk(KERN_ERR "s3c_pm_check: Could not map "
122 "page for pfn %d with addr 0x%x\n",
123 pfn, addr + len - processed);
126 if (pm_check_use_xor)
127 val = s3c_pm_xor_mem(val, virt, left);
129 val = crc32_le(val, virt, left);
138 * go through the given resource list, and look for system ram
141 static void s3c_pm_run_res(struct resource *ptr, run_fn_t fn, u32 *arg)
143 while (ptr != NULL) {
144 if (ptr->child != NULL)
145 s3c_pm_run_res(ptr->child, fn, arg);
147 if ((ptr->flags & IORESOURCE_MEM) &&
148 strcmp(ptr->name, "System RAM") == 0) {
149 S3C_PMDBG("s3c_pm_check: Found system RAM at "
151 (unsigned long)ptr->start,
152 (unsigned long)ptr->end);
153 arg = (fn)(ptr, arg);
160 static void s3c_pm_run_sysram(run_fn_t fn, u32 *arg)
162 s3c_pm_run_res(&iomem_resource, fn, arg);
165 static u32 *s3c_pm_countram(struct resource *res, u32 *val)
167 u32 size = (u32)resource_size(res);
169 size = DIV_ROUND_UP(size, pm_check_chunksize);
171 S3C_PMDBG("s3c_pm_check: Area %08lx..%08lx, %d blocks\n",
172 (unsigned long)res->start, (unsigned long)res->end, size);
174 *val += size * sizeof(u32);
178 /* s3c_pm_prepare_check
180 * prepare the necessary information for creating the CRCs. This
181 * must be done before the final save, as it will require memory
182 * allocating, and thus touching bits of the kernel we do not
185 void s3c_pm_check_prepare(void)
187 ktime_t start, stop, delta;
189 if (!pm_check_enabled)
193 /* Timing code generates warnings at this point in suspend */
194 if (pm_check_print_timings)
196 s3c_pm_run_sysram(s3c_pm_countram, &crc_size);
197 if (pm_check_print_timings) {
199 delta = ktime_sub(stop, start);
200 S3C_PMDBG("s3c_pm_check: Suspend prescan took %lld usecs\n",
201 (unsigned long long)ktime_to_ns(delta) >> 10);
204 S3C_PMDBG("s3c_pm_check: Chunk size: %d, %u bytes for checks needed\n",
205 pm_check_chunksize, crc_size);
207 crcs = kmalloc(crc_size+4, GFP_KERNEL);
209 printk(KERN_ERR "Cannot allocated CRC save area\n");
212 static inline void s3c_pm_printskip(char *desc, unsigned long addr)
214 if (!pm_check_print_skips)
216 S3C_PMDBG("s3c_pm_check: skipping %08lx, has %s in\n", addr, desc);
219 /* externs from sleep.S */
220 extern u32 *sleep_save_sp;
223 * Zero memory that the sleep code may have used but doesn't care about.
225 * The sleep code uses some temporary memory that it doesn't need anymore
226 * after resume. We zero it out here so we don't have to skip a whole chunk.
227 * This keeps CRCs consistent.
229 * Alternatively we could change sleep code to zero this memory, but we run
230 * into some issues if we ever get a failed suspend (the normal resume code
233 static void zero_temp_memory(void)
238 static bool s3c_pm_should_skip(phys_addr_t addr, u32 size)
240 void *stkbase = current_thread_info();
242 if (phys_addrs_overlap(addr, size,
243 virt_to_phys(stkbase), THREAD_SIZE)) {
244 s3c_pm_printskip("stack", addr);
247 if (phys_addrs_overlap(addr, size, virt_to_phys(crcs), crc_size)) {
248 s3c_pm_printskip("crc block", addr);
251 if (pm_does_overlap_suspend_volatile(addr, size)) {
252 s3c_pm_printskip("volatile suspend data", addr);
259 static u32 *s3c_pm_makecheck(struct resource *res, u32 *val)
261 unsigned long addr, left;
263 for (addr = res->start; addr <= res->end;
264 addr += pm_check_chunksize, val++) {
265 left = res->end - addr + 1;
266 if (left > pm_check_chunksize)
267 left = pm_check_chunksize;
269 if (!s3c_pm_should_skip(addr, left))
270 *val = s3c_pm_process_mem(~0, addr, left);
276 /* s3c_pm_check_store
278 * compute the CRC values for the memory blocks before the final
282 void s3c_pm_check_store(void)
284 ktime_t start, stop, delta;
289 if (pm_check_print_timings)
293 s3c_pm_run_sysram(s3c_pm_makecheck, crcs);
294 if (pm_check_print_timings) {
296 delta = ktime_sub(stop, start);
297 S3C_PMDBG("s3c_pm_check: Suspend memory scan took %lld usecs\n",
298 (unsigned long long)ktime_to_ns(delta) >> 10);
303 * s3c_pm_runcheck() - helper to check a resource on restore.
304 * @res: The resource to check
305 * @vak: Pointer to list of CRC32 values to check.
307 * Called from the s3c_pm_check_restore() via s3c_pm_run_sysram(), this
308 * function runs the given memory resource checking it against the stored
309 * CRC to ensure that memory is restored. The function tries to skip as
310 * many of the areas used during the suspend process.
312 static u32 *s3c_pm_runcheck(struct resource *res, u32 *val)
318 for (addr = res->start; addr <= res->end;
319 addr += pm_check_chunksize, val++) {
320 left = res->end - addr + 1;
322 if (left > pm_check_chunksize)
323 left = pm_check_chunksize;
325 if (s3c_pm_should_skip(addr, left))
328 /* calculate and check the checksum */
330 calc = s3c_pm_process_mem(~0, addr, left);
333 S3C_PMDBG("s3c_pm_check: Restore CRC error at %08lx "
343 * s3c_pm_check_restore() - memory check called on resume
345 * check the CRCs after the restore event and free the memory used
348 void s3c_pm_check_restore(void)
350 ktime_t start, stop, delta;
356 if (pm_check_print_timings)
359 s3c_pm_run_sysram(s3c_pm_runcheck, crcs);
360 if (pm_check_print_timings) {
362 delta = ktime_sub(stop, start);
363 S3C_PMDBG("s3c_pm_check: Resume memory scan took %lld usecs\n",
364 (unsigned long long)ktime_to_ns(delta) >> 10);
367 S3C_PMDBG("s3c_pm_check: %d CRC errors\n", crc_err_cnt);
368 if (pm_check_should_panic)
369 panic("%d CRC errors\n", crc_err_cnt);
374 * s3c_pm_check_cleanup() - free memory resources
376 * Free the resources that where allocated by the suspend
377 * memory check code. We do this separately from the
378 * s3c_pm_check_restore() function as we cannot call any
379 * functions that might sleep during that resume.
381 void s3c_pm_check_cleanup(void)
388 * s3c_pm_check_enable() - enable suspend/resume memory checks
389 * @enabled: True to enable, false to disable
392 void s3c_pm_check_set_enable(bool enabled)
394 pm_check_enabled = enabled;