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