upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / char / ramoops.c
1 /*
2  * RAM Oops/Panic logger
3  *
4  * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301 USA
19  *
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/kmsg_dump.h>
25 #include <linux/time.h>
26 #include <linux/io.h>
27 #include <linux/ioport.h>
28 #include <linux/platform_device.h>
29 #include <linux/ramoops.h>
30
31 #define RAMOOPS_KERNMSG_HDR "===="
32
33 #define RECORD_SIZE 4096
34
35 static ulong mem_address;
36 module_param(mem_address, ulong, 0400);
37 MODULE_PARM_DESC(mem_address,
38                 "start of reserved RAM used to store oops/panic logs");
39
40 static ulong mem_size;
41 module_param(mem_size, ulong, 0400);
42 MODULE_PARM_DESC(mem_size,
43                 "size of reserved RAM used to store oops/panic logs");
44
45 static int dump_oops = 1;
46 module_param(dump_oops, int, 0600);
47 MODULE_PARM_DESC(dump_oops,
48                 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
49
50 static struct ramoops_context {
51         struct kmsg_dumper dump;
52         void *virt_addr;
53         phys_addr_t phys_addr;
54         unsigned long size;
55         int count;
56         int max_count;
57         void (*enable)(int enable);
58 } oops_cxt;
59
60 static void ramoops_do_dump(struct kmsg_dumper *dumper,
61                 enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
62                 const char *s2, unsigned long l2)
63 {
64         struct ramoops_context *cxt = container_of(dumper,
65                         struct ramoops_context, dump);
66         unsigned long s1_start, s2_start;
67         unsigned long l1_cpy, l2_cpy;
68         int res, hdr_size;
69         char *buf, *buf_orig;
70         struct timeval timestamp;
71
72         /* Only dump oopses if dump_oops is set */
73         if (reason == KMSG_DUMP_OOPS && !dump_oops)
74                 return;
75
76         if (cxt->enable)
77                 cxt->enable(true);
78
79         buf = (char *)(cxt->virt_addr + (cxt->count * RECORD_SIZE));
80         buf_orig = buf;
81
82         memset(buf, '\0', RECORD_SIZE);
83         res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
84         buf += res;
85         do_gettimeofday(&timestamp);
86         res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
87         buf += res;
88
89         hdr_size = buf - buf_orig;
90         l2_cpy = min(l2, (unsigned long)(RECORD_SIZE - hdr_size));
91         l1_cpy = min(l1, (unsigned long)(RECORD_SIZE - hdr_size) - l2_cpy);
92
93         s2_start = l2 - l2_cpy;
94         s1_start = l1 - l1_cpy;
95
96         memcpy(buf, s1 + s1_start, l1_cpy);
97         memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
98
99         cxt->count = (cxt->count + 1) % cxt->max_count;
100
101         if (cxt->enable)
102                 cxt->enable(false);
103 }
104
105 static int __init ramoops_probe(struct platform_device *pdev)
106 {
107         struct ramoops_platform_data *pdata = pdev->dev.platform_data;
108         struct ramoops_context *cxt = &oops_cxt;
109         int err = -EINVAL;
110
111         if (pdata) {
112                 mem_size = pdata->mem_size;
113                 mem_address = pdata->mem_address;
114                 if (pdata->enable)
115                         cxt->enable = pdata->enable;
116         }
117
118         if (!mem_size) {
119                 printk(KERN_ERR "ramoops: invalid size specification");
120                 goto fail3;
121         }
122
123         rounddown_pow_of_two(mem_size);
124
125         if (mem_size < RECORD_SIZE) {
126                 printk(KERN_ERR "ramoops: size too small");
127                 goto fail3;
128         }
129
130         cxt->max_count = mem_size / RECORD_SIZE;
131         cxt->count = 0;
132         cxt->size = mem_size;
133         cxt->phys_addr = mem_address;
134
135         if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
136                 printk(KERN_ERR "ramoops: request mem region failed");
137                 err = -EINVAL;
138                 goto fail3;
139         }
140
141         cxt->virt_addr = ioremap(cxt->phys_addr,  cxt->size);
142         if (!cxt->virt_addr) {
143                 printk(KERN_ERR "ramoops: ioremap failed");
144                 goto fail2;
145         }
146
147         cxt->dump.dump = ramoops_do_dump;
148         err = kmsg_dump_register(&cxt->dump);
149         if (err) {
150                 printk(KERN_ERR "ramoops: registering kmsg dumper failed");
151                 goto fail1;
152         }
153
154         return 0;
155
156 fail1:
157         iounmap(cxt->virt_addr);
158 fail2:
159         release_mem_region(cxt->phys_addr, cxt->size);
160 fail3:
161         return err;
162 }
163
164 static int __exit ramoops_remove(struct platform_device *pdev)
165 {
166         struct ramoops_context *cxt = &oops_cxt;
167
168         if (kmsg_dump_unregister(&cxt->dump) < 0)
169                 printk(KERN_WARNING "ramoops: could not unregister kmsg_dumper");
170
171         iounmap(cxt->virt_addr);
172         release_mem_region(cxt->phys_addr, cxt->size);
173         return 0;
174 }
175
176 static struct platform_driver ramoops_driver = {
177         .remove                 = __exit_p(ramoops_remove),
178         .driver                 = {
179                 .name           = "ramoops",
180                 .owner          = THIS_MODULE,
181         },
182 };
183
184 static int __init ramoops_init(void)
185 {
186         return platform_driver_probe(&ramoops_driver, ramoops_probe);
187 }
188
189 static void __exit ramoops_exit(void)
190 {
191         platform_driver_unregister(&ramoops_driver);
192 }
193
194 module_init(ramoops_init);
195 module_exit(ramoops_exit);
196
197 MODULE_LICENSE("GPL");
198 MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
199 MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");