ram.c 26 KB

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