blob: 73dcb0ee41fdaebcbefee2b0ddf705bd5a26b1e9 [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
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/kmsg_dump.h>
25#include <linux/time.h>
26#include <linux/io.h>
27#include <linux/ioport.h>
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070028#include <linux/platform_device.h>
29#include <linux/ramoops.h>
Marco Stornelli56d611a2010-05-26 14:43:54 -070030
31#define RAMOOPS_KERNMSG_HDR "===="
32#define RAMOOPS_HEADER_SIZE (5 + sizeof(struct timeval))
33
34#define RECORD_SIZE 4096
35
36static ulong mem_address;
37module_param(mem_address, ulong, 0400);
38MODULE_PARM_DESC(mem_address,
39 "start of reserved RAM used to store oops/panic logs");
40
41static ulong mem_size;
42module_param(mem_size, ulong, 0400);
43MODULE_PARM_DESC(mem_size,
44 "size of reserved RAM used to store oops/panic logs");
45
46static int dump_oops = 1;
47module_param(dump_oops, int, 0600);
48MODULE_PARM_DESC(dump_oops,
49 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
50
51static struct ramoops_context {
52 struct kmsg_dumper dump;
53 void *virt_addr;
54 phys_addr_t phys_addr;
55 unsigned long size;
56 int count;
57 int max_count;
58} oops_cxt;
59
60static void ramoops_do_dump(struct kmsg_dumper *dumper,
61 enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
62 const char *s2, unsigned long l2)
63{
64 struct ramoops_context *cxt = container_of(dumper,
65 struct ramoops_context, dump);
66 unsigned long s1_start, s2_start;
67 unsigned long l1_cpy, l2_cpy;
68 int res;
69 char *buf;
70 struct timeval timestamp;
71
72 /* Only dump oopses if dump_oops is set */
73 if (reason == KMSG_DUMP_OOPS && !dump_oops)
74 return;
75
76 buf = (char *)(cxt->virt_addr + (cxt->count * RECORD_SIZE));
77 memset(buf, '\0', RECORD_SIZE);
78 res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
79 buf += res;
80 do_gettimeofday(&timestamp);
81 res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
82 buf += res;
83
84 l2_cpy = min(l2, (unsigned long)(RECORD_SIZE - RAMOOPS_HEADER_SIZE));
85 l1_cpy = min(l1, (unsigned long)(RECORD_SIZE - RAMOOPS_HEADER_SIZE) - l2_cpy);
86
87 s2_start = l2 - l2_cpy;
88 s1_start = l1 - l1_cpy;
89
90 memcpy(buf, s1 + s1_start, l1_cpy);
91 memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
92
93 cxt->count = (cxt->count + 1) % cxt->max_count;
94}
95
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070096static int __init ramoops_probe(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -070097{
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070098 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
Marco Stornelli56d611a2010-05-26 14:43:54 -070099 struct ramoops_context *cxt = &oops_cxt;
100 int err = -EINVAL;
101
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700102 if (pdata) {
103 mem_size = pdata->mem_size;
104 mem_address = pdata->mem_address;
105 }
106
Marco Stornelli56d611a2010-05-26 14:43:54 -0700107 if (!mem_size) {
108 printk(KERN_ERR "ramoops: invalid size specification");
109 goto fail3;
110 }
111
112 rounddown_pow_of_two(mem_size);
113
114 if (mem_size < RECORD_SIZE) {
115 printk(KERN_ERR "ramoops: size too small");
116 goto fail3;
117 }
118
119 cxt->max_count = mem_size / RECORD_SIZE;
120 cxt->count = 0;
121 cxt->size = mem_size;
122 cxt->phys_addr = mem_address;
123
124 if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
125 printk(KERN_ERR "ramoops: request mem region failed");
126 err = -EINVAL;
127 goto fail3;
128 }
129
130 cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
131 if (!cxt->virt_addr) {
132 printk(KERN_ERR "ramoops: ioremap failed");
133 goto fail2;
134 }
135
136 cxt->dump.dump = ramoops_do_dump;
137 err = kmsg_dump_register(&cxt->dump);
138 if (err) {
139 printk(KERN_ERR "ramoops: registering kmsg dumper failed");
140 goto fail1;
141 }
142
143 return 0;
144
145fail1:
146 iounmap(cxt->virt_addr);
147fail2:
148 release_mem_region(cxt->phys_addr, cxt->size);
149fail3:
150 return err;
151}
152
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700153static int __exit ramoops_remove(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700154{
155 struct ramoops_context *cxt = &oops_cxt;
156
157 if (kmsg_dump_unregister(&cxt->dump) < 0)
158 printk(KERN_WARNING "ramoops: could not unregister kmsg_dumper");
159
160 iounmap(cxt->virt_addr);
161 release_mem_region(cxt->phys_addr, cxt->size);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700162 return 0;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700163}
164
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700165static struct platform_driver ramoops_driver = {
166 .remove = __exit_p(ramoops_remove),
167 .driver = {
168 .name = "ramoops",
169 .owner = THIS_MODULE,
170 },
171};
172
173static int __init ramoops_init(void)
174{
175 return platform_driver_probe(&ramoops_driver, ramoops_probe);
176}
177
178static void __exit ramoops_exit(void)
179{
180 platform_driver_unregister(&ramoops_driver);
181}
Marco Stornelli56d611a2010-05-26 14:43:54 -0700182
183module_init(ramoops_init);
184module_exit(ramoops_exit);
185
186MODULE_LICENSE("GPL");
187MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
188MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");