blob: 2a5e45d2a9f839f216a6415dc888edc30cc55463 [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>
James Bottomley83c1b312011-07-29 17:11:32 +040025#include <linux/err.h>
Marco Stornelli56d611a2010-05-26 14:43:54 -070026#include <linux/module.h>
27#include <linux/kmsg_dump.h>
28#include <linux/time.h>
29#include <linux/io.h>
30#include <linux/ioport.h>
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070031#include <linux/platform_device.h>
Marco Stornelli13aefd72011-07-26 16:08:57 -070032#include <linux/slab.h>
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070033#include <linux/ramoops.h>
Marco Stornelli56d611a2010-05-26 14:43:54 -070034
35#define RAMOOPS_KERNMSG_HDR "===="
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070036#define MIN_MEM_SIZE 4096UL
Marco Stornelli56d611a2010-05-26 14:43:54 -070037
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070038static ulong record_size = MIN_MEM_SIZE;
39module_param(record_size, ulong, 0400);
40MODULE_PARM_DESC(record_size,
41 "size of each dump done on oops/panic");
Marco Stornelli56d611a2010-05-26 14:43:54 -070042
43static ulong mem_address;
44module_param(mem_address, ulong, 0400);
45MODULE_PARM_DESC(mem_address,
46 "start of reserved RAM used to store oops/panic logs");
47
48static ulong mem_size;
49module_param(mem_size, ulong, 0400);
50MODULE_PARM_DESC(mem_size,
51 "size of reserved RAM used to store oops/panic logs");
52
53static int dump_oops = 1;
54module_param(dump_oops, int, 0600);
55MODULE_PARM_DESC(dump_oops,
56 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
57
58static struct ramoops_context {
59 struct kmsg_dumper dump;
60 void *virt_addr;
61 phys_addr_t phys_addr;
62 unsigned long size;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070063 unsigned long record_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -070064 int dump_oops;
Marco Stornelli56d611a2010-05-26 14:43:54 -070065 int count;
66 int max_count;
67} oops_cxt;
68
Marco Stornelli13aefd72011-07-26 16:08:57 -070069static struct platform_device *dummy;
70static struct ramoops_platform_data *dummy_data;
71
Marco Stornelli56d611a2010-05-26 14:43:54 -070072static void ramoops_do_dump(struct kmsg_dumper *dumper,
73 enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
74 const char *s2, unsigned long l2)
75{
76 struct ramoops_context *cxt = container_of(dumper,
77 struct ramoops_context, dump);
78 unsigned long s1_start, s2_start;
79 unsigned long l1_cpy, l2_cpy;
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +020080 int res, hdr_size;
81 char *buf, *buf_orig;
Marco Stornelli56d611a2010-05-26 14:43:54 -070082 struct timeval timestamp;
83
Seiji Aguchifc2d5572011-01-12 16:59:29 -080084 if (reason != KMSG_DUMP_OOPS &&
WANG Conga3dd3322012-01-12 17:20:11 -080085 reason != KMSG_DUMP_PANIC)
Seiji Aguchifc2d5572011-01-12 16:59:29 -080086 return;
87
Marco Stornelli56d611a2010-05-26 14:43:54 -070088 /* Only dump oopses if dump_oops is set */
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -070089 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
Marco Stornelli56d611a2010-05-26 14:43:54 -070090 return;
91
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070092 buf = cxt->virt_addr + (cxt->count * cxt->record_size);
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +020093 buf_orig = buf;
94
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070095 memset(buf, '\0', cxt->record_size);
Marco Stornelli56d611a2010-05-26 14:43:54 -070096 res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
97 buf += res;
98 do_gettimeofday(&timestamp);
99 res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
100 buf += res;
101
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +0200102 hdr_size = buf - buf_orig;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700103 l2_cpy = min(l2, cxt->record_size - hdr_size);
104 l1_cpy = min(l1, cxt->record_size - hdr_size - l2_cpy);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700105
106 s2_start = l2 - l2_cpy;
107 s1_start = l1 - l1_cpy;
108
109 memcpy(buf, s1 + s1_start, l1_cpy);
110 memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
111
112 cxt->count = (cxt->count + 1) % cxt->max_count;
113}
114
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700115static int __init ramoops_probe(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700116{
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700117 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700118 struct ramoops_context *cxt = &oops_cxt;
119 int err = -EINVAL;
120
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700121 if (!pdata->mem_size || !pdata->record_size) {
122 pr_err("The memory size and the record size must be "
123 "non-zero\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700124 goto fail3;
125 }
126
Marco Stornellifdb59502012-01-12 17:20:58 -0800127 pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
128 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700129
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700130 /* Check for the minimum memory size */
131 if (pdata->mem_size < MIN_MEM_SIZE &&
132 pdata->record_size < MIN_MEM_SIZE) {
133 pr_err("memory size too small, minium is %lu\n", MIN_MEM_SIZE);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700134 goto fail3;
135 }
136
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700137 if (pdata->mem_size < pdata->record_size) {
138 pr_err("The memory size must be larger than the "
139 "records size\n");
140 goto fail3;
141 }
142
143 cxt->max_count = pdata->mem_size / pdata->record_size;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700144 cxt->count = 0;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700145 cxt->size = pdata->mem_size;
146 cxt->phys_addr = pdata->mem_address;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700147 cxt->record_size = pdata->record_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700148 cxt->dump_oops = pdata->dump_oops;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700149
150 if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
Marco Stornelli01692562011-07-26 16:08:57 -0700151 pr_err("request mem region failed\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700152 err = -EINVAL;
153 goto fail3;
154 }
155
156 cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
157 if (!cxt->virt_addr) {
Marco Stornelli01692562011-07-26 16:08:57 -0700158 pr_err("ioremap failed\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700159 goto fail2;
160 }
161
162 cxt->dump.dump = ramoops_do_dump;
163 err = kmsg_dump_register(&cxt->dump);
164 if (err) {
Marco Stornelli01692562011-07-26 16:08:57 -0700165 pr_err("registering kmsg dumper failed\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700166 goto fail1;
167 }
168
Kees Cookc7552012012-01-12 17:20:59 -0800169 /*
170 * Update the module parameter variables as well so they are visible
171 * through /sys/module/ramoops/parameters/
172 */
173 mem_size = pdata->mem_size;
174 mem_address = pdata->mem_address;
175 record_size = pdata->record_size;
176 dump_oops = pdata->dump_oops;
177
Marco Stornelli56d611a2010-05-26 14:43:54 -0700178 return 0;
179
180fail1:
181 iounmap(cxt->virt_addr);
182fail2:
183 release_mem_region(cxt->phys_addr, cxt->size);
184fail3:
185 return err;
186}
187
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700188static int __exit ramoops_remove(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700189{
190 struct ramoops_context *cxt = &oops_cxt;
191
192 if (kmsg_dump_unregister(&cxt->dump) < 0)
Marco Stornelli01692562011-07-26 16:08:57 -0700193 pr_warn("could not unregister kmsg_dumper\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700194
195 iounmap(cxt->virt_addr);
196 release_mem_region(cxt->phys_addr, cxt->size);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700197 return 0;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700198}
199
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700200static struct platform_driver ramoops_driver = {
201 .remove = __exit_p(ramoops_remove),
202 .driver = {
203 .name = "ramoops",
204 .owner = THIS_MODULE,
205 },
206};
207
208static int __init ramoops_init(void)
209{
Marco Stornelli13aefd72011-07-26 16:08:57 -0700210 int ret;
211 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
212 if (ret == -ENODEV) {
213 /*
214 * If we didn't find a platform device, we use module parameters
215 * building platform data on the fly.
216 */
Marco Stornelli01692562011-07-26 16:08:57 -0700217 pr_info("platform device not found, using module parameters\n");
Marco Stornelli13aefd72011-07-26 16:08:57 -0700218 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
219 GFP_KERNEL);
220 if (!dummy_data)
221 return -ENOMEM;
222 dummy_data->mem_size = mem_size;
223 dummy_data->mem_address = mem_address;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700224 dummy_data->record_size = record_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700225 dummy_data->dump_oops = dump_oops;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700226 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
227 NULL, 0, dummy_data,
228 sizeof(struct ramoops_platform_data));
229
230 if (IS_ERR(dummy))
231 ret = PTR_ERR(dummy);
232 else
233 ret = 0;
234 }
235
236 return ret;
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700237}
238
239static void __exit ramoops_exit(void)
240{
241 platform_driver_unregister(&ramoops_driver);
Marco Stornelli13aefd72011-07-26 16:08:57 -0700242 kfree(dummy_data);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700243}
Marco Stornelli56d611a2010-05-26 14:43:54 -0700244
245module_init(ramoops_init);
246module_exit(ramoops_exit);
247
248MODULE_LICENSE("GPL");
249MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
250MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");