ram.c 18 KB

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