blob: c9e1028766a4b6de62688cef5adc59928ef924aa [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
Marco Stornelli01692562011-07-26 16:08:57 -070022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Marco Stornelli56d611a2010-05-26 14:43:54 -070024#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/kmsg_dump.h>
27#include <linux/time.h>
28#include <linux/io.h>
29#include <linux/ioport.h>
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070030#include <linux/platform_device.h>
Marco Stornelli13aefd72011-07-26 16:08:57 -070031#include <linux/slab.h>
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070032#include <linux/ramoops.h>
Marco Stornelli56d611a2010-05-26 14:43:54 -070033
34#define RAMOOPS_KERNMSG_HDR "===="
Marco Stornelli56d611a2010-05-26 14:43:54 -070035
Andrew Morton264b7952011-01-12 17:01:11 -080036#define RECORD_SIZE 4096UL
Marco Stornelli56d611a2010-05-26 14:43:54 -070037
38static ulong mem_address;
39module_param(mem_address, ulong, 0400);
40MODULE_PARM_DESC(mem_address,
41 "start of reserved RAM used to store oops/panic logs");
42
43static ulong mem_size;
44module_param(mem_size, ulong, 0400);
45MODULE_PARM_DESC(mem_size,
46 "size of reserved RAM used to store oops/panic logs");
47
48static int dump_oops = 1;
49module_param(dump_oops, int, 0600);
50MODULE_PARM_DESC(dump_oops,
51 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
52
53static struct ramoops_context {
54 struct kmsg_dumper dump;
55 void *virt_addr;
56 phys_addr_t phys_addr;
57 unsigned long size;
58 int count;
59 int max_count;
60} oops_cxt;
61
Marco Stornelli13aefd72011-07-26 16:08:57 -070062static struct platform_device *dummy;
63static struct ramoops_platform_data *dummy_data;
64
Marco Stornelli56d611a2010-05-26 14:43:54 -070065static void ramoops_do_dump(struct kmsg_dumper *dumper,
66 enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
67 const char *s2, unsigned long l2)
68{
69 struct ramoops_context *cxt = container_of(dumper,
70 struct ramoops_context, dump);
71 unsigned long s1_start, s2_start;
72 unsigned long l1_cpy, l2_cpy;
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +020073 int res, hdr_size;
74 char *buf, *buf_orig;
Marco Stornelli56d611a2010-05-26 14:43:54 -070075 struct timeval timestamp;
76
Seiji Aguchifc2d5572011-01-12 16:59:29 -080077 if (reason != KMSG_DUMP_OOPS &&
78 reason != KMSG_DUMP_PANIC &&
79 reason != KMSG_DUMP_KEXEC)
80 return;
81
Marco Stornelli56d611a2010-05-26 14:43:54 -070082 /* Only dump oopses if dump_oops is set */
83 if (reason == KMSG_DUMP_OOPS && !dump_oops)
84 return;
85
Andrew Morton264b7952011-01-12 17:01:11 -080086 buf = cxt->virt_addr + (cxt->count * RECORD_SIZE);
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +020087 buf_orig = buf;
88
Marco Stornelli56d611a2010-05-26 14:43:54 -070089 memset(buf, '\0', RECORD_SIZE);
90 res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
91 buf += res;
92 do_gettimeofday(&timestamp);
93 res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
94 buf += res;
95
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +020096 hdr_size = buf - buf_orig;
Andrew Morton264b7952011-01-12 17:01:11 -080097 l2_cpy = min(l2, RECORD_SIZE - hdr_size);
98 l1_cpy = min(l1, RECORD_SIZE - hdr_size - l2_cpy);
Marco Stornelli56d611a2010-05-26 14:43:54 -070099
100 s2_start = l2 - l2_cpy;
101 s1_start = l1 - l1_cpy;
102
103 memcpy(buf, s1 + s1_start, l1_cpy);
104 memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
105
106 cxt->count = (cxt->count + 1) % cxt->max_count;
107}
108
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700109static int __init ramoops_probe(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700110{
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700111 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700112 struct ramoops_context *cxt = &oops_cxt;
113 int err = -EINVAL;
114
Marco Stornelli13aefd72011-07-26 16:08:57 -0700115 if (!pdata->mem_size) {
Marco Stornelli01692562011-07-26 16:08:57 -0700116 pr_err("invalid size specification\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700117 goto fail3;
118 }
119
Marco Stornelli13aefd72011-07-26 16:08:57 -0700120 rounddown_pow_of_two(pdata->mem_size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700121
Marco Stornelli13aefd72011-07-26 16:08:57 -0700122 if (pdata->mem_size < RECORD_SIZE) {
Marco Stornelli01692562011-07-26 16:08:57 -0700123 pr_err("size too small\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700124 goto fail3;
125 }
126
Marco Stornelli13aefd72011-07-26 16:08:57 -0700127 cxt->max_count = pdata->mem_size / RECORD_SIZE;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700128 cxt->count = 0;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700129 cxt->size = pdata->mem_size;
130 cxt->phys_addr = pdata->mem_address;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700131
132 if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
Marco Stornelli01692562011-07-26 16:08:57 -0700133 pr_err("request mem region failed\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700134 err = -EINVAL;
135 goto fail3;
136 }
137
138 cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
139 if (!cxt->virt_addr) {
Marco Stornelli01692562011-07-26 16:08:57 -0700140 pr_err("ioremap failed\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700141 goto fail2;
142 }
143
144 cxt->dump.dump = ramoops_do_dump;
145 err = kmsg_dump_register(&cxt->dump);
146 if (err) {
Marco Stornelli01692562011-07-26 16:08:57 -0700147 pr_err("registering kmsg dumper failed\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700148 goto fail1;
149 }
150
151 return 0;
152
153fail1:
154 iounmap(cxt->virt_addr);
155fail2:
156 release_mem_region(cxt->phys_addr, cxt->size);
157fail3:
158 return err;
159}
160
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700161static int __exit ramoops_remove(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700162{
163 struct ramoops_context *cxt = &oops_cxt;
164
165 if (kmsg_dump_unregister(&cxt->dump) < 0)
Marco Stornelli01692562011-07-26 16:08:57 -0700166 pr_warn("could not unregister kmsg_dumper\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700167
168 iounmap(cxt->virt_addr);
169 release_mem_region(cxt->phys_addr, cxt->size);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700170 return 0;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700171}
172
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700173static struct platform_driver ramoops_driver = {
174 .remove = __exit_p(ramoops_remove),
175 .driver = {
176 .name = "ramoops",
177 .owner = THIS_MODULE,
178 },
179};
180
181static int __init ramoops_init(void)
182{
Marco Stornelli13aefd72011-07-26 16:08:57 -0700183 int ret;
184 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
185 if (ret == -ENODEV) {
186 /*
187 * If we didn't find a platform device, we use module parameters
188 * building platform data on the fly.
189 */
Marco Stornelli01692562011-07-26 16:08:57 -0700190 pr_info("platform device not found, using module parameters\n");
Marco Stornelli13aefd72011-07-26 16:08:57 -0700191 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
192 GFP_KERNEL);
193 if (!dummy_data)
194 return -ENOMEM;
195 dummy_data->mem_size = mem_size;
196 dummy_data->mem_address = mem_address;
197 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
198 NULL, 0, dummy_data,
199 sizeof(struct ramoops_platform_data));
200
201 if (IS_ERR(dummy))
202 ret = PTR_ERR(dummy);
203 else
204 ret = 0;
205 }
206
207 return ret;
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700208}
209
210static void __exit ramoops_exit(void)
211{
212 platform_driver_unregister(&ramoops_driver);
Marco Stornelli13aefd72011-07-26 16:08:57 -0700213 kfree(dummy_data);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700214}
Marco Stornelli56d611a2010-05-26 14:43:54 -0700215
216module_init(ramoops_init);
217module_exit(ramoops_exit);
218
219MODULE_LICENSE("GPL");
220MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
221MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");