blob: 8613e5b35c22423df808172fa408985191ccc1a2 [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>
Anton Vorontsovcbe7cbf2012-07-17 12:11:12 -070028#include <linux/version.h>
Kees Cook9ba80d92012-05-03 15:45:02 +100029#include <linux/pstore.h>
Marco Stornelli56d611a2010-05-26 14:43:54 -070030#include <linux/time.h>
31#include <linux/io.h>
32#include <linux/ioport.h>
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -070033#include <linux/platform_device.h>
Marco Stornelli13aefd72011-07-26 16:08:57 -070034#include <linux/slab.h>
Anton Vorontsov24203032012-07-17 19:49:37 -070035#include <linux/compiler.h>
Anton Vorontsov1894a252012-05-16 05:43:08 -070036#include <linux/pstore_ram.h>
Marco Stornelli56d611a2010-05-26 14:43:54 -070037
38#define RAMOOPS_KERNMSG_HDR "===="
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070039#define MIN_MEM_SIZE 4096UL
Marco Stornelli56d611a2010-05-26 14:43:54 -070040
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -070041static ulong record_size = MIN_MEM_SIZE;
42module_param(record_size, ulong, 0400);
43MODULE_PARM_DESC(record_size,
44 "size of each dump done on oops/panic");
Marco Stornelli56d611a2010-05-26 14:43:54 -070045
Anton Vorontsovb5d38e92012-05-26 06:20:23 -070046static ulong ramoops_console_size = MIN_MEM_SIZE;
47module_param_named(console_size, ramoops_console_size, ulong, 0400);
48MODULE_PARM_DESC(console_size, "size of kernel console log");
49
Anton Vorontsova694d1b2012-07-09 17:10:44 -070050static ulong ramoops_ftrace_size = MIN_MEM_SIZE;
51module_param_named(ftrace_size, ramoops_ftrace_size, ulong, 0400);
52MODULE_PARM_DESC(ftrace_size, "size of ftrace log");
53
Marco Stornelli56d611a2010-05-26 14:43:54 -070054static ulong mem_address;
55module_param(mem_address, ulong, 0400);
56MODULE_PARM_DESC(mem_address,
57 "start of reserved RAM used to store oops/panic logs");
58
59static ulong mem_size;
60module_param(mem_size, ulong, 0400);
61MODULE_PARM_DESC(mem_size,
62 "size of reserved RAM used to store oops/panic logs");
63
Tony Lindgren027bc8b2014-09-16 13:50:01 -070064static unsigned int mem_type;
65module_param(mem_type, uint, 0600);
66MODULE_PARM_DESC(mem_type,
67 "set to 1 to try to use unbuffered memory (default 0)");
68
Marco Stornelli56d611a2010-05-26 14:43:54 -070069static int dump_oops = 1;
70module_param(dump_oops, int, 0600);
71MODULE_PARM_DESC(dump_oops,
72 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
73
Anton Vorontsov39eb7e972012-05-17 00:15:34 -070074static int ramoops_ecc;
75module_param_named(ecc, ramoops_ecc, int, 0600);
76MODULE_PARM_DESC(ramoops_ecc,
Anton Vorontsov5ca5d4e2012-07-09 17:03:19 -070077 "if non-zero, the option enables ECC support and specifies "
78 "ECC buffer size in bytes (1 is a special value, means 16 "
79 "bytes ECC)");
Anton Vorontsov39eb7e972012-05-17 00:15:34 -070080
Kees Cook9ba80d92012-05-03 15:45:02 +100081struct ramoops_context {
Anton Vorontsov896fc1f2012-05-17 00:15:18 -070082 struct persistent_ram_zone **przs;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -070083 struct persistent_ram_zone *cprz;
Anton Vorontsova694d1b2012-07-09 17:10:44 -070084 struct persistent_ram_zone *fprz;
Marco Stornelli56d611a2010-05-26 14:43:54 -070085 phys_addr_t phys_addr;
86 unsigned long size;
Tony Lindgren027bc8b2014-09-16 13:50:01 -070087 unsigned int memtype;
Kees Cook9ba80d92012-05-03 15:45:02 +100088 size_t record_size;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -070089 size_t console_size;
Anton Vorontsova694d1b2012-07-09 17:10:44 -070090 size_t ftrace_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -070091 int dump_oops;
Arve Hjønnevågc31ad082012-05-22 16:33:23 -070092 struct persistent_ram_ecc_info ecc_info;
Anton Vorontsovcac2eb72012-05-26 06:20:20 -070093 unsigned int max_dump_cnt;
94 unsigned int dump_write_cnt;
Liu ShuoX57fd8352014-03-17 11:24:49 +110095 /* _read_cnt need clear on ramoops_pstore_open */
Anton Vorontsovcac2eb72012-05-26 06:20:20 -070096 unsigned int dump_read_cnt;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -070097 unsigned int console_read_cnt;
Anton Vorontsova694d1b2012-07-09 17:10:44 -070098 unsigned int ftrace_read_cnt;
Kees Cook9ba80d92012-05-03 15:45:02 +100099 struct pstore_info pstore;
100};
Marco Stornelli56d611a2010-05-26 14:43:54 -0700101
Marco Stornelli13aefd72011-07-26 16:08:57 -0700102static struct platform_device *dummy;
103static struct ramoops_platform_data *dummy_data;
104
Kees Cook9ba80d92012-05-03 15:45:02 +1000105static int ramoops_pstore_open(struct pstore_info *psi)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700106{
Kees Cook9ba80d92012-05-03 15:45:02 +1000107 struct ramoops_context *cxt = psi->data;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700108
Anton Vorontsovcac2eb72012-05-26 06:20:20 -0700109 cxt->dump_read_cnt = 0;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700110 cxt->console_read_cnt = 0;
Liu ShuoX57fd8352014-03-17 11:24:49 +1100111 cxt->ftrace_read_cnt = 0;
Kees Cook9ba80d92012-05-03 15:45:02 +1000112 return 0;
113}
114
Anton Vorontsov755d66b2012-05-26 06:20:22 -0700115static struct persistent_ram_zone *
116ramoops_get_next_prz(struct persistent_ram_zone *przs[], uint *c, uint max,
117 u64 *id,
118 enum pstore_type_id *typep, enum pstore_type_id type,
119 bool update)
120{
121 struct persistent_ram_zone *prz;
122 int i = (*c)++;
123
124 if (i >= max)
125 return NULL;
126
127 prz = przs[i];
Liu ShuoXb0aa9312014-03-17 13:57:49 -0700128 if (!prz)
129 return NULL;
Anton Vorontsov755d66b2012-05-26 06:20:22 -0700130
Liu ShuoXaa9a4a12014-03-17 11:24:49 +1100131 /* Update old/shadowed buffer. */
132 if (update)
Anton Vorontsov755d66b2012-05-26 06:20:22 -0700133 persistent_ram_save_old(prz);
Liu ShuoXaa9a4a12014-03-17 11:24:49 +1100134
135 if (!persistent_ram_old_size(prz))
136 return NULL;
Anton Vorontsov755d66b2012-05-26 06:20:22 -0700137
138 *typep = type;
139 *id = i;
140
141 return prz;
142}
143
Ben Zhanga28726b2014-10-30 17:14:21 -0700144static int ramoops_read_kmsg_hdr(char *buffer, struct timespec *time,
Aruna Balakrishnaiah3f8f80f2013-08-16 13:58:00 -0700145 bool *compressed)
146{
147 char data_type;
Ben Zhanga28726b2014-10-30 17:14:21 -0700148 int header_length = 0;
Aruna Balakrishnaiah3f8f80f2013-08-16 13:58:00 -0700149
Ben Zhanga28726b2014-10-30 17:14:21 -0700150 if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n%n", &time->tv_sec,
151 &time->tv_nsec, &data_type, &header_length) == 3) {
Aruna Balakrishnaiah3f8f80f2013-08-16 13:58:00 -0700152 if (data_type == 'C')
153 *compressed = true;
154 else
155 *compressed = false;
Ben Zhanga28726b2014-10-30 17:14:21 -0700156 } else if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lu.%lu\n%n",
157 &time->tv_sec, &time->tv_nsec, &header_length) == 2) {
Aruna Balakrishnaiah3f8f80f2013-08-16 13:58:00 -0700158 *compressed = false;
159 } else {
160 time->tv_sec = 0;
161 time->tv_nsec = 0;
162 *compressed = false;
163 }
Ben Zhanga28726b2014-10-30 17:14:21 -0700164 return header_length;
Aruna Balakrishnaiah3f8f80f2013-08-16 13:58:00 -0700165}
166
Kees Cook9ba80d92012-05-03 15:45:02 +1000167static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -0800168 int *count, struct timespec *time,
Aruna Balakrishnaiah9a4e1392013-08-16 13:53:19 -0700169 char **buf, bool *compressed,
170 struct pstore_info *psi)
Kees Cook9ba80d92012-05-03 15:45:02 +1000171{
172 ssize_t size;
Arve Hjønnevågbd08ec32012-12-05 21:19:51 -0800173 ssize_t ecc_notice_size;
Kees Cook9ba80d92012-05-03 15:45:02 +1000174 struct ramoops_context *cxt = psi->data;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700175 struct persistent_ram_zone *prz;
Ben Zhanga28726b2014-10-30 17:14:21 -0700176 int header_length;
Kees Cook9ba80d92012-05-03 15:45:02 +1000177
Anton Vorontsov755d66b2012-05-26 06:20:22 -0700178 prz = ramoops_get_next_prz(cxt->przs, &cxt->dump_read_cnt,
179 cxt->max_dump_cnt, id, type,
180 PSTORE_TYPE_DMESG, 1);
181 if (!prz)
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700182 prz = ramoops_get_next_prz(&cxt->cprz, &cxt->console_read_cnt,
183 1, id, type, PSTORE_TYPE_CONSOLE, 0);
184 if (!prz)
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700185 prz = ramoops_get_next_prz(&cxt->fprz, &cxt->ftrace_read_cnt,
186 1, id, type, PSTORE_TYPE_FTRACE, 0);
187 if (!prz)
Anton Vorontsov755d66b2012-05-26 06:20:22 -0700188 return 0;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700189
Ben Zhanga28726b2014-10-30 17:14:21 -0700190 if (!persistent_ram_old(prz))
191 return 0;
192
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700193 size = persistent_ram_old_size(prz);
Ben Zhanga28726b2014-10-30 17:14:21 -0700194 header_length = ramoops_read_kmsg_hdr(persistent_ram_old(prz), time,
195 compressed);
196 size -= header_length;
Arve Hjønnevågbd08ec32012-12-05 21:19:51 -0800197
198 /* ECC correction notice */
199 ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0);
200
201 *buf = kmalloc(size + ecc_notice_size + 1, GFP_KERNEL);
Kees Cook9ba80d92012-05-03 15:45:02 +1000202 if (*buf == NULL)
203 return -ENOMEM;
Kees Cook9ba80d92012-05-03 15:45:02 +1000204
Ben Zhanga28726b2014-10-30 17:14:21 -0700205 memcpy(*buf, (char *)persistent_ram_old(prz) + header_length, size);
Arve Hjønnevågbd08ec32012-12-05 21:19:51 -0800206 persistent_ram_ecc_string(prz, *buf + size, ecc_notice_size + 1);
207
208 return size + ecc_notice_size;
Kees Cook9ba80d92012-05-03 15:45:02 +1000209}
210
Aruna Balakrishnaiah3f8f80f2013-08-16 13:58:00 -0700211static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz,
212 bool compressed)
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700213{
214 char *hdr;
Kees Cook1e817fb2012-11-19 10:26:16 -0800215 struct timespec timestamp;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700216 size_t len;
217
Kees Cook1e817fb2012-11-19 10:26:16 -0800218 /* Report zeroed timestamp if called before timekeeping has resumed. */
219 if (__getnstimeofday(&timestamp)) {
220 timestamp.tv_sec = 0;
221 timestamp.tv_nsec = 0;
222 }
Aruna Balakrishnaiah3f8f80f2013-08-16 13:58:00 -0700223 hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n",
224 (long)timestamp.tv_sec, (long)(timestamp.tv_nsec / 1000),
225 compressed ? 'C' : 'D');
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700226 WARN_ON_ONCE(!hdr);
227 len = hdr ? strlen(hdr) : 0;
228 persistent_ram_write(prz, hdr, len);
229 kfree(hdr);
230
231 return len;
232}
233
Anton Vorontsov24203032012-07-17 19:49:37 -0700234static int notrace ramoops_pstore_write_buf(enum pstore_type_id type,
235 enum kmsg_dump_reason reason,
236 u64 *id, unsigned int part,
Aruna Balakrishnaiah6bbbca72013-06-27 14:02:56 +0530237 const char *buf,
Aruna Balakrishnaiahb3b515b2013-08-16 13:52:47 -0700238 bool compressed, size_t size,
Anton Vorontsov24203032012-07-17 19:49:37 -0700239 struct pstore_info *psi)
Kees Cook9ba80d92012-05-03 15:45:02 +1000240{
Kees Cook9ba80d92012-05-03 15:45:02 +1000241 struct ramoops_context *cxt = psi->data;
Arve Hjønnevågc6289372012-12-11 17:49:24 -0800242 struct persistent_ram_zone *prz;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700243 size_t hlen;
Kees Cook9ba80d92012-05-03 15:45:02 +1000244
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700245 if (type == PSTORE_TYPE_CONSOLE) {
246 if (!cxt->cprz)
247 return -ENOMEM;
Anton Vorontsovc2b71132012-07-09 17:10:43 -0700248 persistent_ram_write(cxt->cprz, buf, size);
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700249 return 0;
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700250 } else if (type == PSTORE_TYPE_FTRACE) {
251 if (!cxt->fprz)
252 return -ENOMEM;
253 persistent_ram_write(cxt->fprz, buf, size);
254 return 0;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700255 }
256
Kees Cook9ba80d92012-05-03 15:45:02 +1000257 if (type != PSTORE_TYPE_DMESG)
258 return -EINVAL;
259
260 /* Out of the various dmesg dump types, ramoops is currently designed
261 * to only store crash logs, rather than storing general kernel logs.
262 */
Seiji Aguchifc2d5572011-01-12 16:59:29 -0800263 if (reason != KMSG_DUMP_OOPS &&
WANG Conga3dd3322012-01-12 17:20:11 -0800264 reason != KMSG_DUMP_PANIC)
Kees Cook9ba80d92012-05-03 15:45:02 +1000265 return -EINVAL;
Seiji Aguchifc2d5572011-01-12 16:59:29 -0800266
Kees Cook9ba80d92012-05-03 15:45:02 +1000267 /* Skip Oopes when configured to do so. */
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700268 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
Kees Cook9ba80d92012-05-03 15:45:02 +1000269 return -EINVAL;
270
271 /* Explicitly only take the first part of any new crash.
272 * If our buffer is larger than kmsg_bytes, this can never happen,
273 * and if our buffer is smaller than kmsg_bytes, we don't want the
274 * report split across multiple records.
275 */
276 if (part != 1)
277 return -ENOSPC;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700278
Arve Hjønnevågc6289372012-12-11 17:49:24 -0800279 if (!cxt->przs)
280 return -ENOSPC;
281
282 prz = cxt->przs[cxt->dump_write_cnt];
283
Aruna Balakrishnaiah3f8f80f2013-08-16 13:58:00 -0700284 hlen = ramoops_write_kmsg_hdr(prz, compressed);
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700285 if (size + hlen > prz->buffer_size)
286 size = prz->buffer_size - hlen;
Anton Vorontsovc2b71132012-07-09 17:10:43 -0700287 persistent_ram_write(prz, buf, size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700288
Anton Vorontsovcac2eb72012-05-26 06:20:20 -0700289 cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
Kees Cook9ba80d92012-05-03 15:45:02 +1000290
291 return 0;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700292}
293
Seiji Aguchi755d4fe2012-11-26 16:07:44 -0800294static int ramoops_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +0000295 struct timespec time, struct pstore_info *psi)
Kees Cook9ba80d92012-05-03 15:45:02 +1000296{
Kees Cook9ba80d92012-05-03 15:45:02 +1000297 struct ramoops_context *cxt = psi->data;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700298 struct persistent_ram_zone *prz;
Kees Cook9ba80d92012-05-03 15:45:02 +1000299
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700300 switch (type) {
301 case PSTORE_TYPE_DMESG:
302 if (id >= cxt->max_dump_cnt)
303 return -EINVAL;
304 prz = cxt->przs[id];
305 break;
306 case PSTORE_TYPE_CONSOLE:
307 prz = cxt->cprz;
308 break;
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700309 case PSTORE_TYPE_FTRACE:
310 prz = cxt->fprz;
311 break;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700312 default:
Kees Cook9ba80d92012-05-03 15:45:02 +1000313 return -EINVAL;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700314 }
Kees Cook9ba80d92012-05-03 15:45:02 +1000315
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700316 persistent_ram_free_old(prz);
317 persistent_ram_zap(prz);
Kees Cook9ba80d92012-05-03 15:45:02 +1000318
319 return 0;
320}
321
322static struct ramoops_context oops_cxt = {
323 .pstore = {
324 .owner = THIS_MODULE,
325 .name = "ramoops",
326 .open = ramoops_pstore_open,
327 .read = ramoops_pstore_read,
Anton Vorontsovc2b71132012-07-09 17:10:43 -0700328 .write_buf = ramoops_pstore_write_buf,
Kees Cook9ba80d92012-05-03 15:45:02 +1000329 .erase = ramoops_pstore_erase,
330 },
331};
332
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700333static void ramoops_free_przs(struct ramoops_context *cxt)
334{
335 int i;
336
Liu ShuoX34f0ec82014-03-17 14:07:00 -0700337 cxt->max_dump_cnt = 0;
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700338 if (!cxt->przs)
339 return;
340
Anton Vorontsov90b58d92012-06-18 19:15:51 -0700341 for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700342 persistent_ram_free(cxt->przs[i]);
343 kfree(cxt->przs);
344}
345
Greg Kroah-Hartmanf568f6c2012-12-21 15:02:05 -0800346static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
347 phys_addr_t *paddr, size_t dump_mem_sz)
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700348{
349 int err = -ENOMEM;
350 int i;
351
352 if (!cxt->record_size)
353 return 0;
354
Arve Hjønnevågc6289372012-12-11 17:49:24 -0800355 if (*paddr + dump_mem_sz - cxt->phys_addr > cxt->size) {
356 dev_err(dev, "no room for dumps\n");
357 return -ENOMEM;
358 }
359
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700360 cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
361 if (!cxt->max_dump_cnt)
362 return -ENOMEM;
363
364 cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
365 GFP_KERNEL);
366 if (!cxt->przs) {
367 dev_err(dev, "failed to initialize a prz array for dumps\n");
Liu ShuoX34f0ec82014-03-17 14:07:00 -0700368 goto fail_prz;
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700369 }
370
371 for (i = 0; i < cxt->max_dump_cnt; i++) {
372 size_t sz = cxt->record_size;
373
Arve Hjønnevågc31ad082012-05-22 16:33:23 -0700374 cxt->przs[i] = persistent_ram_new(*paddr, sz, 0,
Tony Lindgren027bc8b2014-09-16 13:50:01 -0700375 &cxt->ecc_info,
376 cxt->memtype);
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700377 if (IS_ERR(cxt->przs[i])) {
378 err = PTR_ERR(cxt->przs[i]);
379 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
380 sz, (unsigned long long)*paddr, err);
381 goto fail_prz;
382 }
383 *paddr += sz;
384 }
385
386 return 0;
387fail_prz:
388 ramoops_free_przs(cxt);
389 return err;
390}
391
Greg Kroah-Hartmanf568f6c2012-12-21 15:02:05 -0800392static int ramoops_init_prz(struct device *dev, struct ramoops_context *cxt,
393 struct persistent_ram_zone **prz,
394 phys_addr_t *paddr, size_t sz, u32 sig)
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700395{
396 if (!sz)
397 return 0;
398
Arve Hjønnevågc6289372012-12-11 17:49:24 -0800399 if (*paddr + sz - cxt->phys_addr > cxt->size) {
400 dev_err(dev, "no room for mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
401 sz, (unsigned long long)*paddr,
402 cxt->size, (unsigned long long)cxt->phys_addr);
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700403 return -ENOMEM;
Arve Hjønnevågc6289372012-12-11 17:49:24 -0800404 }
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700405
Tony Lindgren027bc8b2014-09-16 13:50:01 -0700406 *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info, cxt->memtype);
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700407 if (IS_ERR(*prz)) {
408 int err = PTR_ERR(*prz);
409
410 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
411 sz, (unsigned long long)*paddr, err);
412 return err;
413 }
414
415 persistent_ram_zap(*prz);
416
417 *paddr += sz;
418
419 return 0;
420}
421
Greg Kroah-Hartmanf568f6c2012-12-21 15:02:05 -0800422static int ramoops_probe(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700423{
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700424 struct device *dev = &pdev->dev;
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700425 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700426 struct ramoops_context *cxt = &oops_cxt;
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700427 size_t dump_mem_sz;
428 phys_addr_t paddr;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700429 int err = -EINVAL;
430
Kees Cook9ba80d92012-05-03 15:45:02 +1000431 /* Only a single ramoops area allowed at a time, so fail extra
432 * probes.
433 */
Anton Vorontsovcac2eb72012-05-26 06:20:20 -0700434 if (cxt->max_dump_cnt)
Kees Cook9ba80d92012-05-03 15:45:02 +1000435 goto fail_out;
436
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700437 if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size &&
438 !pdata->ftrace_size)) {
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700439 pr_err("The memory size and the record/console size must be "
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700440 "non-zero\n");
Kees Cook9ba80d92012-05-03 15:45:02 +1000441 goto fail_out;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700442 }
443
Maxime Bizon3bd11cf2013-08-30 18:06:41 +0200444 if (pdata->record_size && !is_power_of_2(pdata->record_size))
Maxime Bizonb042e472012-10-22 11:19:28 +0200445 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
Maxime Bizon3bd11cf2013-08-30 18:06:41 +0200446 if (pdata->console_size && !is_power_of_2(pdata->console_size))
Maxime Bizonb042e472012-10-22 11:19:28 +0200447 pdata->console_size = rounddown_pow_of_two(pdata->console_size);
Maxime Bizon3bd11cf2013-08-30 18:06:41 +0200448 if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size))
Maxime Bizonb042e472012-10-22 11:19:28 +0200449 pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700450
Marco Stornelli13aefd72011-07-26 16:08:57 -0700451 cxt->size = pdata->mem_size;
452 cxt->phys_addr = pdata->mem_address;
Tony Lindgren027bc8b2014-09-16 13:50:01 -0700453 cxt->memtype = pdata->mem_type;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700454 cxt->record_size = pdata->record_size;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700455 cxt->console_size = pdata->console_size;
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700456 cxt->ftrace_size = pdata->ftrace_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700457 cxt->dump_oops = pdata->dump_oops;
Arve Hjønnevågc31ad082012-05-22 16:33:23 -0700458 cxt->ecc_info = pdata->ecc_info;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700459
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700460 paddr = cxt->phys_addr;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700461
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700462 dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size;
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700463 err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700464 if (err)
465 goto fail_out;
466
Anton Vorontsovcbe7cbf2012-07-17 12:11:12 -0700467 err = ramoops_init_prz(dev, cxt, &cxt->cprz, &paddr,
468 cxt->console_size, 0);
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700469 if (err)
470 goto fail_init_cprz;
471
Anton Vorontsovcbe7cbf2012-07-17 12:11:12 -0700472 err = ramoops_init_prz(dev, cxt, &cxt->fprz, &paddr, cxt->ftrace_size,
473 LINUX_VERSION_CODE);
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700474 if (err)
475 goto fail_init_fprz;
476
477 if (!cxt->przs && !cxt->cprz && !cxt->fprz) {
Randy Dunlap04271932012-08-03 17:02:48 -0700478 pr_err("memory size too small, minimum is %zu\n",
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700479 cxt->console_size + cxt->record_size +
480 cxt->ftrace_size);
Wei Yongjun47110b82013-05-07 19:39:20 +0800481 err = -EINVAL;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700482 goto fail_cnt;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700483 }
484
Kees Cook9ba80d92012-05-03 15:45:02 +1000485 cxt->pstore.data = cxt;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700486 /*
Anton Vorontsova384f6412012-07-19 15:47:11 -0700487 * Console can handle any buffer size, so prefer LOG_LINE_MAX. If we
488 * have to handle dumps, we must have at least record_size buffer. And
489 * for ftrace, bufsize is irrelevant (if bufsize is 0, buf will be
490 * ZERO_SIZE_PTR).
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700491 */
Anton Vorontsova384f6412012-07-19 15:47:11 -0700492 if (cxt->console_size)
493 cxt->pstore.bufsize = 1024; /* LOG_LINE_MAX */
494 cxt->pstore.bufsize = max(cxt->record_size, cxt->pstore.bufsize);
Kees Cook9ba80d92012-05-03 15:45:02 +1000495 cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
496 spin_lock_init(&cxt->pstore.buf_lock);
497 if (!cxt->pstore.buf) {
498 pr_err("cannot allocate pstore buffer\n");
Wei Yongjun47110b82013-05-07 19:39:20 +0800499 err = -ENOMEM;
Kees Cook9ba80d92012-05-03 15:45:02 +1000500 goto fail_clear;
501 }
502
Kees Cook9ba80d92012-05-03 15:45:02 +1000503 err = pstore_register(&cxt->pstore);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700504 if (err) {
Kees Cook9ba80d92012-05-03 15:45:02 +1000505 pr_err("registering with pstore failed\n");
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700506 goto fail_buf;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700507 }
508
Kees Cookc7552012012-01-12 17:20:59 -0800509 /*
510 * Update the module parameter variables as well so they are visible
511 * through /sys/module/ramoops/parameters/
512 */
513 mem_size = pdata->mem_size;
514 mem_address = pdata->mem_address;
515 record_size = pdata->record_size;
516 dump_oops = pdata->dump_oops;
517
Arve Hjønnevågc31ad082012-05-22 16:33:23 -0700518 pr_info("attached 0x%lx@0x%llx, ecc: %d/%d\n",
Randy Dunlapd109a672012-05-03 15:45:03 +1000519 cxt->size, (unsigned long long)cxt->phys_addr,
Arve Hjønnevågc31ad082012-05-22 16:33:23 -0700520 cxt->ecc_info.ecc_size, cxt->ecc_info.block_size);
Kees Cook9ba80d92012-05-03 15:45:02 +1000521
Marco Stornelli56d611a2010-05-26 14:43:54 -0700522 return 0;
523
Kees Cook9ba80d92012-05-03 15:45:02 +1000524fail_buf:
525 kfree(cxt->pstore.buf);
526fail_clear:
527 cxt->pstore.bufsize = 0;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700528fail_cnt:
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700529 kfree(cxt->fprz);
530fail_init_fprz:
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700531 kfree(cxt->cprz);
532fail_init_cprz:
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700533 ramoops_free_przs(cxt);
Kees Cook9ba80d92012-05-03 15:45:02 +1000534fail_out:
Marco Stornelli56d611a2010-05-26 14:43:54 -0700535 return err;
536}
537
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700538static int __exit ramoops_remove(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700539{
Kees Cook9ba80d92012-05-03 15:45:02 +1000540#if 0
541 /* TODO(kees): We cannot unload ramoops since pstore doesn't support
542 * unregistering yet.
543 */
Marco Stornelli56d611a2010-05-26 14:43:54 -0700544 struct ramoops_context *cxt = &oops_cxt;
545
Marco Stornelli56d611a2010-05-26 14:43:54 -0700546 iounmap(cxt->virt_addr);
547 release_mem_region(cxt->phys_addr, cxt->size);
Anton Vorontsovcac2eb72012-05-26 06:20:20 -0700548 cxt->max_dump_cnt = 0;
Kees Cook9ba80d92012-05-03 15:45:02 +1000549
550 /* TODO(kees): When pstore supports unregistering, call it here. */
551 kfree(cxt->pstore.buf);
552 cxt->pstore.bufsize = 0;
553
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700554 return 0;
Kees Cook9ba80d92012-05-03 15:45:02 +1000555#endif
556 return -EBUSY;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700557}
558
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700559static struct platform_driver ramoops_driver = {
Anton Vorontsov924d3712012-06-18 19:15:50 -0700560 .probe = ramoops_probe,
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700561 .remove = __exit_p(ramoops_remove),
562 .driver = {
563 .name = "ramoops",
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700564 },
565};
566
Anton Vorontsov924d3712012-06-18 19:15:50 -0700567static void ramoops_register_dummy(void)
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700568{
Anton Vorontsov924d3712012-06-18 19:15:50 -0700569 if (!mem_size)
570 return;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700571
Anton Vorontsov924d3712012-06-18 19:15:50 -0700572 pr_info("using module parameters\n");
573
574 dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
575 if (!dummy_data) {
576 pr_info("could not allocate pdata\n");
577 return;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700578 }
579
Anton Vorontsov924d3712012-06-18 19:15:50 -0700580 dummy_data->mem_size = mem_size;
581 dummy_data->mem_address = mem_address;
Tony Lindgren027bc8b2014-09-16 13:50:01 -0700582 dummy_data->mem_type = 0;
Anton Vorontsov924d3712012-06-18 19:15:50 -0700583 dummy_data->record_size = record_size;
584 dummy_data->console_size = ramoops_console_size;
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700585 dummy_data->ftrace_size = ramoops_ftrace_size;
Anton Vorontsov924d3712012-06-18 19:15:50 -0700586 dummy_data->dump_oops = dump_oops;
Anton Vorontsov5ca5d4e2012-07-09 17:03:19 -0700587 /*
588 * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
589 * (using 1 byte for ECC isn't much of use anyway).
590 */
Arve Hjønnevågc31ad082012-05-22 16:33:23 -0700591 dummy_data->ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
Anton Vorontsov924d3712012-06-18 19:15:50 -0700592
593 dummy = platform_device_register_data(NULL, "ramoops", -1,
594 dummy_data, sizeof(struct ramoops_platform_data));
595 if (IS_ERR(dummy)) {
596 pr_info("could not create platform device: %ld\n",
597 PTR_ERR(dummy));
598 }
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700599}
600
Anton Vorontsov924d3712012-06-18 19:15:50 -0700601static int __init ramoops_init(void)
602{
603 ramoops_register_dummy();
604 return platform_driver_register(&ramoops_driver);
605}
606postcore_initcall(ramoops_init);
607
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700608static void __exit ramoops_exit(void)
609{
610 platform_driver_unregister(&ramoops_driver);
Jovi Zhangb4a871b2012-08-20 14:58:26 +0800611 platform_device_unregister(dummy);
Marco Stornelli13aefd72011-07-26 16:08:57 -0700612 kfree(dummy_data);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700613}
Marco Stornelli56d611a2010-05-26 14:43:54 -0700614module_exit(ramoops_exit);
615
616MODULE_LICENSE("GPL");
617MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
618MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");