ram.c 24 KB

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