ARM: rk: build resource.img with logo_kernel.bmp
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-rockchip / last_log.c
1 /*
2  *  arch/arm/mach-rockchip/last_log.c
3  *
4  *  Copyright (C) 2011-2014 ROCKCHIP, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #define pr_fmt(fmt) "last_log: " fmt
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/mm.h>
15 #include <linux/module.h>
16 #include <linux/proc_fs.h>
17 #include <linux/vmalloc.h>
18 #include <linux/rockchip/cpu.h>
19 #include <asm/uaccess.h>
20 #include <asm/io.h>
21
22 #define LOG_BUF_SHIFT   CONFIG_LOG_BUF_SHIFT
23 #define LOG_BUF_LEN     (1 << LOG_BUF_SHIFT)
24 #define LOG_BUF_PAGE_ORDER      (LOG_BUF_SHIFT - PAGE_SHIFT)
25 static char *last_log_buf;
26 static char *log_buf;
27 static size_t log_pos;
28 static char early_log_buf[8192];
29
30 char *rk_last_log_get(unsigned *size)
31 {
32         *size = LOG_BUF_LEN;
33         return last_log_buf;
34 }
35
36 static ssize_t last_log_read(struct file *file, char __user *buf,
37                                     size_t len, loff_t *offset)
38 {
39         loff_t pos = *offset;
40         ssize_t count;
41
42         if (pos >= LOG_BUF_LEN)
43                 return 0;
44
45         count = min(len, (size_t)(LOG_BUF_LEN - pos));
46         if (copy_to_user(buf, &last_log_buf[pos], count))
47                 return -EFAULT;
48
49         *offset += count;
50         return count;
51 }
52
53 static const struct file_operations last_log_fops = {
54         .owner = THIS_MODULE,
55         .read = last_log_read,
56 };
57
58 static void * __init last_log_vmap(phys_addr_t start, unsigned int page_count)
59 {
60         struct page *pages[page_count + 1];
61         unsigned int i;
62
63         for (i = 0; i < page_count; i++) {
64                 phys_addr_t addr = start + i * PAGE_SIZE;
65                 pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
66         }
67         pages[page_count] = pfn_to_page(start >> PAGE_SHIFT);
68         return vmap(pages, page_count + 1, VM_MAP, pgprot_noncached(PAGE_KERNEL));
69 }
70
71 static int __init rk_last_log_init(void)
72 {
73         size_t early_log_size;
74         char *buf;
75         struct proc_dir_entry *entry;
76
77         if (!cpu_is_rockchip())
78                 return 0;
79
80         buf = (char *)__get_free_pages(GFP_KERNEL, LOG_BUF_PAGE_ORDER);
81         if (!buf) {
82                 pr_err("failed to __get_free_pages(%d)\n", LOG_BUF_PAGE_ORDER);
83                 return 0;
84         }
85
86         log_buf = last_log_vmap(virt_to_phys(buf), 1 << LOG_BUF_PAGE_ORDER);
87         if (!log_buf) {
88                 pr_err("failed to map %d pages at 0x%08x\n", 1 << LOG_BUF_PAGE_ORDER, virt_to_phys(buf));
89                 return 0;
90         }
91
92         last_log_buf = (char *)vmalloc(LOG_BUF_LEN);
93         if (!last_log_buf) {
94                 pr_err("failed to vmalloc(%d)\n", LOG_BUF_LEN);
95                 return 0;
96         }
97
98         memcpy(last_log_buf, buf, LOG_BUF_LEN);
99         early_log_size = log_pos > sizeof(early_log_buf) ? sizeof(early_log_buf) : log_pos;
100         memcpy(log_buf, early_log_buf, early_log_size);
101         memset(log_buf + early_log_size, 0, LOG_BUF_LEN - early_log_size);
102
103         pr_info("0x%08x map to 0x%p and copy to 0x%p, size 0x%x early 0x%x (version 3.0)\n", virt_to_phys(buf), log_buf, last_log_buf, LOG_BUF_LEN, early_log_size);
104
105         entry = proc_create("last_kmsg", S_IRUSR, NULL, &last_log_fops);
106         if (!entry) {
107                 pr_err("failed to create proc entry\n");
108                 return 0;
109         }
110         proc_set_size(entry, LOG_BUF_LEN);
111
112         proc_symlink("last_log", NULL, "last_kmsg");
113
114         return 0;
115 }
116
117 early_initcall(rk_last_log_init);
118
119 void rk_last_log_text(char *text, size_t size)
120 {
121         char *buf = log_buf ? log_buf : early_log_buf;
122         size_t log_size = log_buf ? LOG_BUF_LEN : sizeof(early_log_buf);
123         size_t pos;
124
125         /* Check overflow */
126         pos = log_pos & (log_size - 1);
127         if (likely(size + pos <= log_size))
128                 memcpy(&buf[pos], text, size);
129         else {
130                 size_t first = log_size - pos;
131                 size_t second = size - first;
132                 memcpy(&buf[pos], text, first);
133                 memcpy(&buf[0], text + first, second);
134         }
135
136         log_pos += size;
137 }