ram.c 17 KB

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