samsung: pm-check: Cleanup chunk exclude code to use suspend_volatile
[cascardo/linux.git] / arch / arm / plat-samsung / pm-check.c
1 /* linux/arch/arm/plat-s3c/pm-check.c
2  *  originally in linux/arch/arm/plat-s3c24xx/pm.c
3  *
4  * Copyright (c) 2004-2008 Simtec Electronics
5  *      http://armlinux.simtec.co.uk
6  *      Ben Dooks <ben@simtec.co.uk>
7  *
8  * S3C Power Mangament - suspend/resume memory corruptiuon check.
9  *
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.
13 */
14
15 #include <linux/kernel.h>
16
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>
27
28 #include <plat/pm.h>
29
30 #if CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE < 1
31 #error CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE must be a positive non-zero value
32 #endif
33
34 /* suspend checking code...
35  *
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.
38  *
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
42 */
43
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;
50
51 static u32 crc_size;    /* size needed for the crc block */
52 static u32 *crcs;       /* allocated over suspend/resume */
53
54 /* number of errors found this resume */
55 static __suspend_volatile_bss u32 crc_err_cnt;
56
57
58 typedef u32 *(run_fn_t)(struct resource *ptr, u32 *arg);
59
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");
75
76 /**
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
81  *
82  */
83 static u32 s3c_pm_xor_mem(u32 val, const u32 *ptr, size_t len)
84 {
85         int i;
86
87         len >>= 2; /* get words, not bytes */
88         for (i = 0; i < len; i++, ptr++)
89                 val ^= *ptr;
90         return val;
91 }
92
93 /**
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
98  *
99  * Loop through the chunk in PAGE_SIZE blocks, ensuring each
100  * page is mapped.  Use either crc32 or xor checksum and return
101  * back.
102  */
103 static inline u32 s3c_pm_process_mem(u32 val, u32 addr, size_t len)
104 {
105         size_t processed = 0;
106
107         if (unlikely(addr & (PAGE_SIZE - 1))) {
108                 printk(KERN_ERR "s3c_pm_check: Unaligned address: 0x%x\n",
109                         addr);
110                 return val;
111         }
112         while (processed < len) {
113                 int pfn = __phys_to_pfn(addr + processed);
114                 unsigned long left = len - processed;
115                 void *virt;
116
117                 if (left > PAGE_SIZE)
118                         left = PAGE_SIZE;
119                 virt = kmap_atomic(pfn_to_page(pfn));
120                 if (!virt) {
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);
124                         return val;
125                 }
126                 if (pm_check_use_xor)
127                         val = s3c_pm_xor_mem(val, virt, left);
128                 else
129                         val = crc32_le(val, virt, left);
130                 kunmap_atomic(virt);
131                 processed += left;
132         }
133         return val;
134 }
135
136 /* s3c_pm_run_res
137  *
138  * go through the given resource list, and look for system ram
139 */
140
141 static void s3c_pm_run_res(struct resource *ptr, run_fn_t fn, u32 *arg)
142 {
143         while (ptr != NULL) {
144                 if (ptr->child != NULL)
145                         s3c_pm_run_res(ptr->child, fn, arg);
146
147                 if ((ptr->flags & IORESOURCE_MEM) &&
148                     strcmp(ptr->name, "System RAM") == 0) {
149                         S3C_PMDBG("s3c_pm_check: Found system RAM at "
150                                   "%08lx..%08lx\n",
151                                   (unsigned long)ptr->start,
152                                   (unsigned long)ptr->end);
153                         arg = (fn)(ptr, arg);
154                 }
155
156                 ptr = ptr->sibling;
157         }
158 }
159
160 static void s3c_pm_run_sysram(run_fn_t fn, u32 *arg)
161 {
162         s3c_pm_run_res(&iomem_resource, fn, arg);
163 }
164
165 static u32 *s3c_pm_countram(struct resource *res, u32 *val)
166 {
167         u32 size = (u32)resource_size(res);
168
169         size = DIV_ROUND_UP(size, pm_check_chunksize);
170
171         S3C_PMDBG("s3c_pm_check: Area %08lx..%08lx, %d blocks\n",
172                   (unsigned long)res->start, (unsigned long)res->end, size);
173
174         *val += size * sizeof(u32);
175         return val;
176 }
177
178 /* s3c_pm_prepare_check
179  *
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
183  * know about.
184 */
185 void s3c_pm_check_prepare(void)
186 {
187         ktime_t start, stop, delta;
188
189         if (!pm_check_enabled)
190                 return;
191
192         crc_size = 0;
193         /* Timing code generates warnings at this point in suspend */
194         if (pm_check_print_timings)
195                 start = ktime_get();
196         s3c_pm_run_sysram(s3c_pm_countram, &crc_size);
197         if (pm_check_print_timings) {
198                 stop = ktime_get();
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);
202         }
203
204         S3C_PMDBG("s3c_pm_check: Chunk size: %d, %u bytes for checks needed\n",
205                 pm_check_chunksize, crc_size);
206
207         crcs = kmalloc(crc_size+4, GFP_KERNEL);
208         if (crcs == NULL)
209                 printk(KERN_ERR "Cannot allocated CRC save area\n");
210 }
211
212 static inline void s3c_pm_printskip(char *desc, unsigned long addr)
213 {
214         if (!pm_check_print_skips)
215                 return;
216         S3C_PMDBG("s3c_pm_check: skipping %08lx, has %s in\n", addr, desc);
217 }
218
219 /* externs from sleep.S */
220 extern u32 *sleep_save_sp;
221
222 /**
223  * Zero memory that the sleep code may have used but doesn't care about.
224  *
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.
228  *
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
231  * doesn't run).
232  */
233 static void zero_temp_memory(void)
234 {
235         sleep_save_sp = 0;
236 }
237
238 static bool s3c_pm_should_skip(phys_addr_t addr, u32 size)
239 {
240         void *stkbase = current_thread_info();
241
242         if (phys_addrs_overlap(addr, size,
243                                virt_to_phys(stkbase), THREAD_SIZE)) {
244                 s3c_pm_printskip("stack", addr);
245                 return true;
246         }
247         if (phys_addrs_overlap(addr, size, virt_to_phys(crcs), crc_size)) {
248                 s3c_pm_printskip("crc block", addr);
249                 return true;
250         }
251         if (pm_does_overlap_suspend_volatile(addr, size)) {
252                 s3c_pm_printskip("volatile suspend data", addr);
253                 return true;
254         }
255
256         return false;
257 }
258
259 static u32 *s3c_pm_makecheck(struct resource *res, u32 *val)
260 {
261         unsigned long addr, left;
262
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;
268
269                 if (!s3c_pm_should_skip(addr, left))
270                         *val = s3c_pm_process_mem(~0, addr, left);
271         }
272
273         return val;
274 }
275
276 /* s3c_pm_check_store
277  *
278  * compute the CRC values for the memory blocks before the final
279  * sleep.
280 */
281
282 void s3c_pm_check_store(void)
283 {
284         ktime_t start, stop, delta;
285
286         if (crcs == NULL)
287                 return;
288
289         if (pm_check_print_timings)
290                 start = ktime_get();
291
292         zero_temp_memory();
293         s3c_pm_run_sysram(s3c_pm_makecheck, crcs);
294         if (pm_check_print_timings) {
295                 stop = ktime_get();
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);
299         }
300 }
301
302 /**
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.
306  *
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.
311  */
312 static u32 *s3c_pm_runcheck(struct resource *res, u32 *val)
313 {
314         unsigned long addr;
315         unsigned long left;
316         u32 calc;
317
318         for (addr = res->start; addr <= res->end;
319              addr += pm_check_chunksize, val++) {
320                 left = res->end - addr + 1;
321
322                 if (left > pm_check_chunksize)
323                         left = pm_check_chunksize;
324
325                 if (s3c_pm_should_skip(addr, left))
326                         continue;
327
328                 /* calculate and check the checksum */
329
330                 calc = s3c_pm_process_mem(~0, addr, left);
331                 if (calc != *val) {
332                         crc_err_cnt++;
333                         S3C_PMDBG("s3c_pm_check: Restore CRC error at %08lx "
334                                 "(%08x vs %08x)\n",
335                                 addr, calc, *val);
336                 }
337         }
338
339         return val;
340 }
341
342 /**
343  * s3c_pm_check_restore() - memory check called on resume
344  *
345  * check the CRCs after the restore event and free the memory used
346  * to hold them
347 */
348 void s3c_pm_check_restore(void)
349 {
350         ktime_t start, stop, delta;
351
352         if (crcs == NULL)
353                 return;
354
355         crc_err_cnt = 0;
356         if (pm_check_print_timings)
357                 start = ktime_get();
358         zero_temp_memory();
359         s3c_pm_run_sysram(s3c_pm_runcheck, crcs);
360         if (pm_check_print_timings) {
361                 stop = ktime_get();
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);
365         }
366         if (crc_err_cnt) {
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);
370         }
371 }
372
373 /**
374  * s3c_pm_check_cleanup() - free memory resources
375  *
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.
380  */
381 void s3c_pm_check_cleanup(void)
382 {
383         kfree(crcs);
384         crcs = NULL;
385 }
386
387 /**
388  * s3c_pm_check_enable() - enable suspend/resume memory checks
389  * @enabled: True to enable, false to disable
390  *
391  */
392 void s3c_pm_check_set_enable(bool enabled)
393 {
394         pm_check_enabled = enabled;
395 }