1
2
3
4
5
6
7
8
9
10
11
12
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>
20#include <linux/slab.h>
21
22#include <plat/pm-common.h>
23
24#if CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE < 1
25#error CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE must be a positive non-zero value
26#endif
27
28
29
30
31
32
33
34
35
36
37
38#define CHECK_CHUNKSIZE (CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE * 1024)
39
40static u32 crc_size;
41static u32 *crcs;
42
43typedef u32 *(run_fn_t)(struct resource *ptr, u32 *arg);
44
45
46
47
48
49
50static 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_SYSTEM_RAM)
57 == IORESOURCE_SYSTEM_RAM) {
58 S3C_PMDBG("Found system RAM at %08lx..%08lx\n",
59 (unsigned long)ptr->start,
60 (unsigned long)ptr->end);
61 arg = (fn)(ptr, arg);
62 }
63
64 ptr = ptr->sibling;
65 }
66}
67
68static void s3c_pm_run_sysram(run_fn_t fn, u32 *arg)
69{
70 s3c_pm_run_res(&iomem_resource, fn, arg);
71}
72
73static u32 *s3c_pm_countram(struct resource *res, u32 *val)
74{
75 u32 size = (u32)resource_size(res);
76
77 size += CHECK_CHUNKSIZE-1;
78 size /= CHECK_CHUNKSIZE;
79
80 S3C_PMDBG("Area %08lx..%08lx, %d blocks\n",
81 (unsigned long)res->start, (unsigned long)res->end, size);
82
83 *val += size * sizeof(u32);
84 return val;
85}
86
87
88
89
90
91
92
93
94
95void 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
108static 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
127
128
129
130
131
132void s3c_pm_check_store(void)
133{
134 if (crcs != NULL)
135 s3c_pm_run_sysram(s3c_pm_makecheck, crcs);
136}
137
138
139
140
141
142
143
144static 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
156
157
158
159
160
161
162
163
164
165static u32 *s3c_pm_runcheck(struct resource *res, u32 *val)
166{
167 unsigned long addr;
168 unsigned long left;
169 void *stkpage;
170 void *ptr;
171 u32 calc;
172
173 stkpage = (void *)((u32)&calc & ~PAGE_MASK);
174
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
184 if (in_region(ptr, left, stkpage, 4096)) {
185 S3C_PMDBG("skipping %08lx, has stack in\n", addr);
186 goto skip_check;
187 }
188
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
194
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
214
215
216
217
218void s3c_pm_check_restore(void)
219{
220 if (crcs != NULL)
221 s3c_pm_run_sysram(s3c_pm_runcheck, crcs);
222}
223
224
225
226
227
228
229
230
231
232void s3c_pm_check_cleanup(void)
233{
234 kfree(crcs);
235 crcs = NULL;
236}
237
238