blob: 9123cce28c1e8d6f511738cda7435861f1261ef7 [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 Vorontsov896fc1f2012-05-17 00:15:18 -0700109 size = persistent_ram_old_size(prz);
Kees Cook9ba80d92012-05-03 15:45:02 +1000110 *buf = kmalloc(size, GFP_KERNEL);
111 if (*buf == NULL)
112 return -ENOMEM;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700113 memcpy(*buf, persistent_ram_old(prz), size);
Kees Cook9ba80d92012-05-03 15:45:02 +1000114
115 return size;
116}
117
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700118static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)
119{
120 char *hdr;
121 struct timeval timestamp;
122 size_t len;
123
124 do_gettimeofday(&timestamp);
125 hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu\n",
126 (long)timestamp.tv_sec, (long)timestamp.tv_usec);
127 WARN_ON_ONCE(!hdr);
128 len = hdr ? strlen(hdr) : 0;
129 persistent_ram_write(prz, hdr, len);
130 kfree(hdr);
131
132 return len;
133}
134
Kees Cook9ba80d92012-05-03 15:45:02 +1000135static int ramoops_pstore_write(enum pstore_type_id type,
136 enum kmsg_dump_reason reason,
137 u64 *id,
138 unsigned int part,
139 size_t size, struct pstore_info *psi)
140{
Kees Cook9ba80d92012-05-03 15:45:02 +1000141 struct ramoops_context *cxt = psi->data;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700142 struct persistent_ram_zone *prz = cxt->przs[cxt->count];
143 size_t hlen;
Kees Cook9ba80d92012-05-03 15:45:02 +1000144
145 /* Currently ramoops is designed to only store dmesg dumps. */
146 if (type != PSTORE_TYPE_DMESG)
147 return -EINVAL;
148
149 /* Out of the various dmesg dump types, ramoops is currently designed
150 * to only store crash logs, rather than storing general kernel logs.
151 */
Seiji Aguchifc2d5572011-01-12 16:59:29 -0800152 if (reason != KMSG_DUMP_OOPS &&
WANG Conga3dd3322012-01-12 17:20:11 -0800153 reason != KMSG_DUMP_PANIC)
Kees Cook9ba80d92012-05-03 15:45:02 +1000154 return -EINVAL;
Seiji Aguchifc2d5572011-01-12 16:59:29 -0800155
Kees Cook9ba80d92012-05-03 15:45:02 +1000156 /* Skip Oopes when configured to do so. */
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700157 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
Kees Cook9ba80d92012-05-03 15:45:02 +1000158 return -EINVAL;
159
160 /* Explicitly only take the first part of any new crash.
161 * If our buffer is larger than kmsg_bytes, this can never happen,
162 * and if our buffer is smaller than kmsg_bytes, we don't want the
163 * report split across multiple records.
164 */
165 if (part != 1)
166 return -ENOSPC;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700167
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700168 hlen = ramoops_write_kmsg_hdr(prz);
169 if (size + hlen > prz->buffer_size)
170 size = prz->buffer_size - hlen;
171 persistent_ram_write(prz, cxt->pstore.buf, size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700172
173 cxt->count = (cxt->count + 1) % cxt->max_count;
Kees Cook9ba80d92012-05-03 15:45:02 +1000174
175 return 0;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700176}
177
Kees Cook9ba80d92012-05-03 15:45:02 +1000178static int ramoops_pstore_erase(enum pstore_type_id type, u64 id,
179 struct pstore_info *psi)
180{
Kees Cook9ba80d92012-05-03 15:45:02 +1000181 struct ramoops_context *cxt = psi->data;
182
183 if (id >= cxt->max_count)
184 return -EINVAL;
185
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700186 persistent_ram_free_old(cxt->przs[id]);
Kees Cook9ba80d92012-05-03 15:45:02 +1000187
188 return 0;
189}
190
191static struct ramoops_context oops_cxt = {
192 .pstore = {
193 .owner = THIS_MODULE,
194 .name = "ramoops",
195 .open = ramoops_pstore_open,
196 .read = ramoops_pstore_read,
197 .write = ramoops_pstore_write,
198 .erase = ramoops_pstore_erase,
199 },
200};
201
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700202static int __init ramoops_probe(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700203{
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700204 struct device *dev = &pdev->dev;
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700205 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700206 struct ramoops_context *cxt = &oops_cxt;
207 int err = -EINVAL;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700208 int i;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700209
Kees Cook9ba80d92012-05-03 15:45:02 +1000210 /* Only a single ramoops area allowed at a time, so fail extra
211 * probes.
212 */
213 if (cxt->max_count)
214 goto fail_out;
215
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700216 if (!pdata->mem_size || !pdata->record_size) {
217 pr_err("The memory size and the record size must be "
218 "non-zero\n");
Kees Cook9ba80d92012-05-03 15:45:02 +1000219 goto fail_out;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700220 }
221
Marco Stornellifdb59502012-01-12 17:20:58 -0800222 pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
223 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700224
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700225 /* Check for the minimum memory size */
226 if (pdata->mem_size < MIN_MEM_SIZE &&
227 pdata->record_size < MIN_MEM_SIZE) {
Kees Cook9ba80d92012-05-03 15:45:02 +1000228 pr_err("memory size too small, minimum is %lu\n",
229 MIN_MEM_SIZE);
230 goto fail_out;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700231 }
232
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700233 if (pdata->mem_size < pdata->record_size) {
234 pr_err("The memory size must be larger than the "
235 "records size\n");
Kees Cook9ba80d92012-05-03 15:45:02 +1000236 goto fail_out;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700237 }
238
239 cxt->max_count = pdata->mem_size / pdata->record_size;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700240 cxt->count = 0;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700241 cxt->size = pdata->mem_size;
242 cxt->phys_addr = pdata->mem_address;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700243 cxt->record_size = pdata->record_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700244 cxt->dump_oops = pdata->dump_oops;
Anton Vorontsov39eb7e972012-05-17 00:15:34 -0700245 cxt->ecc = pdata->ecc;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700246
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700247 cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_count, GFP_KERNEL);
248 if (!cxt->przs) {
249 err = -ENOMEM;
250 dev_err(dev, "failed to initialize a prz array\n");
251 goto fail_out;
252 }
253
254 for (i = 0; i < cxt->max_count; i++) {
255 size_t sz = cxt->record_size;
256 phys_addr_t start = cxt->phys_addr + sz * i;
257
Anton Vorontsov39eb7e972012-05-17 00:15:34 -0700258 cxt->przs[i] = persistent_ram_new(start, sz, cxt->ecc);
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700259 if (IS_ERR(cxt->przs[i])) {
260 err = PTR_ERR(cxt->przs[i]);
261 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
262 sz, (unsigned long long)start, err);
263 goto fail_przs;
264 }
265 }
266
Kees Cook9ba80d92012-05-03 15:45:02 +1000267 cxt->pstore.data = cxt;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700268 cxt->pstore.bufsize = cxt->przs[0]->buffer_size;
Kees Cook9ba80d92012-05-03 15:45:02 +1000269 cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
270 spin_lock_init(&cxt->pstore.buf_lock);
271 if (!cxt->pstore.buf) {
272 pr_err("cannot allocate pstore buffer\n");
273 goto fail_clear;
274 }
275
Kees Cook9ba80d92012-05-03 15:45:02 +1000276 err = pstore_register(&cxt->pstore);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700277 if (err) {
Kees Cook9ba80d92012-05-03 15:45:02 +1000278 pr_err("registering with pstore failed\n");
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700279 goto fail_buf;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700280 }
281
Kees Cookc7552012012-01-12 17:20:59 -0800282 /*
283 * Update the module parameter variables as well so they are visible
284 * through /sys/module/ramoops/parameters/
285 */
286 mem_size = pdata->mem_size;
287 mem_address = pdata->mem_address;
288 record_size = pdata->record_size;
289 dump_oops = pdata->dump_oops;
290
Anton Vorontsov39eb7e972012-05-17 00:15:34 -0700291 pr_info("attached 0x%lx@0x%llx (%ux0x%zx), ecc: %s\n",
Randy Dunlapd109a672012-05-03 15:45:03 +1000292 cxt->size, (unsigned long long)cxt->phys_addr,
Anton Vorontsov39eb7e972012-05-17 00:15:34 -0700293 cxt->max_count, cxt->record_size,
294 ramoops_ecc ? "on" : "off");
Kees Cook9ba80d92012-05-03 15:45:02 +1000295
Marco Stornelli56d611a2010-05-26 14:43:54 -0700296 return 0;
297
Kees Cook9ba80d92012-05-03 15:45:02 +1000298fail_buf:
299 kfree(cxt->pstore.buf);
300fail_clear:
301 cxt->pstore.bufsize = 0;
302 cxt->max_count = 0;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700303fail_przs:
304 for (i = 0; cxt->przs[i]; i++)
305 persistent_ram_free(cxt->przs[i]);
306 kfree(cxt->przs);
Kees Cook9ba80d92012-05-03 15:45:02 +1000307fail_out:
Marco Stornelli56d611a2010-05-26 14:43:54 -0700308 return err;
309}
310
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700311static int __exit ramoops_remove(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700312{
Kees Cook9ba80d92012-05-03 15:45:02 +1000313#if 0
314 /* TODO(kees): We cannot unload ramoops since pstore doesn't support
315 * unregistering yet.
316 */
Marco Stornelli56d611a2010-05-26 14:43:54 -0700317 struct ramoops_context *cxt = &oops_cxt;
318
Marco Stornelli56d611a2010-05-26 14:43:54 -0700319 iounmap(cxt->virt_addr);
320 release_mem_region(cxt->phys_addr, cxt->size);
Kees Cook9ba80d92012-05-03 15:45:02 +1000321 cxt->max_count = 0;
322
323 /* TODO(kees): When pstore supports unregistering, call it here. */
324 kfree(cxt->pstore.buf);
325 cxt->pstore.bufsize = 0;
326
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700327 return 0;
Kees Cook9ba80d92012-05-03 15:45:02 +1000328#endif
329 return -EBUSY;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700330}
331
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700332static struct platform_driver ramoops_driver = {
333 .remove = __exit_p(ramoops_remove),
334 .driver = {
335 .name = "ramoops",
336 .owner = THIS_MODULE,
337 },
338};
339
340static int __init ramoops_init(void)
341{
Marco Stornelli13aefd72011-07-26 16:08:57 -0700342 int ret;
343 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
344 if (ret == -ENODEV) {
345 /*
346 * If we didn't find a platform device, we use module parameters
347 * building platform data on the fly.
348 */
Marco Stornelli01692562011-07-26 16:08:57 -0700349 pr_info("platform device not found, using module parameters\n");
Marco Stornelli13aefd72011-07-26 16:08:57 -0700350 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
351 GFP_KERNEL);
352 if (!dummy_data)
353 return -ENOMEM;
354 dummy_data->mem_size = mem_size;
355 dummy_data->mem_address = mem_address;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700356 dummy_data->record_size = record_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700357 dummy_data->dump_oops = dump_oops;
Anton Vorontsov39eb7e972012-05-17 00:15:34 -0700358 dummy_data->ecc = ramoops_ecc;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700359 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
360 NULL, 0, dummy_data,
361 sizeof(struct ramoops_platform_data));
362
363 if (IS_ERR(dummy))
364 ret = PTR_ERR(dummy);
365 else
366 ret = 0;
367 }
368
369 return ret;
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700370}
371
372static void __exit ramoops_exit(void)
373{
374 platform_driver_unregister(&ramoops_driver);
Marco Stornelli13aefd72011-07-26 16:08:57 -0700375 kfree(dummy_data);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700376}
Marco Stornelli56d611a2010-05-26 14:43:54 -0700377
378module_init(ramoops_init);
379module_exit(ramoops_exit);
380
381MODULE_LICENSE("GPL");
382MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
383MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");