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) "persistent_ram: " fmt
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/memblock.h>
25 #include <linux/rslib.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/pstore_ram.h>
31 struct persistent_ram_buffer {
38 #define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */
40 static inline size_t buffer_size(struct persistent_ram_zone *prz)
42 return atomic_read(&prz->buffer->size);
45 static inline size_t buffer_start(struct persistent_ram_zone *prz)
47 return atomic_read(&prz->buffer->start);
50 static DEFINE_RAW_SPINLOCK(buffer_lock);
52 /* increase and wrap the start pointer, returning the old value */
53 static size_t buffer_start_add(struct persistent_ram_zone *prz, size_t a)
59 raw_spin_lock_irqsave(&buffer_lock, flags);
61 old = atomic_read(&prz->buffer->start);
63 while (unlikely(new >= prz->buffer_size))
64 new -= prz->buffer_size;
65 atomic_set(&prz->buffer->start, new);
67 raw_spin_unlock_irqrestore(&buffer_lock, flags);
72 /* increase the size counter until it hits the max size */
73 static void buffer_size_add(struct persistent_ram_zone *prz, size_t a)
79 raw_spin_lock_irqsave(&buffer_lock, flags);
81 old = atomic_read(&prz->buffer->size);
82 if (old == prz->buffer_size)
86 if (new > prz->buffer_size)
87 new = prz->buffer_size;
88 atomic_set(&prz->buffer->size, new);
91 raw_spin_unlock_irqrestore(&buffer_lock, flags);
94 static void notrace persistent_ram_encode_rs8(struct persistent_ram_zone *prz,
95 uint8_t *data, size_t len, uint8_t *ecc)
98 uint16_t par[prz->ecc_info.ecc_size];
100 /* Initialize the parity buffer */
101 memset(par, 0, sizeof(par));
102 encode_rs8(prz->rs_decoder, data, len, par, 0);
103 for (i = 0; i < prz->ecc_info.ecc_size; i++)
107 static int persistent_ram_decode_rs8(struct persistent_ram_zone *prz,
108 void *data, size_t len, uint8_t *ecc)
111 uint16_t par[prz->ecc_info.ecc_size];
113 for (i = 0; i < prz->ecc_info.ecc_size; i++)
115 return decode_rs8(prz->rs_decoder, data, par, len,
116 NULL, 0, NULL, 0, NULL);
119 static void notrace persistent_ram_update_ecc(struct persistent_ram_zone *prz,
120 unsigned int start, unsigned int count)
122 struct persistent_ram_buffer *buffer = prz->buffer;
123 uint8_t *buffer_end = buffer->data + prz->buffer_size;
126 int ecc_block_size = prz->ecc_info.block_size;
127 int ecc_size = prz->ecc_info.ecc_size;
128 int size = ecc_block_size;
133 block = buffer->data + (start & ~(ecc_block_size - 1));
134 par = prz->par_buffer + (start / ecc_block_size) * ecc_size;
137 if (block + ecc_block_size > buffer_end)
138 size = buffer_end - block;
139 persistent_ram_encode_rs8(prz, block, size, par);
140 block += ecc_block_size;
142 } while (block < buffer->data + start + count);
145 static void persistent_ram_update_header_ecc(struct persistent_ram_zone *prz)
147 struct persistent_ram_buffer *buffer = prz->buffer;
149 if (!prz->ecc_info.ecc_size)
152 persistent_ram_encode_rs8(prz, (uint8_t *)buffer, sizeof(*buffer),
156 static void persistent_ram_ecc_old(struct persistent_ram_zone *prz)
158 struct persistent_ram_buffer *buffer = prz->buffer;
162 if (!prz->ecc_info.ecc_size)
165 block = buffer->data;
166 par = prz->par_buffer;
167 while (block < buffer->data + buffer_size(prz)) {
169 int size = prz->ecc_info.block_size;
170 if (block + size > buffer->data + prz->buffer_size)
171 size = buffer->data + prz->buffer_size - block;
172 numerr = persistent_ram_decode_rs8(prz, block, size, par);
174 pr_devel("error in block %p, %d\n", block, numerr);
175 prz->corrected_bytes += numerr;
176 } else if (numerr < 0) {
177 pr_devel("uncorrectable error in block %p\n", block);
180 block += prz->ecc_info.block_size;
181 par += prz->ecc_info.ecc_size;
185 static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,
186 struct persistent_ram_ecc_info *ecc_info)
189 struct persistent_ram_buffer *buffer = prz->buffer;
193 if (!ecc_info || !ecc_info->ecc_size)
196 prz->ecc_info.block_size = ecc_info->block_size ?: 128;
197 prz->ecc_info.ecc_size = ecc_info->ecc_size ?: 16;
198 prz->ecc_info.symsize = ecc_info->symsize ?: 8;
199 prz->ecc_info.poly = ecc_info->poly ?: 0x11d;
201 ecc_blocks = DIV_ROUND_UP(prz->buffer_size - prz->ecc_info.ecc_size,
202 prz->ecc_info.block_size +
203 prz->ecc_info.ecc_size);
204 ecc_total = (ecc_blocks + 1) * prz->ecc_info.ecc_size;
205 if (ecc_total >= prz->buffer_size) {
206 pr_err("%s: invalid ecc_size %u (total %zu, buffer size %zu)\n",
207 __func__, prz->ecc_info.ecc_size,
208 ecc_total, prz->buffer_size);
212 prz->buffer_size -= ecc_total;
213 prz->par_buffer = buffer->data + prz->buffer_size;
214 prz->par_header = prz->par_buffer +
215 ecc_blocks * prz->ecc_info.ecc_size;
218 * first consecutive root is 0
219 * primitive element to generate roots = 1
221 prz->rs_decoder = init_rs(prz->ecc_info.symsize, prz->ecc_info.poly,
222 0, 1, prz->ecc_info.ecc_size);
223 if (prz->rs_decoder == NULL) {
224 pr_info("init_rs failed\n");
228 prz->corrected_bytes = 0;
231 numerr = persistent_ram_decode_rs8(prz, buffer, sizeof(*buffer),
234 pr_info("error in header, %d\n", numerr);
235 prz->corrected_bytes += numerr;
236 } else if (numerr < 0) {
237 pr_info("uncorrectable error in header\n");
244 ssize_t persistent_ram_ecc_string(struct persistent_ram_zone *prz,
245 char *str, size_t len)
249 if (!prz->ecc_info.ecc_size)
252 if (prz->corrected_bytes || prz->bad_blocks)
253 ret = snprintf(str, len, ""
254 "\n%d Corrected bytes, %d unrecoverable blocks\n",
255 prz->corrected_bytes, prz->bad_blocks);
257 ret = snprintf(str, len, "\nNo errors detected\n");
262 static void notrace persistent_ram_update(struct persistent_ram_zone *prz,
263 const void *s, unsigned int start, unsigned int count)
265 struct persistent_ram_buffer *buffer = prz->buffer;
266 memcpy_toio(buffer->data + start, s, count);
267 persistent_ram_update_ecc(prz, start, count);
270 void persistent_ram_save_old(struct persistent_ram_zone *prz)
272 struct persistent_ram_buffer *buffer = prz->buffer;
273 size_t size = buffer_size(prz);
274 size_t start = buffer_start(prz);
280 persistent_ram_ecc_old(prz);
281 prz->old_log = kmalloc(size, GFP_KERNEL);
284 pr_err("failed to allocate buffer\n");
288 prz->old_log_size = size;
289 memcpy(prz->old_log, &buffer->data[start], size - start);
290 memcpy(prz->old_log + size - start, &buffer->data[0], start);
293 int notrace persistent_ram_write(struct persistent_ram_zone *prz,
294 const void *s, unsigned int count)
300 if (unlikely(c > prz->buffer_size)) {
301 s += c - prz->buffer_size;
302 c = prz->buffer_size;
305 buffer_size_add(prz, c);
307 start = buffer_start_add(prz, c);
309 rem = prz->buffer_size - start;
310 if (unlikely(rem < c)) {
311 persistent_ram_update(prz, s, start, rem);
316 persistent_ram_update(prz, s, start, c);
318 persistent_ram_update_header_ecc(prz);
323 size_t persistent_ram_old_size(struct persistent_ram_zone *prz)
325 return prz->old_log_size;
328 void *persistent_ram_old(struct persistent_ram_zone *prz)
333 void persistent_ram_free_old(struct persistent_ram_zone *prz)
337 prz->old_log_size = 0;
340 void persistent_ram_zap(struct persistent_ram_zone *prz)
342 atomic_set(&prz->buffer->start, 0);
343 atomic_set(&prz->buffer->size, 0);
344 persistent_ram_update_header_ecc(prz);
347 static void *persistent_ram_vmap(phys_addr_t start, size_t size,
348 unsigned int memtype)
351 phys_addr_t page_start;
352 unsigned int page_count;
357 page_start = start - offset_in_page(start);
358 page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE);
361 prot = pgprot_noncached(PAGE_KERNEL);
363 prot = pgprot_writecombine(PAGE_KERNEL);
365 pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
367 pr_err("%s: Failed to allocate array for %u pages\n",
368 __func__, page_count);
372 for (i = 0; i < page_count; i++) {
373 phys_addr_t addr = page_start + i * PAGE_SIZE;
374 pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
376 vaddr = vmap(pages, page_count, VM_MAP, prot);
382 static void *persistent_ram_iomap(phys_addr_t start, size_t size,
383 unsigned int memtype)
387 if (!request_mem_region(start, size, "persistent_ram")) {
388 pr_err("request mem region (0x%llx@0x%llx) failed\n",
389 (unsigned long long)size, (unsigned long long)start);
394 va = ioremap(start, size);
396 va = ioremap_wc(start, size);
401 static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size,
402 struct persistent_ram_zone *prz, int memtype)
407 if (pfn_valid(start >> PAGE_SHIFT))
408 prz->vaddr = persistent_ram_vmap(start, size, memtype);
410 prz->vaddr = persistent_ram_iomap(start, size, memtype);
413 pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__,
414 (unsigned long long)size, (unsigned long long)start);
418 prz->buffer = prz->vaddr + offset_in_page(start);
419 prz->buffer_size = size - sizeof(struct persistent_ram_buffer);
424 static int persistent_ram_post_init(struct persistent_ram_zone *prz, u32 sig,
425 struct persistent_ram_ecc_info *ecc_info)
429 ret = persistent_ram_init_ecc(prz, ecc_info);
433 sig ^= PERSISTENT_RAM_SIG;
435 if (prz->buffer->sig == sig) {
436 if (buffer_size(prz) > prz->buffer_size ||
437 buffer_start(prz) > buffer_size(prz))
438 pr_info("found existing invalid buffer, size %zu, start %zu\n",
439 buffer_size(prz), buffer_start(prz));
441 pr_debug("found existing buffer, size %zu, start %zu\n",
442 buffer_size(prz), buffer_start(prz));
443 persistent_ram_save_old(prz);
447 pr_debug("no valid data in buffer (sig = 0x%08x)\n",
451 prz->buffer->sig = sig;
452 persistent_ram_zap(prz);
457 void persistent_ram_free(struct persistent_ram_zone *prz)
463 if (pfn_valid(prz->paddr >> PAGE_SHIFT)) {
467 release_mem_region(prz->paddr, prz->size);
471 persistent_ram_free_old(prz);
475 struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
476 u32 sig, struct persistent_ram_ecc_info *ecc_info,
477 unsigned int memtype)
479 struct persistent_ram_zone *prz;
482 prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
484 pr_err("failed to allocate persistent ram zone\n");
488 ret = persistent_ram_buffer_map(start, size, prz, memtype);
492 ret = persistent_ram_post_init(prz, sig, ecc_info);
498 persistent_ram_free(prz);