Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * RAM Oops/Panic logger
0004  *
0005  * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
0006  * Copyright (C) 2011 Kees Cook <keescook@chromium.org>
0007  */
0008 
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/err.h>
0013 #include <linux/module.h>
0014 #include <linux/version.h>
0015 #include <linux/pstore.h>
0016 #include <linux/io.h>
0017 #include <linux/ioport.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/slab.h>
0020 #include <linux/compiler.h>
0021 #include <linux/pstore_ram.h>
0022 #include <linux/of.h>
0023 #include <linux/of_address.h>
0024 #include "internal.h"
0025 
0026 #define RAMOOPS_KERNMSG_HDR "===="
0027 #define MIN_MEM_SIZE 4096UL
0028 
0029 static ulong record_size = MIN_MEM_SIZE;
0030 module_param(record_size, ulong, 0400);
0031 MODULE_PARM_DESC(record_size,
0032         "size of each dump done on oops/panic");
0033 
0034 static ulong ramoops_console_size = MIN_MEM_SIZE;
0035 module_param_named(console_size, ramoops_console_size, ulong, 0400);
0036 MODULE_PARM_DESC(console_size, "size of kernel console log");
0037 
0038 static ulong ramoops_ftrace_size = MIN_MEM_SIZE;
0039 module_param_named(ftrace_size, ramoops_ftrace_size, ulong, 0400);
0040 MODULE_PARM_DESC(ftrace_size, "size of ftrace log");
0041 
0042 static ulong ramoops_pmsg_size = MIN_MEM_SIZE;
0043 module_param_named(pmsg_size, ramoops_pmsg_size, ulong, 0400);
0044 MODULE_PARM_DESC(pmsg_size, "size of user space message log");
0045 
0046 static unsigned long long mem_address;
0047 module_param_hw(mem_address, ullong, other, 0400);
0048 MODULE_PARM_DESC(mem_address,
0049         "start of reserved RAM used to store oops/panic logs");
0050 
0051 static ulong mem_size;
0052 module_param(mem_size, ulong, 0400);
0053 MODULE_PARM_DESC(mem_size,
0054         "size of reserved RAM used to store oops/panic logs");
0055 
0056 static unsigned int mem_type;
0057 module_param(mem_type, uint, 0400);
0058 MODULE_PARM_DESC(mem_type,
0059         "memory type: 0=write-combined (default), 1=unbuffered, 2=cached");
0060 
0061 static int ramoops_max_reason = -1;
0062 module_param_named(max_reason, ramoops_max_reason, int, 0400);
0063 MODULE_PARM_DESC(max_reason,
0064          "maximum reason for kmsg dump (default 2: Oops and Panic) ");
0065 
0066 static int ramoops_ecc;
0067 module_param_named(ecc, ramoops_ecc, int, 0400);
0068 MODULE_PARM_DESC(ramoops_ecc,
0069         "if non-zero, the option enables ECC support and specifies "
0070         "ECC buffer size in bytes (1 is a special value, means 16 "
0071         "bytes ECC)");
0072 
0073 static int ramoops_dump_oops = -1;
0074 module_param_named(dump_oops, ramoops_dump_oops, int, 0400);
0075 MODULE_PARM_DESC(dump_oops,
0076          "(deprecated: use max_reason instead) set to 1 to dump oopses & panics, 0 to only dump panics");
0077 
0078 struct ramoops_context {
0079     struct persistent_ram_zone **dprzs; /* Oops dump zones */
0080     struct persistent_ram_zone *cprz;   /* Console zone */
0081     struct persistent_ram_zone **fprzs; /* Ftrace zones */
0082     struct persistent_ram_zone *mprz;   /* PMSG zone */
0083     phys_addr_t phys_addr;
0084     unsigned long size;
0085     unsigned int memtype;
0086     size_t record_size;
0087     size_t console_size;
0088     size_t ftrace_size;
0089     size_t pmsg_size;
0090     u32 flags;
0091     struct persistent_ram_ecc_info ecc_info;
0092     unsigned int max_dump_cnt;
0093     unsigned int dump_write_cnt;
0094     /* _read_cnt need clear on ramoops_pstore_open */
0095     unsigned int dump_read_cnt;
0096     unsigned int console_read_cnt;
0097     unsigned int max_ftrace_cnt;
0098     unsigned int ftrace_read_cnt;
0099     unsigned int pmsg_read_cnt;
0100     struct pstore_info pstore;
0101 };
0102 
0103 static struct platform_device *dummy;
0104 
0105 static int ramoops_pstore_open(struct pstore_info *psi)
0106 {
0107     struct ramoops_context *cxt = psi->data;
0108 
0109     cxt->dump_read_cnt = 0;
0110     cxt->console_read_cnt = 0;
0111     cxt->ftrace_read_cnt = 0;
0112     cxt->pmsg_read_cnt = 0;
0113     return 0;
0114 }
0115 
0116 static struct persistent_ram_zone *
0117 ramoops_get_next_prz(struct persistent_ram_zone *przs[], int id,
0118              struct pstore_record *record)
0119 {
0120     struct persistent_ram_zone *prz;
0121 
0122     /* Give up if we never existed or have hit the end. */
0123     if (!przs)
0124         return NULL;
0125 
0126     prz = przs[id];
0127     if (!prz)
0128         return NULL;
0129 
0130     /* Update old/shadowed buffer. */
0131     if (prz->type == PSTORE_TYPE_DMESG)
0132         persistent_ram_save_old(prz);
0133 
0134     if (!persistent_ram_old_size(prz))
0135         return NULL;
0136 
0137     record->type = prz->type;
0138     record->id = id;
0139 
0140     return prz;
0141 }
0142 
0143 static int ramoops_read_kmsg_hdr(char *buffer, struct timespec64 *time,
0144                   bool *compressed)
0145 {
0146     char data_type;
0147     int header_length = 0;
0148 
0149     if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lld.%lu-%c\n%n",
0150            (time64_t *)&time->tv_sec, &time->tv_nsec, &data_type,
0151            &header_length) == 3) {
0152         time->tv_nsec *= 1000;
0153         if (data_type == 'C')
0154             *compressed = true;
0155         else
0156             *compressed = false;
0157     } else if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lld.%lu\n%n",
0158               (time64_t *)&time->tv_sec, &time->tv_nsec,
0159               &header_length) == 2) {
0160         time->tv_nsec *= 1000;
0161         *compressed = false;
0162     } else {
0163         time->tv_sec = 0;
0164         time->tv_nsec = 0;
0165         *compressed = false;
0166     }
0167     return header_length;
0168 }
0169 
0170 static bool prz_ok(struct persistent_ram_zone *prz)
0171 {
0172     return !!prz && !!(persistent_ram_old_size(prz) +
0173                persistent_ram_ecc_string(prz, NULL, 0));
0174 }
0175 
0176 static ssize_t ramoops_pstore_read(struct pstore_record *record)
0177 {
0178     ssize_t size = 0;
0179     struct ramoops_context *cxt = record->psi->data;
0180     struct persistent_ram_zone *prz = NULL;
0181     int header_length = 0;
0182     bool free_prz = false;
0183 
0184     /*
0185      * Ramoops headers provide time stamps for PSTORE_TYPE_DMESG, but
0186      * PSTORE_TYPE_CONSOLE and PSTORE_TYPE_FTRACE don't currently have
0187      * valid time stamps, so it is initialized to zero.
0188      */
0189     record->time.tv_sec = 0;
0190     record->time.tv_nsec = 0;
0191     record->compressed = false;
0192 
0193     /* Find the next valid persistent_ram_zone for DMESG */
0194     while (cxt->dump_read_cnt < cxt->max_dump_cnt && !prz) {
0195         prz = ramoops_get_next_prz(cxt->dprzs, cxt->dump_read_cnt++,
0196                        record);
0197         if (!prz_ok(prz))
0198             continue;
0199         header_length = ramoops_read_kmsg_hdr(persistent_ram_old(prz),
0200                               &record->time,
0201                               &record->compressed);
0202         /* Clear and skip this DMESG record if it has no valid header */
0203         if (!header_length) {
0204             persistent_ram_free_old(prz);
0205             persistent_ram_zap(prz);
0206             prz = NULL;
0207         }
0208     }
0209 
0210     if (!prz_ok(prz) && !cxt->console_read_cnt++)
0211         prz = ramoops_get_next_prz(&cxt->cprz, 0 /* single */, record);
0212 
0213     if (!prz_ok(prz) && !cxt->pmsg_read_cnt++)
0214         prz = ramoops_get_next_prz(&cxt->mprz, 0 /* single */, record);
0215 
0216     /* ftrace is last since it may want to dynamically allocate memory. */
0217     if (!prz_ok(prz)) {
0218         if (!(cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU) &&
0219             !cxt->ftrace_read_cnt++) {
0220             prz = ramoops_get_next_prz(cxt->fprzs, 0 /* single */,
0221                            record);
0222         } else {
0223             /*
0224              * Build a new dummy record which combines all the
0225              * per-cpu records including metadata and ecc info.
0226              */
0227             struct persistent_ram_zone *tmp_prz, *prz_next;
0228 
0229             tmp_prz = kzalloc(sizeof(struct persistent_ram_zone),
0230                       GFP_KERNEL);
0231             if (!tmp_prz)
0232                 return -ENOMEM;
0233             prz = tmp_prz;
0234             free_prz = true;
0235 
0236             while (cxt->ftrace_read_cnt < cxt->max_ftrace_cnt) {
0237                 prz_next = ramoops_get_next_prz(cxt->fprzs,
0238                         cxt->ftrace_read_cnt++, record);
0239 
0240                 if (!prz_ok(prz_next))
0241                     continue;
0242 
0243                 tmp_prz->ecc_info = prz_next->ecc_info;
0244                 tmp_prz->corrected_bytes +=
0245                         prz_next->corrected_bytes;
0246                 tmp_prz->bad_blocks += prz_next->bad_blocks;
0247 
0248                 size = pstore_ftrace_combine_log(
0249                         &tmp_prz->old_log,
0250                         &tmp_prz->old_log_size,
0251                         prz_next->old_log,
0252                         prz_next->old_log_size);
0253                 if (size)
0254                     goto out;
0255             }
0256             record->id = 0;
0257         }
0258     }
0259 
0260     if (!prz_ok(prz)) {
0261         size = 0;
0262         goto out;
0263     }
0264 
0265     size = persistent_ram_old_size(prz) - header_length;
0266 
0267     /* ECC correction notice */
0268     record->ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0);
0269 
0270     record->buf = kmalloc(size + record->ecc_notice_size + 1, GFP_KERNEL);
0271     if (record->buf == NULL) {
0272         size = -ENOMEM;
0273         goto out;
0274     }
0275 
0276     memcpy(record->buf, (char *)persistent_ram_old(prz) + header_length,
0277            size);
0278 
0279     persistent_ram_ecc_string(prz, record->buf + size,
0280                   record->ecc_notice_size + 1);
0281 
0282 out:
0283     if (free_prz) {
0284         kfree(prz->old_log);
0285         kfree(prz);
0286     }
0287 
0288     return size;
0289 }
0290 
0291 static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz,
0292                      struct pstore_record *record)
0293 {
0294     char hdr[36]; /* "===="(4), %lld(20), "."(1), %06lu(6), "-%c\n"(3) */
0295     size_t len;
0296 
0297     len = scnprintf(hdr, sizeof(hdr),
0298         RAMOOPS_KERNMSG_HDR "%lld.%06lu-%c\n",
0299         (time64_t)record->time.tv_sec,
0300         record->time.tv_nsec / 1000,
0301         record->compressed ? 'C' : 'D');
0302     persistent_ram_write(prz, hdr, len);
0303 
0304     return len;
0305 }
0306 
0307 static int notrace ramoops_pstore_write(struct pstore_record *record)
0308 {
0309     struct ramoops_context *cxt = record->psi->data;
0310     struct persistent_ram_zone *prz;
0311     size_t size, hlen;
0312 
0313     if (record->type == PSTORE_TYPE_CONSOLE) {
0314         if (!cxt->cprz)
0315             return -ENOMEM;
0316         persistent_ram_write(cxt->cprz, record->buf, record->size);
0317         return 0;
0318     } else if (record->type == PSTORE_TYPE_FTRACE) {
0319         int zonenum;
0320 
0321         if (!cxt->fprzs)
0322             return -ENOMEM;
0323         /*
0324          * Choose zone by if we're using per-cpu buffers.
0325          */
0326         if (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
0327             zonenum = smp_processor_id();
0328         else
0329             zonenum = 0;
0330 
0331         persistent_ram_write(cxt->fprzs[zonenum], record->buf,
0332                      record->size);
0333         return 0;
0334     } else if (record->type == PSTORE_TYPE_PMSG) {
0335         pr_warn_ratelimited("PMSG shouldn't call %s\n", __func__);
0336         return -EINVAL;
0337     }
0338 
0339     if (record->type != PSTORE_TYPE_DMESG)
0340         return -EINVAL;
0341 
0342     /*
0343      * We could filter on record->reason here if we wanted to (which
0344      * would duplicate what happened before the "max_reason" setting
0345      * was added), but that would defeat the purpose of a system
0346      * changing printk.always_kmsg_dump, so instead log everything that
0347      * the kmsg dumper sends us, since it should be doing the filtering
0348      * based on the combination of printk.always_kmsg_dump and our
0349      * requested "max_reason".
0350      */
0351 
0352     /*
0353      * Explicitly only take the first part of any new crash.
0354      * If our buffer is larger than kmsg_bytes, this can never happen,
0355      * and if our buffer is smaller than kmsg_bytes, we don't want the
0356      * report split across multiple records.
0357      */
0358     if (record->part != 1)
0359         return -ENOSPC;
0360 
0361     if (!cxt->dprzs)
0362         return -ENOSPC;
0363 
0364     prz = cxt->dprzs[cxt->dump_write_cnt];
0365 
0366     /*
0367      * Since this is a new crash dump, we need to reset the buffer in
0368      * case it still has an old dump present. Without this, the new dump
0369      * will get appended, which would seriously confuse anything trying
0370      * to check dump file contents. Specifically, ramoops_read_kmsg_hdr()
0371      * expects to find a dump header in the beginning of buffer data, so
0372      * we must to reset the buffer values, in order to ensure that the
0373      * header will be written to the beginning of the buffer.
0374      */
0375     persistent_ram_zap(prz);
0376 
0377     /* Build header and append record contents. */
0378     hlen = ramoops_write_kmsg_hdr(prz, record);
0379     if (!hlen)
0380         return -ENOMEM;
0381 
0382     size = record->size;
0383     if (size + hlen > prz->buffer_size)
0384         size = prz->buffer_size - hlen;
0385     persistent_ram_write(prz, record->buf, size);
0386 
0387     cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
0388 
0389     return 0;
0390 }
0391 
0392 static int notrace ramoops_pstore_write_user(struct pstore_record *record,
0393                          const char __user *buf)
0394 {
0395     if (record->type == PSTORE_TYPE_PMSG) {
0396         struct ramoops_context *cxt = record->psi->data;
0397 
0398         if (!cxt->mprz)
0399             return -ENOMEM;
0400         return persistent_ram_write_user(cxt->mprz, buf, record->size);
0401     }
0402 
0403     return -EINVAL;
0404 }
0405 
0406 static int ramoops_pstore_erase(struct pstore_record *record)
0407 {
0408     struct ramoops_context *cxt = record->psi->data;
0409     struct persistent_ram_zone *prz;
0410 
0411     switch (record->type) {
0412     case PSTORE_TYPE_DMESG:
0413         if (record->id >= cxt->max_dump_cnt)
0414             return -EINVAL;
0415         prz = cxt->dprzs[record->id];
0416         break;
0417     case PSTORE_TYPE_CONSOLE:
0418         prz = cxt->cprz;
0419         break;
0420     case PSTORE_TYPE_FTRACE:
0421         if (record->id >= cxt->max_ftrace_cnt)
0422             return -EINVAL;
0423         prz = cxt->fprzs[record->id];
0424         break;
0425     case PSTORE_TYPE_PMSG:
0426         prz = cxt->mprz;
0427         break;
0428     default:
0429         return -EINVAL;
0430     }
0431 
0432     persistent_ram_free_old(prz);
0433     persistent_ram_zap(prz);
0434 
0435     return 0;
0436 }
0437 
0438 static struct ramoops_context oops_cxt = {
0439     .pstore = {
0440         .owner  = THIS_MODULE,
0441         .name   = "ramoops",
0442         .open   = ramoops_pstore_open,
0443         .read   = ramoops_pstore_read,
0444         .write  = ramoops_pstore_write,
0445         .write_user = ramoops_pstore_write_user,
0446         .erase  = ramoops_pstore_erase,
0447     },
0448 };
0449 
0450 static void ramoops_free_przs(struct ramoops_context *cxt)
0451 {
0452     int i;
0453 
0454     /* Free dump PRZs */
0455     if (cxt->dprzs) {
0456         for (i = 0; i < cxt->max_dump_cnt; i++)
0457             persistent_ram_free(cxt->dprzs[i]);
0458 
0459         kfree(cxt->dprzs);
0460         cxt->max_dump_cnt = 0;
0461     }
0462 
0463     /* Free ftrace PRZs */
0464     if (cxt->fprzs) {
0465         for (i = 0; i < cxt->max_ftrace_cnt; i++)
0466             persistent_ram_free(cxt->fprzs[i]);
0467         kfree(cxt->fprzs);
0468         cxt->max_ftrace_cnt = 0;
0469     }
0470 }
0471 
0472 static int ramoops_init_przs(const char *name,
0473                  struct device *dev, struct ramoops_context *cxt,
0474                  struct persistent_ram_zone ***przs,
0475                  phys_addr_t *paddr, size_t mem_sz,
0476                  ssize_t record_size,
0477                  unsigned int *cnt, u32 sig, u32 flags)
0478 {
0479     int err = -ENOMEM;
0480     int i;
0481     size_t zone_sz;
0482     struct persistent_ram_zone **prz_ar;
0483 
0484     /* Allocate nothing for 0 mem_sz or 0 record_size. */
0485     if (mem_sz == 0 || record_size == 0) {
0486         *cnt = 0;
0487         return 0;
0488     }
0489 
0490     /*
0491      * If we have a negative record size, calculate it based on
0492      * mem_sz / *cnt. If we have a positive record size, calculate
0493      * cnt from mem_sz / record_size.
0494      */
0495     if (record_size < 0) {
0496         if (*cnt == 0)
0497             return 0;
0498         record_size = mem_sz / *cnt;
0499         if (record_size == 0) {
0500             dev_err(dev, "%s record size == 0 (%zu / %u)\n",
0501                 name, mem_sz, *cnt);
0502             goto fail;
0503         }
0504     } else {
0505         *cnt = mem_sz / record_size;
0506         if (*cnt == 0) {
0507             dev_err(dev, "%s record count == 0 (%zu / %zu)\n",
0508                 name, mem_sz, record_size);
0509             goto fail;
0510         }
0511     }
0512 
0513     if (*paddr + mem_sz - cxt->phys_addr > cxt->size) {
0514         dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
0515             name,
0516             mem_sz, (unsigned long long)*paddr,
0517             cxt->size, (unsigned long long)cxt->phys_addr);
0518         goto fail;
0519     }
0520 
0521     zone_sz = mem_sz / *cnt;
0522     if (!zone_sz) {
0523         dev_err(dev, "%s zone size == 0\n", name);
0524         goto fail;
0525     }
0526 
0527     prz_ar = kcalloc(*cnt, sizeof(**przs), GFP_KERNEL);
0528     if (!prz_ar)
0529         goto fail;
0530 
0531     for (i = 0; i < *cnt; i++) {
0532         char *label;
0533 
0534         if (*cnt == 1)
0535             label = kasprintf(GFP_KERNEL, "ramoops:%s", name);
0536         else
0537             label = kasprintf(GFP_KERNEL, "ramoops:%s(%d/%d)",
0538                       name, i, *cnt - 1);
0539         prz_ar[i] = persistent_ram_new(*paddr, zone_sz, sig,
0540                            &cxt->ecc_info,
0541                            cxt->memtype, flags, label);
0542         kfree(label);
0543         if (IS_ERR(prz_ar[i])) {
0544             err = PTR_ERR(prz_ar[i]);
0545             dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n",
0546                 name, record_size,
0547                 (unsigned long long)*paddr, err);
0548 
0549             while (i > 0) {
0550                 i--;
0551                 persistent_ram_free(prz_ar[i]);
0552             }
0553             kfree(prz_ar);
0554             goto fail;
0555         }
0556         *paddr += zone_sz;
0557         prz_ar[i]->type = pstore_name_to_type(name);
0558     }
0559 
0560     *przs = prz_ar;
0561     return 0;
0562 
0563 fail:
0564     *cnt = 0;
0565     return err;
0566 }
0567 
0568 static int ramoops_init_prz(const char *name,
0569                 struct device *dev, struct ramoops_context *cxt,
0570                 struct persistent_ram_zone **prz,
0571                 phys_addr_t *paddr, size_t sz, u32 sig)
0572 {
0573     char *label;
0574 
0575     if (!sz)
0576         return 0;
0577 
0578     if (*paddr + sz - cxt->phys_addr > cxt->size) {
0579         dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
0580             name, sz, (unsigned long long)*paddr,
0581             cxt->size, (unsigned long long)cxt->phys_addr);
0582         return -ENOMEM;
0583     }
0584 
0585     label = kasprintf(GFP_KERNEL, "ramoops:%s", name);
0586     *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info,
0587                   cxt->memtype, PRZ_FLAG_ZAP_OLD, label);
0588     kfree(label);
0589     if (IS_ERR(*prz)) {
0590         int err = PTR_ERR(*prz);
0591 
0592         dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n",
0593             name, sz, (unsigned long long)*paddr, err);
0594         return err;
0595     }
0596 
0597     *paddr += sz;
0598     (*prz)->type = pstore_name_to_type(name);
0599 
0600     return 0;
0601 }
0602 
0603 /* Read a u32 from a dt property and make sure it's safe for an int. */
0604 static int ramoops_parse_dt_u32(struct platform_device *pdev,
0605                 const char *propname,
0606                 u32 default_value, u32 *value)
0607 {
0608     u32 val32 = 0;
0609     int ret;
0610 
0611     ret = of_property_read_u32(pdev->dev.of_node, propname, &val32);
0612     if (ret == -EINVAL) {
0613         /* field is missing, use default value. */
0614         val32 = default_value;
0615     } else if (ret < 0) {
0616         dev_err(&pdev->dev, "failed to parse property %s: %d\n",
0617             propname, ret);
0618         return ret;
0619     }
0620 
0621     /* Sanity check our results. */
0622     if (val32 > INT_MAX) {
0623         dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32);
0624         return -EOVERFLOW;
0625     }
0626 
0627     *value = val32;
0628     return 0;
0629 }
0630 
0631 static int ramoops_parse_dt(struct platform_device *pdev,
0632                 struct ramoops_platform_data *pdata)
0633 {
0634     struct device_node *of_node = pdev->dev.of_node;
0635     struct device_node *parent_node;
0636     struct resource *res;
0637     u32 value;
0638     int ret;
0639 
0640     dev_dbg(&pdev->dev, "using Device Tree\n");
0641 
0642     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0643     if (!res) {
0644         dev_err(&pdev->dev,
0645             "failed to locate DT /reserved-memory resource\n");
0646         return -EINVAL;
0647     }
0648 
0649     pdata->mem_size = resource_size(res);
0650     pdata->mem_address = res->start;
0651     /*
0652      * Setting "unbuffered" is deprecated and will be ignored if
0653      * "mem_type" is also specified.
0654      */
0655     pdata->mem_type = of_property_read_bool(of_node, "unbuffered");
0656     /*
0657      * Setting "no-dump-oops" is deprecated and will be ignored if
0658      * "max_reason" is also specified.
0659      */
0660     if (of_property_read_bool(of_node, "no-dump-oops"))
0661         pdata->max_reason = KMSG_DUMP_PANIC;
0662     else
0663         pdata->max_reason = KMSG_DUMP_OOPS;
0664 
0665 #define parse_u32(name, field, default_value) {             \
0666         ret = ramoops_parse_dt_u32(pdev, name, default_value,   \
0667                         &value);            \
0668         if (ret < 0)                        \
0669             return ret;                 \
0670         field = value;                      \
0671     }
0672 
0673     parse_u32("mem-type", pdata->record_size, pdata->mem_type);
0674     parse_u32("record-size", pdata->record_size, 0);
0675     parse_u32("console-size", pdata->console_size, 0);
0676     parse_u32("ftrace-size", pdata->ftrace_size, 0);
0677     parse_u32("pmsg-size", pdata->pmsg_size, 0);
0678     parse_u32("ecc-size", pdata->ecc_info.ecc_size, 0);
0679     parse_u32("flags", pdata->flags, 0);
0680     parse_u32("max-reason", pdata->max_reason, pdata->max_reason);
0681 
0682 #undef parse_u32
0683 
0684     /*
0685      * Some old Chromebooks relied on the kernel setting the
0686      * console_size and pmsg_size to the record size since that's
0687      * what the downstream kernel did.  These same Chromebooks had
0688      * "ramoops" straight under the root node which isn't
0689      * according to the current upstream bindings (though it was
0690      * arguably acceptable under a prior version of the bindings).
0691      * Let's make those old Chromebooks work by detecting that
0692      * we're not a child of "reserved-memory" and mimicking the
0693      * expected behavior.
0694      */
0695     parent_node = of_get_parent(of_node);
0696     if (!of_node_name_eq(parent_node, "reserved-memory") &&
0697         !pdata->console_size && !pdata->ftrace_size &&
0698         !pdata->pmsg_size && !pdata->ecc_info.ecc_size) {
0699         pdata->console_size = pdata->record_size;
0700         pdata->pmsg_size = pdata->record_size;
0701     }
0702     of_node_put(parent_node);
0703 
0704     return 0;
0705 }
0706 
0707 static int ramoops_probe(struct platform_device *pdev)
0708 {
0709     struct device *dev = &pdev->dev;
0710     struct ramoops_platform_data *pdata = dev->platform_data;
0711     struct ramoops_platform_data pdata_local;
0712     struct ramoops_context *cxt = &oops_cxt;
0713     size_t dump_mem_sz;
0714     phys_addr_t paddr;
0715     int err = -EINVAL;
0716 
0717     /*
0718      * Only a single ramoops area allowed at a time, so fail extra
0719      * probes.
0720      */
0721     if (cxt->max_dump_cnt) {
0722         pr_err("already initialized\n");
0723         goto fail_out;
0724     }
0725 
0726     if (dev_of_node(dev) && !pdata) {
0727         pdata = &pdata_local;
0728         memset(pdata, 0, sizeof(*pdata));
0729 
0730         err = ramoops_parse_dt(pdev, pdata);
0731         if (err < 0)
0732             goto fail_out;
0733     }
0734 
0735     /* Make sure we didn't get bogus platform data pointer. */
0736     if (!pdata) {
0737         pr_err("NULL platform data\n");
0738         goto fail_out;
0739     }
0740 
0741     if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size &&
0742             !pdata->ftrace_size && !pdata->pmsg_size)) {
0743         pr_err("The memory size and the record/console size must be "
0744             "non-zero\n");
0745         goto fail_out;
0746     }
0747 
0748     if (pdata->record_size && !is_power_of_2(pdata->record_size))
0749         pdata->record_size = rounddown_pow_of_two(pdata->record_size);
0750     if (pdata->console_size && !is_power_of_2(pdata->console_size))
0751         pdata->console_size = rounddown_pow_of_two(pdata->console_size);
0752     if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size))
0753         pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
0754     if (pdata->pmsg_size && !is_power_of_2(pdata->pmsg_size))
0755         pdata->pmsg_size = rounddown_pow_of_two(pdata->pmsg_size);
0756 
0757     cxt->size = pdata->mem_size;
0758     cxt->phys_addr = pdata->mem_address;
0759     cxt->memtype = pdata->mem_type;
0760     cxt->record_size = pdata->record_size;
0761     cxt->console_size = pdata->console_size;
0762     cxt->ftrace_size = pdata->ftrace_size;
0763     cxt->pmsg_size = pdata->pmsg_size;
0764     cxt->flags = pdata->flags;
0765     cxt->ecc_info = pdata->ecc_info;
0766 
0767     paddr = cxt->phys_addr;
0768 
0769     dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size
0770             - cxt->pmsg_size;
0771     err = ramoops_init_przs("dmesg", dev, cxt, &cxt->dprzs, &paddr,
0772                 dump_mem_sz, cxt->record_size,
0773                 &cxt->max_dump_cnt, 0, 0);
0774     if (err)
0775         goto fail_out;
0776 
0777     err = ramoops_init_prz("console", dev, cxt, &cxt->cprz, &paddr,
0778                    cxt->console_size, 0);
0779     if (err)
0780         goto fail_init_cprz;
0781 
0782     cxt->max_ftrace_cnt = (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
0783                 ? nr_cpu_ids
0784                 : 1;
0785     err = ramoops_init_przs("ftrace", dev, cxt, &cxt->fprzs, &paddr,
0786                 cxt->ftrace_size, -1,
0787                 &cxt->max_ftrace_cnt, LINUX_VERSION_CODE,
0788                 (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
0789                     ? PRZ_FLAG_NO_LOCK : 0);
0790     if (err)
0791         goto fail_init_fprz;
0792 
0793     err = ramoops_init_prz("pmsg", dev, cxt, &cxt->mprz, &paddr,
0794                 cxt->pmsg_size, 0);
0795     if (err)
0796         goto fail_init_mprz;
0797 
0798     cxt->pstore.data = cxt;
0799     /*
0800      * Prepare frontend flags based on which areas are initialized.
0801      * For ramoops_init_przs() cases, the "max count" variable tells
0802      * if there are regions present. For ramoops_init_prz() cases,
0803      * the single region size is how to check.
0804      */
0805     cxt->pstore.flags = 0;
0806     if (cxt->max_dump_cnt) {
0807         cxt->pstore.flags |= PSTORE_FLAGS_DMESG;
0808         cxt->pstore.max_reason = pdata->max_reason;
0809     }
0810     if (cxt->console_size)
0811         cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
0812     if (cxt->max_ftrace_cnt)
0813         cxt->pstore.flags |= PSTORE_FLAGS_FTRACE;
0814     if (cxt->pmsg_size)
0815         cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
0816 
0817     /*
0818      * Since bufsize is only used for dmesg crash dumps, it
0819      * must match the size of the dprz record (after PRZ header
0820      * and ECC bytes have been accounted for).
0821      */
0822     if (cxt->pstore.flags & PSTORE_FLAGS_DMESG) {
0823         cxt->pstore.bufsize = cxt->dprzs[0]->buffer_size;
0824         cxt->pstore.buf = kzalloc(cxt->pstore.bufsize, GFP_KERNEL);
0825         if (!cxt->pstore.buf) {
0826             pr_err("cannot allocate pstore crash dump buffer\n");
0827             err = -ENOMEM;
0828             goto fail_clear;
0829         }
0830     }
0831 
0832     err = pstore_register(&cxt->pstore);
0833     if (err) {
0834         pr_err("registering with pstore failed\n");
0835         goto fail_buf;
0836     }
0837 
0838     /*
0839      * Update the module parameter variables as well so they are visible
0840      * through /sys/module/ramoops/parameters/
0841      */
0842     mem_size = pdata->mem_size;
0843     mem_address = pdata->mem_address;
0844     record_size = pdata->record_size;
0845     ramoops_max_reason = pdata->max_reason;
0846     ramoops_console_size = pdata->console_size;
0847     ramoops_pmsg_size = pdata->pmsg_size;
0848     ramoops_ftrace_size = pdata->ftrace_size;
0849 
0850     pr_info("using 0x%lx@0x%llx, ecc: %d\n",
0851         cxt->size, (unsigned long long)cxt->phys_addr,
0852         cxt->ecc_info.ecc_size);
0853 
0854     return 0;
0855 
0856 fail_buf:
0857     kfree(cxt->pstore.buf);
0858 fail_clear:
0859     cxt->pstore.bufsize = 0;
0860     persistent_ram_free(cxt->mprz);
0861 fail_init_mprz:
0862 fail_init_fprz:
0863     persistent_ram_free(cxt->cprz);
0864 fail_init_cprz:
0865     ramoops_free_przs(cxt);
0866 fail_out:
0867     return err;
0868 }
0869 
0870 static int ramoops_remove(struct platform_device *pdev)
0871 {
0872     struct ramoops_context *cxt = &oops_cxt;
0873 
0874     pstore_unregister(&cxt->pstore);
0875 
0876     kfree(cxt->pstore.buf);
0877     cxt->pstore.bufsize = 0;
0878 
0879     persistent_ram_free(cxt->mprz);
0880     persistent_ram_free(cxt->cprz);
0881     ramoops_free_przs(cxt);
0882 
0883     return 0;
0884 }
0885 
0886 static const struct of_device_id dt_match[] = {
0887     { .compatible = "ramoops" },
0888     {}
0889 };
0890 
0891 static struct platform_driver ramoops_driver = {
0892     .probe      = ramoops_probe,
0893     .remove     = ramoops_remove,
0894     .driver     = {
0895         .name       = "ramoops",
0896         .of_match_table = dt_match,
0897     },
0898 };
0899 
0900 static inline void ramoops_unregister_dummy(void)
0901 {
0902     platform_device_unregister(dummy);
0903     dummy = NULL;
0904 }
0905 
0906 static void __init ramoops_register_dummy(void)
0907 {
0908     struct ramoops_platform_data pdata;
0909 
0910     /*
0911      * Prepare a dummy platform data structure to carry the module
0912      * parameters. If mem_size isn't set, then there are no module
0913      * parameters, and we can skip this.
0914      */
0915     if (!mem_size)
0916         return;
0917 
0918     pr_info("using module parameters\n");
0919 
0920     memset(&pdata, 0, sizeof(pdata));
0921     pdata.mem_size = mem_size;
0922     pdata.mem_address = mem_address;
0923     pdata.mem_type = mem_type;
0924     pdata.record_size = record_size;
0925     pdata.console_size = ramoops_console_size;
0926     pdata.ftrace_size = ramoops_ftrace_size;
0927     pdata.pmsg_size = ramoops_pmsg_size;
0928     /* If "max_reason" is set, its value has priority over "dump_oops". */
0929     if (ramoops_max_reason >= 0)
0930         pdata.max_reason = ramoops_max_reason;
0931     /* Otherwise, if "dump_oops" is set, parse it into "max_reason". */
0932     else if (ramoops_dump_oops != -1)
0933         pdata.max_reason = ramoops_dump_oops ? KMSG_DUMP_OOPS
0934                              : KMSG_DUMP_PANIC;
0935     /* And if neither are explicitly set, use the default. */
0936     else
0937         pdata.max_reason = KMSG_DUMP_OOPS;
0938     pdata.flags = RAMOOPS_FLAG_FTRACE_PER_CPU;
0939 
0940     /*
0941      * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
0942      * (using 1 byte for ECC isn't much of use anyway).
0943      */
0944     pdata.ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
0945 
0946     dummy = platform_device_register_data(NULL, "ramoops", -1,
0947             &pdata, sizeof(pdata));
0948     if (IS_ERR(dummy)) {
0949         pr_info("could not create platform device: %ld\n",
0950             PTR_ERR(dummy));
0951         dummy = NULL;
0952     }
0953 }
0954 
0955 static int __init ramoops_init(void)
0956 {
0957     int ret;
0958 
0959     ramoops_register_dummy();
0960     ret = platform_driver_register(&ramoops_driver);
0961     if (ret != 0)
0962         ramoops_unregister_dummy();
0963 
0964     return ret;
0965 }
0966 postcore_initcall(ramoops_init);
0967 
0968 static void __exit ramoops_exit(void)
0969 {
0970     platform_driver_unregister(&ramoops_driver);
0971     ramoops_unregister_dummy();
0972 }
0973 module_exit(ramoops_exit);
0974 
0975 MODULE_LICENSE("GPL");
0976 MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
0977 MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");