Merge branch 'linux-next' of git://git.kernel.org/pub/scm/linux/kernel/git/jbarnes...
[deliverable/linux.git] / drivers / char / ramoops.c
CommitLineData
56d611a0
MS
1/*
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
0169256e
MS
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
56d611a0
MS
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/kmsg_dump.h>
27#include <linux/time.h>
28#include <linux/io.h>
29#include <linux/ioport.h>
c3b92ce9 30#include <linux/platform_device.h>
13aefd72 31#include <linux/slab.h>
c3b92ce9 32#include <linux/ramoops.h>
56d611a0
MS
33
34#define RAMOOPS_KERNMSG_HDR "===="
3e5c4fad 35#define MIN_MEM_SIZE 4096UL
56d611a0 36
3e5c4fad
SI
37static ulong record_size = MIN_MEM_SIZE;
38module_param(record_size, ulong, 0400);
39MODULE_PARM_DESC(record_size,
40 "size of each dump done on oops/panic");
56d611a0
MS
41
42static ulong mem_address;
43module_param(mem_address, ulong, 0400);
44MODULE_PARM_DESC(mem_address,
45 "start of reserved RAM used to store oops/panic logs");
46
47static ulong mem_size;
48module_param(mem_size, ulong, 0400);
49MODULE_PARM_DESC(mem_size,
50 "size of reserved RAM used to store oops/panic logs");
51
52static int dump_oops = 1;
53module_param(dump_oops, int, 0600);
54MODULE_PARM_DESC(dump_oops,
55 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
56
57static struct ramoops_context {
58 struct kmsg_dumper dump;
59 void *virt_addr;
60 phys_addr_t phys_addr;
61 unsigned long size;
3e5c4fad 62 unsigned long record_size;
6b4d2a27 63 int dump_oops;
56d611a0
MS
64 int count;
65 int max_count;
66} oops_cxt;
67
13aefd72
MS
68static struct platform_device *dummy;
69static struct ramoops_platform_data *dummy_data;
70
56d611a0
MS
71static void ramoops_do_dump(struct kmsg_dumper *dumper,
72 enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
73 const char *s2, unsigned long l2)
74{
75 struct ramoops_context *cxt = container_of(dumper,
76 struct ramoops_context, dump);
77 unsigned long s1_start, s2_start;
78 unsigned long l1_cpy, l2_cpy;
1873bb81
AD
79 int res, hdr_size;
80 char *buf, *buf_orig;
56d611a0
MS
81 struct timeval timestamp;
82
fc2d557c
SA
83 if (reason != KMSG_DUMP_OOPS &&
84 reason != KMSG_DUMP_PANIC &&
85 reason != KMSG_DUMP_KEXEC)
86 return;
87
56d611a0 88 /* Only dump oopses if dump_oops is set */
6b4d2a27 89 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
56d611a0
MS
90 return;
91
3e5c4fad 92 buf = cxt->virt_addr + (cxt->count * cxt->record_size);
1873bb81
AD
93 buf_orig = buf;
94
3e5c4fad 95 memset(buf, '\0', cxt->record_size);
56d611a0
MS
96 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
1873bb81 102 hdr_size = buf - buf_orig;
3e5c4fad
SI
103 l2_cpy = min(l2, cxt->record_size - hdr_size);
104 l1_cpy = min(l1, cxt->record_size - hdr_size - l2_cpy);
56d611a0
MS
105
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
c3b92ce9 115static int __init ramoops_probe(struct platform_device *pdev)
56d611a0 116{
c3b92ce9 117 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
56d611a0
MS
118 struct ramoops_context *cxt = &oops_cxt;
119 int err = -EINVAL;
120
3e5c4fad
SI
121 if (!pdata->mem_size || !pdata->record_size) {
122 pr_err("The memory size and the record size must be "
123 "non-zero\n");
56d611a0
MS
124 goto fail3;
125 }
126
13aefd72 127 rounddown_pow_of_two(pdata->mem_size);
3e5c4fad 128 rounddown_pow_of_two(pdata->record_size);
56d611a0 129
3e5c4fad
SI
130 /* 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);
56d611a0
MS
134 goto fail3;
135 }
136
3e5c4fad
SI
137 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;
56d611a0 144 cxt->count = 0;
13aefd72
MS
145 cxt->size = pdata->mem_size;
146 cxt->phys_addr = pdata->mem_address;
3e5c4fad 147 cxt->record_size = pdata->record_size;
6b4d2a27 148 cxt->dump_oops = pdata->dump_oops;
56d611a0
MS
149
150 if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
0169256e 151 pr_err("request mem region failed\n");
56d611a0
MS
152 err = -EINVAL;
153 goto fail3;
154 }
155
156 cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
157 if (!cxt->virt_addr) {
0169256e 158 pr_err("ioremap failed\n");
56d611a0
MS
159 goto fail2;
160 }
161
162 cxt->dump.dump = ramoops_do_dump;
163 err = kmsg_dump_register(&cxt->dump);
164 if (err) {
0169256e 165 pr_err("registering kmsg dumper failed\n");
56d611a0
MS
166 goto fail1;
167 }
168
169 return 0;
170
171fail1:
172 iounmap(cxt->virt_addr);
173fail2:
174 release_mem_region(cxt->phys_addr, cxt->size);
175fail3:
176 return err;
177}
178
c3b92ce9 179static int __exit ramoops_remove(struct platform_device *pdev)
56d611a0
MS
180{
181 struct ramoops_context *cxt = &oops_cxt;
182
183 if (kmsg_dump_unregister(&cxt->dump) < 0)
0169256e 184 pr_warn("could not unregister kmsg_dumper\n");
56d611a0
MS
185
186 iounmap(cxt->virt_addr);
187 release_mem_region(cxt->phys_addr, cxt->size);
c3b92ce9 188 return 0;
56d611a0
MS
189}
190
c3b92ce9
KP
191static struct platform_driver ramoops_driver = {
192 .remove = __exit_p(ramoops_remove),
193 .driver = {
194 .name = "ramoops",
195 .owner = THIS_MODULE,
196 },
197};
198
199static int __init ramoops_init(void)
200{
13aefd72
MS
201 int ret;
202 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
203 if (ret == -ENODEV) {
204 /*
205 * If we didn't find a platform device, we use module parameters
206 * building platform data on the fly.
207 */
0169256e 208 pr_info("platform device not found, using module parameters\n");
13aefd72
MS
209 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
210 GFP_KERNEL);
211 if (!dummy_data)
212 return -ENOMEM;
213 dummy_data->mem_size = mem_size;
214 dummy_data->mem_address = mem_address;
3e5c4fad 215 dummy_data->record_size = record_size;
6b4d2a27 216 dummy_data->dump_oops = dump_oops;
13aefd72
MS
217 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
218 NULL, 0, dummy_data,
219 sizeof(struct ramoops_platform_data));
220
221 if (IS_ERR(dummy))
222 ret = PTR_ERR(dummy);
223 else
224 ret = 0;
225 }
226
227 return ret;
c3b92ce9
KP
228}
229
230static void __exit ramoops_exit(void)
231{
232 platform_driver_unregister(&ramoops_driver);
13aefd72 233 kfree(dummy_data);
c3b92ce9 234}
56d611a0
MS
235
236module_init(ramoops_init);
237module_exit(ramoops_exit);
238
239MODULE_LICENSE("GPL");
240MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
241MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");
This page took 0.118038 seconds and 5 git commands to generate.