]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/char/ramoops.c
ramoops: move dump_oops into platform data
[mirror_ubuntu-zesty-kernel.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 "===="
56d611a0 35
264b795f 36#define RECORD_SIZE 4096UL
56d611a0
MS
37
38static ulong mem_address;
39module_param(mem_address, ulong, 0400);
40MODULE_PARM_DESC(mem_address,
41 "start of reserved RAM used to store oops/panic logs");
42
43static ulong mem_size;
44module_param(mem_size, ulong, 0400);
45MODULE_PARM_DESC(mem_size,
46 "size of reserved RAM used to store oops/panic logs");
47
48static int dump_oops = 1;
49module_param(dump_oops, int, 0600);
50MODULE_PARM_DESC(dump_oops,
51 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
52
53static struct ramoops_context {
54 struct kmsg_dumper dump;
55 void *virt_addr;
56 phys_addr_t phys_addr;
57 unsigned long size;
6b4d2a27 58 int dump_oops;
56d611a0
MS
59 int count;
60 int max_count;
61} oops_cxt;
62
13aefd72
MS
63static struct platform_device *dummy;
64static struct ramoops_platform_data *dummy_data;
65
56d611a0
MS
66static void ramoops_do_dump(struct kmsg_dumper *dumper,
67 enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
68 const char *s2, unsigned long l2)
69{
70 struct ramoops_context *cxt = container_of(dumper,
71 struct ramoops_context, dump);
72 unsigned long s1_start, s2_start;
73 unsigned long l1_cpy, l2_cpy;
1873bb81
AD
74 int res, hdr_size;
75 char *buf, *buf_orig;
56d611a0
MS
76 struct timeval timestamp;
77
fc2d557c
SA
78 if (reason != KMSG_DUMP_OOPS &&
79 reason != KMSG_DUMP_PANIC &&
80 reason != KMSG_DUMP_KEXEC)
81 return;
82
56d611a0 83 /* Only dump oopses if dump_oops is set */
6b4d2a27 84 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
56d611a0
MS
85 return;
86
264b795f 87 buf = cxt->virt_addr + (cxt->count * RECORD_SIZE);
1873bb81
AD
88 buf_orig = buf;
89
56d611a0
MS
90 memset(buf, '\0', RECORD_SIZE);
91 res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
92 buf += res;
93 do_gettimeofday(&timestamp);
94 res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
95 buf += res;
96
1873bb81 97 hdr_size = buf - buf_orig;
264b795f
AM
98 l2_cpy = min(l2, RECORD_SIZE - hdr_size);
99 l1_cpy = min(l1, RECORD_SIZE - hdr_size - l2_cpy);
56d611a0
MS
100
101 s2_start = l2 - l2_cpy;
102 s1_start = l1 - l1_cpy;
103
104 memcpy(buf, s1 + s1_start, l1_cpy);
105 memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
106
107 cxt->count = (cxt->count + 1) % cxt->max_count;
108}
109
c3b92ce9 110static int __init ramoops_probe(struct platform_device *pdev)
56d611a0 111{
c3b92ce9 112 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
56d611a0
MS
113 struct ramoops_context *cxt = &oops_cxt;
114 int err = -EINVAL;
115
13aefd72 116 if (!pdata->mem_size) {
0169256e 117 pr_err("invalid size specification\n");
56d611a0
MS
118 goto fail3;
119 }
120
13aefd72 121 rounddown_pow_of_two(pdata->mem_size);
56d611a0 122
13aefd72 123 if (pdata->mem_size < RECORD_SIZE) {
0169256e 124 pr_err("size too small\n");
56d611a0
MS
125 goto fail3;
126 }
127
13aefd72 128 cxt->max_count = pdata->mem_size / RECORD_SIZE;
56d611a0 129 cxt->count = 0;
13aefd72
MS
130 cxt->size = pdata->mem_size;
131 cxt->phys_addr = pdata->mem_address;
6b4d2a27 132 cxt->dump_oops = pdata->dump_oops;
56d611a0
MS
133
134 if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
0169256e 135 pr_err("request mem region failed\n");
56d611a0
MS
136 err = -EINVAL;
137 goto fail3;
138 }
139
140 cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
141 if (!cxt->virt_addr) {
0169256e 142 pr_err("ioremap failed\n");
56d611a0
MS
143 goto fail2;
144 }
145
146 cxt->dump.dump = ramoops_do_dump;
147 err = kmsg_dump_register(&cxt->dump);
148 if (err) {
0169256e 149 pr_err("registering kmsg dumper failed\n");
56d611a0
MS
150 goto fail1;
151 }
152
153 return 0;
154
155fail1:
156 iounmap(cxt->virt_addr);
157fail2:
158 release_mem_region(cxt->phys_addr, cxt->size);
159fail3:
160 return err;
161}
162
c3b92ce9 163static int __exit ramoops_remove(struct platform_device *pdev)
56d611a0
MS
164{
165 struct ramoops_context *cxt = &oops_cxt;
166
167 if (kmsg_dump_unregister(&cxt->dump) < 0)
0169256e 168 pr_warn("could not unregister kmsg_dumper\n");
56d611a0
MS
169
170 iounmap(cxt->virt_addr);
171 release_mem_region(cxt->phys_addr, cxt->size);
c3b92ce9 172 return 0;
56d611a0
MS
173}
174
c3b92ce9
KP
175static struct platform_driver ramoops_driver = {
176 .remove = __exit_p(ramoops_remove),
177 .driver = {
178 .name = "ramoops",
179 .owner = THIS_MODULE,
180 },
181};
182
183static int __init ramoops_init(void)
184{
13aefd72
MS
185 int ret;
186 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
187 if (ret == -ENODEV) {
188 /*
189 * If we didn't find a platform device, we use module parameters
190 * building platform data on the fly.
191 */
0169256e 192 pr_info("platform device not found, using module parameters\n");
13aefd72
MS
193 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
194 GFP_KERNEL);
195 if (!dummy_data)
196 return -ENOMEM;
197 dummy_data->mem_size = mem_size;
198 dummy_data->mem_address = mem_address;
6b4d2a27 199 dummy_data->dump_oops = dump_oops;
13aefd72
MS
200 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
201 NULL, 0, dummy_data,
202 sizeof(struct ramoops_platform_data));
203
204 if (IS_ERR(dummy))
205 ret = PTR_ERR(dummy);
206 else
207 ret = 0;
208 }
209
210 return ret;
c3b92ce9
KP
211}
212
213static void __exit ramoops_exit(void)
214{
215 platform_driver_unregister(&ramoops_driver);
13aefd72 216 kfree(dummy_data);
c3b92ce9 217}
56d611a0
MS
218
219module_init(ramoops_init);
220module_exit(ramoops_exit);
221
222MODULE_LICENSE("GPL");
223MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
224MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");