blob: 6150e54eed30bc5f585c3afc25485d055ee3a0a3 [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
Mark Salyzynf44f9652015-01-13 14:32:50 -0800167static bool prz_ok(struct persistent_ram_zone *prz)
168{
169 return !!prz && !!(persistent_ram_old_size(prz) +
170 persistent_ram_ecc_string(prz, NULL, 0));
171}
172
Kees Cook9ba80d92012-05-03 15:45:02 +1000173static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
Seiji Aguchi755d4fe2012-11-26 16:07:44 -0800174 int *count, struct timespec *time,
Aruna Balakrishnaiah9a4e1392013-08-16 13:53:19 -0700175 char **buf, bool *compressed,
176 struct pstore_info *psi)
Kees Cook9ba80d92012-05-03 15:45:02 +1000177{
178 ssize_t size;
Arve Hjønnevågbd08ec32012-12-05 21:19:51 -0800179 ssize_t ecc_notice_size;
Kees Cook9ba80d92012-05-03 15:45:02 +1000180 struct ramoops_context *cxt = psi->data;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700181 struct persistent_ram_zone *prz;
Ben Zhanga28726b2014-10-30 17:14:21 -0700182 int header_length;
Kees Cook9ba80d92012-05-03 15:45:02 +1000183
Anton Vorontsov755d66b2012-05-26 06:20:22 -0700184 prz = ramoops_get_next_prz(cxt->przs, &cxt->dump_read_cnt,
185 cxt->max_dump_cnt, id, type,
186 PSTORE_TYPE_DMESG, 1);
Mark Salyzynf44f9652015-01-13 14:32:50 -0800187 if (!prz_ok(prz))
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700188 prz = ramoops_get_next_prz(&cxt->cprz, &cxt->console_read_cnt,
189 1, id, type, PSTORE_TYPE_CONSOLE, 0);
Mark Salyzynf44f9652015-01-13 14:32:50 -0800190 if (!prz_ok(prz))
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700191 prz = ramoops_get_next_prz(&cxt->fprz, &cxt->ftrace_read_cnt,
192 1, id, type, PSTORE_TYPE_FTRACE, 0);
Mark Salyzynf44f9652015-01-13 14:32:50 -0800193 if (!prz_ok(prz))
Anton Vorontsov755d66b2012-05-26 06:20:22 -0700194 return 0;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700195
Ben Zhanga28726b2014-10-30 17:14:21 -0700196 if (!persistent_ram_old(prz))
197 return 0;
198
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700199 size = persistent_ram_old_size(prz);
Ben Zhanga28726b2014-10-30 17:14:21 -0700200 header_length = ramoops_read_kmsg_hdr(persistent_ram_old(prz), time,
201 compressed);
202 size -= header_length;
Arve Hjønnevågbd08ec32012-12-05 21:19:51 -0800203
204 /* ECC correction notice */
205 ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0);
206
207 *buf = kmalloc(size + ecc_notice_size + 1, GFP_KERNEL);
Kees Cook9ba80d92012-05-03 15:45:02 +1000208 if (*buf == NULL)
209 return -ENOMEM;
Kees Cook9ba80d92012-05-03 15:45:02 +1000210
Ben Zhanga28726b2014-10-30 17:14:21 -0700211 memcpy(*buf, (char *)persistent_ram_old(prz) + header_length, size);
Arve Hjønnevågbd08ec32012-12-05 21:19:51 -0800212 persistent_ram_ecc_string(prz, *buf + size, ecc_notice_size + 1);
213
214 return size + ecc_notice_size;
Kees Cook9ba80d92012-05-03 15:45:02 +1000215}
216
Aruna Balakrishnaiah3f8f80f2013-08-16 13:58:00 -0700217static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz,
218 bool compressed)
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700219{
220 char *hdr;
Kees Cook1e817fb2012-11-19 10:26:16 -0800221 struct timespec timestamp;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700222 size_t len;
223
Kees Cook1e817fb2012-11-19 10:26:16 -0800224 /* Report zeroed timestamp if called before timekeeping has resumed. */
225 if (__getnstimeofday(&timestamp)) {
226 timestamp.tv_sec = 0;
227 timestamp.tv_nsec = 0;
228 }
Aruna Balakrishnaiah3f8f80f2013-08-16 13:58:00 -0700229 hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n",
230 (long)timestamp.tv_sec, (long)(timestamp.tv_nsec / 1000),
231 compressed ? 'C' : 'D');
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700232 WARN_ON_ONCE(!hdr);
233 len = hdr ? strlen(hdr) : 0;
234 persistent_ram_write(prz, hdr, len);
235 kfree(hdr);
236
237 return len;
238}
239
Anton Vorontsov24203032012-07-17 19:49:37 -0700240static int notrace ramoops_pstore_write_buf(enum pstore_type_id type,
241 enum kmsg_dump_reason reason,
242 u64 *id, unsigned int part,
Aruna Balakrishnaiah6bbbca72013-06-27 14:02:56 +0530243 const char *buf,
Aruna Balakrishnaiahb3b515b2013-08-16 13:52:47 -0700244 bool compressed, size_t size,
Anton Vorontsov24203032012-07-17 19:49:37 -0700245 struct pstore_info *psi)
Kees Cook9ba80d92012-05-03 15:45:02 +1000246{
Kees Cook9ba80d92012-05-03 15:45:02 +1000247 struct ramoops_context *cxt = psi->data;
Arve Hjønnevågc6289372012-12-11 17:49:24 -0800248 struct persistent_ram_zone *prz;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700249 size_t hlen;
Kees Cook9ba80d92012-05-03 15:45:02 +1000250
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700251 if (type == PSTORE_TYPE_CONSOLE) {
252 if (!cxt->cprz)
253 return -ENOMEM;
Anton Vorontsovc2b71132012-07-09 17:10:43 -0700254 persistent_ram_write(cxt->cprz, buf, size);
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700255 return 0;
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700256 } else if (type == PSTORE_TYPE_FTRACE) {
257 if (!cxt->fprz)
258 return -ENOMEM;
259 persistent_ram_write(cxt->fprz, buf, size);
260 return 0;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700261 }
262
Kees Cook9ba80d92012-05-03 15:45:02 +1000263 if (type != PSTORE_TYPE_DMESG)
264 return -EINVAL;
265
266 /* Out of the various dmesg dump types, ramoops is currently designed
267 * to only store crash logs, rather than storing general kernel logs.
268 */
Seiji Aguchifc2d5572011-01-12 16:59:29 -0800269 if (reason != KMSG_DUMP_OOPS &&
WANG Conga3dd3322012-01-12 17:20:11 -0800270 reason != KMSG_DUMP_PANIC)
Kees Cook9ba80d92012-05-03 15:45:02 +1000271 return -EINVAL;
Seiji Aguchifc2d5572011-01-12 16:59:29 -0800272
Kees Cook9ba80d92012-05-03 15:45:02 +1000273 /* Skip Oopes when configured to do so. */
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700274 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
Kees Cook9ba80d92012-05-03 15:45:02 +1000275 return -EINVAL;
276
277 /* Explicitly only take the first part of any new crash.
278 * If our buffer is larger than kmsg_bytes, this can never happen,
279 * and if our buffer is smaller than kmsg_bytes, we don't want the
280 * report split across multiple records.
281 */
282 if (part != 1)
283 return -ENOSPC;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700284
Arve Hjønnevågc6289372012-12-11 17:49:24 -0800285 if (!cxt->przs)
286 return -ENOSPC;
287
288 prz = cxt->przs[cxt->dump_write_cnt];
289
Aruna Balakrishnaiah3f8f80f2013-08-16 13:58:00 -0700290 hlen = ramoops_write_kmsg_hdr(prz, compressed);
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700291 if (size + hlen > prz->buffer_size)
292 size = prz->buffer_size - hlen;
Anton Vorontsovc2b71132012-07-09 17:10:43 -0700293 persistent_ram_write(prz, buf, size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700294
Anton Vorontsovcac2eb72012-05-26 06:20:20 -0700295 cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
Kees Cook9ba80d92012-05-03 15:45:02 +1000296
297 return 0;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700298}
299
Seiji Aguchi755d4fe2012-11-26 16:07:44 -0800300static int ramoops_pstore_erase(enum pstore_type_id type, u64 id, int count,
Seiji Aguchia9efd392012-11-14 20:27:28 +0000301 struct timespec time, struct pstore_info *psi)
Kees Cook9ba80d92012-05-03 15:45:02 +1000302{
Kees Cook9ba80d92012-05-03 15:45:02 +1000303 struct ramoops_context *cxt = psi->data;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700304 struct persistent_ram_zone *prz;
Kees Cook9ba80d92012-05-03 15:45:02 +1000305
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700306 switch (type) {
307 case PSTORE_TYPE_DMESG:
308 if (id >= cxt->max_dump_cnt)
309 return -EINVAL;
310 prz = cxt->przs[id];
311 break;
312 case PSTORE_TYPE_CONSOLE:
313 prz = cxt->cprz;
314 break;
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700315 case PSTORE_TYPE_FTRACE:
316 prz = cxt->fprz;
317 break;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700318 default:
Kees Cook9ba80d92012-05-03 15:45:02 +1000319 return -EINVAL;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700320 }
Kees Cook9ba80d92012-05-03 15:45:02 +1000321
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700322 persistent_ram_free_old(prz);
323 persistent_ram_zap(prz);
Kees Cook9ba80d92012-05-03 15:45:02 +1000324
325 return 0;
326}
327
328static struct ramoops_context oops_cxt = {
329 .pstore = {
330 .owner = THIS_MODULE,
331 .name = "ramoops",
332 .open = ramoops_pstore_open,
333 .read = ramoops_pstore_read,
Anton Vorontsovc2b71132012-07-09 17:10:43 -0700334 .write_buf = ramoops_pstore_write_buf,
Kees Cook9ba80d92012-05-03 15:45:02 +1000335 .erase = ramoops_pstore_erase,
336 },
337};
338
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700339static void ramoops_free_przs(struct ramoops_context *cxt)
340{
341 int i;
342
Liu ShuoX34f0ec82014-03-17 14:07:00 -0700343 cxt->max_dump_cnt = 0;
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700344 if (!cxt->przs)
345 return;
346
Anton Vorontsov90b58d92012-06-18 19:15:51 -0700347 for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700348 persistent_ram_free(cxt->przs[i]);
349 kfree(cxt->przs);
350}
351
Greg Kroah-Hartmanf568f6c2012-12-21 15:02:05 -0800352static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
353 phys_addr_t *paddr, size_t dump_mem_sz)
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700354{
355 int err = -ENOMEM;
356 int i;
357
358 if (!cxt->record_size)
359 return 0;
360
Arve Hjønnevågc6289372012-12-11 17:49:24 -0800361 if (*paddr + dump_mem_sz - cxt->phys_addr > cxt->size) {
362 dev_err(dev, "no room for dumps\n");
363 return -ENOMEM;
364 }
365
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700366 cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
367 if (!cxt->max_dump_cnt)
368 return -ENOMEM;
369
370 cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
371 GFP_KERNEL);
372 if (!cxt->przs) {
373 dev_err(dev, "failed to initialize a prz array for dumps\n");
Liu ShuoX34f0ec82014-03-17 14:07:00 -0700374 goto fail_prz;
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700375 }
376
377 for (i = 0; i < cxt->max_dump_cnt; i++) {
378 size_t sz = cxt->record_size;
379
Arve Hjønnevågc31ad082012-05-22 16:33:23 -0700380 cxt->przs[i] = persistent_ram_new(*paddr, sz, 0,
Tony Lindgren027bc8b2014-09-16 13:50:01 -0700381 &cxt->ecc_info,
382 cxt->memtype);
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700383 if (IS_ERR(cxt->przs[i])) {
384 err = PTR_ERR(cxt->przs[i]);
385 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
386 sz, (unsigned long long)*paddr, err);
387 goto fail_prz;
388 }
389 *paddr += sz;
390 }
391
392 return 0;
393fail_prz:
394 ramoops_free_przs(cxt);
395 return err;
396}
397
Greg Kroah-Hartmanf568f6c2012-12-21 15:02:05 -0800398static int ramoops_init_prz(struct device *dev, struct ramoops_context *cxt,
399 struct persistent_ram_zone **prz,
400 phys_addr_t *paddr, size_t sz, u32 sig)
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700401{
402 if (!sz)
403 return 0;
404
Arve Hjønnevågc6289372012-12-11 17:49:24 -0800405 if (*paddr + sz - cxt->phys_addr > cxt->size) {
406 dev_err(dev, "no room for mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
407 sz, (unsigned long long)*paddr,
408 cxt->size, (unsigned long long)cxt->phys_addr);
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700409 return -ENOMEM;
Arve Hjønnevågc6289372012-12-11 17:49:24 -0800410 }
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700411
Tony Lindgren027bc8b2014-09-16 13:50:01 -0700412 *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info, cxt->memtype);
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700413 if (IS_ERR(*prz)) {
414 int err = PTR_ERR(*prz);
415
416 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
417 sz, (unsigned long long)*paddr, err);
418 return err;
419 }
420
421 persistent_ram_zap(*prz);
422
423 *paddr += sz;
424
425 return 0;
426}
427
Greg Kroah-Hartmanf568f6c2012-12-21 15:02:05 -0800428static int ramoops_probe(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700429{
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700430 struct device *dev = &pdev->dev;
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700431 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700432 struct ramoops_context *cxt = &oops_cxt;
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700433 size_t dump_mem_sz;
434 phys_addr_t paddr;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700435 int err = -EINVAL;
436
Kees Cook9ba80d92012-05-03 15:45:02 +1000437 /* Only a single ramoops area allowed at a time, so fail extra
438 * probes.
439 */
Anton Vorontsovcac2eb72012-05-26 06:20:20 -0700440 if (cxt->max_dump_cnt)
Kees Cook9ba80d92012-05-03 15:45:02 +1000441 goto fail_out;
442
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700443 if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size &&
444 !pdata->ftrace_size)) {
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700445 pr_err("The memory size and the record/console size must be "
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700446 "non-zero\n");
Kees Cook9ba80d92012-05-03 15:45:02 +1000447 goto fail_out;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700448 }
449
Maxime Bizon3bd11cf2013-08-30 18:06:41 +0200450 if (pdata->record_size && !is_power_of_2(pdata->record_size))
Maxime Bizonb042e472012-10-22 11:19:28 +0200451 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
Maxime Bizon3bd11cf2013-08-30 18:06:41 +0200452 if (pdata->console_size && !is_power_of_2(pdata->console_size))
Maxime Bizonb042e472012-10-22 11:19:28 +0200453 pdata->console_size = rounddown_pow_of_two(pdata->console_size);
Maxime Bizon3bd11cf2013-08-30 18:06:41 +0200454 if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size))
Maxime Bizonb042e472012-10-22 11:19:28 +0200455 pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700456
Marco Stornelli13aefd72011-07-26 16:08:57 -0700457 cxt->size = pdata->mem_size;
458 cxt->phys_addr = pdata->mem_address;
Tony Lindgren027bc8b2014-09-16 13:50:01 -0700459 cxt->memtype = pdata->mem_type;
Sergiu Iordache3e5c4fa2011-07-26 16:08:59 -0700460 cxt->record_size = pdata->record_size;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700461 cxt->console_size = pdata->console_size;
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700462 cxt->ftrace_size = pdata->ftrace_size;
Sergiu Iordache6b4d2a22011-07-26 16:08:58 -0700463 cxt->dump_oops = pdata->dump_oops;
Arve Hjønnevågc31ad082012-05-22 16:33:23 -0700464 cxt->ecc_info = pdata->ecc_info;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700465
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700466 paddr = cxt->phys_addr;
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700467
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700468 dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size;
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700469 err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700470 if (err)
471 goto fail_out;
472
Anton Vorontsovcbe7cbf2012-07-17 12:11:12 -0700473 err = ramoops_init_prz(dev, cxt, &cxt->cprz, &paddr,
474 cxt->console_size, 0);
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700475 if (err)
476 goto fail_init_cprz;
477
Anton Vorontsovcbe7cbf2012-07-17 12:11:12 -0700478 err = ramoops_init_prz(dev, cxt, &cxt->fprz, &paddr, cxt->ftrace_size,
479 LINUX_VERSION_CODE);
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700480 if (err)
481 goto fail_init_fprz;
482
Kees Cook9ba80d92012-05-03 15:45:02 +1000483 cxt->pstore.data = cxt;
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700484 /*
Anton Vorontsova384f6412012-07-19 15:47:11 -0700485 * Console can handle any buffer size, so prefer LOG_LINE_MAX. If we
486 * have to handle dumps, we must have at least record_size buffer. And
487 * for ftrace, bufsize is irrelevant (if bufsize is 0, buf will be
488 * ZERO_SIZE_PTR).
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700489 */
Anton Vorontsova384f6412012-07-19 15:47:11 -0700490 if (cxt->console_size)
491 cxt->pstore.bufsize = 1024; /* LOG_LINE_MAX */
492 cxt->pstore.bufsize = max(cxt->record_size, cxt->pstore.bufsize);
Kees Cook9ba80d92012-05-03 15:45:02 +1000493 cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
494 spin_lock_init(&cxt->pstore.buf_lock);
495 if (!cxt->pstore.buf) {
496 pr_err("cannot allocate pstore buffer\n");
Wei Yongjun47110b82013-05-07 19:39:20 +0800497 err = -ENOMEM;
Kees Cook9ba80d92012-05-03 15:45:02 +1000498 goto fail_clear;
499 }
500
Kees Cook9ba80d92012-05-03 15:45:02 +1000501 err = pstore_register(&cxt->pstore);
Marco Stornelli56d611a2010-05-26 14:43:54 -0700502 if (err) {
Kees Cook9ba80d92012-05-03 15:45:02 +1000503 pr_err("registering with pstore failed\n");
Anton Vorontsov896fc1f2012-05-17 00:15:18 -0700504 goto fail_buf;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700505 }
506
Kees Cookc7552012012-01-12 17:20:59 -0800507 /*
508 * Update the module parameter variables as well so they are visible
509 * through /sys/module/ramoops/parameters/
510 */
511 mem_size = pdata->mem_size;
512 mem_address = pdata->mem_address;
513 record_size = pdata->record_size;
514 dump_oops = pdata->dump_oops;
515
Arve Hjønnevågc31ad082012-05-22 16:33:23 -0700516 pr_info("attached 0x%lx@0x%llx, ecc: %d/%d\n",
Randy Dunlapd109a672012-05-03 15:45:03 +1000517 cxt->size, (unsigned long long)cxt->phys_addr,
Arve Hjønnevågc31ad082012-05-22 16:33:23 -0700518 cxt->ecc_info.ecc_size, cxt->ecc_info.block_size);
Kees Cook9ba80d92012-05-03 15:45:02 +1000519
Marco Stornelli56d611a2010-05-26 14:43:54 -0700520 return 0;
521
Kees Cook9ba80d92012-05-03 15:45:02 +1000522fail_buf:
523 kfree(cxt->pstore.buf);
524fail_clear:
525 cxt->pstore.bufsize = 0;
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700526 kfree(cxt->fprz);
527fail_init_fprz:
Anton Vorontsovb5d38e92012-05-26 06:20:23 -0700528 kfree(cxt->cprz);
529fail_init_cprz:
Anton Vorontsovf4c5d242012-05-26 06:20:21 -0700530 ramoops_free_przs(cxt);
Kees Cook9ba80d92012-05-03 15:45:02 +1000531fail_out:
Marco Stornelli56d611a2010-05-26 14:43:54 -0700532 return err;
533}
534
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700535static int __exit ramoops_remove(struct platform_device *pdev)
Marco Stornelli56d611a2010-05-26 14:43:54 -0700536{
Kees Cook9ba80d92012-05-03 15:45:02 +1000537#if 0
538 /* TODO(kees): We cannot unload ramoops since pstore doesn't support
539 * unregistering yet.
540 */
Marco Stornelli56d611a2010-05-26 14:43:54 -0700541 struct ramoops_context *cxt = &oops_cxt;
542
Marco Stornelli56d611a2010-05-26 14:43:54 -0700543 iounmap(cxt->virt_addr);
544 release_mem_region(cxt->phys_addr, cxt->size);
Anton Vorontsovcac2eb72012-05-26 06:20:20 -0700545 cxt->max_dump_cnt = 0;
Kees Cook9ba80d92012-05-03 15:45:02 +1000546
547 /* TODO(kees): When pstore supports unregistering, call it here. */
548 kfree(cxt->pstore.buf);
549 cxt->pstore.bufsize = 0;
550
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700551 return 0;
Kees Cook9ba80d92012-05-03 15:45:02 +1000552#endif
553 return -EBUSY;
Marco Stornelli56d611a2010-05-26 14:43:54 -0700554}
555
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700556static struct platform_driver ramoops_driver = {
Anton Vorontsov924d3712012-06-18 19:15:50 -0700557 .probe = ramoops_probe,
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700558 .remove = __exit_p(ramoops_remove),
559 .driver = {
560 .name = "ramoops",
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700561 },
562};
563
Anton Vorontsov924d3712012-06-18 19:15:50 -0700564static void ramoops_register_dummy(void)
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700565{
Anton Vorontsov924d3712012-06-18 19:15:50 -0700566 if (!mem_size)
567 return;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700568
Anton Vorontsov924d3712012-06-18 19:15:50 -0700569 pr_info("using module parameters\n");
570
571 dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
572 if (!dummy_data) {
573 pr_info("could not allocate pdata\n");
574 return;
Marco Stornelli13aefd72011-07-26 16:08:57 -0700575 }
576
Anton Vorontsov924d3712012-06-18 19:15:50 -0700577 dummy_data->mem_size = mem_size;
578 dummy_data->mem_address = mem_address;
Tony Lindgren027bc8b2014-09-16 13:50:01 -0700579 dummy_data->mem_type = 0;
Anton Vorontsov924d3712012-06-18 19:15:50 -0700580 dummy_data->record_size = record_size;
581 dummy_data->console_size = ramoops_console_size;
Anton Vorontsova694d1b2012-07-09 17:10:44 -0700582 dummy_data->ftrace_size = ramoops_ftrace_size;
Anton Vorontsov924d3712012-06-18 19:15:50 -0700583 dummy_data->dump_oops = dump_oops;
Anton Vorontsov5ca5d4e2012-07-09 17:03:19 -0700584 /*
585 * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
586 * (using 1 byte for ECC isn't much of use anyway).
587 */
Arve Hjønnevågc31ad082012-05-22 16:33:23 -0700588 dummy_data->ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
Anton Vorontsov924d3712012-06-18 19:15:50 -0700589
590 dummy = platform_device_register_data(NULL, "ramoops", -1,
591 dummy_data, sizeof(struct ramoops_platform_data));
592 if (IS_ERR(dummy)) {
593 pr_info("could not create platform device: %ld\n",
594 PTR_ERR(dummy));
595 }
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700596}
597
Anton Vorontsov924d3712012-06-18 19:15:50 -0700598static int __init ramoops_init(void)
599{
600 ramoops_register_dummy();
601 return platform_driver_register(&ramoops_driver);
602}
603postcore_initcall(ramoops_init);
604
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700605static void __exit ramoops_exit(void)
606{
607 platform_driver_unregister(&ramoops_driver);
Jovi Zhangb4a871b2012-08-20 14:58:26 +0800608 platform_device_unregister(dummy);
Marco Stornelli13aefd72011-07-26 16:08:57 -0700609 kfree(dummy_data);
Kyungmin Parkc3b92ce2010-10-27 15:34:52 -0700610}
Marco Stornelli56d611a2010-05-26 14:43:54 -0700611module_exit(ramoops_exit);
612
613MODULE_LICENSE("GPL");
614MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
615MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");