Merge branch linux-tegra-2.6.36 into android-tegra-2.6.36
[firefly-linux-kernel-4.4.55.git] / drivers / staging / android / ram_console.c
1 /* drivers/android/ram_console.c
2  *
3  * Copyright (C) 2007-2008 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/console.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/proc_fs.h>
21 #include <linux/string.h>
22 #include <linux/uaccess.h>
23 #include <linux/io.h>
24
25 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
26 #include <linux/rslib.h>
27 #endif
28
29 struct ram_console_buffer {
30         uint32_t    sig;
31         uint32_t    start;
32         uint32_t    size;
33         uint8_t     data[0];
34 };
35
36 #define RAM_CONSOLE_SIG (0x43474244) /* DBGC */
37
38 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
39 static char __initdata
40         ram_console_old_log_init_buffer[CONFIG_ANDROID_RAM_CONSOLE_EARLY_SIZE];
41 #endif
42 static char *ram_console_old_log;
43 static size_t ram_console_old_log_size;
44
45 static struct ram_console_buffer *ram_console_buffer;
46 static size_t ram_console_buffer_size;
47 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
48 static char *ram_console_par_buffer;
49 static struct rs_control *ram_console_rs_decoder;
50 static int ram_console_corrected_bytes;
51 static int ram_console_bad_blocks;
52 #define ECC_BLOCK_SIZE CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_DATA_SIZE
53 #define ECC_SIZE CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_ECC_SIZE
54 #define ECC_SYMSIZE CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_SYMBOL_SIZE
55 #define ECC_POLY CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION_POLYNOMIAL
56 #endif
57
58 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
59 static void ram_console_encode_rs8(uint8_t *data, size_t len, uint8_t *ecc)
60 {
61         int i;
62         uint16_t par[ECC_SIZE];
63         /* Initialize the parity buffer */
64         memset(par, 0, sizeof(par));
65         encode_rs8(ram_console_rs_decoder, data, len, par, 0);
66         for (i = 0; i < ECC_SIZE; i++)
67                 ecc[i] = par[i];
68 }
69
70 static int ram_console_decode_rs8(void *data, size_t len, uint8_t *ecc)
71 {
72         int i;
73         uint16_t par[ECC_SIZE];
74         for (i = 0; i < ECC_SIZE; i++)
75                 par[i] = ecc[i];
76         return decode_rs8(ram_console_rs_decoder, data, par, len,
77                                 NULL, 0, NULL, 0, NULL);
78 }
79 #endif
80
81 static void ram_console_update(const char *s, unsigned int count)
82 {
83         struct ram_console_buffer *buffer = ram_console_buffer;
84 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
85         uint8_t *buffer_end = buffer->data + ram_console_buffer_size;
86         uint8_t *block;
87         uint8_t *par;
88         int size = ECC_BLOCK_SIZE;
89 #endif
90         memcpy(buffer->data + buffer->start, s, count);
91 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
92         block = buffer->data + (buffer->start & ~(ECC_BLOCK_SIZE - 1));
93         par = ram_console_par_buffer +
94               (buffer->start / ECC_BLOCK_SIZE) * ECC_SIZE;
95         do {
96                 if (block + ECC_BLOCK_SIZE > buffer_end)
97                         size = buffer_end - block;
98                 ram_console_encode_rs8(block, size, par);
99                 block += ECC_BLOCK_SIZE;
100                 par += ECC_SIZE;
101         } while (block < buffer->data + buffer->start + count);
102 #endif
103 }
104
105 static void ram_console_update_header(void)
106 {
107 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
108         struct ram_console_buffer *buffer = ram_console_buffer;
109         uint8_t *par;
110         par = ram_console_par_buffer +
111               DIV_ROUND_UP(ram_console_buffer_size, ECC_BLOCK_SIZE) * ECC_SIZE;
112         ram_console_encode_rs8((uint8_t *)buffer, sizeof(*buffer), par);
113 #endif
114 }
115
116 static void
117 ram_console_write(struct console *console, const char *s, unsigned int count)
118 {
119         int rem;
120         struct ram_console_buffer *buffer = ram_console_buffer;
121
122         if (count > ram_console_buffer_size) {
123                 s += count - ram_console_buffer_size;
124                 count = ram_console_buffer_size;
125         }
126         rem = ram_console_buffer_size - buffer->start;
127         if (rem < count) {
128                 ram_console_update(s, rem);
129                 s += rem;
130                 count -= rem;
131                 buffer->start = 0;
132                 buffer->size = ram_console_buffer_size;
133         }
134         ram_console_update(s, count);
135
136         buffer->start += count;
137         if (buffer->size < ram_console_buffer_size)
138                 buffer->size += count;
139         ram_console_update_header();
140 }
141
142 static struct console ram_console = {
143         .name   = "ram",
144         .write  = ram_console_write,
145         .flags  = CON_PRINTBUFFER | CON_ENABLED,
146         .index  = -1,
147 };
148
149 void ram_console_enable_console(int enabled)
150 {
151         if (enabled)
152                 ram_console.flags |= CON_ENABLED;
153         else
154                 ram_console.flags &= ~CON_ENABLED;
155 }
156
157 static void __init
158 ram_console_save_old(struct ram_console_buffer *buffer, char *dest)
159 {
160         size_t old_log_size = buffer->size;
161 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
162         uint8_t *block;
163         uint8_t *par;
164         char strbuf[80];
165         int strbuf_len;
166
167         block = buffer->data;
168         par = ram_console_par_buffer;
169         while (block < buffer->data + buffer->size) {
170                 int numerr;
171                 int size = ECC_BLOCK_SIZE;
172                 if (block + size > buffer->data + ram_console_buffer_size)
173                         size = buffer->data + ram_console_buffer_size - block;
174                 numerr = ram_console_decode_rs8(block, size, par);
175                 if (numerr > 0) {
176 #if 0
177                         printk(KERN_INFO "ram_console: error in block %p, %d\n",
178                                block, numerr);
179 #endif
180                         ram_console_corrected_bytes += numerr;
181                 } else if (numerr < 0) {
182 #if 0
183                         printk(KERN_INFO "ram_console: uncorrectable error in "
184                                "block %p\n", block);
185 #endif
186                         ram_console_bad_blocks++;
187                 }
188                 block += ECC_BLOCK_SIZE;
189                 par += ECC_SIZE;
190         }
191         if (ram_console_corrected_bytes || ram_console_bad_blocks)
192                 strbuf_len = snprintf(strbuf, sizeof(strbuf),
193                         "\n%d Corrected bytes, %d unrecoverable blocks\n",
194                         ram_console_corrected_bytes, ram_console_bad_blocks);
195         else
196                 strbuf_len = snprintf(strbuf, sizeof(strbuf),
197                                       "\nNo errors detected\n");
198         if (strbuf_len >= sizeof(strbuf))
199                 strbuf_len = sizeof(strbuf) - 1;
200         old_log_size += strbuf_len;
201 #endif
202
203         if (dest == NULL) {
204                 dest = kmalloc(old_log_size, GFP_KERNEL);
205                 if (dest == NULL) {
206                         printk(KERN_ERR
207                                "ram_console: failed to allocate buffer\n");
208                         return;
209                 }
210         }
211
212         ram_console_old_log = dest;
213         ram_console_old_log_size = old_log_size;
214         memcpy(ram_console_old_log,
215                &buffer->data[buffer->start], buffer->size - buffer->start);
216         memcpy(ram_console_old_log + buffer->size - buffer->start,
217                &buffer->data[0], buffer->start);
218 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
219         memcpy(ram_console_old_log + old_log_size - strbuf_len,
220                strbuf, strbuf_len);
221 #endif
222 }
223
224 static int __init ram_console_init(struct ram_console_buffer *buffer,
225                                    size_t buffer_size, char *old_buf)
226 {
227 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
228         int numerr;
229         uint8_t *par;
230 #endif
231         ram_console_buffer = buffer;
232         ram_console_buffer_size =
233                 buffer_size - sizeof(struct ram_console_buffer);
234
235         if (ram_console_buffer_size > buffer_size) {
236                 pr_err("ram_console: buffer %p, invalid size %zu, "
237                        "datasize %zu\n", buffer, buffer_size,
238                        ram_console_buffer_size);
239                 return 0;
240         }
241
242 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ERROR_CORRECTION
243         ram_console_buffer_size -= (DIV_ROUND_UP(ram_console_buffer_size,
244                                                 ECC_BLOCK_SIZE) + 1) * ECC_SIZE;
245
246         if (ram_console_buffer_size > buffer_size) {
247                 pr_err("ram_console: buffer %p, invalid size %zu, "
248                        "non-ecc datasize %zu\n",
249                        buffer, buffer_size, ram_console_buffer_size);
250                 return 0;
251         }
252
253         ram_console_par_buffer = buffer->data + ram_console_buffer_size;
254
255
256         /* first consecutive root is 0
257          * primitive element to generate roots = 1
258          */
259         ram_console_rs_decoder = init_rs(ECC_SYMSIZE, ECC_POLY, 0, 1, ECC_SIZE);
260         if (ram_console_rs_decoder == NULL) {
261                 printk(KERN_INFO "ram_console: init_rs failed\n");
262                 return 0;
263         }
264
265         ram_console_corrected_bytes = 0;
266         ram_console_bad_blocks = 0;
267
268         par = ram_console_par_buffer +
269               DIV_ROUND_UP(ram_console_buffer_size, ECC_BLOCK_SIZE) * ECC_SIZE;
270
271         numerr = ram_console_decode_rs8(buffer, sizeof(*buffer), par);
272         if (numerr > 0) {
273                 printk(KERN_INFO "ram_console: error in header, %d\n", numerr);
274                 ram_console_corrected_bytes += numerr;
275         } else if (numerr < 0) {
276                 printk(KERN_INFO
277                        "ram_console: uncorrectable error in header\n");
278                 ram_console_bad_blocks++;
279         }
280 #endif
281
282         if (buffer->sig == RAM_CONSOLE_SIG) {
283                 if (buffer->size > ram_console_buffer_size
284                     || buffer->start > buffer->size)
285                         printk(KERN_INFO "ram_console: found existing invalid "
286                                "buffer, size %d, start %d\n",
287                                buffer->size, buffer->start);
288                 else {
289                         printk(KERN_INFO "ram_console: found existing buffer, "
290                                "size %d, start %d\n",
291                                buffer->size, buffer->start);
292                         ram_console_save_old(buffer, old_buf);
293                 }
294         } else {
295                 printk(KERN_INFO "ram_console: no valid data in buffer "
296                        "(sig = 0x%08x)\n", buffer->sig);
297         }
298
299         buffer->sig = RAM_CONSOLE_SIG;
300         buffer->start = 0;
301         buffer->size = 0;
302
303         register_console(&ram_console);
304 #ifdef CONFIG_ANDROID_RAM_CONSOLE_ENABLE_VERBOSE
305         console_verbose();
306 #endif
307         return 0;
308 }
309
310 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
311 static int __init ram_console_early_init(void)
312 {
313         return ram_console_init((struct ram_console_buffer *)
314                 CONFIG_ANDROID_RAM_CONSOLE_EARLY_ADDR,
315                 CONFIG_ANDROID_RAM_CONSOLE_EARLY_SIZE,
316                 ram_console_old_log_init_buffer);
317 }
318 #else
319 static int ram_console_driver_probe(struct platform_device *pdev)
320 {
321         struct resource *res = pdev->resource;
322         size_t start;
323         size_t buffer_size;
324         void *buffer;
325
326         if (res == NULL || pdev->num_resources != 1 ||
327             !(res->flags & IORESOURCE_MEM)) {
328                 printk(KERN_ERR "ram_console: invalid resource, %p %d flags "
329                        "%lx\n", res, pdev->num_resources, res ? res->flags : 0);
330                 return -ENXIO;
331         }
332         buffer_size = res->end - res->start + 1;
333         start = res->start;
334         printk(KERN_INFO "ram_console: got buffer at %zx, size %zx\n",
335                start, buffer_size);
336         buffer = ioremap(res->start, buffer_size);
337         if (buffer == NULL) {
338                 printk(KERN_ERR "ram_console: failed to map memory\n");
339                 return -ENOMEM;
340         }
341
342         return ram_console_init(buffer, buffer_size, NULL/* allocate */);
343 }
344
345 static struct platform_driver ram_console_driver = {
346         .probe = ram_console_driver_probe,
347         .driver         = {
348                 .name   = "ram_console",
349         },
350 };
351
352 static int __init ram_console_module_init(void)
353 {
354         int err;
355         err = platform_driver_register(&ram_console_driver);
356         return err;
357 }
358 #endif
359
360 static ssize_t ram_console_read_old(struct file *file, char __user *buf,
361                                     size_t len, loff_t *offset)
362 {
363         loff_t pos = *offset;
364         ssize_t count;
365
366         if (pos >= ram_console_old_log_size)
367                 return 0;
368
369         count = min(len, (size_t)(ram_console_old_log_size - pos));
370         if (copy_to_user(buf, ram_console_old_log + pos, count))
371                 return -EFAULT;
372
373         *offset += count;
374         return count;
375 }
376
377 static const struct file_operations ram_console_file_ops = {
378         .owner = THIS_MODULE,
379         .read = ram_console_read_old,
380 };
381
382 static int __init ram_console_late_init(void)
383 {
384         struct proc_dir_entry *entry;
385
386         if (ram_console_old_log == NULL)
387                 return 0;
388 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
389         ram_console_old_log = kmalloc(ram_console_old_log_size, GFP_KERNEL);
390         if (ram_console_old_log == NULL) {
391                 printk(KERN_ERR
392                        "ram_console: failed to allocate buffer for old log\n");
393                 ram_console_old_log_size = 0;
394                 return 0;
395         }
396         memcpy(ram_console_old_log,
397                ram_console_old_log_init_buffer, ram_console_old_log_size);
398 #endif
399         entry = create_proc_entry("last_kmsg", S_IFREG | S_IRUGO, NULL);
400         if (!entry) {
401                 printk(KERN_ERR "ram_console: failed to create proc entry\n");
402                 kfree(ram_console_old_log);
403                 ram_console_old_log = NULL;
404                 return 0;
405         }
406
407         entry->proc_fops = &ram_console_file_ops;
408         entry->size = ram_console_old_log_size;
409         return 0;
410 }
411
412 #ifdef CONFIG_ANDROID_RAM_CONSOLE_EARLY_INIT
413 console_initcall(ram_console_early_init);
414 #else
415 postcore_initcall(ram_console_module_init);
416 #endif
417 late_initcall(ram_console_late_init);
418