ram.c 24 KB

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