blob: 453030f9c5bc2a68523cafc26c6d331d86855949 [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>
Kees Cook9ba80d92012-05-03 15:45:02 +10005 * Copyright (C) 2011 Kees Cook <keescook@chromium.org>
Marco Stornelli56d611a2010-05-26 14:43:54 -07006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 *
21 */
22
Marco Stornelli01692562011-07-26 16:08:57 -070023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Marco Stornelli56d611a2010-05-26 14:43:54 -070025#include <linux/kernel.h>
James Bottomley83c1b312011-07-29 17:11:32 +040026#include <linux/err.h>
Marco Stornelli56d611a2010-05-26 14:43:54 -070027#include <linux/module.h>
Kees Cook9ba80d92012-05-03 15:45:02 +100028#include <linux/pstore.h>
Marco Stornelli56d611a2010-05-26 14:43:54 -070029#include <linux/time.h>
30#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>
Anton Vorontsov1894a252012-05-16 05:43:08 -070034#include <linux/pstore_ram.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
Anton Vorontsov39eb7e972012-05-17 00:15:34 -070059static int ramoops_ecc;
60module_param_named(ecc, ramoops_ecc, int, 0600);
61MODULE_PARM_DESC(ramoops_ecc,
62 "set to 1 to enable ECC support");
63
Kees Cook9ba80d92012-05-03 15:45:02 +100064struct ramoops_context {
Anton Vorontsov896fc1f2012-05-17 00:15:18 -070065 struct persistent_ram_zone **przs;
Marco Stornelli56d611a2010-05-26 14:43:54 -070066 phys_addr_t phys_addr;
67 unsigned long size;
Kees Cook9ba80d92012-05-03 15:45:02 +100068 size_t record_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -070069 int dump_oops;
Anton Vorontsov39eb7e972012-05-17 00:15:34 -070070 bool ecc;
Kees Cook9ba80d92012-05-03 15:45:02 +100071 unsigned int count;
72 unsigned int max_count;
73 unsigned int read_count;
74 struct pstore_info pstore;
75};
Marco Stornelli56d611a2010-05-26 14:43:54 -070076
Marco Stornelli13aefd72011-07-26 16:08:57 -070077static struct platform_device *dummy;
78static struct ramoops_platform_data *dummy_data;
79
Kees Cook9ba80d92012-05-03 15:45:02 +100080static int ramoops_pstore_open(struct pstore_info *psi)
Marco Stornelli56d611a2010-05-26 14:43:54 -070081{
Kees Cook9ba80d92012-05-03 15:45:02 +100082 struct ramoops_context *cxt = psi->data;
Marco Stornelli56d611a2010-05-26 14:43:54 -070083
Kees Cook9ba80d92012-05-03 15:45:02 +100084 cxt->read_count = 0;
85 return 0;
86}
87
88static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
89 struct timespec *time,
90 char **buf,
91 struct pstore_info *psi)
92{
93 ssize_t size;
Kees Cook9ba80d92012-05-03 15:45:02 +100094 struct ramoops_context *cxt = psi->data;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -070095 struct persistent_ram_zone *prz;
Kees Cook9ba80d92012-05-03 15:45:02 +100096
97 if (cxt->read_count >= cxt->max_count)
98 return -EINVAL;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -070099
Kees Cook9ba80d92012-05-03 15:45:02 +1000100 *id = cxt->read_count++;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700101 prz = cxt->przs[*id];
102
Kees Cook9ba80d92012-05-03 15:45:02 +1000103 /* Only supports dmesg output so far. */
104 *type = PSTORE_TYPE_DMESG;
105 /* TODO(kees): Bogus time for the moment. */
106 time->tv_sec = 0;
107 time->tv_nsec = 0;
108
Anton Vorontsov201e4ac2012-05-26 06:07:49 -0700109 /* Update old/shadowed buffer. */
110 persistent_ram_save_old(prz);
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700111 size = persistent_ram_old_size(prz);
Kees Cook9ba80d92012-05-03 15:45:02 +1000112 *buf = kmalloc(size, GFP_KERNEL);
113 if (*buf == NULL)
114 return -ENOMEM;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700115 memcpy(*buf, persistent_ram_old(prz), size);
Kees Cook9ba80d92012-05-03 15:45:02 +1000116
117 return size;
118}
119
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700120static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)
121{
122 char *hdr;
123 struct timeval timestamp;
124 size_t len;
125
126 do_gettimeofday(&timestamp);
127 hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu\n",
128 (long)timestamp.tv_sec, (long)timestamp.tv_usec);
129 WARN_ON_ONCE(!hdr);
130 len = hdr ? strlen(hdr) : 0;
131 persistent_ram_write(prz, hdr, len);
132 kfree(hdr);
133
134 return len;
135}
136
Kees Cook9ba80d92012-05-03 15:45:02 +1000137static int ramoops_pstore_write(enum pstore_type_id type,
138 enum kmsg_dump_reason reason,
139 u64 *id,
140 unsigned int part,
141 size_t size, struct pstore_info *psi)
142{
Kees Cook9ba80d92012-05-03 15:45:02 +1000143 struct ramoops_context *cxt = psi->data;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700144 struct persistent_ram_zone *prz = cxt->przs[cxt->count];
145 size_t hlen;
Kees Cook9ba80d92012-05-03 15:45:02 +1000146
147 /* Currently ramoops is designed to only store dmesg dumps. */
148 if (type != PSTORE_TYPE_DMESG)
149 return -EINVAL;
150
151 /* Out of the various dmesg dump types, ramoops is currently designed
152 * to only store crash logs, rather than storing general kernel logs.
153 */
Seiji Aguchifc2d5572011-01-12 16:59:29 -0800154 if (reason != KMSG_DUMP_OOPS &&
WANG Conga3dd3322012-01-12 17:20:11 -0800155 reason != KMSG_DUMP_PANIC)
Kees Cook9ba80d92012-05-03 15:45:02 +1000156 return -EINVAL;
Seiji Aguchifc2d5572011-01-12 16:59:29 -0800157
Kees Cook9ba80d92012-05-03 15:45:02 +1000158 /* Skip Oopes when configured to do so. */
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700159 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
Kees Cook9ba80d92012-05-03 15:45:02 +1000160 return -EINVAL;
161
162 /* Explicitly only take the first part of any new crash.
163 * If our buffer is larger than kmsg_bytes, this can never happen,
164 * and if our buffer is smaller than kmsg_bytes, we don't want the
165 * report split across multiple records.
166 */
167 if (part != 1)
168 return -ENOSPC;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700169
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700170 hlen = ramoops_write_kmsg_hdr(prz);
171 if (size + hlen > prz->buffer_size)
172 size = prz->buffer_size - hlen;
173 persistent_ram_write(prz, cxt->pstore.buf, size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700174
175 cxt->count = (cxt->count + 1) % cxt->max_count;
Kees Cook9ba80d92012-05-03 15:45:02 +1000176
177 return 0;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700178}
179
Kees Cook9ba80d92012-05-03 15:45:02 +1000180static int ramoops_pstore_erase(enum pstore_type_id type, u64 id,
181 struct pstore_info *psi)
182{
Kees Cook9ba80d92012-05-03 15:45:02 +1000183 struct ramoops_context *cxt = psi->data;
184
185 if (id >= cxt->max_count)
186 return -EINVAL;
187
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700188 persistent_ram_free_old(cxt->przs[id]);
Anton Vorontsov93cce042012-05-26 06:07:52 -0700189 persistent_ram_zap(cxt->przs[id]);
Kees Cook9ba80d92012-05-03 15:45:02 +1000190
191 return 0;
192}
193
194static struct ramoops_context oops_cxt = {
195 .pstore = {
196 .owner = THIS_MODULE,
197 .name = "ramoops",
198 .open = ramoops_pstore_open,
199 .read = ramoops_pstore_read,
200 .write = ramoops_pstore_write,
201 .erase = ramoops_pstore_erase,
202 },
203};
204
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700205static int __init ramoops_probe(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700206{
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700207 struct device *dev = &pdev->dev;
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700208 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700209 struct ramoops_context *cxt = &oops_cxt;
210 int err = -EINVAL;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700211 int i;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700212
Kees Cook9ba80d92012-05-03 15:45:02 +1000213 /* Only a single ramoops area allowed at a time, so fail extra
214 * probes.
215 */
216 if (cxt->max_count)
217 goto fail_out;
218
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700219 if (!pdata->mem_size || !pdata->record_size) {
220 pr_err("The memory size and the record size must be "
221 "non-zero\n");
Kees Cook9ba80d92012-05-03 15:45:02 +1000222 goto fail_out;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700223 }
224
Marco Stornellifdb59502012-01-12 17:20:58 -0800225 pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
226 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700227
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700228 /* Check for the minimum memory size */
229 if (pdata->mem_size < MIN_MEM_SIZE &&
230 pdata->record_size < MIN_MEM_SIZE) {
Kees Cook9ba80d92012-05-03 15:45:02 +1000231 pr_err("memory size too small, minimum is %lu\n",
232 MIN_MEM_SIZE);
233 goto fail_out;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700234 }
235
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700236 if (pdata->mem_size < pdata->record_size) {
237 pr_err("The memory size must be larger than the "
238 "records size\n");
Kees Cook9ba80d92012-05-03 15:45:02 +1000239 goto fail_out;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700240 }
241
242 cxt->max_count = pdata->mem_size / pdata->record_size;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700243 cxt->count = 0;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700244 cxt->size = pdata->mem_size;
245 cxt->phys_addr = pdata->mem_address;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700246 cxt->record_size = pdata->record_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700247 cxt->dump_oops = pdata->dump_oops;
Anton Vorontsov39eb7e972012-05-17 00:15:34 -0700248 cxt->ecc = pdata->ecc;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700249
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700250 cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_count, GFP_KERNEL);
251 if (!cxt->przs) {
252 err = -ENOMEM;
253 dev_err(dev, "failed to initialize a prz array\n");
254 goto fail_out;
255 }
256
257 for (i = 0; i < cxt->max_count; i++) {
258 size_t sz = cxt->record_size;
259 phys_addr_t start = cxt->phys_addr + sz * i;
260
Anton Vorontsov39eb7e972012-05-17 00:15:34 -0700261 cxt->przs[i] = persistent_ram_new(start, sz, cxt->ecc);
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700262 if (IS_ERR(cxt->przs[i])) {
263 err = PTR_ERR(cxt->przs[i]);
264 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
265 sz, (unsigned long long)start, err);
266 goto fail_przs;
267 }
268 }
269
Kees Cook9ba80d92012-05-03 15:45:02 +1000270 cxt->pstore.data = cxt;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700271 cxt->pstore.bufsize = cxt->przs[0]->buffer_size;
Kees Cook9ba80d92012-05-03 15:45:02 +1000272 cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
273 spin_lock_init(&cxt->pstore.buf_lock);
274 if (!cxt->pstore.buf) {
275 pr_err("cannot allocate pstore buffer\n");
276 goto fail_clear;
277 }
278
Kees Cook9ba80d92012-05-03 15:45:02 +1000279 err = pstore_register(&cxt->pstore);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700280 if (err) {
Kees Cook9ba80d92012-05-03 15:45:02 +1000281 pr_err("registering with pstore failed\n");
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700282 goto fail_buf;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700283 }
284
Kees Cookc7552012012-01-12 17:20:59 -0800285 /*
286 * Update the module parameter variables as well so they are visible
287 * through /sys/module/ramoops/parameters/
288 */
289 mem_size = pdata->mem_size;
290 mem_address = pdata->mem_address;
291 record_size = pdata->record_size;
292 dump_oops = pdata->dump_oops;
293
Anton Vorontsov39eb7e972012-05-17 00:15:34 -0700294 pr_info("attached 0x%lx@0x%llx (%ux0x%zx), ecc: %s\n",
Randy Dunlapd109a672012-05-03 15:45:03 +1000295 cxt->size, (unsigned long long)cxt->phys_addr,
Anton Vorontsov39eb7e972012-05-17 00:15:34 -0700296 cxt->max_count, cxt->record_size,
297 ramoops_ecc ? "on" : "off");
Kees Cook9ba80d92012-05-03 15:45:02 +1000298
Marco Stornelli56d611a2010-05-26 14:43:54 -0700299 return 0;
300
Kees Cook9ba80d92012-05-03 15:45:02 +1000301fail_buf:
302 kfree(cxt->pstore.buf);
303fail_clear:
304 cxt->pstore.bufsize = 0;
305 cxt->max_count = 0;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700306fail_przs:
307 for (i = 0; cxt->przs[i]; i++)
308 persistent_ram_free(cxt->przs[i]);
309 kfree(cxt->przs);
Kees Cook9ba80d92012-05-03 15:45:02 +1000310fail_out:
Marco Stornelli56d611a2010-05-26 14:43:54 -0700311 return err;
312}
313
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700314static int __exit ramoops_remove(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700315{
Kees Cook9ba80d92012-05-03 15:45:02 +1000316#if 0
317 /* TODO(kees): We cannot unload ramoops since pstore doesn't support
318 * unregistering yet.
319 */
Marco Stornelli56d611a2010-05-26 14:43:54 -0700320 struct ramoops_context *cxt = &oops_cxt;
321
Marco Stornelli56d611a2010-05-26 14:43:54 -0700322 iounmap(cxt->virt_addr);
323 release_mem_region(cxt->phys_addr, cxt->size);
Kees Cook9ba80d92012-05-03 15:45:02 +1000324 cxt->max_count = 0;
325
326 /* TODO(kees): When pstore supports unregistering, call it here. */
327 kfree(cxt->pstore.buf);
328 cxt->pstore.bufsize = 0;
329
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700330 return 0;
Kees Cook9ba80d92012-05-03 15:45:02 +1000331#endif
332 return -EBUSY;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700333}
334
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700335static struct platform_driver ramoops_driver = {
336 .remove = __exit_p(ramoops_remove),
337 .driver = {
338 .name = "ramoops",
339 .owner = THIS_MODULE,
340 },
341};
342
343static int __init ramoops_init(void)
344{
Marco Stornelli13aefd72011-07-26 16:08:57 -0700345 int ret;
346 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
347 if (ret == -ENODEV) {
348 /*
349 * If we didn't find a platform device, we use module parameters
350 * building platform data on the fly.
351 */
Marco Stornelli01692562011-07-26 16:08:57 -0700352 pr_info("platform device not found, using module parameters\n");
Marco Stornelli13aefd72011-07-26 16:08:57 -0700353 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
354 GFP_KERNEL);
355 if (!dummy_data)
356 return -ENOMEM;
357 dummy_data->mem_size = mem_size;
358 dummy_data->mem_address = mem_address;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700359 dummy_data->record_size = record_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700360 dummy_data->dump_oops = dump_oops;
Anton Vorontsov39eb7e972012-05-17 00:15:34 -0700361 dummy_data->ecc = ramoops_ecc;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700362 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
363 NULL, 0, dummy_data,
364 sizeof(struct ramoops_platform_data));
365
366 if (IS_ERR(dummy))
367 ret = PTR_ERR(dummy);
368 else
369 ret = 0;
370 }
371
372 return ret;
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700373}
374
375static void __exit ramoops_exit(void)
376{
377 platform_driver_unregister(&ramoops_driver);
Marco Stornelli13aefd72011-07-26 16:08:57 -0700378 kfree(dummy_data);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700379}
Marco Stornelli56d611a2010-05-26 14:43:54 -0700380
381module_init(ramoops_init);
382module_exit(ramoops_exit);
383
384MODULE_LICENSE("GPL");
385MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
386MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");