ram.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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/time.h>
  29. #include <linux/io.h>
  30. #include <linux/ioport.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/slab.h>
  33. #include <linux/compiler.h>
  34. #include <linux/pstore_ram.h>
  35. #include <linux/of.h>
  36. #include <linux/of_address.h>
  37. #define RAMOOPS_KERNMSG_HDR "===="
  38. #define MIN_MEM_SIZE 4096UL
  39. static ulong record_size = MIN_MEM_SIZE;
  40. module_param(record_size, ulong, 0400);
  41. MODULE_PARM_DESC(record_size,
  42. "size of each dump done on oops/panic");
  43. static ulong ramoops_console_size = MIN_MEM_SIZE;
  44. module_param_named(console_size, ramoops_console_size, ulong, 0400);
  45. MODULE_PARM_DESC(console_size, "size of kernel console log");
  46. static ulong ramoops_ftrace_size = MIN_MEM_SIZE;
  47. module_param_named(ftrace_size, ramoops_ftrace_size, ulong, 0400);
  48. MODULE_PARM_DESC(ftrace_size, "size of ftrace log");
  49. static ulong ramoops_pmsg_size = MIN_MEM_SIZE;
  50. module_param_named(pmsg_size, ramoops_pmsg_size, ulong, 0400);
  51. MODULE_PARM_DESC(pmsg_size, "size of user space message log");
  52. static unsigned long long mem_address;
  53. module_param(mem_address, ullong, 0400);
  54. MODULE_PARM_DESC(mem_address,
  55. "start of reserved RAM used to store oops/panic logs");
  56. static ulong mem_size;
  57. module_param(mem_size, ulong, 0400);
  58. MODULE_PARM_DESC(mem_size,
  59. "size of reserved RAM used to store oops/panic logs");
  60. static unsigned int mem_type;
  61. module_param(mem_type, uint, 0600);
  62. MODULE_PARM_DESC(mem_type,
  63. "set to 1 to try to use unbuffered memory (default 0)");
  64. static int dump_oops = 1;
  65. module_param(dump_oops, int, 0600);
  66. MODULE_PARM_DESC(dump_oops,
  67. "set to 1 to dump oopses, 0 to only dump panics (default 1)");
  68. static int ramoops_ecc;
  69. module_param_named(ecc, ramoops_ecc, int, 0600);
  70. MODULE_PARM_DESC(ramoops_ecc,
  71. "if non-zero, the option enables ECC support and specifies "
  72. "ECC buffer size in bytes (1 is a special value, means 16 "
  73. "bytes ECC)");
  74. struct ramoops_context {
  75. struct persistent_ram_zone **przs;
  76. struct persistent_ram_zone *cprz;
  77. struct persistent_ram_zone *fprz;
  78. struct persistent_ram_zone *mprz;
  79. phys_addr_t phys_addr;
  80. unsigned long size;
  81. unsigned int memtype;
  82. size_t record_size;
  83. size_t console_size;
  84. size_t ftrace_size;
  85. size_t pmsg_size;
  86. int dump_oops;
  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 ftrace_read_cnt;
  94. unsigned int pmsg_read_cnt;
  95. struct pstore_info pstore;
  96. };
  97. static struct platform_device *dummy;
  98. static struct ramoops_platform_data *dummy_data;
  99. static int ramoops_pstore_open(struct pstore_info *psi)
  100. {
  101. struct ramoops_context *cxt = psi->data;
  102. cxt->dump_read_cnt = 0;
  103. cxt->console_read_cnt = 0;
  104. cxt->ftrace_read_cnt = 0;
  105. cxt->pmsg_read_cnt = 0;
  106. return 0;
  107. }
  108. static struct persistent_ram_zone *
  109. ramoops_get_next_prz(struct persistent_ram_zone *przs[], uint *c, uint max,
  110. u64 *id,
  111. enum pstore_type_id *typep, enum pstore_type_id type,
  112. bool update)
  113. {
  114. struct persistent_ram_zone *prz;
  115. int i = (*c)++;
  116. if (i >= max)
  117. return NULL;
  118. prz = przs[i];
  119. if (!prz)
  120. return NULL;
  121. /* Update old/shadowed buffer. */
  122. if (update)
  123. persistent_ram_save_old(prz);
  124. if (!persistent_ram_old_size(prz))
  125. return NULL;
  126. *typep = type;
  127. *id = i;
  128. return prz;
  129. }
  130. static int ramoops_read_kmsg_hdr(char *buffer, struct timespec *time,
  131. bool *compressed)
  132. {
  133. char data_type;
  134. int header_length = 0;
  135. if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n%n", &time->tv_sec,
  136. &time->tv_nsec, &data_type, &header_length) == 3) {
  137. if (data_type == 'C')
  138. *compressed = true;
  139. else
  140. *compressed = false;
  141. } else if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lu.%lu\n%n",
  142. &time->tv_sec, &time->tv_nsec, &header_length) == 2) {
  143. *compressed = false;
  144. } else {
  145. time->tv_sec = 0;
  146. time->tv_nsec = 0;
  147. *compressed = false;
  148. }
  149. return header_length;
  150. }
  151. static bool prz_ok(struct persistent_ram_zone *prz)
  152. {
  153. return !!prz && !!(persistent_ram_old_size(prz) +
  154. persistent_ram_ecc_string(prz, NULL, 0));
  155. }
  156. static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
  157. int *count, struct timespec *time,
  158. char **buf, bool *compressed,
  159. ssize_t *ecc_notice_size,
  160. struct pstore_info *psi)
  161. {
  162. ssize_t size;
  163. struct ramoops_context *cxt = psi->data;
  164. struct persistent_ram_zone *prz = NULL;
  165. int header_length = 0;
  166. /* Ramoops headers provide time stamps for PSTORE_TYPE_DMESG, but
  167. * PSTORE_TYPE_CONSOLE and PSTORE_TYPE_FTRACE don't currently have
  168. * valid time stamps, so it is initialized to zero.
  169. */
  170. time->tv_sec = 0;
  171. time->tv_nsec = 0;
  172. *compressed = false;
  173. /* Find the next valid persistent_ram_zone for DMESG */
  174. while (cxt->dump_read_cnt < cxt->max_dump_cnt && !prz) {
  175. prz = ramoops_get_next_prz(cxt->przs, &cxt->dump_read_cnt,
  176. cxt->max_dump_cnt, id, type,
  177. PSTORE_TYPE_DMESG, 1);
  178. if (!prz_ok(prz))
  179. continue;
  180. header_length = ramoops_read_kmsg_hdr(persistent_ram_old(prz),
  181. time, compressed);
  182. /* Clear and skip this DMESG record if it has no valid header */
  183. if (!header_length) {
  184. persistent_ram_free_old(prz);
  185. persistent_ram_zap(prz);
  186. prz = NULL;
  187. }
  188. }
  189. if (!prz_ok(prz))
  190. prz = ramoops_get_next_prz(&cxt->cprz, &cxt->console_read_cnt,
  191. 1, id, type, PSTORE_TYPE_CONSOLE, 0);
  192. if (!prz_ok(prz))
  193. prz = ramoops_get_next_prz(&cxt->fprz, &cxt->ftrace_read_cnt,
  194. 1, id, type, PSTORE_TYPE_FTRACE, 0);
  195. if (!prz_ok(prz))
  196. prz = ramoops_get_next_prz(&cxt->mprz, &cxt->pmsg_read_cnt,
  197. 1, id, type, PSTORE_TYPE_PMSG, 0);
  198. if (!prz_ok(prz))
  199. return 0;
  200. size = persistent_ram_old_size(prz) - header_length;
  201. /* ECC correction notice */
  202. *ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0);
  203. *buf = kmalloc(size + *ecc_notice_size + 1, GFP_KERNEL);
  204. if (*buf == NULL)
  205. return -ENOMEM;
  206. memcpy(*buf, (char *)persistent_ram_old(prz) + header_length, size);
  207. persistent_ram_ecc_string(prz, *buf + size, *ecc_notice_size + 1);
  208. return size;
  209. }
  210. static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz,
  211. bool compressed)
  212. {
  213. char *hdr;
  214. struct timespec timestamp;
  215. size_t len;
  216. /* Report zeroed timestamp if called before timekeeping has resumed. */
  217. if (__getnstimeofday(&timestamp)) {
  218. timestamp.tv_sec = 0;
  219. timestamp.tv_nsec = 0;
  220. }
  221. hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n",
  222. (long)timestamp.tv_sec, (long)(timestamp.tv_nsec / 1000),
  223. compressed ? 'C' : 'D');
  224. WARN_ON_ONCE(!hdr);
  225. len = hdr ? strlen(hdr) : 0;
  226. persistent_ram_write(prz, hdr, len);
  227. kfree(hdr);
  228. return len;
  229. }
  230. static int notrace ramoops_pstore_write_buf(enum pstore_type_id type,
  231. enum kmsg_dump_reason reason,
  232. u64 *id, unsigned int part,
  233. const char *buf,
  234. bool compressed, size_t size,
  235. struct pstore_info *psi)
  236. {
  237. struct ramoops_context *cxt = psi->data;
  238. struct persistent_ram_zone *prz;
  239. size_t hlen;
  240. if (type == PSTORE_TYPE_CONSOLE) {
  241. if (!cxt->cprz)
  242. return -ENOMEM;
  243. persistent_ram_write(cxt->cprz, buf, size);
  244. return 0;
  245. } else if (type == PSTORE_TYPE_FTRACE) {
  246. if (!cxt->fprz)
  247. return -ENOMEM;
  248. persistent_ram_write(cxt->fprz, buf, size);
  249. return 0;
  250. } else if (type == PSTORE_TYPE_PMSG) {
  251. if (!cxt->mprz)
  252. return -ENOMEM;
  253. persistent_ram_write(cxt->mprz, buf, size);
  254. return 0;
  255. }
  256. if (type != PSTORE_TYPE_DMESG)
  257. return -EINVAL;
  258. /* Out of the various dmesg dump types, ramoops is currently designed
  259. * to only store crash logs, rather than storing general kernel logs.
  260. */
  261. if (reason != KMSG_DUMP_OOPS &&
  262. reason != KMSG_DUMP_PANIC)
  263. return -EINVAL;
  264. /* Skip Oopes when configured to do so. */
  265. if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
  266. return -EINVAL;
  267. /* Explicitly only take the first part of any new crash.
  268. * If our buffer is larger than kmsg_bytes, this can never happen,
  269. * and if our buffer is smaller than kmsg_bytes, we don't want the
  270. * report split across multiple records.
  271. */
  272. if (part != 1)
  273. return -ENOSPC;
  274. if (!cxt->przs)
  275. return -ENOSPC;
  276. prz = cxt->przs[cxt->dump_write_cnt];
  277. hlen = ramoops_write_kmsg_hdr(prz, compressed);
  278. if (size + hlen > prz->buffer_size)
  279. size = prz->buffer_size - hlen;
  280. persistent_ram_write(prz, buf, size);
  281. cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
  282. return 0;
  283. }
  284. static int notrace ramoops_pstore_write_buf_user(enum pstore_type_id type,
  285. enum kmsg_dump_reason reason,
  286. u64 *id, unsigned int part,
  287. const char __user *buf,
  288. bool compressed, size_t size,
  289. struct pstore_info *psi)
  290. {
  291. if (type == PSTORE_TYPE_PMSG) {
  292. struct ramoops_context *cxt = psi->data;
  293. if (!cxt->mprz)
  294. return -ENOMEM;
  295. return persistent_ram_write_user(cxt->mprz, buf, size);
  296. }
  297. return -EINVAL;
  298. }
  299. static int ramoops_pstore_erase(enum pstore_type_id type, u64 id, int count,
  300. struct timespec time, struct pstore_info *psi)
  301. {
  302. struct ramoops_context *cxt = psi->data;
  303. struct persistent_ram_zone *prz;
  304. switch (type) {
  305. case PSTORE_TYPE_DMESG:
  306. if (id >= cxt->max_dump_cnt)
  307. return -EINVAL;
  308. prz = cxt->przs[id];
  309. break;
  310. case PSTORE_TYPE_CONSOLE:
  311. prz = cxt->cprz;
  312. break;
  313. case PSTORE_TYPE_FTRACE:
  314. prz = cxt->fprz;
  315. break;
  316. case PSTORE_TYPE_PMSG:
  317. prz = cxt->mprz;
  318. break;
  319. default:
  320. return -EINVAL;
  321. }
  322. persistent_ram_free_old(prz);
  323. persistent_ram_zap(prz);
  324. return 0;
  325. }
  326. static struct ramoops_context oops_cxt = {
  327. .pstore = {
  328. .owner = THIS_MODULE,
  329. .name = "ramoops",
  330. .open = ramoops_pstore_open,
  331. .read = ramoops_pstore_read,
  332. .write_buf = ramoops_pstore_write_buf,
  333. .write_buf_user = ramoops_pstore_write_buf_user,
  334. .erase = ramoops_pstore_erase,
  335. },
  336. };
  337. static void ramoops_free_przs(struct ramoops_context *cxt)
  338. {
  339. int i;
  340. if (!cxt->przs)
  341. return;
  342. for (i = 0; i < cxt->max_dump_cnt; i++)
  343. persistent_ram_free(cxt->przs[i]);
  344. kfree(cxt->przs);
  345. cxt->max_dump_cnt = 0;
  346. }
  347. static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
  348. phys_addr_t *paddr, size_t dump_mem_sz)
  349. {
  350. int err = -ENOMEM;
  351. int i;
  352. if (!cxt->record_size)
  353. return 0;
  354. if (*paddr + dump_mem_sz - cxt->phys_addr > cxt->size) {
  355. dev_err(dev, "no room for dumps\n");
  356. return -ENOMEM;
  357. }
  358. cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
  359. if (!cxt->max_dump_cnt)
  360. return -ENOMEM;
  361. cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
  362. GFP_KERNEL);
  363. if (!cxt->przs) {
  364. dev_err(dev, "failed to initialize a prz array for dumps\n");
  365. goto fail_mem;
  366. }
  367. for (i = 0; i < cxt->max_dump_cnt; i++) {
  368. cxt->przs[i] = persistent_ram_new(*paddr, cxt->record_size, 0,
  369. &cxt->ecc_info,
  370. cxt->memtype, 0);
  371. if (IS_ERR(cxt->przs[i])) {
  372. err = PTR_ERR(cxt->przs[i]);
  373. dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
  374. cxt->record_size, (unsigned long long)*paddr, err);
  375. while (i > 0) {
  376. i--;
  377. persistent_ram_free(cxt->przs[i]);
  378. }
  379. goto fail_prz;
  380. }
  381. *paddr += cxt->record_size;
  382. }
  383. return 0;
  384. fail_prz:
  385. kfree(cxt->przs);
  386. fail_mem:
  387. cxt->max_dump_cnt = 0;
  388. return err;
  389. }
  390. static int ramoops_init_prz(struct device *dev, struct ramoops_context *cxt,
  391. struct persistent_ram_zone **prz,
  392. phys_addr_t *paddr, size_t sz, u32 sig)
  393. {
  394. if (!sz)
  395. return 0;
  396. if (*paddr + sz - cxt->phys_addr > cxt->size) {
  397. dev_err(dev, "no room for mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
  398. sz, (unsigned long long)*paddr,
  399. cxt->size, (unsigned long long)cxt->phys_addr);
  400. return -ENOMEM;
  401. }
  402. *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info,
  403. cxt->memtype, 0);
  404. if (IS_ERR(*prz)) {
  405. int err = PTR_ERR(*prz);
  406. dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
  407. sz, (unsigned long long)*paddr, err);
  408. return err;
  409. }
  410. persistent_ram_zap(*prz);
  411. *paddr += sz;
  412. return 0;
  413. }
  414. static int ramoops_parse_dt_size(struct platform_device *pdev,
  415. const char *propname, u32 *value)
  416. {
  417. u32 val32 = 0;
  418. int ret;
  419. ret = of_property_read_u32(pdev->dev.of_node, propname, &val32);
  420. if (ret < 0 && ret != -EINVAL) {
  421. dev_err(&pdev->dev, "failed to parse property %s: %d\n",
  422. propname, ret);
  423. return ret;
  424. }
  425. if (val32 > INT_MAX) {
  426. dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32);
  427. return -EOVERFLOW;
  428. }
  429. *value = val32;
  430. return 0;
  431. }
  432. static int ramoops_parse_dt(struct platform_device *pdev,
  433. struct ramoops_platform_data *pdata)
  434. {
  435. struct device_node *of_node = pdev->dev.of_node;
  436. struct resource *res;
  437. u32 value;
  438. int ret;
  439. dev_dbg(&pdev->dev, "using Device Tree\n");
  440. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  441. if (!res) {
  442. dev_err(&pdev->dev,
  443. "failed to locate DT /reserved-memory resource\n");
  444. return -EINVAL;
  445. }
  446. pdata->mem_size = resource_size(res);
  447. pdata->mem_address = res->start;
  448. pdata->mem_type = of_property_read_bool(of_node, "unbuffered");
  449. pdata->dump_oops = !of_property_read_bool(of_node, "no-dump-oops");
  450. #define parse_size(name, field) { \
  451. ret = ramoops_parse_dt_size(pdev, name, &value); \
  452. if (ret < 0) \
  453. return ret; \
  454. field = value; \
  455. }
  456. parse_size("record-size", pdata->record_size);
  457. parse_size("console-size", pdata->console_size);
  458. parse_size("ftrace-size", pdata->ftrace_size);
  459. parse_size("pmsg-size", pdata->pmsg_size);
  460. parse_size("ecc-size", pdata->ecc_info.ecc_size);
  461. #undef parse_size
  462. return 0;
  463. }
  464. static int ramoops_probe(struct platform_device *pdev)
  465. {
  466. struct device *dev = &pdev->dev;
  467. struct ramoops_platform_data *pdata = dev->platform_data;
  468. struct ramoops_context *cxt = &oops_cxt;
  469. size_t dump_mem_sz;
  470. phys_addr_t paddr;
  471. int err = -EINVAL;
  472. if (dev_of_node(dev) && !pdata) {
  473. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  474. if (!pdata) {
  475. err = -ENOMEM;
  476. goto fail_out;
  477. }
  478. err = ramoops_parse_dt(pdev, pdata);
  479. if (err < 0)
  480. goto fail_out;
  481. }
  482. /* Only a single ramoops area allowed at a time, so fail extra
  483. * probes.
  484. */
  485. if (cxt->max_dump_cnt)
  486. goto fail_out;
  487. if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size &&
  488. !pdata->ftrace_size && !pdata->pmsg_size)) {
  489. pr_err("The memory size and the record/console size must be "
  490. "non-zero\n");
  491. goto fail_out;
  492. }
  493. if (pdata->record_size && !is_power_of_2(pdata->record_size))
  494. pdata->record_size = rounddown_pow_of_two(pdata->record_size);
  495. if (pdata->console_size && !is_power_of_2(pdata->console_size))
  496. pdata->console_size = rounddown_pow_of_two(pdata->console_size);
  497. if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size))
  498. pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
  499. if (pdata->pmsg_size && !is_power_of_2(pdata->pmsg_size))
  500. pdata->pmsg_size = rounddown_pow_of_two(pdata->pmsg_size);
  501. cxt->size = pdata->mem_size;
  502. cxt->phys_addr = pdata->mem_address;
  503. cxt->memtype = pdata->mem_type;
  504. cxt->record_size = pdata->record_size;
  505. cxt->console_size = pdata->console_size;
  506. cxt->ftrace_size = pdata->ftrace_size;
  507. cxt->pmsg_size = pdata->pmsg_size;
  508. cxt->dump_oops = pdata->dump_oops;
  509. cxt->ecc_info = pdata->ecc_info;
  510. paddr = cxt->phys_addr;
  511. dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size
  512. - cxt->pmsg_size;
  513. err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
  514. if (err)
  515. goto fail_out;
  516. err = ramoops_init_prz(dev, cxt, &cxt->cprz, &paddr,
  517. cxt->console_size, 0);
  518. if (err)
  519. goto fail_init_cprz;
  520. err = ramoops_init_prz(dev, cxt, &cxt->fprz, &paddr, cxt->ftrace_size,
  521. LINUX_VERSION_CODE);
  522. if (err)
  523. goto fail_init_fprz;
  524. err = ramoops_init_prz(dev, cxt, &cxt->mprz, &paddr, cxt->pmsg_size, 0);
  525. if (err)
  526. goto fail_init_mprz;
  527. cxt->pstore.data = cxt;
  528. /*
  529. * Console can handle any buffer size, so prefer LOG_LINE_MAX. If we
  530. * have to handle dumps, we must have at least record_size buffer. And
  531. * for ftrace, bufsize is irrelevant (if bufsize is 0, buf will be
  532. * ZERO_SIZE_PTR).
  533. */
  534. if (cxt->console_size)
  535. cxt->pstore.bufsize = 1024; /* LOG_LINE_MAX */
  536. cxt->pstore.bufsize = max(cxt->record_size, cxt->pstore.bufsize);
  537. cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
  538. if (!cxt->pstore.buf) {
  539. pr_err("cannot allocate pstore buffer\n");
  540. err = -ENOMEM;
  541. goto fail_clear;
  542. }
  543. spin_lock_init(&cxt->pstore.buf_lock);
  544. cxt->pstore.flags = PSTORE_FLAGS_DMESG;
  545. if (cxt->console_size)
  546. cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
  547. if (cxt->ftrace_size)
  548. cxt->pstore.flags |= PSTORE_FLAGS_FTRACE;
  549. if (cxt->pmsg_size)
  550. cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
  551. err = pstore_register(&cxt->pstore);
  552. if (err) {
  553. pr_err("registering with pstore failed\n");
  554. goto fail_buf;
  555. }
  556. /*
  557. * Update the module parameter variables as well so they are visible
  558. * through /sys/module/ramoops/parameters/
  559. */
  560. mem_size = pdata->mem_size;
  561. mem_address = pdata->mem_address;
  562. record_size = pdata->record_size;
  563. dump_oops = pdata->dump_oops;
  564. ramoops_console_size = pdata->console_size;
  565. ramoops_pmsg_size = pdata->pmsg_size;
  566. ramoops_ftrace_size = pdata->ftrace_size;
  567. pr_info("attached 0x%lx@0x%llx, ecc: %d/%d\n",
  568. cxt->size, (unsigned long long)cxt->phys_addr,
  569. cxt->ecc_info.ecc_size, cxt->ecc_info.block_size);
  570. return 0;
  571. fail_buf:
  572. kfree(cxt->pstore.buf);
  573. fail_clear:
  574. cxt->pstore.bufsize = 0;
  575. persistent_ram_free(cxt->mprz);
  576. fail_init_mprz:
  577. persistent_ram_free(cxt->fprz);
  578. fail_init_fprz:
  579. persistent_ram_free(cxt->cprz);
  580. fail_init_cprz:
  581. ramoops_free_przs(cxt);
  582. fail_out:
  583. return err;
  584. }
  585. static int ramoops_remove(struct platform_device *pdev)
  586. {
  587. struct ramoops_context *cxt = &oops_cxt;
  588. pstore_unregister(&cxt->pstore);
  589. kfree(cxt->pstore.buf);
  590. cxt->pstore.bufsize = 0;
  591. persistent_ram_free(cxt->mprz);
  592. persistent_ram_free(cxt->fprz);
  593. persistent_ram_free(cxt->cprz);
  594. ramoops_free_przs(cxt);
  595. return 0;
  596. }
  597. static const struct of_device_id dt_match[] = {
  598. { .compatible = "ramoops" },
  599. {}
  600. };
  601. static struct platform_driver ramoops_driver = {
  602. .probe = ramoops_probe,
  603. .remove = ramoops_remove,
  604. .driver = {
  605. .name = "ramoops",
  606. .of_match_table = dt_match,
  607. },
  608. };
  609. static void ramoops_register_dummy(void)
  610. {
  611. if (!mem_size)
  612. return;
  613. pr_info("using module parameters\n");
  614. dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
  615. if (!dummy_data) {
  616. pr_info("could not allocate pdata\n");
  617. return;
  618. }
  619. dummy_data->mem_size = mem_size;
  620. dummy_data->mem_address = mem_address;
  621. dummy_data->mem_type = mem_type;
  622. dummy_data->record_size = record_size;
  623. dummy_data->console_size = ramoops_console_size;
  624. dummy_data->ftrace_size = ramoops_ftrace_size;
  625. dummy_data->pmsg_size = ramoops_pmsg_size;
  626. dummy_data->dump_oops = dump_oops;
  627. /*
  628. * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
  629. * (using 1 byte for ECC isn't much of use anyway).
  630. */
  631. dummy_data->ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
  632. dummy = platform_device_register_data(NULL, "ramoops", -1,
  633. dummy_data, sizeof(struct ramoops_platform_data));
  634. if (IS_ERR(dummy)) {
  635. pr_info("could not create platform device: %ld\n",
  636. PTR_ERR(dummy));
  637. }
  638. }
  639. static int __init ramoops_init(void)
  640. {
  641. ramoops_register_dummy();
  642. return platform_driver_register(&ramoops_driver);
  643. }
  644. postcore_initcall(ramoops_init);
  645. static void __exit ramoops_exit(void)
  646. {
  647. platform_driver_unregister(&ramoops_driver);
  648. platform_device_unregister(dummy);
  649. kfree(dummy_data);
  650. }
  651. module_exit(ramoops_exit);
  652. MODULE_LICENSE("GPL");
  653. MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
  654. MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");