ram.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 ramoops_pstore_erase(enum pstore_type_id type, u64 id, int count,
  285. struct timespec time, struct pstore_info *psi)
  286. {
  287. struct ramoops_context *cxt = psi->data;
  288. struct persistent_ram_zone *prz;
  289. switch (type) {
  290. case PSTORE_TYPE_DMESG:
  291. if (id >= cxt->max_dump_cnt)
  292. return -EINVAL;
  293. prz = cxt->przs[id];
  294. break;
  295. case PSTORE_TYPE_CONSOLE:
  296. prz = cxt->cprz;
  297. break;
  298. case PSTORE_TYPE_FTRACE:
  299. prz = cxt->fprz;
  300. break;
  301. case PSTORE_TYPE_PMSG:
  302. prz = cxt->mprz;
  303. break;
  304. default:
  305. return -EINVAL;
  306. }
  307. persistent_ram_free_old(prz);
  308. persistent_ram_zap(prz);
  309. return 0;
  310. }
  311. static struct ramoops_context oops_cxt = {
  312. .pstore = {
  313. .owner = THIS_MODULE,
  314. .name = "ramoops",
  315. .open = ramoops_pstore_open,
  316. .read = ramoops_pstore_read,
  317. .write_buf = ramoops_pstore_write_buf,
  318. .erase = ramoops_pstore_erase,
  319. },
  320. };
  321. static void ramoops_free_przs(struct ramoops_context *cxt)
  322. {
  323. int i;
  324. cxt->max_dump_cnt = 0;
  325. if (!cxt->przs)
  326. return;
  327. for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
  328. persistent_ram_free(cxt->przs[i]);
  329. kfree(cxt->przs);
  330. }
  331. static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
  332. phys_addr_t *paddr, size_t dump_mem_sz)
  333. {
  334. int err = -ENOMEM;
  335. int i;
  336. if (!cxt->record_size)
  337. return 0;
  338. if (*paddr + dump_mem_sz - cxt->phys_addr > cxt->size) {
  339. dev_err(dev, "no room for dumps\n");
  340. return -ENOMEM;
  341. }
  342. cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
  343. if (!cxt->max_dump_cnt)
  344. return -ENOMEM;
  345. cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
  346. GFP_KERNEL);
  347. if (!cxt->przs) {
  348. dev_err(dev, "failed to initialize a prz array for dumps\n");
  349. goto fail_prz;
  350. }
  351. for (i = 0; i < cxt->max_dump_cnt; i++) {
  352. cxt->przs[i] = persistent_ram_new(*paddr, cxt->record_size, 0,
  353. &cxt->ecc_info,
  354. cxt->memtype);
  355. if (IS_ERR(cxt->przs[i])) {
  356. err = PTR_ERR(cxt->przs[i]);
  357. dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
  358. cxt->record_size, (unsigned long long)*paddr, err);
  359. goto fail_prz;
  360. }
  361. *paddr += cxt->record_size;
  362. }
  363. return 0;
  364. fail_prz:
  365. ramoops_free_przs(cxt);
  366. return err;
  367. }
  368. static int ramoops_init_prz(struct device *dev, struct ramoops_context *cxt,
  369. struct persistent_ram_zone **prz,
  370. phys_addr_t *paddr, size_t sz, u32 sig)
  371. {
  372. if (!sz)
  373. return 0;
  374. if (*paddr + sz - cxt->phys_addr > cxt->size) {
  375. dev_err(dev, "no room for mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
  376. sz, (unsigned long long)*paddr,
  377. cxt->size, (unsigned long long)cxt->phys_addr);
  378. return -ENOMEM;
  379. }
  380. *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info, cxt->memtype);
  381. if (IS_ERR(*prz)) {
  382. int err = PTR_ERR(*prz);
  383. dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
  384. sz, (unsigned long long)*paddr, err);
  385. return err;
  386. }
  387. persistent_ram_zap(*prz);
  388. *paddr += sz;
  389. return 0;
  390. }
  391. static int ramoops_parse_dt_size(struct platform_device *pdev,
  392. const char *propname, u32 *value)
  393. {
  394. u32 val32 = 0;
  395. int ret;
  396. ret = of_property_read_u32(pdev->dev.of_node, propname, &val32);
  397. if (ret < 0 && ret != -EINVAL) {
  398. dev_err(&pdev->dev, "failed to parse property %s: %d\n",
  399. propname, ret);
  400. return ret;
  401. }
  402. if (val32 > INT_MAX) {
  403. dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32);
  404. return -EOVERFLOW;
  405. }
  406. *value = val32;
  407. return 0;
  408. }
  409. static int ramoops_parse_dt(struct platform_device *pdev,
  410. struct ramoops_platform_data *pdata)
  411. {
  412. struct device_node *of_node = pdev->dev.of_node;
  413. struct resource *res;
  414. u32 value;
  415. int ret;
  416. dev_dbg(&pdev->dev, "using Device Tree\n");
  417. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  418. if (!res) {
  419. dev_err(&pdev->dev,
  420. "failed to locate DT /reserved-memory resource\n");
  421. return -EINVAL;
  422. }
  423. pdata->mem_size = resource_size(res);
  424. pdata->mem_address = res->start;
  425. pdata->mem_type = of_property_read_bool(of_node, "unbuffered");
  426. pdata->dump_oops = !of_property_read_bool(of_node, "no-dump-oops");
  427. #define parse_size(name, field) { \
  428. ret = ramoops_parse_dt_size(pdev, name, &value); \
  429. if (ret < 0) \
  430. return ret; \
  431. field = value; \
  432. }
  433. parse_size("record-size", pdata->record_size);
  434. parse_size("console-size", pdata->console_size);
  435. parse_size("ftrace-size", pdata->ftrace_size);
  436. parse_size("pmsg-size", pdata->pmsg_size);
  437. parse_size("ecc-size", pdata->ecc_info.ecc_size);
  438. #undef parse_size
  439. return 0;
  440. }
  441. static int ramoops_probe(struct platform_device *pdev)
  442. {
  443. struct device *dev = &pdev->dev;
  444. struct ramoops_platform_data *pdata = dev->platform_data;
  445. struct ramoops_context *cxt = &oops_cxt;
  446. size_t dump_mem_sz;
  447. phys_addr_t paddr;
  448. int err = -EINVAL;
  449. if (dev_of_node(dev) && !pdata) {
  450. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  451. if (!pdata) {
  452. err = -ENOMEM;
  453. goto fail_out;
  454. }
  455. err = ramoops_parse_dt(pdev, pdata);
  456. if (err < 0)
  457. goto fail_out;
  458. }
  459. /* Only a single ramoops area allowed at a time, so fail extra
  460. * probes.
  461. */
  462. if (cxt->max_dump_cnt)
  463. goto fail_out;
  464. if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size &&
  465. !pdata->ftrace_size && !pdata->pmsg_size)) {
  466. pr_err("The memory size and the record/console size must be "
  467. "non-zero\n");
  468. goto fail_out;
  469. }
  470. if (pdata->record_size && !is_power_of_2(pdata->record_size))
  471. pdata->record_size = rounddown_pow_of_two(pdata->record_size);
  472. if (pdata->console_size && !is_power_of_2(pdata->console_size))
  473. pdata->console_size = rounddown_pow_of_two(pdata->console_size);
  474. if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size))
  475. pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
  476. if (pdata->pmsg_size && !is_power_of_2(pdata->pmsg_size))
  477. pdata->pmsg_size = rounddown_pow_of_two(pdata->pmsg_size);
  478. cxt->size = pdata->mem_size;
  479. cxt->phys_addr = pdata->mem_address;
  480. cxt->memtype = pdata->mem_type;
  481. cxt->record_size = pdata->record_size;
  482. cxt->console_size = pdata->console_size;
  483. cxt->ftrace_size = pdata->ftrace_size;
  484. cxt->pmsg_size = pdata->pmsg_size;
  485. cxt->dump_oops = pdata->dump_oops;
  486. cxt->ecc_info = pdata->ecc_info;
  487. paddr = cxt->phys_addr;
  488. dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size
  489. - cxt->pmsg_size;
  490. err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
  491. if (err)
  492. goto fail_out;
  493. err = ramoops_init_prz(dev, cxt, &cxt->cprz, &paddr,
  494. cxt->console_size, 0);
  495. if (err)
  496. goto fail_init_cprz;
  497. err = ramoops_init_prz(dev, cxt, &cxt->fprz, &paddr, cxt->ftrace_size,
  498. LINUX_VERSION_CODE);
  499. if (err)
  500. goto fail_init_fprz;
  501. err = ramoops_init_prz(dev, cxt, &cxt->mprz, &paddr, cxt->pmsg_size, 0);
  502. if (err)
  503. goto fail_init_mprz;
  504. cxt->pstore.data = cxt;
  505. /*
  506. * Console can handle any buffer size, so prefer LOG_LINE_MAX. If we
  507. * have to handle dumps, we must have at least record_size buffer. And
  508. * for ftrace, bufsize is irrelevant (if bufsize is 0, buf will be
  509. * ZERO_SIZE_PTR).
  510. */
  511. if (cxt->console_size)
  512. cxt->pstore.bufsize = 1024; /* LOG_LINE_MAX */
  513. cxt->pstore.bufsize = max(cxt->record_size, cxt->pstore.bufsize);
  514. cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
  515. spin_lock_init(&cxt->pstore.buf_lock);
  516. if (!cxt->pstore.buf) {
  517. pr_err("cannot allocate pstore buffer\n");
  518. err = -ENOMEM;
  519. goto fail_clear;
  520. }
  521. err = pstore_register(&cxt->pstore);
  522. if (err) {
  523. pr_err("registering with pstore failed\n");
  524. goto fail_buf;
  525. }
  526. /*
  527. * Update the module parameter variables as well so they are visible
  528. * through /sys/module/ramoops/parameters/
  529. */
  530. mem_size = pdata->mem_size;
  531. mem_address = pdata->mem_address;
  532. record_size = pdata->record_size;
  533. dump_oops = pdata->dump_oops;
  534. ramoops_console_size = pdata->console_size;
  535. ramoops_pmsg_size = pdata->pmsg_size;
  536. ramoops_ftrace_size = pdata->ftrace_size;
  537. pr_info("attached 0x%lx@0x%llx, ecc: %d/%d\n",
  538. cxt->size, (unsigned long long)cxt->phys_addr,
  539. cxt->ecc_info.ecc_size, cxt->ecc_info.block_size);
  540. return 0;
  541. fail_buf:
  542. kfree(cxt->pstore.buf);
  543. fail_clear:
  544. cxt->pstore.bufsize = 0;
  545. persistent_ram_free(cxt->mprz);
  546. fail_init_mprz:
  547. persistent_ram_free(cxt->fprz);
  548. fail_init_fprz:
  549. persistent_ram_free(cxt->cprz);
  550. fail_init_cprz:
  551. ramoops_free_przs(cxt);
  552. fail_out:
  553. return err;
  554. }
  555. static int ramoops_remove(struct platform_device *pdev)
  556. {
  557. struct ramoops_context *cxt = &oops_cxt;
  558. pstore_unregister(&cxt->pstore);
  559. cxt->max_dump_cnt = 0;
  560. kfree(cxt->pstore.buf);
  561. cxt->pstore.bufsize = 0;
  562. persistent_ram_free(cxt->mprz);
  563. persistent_ram_free(cxt->fprz);
  564. persistent_ram_free(cxt->cprz);
  565. ramoops_free_przs(cxt);
  566. return 0;
  567. }
  568. static const struct of_device_id dt_match[] = {
  569. { .compatible = "ramoops" },
  570. {}
  571. };
  572. static struct platform_driver ramoops_driver = {
  573. .probe = ramoops_probe,
  574. .remove = ramoops_remove,
  575. .driver = {
  576. .name = "ramoops",
  577. .of_match_table = dt_match,
  578. },
  579. };
  580. static void ramoops_register_dummy(void)
  581. {
  582. if (!mem_size)
  583. return;
  584. pr_info("using module parameters\n");
  585. dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
  586. if (!dummy_data) {
  587. pr_info("could not allocate pdata\n");
  588. return;
  589. }
  590. dummy_data->mem_size = mem_size;
  591. dummy_data->mem_address = mem_address;
  592. dummy_data->mem_type = mem_type;
  593. dummy_data->record_size = record_size;
  594. dummy_data->console_size = ramoops_console_size;
  595. dummy_data->ftrace_size = ramoops_ftrace_size;
  596. dummy_data->pmsg_size = ramoops_pmsg_size;
  597. dummy_data->dump_oops = dump_oops;
  598. /*
  599. * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
  600. * (using 1 byte for ECC isn't much of use anyway).
  601. */
  602. dummy_data->ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
  603. dummy = platform_device_register_data(NULL, "ramoops", -1,
  604. dummy_data, sizeof(struct ramoops_platform_data));
  605. if (IS_ERR(dummy)) {
  606. pr_info("could not create platform device: %ld\n",
  607. PTR_ERR(dummy));
  608. }
  609. }
  610. static int __init ramoops_init(void)
  611. {
  612. ramoops_register_dummy();
  613. return platform_driver_register(&ramoops_driver);
  614. }
  615. postcore_initcall(ramoops_init);
  616. static void __exit ramoops_exit(void)
  617. {
  618. platform_driver_unregister(&ramoops_driver);
  619. platform_device_unregister(dummy);
  620. kfree(dummy_data);
  621. }
  622. module_exit(ramoops_exit);
  623. MODULE_LICENSE("GPL");
  624. MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
  625. MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");