2 * Copyright (C) 2012 Google, Inc.
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.
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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/memblock.h>
25 #include <linux/pstore_ram.h>
26 #include <linux/rslib.h>
27 #include <linux/slab.h>
28 #include <linux/uaccess.h>
29 #include <linux/vmalloc.h>
33 * struct persistent_ram_buffer - persistent circular RAM buffer
36 * signature to indicate header (PERSISTENT_RAM_SIG xor PRZ-type value)
38 * offset into @data where the beginning of the stored bytes begin
40 * number of valid bytes stored in @data
42 struct persistent_ram_buffer {
49 #define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */
51 static inline size_t buffer_size(struct persistent_ram_zone *prz)
53 return atomic_read(&prz->buffer->size);
56 static inline size_t buffer_start(struct persistent_ram_zone *prz)
58 return atomic_read(&prz->buffer->start);
61 /* increase and wrap the start pointer, returning the old value */
62 static size_t buffer_start_add(struct persistent_ram_zone *prz, size_t a)
66 unsigned long flags = 0;
68 if (!(prz->flags & PRZ_FLAG_NO_LOCK))
69 raw_spin_lock_irqsave(&prz->buffer_lock, flags);
71 old = atomic_read(&prz->buffer->start);
73 while (unlikely(new >= prz->buffer_size))
74 new -= prz->buffer_size;
75 atomic_set(&prz->buffer->start, new);
77 if (!(prz->flags & PRZ_FLAG_NO_LOCK))
78 raw_spin_unlock_irqrestore(&prz->buffer_lock, flags);
83 /* increase the size counter until it hits the max size */
84 static void buffer_size_add(struct persistent_ram_zone *prz, size_t a)
88 unsigned long flags = 0;
90 if (!(prz->flags & PRZ_FLAG_NO_LOCK))
91 raw_spin_lock_irqsave(&prz->buffer_lock, flags);
93 old = atomic_read(&prz->buffer->size);
94 if (old == prz->buffer_size)
98 if (new > prz->buffer_size)
99 new = prz->buffer_size;
100 atomic_set(&prz->buffer->size, new);
103 if (!(prz->flags & PRZ_FLAG_NO_LOCK))
104 raw_spin_unlock_irqrestore(&prz->buffer_lock, flags);
107 static void notrace persistent_ram_encode_rs8(struct persistent_ram_zone *prz,
108 uint8_t *data, size_t len, uint8_t *ecc)
112 /* Initialize the parity buffer */
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);
116 for (i = 0; i < prz->ecc_info.ecc_size; i++)
117 ecc[i] = prz->ecc_info.par[i];
120 static int persistent_ram_decode_rs8(struct persistent_ram_zone *prz,
121 void *data, size_t len, uint8_t *ecc)
125 for (i = 0; i < prz->ecc_info.ecc_size; i++)
126 prz->ecc_info.par[i] = ecc[i];
127 return decode_rs8(prz->rs_decoder, data, prz->ecc_info.par, len,
128 NULL, 0, NULL, 0, NULL);
131 static void notrace persistent_ram_update_ecc(struct persistent_ram_zone *prz,
132 unsigned int start, unsigned int count)
134 struct persistent_ram_buffer *buffer = prz->buffer;
135 uint8_t *buffer_end = buffer->data + prz->buffer_size;
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;
145 block = buffer->data + (start & ~(ecc_block_size - 1));
146 par = prz->par_buffer + (start / ecc_block_size) * ecc_size;
149 if (block + ecc_block_size > buffer_end)
150 size = buffer_end - block;
151 persistent_ram_encode_rs8(prz, block, size, par);
152 block += ecc_block_size;
154 } while (block < buffer->data + start + count);
157 static void persistent_ram_update_header_ecc(struct persistent_ram_zone *prz)
159 struct persistent_ram_buffer *buffer = prz->buffer;
161 if (!prz->ecc_info.ecc_size)
164 persistent_ram_encode_rs8(prz, (uint8_t *)buffer, sizeof(*buffer),
168 static void persistent_ram_ecc_old(struct persistent_ram_zone *prz)
170 struct persistent_ram_buffer *buffer = prz->buffer;
174 if (!prz->ecc_info.ecc_size)
177 block = buffer->data;
178 par = prz->par_buffer;
179 while (block < buffer->data + buffer_size(prz)) {
181 int size = prz->ecc_info.block_size;
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);
186 pr_devel("error in block %p, %d\n", block, numerr);
187 prz->corrected_bytes += numerr;
188 } else if (numerr < 0) {
189 pr_devel("uncorrectable error in block %p\n", block);
192 block += prz->ecc_info.block_size;
193 par += prz->ecc_info.ecc_size;
197 static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,
198 struct persistent_ram_ecc_info *ecc_info)
201 struct persistent_ram_buffer *buffer = prz->buffer;
205 if (!ecc_info || !ecc_info->ecc_size)
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;
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;
217 if (ecc_total >= prz->buffer_size) {
218 pr_err("%s: invalid ecc_size %u (total %zu, buffer size %zu)\n",
219 __func__, prz->ecc_info.ecc_size,
220 ecc_total, prz->buffer_size);
224 prz->buffer_size -= ecc_total;
225 prz->par_buffer = buffer->data + prz->buffer_size;
226 prz->par_header = prz->par_buffer +
227 ecc_blocks * prz->ecc_info.ecc_size;
230 * first consecutive root is 0
231 * primitive element to generate roots = 1
233 prz->rs_decoder = init_rs(prz->ecc_info.symsize, prz->ecc_info.poly,
234 0, 1, prz->ecc_info.ecc_size);
235 if (prz->rs_decoder == NULL) {
236 pr_info("init_rs failed\n");
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),
244 if (!prz->ecc_info.par) {
245 pr_err("cannot allocate ECC parity workspace\n");
249 prz->corrected_bytes = 0;
252 numerr = persistent_ram_decode_rs8(prz, buffer, sizeof(*buffer),
255 pr_info("error in header, %d\n", numerr);
256 prz->corrected_bytes += numerr;
257 } else if (numerr < 0) {
258 pr_info("uncorrectable error in header\n");
265 ssize_t persistent_ram_ecc_string(struct persistent_ram_zone *prz,
266 char *str, size_t len)
270 if (!prz->ecc_info.ecc_size)
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);
278 ret = snprintf(str, len, "\nNo errors detected\n");
283 static void notrace persistent_ram_update(struct persistent_ram_zone *prz,
284 const void *s, unsigned int start, unsigned int count)
286 struct persistent_ram_buffer *buffer = prz->buffer;
287 memcpy_toio(buffer->data + start, s, count);
288 persistent_ram_update_ecc(prz, start, count);
291 static int notrace persistent_ram_update_user(struct persistent_ram_zone *prz,
292 const void __user *s, unsigned int start, unsigned int count)
294 struct persistent_ram_buffer *buffer = prz->buffer;
295 int ret = unlikely(__copy_from_user(buffer->data + start, s, count)) ?
297 persistent_ram_update_ecc(prz, start, count);
301 void persistent_ram_save_old(struct persistent_ram_zone *prz)
303 struct persistent_ram_buffer *buffer = prz->buffer;
304 size_t size = buffer_size(prz);
305 size_t start = buffer_start(prz);
311 persistent_ram_ecc_old(prz);
312 prz->old_log = kmalloc(size, GFP_KERNEL);
315 pr_err("failed to allocate buffer\n");
319 prz->old_log_size = size;
320 memcpy_fromio(prz->old_log, &buffer->data[start], size - start);
321 memcpy_fromio(prz->old_log + size - start, &buffer->data[0], start);
324 int notrace persistent_ram_write(struct persistent_ram_zone *prz,
325 const void *s, unsigned int count)
331 if (unlikely(c > prz->buffer_size)) {
332 s += c - prz->buffer_size;
333 c = prz->buffer_size;
336 buffer_size_add(prz, c);
338 start = buffer_start_add(prz, c);
340 rem = prz->buffer_size - start;
341 if (unlikely(rem < c)) {
342 persistent_ram_update(prz, s, start, rem);
347 persistent_ram_update(prz, s, start, c);
349 persistent_ram_update_header_ecc(prz);
354 int notrace persistent_ram_write_user(struct persistent_ram_zone *prz,
355 const void __user *s, unsigned int count)
357 int rem, ret = 0, c = count;
360 if (unlikely(!access_ok(s, count)))
362 if (unlikely(c > prz->buffer_size)) {
363 s += c - prz->buffer_size;
364 c = prz->buffer_size;
367 buffer_size_add(prz, c);
369 start = buffer_start_add(prz, c);
371 rem = prz->buffer_size - start;
372 if (unlikely(rem < c)) {
373 ret = persistent_ram_update_user(prz, s, start, rem);
379 ret = persistent_ram_update_user(prz, s, start, c);
381 persistent_ram_update_header_ecc(prz);
383 return unlikely(ret) ? ret : count;
386 size_t persistent_ram_old_size(struct persistent_ram_zone *prz)
388 return prz->old_log_size;
391 void *persistent_ram_old(struct persistent_ram_zone *prz)
396 void persistent_ram_free_old(struct persistent_ram_zone *prz)
400 prz->old_log_size = 0;
403 void persistent_ram_zap(struct persistent_ram_zone *prz)
405 atomic_set(&prz->buffer->start, 0);
406 atomic_set(&prz->buffer->size, 0);
407 persistent_ram_update_header_ecc(prz);
410 static void *persistent_ram_vmap(phys_addr_t start, size_t size,
411 unsigned int memtype)
414 phys_addr_t page_start;
415 unsigned int page_count;
420 page_start = start - offset_in_page(start);
421 page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE);
424 prot = pgprot_noncached(PAGE_KERNEL);
426 prot = pgprot_writecombine(PAGE_KERNEL);
428 pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
430 pr_err("%s: Failed to allocate array for %u pages\n",
431 __func__, page_count);
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);
439 vaddr = vmap(pages, page_count, VM_MAP, prot);
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.
447 return vaddr + offset_in_page(start);
450 static void *persistent_ram_iomap(phys_addr_t start, size_t size,
451 unsigned int memtype, char *label)
455 if (!request_mem_region(start, size, label ?: "ramoops")) {
456 pr_err("request mem region (%s 0x%llx@0x%llx) failed\n",
458 (unsigned long long)size, (unsigned long long)start);
463 va = ioremap(start, size);
465 va = ioremap_wc(start, size);
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.
475 static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size,
476 struct persistent_ram_zone *prz, int memtype)
481 if (pfn_valid(start >> PAGE_SHIFT))
482 prz->vaddr = persistent_ram_vmap(start, size, memtype);
484 prz->vaddr = persistent_ram_iomap(start, size, memtype,
488 pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__,
489 (unsigned long long)size, (unsigned long long)start);
493 prz->buffer = prz->vaddr;
494 prz->buffer_size = size - sizeof(struct persistent_ram_buffer);
499 static int persistent_ram_post_init(struct persistent_ram_zone *prz, u32 sig,
500 struct persistent_ram_ecc_info *ecc_info)
503 bool zap = !!(prz->flags & PRZ_FLAG_ZAP_OLD);
505 ret = persistent_ram_init_ecc(prz, ecc_info);
507 pr_warn("ECC failed %s\n", prz->label);
511 sig ^= PERSISTENT_RAM_SIG;
513 if (prz->buffer->sig == sig) {
514 if (buffer_size(prz) == 0) {
515 pr_debug("found existing empty buffer\n");
519 if (buffer_size(prz) > prz->buffer_size ||
520 buffer_start(prz) > buffer_size(prz)) {
521 pr_info("found existing invalid buffer, size %zu, start %zu\n",
522 buffer_size(prz), buffer_start(prz));
525 pr_debug("found existing buffer, size %zu, start %zu\n",
526 buffer_size(prz), buffer_start(prz));
527 persistent_ram_save_old(prz);
530 pr_debug("no valid data in buffer (sig = 0x%08x)\n",
532 prz->buffer->sig = sig;
536 /* Reset missing, invalid, or single-use memory area. */
538 persistent_ram_zap(prz);
543 void persistent_ram_free(struct persistent_ram_zone *prz)
549 if (pfn_valid(prz->paddr >> PAGE_SHIFT)) {
550 /* We must vunmap() at page-granularity. */
551 vunmap(prz->vaddr - offset_in_page(prz->paddr));
554 release_mem_region(prz->paddr, prz->size);
558 if (prz->rs_decoder) {
559 free_rs(prz->rs_decoder);
560 prz->rs_decoder = NULL;
562 kfree(prz->ecc_info.par);
563 prz->ecc_info.par = NULL;
565 persistent_ram_free_old(prz);
570 struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
571 u32 sig, struct persistent_ram_ecc_info *ecc_info,
572 unsigned int memtype, u32 flags, char *label)
574 struct persistent_ram_zone *prz;
577 prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
579 pr_err("failed to allocate persistent ram zone\n");
583 /* Initialize general buffer state. */
584 raw_spin_lock_init(&prz->buffer_lock);
588 ret = persistent_ram_buffer_map(start, size, prz, memtype);
592 ret = persistent_ram_post_init(prz, sig, ecc_info);
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);
604 persistent_ram_free(prz);