Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 1 | /* linux/arch/arm/plat-s3c/pm-check.c |
| 2 | * originally in linux/arch/arm/plat-s3c24xx/pm.c |
| 3 | * |
Ben Dooks | ccae941 | 2009-11-13 22:54:14 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004-2008 Simtec Electronics |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 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 | #include <linux/suspend.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/crc32.h> |
| 19 | #include <linux/ioport.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 21 | |
| 22 | #include <plat/pm.h> |
| 23 | |
Ben Dooks | 8005745 | 2010-01-20 12:29:25 +0900 | [diff] [blame] | 24 | #if CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE < 1 |
| 25 | #error CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE must be a positive non-zero value |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | /* suspend checking code... |
| 29 | * |
| 30 | * this next area does a set of crc checks over all the installed |
| 31 | * memory, so the system can verify if the resume was ok. |
| 32 | * |
Ben Dooks | 8005745 | 2010-01-20 12:29:25 +0900 | [diff] [blame] | 33 | * CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE defines the block-size for the CRC, |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 34 | * increasing it will mean that the area corrupted will be less easy to spot, |
| 35 | * and reducing the size will cause the CRC save area to grow |
| 36 | */ |
| 37 | |
Ben Dooks | 8005745 | 2010-01-20 12:29:25 +0900 | [diff] [blame] | 38 | #define CHECK_CHUNKSIZE (CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE * 1024) |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 39 | |
| 40 | static u32 crc_size; /* size needed for the crc block */ |
| 41 | static u32 *crcs; /* allocated over suspend/resume */ |
| 42 | |
| 43 | typedef u32 *(run_fn_t)(struct resource *ptr, u32 *arg); |
| 44 | |
| 45 | /* s3c_pm_run_res |
| 46 | * |
| 47 | * go through the given resource list, and look for system ram |
| 48 | */ |
| 49 | |
| 50 | static void s3c_pm_run_res(struct resource *ptr, run_fn_t fn, u32 *arg) |
| 51 | { |
| 52 | while (ptr != NULL) { |
| 53 | if (ptr->child != NULL) |
| 54 | s3c_pm_run_res(ptr->child, fn, arg); |
| 55 | |
| 56 | if ((ptr->flags & IORESOURCE_MEM) && |
| 57 | strcmp(ptr->name, "System RAM") == 0) { |
| 58 | S3C_PMDBG("Found system RAM at %08lx..%08lx\n", |
Ben Dooks | 840eeeb | 2008-12-12 00:24:09 +0000 | [diff] [blame] | 59 | (unsigned long)ptr->start, |
| 60 | (unsigned long)ptr->end); |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 61 | arg = (fn)(ptr, arg); |
| 62 | } |
| 63 | |
| 64 | ptr = ptr->sibling; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static void s3c_pm_run_sysram(run_fn_t fn, u32 *arg) |
| 69 | { |
| 70 | s3c_pm_run_res(&iomem_resource, fn, arg); |
| 71 | } |
| 72 | |
| 73 | static u32 *s3c_pm_countram(struct resource *res, u32 *val) |
| 74 | { |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 75 | u32 size = (u32)resource_size(res); |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 76 | |
| 77 | size += CHECK_CHUNKSIZE-1; |
| 78 | size /= CHECK_CHUNKSIZE; |
| 79 | |
Ben Dooks | 840eeeb | 2008-12-12 00:24:09 +0000 | [diff] [blame] | 80 | S3C_PMDBG("Area %08lx..%08lx, %d blocks\n", |
| 81 | (unsigned long)res->start, (unsigned long)res->end, size); |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 82 | |
| 83 | *val += size * sizeof(u32); |
| 84 | return val; |
| 85 | } |
| 86 | |
| 87 | /* s3c_pm_prepare_check |
| 88 | * |
| 89 | * prepare the necessary information for creating the CRCs. This |
| 90 | * must be done before the final save, as it will require memory |
| 91 | * allocating, and thus touching bits of the kernel we do not |
| 92 | * know about. |
| 93 | */ |
| 94 | |
| 95 | void s3c_pm_check_prepare(void) |
| 96 | { |
| 97 | crc_size = 0; |
| 98 | |
| 99 | s3c_pm_run_sysram(s3c_pm_countram, &crc_size); |
| 100 | |
| 101 | S3C_PMDBG("s3c_pm_prepare_check: %u checks needed\n", crc_size); |
| 102 | |
| 103 | crcs = kmalloc(crc_size+4, GFP_KERNEL); |
| 104 | if (crcs == NULL) |
| 105 | printk(KERN_ERR "Cannot allocated CRC save area\n"); |
| 106 | } |
| 107 | |
| 108 | static u32 *s3c_pm_makecheck(struct resource *res, u32 *val) |
| 109 | { |
| 110 | unsigned long addr, left; |
| 111 | |
| 112 | for (addr = res->start; addr < res->end; |
| 113 | addr += CHECK_CHUNKSIZE) { |
| 114 | left = res->end - addr; |
| 115 | |
| 116 | if (left > CHECK_CHUNKSIZE) |
| 117 | left = CHECK_CHUNKSIZE; |
| 118 | |
| 119 | *val = crc32_le(~0, phys_to_virt(addr), left); |
| 120 | val++; |
| 121 | } |
| 122 | |
| 123 | return val; |
| 124 | } |
| 125 | |
| 126 | /* s3c_pm_check_store |
| 127 | * |
| 128 | * compute the CRC values for the memory blocks before the final |
| 129 | * sleep. |
| 130 | */ |
| 131 | |
| 132 | void s3c_pm_check_store(void) |
| 133 | { |
| 134 | if (crcs != NULL) |
| 135 | s3c_pm_run_sysram(s3c_pm_makecheck, crcs); |
| 136 | } |
| 137 | |
| 138 | /* in_region |
| 139 | * |
| 140 | * return TRUE if the area defined by ptr..ptr+size contains the |
| 141 | * what..what+whatsz |
| 142 | */ |
| 143 | |
| 144 | static inline int in_region(void *ptr, int size, void *what, size_t whatsz) |
| 145 | { |
| 146 | if ((what+whatsz) < ptr) |
| 147 | return 0; |
| 148 | |
| 149 | if (what > (ptr+size)) |
| 150 | return 0; |
| 151 | |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | /** |
Ben Dooks | 663a830 | 2008-12-12 00:24:36 +0000 | [diff] [blame] | 156 | * s3c_pm_runcheck() - helper to check a resource on restore. |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 157 | * @res: The resource to check |
| 158 | * @vak: Pointer to list of CRC32 values to check. |
| 159 | * |
| 160 | * Called from the s3c_pm_check_restore() via s3c_pm_run_sysram(), this |
| 161 | * function runs the given memory resource checking it against the stored |
| 162 | * CRC to ensure that memory is restored. The function tries to skip as |
| 163 | * many of the areas used during the suspend process. |
| 164 | */ |
| 165 | static u32 *s3c_pm_runcheck(struct resource *res, u32 *val) |
| 166 | { |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 167 | unsigned long addr; |
| 168 | unsigned long left; |
Ben Dooks | 663a830 | 2008-12-12 00:24:36 +0000 | [diff] [blame] | 169 | void *stkpage; |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 170 | void *ptr; |
| 171 | u32 calc; |
| 172 | |
Ben Dooks | 663a830 | 2008-12-12 00:24:36 +0000 | [diff] [blame] | 173 | stkpage = (void *)((u32)&calc & ~PAGE_MASK); |
| 174 | |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 175 | for (addr = res->start; addr < res->end; |
| 176 | addr += CHECK_CHUNKSIZE) { |
| 177 | left = res->end - addr; |
| 178 | |
| 179 | if (left > CHECK_CHUNKSIZE) |
| 180 | left = CHECK_CHUNKSIZE; |
| 181 | |
| 182 | ptr = phys_to_virt(addr); |
| 183 | |
Ben Dooks | 663a830 | 2008-12-12 00:24:36 +0000 | [diff] [blame] | 184 | if (in_region(ptr, left, stkpage, 4096)) { |
| 185 | S3C_PMDBG("skipping %08lx, has stack in\n", addr); |
| 186 | goto skip_check; |
| 187 | } |
| 188 | |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 189 | if (in_region(ptr, left, crcs, crc_size)) { |
| 190 | S3C_PMDBG("skipping %08lx, has crc block in\n", addr); |
| 191 | goto skip_check; |
| 192 | } |
| 193 | |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 194 | /* calculate and check the checksum */ |
| 195 | |
| 196 | calc = crc32_le(~0, ptr, left); |
| 197 | if (calc != *val) { |
| 198 | printk(KERN_ERR "Restore CRC error at " |
| 199 | "%08lx (%08x vs %08x)\n", addr, calc, *val); |
| 200 | |
| 201 | S3C_PMDBG("Restore CRC error at %08lx (%08x vs %08x)\n", |
| 202 | addr, calc, *val); |
| 203 | } |
| 204 | |
| 205 | skip_check: |
| 206 | val++; |
| 207 | } |
| 208 | |
| 209 | return val; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * s3c_pm_check_restore() - memory check called on resume |
| 214 | * |
| 215 | * check the CRCs after the restore event and free the memory used |
| 216 | * to hold them |
| 217 | */ |
| 218 | void s3c_pm_check_restore(void) |
| 219 | { |
Ben Dooks | aa8aba6 | 2008-12-12 00:24:34 +0000 | [diff] [blame] | 220 | if (crcs != NULL) |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 221 | s3c_pm_run_sysram(s3c_pm_runcheck, crcs); |
Ben Dooks | 549c7e3 | 2008-12-12 00:24:07 +0000 | [diff] [blame] | 222 | } |
Ben Dooks | aa8aba6 | 2008-12-12 00:24:34 +0000 | [diff] [blame] | 223 | |
| 224 | /** |
| 225 | * s3c_pm_check_cleanup() - free memory resources |
| 226 | * |
| 227 | * Free the resources that where allocated by the suspend |
| 228 | * memory check code. We do this separately from the |
| 229 | * s3c_pm_check_restore() function as we cannot call any |
| 230 | * functions that might sleep during that resume. |
| 231 | */ |
| 232 | void s3c_pm_check_cleanup(void) |
| 233 | { |
| 234 | kfree(crcs); |
| 235 | crcs = NULL; |
| 236 | } |
| 237 | |