ram.c 26 KB

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