ram.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. time->tv_nsec *= 1000;
  141. if (data_type == 'C')
  142. *compressed = true;
  143. else
  144. *compressed = false;
  145. } else if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lld.%lu\n%n",
  146. (time64_t *)&time->tv_sec, &time->tv_nsec,
  147. &header_length) == 2) {
  148. time->tv_nsec *= 1000;
  149. *compressed = false;
  150. } else {
  151. time->tv_sec = 0;
  152. time->tv_nsec = 0;
  153. *compressed = false;
  154. }
  155. return header_length;
  156. }
  157. static bool prz_ok(struct persistent_ram_zone *prz)
  158. {
  159. return !!prz && !!(persistent_ram_old_size(prz) +
  160. persistent_ram_ecc_string(prz, NULL, 0));
  161. }
  162. static ssize_t ftrace_log_combine(struct persistent_ram_zone *dest,
  163. struct persistent_ram_zone *src)
  164. {
  165. size_t dest_size, src_size, total, dest_off, src_off;
  166. size_t dest_idx = 0, src_idx = 0, merged_idx = 0;
  167. void *merged_buf;
  168. struct pstore_ftrace_record *drec, *srec, *mrec;
  169. size_t record_size = sizeof(struct pstore_ftrace_record);
  170. dest_off = dest->old_log_size % record_size;
  171. dest_size = dest->old_log_size - dest_off;
  172. src_off = src->old_log_size % record_size;
  173. src_size = src->old_log_size - src_off;
  174. total = dest_size + src_size;
  175. merged_buf = kmalloc(total, GFP_KERNEL);
  176. if (!merged_buf)
  177. return -ENOMEM;
  178. drec = (struct pstore_ftrace_record *)(dest->old_log + dest_off);
  179. srec = (struct pstore_ftrace_record *)(src->old_log + src_off);
  180. mrec = (struct pstore_ftrace_record *)(merged_buf);
  181. while (dest_size > 0 && src_size > 0) {
  182. if (pstore_ftrace_read_timestamp(&drec[dest_idx]) <
  183. pstore_ftrace_read_timestamp(&srec[src_idx])) {
  184. mrec[merged_idx++] = drec[dest_idx++];
  185. dest_size -= record_size;
  186. } else {
  187. mrec[merged_idx++] = srec[src_idx++];
  188. src_size -= record_size;
  189. }
  190. }
  191. while (dest_size > 0) {
  192. mrec[merged_idx++] = drec[dest_idx++];
  193. dest_size -= record_size;
  194. }
  195. while (src_size > 0) {
  196. mrec[merged_idx++] = srec[src_idx++];
  197. src_size -= record_size;
  198. }
  199. kfree(dest->old_log);
  200. dest->old_log = merged_buf;
  201. dest->old_log_size = total;
  202. return 0;
  203. }
  204. static ssize_t ramoops_pstore_read(struct pstore_record *record)
  205. {
  206. ssize_t size = 0;
  207. struct ramoops_context *cxt = record->psi->data;
  208. struct persistent_ram_zone *prz = NULL;
  209. int header_length = 0;
  210. bool free_prz = false;
  211. /*
  212. * Ramoops headers provide time stamps for PSTORE_TYPE_DMESG, but
  213. * PSTORE_TYPE_CONSOLE and PSTORE_TYPE_FTRACE don't currently have
  214. * valid time stamps, so it is initialized to zero.
  215. */
  216. record->time.tv_sec = 0;
  217. record->time.tv_nsec = 0;
  218. record->compressed = false;
  219. /* Find the next valid persistent_ram_zone for DMESG */
  220. while (cxt->dump_read_cnt < cxt->max_dump_cnt && !prz) {
  221. prz = ramoops_get_next_prz(cxt->dprzs, &cxt->dump_read_cnt,
  222. cxt->max_dump_cnt, &record->id,
  223. &record->type,
  224. PSTORE_TYPE_DMESG, 1);
  225. if (!prz_ok(prz))
  226. continue;
  227. header_length = ramoops_read_kmsg_hdr(persistent_ram_old(prz),
  228. &record->time,
  229. &record->compressed);
  230. /* Clear and skip this DMESG record if it has no valid header */
  231. if (!header_length) {
  232. persistent_ram_free_old(prz);
  233. persistent_ram_zap(prz);
  234. prz = NULL;
  235. }
  236. }
  237. if (!prz_ok(prz))
  238. prz = ramoops_get_next_prz(&cxt->cprz, &cxt->console_read_cnt,
  239. 1, &record->id, &record->type,
  240. PSTORE_TYPE_CONSOLE, 0);
  241. if (!prz_ok(prz))
  242. prz = ramoops_get_next_prz(&cxt->mprz, &cxt->pmsg_read_cnt,
  243. 1, &record->id, &record->type,
  244. PSTORE_TYPE_PMSG, 0);
  245. /* ftrace is last since it may want to dynamically allocate memory. */
  246. if (!prz_ok(prz)) {
  247. if (!(cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)) {
  248. prz = ramoops_get_next_prz(cxt->fprzs,
  249. &cxt->ftrace_read_cnt, 1, &record->id,
  250. &record->type, PSTORE_TYPE_FTRACE, 0);
  251. } else {
  252. /*
  253. * Build a new dummy record which combines all the
  254. * per-cpu records including metadata and ecc info.
  255. */
  256. struct persistent_ram_zone *tmp_prz, *prz_next;
  257. tmp_prz = kzalloc(sizeof(struct persistent_ram_zone),
  258. GFP_KERNEL);
  259. if (!tmp_prz)
  260. return -ENOMEM;
  261. prz = tmp_prz;
  262. free_prz = true;
  263. while (cxt->ftrace_read_cnt < cxt->max_ftrace_cnt) {
  264. prz_next = ramoops_get_next_prz(cxt->fprzs,
  265. &cxt->ftrace_read_cnt,
  266. cxt->max_ftrace_cnt,
  267. &record->id,
  268. &record->type,
  269. PSTORE_TYPE_FTRACE, 0);
  270. if (!prz_ok(prz_next))
  271. continue;
  272. tmp_prz->ecc_info = prz_next->ecc_info;
  273. tmp_prz->corrected_bytes +=
  274. prz_next->corrected_bytes;
  275. tmp_prz->bad_blocks += prz_next->bad_blocks;
  276. size = ftrace_log_combine(tmp_prz, prz_next);
  277. if (size)
  278. goto out;
  279. }
  280. record->id = 0;
  281. }
  282. }
  283. if (!prz_ok(prz)) {
  284. size = 0;
  285. goto out;
  286. }
  287. size = persistent_ram_old_size(prz) - header_length;
  288. /* ECC correction notice */
  289. record->ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0);
  290. record->buf = kmalloc(size + record->ecc_notice_size + 1, GFP_KERNEL);
  291. if (record->buf == NULL) {
  292. size = -ENOMEM;
  293. goto out;
  294. }
  295. memcpy(record->buf, (char *)persistent_ram_old(prz) + header_length,
  296. size);
  297. persistent_ram_ecc_string(prz, record->buf + size,
  298. record->ecc_notice_size + 1);
  299. out:
  300. if (free_prz) {
  301. kfree(prz->old_log);
  302. kfree(prz);
  303. }
  304. return size;
  305. }
  306. static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz,
  307. struct pstore_record *record)
  308. {
  309. char *hdr;
  310. size_t len;
  311. hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lld.%06lu-%c\n",
  312. (time64_t)record->time.tv_sec,
  313. record->time.tv_nsec / 1000,
  314. record->compressed ? 'C' : 'D');
  315. WARN_ON_ONCE(!hdr);
  316. len = hdr ? strlen(hdr) : 0;
  317. persistent_ram_write(prz, hdr, len);
  318. kfree(hdr);
  319. return len;
  320. }
  321. static int notrace ramoops_pstore_write(struct pstore_record *record)
  322. {
  323. struct ramoops_context *cxt = record->psi->data;
  324. struct persistent_ram_zone *prz;
  325. size_t size, hlen;
  326. if (record->type == PSTORE_TYPE_CONSOLE) {
  327. if (!cxt->cprz)
  328. return -ENOMEM;
  329. persistent_ram_write(cxt->cprz, record->buf, record->size);
  330. return 0;
  331. } else if (record->type == PSTORE_TYPE_FTRACE) {
  332. int zonenum;
  333. if (!cxt->fprzs)
  334. return -ENOMEM;
  335. /*
  336. * Choose zone by if we're using per-cpu buffers.
  337. */
  338. if (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
  339. zonenum = smp_processor_id();
  340. else
  341. zonenum = 0;
  342. persistent_ram_write(cxt->fprzs[zonenum], record->buf,
  343. record->size);
  344. return 0;
  345. } else if (record->type == PSTORE_TYPE_PMSG) {
  346. pr_warn_ratelimited("PMSG shouldn't call %s\n", __func__);
  347. return -EINVAL;
  348. }
  349. if (record->type != PSTORE_TYPE_DMESG)
  350. return -EINVAL;
  351. /*
  352. * Out of the various dmesg dump types, ramoops is currently designed
  353. * to only store crash logs, rather than storing general kernel logs.
  354. */
  355. if (record->reason != KMSG_DUMP_OOPS &&
  356. record->reason != KMSG_DUMP_PANIC)
  357. return -EINVAL;
  358. /* Skip Oopes when configured to do so. */
  359. if (record->reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
  360. return -EINVAL;
  361. /*
  362. * Explicitly only take the first part of any new crash.
  363. * If our buffer is larger than kmsg_bytes, this can never happen,
  364. * and if our buffer is smaller than kmsg_bytes, we don't want the
  365. * report split across multiple records.
  366. */
  367. if (record->part != 1)
  368. return -ENOSPC;
  369. if (!cxt->dprzs)
  370. return -ENOSPC;
  371. prz = cxt->dprzs[cxt->dump_write_cnt];
  372. /*
  373. * Since this is a new crash dump, we need to reset the buffer in
  374. * case it still has an old dump present. Without this, the new dump
  375. * will get appended, which would seriously confuse anything trying
  376. * to check dump file contents. Specifically, ramoops_read_kmsg_hdr()
  377. * expects to find a dump header in the beginning of buffer data, so
  378. * we must to reset the buffer values, in order to ensure that the
  379. * header will be written to the beginning of the buffer.
  380. */
  381. persistent_ram_zap(prz);
  382. /* Build header and append record contents. */
  383. hlen = ramoops_write_kmsg_hdr(prz, record);
  384. size = record->size;
  385. if (size + hlen > prz->buffer_size)
  386. size = prz->buffer_size - hlen;
  387. persistent_ram_write(prz, record->buf, size);
  388. cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
  389. return 0;
  390. }
  391. static int notrace ramoops_pstore_write_user(struct pstore_record *record,
  392. const char __user *buf)
  393. {
  394. if (record->type == PSTORE_TYPE_PMSG) {
  395. struct ramoops_context *cxt = record->psi->data;
  396. if (!cxt->mprz)
  397. return -ENOMEM;
  398. return persistent_ram_write_user(cxt->mprz, buf, record->size);
  399. }
  400. return -EINVAL;
  401. }
  402. static int ramoops_pstore_erase(struct pstore_record *record)
  403. {
  404. struct ramoops_context *cxt = record->psi->data;
  405. struct persistent_ram_zone *prz;
  406. switch (record->type) {
  407. case PSTORE_TYPE_DMESG:
  408. if (record->id >= cxt->max_dump_cnt)
  409. return -EINVAL;
  410. prz = cxt->dprzs[record->id];
  411. break;
  412. case PSTORE_TYPE_CONSOLE:
  413. prz = cxt->cprz;
  414. break;
  415. case PSTORE_TYPE_FTRACE:
  416. if (record->id >= cxt->max_ftrace_cnt)
  417. return -EINVAL;
  418. prz = cxt->fprzs[record->id];
  419. break;
  420. case PSTORE_TYPE_PMSG:
  421. prz = cxt->mprz;
  422. break;
  423. default:
  424. return -EINVAL;
  425. }
  426. persistent_ram_free_old(prz);
  427. persistent_ram_zap(prz);
  428. return 0;
  429. }
  430. static struct ramoops_context oops_cxt = {
  431. .pstore = {
  432. .owner = THIS_MODULE,
  433. .name = "ramoops",
  434. .open = ramoops_pstore_open,
  435. .read = ramoops_pstore_read,
  436. .write = ramoops_pstore_write,
  437. .write_user = ramoops_pstore_write_user,
  438. .erase = ramoops_pstore_erase,
  439. },
  440. };
  441. static void ramoops_free_przs(struct ramoops_context *cxt)
  442. {
  443. int i;
  444. /* Free dump PRZs */
  445. if (cxt->dprzs) {
  446. for (i = 0; i < cxt->max_dump_cnt; i++)
  447. persistent_ram_free(cxt->dprzs[i]);
  448. kfree(cxt->dprzs);
  449. cxt->max_dump_cnt = 0;
  450. }
  451. /* Free ftrace PRZs */
  452. if (cxt->fprzs) {
  453. for (i = 0; i < cxt->max_ftrace_cnt; i++)
  454. persistent_ram_free(cxt->fprzs[i]);
  455. kfree(cxt->fprzs);
  456. cxt->max_ftrace_cnt = 0;
  457. }
  458. }
  459. static int ramoops_init_przs(const char *name,
  460. struct device *dev, struct ramoops_context *cxt,
  461. struct persistent_ram_zone ***przs,
  462. phys_addr_t *paddr, size_t mem_sz,
  463. ssize_t record_size,
  464. unsigned int *cnt, u32 sig, u32 flags)
  465. {
  466. int err = -ENOMEM;
  467. int i;
  468. size_t zone_sz;
  469. struct persistent_ram_zone **prz_ar;
  470. /* Allocate nothing for 0 mem_sz or 0 record_size. */
  471. if (mem_sz == 0 || record_size == 0) {
  472. *cnt = 0;
  473. return 0;
  474. }
  475. /*
  476. * If we have a negative record size, calculate it based on
  477. * mem_sz / *cnt. If we have a positive record size, calculate
  478. * cnt from mem_sz / record_size.
  479. */
  480. if (record_size < 0) {
  481. if (*cnt == 0)
  482. return 0;
  483. record_size = mem_sz / *cnt;
  484. if (record_size == 0) {
  485. dev_err(dev, "%s record size == 0 (%zu / %u)\n",
  486. name, mem_sz, *cnt);
  487. goto fail;
  488. }
  489. } else {
  490. *cnt = mem_sz / record_size;
  491. if (*cnt == 0) {
  492. dev_err(dev, "%s record count == 0 (%zu / %zu)\n",
  493. name, mem_sz, record_size);
  494. goto fail;
  495. }
  496. }
  497. if (*paddr + mem_sz - cxt->phys_addr > cxt->size) {
  498. dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
  499. name,
  500. mem_sz, (unsigned long long)*paddr,
  501. cxt->size, (unsigned long long)cxt->phys_addr);
  502. goto fail;
  503. }
  504. zone_sz = mem_sz / *cnt;
  505. if (!zone_sz) {
  506. dev_err(dev, "%s zone size == 0\n", name);
  507. goto fail;
  508. }
  509. prz_ar = kcalloc(*cnt, sizeof(**przs), GFP_KERNEL);
  510. if (!prz_ar)
  511. goto fail;
  512. for (i = 0; i < *cnt; i++) {
  513. prz_ar[i] = persistent_ram_new(*paddr, zone_sz, sig,
  514. &cxt->ecc_info,
  515. cxt->memtype, flags);
  516. if (IS_ERR(prz_ar[i])) {
  517. err = PTR_ERR(prz_ar[i]);
  518. dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n",
  519. name, record_size,
  520. (unsigned long long)*paddr, err);
  521. while (i > 0) {
  522. i--;
  523. persistent_ram_free(prz_ar[i]);
  524. }
  525. kfree(prz_ar);
  526. goto fail;
  527. }
  528. *paddr += zone_sz;
  529. }
  530. *przs = prz_ar;
  531. return 0;
  532. fail:
  533. *cnt = 0;
  534. return err;
  535. }
  536. static int ramoops_init_prz(const char *name,
  537. struct device *dev, struct ramoops_context *cxt,
  538. struct persistent_ram_zone **prz,
  539. phys_addr_t *paddr, size_t sz, u32 sig)
  540. {
  541. if (!sz)
  542. return 0;
  543. if (*paddr + sz - cxt->phys_addr > cxt->size) {
  544. dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
  545. name, sz, (unsigned long long)*paddr,
  546. cxt->size, (unsigned long long)cxt->phys_addr);
  547. return -ENOMEM;
  548. }
  549. *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info,
  550. cxt->memtype, 0);
  551. if (IS_ERR(*prz)) {
  552. int err = PTR_ERR(*prz);
  553. dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n",
  554. name, sz, (unsigned long long)*paddr, err);
  555. return err;
  556. }
  557. persistent_ram_zap(*prz);
  558. *paddr += sz;
  559. return 0;
  560. }
  561. static int ramoops_parse_dt_size(struct platform_device *pdev,
  562. const char *propname, u32 *value)
  563. {
  564. u32 val32 = 0;
  565. int ret;
  566. ret = of_property_read_u32(pdev->dev.of_node, propname, &val32);
  567. if (ret < 0 && ret != -EINVAL) {
  568. dev_err(&pdev->dev, "failed to parse property %s: %d\n",
  569. propname, ret);
  570. return ret;
  571. }
  572. if (val32 > INT_MAX) {
  573. dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32);
  574. return -EOVERFLOW;
  575. }
  576. *value = val32;
  577. return 0;
  578. }
  579. static int ramoops_parse_dt(struct platform_device *pdev,
  580. struct ramoops_platform_data *pdata)
  581. {
  582. struct device_node *of_node = pdev->dev.of_node;
  583. struct resource *res;
  584. u32 value;
  585. int ret;
  586. dev_dbg(&pdev->dev, "using Device Tree\n");
  587. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  588. if (!res) {
  589. dev_err(&pdev->dev,
  590. "failed to locate DT /reserved-memory resource\n");
  591. return -EINVAL;
  592. }
  593. pdata->mem_size = resource_size(res);
  594. pdata->mem_address = res->start;
  595. pdata->mem_type = of_property_read_bool(of_node, "unbuffered");
  596. pdata->dump_oops = !of_property_read_bool(of_node, "no-dump-oops");
  597. #define parse_size(name, field) { \
  598. ret = ramoops_parse_dt_size(pdev, name, &value); \
  599. if (ret < 0) \
  600. return ret; \
  601. field = value; \
  602. }
  603. parse_size("record-size", pdata->record_size);
  604. parse_size("console-size", pdata->console_size);
  605. parse_size("ftrace-size", pdata->ftrace_size);
  606. parse_size("pmsg-size", pdata->pmsg_size);
  607. parse_size("ecc-size", pdata->ecc_info.ecc_size);
  608. parse_size("flags", pdata->flags);
  609. #undef parse_size
  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. if (dev_of_node(dev) && !pdata) {
  622. pdata = &pdata_local;
  623. memset(pdata, 0, sizeof(*pdata));
  624. err = ramoops_parse_dt(pdev, pdata);
  625. if (err < 0)
  626. goto fail_out;
  627. }
  628. /*
  629. * Only a single ramoops area allowed at a time, so fail extra
  630. * probes.
  631. */
  632. if (cxt->max_dump_cnt) {
  633. pr_err("already initialized\n");
  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. 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. goto fail_out;
  646. }
  647. if (pdata->record_size && !is_power_of_2(pdata->record_size))
  648. pdata->record_size = rounddown_pow_of_two(pdata->record_size);
  649. if (pdata->console_size && !is_power_of_2(pdata->console_size))
  650. pdata->console_size = rounddown_pow_of_two(pdata->console_size);
  651. if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size))
  652. pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
  653. if (pdata->pmsg_size && !is_power_of_2(pdata->pmsg_size))
  654. pdata->pmsg_size = rounddown_pow_of_two(pdata->pmsg_size);
  655. cxt->size = pdata->mem_size;
  656. cxt->phys_addr = pdata->mem_address;
  657. cxt->memtype = pdata->mem_type;
  658. cxt->record_size = pdata->record_size;
  659. cxt->console_size = pdata->console_size;
  660. cxt->ftrace_size = pdata->ftrace_size;
  661. cxt->pmsg_size = pdata->pmsg_size;
  662. cxt->dump_oops = pdata->dump_oops;
  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("dump", 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_out;
  673. err = ramoops_init_prz("console", dev, cxt, &cxt->cprz, &paddr,
  674. cxt->console_size, 0);
  675. if (err)
  676. goto fail_init_cprz;
  677. cxt->max_ftrace_cnt = (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
  678. ? nr_cpu_ids
  679. : 1;
  680. err = ramoops_init_przs("ftrace", dev, cxt, &cxt->fprzs, &paddr,
  681. cxt->ftrace_size, -1,
  682. &cxt->max_ftrace_cnt, LINUX_VERSION_CODE,
  683. (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)
  684. ? PRZ_FLAG_NO_LOCK : 0);
  685. if (err)
  686. goto fail_init_fprz;
  687. err = ramoops_init_prz("pmsg", dev, cxt, &cxt->mprz, &paddr,
  688. cxt->pmsg_size, 0);
  689. if (err)
  690. goto fail_init_mprz;
  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. if (cxt->console_size)
  702. cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
  703. if (cxt->max_ftrace_cnt)
  704. cxt->pstore.flags |= PSTORE_FLAGS_FTRACE;
  705. if (cxt->pmsg_size)
  706. cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
  707. /*
  708. * Since bufsize is only used for dmesg crash dumps, it
  709. * must match the size of the dprz record (after PRZ header
  710. * and ECC bytes have been accounted for).
  711. */
  712. if (cxt->pstore.flags & PSTORE_FLAGS_DMESG) {
  713. cxt->pstore.bufsize = cxt->dprzs[0]->buffer_size;
  714. cxt->pstore.buf = kzalloc(cxt->pstore.bufsize, GFP_KERNEL);
  715. if (!cxt->pstore.buf) {
  716. pr_err("cannot allocate pstore crash dump buffer\n");
  717. err = -ENOMEM;
  718. goto fail_clear;
  719. }
  720. }
  721. err = pstore_register(&cxt->pstore);
  722. if (err) {
  723. pr_err("registering with pstore failed\n");
  724. goto fail_buf;
  725. }
  726. /*
  727. * Update the module parameter variables as well so they are visible
  728. * through /sys/module/ramoops/parameters/
  729. */
  730. mem_size = pdata->mem_size;
  731. mem_address = pdata->mem_address;
  732. record_size = pdata->record_size;
  733. dump_oops = pdata->dump_oops;
  734. ramoops_console_size = pdata->console_size;
  735. ramoops_pmsg_size = pdata->pmsg_size;
  736. ramoops_ftrace_size = pdata->ftrace_size;
  737. pr_info("attached 0x%lx@0x%llx, ecc: %d/%d\n",
  738. cxt->size, (unsigned long long)cxt->phys_addr,
  739. cxt->ecc_info.ecc_size, cxt->ecc_info.block_size);
  740. return 0;
  741. fail_buf:
  742. kfree(cxt->pstore.buf);
  743. fail_clear:
  744. cxt->pstore.bufsize = 0;
  745. persistent_ram_free(cxt->mprz);
  746. fail_init_mprz:
  747. fail_init_fprz:
  748. persistent_ram_free(cxt->cprz);
  749. fail_init_cprz:
  750. ramoops_free_przs(cxt);
  751. fail_out:
  752. return err;
  753. }
  754. static int ramoops_remove(struct platform_device *pdev)
  755. {
  756. struct ramoops_context *cxt = &oops_cxt;
  757. pstore_unregister(&cxt->pstore);
  758. kfree(cxt->pstore.buf);
  759. cxt->pstore.bufsize = 0;
  760. persistent_ram_free(cxt->mprz);
  761. persistent_ram_free(cxt->cprz);
  762. ramoops_free_przs(cxt);
  763. return 0;
  764. }
  765. static const struct of_device_id dt_match[] = {
  766. { .compatible = "ramoops" },
  767. {}
  768. };
  769. static struct platform_driver ramoops_driver = {
  770. .probe = ramoops_probe,
  771. .remove = ramoops_remove,
  772. .driver = {
  773. .name = "ramoops",
  774. .of_match_table = dt_match,
  775. },
  776. };
  777. static inline void ramoops_unregister_dummy(void)
  778. {
  779. platform_device_unregister(dummy);
  780. dummy = NULL;
  781. kfree(dummy_data);
  782. dummy_data = NULL;
  783. }
  784. static void __init ramoops_register_dummy(void)
  785. {
  786. /*
  787. * Prepare a dummy platform data structure to carry the module
  788. * parameters. If mem_size isn't set, then there are no module
  789. * parameters, and we can skip this.
  790. */
  791. if (!mem_size)
  792. return;
  793. pr_info("using module parameters\n");
  794. dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
  795. if (!dummy_data) {
  796. pr_info("could not allocate pdata\n");
  797. return;
  798. }
  799. dummy_data->mem_size = mem_size;
  800. dummy_data->mem_address = mem_address;
  801. dummy_data->mem_type = mem_type;
  802. dummy_data->record_size = record_size;
  803. dummy_data->console_size = ramoops_console_size;
  804. dummy_data->ftrace_size = ramoops_ftrace_size;
  805. dummy_data->pmsg_size = ramoops_pmsg_size;
  806. dummy_data->dump_oops = dump_oops;
  807. dummy_data->flags = RAMOOPS_FLAG_FTRACE_PER_CPU;
  808. /*
  809. * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
  810. * (using 1 byte for ECC isn't much of use anyway).
  811. */
  812. dummy_data->ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
  813. dummy = platform_device_register_data(NULL, "ramoops", -1,
  814. dummy_data, sizeof(struct ramoops_platform_data));
  815. if (IS_ERR(dummy)) {
  816. pr_info("could not create platform device: %ld\n",
  817. PTR_ERR(dummy));
  818. dummy = NULL;
  819. ramoops_unregister_dummy();
  820. }
  821. }
  822. static int __init ramoops_init(void)
  823. {
  824. int ret;
  825. ramoops_register_dummy();
  826. ret = platform_driver_register(&ramoops_driver);
  827. if (ret != 0)
  828. ramoops_unregister_dummy();
  829. return ret;
  830. }
  831. postcore_initcall(ramoops_init);
  832. static void __exit ramoops_exit(void)
  833. {
  834. platform_driver_unregister(&ramoops_driver);
  835. ramoops_unregister_dummy();
  836. }
  837. module_exit(ramoops_exit);
  838. MODULE_LICENSE("GPL");
  839. MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
  840. MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");