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