1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * RAM Oops/Panic logger
4 *
5 * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
6 * Copyright (C) 2011 Kees Cook <keescook@chromium.org>
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/kernel.h>
12 #include <linux/err.h>
13 #include <linux/module.h>
14 #include <linux/version.h>
15 #include <linux/pstore.h>
16 #include <linux/io.h>
17 #include <linux/ioport.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/compiler.h>
21 #include <linux/pstore_ram.h>
22 #include <linux/of.h>
23 #include <linux/of_address.h>
24 #include "internal.h"
25
26 #define RAMOOPS_KERNMSG_HDR "===="
27 #define MIN_MEM_SIZE 4096UL
28
29 static ulong record_size = MIN_MEM_SIZE;
30 module_param(record_size, ulong, 0400);
31 MODULE_PARM_DESC(record_size,
32 "size of each dump done on oops/panic");
33
34 static ulong ramoops_console_size = MIN_MEM_SIZE;
35 module_param_named(console_size, ramoops_console_size, ulong, 0400);
36 MODULE_PARM_DESC(console_size, "size of kernel console log");
37
38 static ulong ramoops_ftrace_size = MIN_MEM_SIZE;
39 module_param_named(ftrace_size, ramoops_ftrace_size, ulong, 0400);
40 MODULE_PARM_DESC(ftrace_size, "size of ftrace log");
41
42 static ulong ramoops_pmsg_size = MIN_MEM_SIZE;
43 module_param_named(pmsg_size, ramoops_pmsg_size, ulong, 0400);
44 MODULE_PARM_DESC(pmsg_size, "size of user space message log");
45
46 static ulong ramoops_blackbox_size = MIN_MEM_SIZE;
47 module_param_named(blackbox_size, ramoops_blackbox_size, ulong, 0400);
48 MODULE_PARM_DESC(blackbox_size, "size of blackbox log");
49 #if IS_ENABLED(CONFIG_PSTORE_BLACKBOX)
50 bool pstore_ready;
51 #endif
52
53
54 static unsigned long long mem_address;
55 module_param_hw(mem_address, ullong, other, 0400);
56 MODULE_PARM_DESC(mem_address,
57 "start of reserved RAM used to store oops/panic logs");
58
59 static ulong mem_size;
60 module_param(mem_size, ulong, 0400);
61 MODULE_PARM_DESC(mem_size,
62 "size of reserved RAM used to store oops/panic logs");
63
64 static unsigned int mem_type;
65 module_param(mem_type, uint, 0400);
66 MODULE_PARM_DESC(mem_type,
67 "set to 1 to try to use unbuffered memory (default 0)");
68
69 static int ramoops_max_reason = -1;
70 module_param_named(max_reason, ramoops_max_reason, int, 0400);
71 MODULE_PARM_DESC(max_reason,
72 "maximum reason for kmsg dump (default 2: Oops and Panic) ");
73
74 static int ramoops_ecc;
75 module_param_named(ecc, ramoops_ecc, int, 0400);
76 MODULE_PARM_DESC(ramoops_ecc,
77 "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)");
80
81 static int ramoops_dump_oops = -1;
82 module_param_named(dump_oops, ramoops_dump_oops, int, 0400);
83 MODULE_PARM_DESC(dump_oops,
84 "(deprecated: use max_reason instead) set to 1 to dump oopses & panics, 0 to only dump panics");
85
86 struct ramoops_context {
87 struct persistent_ram_zone **dprzs; /* Oops dump zones */
88 struct persistent_ram_zone *cprz; /* Console zone */
89 struct persistent_ram_zone **fprzs; /* Ftrace zones */
90 struct persistent_ram_zone *mprz; /* PMSG zone */
91 struct persistent_ram_zone *bprz; /* BLACKBOX zone */
92 phys_addr_t phys_addr;
93 unsigned long size;
94 unsigned int memtype;
95 size_t record_size;
96 size_t console_size;
97 size_t ftrace_size;
98 size_t pmsg_size;
99 size_t blackbox_size;
100 u32 flags;
101 struct persistent_ram_ecc_info ecc_info;
102 unsigned int max_dump_cnt;
103 unsigned int dump_write_cnt;
104 /* _read_cnt need clear on ramoops_pstore_open */
105 unsigned int dump_read_cnt;
106 unsigned int console_read_cnt;
107 unsigned int max_ftrace_cnt;
108 unsigned int ftrace_read_cnt;
109 unsigned int pmsg_read_cnt;
110 unsigned int blackbox_read_cnt;
111 struct pstore_info pstore;
112 };
113
114 static struct platform_device *dummy;
115
ramoops_pstore_open(struct pstore_info * psi)116 static int ramoops_pstore_open(struct pstore_info *psi)
117 {
118 struct ramoops_context *cxt = psi->data;
119
120 cxt->dump_read_cnt = 0;
121 cxt->console_read_cnt = 0;
122 cxt->ftrace_read_cnt = 0;
123 cxt->pmsg_read_cnt = 0;
124 cxt->blackbox_read_cnt = 0;
125 return 0;
126 }
127
128 static struct persistent_ram_zone *
ramoops_get_next_prz(struct persistent_ram_zone * przs[],int id,struct pstore_record * record)129 ramoops_get_next_prz(struct persistent_ram_zone *przs[], int id,
130 struct pstore_record *record)
131 {
132 struct persistent_ram_zone *prz;
133
134 /* Give up if we never existed or have hit the end. */
135 if (!przs)
136 return NULL;
137
138 prz = przs[id];
139 if (!prz)
140 return NULL;
141
142 /* Update old/shadowed buffer. */
143 if (prz->type == PSTORE_TYPE_DMESG)
144 persistent_ram_save_old(prz);
145
146 if (!persistent_ram_old_size(prz))
147 return NULL;
148
149 record->type = prz->type;
150 record->id = id;
151
152 return prz;
153 }
154
ramoops_read_kmsg_hdr(char * buffer,struct timespec64 * time,bool * compressed)155 static int ramoops_read_kmsg_hdr(char *buffer, struct timespec64 *time,
156 bool *compressed)
157 {
158 char data_type;
159 int header_length = 0;
160
161 if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lld.%lu-%c\n%n",
162 (time64_t *)&time->tv_sec, &time->tv_nsec, &data_type,
163 &header_length) == 3) {
164 time->tv_nsec *= 1000;
165 if (data_type == 'C')
166 *compressed = true;
167 else
168 *compressed = false;
169 } else if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lld.%lu\n%n",
170 (time64_t *)&time->tv_sec, &time->tv_nsec,
171 &header_length) == 2) {
172 time->tv_nsec *= 1000;
173 *compressed = false;
174 } else {
175 time->tv_sec = 0;
176 time->tv_nsec = 0;
177 *compressed = false;
178 }
179 return header_length;
180 }
181
prz_ok(struct persistent_ram_zone * prz)182 static bool prz_ok(struct persistent_ram_zone *prz)
183 {
184 return !!prz && !!(persistent_ram_old_size(prz) +
185 persistent_ram_ecc_string(prz, NULL, 0));
186 }
187
ramoops_pstore_read(struct pstore_record * record)188 static ssize_t ramoops_pstore_read(struct pstore_record *record)
189 {
190 ssize_t size = 0;
191 struct ramoops_context *cxt = record->psi->data;
192 struct persistent_ram_zone *prz = NULL;
193 int header_length = 0;
194 bool free_prz = false;
195
196 /*
197 * Ramoops headers provide time stamps for PSTORE_TYPE_DMESG, but
198 * PSTORE_TYPE_CONSOLE and PSTORE_TYPE_FTRACE don't currently have
199 * valid time stamps, so it is initialized to zero.
200 */
201 record->time.tv_sec = 0;
202 record->time.tv_nsec = 0;
203 record->compressed = false;
204
205 /* Find the next valid persistent_ram_zone for DMESG */
206 while (cxt->dump_read_cnt < cxt->max_dump_cnt && !prz) {
207 prz = ramoops_get_next_prz(cxt->dprzs, cxt->dump_read_cnt++,
208 record);
209 if (!prz_ok(prz))
210 continue;
211 header_length = ramoops_read_kmsg_hdr(persistent_ram_old(prz),
212 &record->time,
213 &record->compressed);
214 /* Clear and skip this DMESG record if it has no valid header */
215 if (!header_length) {
216 persistent_ram_free_old(prz);
217 persistent_ram_zap(prz);
218 prz = NULL;
219 }
220 }
221
222 if (!prz_ok(prz) && !cxt->console_read_cnt++)
223 prz = ramoops_get_next_prz(&cxt->cprz, 0 /* single */, record);
224
225 if (!prz_ok(prz) && !cxt->pmsg_read_cnt++)
226 prz = ramoops_get_next_prz(&cxt->mprz, 0 /* single */, record);
227
228 if (!prz_ok(prz) && !cxt->blackbox_read_cnt++)
229 prz = ramoops_get_next_prz(&cxt->bprz, 0 /* single */, record);
230
231 /* ftrace is last since it may want to dynamically allocate memory. */
232 if (!prz_ok(prz)) {
233 if (!(cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU) &&
234 !cxt->ftrace_read_cnt++) {
235 prz = ramoops_get_next_prz(cxt->fprzs, 0 /* single */,
236 record);
237 } else {
238 /*
239 * Build a new dummy record which combines all the
240 * per-cpu records including metadata and ecc info.
241 */
242 struct persistent_ram_zone *tmp_prz, *prz_next;
243
244 tmp_prz = kzalloc(sizeof(struct persistent_ram_zone),
245 GFP_KERNEL);
246 if (!tmp_prz)
247 return -ENOMEM;
248 prz = tmp_prz;
249 free_prz = true;
250
251 while (cxt->ftrace_read_cnt < cxt->max_ftrace_cnt) {
252 prz_next = ramoops_get_next_prz(cxt->fprzs,
253 cxt->ftrace_read_cnt++, record);
254
255 if (!prz_ok(prz_next))
256 continue;
257
258 tmp_prz->ecc_info = prz_next->ecc_info;
259 tmp_prz->corrected_bytes +=
260 prz_next->corrected_bytes;
261 tmp_prz->bad_blocks += prz_next->bad_blocks;
262
263 size = pstore_ftrace_combine_log(
264 &tmp_prz->old_log,
265 &tmp_prz->old_log_size,
266 prz_next->old_log,
267 prz_next->old_log_size);
268 if (size)
269 goto out;
270 }
271 record->id = 0;
272 }
273 }
274
275 if (!prz_ok(prz)) {
276 size = 0;
277 goto out;
278 }
279
280 size = persistent_ram_old_size(prz) - header_length;
281
282 /* ECC correction notice */
283 record->ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0);
284
285 record->buf = kmalloc(size + record->ecc_notice_size + 1, GFP_KERNEL);
286 if (record->buf == NULL) {
287 size = -ENOMEM;
288 goto out;
289 }
290
291 memcpy(record->buf, (char *)persistent_ram_old(prz) + header_length,
292 size);
293
294 persistent_ram_ecc_string(prz, record->buf + size,
295 record->ecc_notice_size + 1);
296
297 out:
298 if (free_prz) {
299 kfree(prz->old_log);
300 kfree(prz);
301 }
302
303 return size;
304 }
305
ramoops_write_kmsg_hdr(struct persistent_ram_zone * prz,struct pstore_record * record)306 static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz,
307 struct pstore_record *record)
308 {
309 char hdr[36]; /* "===="(4), %lld(20), "."(1), %06lu(6), "-%c\n"(3) */
310 size_t len;
311
312 len = scnprintf(hdr, sizeof(hdr),
313 RAMOOPS_KERNMSG_HDR "%lld.%06lu-%c\n",
314 (time64_t)record->time.tv_sec,
315 record->time.tv_nsec / 1000,
316 record->compressed ? 'C' : 'D');
317 persistent_ram_write(prz, hdr, len);
318
319 return len;
320 }
321
ramoops_pstore_write(struct pstore_record * record)322 static int notrace ramoops_pstore_write(struct pstore_record *record)
323 {
324 struct ramoops_context *cxt = record->psi->data;
325 struct persistent_ram_zone *prz;
326 size_t size, hlen;
327
328 if (record->type == PSTORE_TYPE_CONSOLE) {
329 if (!cxt->cprz)
330 return -ENOMEM;
331 persistent_ram_write(cxt->cprz, record->buf, record->size);
332 return 0;
333 } else if (record->type == PSTORE_TYPE_FTRACE) {
334 int zonenum;
335
336 if (!cxt->fprzs)
337 return -ENOMEM;
338 /*
339 * Choose zone by if we're using per-cpu buffers.
340 */
341 if (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
342 zonenum = smp_processor_id();
343 else
344 zonenum = 0;
345
346 persistent_ram_write(cxt->fprzs[zonenum], record->buf,
347 record->size);
348 return 0;
349 } else if (record->type == PSTORE_TYPE_PMSG) {
350 pr_warn_ratelimited("PMSG shouldn't call %s\n", __func__);
351 return -EINVAL;
352 } else if (record->type == PSTORE_TYPE_BLACKBOX) {
353 if (!cxt->bprz)
354 return -ENOMEM;
355 persistent_ram_write(cxt->bprz, record->buf, record->size);
356 return 0;
357 }
358
359 if (record->type != PSTORE_TYPE_DMESG)
360 return -EINVAL;
361
362 /*
363 * We could filter on record->reason here if we wanted to (which
364 * would duplicate what happened before the "max_reason" setting
365 * was added), but that would defeat the purpose of a system
366 * changing printk.always_kmsg_dump, so instead log everything that
367 * the kmsg dumper sends us, since it should be doing the filtering
368 * based on the combination of printk.always_kmsg_dump and our
369 * requested "max_reason".
370 */
371
372 /*
373 * Explicitly only take the first part of any new crash.
374 * If our buffer is larger than kmsg_bytes, this can never happen,
375 * and if our buffer is smaller than kmsg_bytes, we don't want the
376 * report split across multiple records.
377 */
378 if (record->part != 1)
379 return -ENOSPC;
380
381 if (!cxt->dprzs)
382 return -ENOSPC;
383
384 prz = cxt->dprzs[cxt->dump_write_cnt];
385
386 /*
387 * Since this is a new crash dump, we need to reset the buffer in
388 * case it still has an old dump present. Without this, the new dump
389 * will get appended, which would seriously confuse anything trying
390 * to check dump file contents. Specifically, ramoops_read_kmsg_hdr()
391 * expects to find a dump header in the beginning of buffer data, so
392 * we must to reset the buffer values, in order to ensure that the
393 * header will be written to the beginning of the buffer.
394 */
395 persistent_ram_zap(prz);
396
397 /* Build header and append record contents. */
398 hlen = ramoops_write_kmsg_hdr(prz, record);
399 if (!hlen)
400 return -ENOMEM;
401
402 size = record->size;
403 if (size + hlen > prz->buffer_size)
404 size = prz->buffer_size - hlen;
405 persistent_ram_write(prz, record->buf, size);
406
407 cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
408
409 return 0;
410 }
411
ramoops_pstore_write_user(struct pstore_record * record,const char __user * buf)412 static int notrace ramoops_pstore_write_user(struct pstore_record *record,
413 const char __user *buf)
414 {
415 if (record->type == PSTORE_TYPE_PMSG) {
416 struct ramoops_context *cxt = record->psi->data;
417
418 if (!cxt->mprz)
419 return -ENOMEM;
420 return persistent_ram_write_user(cxt->mprz, buf, record->size);
421 }
422
423 return -EINVAL;
424 }
425
ramoops_pstore_erase(struct pstore_record * record)426 static int ramoops_pstore_erase(struct pstore_record *record)
427 {
428 struct ramoops_context *cxt = record->psi->data;
429 struct persistent_ram_zone *prz;
430
431 switch (record->type) {
432 case PSTORE_TYPE_DMESG:
433 if (record->id >= cxt->max_dump_cnt)
434 return -EINVAL;
435 prz = cxt->dprzs[record->id];
436 break;
437 case PSTORE_TYPE_CONSOLE:
438 prz = cxt->cprz;
439 break;
440 case PSTORE_TYPE_FTRACE:
441 if (record->id >= cxt->max_ftrace_cnt)
442 return -EINVAL;
443 prz = cxt->fprzs[record->id];
444 break;
445 case PSTORE_TYPE_PMSG:
446 prz = cxt->mprz;
447 break;
448 case PSTORE_TYPE_BLACKBOX:
449 prz = cxt->bprz;
450 break;
451 default:
452 return -EINVAL;
453 }
454
455 persistent_ram_free_old(prz);
456 persistent_ram_zap(prz);
457
458 return 0;
459 }
460
461 static struct ramoops_context oops_cxt = {
462 .pstore = {
463 .owner = THIS_MODULE,
464 .name = "ramoops",
465 .open = ramoops_pstore_open,
466 .read = ramoops_pstore_read,
467 .write = ramoops_pstore_write,
468 .write_user = ramoops_pstore_write_user,
469 .erase = ramoops_pstore_erase,
470 },
471 };
472
ramoops_free_przs(struct ramoops_context * cxt)473 static void ramoops_free_przs(struct ramoops_context *cxt)
474 {
475 int i;
476
477 /* Free dump PRZs */
478 if (cxt->dprzs) {
479 for (i = 0; i < cxt->max_dump_cnt; i++)
480 persistent_ram_free(cxt->dprzs[i]);
481
482 kfree(cxt->dprzs);
483 cxt->max_dump_cnt = 0;
484 }
485
486 /* Free ftrace PRZs */
487 if (cxt->fprzs) {
488 for (i = 0; i < cxt->max_ftrace_cnt; i++)
489 persistent_ram_free(cxt->fprzs[i]);
490 kfree(cxt->fprzs);
491 cxt->max_ftrace_cnt = 0;
492 }
493 }
494
ramoops_init_przs(const char * name,struct device * dev,struct ramoops_context * cxt,struct persistent_ram_zone *** przs,phys_addr_t * paddr,size_t mem_sz,ssize_t record_size,unsigned int * cnt,u32 sig,u32 flags)495 static int ramoops_init_przs(const char *name,
496 struct device *dev, struct ramoops_context *cxt,
497 struct persistent_ram_zone ***przs,
498 phys_addr_t *paddr, size_t mem_sz,
499 ssize_t record_size,
500 unsigned int *cnt, u32 sig, u32 flags)
501 {
502 int err = -ENOMEM;
503 int i;
504 size_t zone_sz;
505 struct persistent_ram_zone **prz_ar;
506
507 /* Allocate nothing for 0 mem_sz or 0 record_size. */
508 if (mem_sz == 0 || record_size == 0) {
509 *cnt = 0;
510 return 0;
511 }
512
513 /*
514 * If we have a negative record size, calculate it based on
515 * mem_sz / *cnt. If we have a positive record size, calculate
516 * cnt from mem_sz / record_size.
517 */
518 if (record_size < 0) {
519 if (*cnt == 0)
520 return 0;
521 record_size = mem_sz / *cnt;
522 if (record_size == 0) {
523 dev_err(dev, "%s record size == 0 (%zu / %u)\n",
524 name, mem_sz, *cnt);
525 goto fail;
526 }
527 } else {
528 *cnt = mem_sz / record_size;
529 if (*cnt == 0) {
530 dev_err(dev, "%s record count == 0 (%zu / %zu)\n",
531 name, mem_sz, record_size);
532 goto fail;
533 }
534 }
535
536 if (*paddr + mem_sz - cxt->phys_addr > cxt->size) {
537 dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
538 name,
539 mem_sz, (unsigned long long)*paddr,
540 cxt->size, (unsigned long long)cxt->phys_addr);
541 goto fail;
542 }
543
544 zone_sz = mem_sz / *cnt;
545 if (!zone_sz) {
546 dev_err(dev, "%s zone size == 0\n", name);
547 goto fail;
548 }
549
550 prz_ar = kcalloc(*cnt, sizeof(**przs), GFP_KERNEL);
551 if (!prz_ar)
552 goto fail;
553
554 for (i = 0; i < *cnt; i++) {
555 char *label;
556
557 if (*cnt == 1)
558 label = kasprintf(GFP_KERNEL, "ramoops:%s", name);
559 else
560 label = kasprintf(GFP_KERNEL, "ramoops:%s(%d/%d)",
561 name, i, *cnt - 1);
562 prz_ar[i] = persistent_ram_new(*paddr, zone_sz, sig,
563 &cxt->ecc_info,
564 cxt->memtype, flags, label);
565 kfree(label);
566 if (IS_ERR(prz_ar[i])) {
567 err = PTR_ERR(prz_ar[i]);
568 dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n",
569 name, record_size,
570 (unsigned long long)*paddr, err);
571
572 while (i > 0) {
573 i--;
574 persistent_ram_free(prz_ar[i]);
575 }
576 kfree(prz_ar);
577 goto fail;
578 }
579 *paddr += zone_sz;
580 prz_ar[i]->type = pstore_name_to_type(name);
581 }
582
583 *przs = prz_ar;
584 return 0;
585
586 fail:
587 *cnt = 0;
588 return err;
589 }
590
ramoops_init_prz(const char * name,struct device * dev,struct ramoops_context * cxt,struct persistent_ram_zone ** prz,phys_addr_t * paddr,size_t sz,u32 sig)591 static int ramoops_init_prz(const char *name,
592 struct device *dev, struct ramoops_context *cxt,
593 struct persistent_ram_zone **prz,
594 phys_addr_t *paddr, size_t sz, u32 sig)
595 {
596 char *label;
597
598 if (!sz)
599 return 0;
600
601 if (*paddr + sz - cxt->phys_addr > cxt->size) {
602 dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
603 name, sz, (unsigned long long)*paddr,
604 cxt->size, (unsigned long long)cxt->phys_addr);
605 return -ENOMEM;
606 }
607
608 label = kasprintf(GFP_KERNEL, "ramoops:%s", name);
609 *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info,
610 cxt->memtype, PRZ_FLAG_ZAP_OLD, label);
611 kfree(label);
612 if (IS_ERR(*prz)) {
613 int err = PTR_ERR(*prz);
614
615 dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n",
616 name, sz, (unsigned long long)*paddr, err);
617 return err;
618 }
619
620 *paddr += sz;
621 (*prz)->type = pstore_name_to_type(name);
622
623 return 0;
624 }
625
626 /* Read a u32 from a dt property and make sure it's safe for an int. */
ramoops_parse_dt_u32(struct platform_device * pdev,const char * propname,u32 default_value,u32 * value)627 static int ramoops_parse_dt_u32(struct platform_device *pdev,
628 const char *propname,
629 u32 default_value, u32 *value)
630 {
631 u32 val32 = 0;
632 int ret;
633
634 ret = of_property_read_u32(pdev->dev.of_node, propname, &val32);
635 if (ret == -EINVAL) {
636 /* field is missing, use default value. */
637 val32 = default_value;
638 } else if (ret < 0) {
639 dev_err(&pdev->dev, "failed to parse property %s: %d\n",
640 propname, ret);
641 return ret;
642 }
643
644 /* Sanity check our results. */
645 if (val32 > INT_MAX) {
646 dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32);
647 return -EOVERFLOW;
648 }
649
650 *value = val32;
651 return 0;
652 }
653
ramoops_parse_dt(struct platform_device * pdev,struct ramoops_platform_data * pdata)654 static int ramoops_parse_dt(struct platform_device *pdev,
655 struct ramoops_platform_data *pdata)
656 {
657 struct device_node *of_node = pdev->dev.of_node;
658 struct device_node *parent_node;
659 struct resource *res;
660 u32 value;
661 int ret;
662
663 dev_dbg(&pdev->dev, "using Device Tree\n");
664
665 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
666 if (!res) {
667 dev_err(&pdev->dev,
668 "failed to locate DT /reserved-memory resource\n");
669 return -EINVAL;
670 }
671
672 pdata->mem_size = resource_size(res);
673 pdata->mem_address = res->start;
674 pdata->mem_type = of_property_read_bool(of_node, "unbuffered");
675 /*
676 * Setting "no-dump-oops" is deprecated and will be ignored if
677 * "max_reason" is also specified.
678 */
679 if (of_property_read_bool(of_node, "no-dump-oops"))
680 pdata->max_reason = KMSG_DUMP_PANIC;
681 else
682 pdata->max_reason = KMSG_DUMP_OOPS;
683
684 #define parse_u32(name, field, default_value) { \
685 ret = ramoops_parse_dt_u32(pdev, name, default_value, \
686 &value); \
687 if (ret < 0) \
688 return ret; \
689 field = value; \
690 }
691
692 parse_u32("record-size", pdata->record_size, 0);
693 parse_u32("console-size", pdata->console_size, 0);
694 parse_u32("ftrace-size", pdata->ftrace_size, 0);
695 parse_u32("pmsg-size", pdata->pmsg_size, 0);
696 parse_u32("blackbox-size", pdata->blackbox_size, 0);
697 parse_u32("ecc-size", pdata->ecc_info.ecc_size, 0);
698 parse_u32("flags", pdata->flags, 0);
699 parse_u32("max-reason", pdata->max_reason, pdata->max_reason);
700
701 #undef parse_u32
702
703 /*
704 * Some old Chromebooks relied on the kernel setting the
705 * console_size and pmsg_size to the record size since that's
706 * what the downstream kernel did. These same Chromebooks had
707 * "ramoops" straight under the root node which isn't
708 * according to the current upstream bindings (though it was
709 * arguably acceptable under a prior version of the bindings).
710 * Let's make those old Chromebooks work by detecting that
711 * we're not a child of "reserved-memory" and mimicking the
712 * expected behavior.
713 */
714 parent_node = of_get_parent(of_node);
715 if (!of_node_name_eq(parent_node, "reserved-memory") &&
716 !pdata->console_size && !pdata->ftrace_size &&
717 !pdata->pmsg_size && !pdata->ecc_info.ecc_size &&
718 !pdata->blackbox_size) {
719 pdata->console_size = pdata->record_size;
720 pdata->pmsg_size = pdata->record_size;
721 pdata->blackbox_size = pdata->record_size;
722 }
723 of_node_put(parent_node);
724
725 return 0;
726 }
727
ramoops_probe(struct platform_device * pdev)728 static int ramoops_probe(struct platform_device *pdev)
729 {
730 struct device *dev = &pdev->dev;
731 struct ramoops_platform_data *pdata = dev->platform_data;
732 struct ramoops_platform_data pdata_local;
733 struct ramoops_context *cxt = &oops_cxt;
734 size_t dump_mem_sz;
735 phys_addr_t paddr;
736 int err = -EINVAL;
737
738 /*
739 * Only a single ramoops area allowed at a time, so fail extra
740 * probes.
741 */
742 if (cxt->max_dump_cnt) {
743 pr_err("already initialized\n");
744 goto fail_out;
745 }
746
747 if (dev_of_node(dev) && !pdata) {
748 pdata = &pdata_local;
749 memset(pdata, 0, sizeof(*pdata));
750
751 err = ramoops_parse_dt(pdev, pdata);
752 if (err < 0)
753 goto fail_out;
754 }
755
756 /* Make sure we didn't get bogus platform data pointer. */
757 if (!pdata) {
758 pr_err("NULL platform data\n");
759 goto fail_out;
760 }
761
762 if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size &&
763 !pdata->ftrace_size && !pdata->pmsg_size && !pdata->blackbox_size)) {
764 pr_err("The memory size and the record/console size must be "
765 "non-zero\n");
766 goto fail_out;
767 }
768
769 if (pdata->record_size && !is_power_of_2(pdata->record_size))
770 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
771 if (pdata->console_size && !is_power_of_2(pdata->console_size))
772 pdata->console_size = rounddown_pow_of_two(pdata->console_size);
773 if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size))
774 pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
775 if (pdata->pmsg_size && !is_power_of_2(pdata->pmsg_size))
776 pdata->pmsg_size = rounddown_pow_of_two(pdata->pmsg_size);
777 if (pdata->blackbox_size && !is_power_of_2(pdata->blackbox_size))
778 pdata->blackbox_size = rounddown_pow_of_two(pdata->blackbox_size);
779
780 cxt->size = pdata->mem_size;
781 cxt->phys_addr = pdata->mem_address;
782 cxt->memtype = pdata->mem_type;
783 cxt->record_size = pdata->record_size;
784 cxt->console_size = pdata->console_size;
785 cxt->ftrace_size = pdata->ftrace_size;
786 cxt->pmsg_size = pdata->pmsg_size;
787 cxt->blackbox_size = pdata->blackbox_size;
788 cxt->flags = pdata->flags;
789 cxt->ecc_info = pdata->ecc_info;
790
791 paddr = cxt->phys_addr;
792
793 dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size
794 - cxt->pmsg_size - cxt->blackbox_size;
795
796 err = ramoops_init_prz("blackbox", dev, cxt, &cxt->bprz, &paddr,
797 cxt->blackbox_size, 0);
798 if (err)
799 goto fail_init_bprz;
800 #if IS_ENABLED(CONFIG_PSTORE_BLACKBOX)
801 else
802 pstore_ready = true;
803 #endif
804
805 err = ramoops_init_przs("dmesg", dev, cxt, &cxt->dprzs, &paddr,
806 dump_mem_sz, cxt->record_size,
807 &cxt->max_dump_cnt, 0, 0);
808 if (err)
809 goto fail_out;
810
811 err = ramoops_init_prz("console", dev, cxt, &cxt->cprz, &paddr,
812 cxt->console_size, 0);
813 if (err)
814 goto fail_init_cprz;
815
816 cxt->max_ftrace_cnt = (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
817 ? nr_cpu_ids
818 : 1;
819 err = ramoops_init_przs("ftrace", dev, cxt, &cxt->fprzs, &paddr,
820 cxt->ftrace_size, -1,
821 &cxt->max_ftrace_cnt, LINUX_VERSION_CODE,
822 (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
823 ? PRZ_FLAG_NO_LOCK : 0);
824 if (err)
825 goto fail_init_fprz;
826
827 err = ramoops_init_prz("pmsg", dev, cxt, &cxt->mprz, &paddr,
828 cxt->pmsg_size, 0);
829 if (err)
830 goto fail_init_mprz;
831
832 cxt->pstore.data = cxt;
833 /*
834 * Prepare frontend flags based on which areas are initialized.
835 * For ramoops_init_przs() cases, the "max count" variable tells
836 * if there are regions present. For ramoops_init_prz() cases,
837 * the single region size is how to check.
838 */
839 cxt->pstore.flags = 0;
840 if (cxt->max_dump_cnt) {
841 cxt->pstore.flags |= PSTORE_FLAGS_DMESG;
842 cxt->pstore.max_reason = pdata->max_reason;
843 }
844 if (cxt->console_size)
845 cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
846 if (cxt->max_ftrace_cnt)
847 cxt->pstore.flags |= PSTORE_FLAGS_FTRACE;
848 if (cxt->pmsg_size)
849 cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
850 if (cxt->blackbox_size)
851 cxt->pstore.flags |= PSTORE_FLAGS_BLACKBOX;
852
853 /*
854 * Since bufsize is only used for dmesg crash dumps, it
855 * must match the size of the dprz record (after PRZ header
856 * and ECC bytes have been accounted for).
857 */
858 if (cxt->pstore.flags & PSTORE_FLAGS_DMESG) {
859 cxt->pstore.bufsize = cxt->dprzs[0]->buffer_size;
860 cxt->pstore.buf = kzalloc(cxt->pstore.bufsize, GFP_KERNEL);
861 if (!cxt->pstore.buf) {
862 pr_err("cannot allocate pstore crash dump buffer\n");
863 err = -ENOMEM;
864 goto fail_clear;
865 }
866 }
867
868 err = pstore_register(&cxt->pstore);
869 if (err) {
870 pr_err("registering with pstore failed\n");
871 goto fail_buf;
872 }
873
874 /*
875 * Update the module parameter variables as well so they are visible
876 * through /sys/module/ramoops/parameters/
877 */
878 mem_size = pdata->mem_size;
879 mem_address = pdata->mem_address;
880 record_size = pdata->record_size;
881 ramoops_max_reason = pdata->max_reason;
882 ramoops_console_size = pdata->console_size;
883 ramoops_pmsg_size = pdata->pmsg_size;
884 ramoops_ftrace_size = pdata->ftrace_size;
885 ramoops_blackbox_size = pdata->blackbox_size;
886
887 pr_info("using 0x%lx@0x%llx, ecc: %d\n",
888 cxt->size, (unsigned long long)cxt->phys_addr,
889 cxt->ecc_info.ecc_size);
890
891 return 0;
892
893 fail_buf:
894 kfree(cxt->pstore.buf);
895 fail_clear:
896 cxt->pstore.bufsize = 0;
897 persistent_ram_free(cxt->mprz);
898 fail_init_mprz:
899 fail_init_fprz:
900 persistent_ram_free(cxt->cprz);
901 fail_init_bprz:
902 persistent_ram_free(cxt->bprz);
903 fail_init_cprz:
904 ramoops_free_przs(cxt);
905 fail_out:
906 return err;
907 }
908
ramoops_remove(struct platform_device * pdev)909 static int ramoops_remove(struct platform_device *pdev)
910 {
911 struct ramoops_context *cxt = &oops_cxt;
912
913 pstore_unregister(&cxt->pstore);
914
915 kfree(cxt->pstore.buf);
916 cxt->pstore.bufsize = 0;
917
918 persistent_ram_free(cxt->mprz);
919 persistent_ram_free(cxt->cprz);
920 persistent_ram_free(cxt->bprz);
921 ramoops_free_przs(cxt);
922
923 return 0;
924 }
925
926 static const struct of_device_id dt_match[] = {
927 { .compatible = "ramoops" },
928 {}
929 };
930
931 static struct platform_driver ramoops_driver = {
932 .probe = ramoops_probe,
933 .remove = ramoops_remove,
934 .driver = {
935 .name = "ramoops",
936 .of_match_table = dt_match,
937 },
938 };
939
ramoops_unregister_dummy(void)940 static inline void ramoops_unregister_dummy(void)
941 {
942 platform_device_unregister(dummy);
943 dummy = NULL;
944 }
945
ramoops_register_dummy(void)946 static void __init ramoops_register_dummy(void)
947 {
948 struct ramoops_platform_data pdata;
949
950 /*
951 * Prepare a dummy platform data structure to carry the module
952 * parameters. If mem_size isn't set, then there are no module
953 * parameters, and we can skip this.
954 */
955 if (!mem_size)
956 return;
957
958 pr_info("using module parameters\n");
959
960 memset(&pdata, 0, sizeof(pdata));
961 pdata.mem_size = mem_size;
962 pdata.mem_address = mem_address;
963 pdata.mem_type = mem_type;
964 pdata.record_size = record_size;
965 pdata.console_size = ramoops_console_size;
966 pdata.ftrace_size = ramoops_ftrace_size;
967 pdata.pmsg_size = ramoops_pmsg_size;
968 pdata.blackbox_size = ramoops_blackbox_size;
969 /* If "max_reason" is set, its value has priority over "dump_oops". */
970 if (ramoops_max_reason >= 0)
971 pdata.max_reason = ramoops_max_reason;
972 /* Otherwise, if "dump_oops" is set, parse it into "max_reason". */
973 else if (ramoops_dump_oops != -1)
974 pdata.max_reason = ramoops_dump_oops ? KMSG_DUMP_OOPS
975 : KMSG_DUMP_PANIC;
976 /* And if neither are explicitly set, use the default. */
977 else
978 pdata.max_reason = KMSG_DUMP_OOPS;
979 pdata.flags = RAMOOPS_FLAG_FTRACE_PER_CPU;
980
981 /*
982 * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
983 * (using 1 byte for ECC isn't much of use anyway).
984 */
985 pdata.ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
986
987 dummy = platform_device_register_data(NULL, "ramoops", -1,
988 &pdata, sizeof(pdata));
989 if (IS_ERR(dummy)) {
990 pr_info("could not create platform device: %ld\n",
991 PTR_ERR(dummy));
992 dummy = NULL;
993 }
994 }
995
ramoops_init(void)996 static int __init ramoops_init(void)
997 {
998 int ret;
999
1000 ramoops_register_dummy();
1001 ret = platform_driver_register(&ramoops_driver);
1002 if (ret != 0)
1003 ramoops_unregister_dummy();
1004
1005 return ret;
1006 }
1007 postcore_initcall(ramoops_init);
1008
ramoops_exit(void)1009 static void __exit ramoops_exit(void)
1010 {
1011 platform_driver_unregister(&ramoops_driver);
1012 ramoops_unregister_dummy();
1013 }
1014 module_exit(ramoops_exit);
1015
1016 MODULE_LICENSE("GPL");
1017 MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
1018 MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");
1019