Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / fs / pstore / ram_core.c
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
15 #define pr_fmt(fmt) "persistent_ram: " fmt
16
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>
22 #include <linux/io.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>
29 #include <asm/page.h>
30
31 struct persistent_ram_buffer {
32         uint32_t    sig;
33         atomic_t    start;
34         atomic_t    size;
35         uint8_t     data[0];
36 };
37
38 #define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */
39
40 static inline size_t buffer_size(struct persistent_ram_zone *prz)
41 {
42         return atomic_read(&prz->buffer->size);
43 }
44
45 static inline size_t buffer_start(struct persistent_ram_zone *prz)
46 {
47         return atomic_read(&prz->buffer->start);
48 }
49
50 static DEFINE_RAW_SPINLOCK(buffer_lock);
51
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)
54 {
55         int old;
56         int new;
57         unsigned long flags;
58
59         raw_spin_lock_irqsave(&buffer_lock, flags);
60
61         old = atomic_read(&prz->buffer->start);
62         new = old + a;
63         while (unlikely(new >= prz->buffer_size))
64                 new -= prz->buffer_size;
65         atomic_set(&prz->buffer->start, new);
66
67         raw_spin_unlock_irqrestore(&buffer_lock, flags);
68
69         return old;
70 }
71
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)
74 {
75         size_t old;
76         size_t new;
77         unsigned long flags;
78
79         raw_spin_lock_irqsave(&buffer_lock, flags);
80
81         old = atomic_read(&prz->buffer->size);
82         if (old == prz->buffer_size)
83                 goto exit;
84
85         new = old + a;
86         if (new > prz->buffer_size)
87                 new = prz->buffer_size;
88         atomic_set(&prz->buffer->size, new);
89
90 exit:
91         raw_spin_unlock_irqrestore(&buffer_lock, flags);
92 }
93
94 static void notrace persistent_ram_encode_rs8(struct persistent_ram_zone *prz,
95         uint8_t *data, size_t len, uint8_t *ecc)
96 {
97         int i;
98         uint16_t par[prz->ecc_info.ecc_size];
99
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++)
104                 ecc[i] = par[i];
105 }
106
107 static int persistent_ram_decode_rs8(struct persistent_ram_zone *prz,
108         void *data, size_t len, uint8_t *ecc)
109 {
110         int i;
111         uint16_t par[prz->ecc_info.ecc_size];
112
113         for (i = 0; i < prz->ecc_info.ecc_size; i++)
114                 par[i] = ecc[i];
115         return decode_rs8(prz->rs_decoder, data, par, len,
116                                 NULL, 0, NULL, 0, NULL);
117 }
118
119 static void notrace persistent_ram_update_ecc(struct persistent_ram_zone *prz,
120         unsigned int start, unsigned int count)
121 {
122         struct persistent_ram_buffer *buffer = prz->buffer;
123         uint8_t *buffer_end = buffer->data + prz->buffer_size;
124         uint8_t *block;
125         uint8_t *par;
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;
129
130         if (!ecc_size)
131                 return;
132
133         block = buffer->data + (start & ~(ecc_block_size - 1));
134         par = prz->par_buffer + (start / ecc_block_size) * ecc_size;
135
136         do {
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;
141                 par += ecc_size;
142         } while (block < buffer->data + start + count);
143 }
144
145 static void persistent_ram_update_header_ecc(struct persistent_ram_zone *prz)
146 {
147         struct persistent_ram_buffer *buffer = prz->buffer;
148
149         if (!prz->ecc_info.ecc_size)
150                 return;
151
152         persistent_ram_encode_rs8(prz, (uint8_t *)buffer, sizeof(*buffer),
153                                   prz->par_header);
154 }
155
156 static void persistent_ram_ecc_old(struct persistent_ram_zone *prz)
157 {
158         struct persistent_ram_buffer *buffer = prz->buffer;
159         uint8_t *block;
160         uint8_t *par;
161
162         if (!prz->ecc_info.ecc_size)
163                 return;
164
165         block = buffer->data;
166         par = prz->par_buffer;
167         while (block < buffer->data + buffer_size(prz)) {
168                 int numerr;
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);
173                 if (numerr > 0) {
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);
178                         prz->bad_blocks++;
179                 }
180                 block += prz->ecc_info.block_size;
181                 par += prz->ecc_info.ecc_size;
182         }
183 }
184
185 static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,
186                                    struct persistent_ram_ecc_info *ecc_info)
187 {
188         int numerr;
189         struct persistent_ram_buffer *buffer = prz->buffer;
190         int ecc_blocks;
191         size_t ecc_total;
192
193         if (!ecc_info || !ecc_info->ecc_size)
194                 return 0;
195
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;
200
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);
209                 return -EINVAL;
210         }
211
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;
216
217         /*
218          * first consecutive root is 0
219          * primitive element to generate roots = 1
220          */
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");
225                 return -EINVAL;
226         }
227
228         prz->corrected_bytes = 0;
229         prz->bad_blocks = 0;
230
231         numerr = persistent_ram_decode_rs8(prz, buffer, sizeof(*buffer),
232                                            prz->par_header);
233         if (numerr > 0) {
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");
238                 prz->bad_blocks++;
239         }
240
241         return 0;
242 }
243
244 ssize_t persistent_ram_ecc_string(struct persistent_ram_zone *prz,
245         char *str, size_t len)
246 {
247         ssize_t ret;
248
249         if (!prz->ecc_info.ecc_size)
250                 return 0;
251
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);
256         else
257                 ret = snprintf(str, len, "\nNo errors detected\n");
258
259         return ret;
260 }
261
262 static void notrace persistent_ram_update(struct persistent_ram_zone *prz,
263         const void *s, unsigned int start, unsigned int count)
264 {
265         struct persistent_ram_buffer *buffer = prz->buffer;
266         memcpy_toio(buffer->data + start, s, count);
267         persistent_ram_update_ecc(prz, start, count);
268 }
269
270 void persistent_ram_save_old(struct persistent_ram_zone *prz)
271 {
272         struct persistent_ram_buffer *buffer = prz->buffer;
273         size_t size = buffer_size(prz);
274         size_t start = buffer_start(prz);
275
276         if (!size)
277                 return;
278
279         if (!prz->old_log) {
280                 persistent_ram_ecc_old(prz);
281                 prz->old_log = kmalloc(size, GFP_KERNEL);
282         }
283         if (!prz->old_log) {
284                 pr_err("failed to allocate buffer\n");
285                 return;
286         }
287
288         prz->old_log_size = size;
289         memcpy_fromio(prz->old_log, &buffer->data[start], size - start);
290         memcpy_fromio(prz->old_log + size - start, &buffer->data[0], start);
291 }
292
293 int notrace persistent_ram_write(struct persistent_ram_zone *prz,
294         const void *s, unsigned int count)
295 {
296         int rem;
297         int c = count;
298         size_t start;
299
300         if (unlikely(c > prz->buffer_size)) {
301                 s += c - prz->buffer_size;
302                 c = prz->buffer_size;
303         }
304
305         buffer_size_add(prz, c);
306
307         start = buffer_start_add(prz, c);
308
309         rem = prz->buffer_size - start;
310         if (unlikely(rem < c)) {
311                 persistent_ram_update(prz, s, start, rem);
312                 s += rem;
313                 c -= rem;
314                 start = 0;
315         }
316         persistent_ram_update(prz, s, start, c);
317
318         persistent_ram_update_header_ecc(prz);
319
320         return count;
321 }
322
323 size_t persistent_ram_old_size(struct persistent_ram_zone *prz)
324 {
325         return prz->old_log_size;
326 }
327
328 void *persistent_ram_old(struct persistent_ram_zone *prz)
329 {
330         return prz->old_log;
331 }
332
333 void persistent_ram_free_old(struct persistent_ram_zone *prz)
334 {
335         kfree(prz->old_log);
336         prz->old_log = NULL;
337         prz->old_log_size = 0;
338 }
339
340 void persistent_ram_zap(struct persistent_ram_zone *prz)
341 {
342         atomic_set(&prz->buffer->start, 0);
343         atomic_set(&prz->buffer->size, 0);
344         persistent_ram_update_header_ecc(prz);
345 }
346
347 static void *persistent_ram_vmap(phys_addr_t start, size_t size,
348                 unsigned int memtype)
349 {
350         struct page **pages;
351         phys_addr_t page_start;
352         unsigned int page_count;
353         pgprot_t prot;
354         unsigned int i;
355         void *vaddr;
356
357         page_start = start - offset_in_page(start);
358         page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE);
359
360         if (memtype)
361                 prot = pgprot_noncached(PAGE_KERNEL);
362         else
363                 prot = pgprot_writecombine(PAGE_KERNEL);
364
365         pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
366         if (!pages) {
367                 pr_err("%s: Failed to allocate array for %u pages\n",
368                        __func__, page_count);
369                 return NULL;
370         }
371
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);
375         }
376         vaddr = vmap(pages, page_count, VM_MAP, prot);
377         kfree(pages);
378
379         return vaddr;
380 }
381
382 static void *persistent_ram_iomap(phys_addr_t start, size_t size,
383                 unsigned int memtype)
384 {
385         void *va;
386
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);
390                 return NULL;
391         }
392
393         if (memtype)
394                 va = ioremap(start, size);
395         else
396                 va = ioremap_wc(start, size);
397
398         return va;
399 }
400
401 static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size,
402                 struct persistent_ram_zone *prz, int memtype)
403 {
404         prz->paddr = start;
405         prz->size = size;
406
407         if (pfn_valid(start >> PAGE_SHIFT))
408                 prz->vaddr = persistent_ram_vmap(start, size, memtype);
409         else
410                 prz->vaddr = persistent_ram_iomap(start, size, memtype);
411
412         if (!prz->vaddr) {
413                 pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__,
414                         (unsigned long long)size, (unsigned long long)start);
415                 return -ENOMEM;
416         }
417
418         prz->buffer = prz->vaddr + offset_in_page(start);
419         prz->buffer_size = size - sizeof(struct persistent_ram_buffer);
420
421         return 0;
422 }
423
424 static int persistent_ram_post_init(struct persistent_ram_zone *prz, u32 sig,
425                                     struct persistent_ram_ecc_info *ecc_info)
426 {
427         int ret;
428
429         ret = persistent_ram_init_ecc(prz, ecc_info);
430         if (ret)
431                 return ret;
432
433         sig ^= PERSISTENT_RAM_SIG;
434
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));
440                 else {
441                         pr_debug("found existing buffer, size %zu, start %zu\n",
442                                  buffer_size(prz), buffer_start(prz));
443                         persistent_ram_save_old(prz);
444                         return 0;
445                 }
446         } else {
447                 pr_debug("no valid data in buffer (sig = 0x%08x)\n",
448                          prz->buffer->sig);
449         }
450
451         prz->buffer->sig = sig;
452         persistent_ram_zap(prz);
453
454         return 0;
455 }
456
457 void persistent_ram_free(struct persistent_ram_zone *prz)
458 {
459         if (!prz)
460                 return;
461
462         if (prz->vaddr) {
463                 if (pfn_valid(prz->paddr >> PAGE_SHIFT)) {
464                         vunmap(prz->vaddr);
465                 } else {
466                         iounmap(prz->vaddr);
467                         release_mem_region(prz->paddr, prz->size);
468                 }
469                 prz->vaddr = NULL;
470         }
471         persistent_ram_free_old(prz);
472         kfree(prz);
473 }
474
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)
478 {
479         struct persistent_ram_zone *prz;
480         int ret = -ENOMEM;
481
482         prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
483         if (!prz) {
484                 pr_err("failed to allocate persistent ram zone\n");
485                 goto err;
486         }
487
488         ret = persistent_ram_buffer_map(start, size, prz, memtype);
489         if (ret)
490                 goto err;
491
492         ret = persistent_ram_post_init(prz, sig, ecc_info);
493         if (ret)
494                 goto err;
495
496         return prz;
497 err:
498         persistent_ram_free(prz);
499         return ERR_PTR(ret);
500 }