12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21894a253SAnton Vorontsov /*
31894a253SAnton Vorontsov * RAM Oops/Panic logger
41894a253SAnton Vorontsov *
51894a253SAnton Vorontsov * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
61894a253SAnton Vorontsov * Copyright (C) 2011 Kees Cook <keescook@chromium.org>
71894a253SAnton Vorontsov */
81894a253SAnton Vorontsov
91894a253SAnton Vorontsov #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
101894a253SAnton Vorontsov
111894a253SAnton Vorontsov #include <linux/kernel.h>
121894a253SAnton Vorontsov #include <linux/err.h>
131894a253SAnton Vorontsov #include <linux/module.h>
14cbe7cbf5SAnton Vorontsov #include <linux/version.h>
151894a253SAnton Vorontsov #include <linux/pstore.h>
161894a253SAnton Vorontsov #include <linux/io.h>
171894a253SAnton Vorontsov #include <linux/ioport.h>
181894a253SAnton Vorontsov #include <linux/platform_device.h>
191894a253SAnton Vorontsov #include <linux/slab.h>
2024203036SAnton Vorontsov #include <linux/compiler.h>
2135da6094SGreg Hackmann #include <linux/of.h>
2235da6094SGreg Hackmann #include <linux/of_address.h>
23104fd0b5SYuxiao Zhang #include <linux/mm.h>
248bd4da0fSKees Cook
2516a58307SKees Cook #include "internal.h"
268bd4da0fSKees Cook #include "ram_internal.h"
271894a253SAnton Vorontsov
281894a253SAnton Vorontsov #define RAMOOPS_KERNMSG_HDR "===="
291894a253SAnton Vorontsov #define MIN_MEM_SIZE 4096UL
301894a253SAnton Vorontsov
311894a253SAnton Vorontsov static ulong record_size = MIN_MEM_SIZE;
321894a253SAnton Vorontsov module_param(record_size, ulong, 0400);
331894a253SAnton Vorontsov MODULE_PARM_DESC(record_size,
341894a253SAnton Vorontsov "size of each dump done on oops/panic");
351894a253SAnton Vorontsov
36b5d38e9bSAnton Vorontsov static ulong ramoops_console_size = MIN_MEM_SIZE;
37b5d38e9bSAnton Vorontsov module_param_named(console_size, ramoops_console_size, ulong, 0400);
38b5d38e9bSAnton Vorontsov MODULE_PARM_DESC(console_size, "size of kernel console log");
39b5d38e9bSAnton Vorontsov
40a694d1b5SAnton Vorontsov static ulong ramoops_ftrace_size = MIN_MEM_SIZE;
41a694d1b5SAnton Vorontsov module_param_named(ftrace_size, ramoops_ftrace_size, ulong, 0400);
42a694d1b5SAnton Vorontsov MODULE_PARM_DESC(ftrace_size, "size of ftrace log");
43a694d1b5SAnton Vorontsov
449d5438f4SMark Salyzyn static ulong ramoops_pmsg_size = MIN_MEM_SIZE;
459d5438f4SMark Salyzyn module_param_named(pmsg_size, ramoops_pmsg_size, ulong, 0400);
469d5438f4SMark Salyzyn MODULE_PARM_DESC(pmsg_size, "size of user space message log");
479d5438f4SMark Salyzyn
48764fd639SWiebe, Wladislav (Nokia - DE/Ulm) static unsigned long long mem_address;
49b90fe0c4SDavid Howells module_param_hw(mem_address, ullong, other, 0400);
501894a253SAnton Vorontsov MODULE_PARM_DESC(mem_address,
511894a253SAnton Vorontsov "start of reserved RAM used to store oops/panic logs");
521894a253SAnton Vorontsov
531894a253SAnton Vorontsov static ulong mem_size;
541894a253SAnton Vorontsov module_param(mem_size, ulong, 0400);
551894a253SAnton Vorontsov MODULE_PARM_DESC(mem_size,
561894a253SAnton Vorontsov "size of reserved RAM used to store oops/panic logs");
571894a253SAnton Vorontsov
58027bc8b0STony Lindgren static unsigned int mem_type;
59f858b57fSKees Cook module_param(mem_type, uint, 0400);
60027bc8b0STony Lindgren MODULE_PARM_DESC(mem_type,
619d843e8fSMukesh Ojha "memory type: 0=write-combined (default), 1=unbuffered, 2=cached");
62027bc8b0STony Lindgren
63791205e3SKees Cook static int ramoops_max_reason = -1;
64791205e3SKees Cook module_param_named(max_reason, ramoops_max_reason, int, 0400);
65791205e3SKees Cook MODULE_PARM_DESC(max_reason,
66791205e3SKees Cook "maximum reason for kmsg dump (default 2: Oops and Panic) ");
671894a253SAnton Vorontsov
6839eb7e97SAnton Vorontsov static int ramoops_ecc;
69f858b57fSKees Cook module_param_named(ecc, ramoops_ecc, int, 0400);
7039eb7e97SAnton Vorontsov MODULE_PARM_DESC(ramoops_ecc,
715ca5d4e6SAnton Vorontsov "if non-zero, the option enables ECC support and specifies "
725ca5d4e6SAnton Vorontsov "ECC buffer size in bytes (1 is a special value, means 16 "
735ca5d4e6SAnton Vorontsov "bytes ECC)");
7439eb7e97SAnton Vorontsov
75791205e3SKees Cook static int ramoops_dump_oops = -1;
76791205e3SKees Cook module_param_named(dump_oops, ramoops_dump_oops, int, 0400);
77791205e3SKees Cook MODULE_PARM_DESC(dump_oops,
78791205e3SKees Cook "(deprecated: use max_reason instead) set to 1 to dump oopses & panics, 0 to only dump panics");
79791205e3SKees Cook
801894a253SAnton Vorontsov struct ramoops_context {
81a5d23b95SKees Cook struct persistent_ram_zone **dprzs; /* Oops dump zones */
82a5d23b95SKees Cook struct persistent_ram_zone *cprz; /* Console zone */
83a5d23b95SKees Cook struct persistent_ram_zone **fprzs; /* Ftrace zones */
84a5d23b95SKees Cook struct persistent_ram_zone *mprz; /* PMSG zone */
851894a253SAnton Vorontsov phys_addr_t phys_addr;
861894a253SAnton Vorontsov unsigned long size;
87027bc8b0STony Lindgren unsigned int memtype;
881894a253SAnton Vorontsov size_t record_size;
89b5d38e9bSAnton Vorontsov size_t console_size;
90a694d1b5SAnton Vorontsov size_t ftrace_size;
919d5438f4SMark Salyzyn size_t pmsg_size;
92a1cf53acSJoel Fernandes u32 flags;
93c31ad081SArve Hjønnevåg struct persistent_ram_ecc_info ecc_info;
94cac2eb7bSAnton Vorontsov unsigned int max_dump_cnt;
95cac2eb7bSAnton Vorontsov unsigned int dump_write_cnt;
9657fd8353SLiu ShuoX /* _read_cnt need clear on ramoops_pstore_open */
97cac2eb7bSAnton Vorontsov unsigned int dump_read_cnt;
98b5d38e9bSAnton Vorontsov unsigned int console_read_cnt;
99a1cf53acSJoel Fernandes unsigned int max_ftrace_cnt;
100a694d1b5SAnton Vorontsov unsigned int ftrace_read_cnt;
1019d5438f4SMark Salyzyn unsigned int pmsg_read_cnt;
1021894a253SAnton Vorontsov struct pstore_info pstore;
1031894a253SAnton Vorontsov };
1041894a253SAnton Vorontsov
1051894a253SAnton Vorontsov static struct platform_device *dummy;
1061894a253SAnton Vorontsov
ramoops_pstore_open(struct pstore_info * psi)1071894a253SAnton Vorontsov static int ramoops_pstore_open(struct pstore_info *psi)
1081894a253SAnton Vorontsov {
1091894a253SAnton Vorontsov struct ramoops_context *cxt = psi->data;
1101894a253SAnton Vorontsov
111cac2eb7bSAnton Vorontsov cxt->dump_read_cnt = 0;
112b5d38e9bSAnton Vorontsov cxt->console_read_cnt = 0;
11357fd8353SLiu ShuoX cxt->ftrace_read_cnt = 0;
1149d5438f4SMark Salyzyn cxt->pmsg_read_cnt = 0;
1151894a253SAnton Vorontsov return 0;
1161894a253SAnton Vorontsov }
1171894a253SAnton Vorontsov
118755d66b4SAnton Vorontsov static struct persistent_ram_zone *
ramoops_get_next_prz(struct persistent_ram_zone * przs[],int id,struct pstore_record * record)119b05c9506SJoel Fernandes (Google) ramoops_get_next_prz(struct persistent_ram_zone *przs[], int id,
120b05c9506SJoel Fernandes (Google) struct pstore_record *record)
121755d66b4SAnton Vorontsov {
122755d66b4SAnton Vorontsov struct persistent_ram_zone *prz;
123755d66b4SAnton Vorontsov
12446418413SKees Cook /* Give up if we never existed or have hit the end. */
125b05c9506SJoel Fernandes (Google) if (!przs)
126755d66b4SAnton Vorontsov return NULL;
127755d66b4SAnton Vorontsov
128b05c9506SJoel Fernandes (Google) prz = przs[id];
129b0aa931fSLiu ShuoX if (!prz)
130b0aa931fSLiu ShuoX return NULL;
131755d66b4SAnton Vorontsov
132755d66b4SAnton Vorontsov /* Update old/shadowed buffer. */
1336a4c9ab1SSai Prakash Ranjan if (prz->type == PSTORE_TYPE_DMESG)
134755d66b4SAnton Vorontsov persistent_ram_save_old(prz);
135aa9a4a1eSLiu ShuoX
136755d66b4SAnton Vorontsov if (!persistent_ram_old_size(prz))
137755d66b4SAnton Vorontsov return NULL;
138755d66b4SAnton Vorontsov
139b05c9506SJoel Fernandes (Google) record->type = prz->type;
140b05c9506SJoel Fernandes (Google) record->id = id;
141755d66b4SAnton Vorontsov
142755d66b4SAnton Vorontsov return prz;
143755d66b4SAnton Vorontsov }
144755d66b4SAnton Vorontsov
ramoops_read_kmsg_hdr(char * buffer,struct timespec64 * time,bool * compressed)1457aaa822eSKees Cook static int ramoops_read_kmsg_hdr(char *buffer, struct timespec64 *time,
1463f8f80f0SAruna Balakrishnaiah bool *compressed)
1473f8f80f0SAruna Balakrishnaiah {
1483f8f80f0SAruna Balakrishnaiah char data_type;
149a28726b4SBen Zhang int header_length = 0;
1503f8f80f0SAruna Balakrishnaiah
151e264abeaSArnd Bergmann if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lld.%lu-%c\n%n",
152e264abeaSArnd Bergmann (time64_t *)&time->tv_sec, &time->tv_nsec, &data_type,
1537aaa822eSKees Cook &header_length) == 3) {
15483b8a3fbSDeepa Dinamani time->tv_nsec *= 1000;
1553f8f80f0SAruna Balakrishnaiah if (data_type == 'C')
1563f8f80f0SAruna Balakrishnaiah *compressed = true;
1573f8f80f0SAruna Balakrishnaiah else
1583f8f80f0SAruna Balakrishnaiah *compressed = false;
159e264abeaSArnd Bergmann } else if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lld.%lu\n%n",
160e264abeaSArnd Bergmann (time64_t *)&time->tv_sec, &time->tv_nsec,
1617aaa822eSKees Cook &header_length) == 2) {
16283b8a3fbSDeepa Dinamani time->tv_nsec *= 1000;
1633f8f80f0SAruna Balakrishnaiah *compressed = false;
1643f8f80f0SAruna Balakrishnaiah } else {
1653f8f80f0SAruna Balakrishnaiah time->tv_sec = 0;
1663f8f80f0SAruna Balakrishnaiah time->tv_nsec = 0;
1673f8f80f0SAruna Balakrishnaiah *compressed = false;
1683f8f80f0SAruna Balakrishnaiah }
169a28726b4SBen Zhang return header_length;
1703f8f80f0SAruna Balakrishnaiah }
1713f8f80f0SAruna Balakrishnaiah
prz_ok(struct persistent_ram_zone * prz)172f44f9652SMark Salyzyn static bool prz_ok(struct persistent_ram_zone *prz)
173f44f9652SMark Salyzyn {
174f44f9652SMark Salyzyn return !!prz && !!(persistent_ram_old_size(prz) +
175f44f9652SMark Salyzyn persistent_ram_ecc_string(prz, NULL, 0));
176f44f9652SMark Salyzyn }
177f44f9652SMark Salyzyn
ramoops_pstore_read(struct pstore_record * record)178125cc42bSKees Cook static ssize_t ramoops_pstore_read(struct pstore_record *record)
1791894a253SAnton Vorontsov {
1802fbea82bSJoel Fernandes ssize_t size = 0;
181125cc42bSKees Cook struct ramoops_context *cxt = record->psi->data;
182e036bd33SBen Zhang struct persistent_ram_zone *prz = NULL;
183e036bd33SBen Zhang int header_length = 0;
1842fbea82bSJoel Fernandes bool free_prz = false;
1851894a253SAnton Vorontsov
186125cc42bSKees Cook /*
187125cc42bSKees Cook * Ramoops headers provide time stamps for PSTORE_TYPE_DMESG, but
188e036bd33SBen Zhang * PSTORE_TYPE_CONSOLE and PSTORE_TYPE_FTRACE don't currently have
189e036bd33SBen Zhang * valid time stamps, so it is initialized to zero.
190e036bd33SBen Zhang */
191125cc42bSKees Cook record->time.tv_sec = 0;
192125cc42bSKees Cook record->time.tv_nsec = 0;
193125cc42bSKees Cook record->compressed = false;
194e036bd33SBen Zhang
195e036bd33SBen Zhang /* Find the next valid persistent_ram_zone for DMESG */
196e036bd33SBen Zhang while (cxt->dump_read_cnt < cxt->max_dump_cnt && !prz) {
197b05c9506SJoel Fernandes (Google) prz = ramoops_get_next_prz(cxt->dprzs, cxt->dump_read_cnt++,
198b05c9506SJoel Fernandes (Google) record);
199f44f9652SMark Salyzyn if (!prz_ok(prz))
200e036bd33SBen Zhang continue;
201e036bd33SBen Zhang header_length = ramoops_read_kmsg_hdr(persistent_ram_old(prz),
202125cc42bSKees Cook &record->time,
203125cc42bSKees Cook &record->compressed);
204e036bd33SBen Zhang /* Clear and skip this DMESG record if it has no valid header */
205e036bd33SBen Zhang if (!header_length) {
206e036bd33SBen Zhang persistent_ram_free_old(prz);
207e036bd33SBen Zhang persistent_ram_zap(prz);
208e036bd33SBen Zhang prz = NULL;
209e036bd33SBen Zhang }
210e036bd33SBen Zhang }
211e036bd33SBen Zhang
212b05c9506SJoel Fernandes (Google) if (!prz_ok(prz) && !cxt->console_read_cnt++)
213b05c9506SJoel Fernandes (Google) prz = ramoops_get_next_prz(&cxt->cprz, 0 /* single */, record);
214a1cf53acSJoel Fernandes
215b05c9506SJoel Fernandes (Google) if (!prz_ok(prz) && !cxt->pmsg_read_cnt++)
216b05c9506SJoel Fernandes (Google) prz = ramoops_get_next_prz(&cxt->mprz, 0 /* single */, record);
2172fbea82bSJoel Fernandes
2182fbea82bSJoel Fernandes /* ftrace is last since it may want to dynamically allocate memory. */
21946418413SKees Cook if (!prz_ok(prz)) {
220b05c9506SJoel Fernandes (Google) if (!(cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU) &&
221b05c9506SJoel Fernandes (Google) !cxt->ftrace_read_cnt++) {
222b05c9506SJoel Fernandes (Google) prz = ramoops_get_next_prz(cxt->fprzs, 0 /* single */,
223b05c9506SJoel Fernandes (Google) record);
2242fbea82bSJoel Fernandes } else {
2252fbea82bSJoel Fernandes /*
2262fbea82bSJoel Fernandes * Build a new dummy record which combines all the
2272fbea82bSJoel Fernandes * per-cpu records including metadata and ecc info.
2282fbea82bSJoel Fernandes */
2292fbea82bSJoel Fernandes struct persistent_ram_zone *tmp_prz, *prz_next;
2302fbea82bSJoel Fernandes
2312fbea82bSJoel Fernandes tmp_prz = kzalloc(sizeof(struct persistent_ram_zone),
2322fbea82bSJoel Fernandes GFP_KERNEL);
2332fbea82bSJoel Fernandes if (!tmp_prz)
2342fbea82bSJoel Fernandes return -ENOMEM;
2358665569eSKees Cook prz = tmp_prz;
2362fbea82bSJoel Fernandes free_prz = true;
2372fbea82bSJoel Fernandes
2382fbea82bSJoel Fernandes while (cxt->ftrace_read_cnt < cxt->max_ftrace_cnt) {
2392fbea82bSJoel Fernandes prz_next = ramoops_get_next_prz(cxt->fprzs,
240b05c9506SJoel Fernandes (Google) cxt->ftrace_read_cnt++, record);
2412fbea82bSJoel Fernandes
2422fbea82bSJoel Fernandes if (!prz_ok(prz_next))
2432fbea82bSJoel Fernandes continue;
2442fbea82bSJoel Fernandes
2452fbea82bSJoel Fernandes tmp_prz->ecc_info = prz_next->ecc_info;
2462fbea82bSJoel Fernandes tmp_prz->corrected_bytes +=
2472fbea82bSJoel Fernandes prz_next->corrected_bytes;
2482fbea82bSJoel Fernandes tmp_prz->bad_blocks += prz_next->bad_blocks;
249df9bf19dSKees Cook
25016a58307SKees Cook size = pstore_ftrace_combine_log(
25116a58307SKees Cook &tmp_prz->old_log,
252df9bf19dSKees Cook &tmp_prz->old_log_size,
253df9bf19dSKees Cook prz_next->old_log,
254df9bf19dSKees Cook prz_next->old_log_size);
2552fbea82bSJoel Fernandes if (size)
2562fbea82bSJoel Fernandes goto out;
2572fbea82bSJoel Fernandes }
258125cc42bSKees Cook record->id = 0;
2592fbea82bSJoel Fernandes }
2602fbea82bSJoel Fernandes }
2612fbea82bSJoel Fernandes
2622fbea82bSJoel Fernandes if (!prz_ok(prz)) {
2632fbea82bSJoel Fernandes size = 0;
2642fbea82bSJoel Fernandes goto out;
2652fbea82bSJoel Fernandes }
266896fc1f0SAnton Vorontsov
267e036bd33SBen Zhang size = persistent_ram_old_size(prz) - header_length;
268bd08ec33SArve Hjønnevåg
269bd08ec33SArve Hjønnevåg /* ECC correction notice */
270125cc42bSKees Cook record->ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0);
271bd08ec33SArve Hjønnevåg
272104fd0b5SYuxiao Zhang record->buf = kvzalloc(size + record->ecc_notice_size + 1, GFP_KERNEL);
273125cc42bSKees Cook if (record->buf == NULL) {
2742fbea82bSJoel Fernandes size = -ENOMEM;
2752fbea82bSJoel Fernandes goto out;
2762fbea82bSJoel Fernandes }
2771894a253SAnton Vorontsov
278125cc42bSKees Cook memcpy(record->buf, (char *)persistent_ram_old(prz) + header_length,
279125cc42bSKees Cook size);
2802fbea82bSJoel Fernandes
281125cc42bSKees Cook persistent_ram_ecc_string(prz, record->buf + size,
282125cc42bSKees Cook record->ecc_notice_size + 1);
283bd08ec33SArve Hjønnevåg
2842fbea82bSJoel Fernandes out:
2852fbea82bSJoel Fernandes if (free_prz) {
286104fd0b5SYuxiao Zhang kvfree(prz->old_log);
2872fbea82bSJoel Fernandes kfree(prz);
2882fbea82bSJoel Fernandes }
2892fbea82bSJoel Fernandes
2908cfc8ddcSGeliang Tang return size;
2911894a253SAnton Vorontsov }
2921894a253SAnton Vorontsov
ramoops_write_kmsg_hdr(struct persistent_ram_zone * prz,struct pstore_record * record)2933f8f80f0SAruna Balakrishnaiah static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz,
294c7f3c595SKees Cook struct pstore_record *record)
295896fc1f0SAnton Vorontsov {
29693ee4b7dSKees Cook char hdr[36]; /* "===="(4), %lld(20), "."(1), %06lu(6), "-%c\n"(3) */
297896fc1f0SAnton Vorontsov size_t len;
298896fc1f0SAnton Vorontsov
29993ee4b7dSKees Cook len = scnprintf(hdr, sizeof(hdr),
30093ee4b7dSKees Cook RAMOOPS_KERNMSG_HDR "%lld.%06lu-%c\n",
301e264abeaSArnd Bergmann (time64_t)record->time.tv_sec,
302c7f3c595SKees Cook record->time.tv_nsec / 1000,
303c7f3c595SKees Cook record->compressed ? 'C' : 'D');
304896fc1f0SAnton Vorontsov persistent_ram_write(prz, hdr, len);
305896fc1f0SAnton Vorontsov
306896fc1f0SAnton Vorontsov return len;
307896fc1f0SAnton Vorontsov }
308896fc1f0SAnton Vorontsov
ramoops_pstore_write(struct pstore_record * record)3094c9ec219SKees Cook static int notrace ramoops_pstore_write(struct pstore_record *record)
3101894a253SAnton Vorontsov {
311b10b4711SKees Cook struct ramoops_context *cxt = record->psi->data;
312c6289378SArve Hjønnevåg struct persistent_ram_zone *prz;
313b10b4711SKees Cook size_t size, hlen;
3141894a253SAnton Vorontsov
315b10b4711SKees Cook if (record->type == PSTORE_TYPE_CONSOLE) {
316b5d38e9bSAnton Vorontsov if (!cxt->cprz)
317b5d38e9bSAnton Vorontsov return -ENOMEM;
318b10b4711SKees Cook persistent_ram_write(cxt->cprz, record->buf, record->size);
319b5d38e9bSAnton Vorontsov return 0;
320b10b4711SKees Cook } else if (record->type == PSTORE_TYPE_FTRACE) {
321a1cf53acSJoel Fernandes int zonenum;
322a1cf53acSJoel Fernandes
323a1cf53acSJoel Fernandes if (!cxt->fprzs)
324a694d1b5SAnton Vorontsov return -ENOMEM;
325a1cf53acSJoel Fernandes /*
326a1cf53acSJoel Fernandes * Choose zone by if we're using per-cpu buffers.
327a1cf53acSJoel Fernandes */
328a1cf53acSJoel Fernandes if (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
329a1cf53acSJoel Fernandes zonenum = smp_processor_id();
330a1cf53acSJoel Fernandes else
331a1cf53acSJoel Fernandes zonenum = 0;
332a1cf53acSJoel Fernandes
333b10b4711SKees Cook persistent_ram_write(cxt->fprzs[zonenum], record->buf,
334b10b4711SKees Cook record->size);
335a694d1b5SAnton Vorontsov return 0;
336b10b4711SKees Cook } else if (record->type == PSTORE_TYPE_PMSG) {
337d8991f51SJoel Fernandes pr_warn_ratelimited("PMSG shouldn't call %s\n", __func__);
338d8991f51SJoel Fernandes return -EINVAL;
339b5d38e9bSAnton Vorontsov }
340b5d38e9bSAnton Vorontsov
341b10b4711SKees Cook if (record->type != PSTORE_TYPE_DMESG)
3421894a253SAnton Vorontsov return -EINVAL;
3431894a253SAnton Vorontsov
344b10b4711SKees Cook /*
345791205e3SKees Cook * We could filter on record->reason here if we wanted to (which
346791205e3SKees Cook * would duplicate what happened before the "max_reason" setting
347791205e3SKees Cook * was added), but that would defeat the purpose of a system
348791205e3SKees Cook * changing printk.always_kmsg_dump, so instead log everything that
349791205e3SKees Cook * the kmsg dumper sends us, since it should be doing the filtering
350791205e3SKees Cook * based on the combination of printk.always_kmsg_dump and our
351791205e3SKees Cook * requested "max_reason".
3521894a253SAnton Vorontsov */
3531894a253SAnton Vorontsov
354b10b4711SKees Cook /*
355b10b4711SKees Cook * Explicitly only take the first part of any new crash.
3561894a253SAnton Vorontsov * If our buffer is larger than kmsg_bytes, this can never happen,
3571894a253SAnton Vorontsov * and if our buffer is smaller than kmsg_bytes, we don't want the
3581894a253SAnton Vorontsov * report split across multiple records.
3591894a253SAnton Vorontsov */
360b10b4711SKees Cook if (record->part != 1)
3611894a253SAnton Vorontsov return -ENOSPC;
3621894a253SAnton Vorontsov
363a5d23b95SKees Cook if (!cxt->dprzs)
364c6289378SArve Hjønnevåg return -ENOSPC;
365c6289378SArve Hjønnevåg
366a5d23b95SKees Cook prz = cxt->dprzs[cxt->dump_write_cnt];
367c6289378SArve Hjønnevåg
3689e5f1c19SAleksandr Yashkin /*
3699e5f1c19SAleksandr Yashkin * Since this is a new crash dump, we need to reset the buffer in
3709e5f1c19SAleksandr Yashkin * case it still has an old dump present. Without this, the new dump
3719e5f1c19SAleksandr Yashkin * will get appended, which would seriously confuse anything trying
3729e5f1c19SAleksandr Yashkin * to check dump file contents. Specifically, ramoops_read_kmsg_hdr()
3739e5f1c19SAleksandr Yashkin * expects to find a dump header in the beginning of buffer data, so
3749e5f1c19SAleksandr Yashkin * we must to reset the buffer values, in order to ensure that the
3759e5f1c19SAleksandr Yashkin * header will be written to the beginning of the buffer.
3769e5f1c19SAleksandr Yashkin */
3779e5f1c19SAleksandr Yashkin persistent_ram_zap(prz);
3789e5f1c19SAleksandr Yashkin
379b10b4711SKees Cook /* Build header and append record contents. */
380c7f3c595SKees Cook hlen = ramoops_write_kmsg_hdr(prz, record);
38147afd7aeSYue Hu if (!hlen)
38247afd7aeSYue Hu return -ENOMEM;
38347afd7aeSYue Hu
384b10b4711SKees Cook size = record->size;
385896fc1f0SAnton Vorontsov if (size + hlen > prz->buffer_size)
386896fc1f0SAnton Vorontsov size = prz->buffer_size - hlen;
387b10b4711SKees Cook persistent_ram_write(prz, record->buf, size);
3881894a253SAnton Vorontsov
389cac2eb7bSAnton Vorontsov cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
3901894a253SAnton Vorontsov
3911894a253SAnton Vorontsov return 0;
3921894a253SAnton Vorontsov }
3931894a253SAnton Vorontsov
ramoops_pstore_write_user(struct pstore_record * record,const char __user * buf)3944c9ec219SKees Cook static int notrace ramoops_pstore_write_user(struct pstore_record *record,
395fdd03118SKees Cook const char __user *buf)
3965bf6d1b9SMark Salyzyn {
397fdd03118SKees Cook if (record->type == PSTORE_TYPE_PMSG) {
398fdd03118SKees Cook struct ramoops_context *cxt = record->psi->data;
3995bf6d1b9SMark Salyzyn
4005bf6d1b9SMark Salyzyn if (!cxt->mprz)
4015bf6d1b9SMark Salyzyn return -ENOMEM;
402fdd03118SKees Cook return persistent_ram_write_user(cxt->mprz, buf, record->size);
4035bf6d1b9SMark Salyzyn }
4045bf6d1b9SMark Salyzyn
4055bf6d1b9SMark Salyzyn return -EINVAL;
4065bf6d1b9SMark Salyzyn }
4075bf6d1b9SMark Salyzyn
ramoops_pstore_erase(struct pstore_record * record)408a61072aaSKees Cook static int ramoops_pstore_erase(struct pstore_record *record)
4091894a253SAnton Vorontsov {
410a61072aaSKees Cook struct ramoops_context *cxt = record->psi->data;
411b5d38e9bSAnton Vorontsov struct persistent_ram_zone *prz;
4121894a253SAnton Vorontsov
413a61072aaSKees Cook switch (record->type) {
414b5d38e9bSAnton Vorontsov case PSTORE_TYPE_DMESG:
415a61072aaSKees Cook if (record->id >= cxt->max_dump_cnt)
4161894a253SAnton Vorontsov return -EINVAL;
417a61072aaSKees Cook prz = cxt->dprzs[record->id];
418b5d38e9bSAnton Vorontsov break;
419b5d38e9bSAnton Vorontsov case PSTORE_TYPE_CONSOLE:
420b5d38e9bSAnton Vorontsov prz = cxt->cprz;
421b5d38e9bSAnton Vorontsov break;
422a694d1b5SAnton Vorontsov case PSTORE_TYPE_FTRACE:
423a61072aaSKees Cook if (record->id >= cxt->max_ftrace_cnt)
424a1cf53acSJoel Fernandes return -EINVAL;
425a61072aaSKees Cook prz = cxt->fprzs[record->id];
426a694d1b5SAnton Vorontsov break;
4279d5438f4SMark Salyzyn case PSTORE_TYPE_PMSG:
4289d5438f4SMark Salyzyn prz = cxt->mprz;
4299d5438f4SMark Salyzyn break;
430b5d38e9bSAnton Vorontsov default:
431b5d38e9bSAnton Vorontsov return -EINVAL;
432b5d38e9bSAnton Vorontsov }
4331894a253SAnton Vorontsov
434b5d38e9bSAnton Vorontsov persistent_ram_free_old(prz);
435b5d38e9bSAnton Vorontsov persistent_ram_zap(prz);
4361894a253SAnton Vorontsov
4371894a253SAnton Vorontsov return 0;
4381894a253SAnton Vorontsov }
4391894a253SAnton Vorontsov
4401894a253SAnton Vorontsov static struct ramoops_context oops_cxt = {
4411894a253SAnton Vorontsov .pstore = {
4421894a253SAnton Vorontsov .owner = THIS_MODULE,
4431894a253SAnton Vorontsov .name = "ramoops",
4441894a253SAnton Vorontsov .open = ramoops_pstore_open,
4451894a253SAnton Vorontsov .read = ramoops_pstore_read,
4464c9ec219SKees Cook .write = ramoops_pstore_write,
4474c9ec219SKees Cook .write_user = ramoops_pstore_write_user,
4481894a253SAnton Vorontsov .erase = ramoops_pstore_erase,
4491894a253SAnton Vorontsov },
4501894a253SAnton Vorontsov };
4511894a253SAnton Vorontsov
ramoops_free_przs(struct ramoops_context * cxt)452f4c5d242SAnton Vorontsov static void ramoops_free_przs(struct ramoops_context *cxt)
453f4c5d242SAnton Vorontsov {
454f4c5d242SAnton Vorontsov int i;
455f4c5d242SAnton Vorontsov
4563219122bSKees Cook /* Free pmsg PRZ */
45706b4e09aSKees Cook persistent_ram_free(&cxt->mprz);
4583219122bSKees Cook
4593219122bSKees Cook /* Free console PRZ */
46006b4e09aSKees Cook persistent_ram_free(&cxt->cprz);
4613219122bSKees Cook
462a1cf53acSJoel Fernandes /* Free dump PRZs */
463a5d23b95SKees Cook if (cxt->dprzs) {
4644407de74SSebastian Andrzej Siewior for (i = 0; i < cxt->max_dump_cnt; i++)
46506b4e09aSKees Cook persistent_ram_free(&cxt->dprzs[i]);
4664407de74SSebastian Andrzej Siewior
467a5d23b95SKees Cook kfree(cxt->dprzs);
46806b4e09aSKees Cook cxt->dprzs = NULL;
4694407de74SSebastian Andrzej Siewior cxt->max_dump_cnt = 0;
470f4c5d242SAnton Vorontsov }
471f4c5d242SAnton Vorontsov
472a1cf53acSJoel Fernandes /* Free ftrace PRZs */
473a1cf53acSJoel Fernandes if (cxt->fprzs) {
474a1cf53acSJoel Fernandes for (i = 0; i < cxt->max_ftrace_cnt; i++)
47506b4e09aSKees Cook persistent_ram_free(&cxt->fprzs[i]);
476a1cf53acSJoel Fernandes kfree(cxt->fprzs);
47706b4e09aSKees Cook cxt->fprzs = NULL;
478a1cf53acSJoel Fernandes cxt->max_ftrace_cnt = 0;
479a1cf53acSJoel Fernandes }
480a1cf53acSJoel Fernandes }
481a1cf53acSJoel Fernandes
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)482c443a5f3SKees Cook static int ramoops_init_przs(const char *name,
483c443a5f3SKees Cook struct device *dev, struct ramoops_context *cxt,
484de832092SKees Cook struct persistent_ram_zone ***przs,
485de832092SKees Cook phys_addr_t *paddr, size_t mem_sz,
486de832092SKees Cook ssize_t record_size,
487de832092SKees Cook unsigned int *cnt, u32 sig, u32 flags)
488f4c5d242SAnton Vorontsov {
489f4c5d242SAnton Vorontsov int err = -ENOMEM;
490f4c5d242SAnton Vorontsov int i;
491de832092SKees Cook size_t zone_sz;
492de832092SKees Cook struct persistent_ram_zone **prz_ar;
493f4c5d242SAnton Vorontsov
494de832092SKees Cook /* Allocate nothing for 0 mem_sz or 0 record_size. */
495de832092SKees Cook if (mem_sz == 0 || record_size == 0) {
496de832092SKees Cook *cnt = 0;
497f4c5d242SAnton Vorontsov return 0;
498c6289378SArve Hjønnevåg }
499c6289378SArve Hjønnevåg
500de832092SKees Cook /*
501de832092SKees Cook * If we have a negative record size, calculate it based on
502de832092SKees Cook * mem_sz / *cnt. If we have a positive record size, calculate
503de832092SKees Cook * cnt from mem_sz / record_size.
504de832092SKees Cook */
505de832092SKees Cook if (record_size < 0) {
506de832092SKees Cook if (*cnt == 0)
507de832092SKees Cook return 0;
508de832092SKees Cook record_size = mem_sz / *cnt;
509c443a5f3SKees Cook if (record_size == 0) {
510c443a5f3SKees Cook dev_err(dev, "%s record size == 0 (%zu / %u)\n",
511c443a5f3SKees Cook name, mem_sz, *cnt);
512de832092SKees Cook goto fail;
513c443a5f3SKees Cook }
514de832092SKees Cook } else {
515de832092SKees Cook *cnt = mem_sz / record_size;
516c443a5f3SKees Cook if (*cnt == 0) {
517c443a5f3SKees Cook dev_err(dev, "%s record count == 0 (%zu / %zu)\n",
518c443a5f3SKees Cook name, mem_sz, record_size);
519de832092SKees Cook goto fail;
520f4c5d242SAnton Vorontsov }
521c443a5f3SKees Cook }
522f4c5d242SAnton Vorontsov
523de832092SKees Cook if (*paddr + mem_sz - cxt->phys_addr > cxt->size) {
524c443a5f3SKees Cook dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
525c443a5f3SKees Cook name,
526de832092SKees Cook mem_sz, (unsigned long long)*paddr,
527de832092SKees Cook cxt->size, (unsigned long long)cxt->phys_addr);
528de832092SKees Cook goto fail;
529de832092SKees Cook }
530de832092SKees Cook
531de832092SKees Cook zone_sz = mem_sz / *cnt;
532*0593cfd3SWeichen Chen zone_sz = ALIGN_DOWN(zone_sz, 2);
533c443a5f3SKees Cook if (!zone_sz) {
534c443a5f3SKees Cook dev_err(dev, "%s zone size == 0\n", name);
535de832092SKees Cook goto fail;
536c443a5f3SKees Cook }
537de832092SKees Cook
538de832092SKees Cook prz_ar = kcalloc(*cnt, sizeof(**przs), GFP_KERNEL);
539de832092SKees Cook if (!prz_ar)
540de832092SKees Cook goto fail;
541de832092SKees Cook
542de832092SKees Cook for (i = 0; i < *cnt; i++) {
5431227daa4SKees Cook char *label;
5441227daa4SKees Cook
5451227daa4SKees Cook if (*cnt == 1)
5461227daa4SKees Cook label = kasprintf(GFP_KERNEL, "ramoops:%s", name);
5471227daa4SKees Cook else
5481227daa4SKees Cook label = kasprintf(GFP_KERNEL, "ramoops:%s(%d/%d)",
5491227daa4SKees Cook name, i, *cnt - 1);
550de832092SKees Cook prz_ar[i] = persistent_ram_new(*paddr, zone_sz, sig,
551027bc8b0STony Lindgren &cxt->ecc_info,
5521227daa4SKees Cook cxt->memtype, flags, label);
553e163fdb3SKees Cook kfree(label);
554de832092SKees Cook if (IS_ERR(prz_ar[i])) {
555de832092SKees Cook err = PTR_ERR(prz_ar[i]);
556c443a5f3SKees Cook dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n",
557c443a5f3SKees Cook name, record_size,
558c443a5f3SKees Cook (unsigned long long)*paddr, err);
5594407de74SSebastian Andrzej Siewior
5604407de74SSebastian Andrzej Siewior while (i > 0) {
5614407de74SSebastian Andrzej Siewior i--;
56206b4e09aSKees Cook persistent_ram_free(&prz_ar[i]);
5634407de74SSebastian Andrzej Siewior }
564de832092SKees Cook kfree(prz_ar);
56506b4e09aSKees Cook prz_ar = NULL;
566de832092SKees Cook goto fail;
567f4c5d242SAnton Vorontsov }
568de832092SKees Cook *paddr += zone_sz;
569f0f23e54SJoel Fernandes (Google) prz_ar[i]->type = pstore_name_to_type(name);
570f4c5d242SAnton Vorontsov }
571f4c5d242SAnton Vorontsov
572de832092SKees Cook *przs = prz_ar;
573f4c5d242SAnton Vorontsov return 0;
574de832092SKees Cook
575de832092SKees Cook fail:
576de832092SKees Cook *cnt = 0;
577f4c5d242SAnton Vorontsov return err;
578f4c5d242SAnton Vorontsov }
579f4c5d242SAnton Vorontsov
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)580c443a5f3SKees Cook static int ramoops_init_prz(const char *name,
581c443a5f3SKees Cook struct device *dev, struct ramoops_context *cxt,
582b5d38e9bSAnton Vorontsov struct persistent_ram_zone **prz,
583cbe7cbf5SAnton Vorontsov phys_addr_t *paddr, size_t sz, u32 sig)
584b5d38e9bSAnton Vorontsov {
5851227daa4SKees Cook char *label;
5861227daa4SKees Cook
587b5d38e9bSAnton Vorontsov if (!sz)
588b5d38e9bSAnton Vorontsov return 0;
589b5d38e9bSAnton Vorontsov
590c6289378SArve Hjønnevåg if (*paddr + sz - cxt->phys_addr > cxt->size) {
591c443a5f3SKees Cook dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
592c443a5f3SKees Cook name, sz, (unsigned long long)*paddr,
593c6289378SArve Hjønnevåg cxt->size, (unsigned long long)cxt->phys_addr);
594b5d38e9bSAnton Vorontsov return -ENOMEM;
595c6289378SArve Hjønnevåg }
596b5d38e9bSAnton Vorontsov
5971227daa4SKees Cook label = kasprintf(GFP_KERNEL, "ramoops:%s", name);
598663deb47SJoel Fernandes *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info,
5997684bd33SPeng Wang cxt->memtype, PRZ_FLAG_ZAP_OLD, label);
600e163fdb3SKees Cook kfree(label);
601b5d38e9bSAnton Vorontsov if (IS_ERR(*prz)) {
602b5d38e9bSAnton Vorontsov int err = PTR_ERR(*prz);
603b5d38e9bSAnton Vorontsov
604c443a5f3SKees Cook dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n",
605c443a5f3SKees Cook name, sz, (unsigned long long)*paddr, err);
606b5d38e9bSAnton Vorontsov return err;
607b5d38e9bSAnton Vorontsov }
608b5d38e9bSAnton Vorontsov
609b5d38e9bSAnton Vorontsov *paddr += sz;
610f0f23e54SJoel Fernandes (Google) (*prz)->type = pstore_name_to_type(name);
611b5d38e9bSAnton Vorontsov
612b5d38e9bSAnton Vorontsov return 0;
613b5d38e9bSAnton Vorontsov }
614b5d38e9bSAnton Vorontsov
61526961d76SKees Cook /* 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)61626961d76SKees Cook static int ramoops_parse_dt_u32(struct platform_device *pdev,
61726961d76SKees Cook const char *propname,
61826961d76SKees Cook u32 default_value, u32 *value)
61935da6094SGreg Hackmann {
62035da6094SGreg Hackmann u32 val32 = 0;
62135da6094SGreg Hackmann int ret;
62235da6094SGreg Hackmann
62335da6094SGreg Hackmann ret = of_property_read_u32(pdev->dev.of_node, propname, &val32);
62426961d76SKees Cook if (ret == -EINVAL) {
62526961d76SKees Cook /* field is missing, use default value. */
62626961d76SKees Cook val32 = default_value;
62726961d76SKees Cook } else if (ret < 0) {
62835da6094SGreg Hackmann dev_err(&pdev->dev, "failed to parse property %s: %d\n",
62935da6094SGreg Hackmann propname, ret);
63035da6094SGreg Hackmann return ret;
63135da6094SGreg Hackmann }
63235da6094SGreg Hackmann
63326961d76SKees Cook /* Sanity check our results. */
63435da6094SGreg Hackmann if (val32 > INT_MAX) {
63535da6094SGreg Hackmann dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32);
63635da6094SGreg Hackmann return -EOVERFLOW;
63735da6094SGreg Hackmann }
63835da6094SGreg Hackmann
63935da6094SGreg Hackmann *value = val32;
64035da6094SGreg Hackmann return 0;
64135da6094SGreg Hackmann }
64235da6094SGreg Hackmann
ramoops_parse_dt(struct platform_device * pdev,struct ramoops_platform_data * pdata)64335da6094SGreg Hackmann static int ramoops_parse_dt(struct platform_device *pdev,
64435da6094SGreg Hackmann struct ramoops_platform_data *pdata)
64535da6094SGreg Hackmann {
64635da6094SGreg Hackmann struct device_node *of_node = pdev->dev.of_node;
6471614e921SDouglas Anderson struct device_node *parent_node;
648529182e2SKees Cook struct resource *res;
64935da6094SGreg Hackmann u32 value;
65035da6094SGreg Hackmann int ret;
65135da6094SGreg Hackmann
65235da6094SGreg Hackmann dev_dbg(&pdev->dev, "using Device Tree\n");
65335da6094SGreg Hackmann
654529182e2SKees Cook res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
655529182e2SKees Cook if (!res) {
65635da6094SGreg Hackmann dev_err(&pdev->dev,
657529182e2SKees Cook "failed to locate DT /reserved-memory resource\n");
658529182e2SKees Cook return -EINVAL;
65935da6094SGreg Hackmann }
66035da6094SGreg Hackmann
661529182e2SKees Cook pdata->mem_size = resource_size(res);
662529182e2SKees Cook pdata->mem_address = res->start;
6639d843e8fSMukesh Ojha /*
6649d843e8fSMukesh Ojha * Setting "unbuffered" is deprecated and will be ignored if
6659d843e8fSMukesh Ojha * "mem_type" is also specified.
6669d843e8fSMukesh Ojha */
66735da6094SGreg Hackmann pdata->mem_type = of_property_read_bool(of_node, "unbuffered");
668791205e3SKees Cook /*
669791205e3SKees Cook * Setting "no-dump-oops" is deprecated and will be ignored if
670791205e3SKees Cook * "max_reason" is also specified.
671791205e3SKees Cook */
672791205e3SKees Cook if (of_property_read_bool(of_node, "no-dump-oops"))
673791205e3SKees Cook pdata->max_reason = KMSG_DUMP_PANIC;
674791205e3SKees Cook else
675791205e3SKees Cook pdata->max_reason = KMSG_DUMP_OOPS;
67635da6094SGreg Hackmann
67726961d76SKees Cook #define parse_u32(name, field, default_value) { \
67826961d76SKees Cook ret = ramoops_parse_dt_u32(pdev, name, default_value, \
67926961d76SKees Cook &value); \
68035da6094SGreg Hackmann if (ret < 0) \
68135da6094SGreg Hackmann return ret; \
68235da6094SGreg Hackmann field = value; \
68335da6094SGreg Hackmann }
68435da6094SGreg Hackmann
685beca3e31SLuca Stefani parse_u32("mem-type", pdata->mem_type, pdata->mem_type);
68626961d76SKees Cook parse_u32("record-size", pdata->record_size, 0);
68726961d76SKees Cook parse_u32("console-size", pdata->console_size, 0);
68826961d76SKees Cook parse_u32("ftrace-size", pdata->ftrace_size, 0);
68926961d76SKees Cook parse_u32("pmsg-size", pdata->pmsg_size, 0);
69026961d76SKees Cook parse_u32("ecc-size", pdata->ecc_info.ecc_size, 0);
69126961d76SKees Cook parse_u32("flags", pdata->flags, 0);
692791205e3SKees Cook parse_u32("max-reason", pdata->max_reason, pdata->max_reason);
69335da6094SGreg Hackmann
69426961d76SKees Cook #undef parse_u32
69535da6094SGreg Hackmann
6961614e921SDouglas Anderson /*
6971614e921SDouglas Anderson * Some old Chromebooks relied on the kernel setting the
6981614e921SDouglas Anderson * console_size and pmsg_size to the record size since that's
6991614e921SDouglas Anderson * what the downstream kernel did. These same Chromebooks had
7001614e921SDouglas Anderson * "ramoops" straight under the root node which isn't
7011614e921SDouglas Anderson * according to the current upstream bindings (though it was
7021614e921SDouglas Anderson * arguably acceptable under a prior version of the bindings).
7031614e921SDouglas Anderson * Let's make those old Chromebooks work by detecting that
7041614e921SDouglas Anderson * we're not a child of "reserved-memory" and mimicking the
7051614e921SDouglas Anderson * expected behavior.
7061614e921SDouglas Anderson */
7071614e921SDouglas Anderson parent_node = of_get_parent(of_node);
7081614e921SDouglas Anderson if (!of_node_name_eq(parent_node, "reserved-memory") &&
7091614e921SDouglas Anderson !pdata->console_size && !pdata->ftrace_size &&
7101614e921SDouglas Anderson !pdata->pmsg_size && !pdata->ecc_info.ecc_size) {
7111614e921SDouglas Anderson pdata->console_size = pdata->record_size;
7121614e921SDouglas Anderson pdata->pmsg_size = pdata->record_size;
7131614e921SDouglas Anderson }
7141614e921SDouglas Anderson of_node_put(parent_node);
7151614e921SDouglas Anderson
71635da6094SGreg Hackmann return 0;
71735da6094SGreg Hackmann }
71835da6094SGreg Hackmann
ramoops_probe(struct platform_device * pdev)719f568f6caSGreg Kroah-Hartman static int ramoops_probe(struct platform_device *pdev)
7201894a253SAnton Vorontsov {
721896fc1f0SAnton Vorontsov struct device *dev = &pdev->dev;
72235da6094SGreg Hackmann struct ramoops_platform_data *pdata = dev->platform_data;
7235631e857SKees Cook struct ramoops_platform_data pdata_local;
7241894a253SAnton Vorontsov struct ramoops_context *cxt = &oops_cxt;
725f4c5d242SAnton Vorontsov size_t dump_mem_sz;
726f4c5d242SAnton Vorontsov phys_addr_t paddr;
7271894a253SAnton Vorontsov int err = -EINVAL;
7281894a253SAnton Vorontsov
729fc46d4e4SKees Cook /*
730fc46d4e4SKees Cook * Only a single ramoops area allowed at a time, so fail extra
7311894a253SAnton Vorontsov * probes.
7321894a253SAnton Vorontsov */
733fc46d4e4SKees Cook if (cxt->max_dump_cnt) {
734fc46d4e4SKees Cook pr_err("already initialized\n");
7351894a253SAnton Vorontsov goto fail_out;
736fc46d4e4SKees Cook }
737fc46d4e4SKees Cook
7381e0f67a9SYue Hu if (dev_of_node(dev) && !pdata) {
7391e0f67a9SYue Hu pdata = &pdata_local;
7401e0f67a9SYue Hu memset(pdata, 0, sizeof(*pdata));
7411e0f67a9SYue Hu
7421e0f67a9SYue Hu err = ramoops_parse_dt(pdev, pdata);
7431e0f67a9SYue Hu if (err < 0)
7441e0f67a9SYue Hu goto fail_out;
7451e0f67a9SYue Hu }
7461e0f67a9SYue Hu
747fc46d4e4SKees Cook /* Make sure we didn't get bogus platform data pointer. */
748fc46d4e4SKees Cook if (!pdata) {
749fc46d4e4SKees Cook pr_err("NULL platform data\n");
750e1fce564SWang Yufen err = -EINVAL;
751fc46d4e4SKees Cook goto fail_out;
752fc46d4e4SKees Cook }
7531894a253SAnton Vorontsov
754a694d1b5SAnton Vorontsov if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size &&
7559d5438f4SMark Salyzyn !pdata->ftrace_size && !pdata->pmsg_size)) {
756b5d38e9bSAnton Vorontsov pr_err("The memory size and the record/console size must be "
7571894a253SAnton Vorontsov "non-zero\n");
758e1fce564SWang Yufen err = -EINVAL;
7591894a253SAnton Vorontsov goto fail_out;
7601894a253SAnton Vorontsov }
7611894a253SAnton Vorontsov
7623bd11cf5SMaxime Bizon if (pdata->record_size && !is_power_of_2(pdata->record_size))
7631894a253SAnton Vorontsov pdata->record_size = rounddown_pow_of_two(pdata->record_size);
7643bd11cf5SMaxime Bizon if (pdata->console_size && !is_power_of_2(pdata->console_size))
765b5d38e9bSAnton Vorontsov pdata->console_size = rounddown_pow_of_two(pdata->console_size);
7663bd11cf5SMaxime Bizon if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size))
767a694d1b5SAnton Vorontsov pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
7689d5438f4SMark Salyzyn if (pdata->pmsg_size && !is_power_of_2(pdata->pmsg_size))
7699d5438f4SMark Salyzyn pdata->pmsg_size = rounddown_pow_of_two(pdata->pmsg_size);
7701894a253SAnton Vorontsov
7711894a253SAnton Vorontsov cxt->size = pdata->mem_size;
7721894a253SAnton Vorontsov cxt->phys_addr = pdata->mem_address;
773027bc8b0STony Lindgren cxt->memtype = pdata->mem_type;
7741894a253SAnton Vorontsov cxt->record_size = pdata->record_size;
775b5d38e9bSAnton Vorontsov cxt->console_size = pdata->console_size;
776a694d1b5SAnton Vorontsov cxt->ftrace_size = pdata->ftrace_size;
7779d5438f4SMark Salyzyn cxt->pmsg_size = pdata->pmsg_size;
778a1cf53acSJoel Fernandes cxt->flags = pdata->flags;
779c31ad081SArve Hjønnevåg cxt->ecc_info = pdata->ecc_info;
7801894a253SAnton Vorontsov
781f4c5d242SAnton Vorontsov paddr = cxt->phys_addr;
782896fc1f0SAnton Vorontsov
7839d5438f4SMark Salyzyn dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size
7849d5438f4SMark Salyzyn - cxt->pmsg_size;
785f0f23e54SJoel Fernandes (Google) err = ramoops_init_przs("dmesg", dev, cxt, &cxt->dprzs, &paddr,
786c443a5f3SKees Cook dump_mem_sz, cxt->record_size,
787c443a5f3SKees Cook &cxt->max_dump_cnt, 0, 0);
788b5d38e9bSAnton Vorontsov if (err)
7893219122bSKees Cook goto fail_init;
790b5d38e9bSAnton Vorontsov
791c443a5f3SKees Cook err = ramoops_init_prz("console", dev, cxt, &cxt->cprz, &paddr,
792cbe7cbf5SAnton Vorontsov cxt->console_size, 0);
793b5d38e9bSAnton Vorontsov if (err)
7943219122bSKees Cook goto fail_init;
795b5d38e9bSAnton Vorontsov
7966daf4e82SKees Cook err = ramoops_init_prz("pmsg", dev, cxt, &cxt->mprz, &paddr,
7976daf4e82SKees Cook cxt->pmsg_size, 0);
7986daf4e82SKees Cook if (err)
7996daf4e82SKees Cook goto fail_init;
8006daf4e82SKees Cook
801a1cf53acSJoel Fernandes cxt->max_ftrace_cnt = (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
802a1cf53acSJoel Fernandes ? nr_cpu_ids
803a1cf53acSJoel Fernandes : 1;
804c443a5f3SKees Cook err = ramoops_init_przs("ftrace", dev, cxt, &cxt->fprzs, &paddr,
805c443a5f3SKees Cook cxt->ftrace_size, -1,
806c443a5f3SKees Cook &cxt->max_ftrace_cnt, LINUX_VERSION_CODE,
807a1cf53acSJoel Fernandes (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
808a1cf53acSJoel Fernandes ? PRZ_FLAG_NO_LOCK : 0);
809a694d1b5SAnton Vorontsov if (err)
8103219122bSKees Cook goto fail_init;
811a694d1b5SAnton Vorontsov
8121894a253SAnton Vorontsov cxt->pstore.data = cxt;
813b5d38e9bSAnton Vorontsov /*
8148880fa32SKees Cook * Prepare frontend flags based on which areas are initialized.
8158880fa32SKees Cook * For ramoops_init_przs() cases, the "max count" variable tells
8168880fa32SKees Cook * if there are regions present. For ramoops_init_prz() cases,
8178880fa32SKees Cook * the single region size is how to check.
8188880fa32SKees Cook */
8198880fa32SKees Cook cxt->pstore.flags = 0;
820791205e3SKees Cook if (cxt->max_dump_cnt) {
8218880fa32SKees Cook cxt->pstore.flags |= PSTORE_FLAGS_DMESG;
822791205e3SKees Cook cxt->pstore.max_reason = pdata->max_reason;
823791205e3SKees Cook }
8248880fa32SKees Cook if (cxt->console_size)
8258880fa32SKees Cook cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
8268880fa32SKees Cook if (cxt->max_ftrace_cnt)
8278880fa32SKees Cook cxt->pstore.flags |= PSTORE_FLAGS_FTRACE;
8288880fa32SKees Cook if (cxt->pmsg_size)
8298880fa32SKees Cook cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
8308880fa32SKees Cook
8318880fa32SKees Cook /*
83289d328f6SKees Cook * Since bufsize is only used for dmesg crash dumps, it
83389d328f6SKees Cook * must match the size of the dprz record (after PRZ header
83489d328f6SKees Cook * and ECC bytes have been accounted for).
835b5d38e9bSAnton Vorontsov */
8368880fa32SKees Cook if (cxt->pstore.flags & PSTORE_FLAGS_DMESG) {
83789d328f6SKees Cook cxt->pstore.bufsize = cxt->dprzs[0]->buffer_size;
838104fd0b5SYuxiao Zhang cxt->pstore.buf = kvzalloc(cxt->pstore.bufsize, GFP_KERNEL);
8391894a253SAnton Vorontsov if (!cxt->pstore.buf) {
84089d328f6SKees Cook pr_err("cannot allocate pstore crash dump buffer\n");
84147110b88SWei Yongjun err = -ENOMEM;
8421894a253SAnton Vorontsov goto fail_clear;
8431894a253SAnton Vorontsov }
8448880fa32SKees Cook }
845c950fd6fSNamhyung Kim
8461894a253SAnton Vorontsov err = pstore_register(&cxt->pstore);
8471894a253SAnton Vorontsov if (err) {
8481894a253SAnton Vorontsov pr_err("registering with pstore failed\n");
849896fc1f0SAnton Vorontsov goto fail_buf;
8501894a253SAnton Vorontsov }
8511894a253SAnton Vorontsov
8521894a253SAnton Vorontsov /*
8531894a253SAnton Vorontsov * Update the module parameter variables as well so they are visible
8541894a253SAnton Vorontsov * through /sys/module/ramoops/parameters/
8551894a253SAnton Vorontsov */
8561894a253SAnton Vorontsov mem_size = pdata->mem_size;
8571894a253SAnton Vorontsov mem_address = pdata->mem_address;
8581894a253SAnton Vorontsov record_size = pdata->record_size;
859791205e3SKees Cook ramoops_max_reason = pdata->max_reason;
86062f269efSWang Long ramoops_console_size = pdata->console_size;
86162f269efSWang Long ramoops_pmsg_size = pdata->pmsg_size;
86262f269efSWang Long ramoops_ftrace_size = pdata->ftrace_size;
8631894a253SAnton Vorontsov
864dc80b1eaSKees Cook pr_info("using 0x%lx@0x%llx, ecc: %d\n",
8651894a253SAnton Vorontsov cxt->size, (unsigned long long)cxt->phys_addr,
866dc80b1eaSKees Cook cxt->ecc_info.ecc_size);
8671894a253SAnton Vorontsov
8681894a253SAnton Vorontsov return 0;
8691894a253SAnton Vorontsov
8701894a253SAnton Vorontsov fail_buf:
871104fd0b5SYuxiao Zhang kvfree(cxt->pstore.buf);
8721894a253SAnton Vorontsov fail_clear:
8731894a253SAnton Vorontsov cxt->pstore.bufsize = 0;
8743219122bSKees Cook fail_init:
875f4c5d242SAnton Vorontsov ramoops_free_przs(cxt);
8761894a253SAnton Vorontsov fail_out:
8771894a253SAnton Vorontsov return err;
8781894a253SAnton Vorontsov }
8791894a253SAnton Vorontsov
ramoops_remove(struct platform_device * pdev)88048f2c681SUwe Kleine-König static void ramoops_remove(struct platform_device *pdev)
8811894a253SAnton Vorontsov {
8821894a253SAnton Vorontsov struct ramoops_context *cxt = &oops_cxt;
8831894a253SAnton Vorontsov
884ee1d2674SGeliang Tang pstore_unregister(&cxt->pstore);
8851894a253SAnton Vorontsov
886104fd0b5SYuxiao Zhang kvfree(cxt->pstore.buf);
8871894a253SAnton Vorontsov cxt->pstore.bufsize = 0;
8881894a253SAnton Vorontsov
889ee1d2674SGeliang Tang ramoops_free_przs(cxt);
8901894a253SAnton Vorontsov }
8911894a253SAnton Vorontsov
89235da6094SGreg Hackmann static const struct of_device_id dt_match[] = {
89335da6094SGreg Hackmann { .compatible = "ramoops" },
89435da6094SGreg Hackmann {}
89535da6094SGreg Hackmann };
89635da6094SGreg Hackmann
8971894a253SAnton Vorontsov static struct platform_driver ramoops_driver = {
898924d3711SAnton Vorontsov .probe = ramoops_probe,
89948f2c681SUwe Kleine-König .remove_new = ramoops_remove,
9001894a253SAnton Vorontsov .driver = {
9011894a253SAnton Vorontsov .name = "ramoops",
90235da6094SGreg Hackmann .of_match_table = dt_match,
9031894a253SAnton Vorontsov },
9041894a253SAnton Vorontsov };
9051894a253SAnton Vorontsov
ramoops_unregister_dummy(void)906bac6f6cdSKees Cook static inline void ramoops_unregister_dummy(void)
9071894a253SAnton Vorontsov {
908bac6f6cdSKees Cook platform_device_unregister(dummy);
909bac6f6cdSKees Cook dummy = NULL;
910bac6f6cdSKees Cook }
911bac6f6cdSKees Cook
ramoops_register_dummy(void)912bac6f6cdSKees Cook static void __init ramoops_register_dummy(void)
913bac6f6cdSKees Cook {
914182ca6e0SYue Hu struct ramoops_platform_data pdata;
915182ca6e0SYue Hu
916bac6f6cdSKees Cook /*
917bac6f6cdSKees Cook * Prepare a dummy platform data structure to carry the module
918bac6f6cdSKees Cook * parameters. If mem_size isn't set, then there are no module
919bac6f6cdSKees Cook * parameters, and we can skip this.
920bac6f6cdSKees Cook */
921924d3711SAnton Vorontsov if (!mem_size)
922924d3711SAnton Vorontsov return;
923924d3711SAnton Vorontsov
924924d3711SAnton Vorontsov pr_info("using module parameters\n");
925924d3711SAnton Vorontsov
926182ca6e0SYue Hu memset(&pdata, 0, sizeof(pdata));
927182ca6e0SYue Hu pdata.mem_size = mem_size;
928182ca6e0SYue Hu pdata.mem_address = mem_address;
929182ca6e0SYue Hu pdata.mem_type = mem_type;
930182ca6e0SYue Hu pdata.record_size = record_size;
931182ca6e0SYue Hu pdata.console_size = ramoops_console_size;
932182ca6e0SYue Hu pdata.ftrace_size = ramoops_ftrace_size;
933182ca6e0SYue Hu pdata.pmsg_size = ramoops_pmsg_size;
934791205e3SKees Cook /* If "max_reason" is set, its value has priority over "dump_oops". */
935791205e3SKees Cook if (ramoops_max_reason >= 0)
936791205e3SKees Cook pdata.max_reason = ramoops_max_reason;
937791205e3SKees Cook /* Otherwise, if "dump_oops" is set, parse it into "max_reason". */
938791205e3SKees Cook else if (ramoops_dump_oops != -1)
939791205e3SKees Cook pdata.max_reason = ramoops_dump_oops ? KMSG_DUMP_OOPS
940791205e3SKees Cook : KMSG_DUMP_PANIC;
941791205e3SKees Cook /* And if neither are explicitly set, use the default. */
942791205e3SKees Cook else
943791205e3SKees Cook pdata.max_reason = KMSG_DUMP_OOPS;
944182ca6e0SYue Hu pdata.flags = RAMOOPS_FLAG_FTRACE_PER_CPU;
945a1cf53acSJoel Fernandes
9465ca5d4e6SAnton Vorontsov /*
9475ca5d4e6SAnton Vorontsov * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
9485ca5d4e6SAnton Vorontsov * (using 1 byte for ECC isn't much of use anyway).
9495ca5d4e6SAnton Vorontsov */
950182ca6e0SYue Hu pdata.ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
9511894a253SAnton Vorontsov
952924d3711SAnton Vorontsov dummy = platform_device_register_data(NULL, "ramoops", -1,
953182ca6e0SYue Hu &pdata, sizeof(pdata));
954924d3711SAnton Vorontsov if (IS_ERR(dummy)) {
955924d3711SAnton Vorontsov pr_info("could not create platform device: %ld\n",
956924d3711SAnton Vorontsov PTR_ERR(dummy));
957bac6f6cdSKees Cook dummy = NULL;
958924d3711SAnton Vorontsov }
9591894a253SAnton Vorontsov }
9601894a253SAnton Vorontsov
ramoops_init(void)961924d3711SAnton Vorontsov static int __init ramoops_init(void)
962924d3711SAnton Vorontsov {
963bac6f6cdSKees Cook int ret;
964bac6f6cdSKees Cook
965924d3711SAnton Vorontsov ramoops_register_dummy();
966bac6f6cdSKees Cook ret = platform_driver_register(&ramoops_driver);
967bac6f6cdSKees Cook if (ret != 0)
968bac6f6cdSKees Cook ramoops_unregister_dummy();
969bac6f6cdSKees Cook
970bac6f6cdSKees Cook return ret;
9711894a253SAnton Vorontsov }
97241603165SJoel Fernandes (Google) postcore_initcall(ramoops_init);
9731894a253SAnton Vorontsov
ramoops_exit(void)9741894a253SAnton Vorontsov static void __exit ramoops_exit(void)
9751894a253SAnton Vorontsov {
9761894a253SAnton Vorontsov platform_driver_unregister(&ramoops_driver);
977bac6f6cdSKees Cook ramoops_unregister_dummy();
9781894a253SAnton Vorontsov }
9791894a253SAnton Vorontsov module_exit(ramoops_exit);
9801894a253SAnton Vorontsov
9811894a253SAnton Vorontsov MODULE_LICENSE("GPL");
9821894a253SAnton Vorontsov MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
9831894a253SAnton Vorontsov MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");
984