blob: 9fec3232b7361b102ff5e05e55f2444eeab6ffb8 [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>
Paul Gortmakerc05aa8f2011-07-27 21:31:17 -040029#include <linux/err.h>
Marco Stornelli56d611a2010-05-26 14:43:54 -070030#include <linux/io.h>
31#include <linux/ioport.h>
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070032#include <linux/platform_device.h>
Marco Stornelli13aefd72011-07-26 16:08:57 -070033#include <linux/slab.h>
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070034#include <linux/ramoops.h>
Marco Stornelli56d611a2010-05-26 14:43:54 -070035
36#define RAMOOPS_KERNMSG_HDR "===="
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070037#define MIN_MEM_SIZE 4096UL
Marco Stornelli56d611a2010-05-26 14:43:54 -070038
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070039static ulong record_size = MIN_MEM_SIZE;
40module_param(record_size, ulong, 0400);
41MODULE_PARM_DESC(record_size,
42 "size of each dump done on oops/panic");
Marco Stornelli56d611a2010-05-26 14:43:54 -070043
44static ulong mem_address;
45module_param(mem_address, ulong, 0400);
46MODULE_PARM_DESC(mem_address,
47 "start of reserved RAM used to store oops/panic logs");
48
49static ulong mem_size;
50module_param(mem_size, ulong, 0400);
51MODULE_PARM_DESC(mem_size,
52 "size of reserved RAM used to store oops/panic logs");
53
54static int dump_oops = 1;
55module_param(dump_oops, int, 0600);
56MODULE_PARM_DESC(dump_oops,
57 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
58
59static struct ramoops_context {
60 struct kmsg_dumper dump;
61 void *virt_addr;
62 phys_addr_t phys_addr;
63 unsigned long size;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070064 unsigned long record_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -070065 int dump_oops;
Marco Stornelli56d611a2010-05-26 14:43:54 -070066 int count;
67 int max_count;
68} oops_cxt;
69
Marco Stornelli13aefd72011-07-26 16:08:57 -070070static struct platform_device *dummy;
71static struct ramoops_platform_data *dummy_data;
72
Marco Stornelli56d611a2010-05-26 14:43:54 -070073static void ramoops_do_dump(struct kmsg_dumper *dumper,
74 enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
75 const char *s2, unsigned long l2)
76{
77 struct ramoops_context *cxt = container_of(dumper,
78 struct ramoops_context, dump);
79 unsigned long s1_start, s2_start;
80 unsigned long l1_cpy, l2_cpy;
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +020081 int res, hdr_size;
82 char *buf, *buf_orig;
Marco Stornelli56d611a2010-05-26 14:43:54 -070083 struct timeval timestamp;
84
Seiji Aguchifc2d5572011-01-12 16:59:29 -080085 if (reason != KMSG_DUMP_OOPS &&
WANG Conga3dd3322012-01-12 17:20:11 -080086 reason != KMSG_DUMP_PANIC)
Seiji Aguchifc2d5572011-01-12 16:59:29 -080087 return;
88
Marco Stornelli56d611a2010-05-26 14:43:54 -070089 /* Only dump oopses if dump_oops is set */
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -070090 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
Marco Stornelli56d611a2010-05-26 14:43:54 -070091 return;
92
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070093 buf = cxt->virt_addr + (cxt->count * cxt->record_size);
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +020094 buf_orig = buf;
95
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070096 memset(buf, '\0', cxt->record_size);
Marco Stornelli56d611a2010-05-26 14:43:54 -070097 res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
98 buf += res;
99 do_gettimeofday(&timestamp);
100 res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
101 buf += res;
102
Ahmed S. Darwish1873bb82010-12-25 11:57:09 +0200103 hdr_size = buf - buf_orig;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700104 l2_cpy = min(l2, cxt->record_size - hdr_size);
105 l1_cpy = min(l1, cxt->record_size - hdr_size - l2_cpy);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700106
107 s2_start = l2 - l2_cpy;
108 s1_start = l1 - l1_cpy;
109
110 memcpy(buf, s1 + s1_start, l1_cpy);
111 memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
112
113 cxt->count = (cxt->count + 1) % cxt->max_count;
114}
115
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700116static int __init ramoops_probe(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700117{
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700118 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700119 struct ramoops_context *cxt = &oops_cxt;
120 int err = -EINVAL;
121
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700122 if (!pdata->mem_size || !pdata->record_size) {
123 pr_err("The memory size and the record size must be "
124 "non-zero\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700125 goto fail3;
126 }
127
Marco Stornellifdb59502012-01-12 17:20:58 -0800128 pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
129 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700130
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700131 /* Check for the minimum memory size */
132 if (pdata->mem_size < MIN_MEM_SIZE &&
133 pdata->record_size < MIN_MEM_SIZE) {
134 pr_err("memory size too small, minium is %lu\n", MIN_MEM_SIZE);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700135 goto fail3;
136 }
137
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700138 if (pdata->mem_size < pdata->record_size) {
139 pr_err("The memory size must be larger than the "
140 "records size\n");
141 goto fail3;
142 }
143
144 cxt->max_count = pdata->mem_size / pdata->record_size;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700145 cxt->count = 0;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700146 cxt->size = pdata->mem_size;
147 cxt->phys_addr = pdata->mem_address;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700148 cxt->record_size = pdata->record_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700149 cxt->dump_oops = pdata->dump_oops;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700150
151 if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
Marco Stornelli01692562011-07-26 16:08:57 -0700152 pr_err("request mem region failed\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700153 err = -EINVAL;
154 goto fail3;
155 }
156
157 cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
158 if (!cxt->virt_addr) {
Marco Stornelli01692562011-07-26 16:08:57 -0700159 pr_err("ioremap failed\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700160 goto fail2;
161 }
162
163 cxt->dump.dump = ramoops_do_dump;
164 err = kmsg_dump_register(&cxt->dump);
165 if (err) {
Marco Stornelli01692562011-07-26 16:08:57 -0700166 pr_err("registering kmsg dumper failed\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700167 goto fail1;
168 }
169
Kees Cookc7552012012-01-12 17:20:59 -0800170 /*
171 * Update the module parameter variables as well so they are visible
172 * through /sys/module/ramoops/parameters/
173 */
174 mem_size = pdata->mem_size;
175 mem_address = pdata->mem_address;
176 record_size = pdata->record_size;
177 dump_oops = pdata->dump_oops;
178
Marco Stornelli56d611a2010-05-26 14:43:54 -0700179 return 0;
180
181fail1:
182 iounmap(cxt->virt_addr);
183fail2:
184 release_mem_region(cxt->phys_addr, cxt->size);
185fail3:
186 return err;
187}
188
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700189static int __exit ramoops_remove(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700190{
191 struct ramoops_context *cxt = &oops_cxt;
192
193 if (kmsg_dump_unregister(&cxt->dump) < 0)
Marco Stornelli01692562011-07-26 16:08:57 -0700194 pr_warn("could not unregister kmsg_dumper\n");
Marco Stornelli56d611a2010-05-26 14:43:54 -0700195
196 iounmap(cxt->virt_addr);
197 release_mem_region(cxt->phys_addr, cxt->size);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700198 return 0;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700199}
200
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700201static struct platform_driver ramoops_driver = {
202 .remove = __exit_p(ramoops_remove),
203 .driver = {
204 .name = "ramoops",
205 .owner = THIS_MODULE,
206 },
207};
208
209static int __init ramoops_init(void)
210{
Marco Stornelli13aefd72011-07-26 16:08:57 -0700211 int ret;
212 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
213 if (ret == -ENODEV) {
214 /*
215 * If we didn't find a platform device, we use module parameters
216 * building platform data on the fly.
217 */
Marco Stornelli01692562011-07-26 16:08:57 -0700218 pr_info("platform device not found, using module parameters\n");
Marco Stornelli13aefd72011-07-26 16:08:57 -0700219 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
220 GFP_KERNEL);
221 if (!dummy_data)
222 return -ENOMEM;
223 dummy_data->mem_size = mem_size;
224 dummy_data->mem_address = mem_address;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700225 dummy_data->record_size = record_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700226 dummy_data->dump_oops = dump_oops;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700227 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
228 NULL, 0, dummy_data,
229 sizeof(struct ramoops_platform_data));
230
231 if (IS_ERR(dummy))
232 ret = PTR_ERR(dummy);
233 else
234 ret = 0;
235 }
236
237 return ret;
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700238}
239
240static void __exit ramoops_exit(void)
241{
242 platform_driver_unregister(&ramoops_driver);
Marco Stornelli13aefd72011-07-26 16:08:57 -0700243 kfree(dummy_data);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700244}
Marco Stornelli56d611a2010-05-26 14:43:54 -0700245
246module_init(ramoops_init);
247module_exit(ramoops_exit);
248
249MODULE_LICENSE("GPL");
250MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
251MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");