1 /* linux/arch/arm/plat-s3c/pm-check.c
2 * originally in linux/arch/arm/plat-s3c24xx/pm.c
4 * Copyright (c) 2004,2006,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>
16 #include <linux/suspend.h>
17 #include <linux/init.h>
18 #include <linux/crc32.h>
19 #include <linux/ioport.h>
23 #if CONFIG_S3C2410_PM_CHECK_CHUNKSIZE < 1
24 #error CONFIG_S3C2410_PM_CHECK_CHUNKSIZE must be a positive non-zero value
27 /* suspend checking code...
29 * this next area does a set of crc checks over all the installed
30 * memory, so the system can verify if the resume was ok.
32 * CONFIG_S3C2410_PM_CHECK_CHUNKSIZE defines the block-size for the CRC,
33 * increasing it will mean that the area corrupted will be less easy to spot,
34 * and reducing the size will cause the CRC save area to grow
37 #define CHECK_CHUNKSIZE (CONFIG_S3C2410_PM_CHECK_CHUNKSIZE * 1024)
39 static u32 crc_size
; /* size needed for the crc block */
40 static u32
*crcs
; /* allocated over suspend/resume */
42 typedef u32
*(run_fn_t
)(struct resource
*ptr
, u32
*arg
);
46 * go through the given resource list, and look for system ram
49 static void s3c_pm_run_res(struct resource
*ptr
, run_fn_t fn
, u32
*arg
)
52 if (ptr
->child
!= NULL
)
53 s3c_pm_run_res(ptr
->child
, fn
, arg
);
55 if ((ptr
->flags
& IORESOURCE_MEM
) &&
56 strcmp(ptr
->name
, "System RAM") == 0) {
57 S3C_PMDBG("Found system RAM at %08lx..%08lx\n",
58 (unsigned long)ptr
->start
,
59 (unsigned long)ptr
->end
);
67 static void s3c_pm_run_sysram(run_fn_t fn
, u32
*arg
)
69 s3c_pm_run_res(&iomem_resource
, fn
, arg
);
72 static u32
*s3c_pm_countram(struct resource
*res
, u32
*val
)
74 u32 size
= (u32
)(res
->end
- res
->start
)+1;
76 size
+= CHECK_CHUNKSIZE
-1;
77 size
/= CHECK_CHUNKSIZE
;
79 S3C_PMDBG("Area %08lx..%08lx, %d blocks\n",
80 (unsigned long)res
->start
, (unsigned long)res
->end
, size
);
82 *val
+= size
* sizeof(u32
);
86 /* s3c_pm_prepare_check
88 * prepare the necessary information for creating the CRCs. This
89 * must be done before the final save, as it will require memory
90 * allocating, and thus touching bits of the kernel we do not
94 void s3c_pm_check_prepare(void)
98 s3c_pm_run_sysram(s3c_pm_countram
, &crc_size
);
100 S3C_PMDBG("s3c_pm_prepare_check: %u checks needed\n", crc_size
);
102 crcs
= kmalloc(crc_size
+4, GFP_KERNEL
);
104 printk(KERN_ERR
"Cannot allocated CRC save area\n");
107 static u32
*s3c_pm_makecheck(struct resource
*res
, u32
*val
)
109 unsigned long addr
, left
;
111 for (addr
= res
->start
; addr
< res
->end
;
112 addr
+= CHECK_CHUNKSIZE
) {
113 left
= res
->end
- addr
;
115 if (left
> CHECK_CHUNKSIZE
)
116 left
= CHECK_CHUNKSIZE
;
118 *val
= crc32_le(~0, phys_to_virt(addr
), left
);
125 /* s3c_pm_check_store
127 * compute the CRC values for the memory blocks before the final
131 void s3c_pm_check_store(void)
134 s3c_pm_run_sysram(s3c_pm_makecheck
, crcs
);
139 * return TRUE if the area defined by ptr..ptr+size contains the
143 static inline int in_region(void *ptr
, int size
, void *what
, size_t whatsz
)
145 if ((what
+whatsz
) < ptr
)
148 if (what
> (ptr
+size
))
155 * s3c_pm_runcheck() - helper to check a resource on restore.
156 * @res: The resource to check
157 * @vak: Pointer to list of CRC32 values to check.
159 * Called from the s3c_pm_check_restore() via s3c_pm_run_sysram(), this
160 * function runs the given memory resource checking it against the stored
161 * CRC to ensure that memory is restored. The function tries to skip as
162 * many of the areas used during the suspend process.
164 static u32
*s3c_pm_runcheck(struct resource
*res
, u32
*val
)
166 void *save_at
= phys_to_virt(s3c_sleep_save_phys
);
173 stkpage
= (void *)((u32
)&calc
& ~PAGE_MASK
);
175 for (addr
= res
->start
; addr
< res
->end
;
176 addr
+= CHECK_CHUNKSIZE
) {
177 left
= res
->end
- addr
;
179 if (left
> CHECK_CHUNKSIZE
)
180 left
= CHECK_CHUNKSIZE
;
182 ptr
= phys_to_virt(addr
);
184 if (in_region(ptr
, left
, stkpage
, 4096)) {
185 S3C_PMDBG("skipping %08lx, has stack in\n", addr
);
189 if (in_region(ptr
, left
, crcs
, crc_size
)) {
190 S3C_PMDBG("skipping %08lx, has crc block in\n", addr
);
194 if (in_region(ptr
, left
, save_at
, 32*4 )) {
195 S3C_PMDBG("skipping %08lx, has save block in\n", addr
);
199 /* calculate and check the checksum */
201 calc
= crc32_le(~0, ptr
, left
);
203 printk(KERN_ERR
"Restore CRC error at "
204 "%08lx (%08x vs %08x)\n", addr
, calc
, *val
);
206 S3C_PMDBG("Restore CRC error at %08lx (%08x vs %08x)\n",
218 * s3c_pm_check_restore() - memory check called on resume
220 * check the CRCs after the restore event and free the memory used
223 void s3c_pm_check_restore(void)
226 s3c_pm_run_sysram(s3c_pm_runcheck
, crcs
);
230 * s3c_pm_check_cleanup() - free memory resources
232 * Free the resources that where allocated by the suspend
233 * memory check code. We do this separately from the
234 * s3c_pm_check_restore() function as we cannot call any
235 * functions that might sleep during that resume.
237 void s3c_pm_check_cleanup(void)