blob: 32aa233df30058af99228a5709e3feec046dd5e9 [file] [log] [blame]
Marco Stornelli56d611a2010-05-26 14:43:54 -07001/*
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>
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070028#include <linux/platform_device.h>
Marco Stornelli13aefd72011-07-26 16:08:57 -070029#include <linux/slab.h>
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070030#include <linux/ramoops.h>
Marco Stornelli56d611a2010-05-26 14:43:54 -070031
32#define RAMOOPS_KERNMSG_HDR "===="
Marco Stornelli56d611a2010-05-26 14:43:54 -070033
Andrew Morton264b7952011-01-12 17:01:11 -080034#define RECORD_SIZE 4096UL
Marco Stornelli56d611a2010-05-26 14:43:54 -070035
36static ulong mem_address;
37module_param(mem_address, ulong, 0400);
38MODULE_PARM_DESC(mem_address,
39 "start of reserved RAM used to store oops/panic logs");
40
41static ulong mem_size;
42module_param(mem_size, ulong, 0400);
43MODULE_PARM_DESC(mem_size,
44 "size of reserved RAM used to store oops/panic logs");
45
46static int dump_oops = 1;
47module_param(dump_oops, int, 0600);
48MODULE_PARM_DESC(dump_oops,
49 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
50
51static struct ramoops_context {
52 struct kmsg_dumper dump;
53 void *virt_addr;
54 phys_addr_t phys_addr;
55 unsigned long size;
56 int count;
57 int max_count;
58} oops_cxt;
59
Marco Stornelli13aefd72011-07-26 16:08:57 -070060static struct platform_device *dummy;
61static struct ramoops_platform_data *dummy_data;
62
Marco Stornelli56d611a2010-05-26 14:43:54 -070063static void ramoops_do_dump(struct kmsg_dumper *dumper,
64 enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
65 const char *s2, unsigned long l2)
66{
67 struct ramoops_context *cxt = container_of(dumper,
68 struct ramoops_context, dump);
69 unsigned long s1_start, s2_start;
70 unsigned long l1_cpy, l2_cpy;
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +020071 int res, hdr_size;
72 char *buf, *buf_orig;
Marco Stornelli56d611a2010-05-26 14:43:54 -070073 struct timeval timestamp;
74
Seiji Aguchifc2d5572011-01-12 16:59:29 -080075 if (reason != KMSG_DUMP_OOPS &&
76 reason != KMSG_DUMP_PANIC &&
77 reason != KMSG_DUMP_KEXEC)
78 return;
79
Marco Stornelli56d611a2010-05-26 14:43:54 -070080 /* Only dump oopses if dump_oops is set */
81 if (reason == KMSG_DUMP_OOPS && !dump_oops)
82 return;
83
Andrew Morton264b7952011-01-12 17:01:11 -080084 buf = cxt->virt_addr + (cxt->count * RECORD_SIZE);
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +020085 buf_orig = buf;
86
Marco Stornelli56d611a2010-05-26 14:43:54 -070087 memset(buf, '\0', RECORD_SIZE);
88 res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
89 buf += res;
90 do_gettimeofday(&timestamp);
91 res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
92 buf += res;
93
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +020094 hdr_size = buf - buf_orig;
Andrew Morton264b7952011-01-12 17:01:11 -080095 l2_cpy = min(l2, RECORD_SIZE - hdr_size);
96 l1_cpy = min(l1, RECORD_SIZE - hdr_size - l2_cpy);
Marco Stornelli56d611a2010-05-26 14:43:54 -070097
98 s2_start = l2 - l2_cpy;
99 s1_start = l1 - l1_cpy;
100
101 memcpy(buf, s1 + s1_start, l1_cpy);
102 memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
103
104 cxt->count = (cxt->count + 1) % cxt->max_count;
105}
106
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700107static int __init ramoops_probe(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700108{
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700109 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700110 struct ramoops_context *cxt = &oops_cxt;
111 int err = -EINVAL;
112
Marco Stornelli13aefd72011-07-26 16:08:57 -0700113 if (!pdata->mem_size) {
Marco Stornelli56d611a2010-05-26 14:43:54 -0700114 printk(KERN_ERR "ramoops: invalid size specification");
115 goto fail3;
116 }
117
Marco Stornelli13aefd72011-07-26 16:08:57 -0700118 rounddown_pow_of_two(pdata->mem_size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700119
Marco Stornelli13aefd72011-07-26 16:08:57 -0700120 if (pdata->mem_size < RECORD_SIZE) {
Marco Stornelli56d611a2010-05-26 14:43:54 -0700121 printk(KERN_ERR "ramoops: size too small");
122 goto fail3;
123 }
124
Marco Stornelli13aefd72011-07-26 16:08:57 -0700125 cxt->max_count = pdata->mem_size / RECORD_SIZE;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700126 cxt->count = 0;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700127 cxt->size = pdata->mem_size;
128 cxt->phys_addr = pdata->mem_address;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700129
130 if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
131 printk(KERN_ERR "ramoops: request mem region failed");
132 err = -EINVAL;
133 goto fail3;
134 }
135
136 cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
137 if (!cxt->virt_addr) {
138 printk(KERN_ERR "ramoops: ioremap failed");
139 goto fail2;
140 }
141
142 cxt->dump.dump = ramoops_do_dump;
143 err = kmsg_dump_register(&cxt->dump);
144 if (err) {
145 printk(KERN_ERR "ramoops: registering kmsg dumper failed");
146 goto fail1;
147 }
148
149 return 0;
150
151fail1:
152 iounmap(cxt->virt_addr);
153fail2:
154 release_mem_region(cxt->phys_addr, cxt->size);
155fail3:
156 return err;
157}
158
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700159static int __exit ramoops_remove(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700160{
161 struct ramoops_context *cxt = &oops_cxt;
162
163 if (kmsg_dump_unregister(&cxt->dump) < 0)
164 printk(KERN_WARNING "ramoops: could not unregister kmsg_dumper");
165
166 iounmap(cxt->virt_addr);
167 release_mem_region(cxt->phys_addr, cxt->size);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700168 return 0;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700169}
170
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700171static struct platform_driver ramoops_driver = {
172 .remove = __exit_p(ramoops_remove),
173 .driver = {
174 .name = "ramoops",
175 .owner = THIS_MODULE,
176 },
177};
178
179static int __init ramoops_init(void)
180{
Marco Stornelli13aefd72011-07-26 16:08:57 -0700181 int ret;
182 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
183 if (ret == -ENODEV) {
184 /*
185 * If we didn't find a platform device, we use module parameters
186 * building platform data on the fly.
187 */
188 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
189 GFP_KERNEL);
190 if (!dummy_data)
191 return -ENOMEM;
192 dummy_data->mem_size = mem_size;
193 dummy_data->mem_address = mem_address;
194 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
195 NULL, 0, dummy_data,
196 sizeof(struct ramoops_platform_data));
197
198 if (IS_ERR(dummy))
199 ret = PTR_ERR(dummy);
200 else
201 ret = 0;
202 }
203
204 return ret;
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700205}
206
207static void __exit ramoops_exit(void)
208{
209 platform_driver_unregister(&ramoops_driver);
Marco Stornelli13aefd72011-07-26 16:08:57 -0700210 kfree(dummy_data);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700211}
Marco Stornelli56d611a2010-05-26 14:43:54 -0700212
213module_init(ramoops_init);
214module_exit(ramoops_exit);
215
216MODULE_LICENSE("GPL");
217MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
218MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");