Commit | Line | Data |
---|---|---|
c672528a CC |
1 | /* |
2 | * Copyright (C) 2012 Google, Inc. | |
3 | * | |
4 | * This software is licensed under the terms of the GNU General Public | |
5 | * License version 2, as published by the Free Software Foundation, and | |
6 | * may be copied, distributed, and modified under those terms. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | */ | |
14 | ||
9ee85b8b | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
ef748853 | 16 | |
404a6043 CC |
17 | #include <linux/device.h> |
18 | #include <linux/err.h> | |
c672528a | 19 | #include <linux/errno.h> |
c672528a CC |
20 | #include <linux/init.h> |
21 | #include <linux/io.h> | |
5bf6d1b9 | 22 | #include <linux/kernel.h> |
404a6043 CC |
23 | #include <linux/list.h> |
24 | #include <linux/memblock.h> | |
5bf6d1b9 | 25 | #include <linux/pstore_ram.h> |
9cc05ad9 | 26 | #include <linux/rslib.h> |
c672528a | 27 | #include <linux/slab.h> |
5bf6d1b9 | 28 | #include <linux/uaccess.h> |
404a6043 | 29 | #include <linux/vmalloc.h> |
24c3d2f3 | 30 | #include <asm/page.h> |
c672528a | 31 | |
c208f7d4 KC |
32 | /** |
33 | * struct persistent_ram_buffer - persistent circular RAM buffer | |
34 | * | |
35 | * @sig: | |
36 | * signature to indicate header (PERSISTENT_RAM_SIG xor PRZ-type value) | |
37 | * @start: | |
38 | * offset into @data where the beginning of the stored bytes begin | |
39 | * @size: | |
40 | * number of valid bytes stored in @data | |
41 | */ | |
c672528a CC |
42 | struct persistent_ram_buffer { |
43 | uint32_t sig; | |
808d0387 CC |
44 | atomic_t start; |
45 | atomic_t size; | |
c672528a CC |
46 | uint8_t data[0]; |
47 | }; | |
48 | ||
49 | #define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */ | |
50 | ||
808d0387 CC |
51 | static inline size_t buffer_size(struct persistent_ram_zone *prz) |
52 | { | |
53 | return atomic_read(&prz->buffer->size); | |
54 | } | |
55 | ||
56 | static inline size_t buffer_start(struct persistent_ram_zone *prz) | |
57 | { | |
58 | return atomic_read(&prz->buffer->start); | |
59 | } | |
60 | ||
0405a5ce | 61 | /* increase and wrap the start pointer, returning the old value */ |
d5a9bf0b | 62 | static size_t buffer_start_add(struct persistent_ram_zone *prz, size_t a) |
0405a5ce RH |
63 | { |
64 | int old; | |
65 | int new; | |
663deb47 | 66 | unsigned long flags = 0; |
0405a5ce | 67 | |
663deb47 JF |
68 | if (!(prz->flags & PRZ_FLAG_NO_LOCK)) |
69 | raw_spin_lock_irqsave(&prz->buffer_lock, flags); | |
0405a5ce RH |
70 | |
71 | old = atomic_read(&prz->buffer->start); | |
72 | new = old + a; | |
017321cf | 73 | while (unlikely(new >= prz->buffer_size)) |
0405a5ce RH |
74 | new -= prz->buffer_size; |
75 | atomic_set(&prz->buffer->start, new); | |
76 | ||
663deb47 JF |
77 | if (!(prz->flags & PRZ_FLAG_NO_LOCK)) |
78 | raw_spin_unlock_irqrestore(&prz->buffer_lock, flags); | |
0405a5ce RH |
79 | |
80 | return old; | |
81 | } | |
82 | ||
83 | /* increase the size counter until it hits the max size */ | |
d5a9bf0b | 84 | static void buffer_size_add(struct persistent_ram_zone *prz, size_t a) |
0405a5ce RH |
85 | { |
86 | size_t old; | |
87 | size_t new; | |
663deb47 | 88 | unsigned long flags = 0; |
0405a5ce | 89 | |
663deb47 JF |
90 | if (!(prz->flags & PRZ_FLAG_NO_LOCK)) |
91 | raw_spin_lock_irqsave(&prz->buffer_lock, flags); | |
0405a5ce RH |
92 | |
93 | old = atomic_read(&prz->buffer->size); | |
94 | if (old == prz->buffer_size) | |
95 | goto exit; | |
96 | ||
97 | new = old + a; | |
98 | if (new > prz->buffer_size) | |
99 | new = prz->buffer_size; | |
100 | atomic_set(&prz->buffer->size, new); | |
101 | ||
102 | exit: | |
663deb47 JF |
103 | if (!(prz->flags & PRZ_FLAG_NO_LOCK)) |
104 | raw_spin_unlock_irqrestore(&prz->buffer_lock, flags); | |
0405a5ce RH |
105 | } |
106 | ||
a15d0b36 | 107 | static void notrace persistent_ram_encode_rs8(struct persistent_ram_zone *prz, |
c672528a CC |
108 | uint8_t *data, size_t len, uint8_t *ecc) |
109 | { | |
110 | int i; | |
9cc05ad9 | 111 | |
c672528a | 112 | /* Initialize the parity buffer */ |
f2531f19 KC |
113 | memset(prz->ecc_info.par, 0, |
114 | prz->ecc_info.ecc_size * sizeof(prz->ecc_info.par[0])); | |
115 | encode_rs8(prz->rs_decoder, data, len, prz->ecc_info.par, 0); | |
c31ad081 | 116 | for (i = 0; i < prz->ecc_info.ecc_size; i++) |
f2531f19 | 117 | ecc[i] = prz->ecc_info.par[i]; |
c672528a CC |
118 | } |
119 | ||
120 | static int persistent_ram_decode_rs8(struct persistent_ram_zone *prz, | |
121 | void *data, size_t len, uint8_t *ecc) | |
122 | { | |
123 | int i; | |
9cc05ad9 | 124 | |
c31ad081 | 125 | for (i = 0; i < prz->ecc_info.ecc_size; i++) |
f2531f19 KC |
126 | prz->ecc_info.par[i] = ecc[i]; |
127 | return decode_rs8(prz->rs_decoder, data, prz->ecc_info.par, len, | |
c672528a CC |
128 | NULL, 0, NULL, 0, NULL); |
129 | } | |
c672528a | 130 | |
a15d0b36 | 131 | static void notrace persistent_ram_update_ecc(struct persistent_ram_zone *prz, |
808d0387 | 132 | unsigned int start, unsigned int count) |
c672528a CC |
133 | { |
134 | struct persistent_ram_buffer *buffer = prz->buffer; | |
c672528a CC |
135 | uint8_t *buffer_end = buffer->data + prz->buffer_size; |
136 | uint8_t *block; | |
137 | uint8_t *par; | |
c31ad081 AH |
138 | int ecc_block_size = prz->ecc_info.block_size; |
139 | int ecc_size = prz->ecc_info.ecc_size; | |
140 | int size = ecc_block_size; | |
9cc05ad9 | 141 | |
c31ad081 | 142 | if (!ecc_size) |
9cc05ad9 CC |
143 | return; |
144 | ||
808d0387 | 145 | block = buffer->data + (start & ~(ecc_block_size - 1)); |
c31ad081 | 146 | par = prz->par_buffer + (start / ecc_block_size) * ecc_size; |
808d0387 | 147 | |
c672528a | 148 | do { |
9cc05ad9 | 149 | if (block + ecc_block_size > buffer_end) |
c672528a CC |
150 | size = buffer_end - block; |
151 | persistent_ram_encode_rs8(prz, block, size, par); | |
9cc05ad9 CC |
152 | block += ecc_block_size; |
153 | par += ecc_size; | |
808d0387 | 154 | } while (block < buffer->data + start + count); |
c672528a CC |
155 | } |
156 | ||
9cc05ad9 | 157 | static void persistent_ram_update_header_ecc(struct persistent_ram_zone *prz) |
c672528a | 158 | { |
c672528a CC |
159 | struct persistent_ram_buffer *buffer = prz->buffer; |
160 | ||
c31ad081 | 161 | if (!prz->ecc_info.ecc_size) |
9cc05ad9 CC |
162 | return; |
163 | ||
c672528a CC |
164 | persistent_ram_encode_rs8(prz, (uint8_t *)buffer, sizeof(*buffer), |
165 | prz->par_header); | |
c672528a CC |
166 | } |
167 | ||
9cc05ad9 | 168 | static void persistent_ram_ecc_old(struct persistent_ram_zone *prz) |
c672528a CC |
169 | { |
170 | struct persistent_ram_buffer *buffer = prz->buffer; | |
c672528a CC |
171 | uint8_t *block; |
172 | uint8_t *par; | |
173 | ||
c31ad081 | 174 | if (!prz->ecc_info.ecc_size) |
9cc05ad9 CC |
175 | return; |
176 | ||
c672528a CC |
177 | block = buffer->data; |
178 | par = prz->par_buffer; | |
808d0387 | 179 | while (block < buffer->data + buffer_size(prz)) { |
c672528a | 180 | int numerr; |
c31ad081 | 181 | int size = prz->ecc_info.block_size; |
c672528a CC |
182 | if (block + size > buffer->data + prz->buffer_size) |
183 | size = buffer->data + prz->buffer_size - block; | |
184 | numerr = persistent_ram_decode_rs8(prz, block, size, par); | |
185 | if (numerr > 0) { | |
ef748853 | 186 | pr_devel("error in block %p, %d\n", block, numerr); |
c672528a CC |
187 | prz->corrected_bytes += numerr; |
188 | } else if (numerr < 0) { | |
ef748853 | 189 | pr_devel("uncorrectable error in block %p\n", block); |
c672528a CC |
190 | prz->bad_blocks++; |
191 | } | |
c31ad081 AH |
192 | block += prz->ecc_info.block_size; |
193 | par += prz->ecc_info.ecc_size; | |
9cc05ad9 CC |
194 | } |
195 | } | |
196 | ||
5ca5d4e6 | 197 | static int persistent_ram_init_ecc(struct persistent_ram_zone *prz, |
c31ad081 | 198 | struct persistent_ram_ecc_info *ecc_info) |
9cc05ad9 CC |
199 | { |
200 | int numerr; | |
201 | struct persistent_ram_buffer *buffer = prz->buffer; | |
202 | int ecc_blocks; | |
1e6a9e56 | 203 | size_t ecc_total; |
9cc05ad9 | 204 | |
c31ad081 | 205 | if (!ecc_info || !ecc_info->ecc_size) |
9cc05ad9 CC |
206 | return 0; |
207 | ||
c31ad081 AH |
208 | prz->ecc_info.block_size = ecc_info->block_size ?: 128; |
209 | prz->ecc_info.ecc_size = ecc_info->ecc_size ?: 16; | |
210 | prz->ecc_info.symsize = ecc_info->symsize ?: 8; | |
211 | prz->ecc_info.poly = ecc_info->poly ?: 0x11d; | |
9cc05ad9 | 212 | |
c31ad081 AH |
213 | ecc_blocks = DIV_ROUND_UP(prz->buffer_size - prz->ecc_info.ecc_size, |
214 | prz->ecc_info.block_size + | |
215 | prz->ecc_info.ecc_size); | |
216 | ecc_total = (ecc_blocks + 1) * prz->ecc_info.ecc_size; | |
1e6a9e56 AV |
217 | if (ecc_total >= prz->buffer_size) { |
218 | pr_err("%s: invalid ecc_size %u (total %zu, buffer size %zu)\n", | |
c31ad081 AH |
219 | __func__, prz->ecc_info.ecc_size, |
220 | ecc_total, prz->buffer_size); | |
9cc05ad9 CC |
221 | return -EINVAL; |
222 | } | |
223 | ||
1e6a9e56 | 224 | prz->buffer_size -= ecc_total; |
9cc05ad9 | 225 | prz->par_buffer = buffer->data + prz->buffer_size; |
c31ad081 AH |
226 | prz->par_header = prz->par_buffer + |
227 | ecc_blocks * prz->ecc_info.ecc_size; | |
9cc05ad9 CC |
228 | |
229 | /* | |
230 | * first consecutive root is 0 | |
231 | * primitive element to generate roots = 1 | |
232 | */ | |
c31ad081 AH |
233 | prz->rs_decoder = init_rs(prz->ecc_info.symsize, prz->ecc_info.poly, |
234 | 0, 1, prz->ecc_info.ecc_size); | |
9cc05ad9 | 235 | if (prz->rs_decoder == NULL) { |
ef748853 | 236 | pr_info("init_rs failed\n"); |
9cc05ad9 | 237 | return -EINVAL; |
c672528a | 238 | } |
9cc05ad9 | 239 | |
f2531f19 KC |
240 | /* allocate workspace instead of using stack VLA */ |
241 | prz->ecc_info.par = kmalloc_array(prz->ecc_info.ecc_size, | |
242 | sizeof(*prz->ecc_info.par), | |
243 | GFP_KERNEL); | |
244 | if (!prz->ecc_info.par) { | |
245 | pr_err("cannot allocate ECC parity workspace\n"); | |
246 | return -ENOMEM; | |
247 | } | |
248 | ||
9cc05ad9 CC |
249 | prz->corrected_bytes = 0; |
250 | prz->bad_blocks = 0; | |
251 | ||
252 | numerr = persistent_ram_decode_rs8(prz, buffer, sizeof(*buffer), | |
253 | prz->par_header); | |
254 | if (numerr > 0) { | |
ef748853 | 255 | pr_info("error in header, %d\n", numerr); |
9cc05ad9 CC |
256 | prz->corrected_bytes += numerr; |
257 | } else if (numerr < 0) { | |
ef748853 | 258 | pr_info("uncorrectable error in header\n"); |
9cc05ad9 CC |
259 | prz->bad_blocks++; |
260 | } | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | ssize_t persistent_ram_ecc_string(struct persistent_ram_zone *prz, | |
266 | char *str, size_t len) | |
267 | { | |
268 | ssize_t ret; | |
269 | ||
bd08ec33 AH |
270 | if (!prz->ecc_info.ecc_size) |
271 | return 0; | |
272 | ||
9cc05ad9 CC |
273 | if (prz->corrected_bytes || prz->bad_blocks) |
274 | ret = snprintf(str, len, "" | |
275 | "\n%d Corrected bytes, %d unrecoverable blocks\n", | |
276 | prz->corrected_bytes, prz->bad_blocks); | |
277 | else | |
278 | ret = snprintf(str, len, "\nNo errors detected\n"); | |
279 | ||
280 | return ret; | |
281 | } | |
282 | ||
a15d0b36 | 283 | static void notrace persistent_ram_update(struct persistent_ram_zone *prz, |
808d0387 | 284 | const void *s, unsigned int start, unsigned int count) |
9cc05ad9 CC |
285 | { |
286 | struct persistent_ram_buffer *buffer = prz->buffer; | |
7e75678d | 287 | memcpy_toio(buffer->data + start, s, count); |
808d0387 | 288 | persistent_ram_update_ecc(prz, start, count); |
9cc05ad9 CC |
289 | } |
290 | ||
5bf6d1b9 MS |
291 | static int notrace persistent_ram_update_user(struct persistent_ram_zone *prz, |
292 | const void __user *s, unsigned int start, unsigned int count) | |
293 | { | |
294 | struct persistent_ram_buffer *buffer = prz->buffer; | |
295 | int ret = unlikely(__copy_from_user(buffer->data + start, s, count)) ? | |
296 | -EFAULT : 0; | |
297 | persistent_ram_update_ecc(prz, start, count); | |
298 | return ret; | |
299 | } | |
300 | ||
201e4aca | 301 | void persistent_ram_save_old(struct persistent_ram_zone *prz) |
9cc05ad9 CC |
302 | { |
303 | struct persistent_ram_buffer *buffer = prz->buffer; | |
808d0387 CC |
304 | size_t size = buffer_size(prz); |
305 | size_t start = buffer_start(prz); | |
9cc05ad9 | 306 | |
201e4aca AV |
307 | if (!size) |
308 | return; | |
c672528a | 309 | |
201e4aca AV |
310 | if (!prz->old_log) { |
311 | persistent_ram_ecc_old(prz); | |
312 | prz->old_log = kmalloc(size, GFP_KERNEL); | |
313 | } | |
314 | if (!prz->old_log) { | |
ef748853 | 315 | pr_err("failed to allocate buffer\n"); |
c672528a CC |
316 | return; |
317 | } | |
318 | ||
808d0387 | 319 | prz->old_log_size = size; |
d771fdf9 AB |
320 | memcpy_fromio(prz->old_log, &buffer->data[start], size - start); |
321 | memcpy_fromio(prz->old_log + size - start, &buffer->data[0], start); | |
c672528a CC |
322 | } |
323 | ||
a15d0b36 | 324 | int notrace persistent_ram_write(struct persistent_ram_zone *prz, |
c672528a CC |
325 | const void *s, unsigned int count) |
326 | { | |
327 | int rem; | |
328 | int c = count; | |
808d0387 | 329 | size_t start; |
c672528a | 330 | |
808d0387 | 331 | if (unlikely(c > prz->buffer_size)) { |
c672528a CC |
332 | s += c - prz->buffer_size; |
333 | c = prz->buffer_size; | |
334 | } | |
808d0387 | 335 | |
484dd30e | 336 | buffer_size_add(prz, c); |
808d0387 CC |
337 | |
338 | start = buffer_start_add(prz, c); | |
339 | ||
340 | rem = prz->buffer_size - start; | |
341 | if (unlikely(rem < c)) { | |
342 | persistent_ram_update(prz, s, start, rem); | |
c672528a CC |
343 | s += rem; |
344 | c -= rem; | |
808d0387 | 345 | start = 0; |
c672528a | 346 | } |
808d0387 | 347 | persistent_ram_update(prz, s, start, c); |
c672528a | 348 | |
9cc05ad9 | 349 | persistent_ram_update_header_ecc(prz); |
c672528a CC |
350 | |
351 | return count; | |
352 | } | |
353 | ||
5bf6d1b9 MS |
354 | int notrace persistent_ram_write_user(struct persistent_ram_zone *prz, |
355 | const void __user *s, unsigned int count) | |
356 | { | |
357 | int rem, ret = 0, c = count; | |
358 | size_t start; | |
359 | ||
96d4f267 | 360 | if (unlikely(!access_ok(s, count))) |
5bf6d1b9 MS |
361 | return -EFAULT; |
362 | if (unlikely(c > prz->buffer_size)) { | |
363 | s += c - prz->buffer_size; | |
364 | c = prz->buffer_size; | |
365 | } | |
366 | ||
367 | buffer_size_add(prz, c); | |
368 | ||
369 | start = buffer_start_add(prz, c); | |
370 | ||
371 | rem = prz->buffer_size - start; | |
372 | if (unlikely(rem < c)) { | |
373 | ret = persistent_ram_update_user(prz, s, start, rem); | |
374 | s += rem; | |
375 | c -= rem; | |
376 | start = 0; | |
377 | } | |
378 | if (likely(!ret)) | |
379 | ret = persistent_ram_update_user(prz, s, start, c); | |
380 | ||
381 | persistent_ram_update_header_ecc(prz); | |
382 | ||
383 | return unlikely(ret) ? ret : count; | |
384 | } | |
385 | ||
c672528a CC |
386 | size_t persistent_ram_old_size(struct persistent_ram_zone *prz) |
387 | { | |
388 | return prz->old_log_size; | |
389 | } | |
390 | ||
391 | void *persistent_ram_old(struct persistent_ram_zone *prz) | |
392 | { | |
393 | return prz->old_log; | |
394 | } | |
395 | ||
396 | void persistent_ram_free_old(struct persistent_ram_zone *prz) | |
397 | { | |
398 | kfree(prz->old_log); | |
399 | prz->old_log = NULL; | |
400 | prz->old_log_size = 0; | |
401 | } | |
402 | ||
fce39793 AV |
403 | void persistent_ram_zap(struct persistent_ram_zone *prz) |
404 | { | |
405 | atomic_set(&prz->buffer->start, 0); | |
406 | atomic_set(&prz->buffer->size, 0); | |
407 | persistent_ram_update_header_ecc(prz); | |
408 | } | |
409 | ||
027bc8b0 TL |
410 | static void *persistent_ram_vmap(phys_addr_t start, size_t size, |
411 | unsigned int memtype) | |
c672528a | 412 | { |
404a6043 CC |
413 | struct page **pages; |
414 | phys_addr_t page_start; | |
415 | unsigned int page_count; | |
416 | pgprot_t prot; | |
417 | unsigned int i; | |
2b1321e4 | 418 | void *vaddr; |
404a6043 CC |
419 | |
420 | page_start = start - offset_in_page(start); | |
421 | page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE); | |
422 | ||
027bc8b0 TL |
423 | if (memtype) |
424 | prot = pgprot_noncached(PAGE_KERNEL); | |
425 | else | |
426 | prot = pgprot_writecombine(PAGE_KERNEL); | |
404a6043 | 427 | |
b8f52d89 | 428 | pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL); |
404a6043 | 429 | if (!pages) { |
ef748853 FF |
430 | pr_err("%s: Failed to allocate array for %u pages\n", |
431 | __func__, page_count); | |
2b1321e4 | 432 | return NULL; |
404a6043 CC |
433 | } |
434 | ||
435 | for (i = 0; i < page_count; i++) { | |
436 | phys_addr_t addr = page_start + i * PAGE_SIZE; | |
437 | pages[i] = pfn_to_page(addr >> PAGE_SHIFT); | |
438 | } | |
2b1321e4 | 439 | vaddr = vmap(pages, page_count, VM_MAP, prot); |
404a6043 | 440 | kfree(pages); |
2b1321e4 | 441 | |
831b624d BY |
442 | /* |
443 | * Since vmap() uses page granularity, we must add the offset | |
444 | * into the page here, to get the byte granularity address | |
445 | * into the mapping to represent the actual "start" location. | |
446 | */ | |
447 | return vaddr + offset_in_page(start); | |
2b1321e4 AV |
448 | } |
449 | ||
027bc8b0 | 450 | static void *persistent_ram_iomap(phys_addr_t start, size_t size, |
1227daa4 | 451 | unsigned int memtype, char *label) |
24c3d2f3 | 452 | { |
027bc8b0 TL |
453 | void *va; |
454 | ||
1227daa4 | 455 | if (!request_mem_region(start, size, label ?: "ramoops")) { |
9ee85b8b KC |
456 | pr_err("request mem region (%s 0x%llx@0x%llx) failed\n", |
457 | label ?: "ramoops", | |
24c3d2f3 AV |
458 | (unsigned long long)size, (unsigned long long)start); |
459 | return NULL; | |
460 | } | |
461 | ||
027bc8b0 TL |
462 | if (memtype) |
463 | va = ioremap(start, size); | |
464 | else | |
465 | va = ioremap_wc(start, size); | |
466 | ||
831b624d BY |
467 | /* |
468 | * Since request_mem_region() and ioremap() are byte-granularity | |
469 | * there is no need handle anything special like we do when the | |
470 | * vmap() case in persistent_ram_vmap() above. | |
471 | */ | |
027bc8b0 | 472 | return va; |
24c3d2f3 AV |
473 | } |
474 | ||
2b1321e4 | 475 | static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size, |
027bc8b0 | 476 | struct persistent_ram_zone *prz, int memtype) |
2b1321e4 | 477 | { |
d3b48769 AV |
478 | prz->paddr = start; |
479 | prz->size = size; | |
480 | ||
24c3d2f3 | 481 | if (pfn_valid(start >> PAGE_SHIFT)) |
027bc8b0 | 482 | prz->vaddr = persistent_ram_vmap(start, size, memtype); |
24c3d2f3 | 483 | else |
1227daa4 KC |
484 | prz->vaddr = persistent_ram_iomap(start, size, memtype, |
485 | prz->label); | |
24c3d2f3 | 486 | |
404a6043 | 487 | if (!prz->vaddr) { |
2b1321e4 AV |
488 | pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__, |
489 | (unsigned long long)size, (unsigned long long)start); | |
404a6043 CC |
490 | return -ENOMEM; |
491 | } | |
492 | ||
831b624d | 493 | prz->buffer = prz->vaddr; |
404a6043 CC |
494 | prz->buffer_size = size - sizeof(struct persistent_ram_buffer); |
495 | ||
496 | return 0; | |
497 | } | |
498 | ||
f568f6ca | 499 | static int persistent_ram_post_init(struct persistent_ram_zone *prz, u32 sig, |
76d5692a | 500 | struct persistent_ram_ecc_info *ecc_info) |
404a6043 | 501 | { |
bb4206f2 | 502 | int ret; |
7684bd33 | 503 | bool zap = !!(prz->flags & PRZ_FLAG_ZAP_OLD); |
c672528a | 504 | |
c31ad081 | 505 | ret = persistent_ram_init_ecc(prz, ecc_info); |
0eed84ff KC |
506 | if (ret) { |
507 | pr_warn("ECC failed %s\n", prz->label); | |
bb4206f2 | 508 | return ret; |
0eed84ff | 509 | } |
c672528a | 510 | |
cbe7cbf5 AV |
511 | sig ^= PERSISTENT_RAM_SIG; |
512 | ||
513 | if (prz->buffer->sig == sig) { | |
30696378 JFG |
514 | if (buffer_size(prz) == 0) { |
515 | pr_debug("found existing empty buffer\n"); | |
516 | return 0; | |
517 | } | |
518 | ||
808d0387 | 519 | if (buffer_size(prz) > prz->buffer_size || |
7684bd33 | 520 | buffer_start(prz) > buffer_size(prz)) { |
ef748853 FF |
521 | pr_info("found existing invalid buffer, size %zu, start %zu\n", |
522 | buffer_size(prz), buffer_start(prz)); | |
7684bd33 PW |
523 | zap = true; |
524 | } else { | |
ef748853 FF |
525 | pr_debug("found existing buffer, size %zu, start %zu\n", |
526 | buffer_size(prz), buffer_start(prz)); | |
c672528a CC |
527 | persistent_ram_save_old(prz); |
528 | } | |
529 | } else { | |
ef748853 FF |
530 | pr_debug("no valid data in buffer (sig = 0x%08x)\n", |
531 | prz->buffer->sig); | |
7684bd33 PW |
532 | prz->buffer->sig = sig; |
533 | zap = true; | |
c672528a CC |
534 | } |
535 | ||
7684bd33 PW |
536 | /* Reset missing, invalid, or single-use memory area. */ |
537 | if (zap) | |
538 | persistent_ram_zap(prz); | |
c672528a | 539 | |
bb4206f2 AV |
540 | return 0; |
541 | } | |
542 | ||
d3b48769 AV |
543 | void persistent_ram_free(struct persistent_ram_zone *prz) |
544 | { | |
beeb9432 AV |
545 | if (!prz) |
546 | return; | |
547 | ||
548 | if (prz->vaddr) { | |
549 | if (pfn_valid(prz->paddr >> PAGE_SHIFT)) { | |
831b624d BY |
550 | /* We must vunmap() at page-granularity. */ |
551 | vunmap(prz->vaddr - offset_in_page(prz->paddr)); | |
beeb9432 AV |
552 | } else { |
553 | iounmap(prz->vaddr); | |
554 | release_mem_region(prz->paddr, prz->size); | |
555 | } | |
556 | prz->vaddr = NULL; | |
d3b48769 | 557 | } |
f2531f19 KC |
558 | if (prz->rs_decoder) { |
559 | free_rs(prz->rs_decoder); | |
560 | prz->rs_decoder = NULL; | |
561 | } | |
562 | kfree(prz->ecc_info.par); | |
563 | prz->ecc_info.par = NULL; | |
564 | ||
d3b48769 | 565 | persistent_ram_free_old(prz); |
1227daa4 | 566 | kfree(prz->label); |
d3b48769 AV |
567 | kfree(prz); |
568 | } | |
569 | ||
f568f6ca | 570 | struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size, |
027bc8b0 | 571 | u32 sig, struct persistent_ram_ecc_info *ecc_info, |
1227daa4 | 572 | unsigned int memtype, u32 flags, char *label) |
8cf5aff8 AV |
573 | { |
574 | struct persistent_ram_zone *prz; | |
575 | int ret = -ENOMEM; | |
576 | ||
577 | prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL); | |
578 | if (!prz) { | |
ef748853 | 579 | pr_err("failed to allocate persistent ram zone\n"); |
8cf5aff8 AV |
580 | goto err; |
581 | } | |
582 | ||
76d5692a | 583 | /* Initialize general buffer state. */ |
e9a330c4 | 584 | raw_spin_lock_init(&prz->buffer_lock); |
76d5692a | 585 | prz->flags = flags; |
1227daa4 | 586 | prz->label = label; |
76d5692a | 587 | |
027bc8b0 | 588 | ret = persistent_ram_buffer_map(start, size, prz, memtype); |
8cf5aff8 AV |
589 | if (ret) |
590 | goto err; | |
591 | ||
76d5692a | 592 | ret = persistent_ram_post_init(prz, sig, ecc_info); |
beeb9432 AV |
593 | if (ret) |
594 | goto err; | |
8cf5aff8 | 595 | |
dc80b1ea KC |
596 | pr_debug("attached %s 0x%zx@0x%llx: %zu header, %zu data, %zu ecc (%d/%d)\n", |
597 | prz->label, prz->size, (unsigned long long)prz->paddr, | |
598 | sizeof(*prz->buffer), prz->buffer_size, | |
599 | prz->size - sizeof(*prz->buffer) - prz->buffer_size, | |
600 | prz->ecc_info.ecc_size, prz->ecc_info.block_size); | |
601 | ||
8cf5aff8 AV |
602 | return prz; |
603 | err: | |
beeb9432 | 604 | persistent_ram_free(prz); |
8cf5aff8 AV |
605 | return ERR_PTR(ret); |
606 | } |