ram.c 24 KB

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