opal-fadump.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Firmware-Assisted Dump support on POWER platform (OPAL).
  4. *
  5. * Copyright 2019, Hari Bathini, IBM Corporation.
  6. */
  7. #define pr_fmt(fmt) "opal fadump: " fmt
  8. #include <linux/string.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/of.h>
  11. #include <linux/of_fdt.h>
  12. #include <linux/libfdt.h>
  13. #include <linux/mm.h>
  14. #include <linux/crash_dump.h>
  15. #include <asm/page.h>
  16. #include <asm/opal.h>
  17. #include <asm/fadump-internal.h>
  18. #include "opal-fadump.h"
  19. #ifdef CONFIG_PRESERVE_FA_DUMP
  20. /*
  21. * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
  22. * ensure crash data is preserved in hope that the subsequent memory
  23. * preserving kernel boot is going to process this crash data.
  24. */
  25. void __init opal_fadump_dt_scan(struct fw_dump *fadump_conf, u64 node)
  26. {
  27. const struct opal_fadump_mem_struct *opal_fdm_active;
  28. const __be32 *prop;
  29. unsigned long dn;
  30. u64 addr = 0;
  31. s64 ret;
  32. dn = of_get_flat_dt_subnode_by_name(node, "dump");
  33. if (dn == -FDT_ERR_NOTFOUND)
  34. return;
  35. /*
  36. * Check if dump has been initiated on last reboot.
  37. */
  38. prop = of_get_flat_dt_prop(dn, "mpipl-boot", NULL);
  39. if (!prop)
  40. return;
  41. ret = opal_mpipl_query_tag(OPAL_MPIPL_TAG_KERNEL, &addr);
  42. if ((ret != OPAL_SUCCESS) || !addr) {
  43. pr_debug("Could not get Kernel metadata (%lld)\n", ret);
  44. return;
  45. }
  46. /*
  47. * Preserve memory only if kernel memory regions are registered
  48. * with f/w for MPIPL.
  49. */
  50. addr = be64_to_cpu(addr);
  51. pr_debug("Kernel metadata addr: %llx\n", addr);
  52. opal_fdm_active = (void *)addr;
  53. if (be16_to_cpu(opal_fdm_active->registered_regions) == 0)
  54. return;
  55. ret = opal_mpipl_query_tag(OPAL_MPIPL_TAG_BOOT_MEM, &addr);
  56. if ((ret != OPAL_SUCCESS) || !addr) {
  57. pr_err("Failed to get boot memory tag (%lld)\n", ret);
  58. return;
  59. }
  60. /*
  61. * Memory below this address can be used for booting a
  62. * capture kernel or petitboot kernel. Preserve everything
  63. * above this address for processing crashdump.
  64. */
  65. fadump_conf->boot_mem_top = be64_to_cpu(addr);
  66. pr_debug("Preserve everything above %llx\n", fadump_conf->boot_mem_top);
  67. pr_info("Firmware-assisted dump is active.\n");
  68. fadump_conf->dump_active = 1;
  69. }
  70. #else /* CONFIG_PRESERVE_FA_DUMP */
  71. static const struct opal_fadump_mem_struct *opal_fdm_active;
  72. static const struct opal_mpipl_fadump *opal_cpu_metadata;
  73. static struct opal_fadump_mem_struct *opal_fdm;
  74. #ifdef CONFIG_OPAL_CORE
  75. extern bool kernel_initiated;
  76. #endif
  77. static int opal_fadump_unregister(struct fw_dump *fadump_conf);
  78. static void opal_fadump_update_config(struct fw_dump *fadump_conf,
  79. const struct opal_fadump_mem_struct *fdm)
  80. {
  81. pr_debug("Boot memory regions count: %d\n", be16_to_cpu(fdm->region_cnt));
  82. /*
  83. * The destination address of the first boot memory region is the
  84. * destination address of boot memory regions.
  85. */
  86. fadump_conf->boot_mem_dest_addr = be64_to_cpu(fdm->rgn[0].dest);
  87. pr_debug("Destination address of boot memory regions: %#016llx\n",
  88. fadump_conf->boot_mem_dest_addr);
  89. fadump_conf->fadumphdr_addr = be64_to_cpu(fdm->fadumphdr_addr);
  90. }
  91. /*
  92. * This function is called in the capture kernel to get configuration details
  93. * from metadata setup by the first kernel.
  94. */
  95. static void __init opal_fadump_get_config(struct fw_dump *fadump_conf,
  96. const struct opal_fadump_mem_struct *fdm)
  97. {
  98. unsigned long base, size, last_end, hole_size;
  99. int i;
  100. if (!fadump_conf->dump_active)
  101. return;
  102. last_end = 0;
  103. hole_size = 0;
  104. fadump_conf->boot_memory_size = 0;
  105. pr_debug("Boot memory regions:\n");
  106. for (i = 0; i < be16_to_cpu(fdm->region_cnt); i++) {
  107. base = be64_to_cpu(fdm->rgn[i].src);
  108. size = be64_to_cpu(fdm->rgn[i].size);
  109. pr_debug("\t[%03d] base: 0x%lx, size: 0x%lx\n", i, base, size);
  110. fadump_conf->boot_mem_addr[i] = base;
  111. fadump_conf->boot_mem_sz[i] = size;
  112. fadump_conf->boot_memory_size += size;
  113. hole_size += (base - last_end);
  114. last_end = base + size;
  115. }
  116. /*
  117. * Start address of reserve dump area (permanent reservation) for
  118. * re-registering FADump after dump capture.
  119. */
  120. fadump_conf->reserve_dump_area_start = be64_to_cpu(fdm->rgn[0].dest);
  121. /*
  122. * Rarely, but it can so happen that system crashes before all
  123. * boot memory regions are registered for MPIPL. In such
  124. * cases, warn that the vmcore may not be accurate and proceed
  125. * anyway as that is the best bet considering free pages, cache
  126. * pages, user pages, etc are usually filtered out.
  127. *
  128. * Hope the memory that could not be preserved only has pages
  129. * that are usually filtered out while saving the vmcore.
  130. */
  131. if (be16_to_cpu(fdm->region_cnt) > be16_to_cpu(fdm->registered_regions)) {
  132. pr_warn("Not all memory regions were saved!!!\n");
  133. pr_warn(" Unsaved memory regions:\n");
  134. i = be16_to_cpu(fdm->registered_regions);
  135. while (i < be16_to_cpu(fdm->region_cnt)) {
  136. pr_warn("\t[%03d] base: 0x%llx, size: 0x%llx\n",
  137. i, be64_to_cpu(fdm->rgn[i].src),
  138. be64_to_cpu(fdm->rgn[i].size));
  139. i++;
  140. }
  141. pr_warn("If the unsaved regions only contain pages that are filtered out (eg. free/user pages), the vmcore should still be usable.\n");
  142. pr_warn("WARNING: If the unsaved regions contain kernel pages, the vmcore will be corrupted.\n");
  143. }
  144. fadump_conf->boot_mem_top = (fadump_conf->boot_memory_size + hole_size);
  145. fadump_conf->boot_mem_regs_cnt = be16_to_cpu(fdm->region_cnt);
  146. opal_fadump_update_config(fadump_conf, fdm);
  147. }
  148. /* Initialize kernel metadata */
  149. static void opal_fadump_init_metadata(struct opal_fadump_mem_struct *fdm)
  150. {
  151. fdm->version = OPAL_FADUMP_VERSION;
  152. fdm->region_cnt = cpu_to_be16(0);
  153. fdm->registered_regions = cpu_to_be16(0);
  154. fdm->fadumphdr_addr = cpu_to_be64(0);
  155. }
  156. static u64 opal_fadump_init_mem_struct(struct fw_dump *fadump_conf)
  157. {
  158. u64 addr = fadump_conf->reserve_dump_area_start;
  159. u16 reg_cnt;
  160. int i;
  161. opal_fdm = __va(fadump_conf->kernel_metadata);
  162. opal_fadump_init_metadata(opal_fdm);
  163. /* Boot memory regions */
  164. reg_cnt = be16_to_cpu(opal_fdm->region_cnt);
  165. for (i = 0; i < fadump_conf->boot_mem_regs_cnt; i++) {
  166. opal_fdm->rgn[i].src = cpu_to_be64(fadump_conf->boot_mem_addr[i]);
  167. opal_fdm->rgn[i].dest = cpu_to_be64(addr);
  168. opal_fdm->rgn[i].size = cpu_to_be64(fadump_conf->boot_mem_sz[i]);
  169. reg_cnt++;
  170. addr += fadump_conf->boot_mem_sz[i];
  171. }
  172. opal_fdm->region_cnt = cpu_to_be16(reg_cnt);
  173. /*
  174. * Kernel metadata is passed to f/w and retrieved in capture kernel.
  175. * So, use it to save fadump header address instead of calculating it.
  176. */
  177. opal_fdm->fadumphdr_addr = cpu_to_be64(be64_to_cpu(opal_fdm->rgn[0].dest) +
  178. fadump_conf->boot_memory_size);
  179. opal_fadump_update_config(fadump_conf, opal_fdm);
  180. return addr;
  181. }
  182. static u64 opal_fadump_get_metadata_size(void)
  183. {
  184. return PAGE_ALIGN(sizeof(struct opal_fadump_mem_struct));
  185. }
  186. static int opal_fadump_setup_metadata(struct fw_dump *fadump_conf)
  187. {
  188. int err = 0;
  189. s64 ret;
  190. /*
  191. * Use the last page(s) in FADump memory reservation for
  192. * kernel metadata.
  193. */
  194. fadump_conf->kernel_metadata = (fadump_conf->reserve_dump_area_start +
  195. fadump_conf->reserve_dump_area_size -
  196. opal_fadump_get_metadata_size());
  197. pr_info("Kernel metadata addr: %llx\n", fadump_conf->kernel_metadata);
  198. /* Initialize kernel metadata before registering the address with f/w */
  199. opal_fdm = __va(fadump_conf->kernel_metadata);
  200. opal_fadump_init_metadata(opal_fdm);
  201. /*
  202. * Register metadata address with f/w. Can be retrieved in
  203. * the capture kernel.
  204. */
  205. ret = opal_mpipl_register_tag(OPAL_MPIPL_TAG_KERNEL,
  206. fadump_conf->kernel_metadata);
  207. if (ret != OPAL_SUCCESS) {
  208. pr_err("Failed to set kernel metadata tag!\n");
  209. err = -EPERM;
  210. }
  211. /*
  212. * Register boot memory top address with f/w. Should be retrieved
  213. * by a kernel that intends to preserve crash'ed kernel's memory.
  214. */
  215. ret = opal_mpipl_register_tag(OPAL_MPIPL_TAG_BOOT_MEM,
  216. fadump_conf->boot_mem_top);
  217. if (ret != OPAL_SUCCESS) {
  218. pr_err("Failed to set boot memory tag!\n");
  219. err = -EPERM;
  220. }
  221. return err;
  222. }
  223. static u64 opal_fadump_get_bootmem_min(void)
  224. {
  225. return OPAL_FADUMP_MIN_BOOT_MEM;
  226. }
  227. static int opal_fadump_register(struct fw_dump *fadump_conf)
  228. {
  229. s64 rc = OPAL_PARAMETER;
  230. u16 registered_regs;
  231. int i, err = -EIO;
  232. registered_regs = be16_to_cpu(opal_fdm->registered_regions);
  233. for (i = 0; i < be16_to_cpu(opal_fdm->region_cnt); i++) {
  234. rc = opal_mpipl_update(OPAL_MPIPL_ADD_RANGE,
  235. be64_to_cpu(opal_fdm->rgn[i].src),
  236. be64_to_cpu(opal_fdm->rgn[i].dest),
  237. be64_to_cpu(opal_fdm->rgn[i].size));
  238. if (rc != OPAL_SUCCESS)
  239. break;
  240. registered_regs++;
  241. }
  242. opal_fdm->registered_regions = cpu_to_be16(registered_regs);
  243. switch (rc) {
  244. case OPAL_SUCCESS:
  245. pr_info("Registration is successful!\n");
  246. fadump_conf->dump_registered = 1;
  247. err = 0;
  248. break;
  249. case OPAL_RESOURCE:
  250. /* If MAX regions limit in f/w is hit, warn and proceed. */
  251. pr_warn("%d regions could not be registered for MPIPL as MAX limit is reached!\n",
  252. (be16_to_cpu(opal_fdm->region_cnt) -
  253. be16_to_cpu(opal_fdm->registered_regions)));
  254. fadump_conf->dump_registered = 1;
  255. err = 0;
  256. break;
  257. case OPAL_PARAMETER:
  258. pr_err("Failed to register. Parameter Error(%lld).\n", rc);
  259. break;
  260. case OPAL_HARDWARE:
  261. pr_err("Support not available.\n");
  262. fadump_conf->fadump_supported = 0;
  263. fadump_conf->fadump_enabled = 0;
  264. break;
  265. default:
  266. pr_err("Failed to register. Unknown Error(%lld).\n", rc);
  267. break;
  268. }
  269. /*
  270. * If some regions were registered before OPAL_MPIPL_ADD_RANGE
  271. * OPAL call failed, unregister all regions.
  272. */
  273. if ((err < 0) && (be16_to_cpu(opal_fdm->registered_regions) > 0))
  274. opal_fadump_unregister(fadump_conf);
  275. return err;
  276. }
  277. static int opal_fadump_unregister(struct fw_dump *fadump_conf)
  278. {
  279. s64 rc;
  280. rc = opal_mpipl_update(OPAL_MPIPL_REMOVE_ALL, 0, 0, 0);
  281. if (rc) {
  282. pr_err("Failed to un-register - unexpected Error(%lld).\n", rc);
  283. return -EIO;
  284. }
  285. opal_fdm->registered_regions = cpu_to_be16(0);
  286. fadump_conf->dump_registered = 0;
  287. return 0;
  288. }
  289. static int opal_fadump_invalidate(struct fw_dump *fadump_conf)
  290. {
  291. s64 rc;
  292. rc = opal_mpipl_update(OPAL_MPIPL_FREE_PRESERVED_MEMORY, 0, 0, 0);
  293. if (rc) {
  294. pr_err("Failed to invalidate - unexpected Error(%lld).\n", rc);
  295. return -EIO;
  296. }
  297. fadump_conf->dump_active = 0;
  298. opal_fdm_active = NULL;
  299. return 0;
  300. }
  301. static void opal_fadump_cleanup(struct fw_dump *fadump_conf)
  302. {
  303. s64 ret;
  304. ret = opal_mpipl_register_tag(OPAL_MPIPL_TAG_KERNEL, 0);
  305. if (ret != OPAL_SUCCESS)
  306. pr_warn("Could not reset (%llu) kernel metadata tag!\n", ret);
  307. }
  308. /*
  309. * Verify if CPU state data is available. If available, do a bit of sanity
  310. * checking before processing this data.
  311. */
  312. static bool __init is_opal_fadump_cpu_data_valid(struct fw_dump *fadump_conf)
  313. {
  314. if (!opal_cpu_metadata)
  315. return false;
  316. fadump_conf->cpu_state_data_version =
  317. be32_to_cpu(opal_cpu_metadata->cpu_data_version);
  318. fadump_conf->cpu_state_entry_size =
  319. be32_to_cpu(opal_cpu_metadata->cpu_data_size);
  320. fadump_conf->cpu_state_dest_vaddr =
  321. (u64)__va(be64_to_cpu(opal_cpu_metadata->region[0].dest));
  322. fadump_conf->cpu_state_data_size =
  323. be64_to_cpu(opal_cpu_metadata->region[0].size);
  324. if (fadump_conf->cpu_state_data_version != HDAT_FADUMP_CPU_DATA_VER) {
  325. pr_warn("Supported CPU state data version: %u, found: %d!\n",
  326. HDAT_FADUMP_CPU_DATA_VER,
  327. fadump_conf->cpu_state_data_version);
  328. pr_warn("WARNING: F/W using newer CPU state data format!!\n");
  329. }
  330. if ((fadump_conf->cpu_state_dest_vaddr == 0) ||
  331. (fadump_conf->cpu_state_entry_size == 0) ||
  332. (fadump_conf->cpu_state_entry_size >
  333. fadump_conf->cpu_state_data_size)) {
  334. pr_err("CPU state data is invalid. Ignoring!\n");
  335. return false;
  336. }
  337. return true;
  338. }
  339. /*
  340. * Convert CPU state data saved at the time of crash into ELF notes.
  341. *
  342. * While the crashing CPU's register data is saved by the kernel, CPU state
  343. * data for all CPUs is saved by f/w. In CPU state data provided by f/w,
  344. * each register entry is of 16 bytes, a numerical identifier along with
  345. * a GPR/SPR flag in the first 8 bytes and the register value in the next
  346. * 8 bytes. For more details refer to F/W documentation. If this data is
  347. * missing or in unsupported format, append crashing CPU's register data
  348. * saved by the kernel in the PT_NOTE, to have something to work with in
  349. * the vmcore file.
  350. */
  351. static int __init
  352. opal_fadump_build_cpu_notes(struct fw_dump *fadump_conf,
  353. struct fadump_crash_info_header *fdh)
  354. {
  355. u32 thread_pir, size_per_thread, regs_offset, regs_cnt, reg_esize;
  356. struct hdat_fadump_thread_hdr *thdr;
  357. bool is_cpu_data_valid = false;
  358. u32 num_cpus = 1, *note_buf;
  359. struct pt_regs regs;
  360. char *bufp;
  361. int rc, i;
  362. if (is_opal_fadump_cpu_data_valid(fadump_conf)) {
  363. size_per_thread = fadump_conf->cpu_state_entry_size;
  364. num_cpus = (fadump_conf->cpu_state_data_size / size_per_thread);
  365. bufp = __va(fadump_conf->cpu_state_dest_vaddr);
  366. is_cpu_data_valid = true;
  367. }
  368. rc = fadump_setup_cpu_notes_buf(num_cpus);
  369. if (rc != 0)
  370. return rc;
  371. note_buf = (u32 *)fadump_conf->cpu_notes_buf_vaddr;
  372. if (!is_cpu_data_valid)
  373. goto out;
  374. /*
  375. * Offset for register entries, entry size and registers count is
  376. * duplicated in every thread header in keeping with HDAT format.
  377. * Use these values from the first thread header.
  378. */
  379. thdr = (struct hdat_fadump_thread_hdr *)bufp;
  380. regs_offset = (offsetof(struct hdat_fadump_thread_hdr, offset) +
  381. be32_to_cpu(thdr->offset));
  382. reg_esize = be32_to_cpu(thdr->esize);
  383. regs_cnt = be32_to_cpu(thdr->ecnt);
  384. pr_debug("--------CPU State Data------------\n");
  385. pr_debug("NumCpus : %u\n", num_cpus);
  386. pr_debug("\tOffset: %u, Entry size: %u, Cnt: %u\n",
  387. regs_offset, reg_esize, regs_cnt);
  388. for (i = 0; i < num_cpus; i++, bufp += size_per_thread) {
  389. thdr = (struct hdat_fadump_thread_hdr *)bufp;
  390. thread_pir = be32_to_cpu(thdr->pir);
  391. pr_debug("[%04d] PIR: 0x%x, core state: 0x%02x\n",
  392. i, thread_pir, thdr->core_state);
  393. /*
  394. * If this is kernel initiated crash, crashing_cpu would be set
  395. * appropriately and register data of the crashing CPU saved by
  396. * crashing kernel. Add this saved register data of crashing CPU
  397. * to elf notes and populate the pt_regs for the remaining CPUs
  398. * from register state data provided by firmware.
  399. */
  400. if (fdh->crashing_cpu == thread_pir) {
  401. note_buf = fadump_regs_to_elf_notes(note_buf,
  402. &fdh->regs);
  403. pr_debug("Crashing CPU PIR: 0x%x - R1 : 0x%lx, NIP : 0x%lx\n",
  404. fdh->crashing_cpu, fdh->regs.gpr[1],
  405. fdh->regs.nip);
  406. continue;
  407. }
  408. /*
  409. * Register state data of MAX cores is provided by firmware,
  410. * but some of this cores may not be active. So, while
  411. * processing register state data, check core state and
  412. * skip threads that belong to inactive cores.
  413. */
  414. if (thdr->core_state == HDAT_FADUMP_CORE_INACTIVE)
  415. continue;
  416. opal_fadump_read_regs((bufp + regs_offset), regs_cnt,
  417. reg_esize, true, &regs);
  418. note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
  419. pr_debug("CPU PIR: 0x%x - R1 : 0x%lx, NIP : 0x%lx\n",
  420. thread_pir, regs.gpr[1], regs.nip);
  421. }
  422. out:
  423. /*
  424. * CPU state data is invalid/unsupported. Try appending crashing CPU's
  425. * register data, if it is saved by the kernel.
  426. */
  427. if (fadump_conf->cpu_notes_buf_vaddr == (u64)note_buf) {
  428. if (fdh->crashing_cpu == FADUMP_CPU_UNKNOWN) {
  429. fadump_free_cpu_notes_buf();
  430. return -ENODEV;
  431. }
  432. pr_warn("WARNING: appending only crashing CPU's register data\n");
  433. note_buf = fadump_regs_to_elf_notes(note_buf, &(fdh->regs));
  434. }
  435. final_note(note_buf);
  436. pr_debug("Updating elfcore header (%llx) with cpu notes\n",
  437. fdh->elfcorehdr_addr);
  438. fadump_update_elfcore_header(__va(fdh->elfcorehdr_addr));
  439. return 0;
  440. }
  441. static int __init opal_fadump_process(struct fw_dump *fadump_conf)
  442. {
  443. struct fadump_crash_info_header *fdh;
  444. int rc = -EINVAL;
  445. if (!opal_fdm_active || !fadump_conf->fadumphdr_addr)
  446. return rc;
  447. /* Validate the fadump crash info header */
  448. fdh = __va(fadump_conf->fadumphdr_addr);
  449. if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
  450. pr_err("Crash info header is not valid.\n");
  451. return rc;
  452. }
  453. #ifdef CONFIG_OPAL_CORE
  454. /*
  455. * If this is a kernel initiated crash, crashing_cpu would be set
  456. * appropriately and register data of the crashing CPU saved by
  457. * crashing kernel. Add this saved register data of crashing CPU
  458. * to elf notes and populate the pt_regs for the remaining CPUs
  459. * from register state data provided by firmware.
  460. */
  461. if (fdh->crashing_cpu != FADUMP_CPU_UNKNOWN)
  462. kernel_initiated = true;
  463. #endif
  464. rc = opal_fadump_build_cpu_notes(fadump_conf, fdh);
  465. if (rc)
  466. return rc;
  467. /*
  468. * We are done validating dump info and elfcore header is now ready
  469. * to be exported. set elfcorehdr_addr so that vmcore module will
  470. * export the elfcore header through '/proc/vmcore'.
  471. */
  472. elfcorehdr_addr = fdh->elfcorehdr_addr;
  473. return rc;
  474. }
  475. static void opal_fadump_region_show(struct fw_dump *fadump_conf,
  476. struct seq_file *m)
  477. {
  478. const struct opal_fadump_mem_struct *fdm_ptr;
  479. u64 dumped_bytes = 0;
  480. int i;
  481. if (fadump_conf->dump_active)
  482. fdm_ptr = opal_fdm_active;
  483. else
  484. fdm_ptr = opal_fdm;
  485. for (i = 0; i < be16_to_cpu(fdm_ptr->region_cnt); i++) {
  486. /*
  487. * Only regions that are registered for MPIPL
  488. * would have dump data.
  489. */
  490. if ((fadump_conf->dump_active) &&
  491. (i < be16_to_cpu(fdm_ptr->registered_regions)))
  492. dumped_bytes = be64_to_cpu(fdm_ptr->rgn[i].size);
  493. seq_printf(m, "DUMP: Src: %#016llx, Dest: %#016llx, ",
  494. be64_to_cpu(fdm_ptr->rgn[i].src),
  495. be64_to_cpu(fdm_ptr->rgn[i].dest));
  496. seq_printf(m, "Size: %#llx, Dumped: %#llx bytes\n",
  497. be64_to_cpu(fdm_ptr->rgn[i].size), dumped_bytes);
  498. }
  499. /* Dump is active. Show preserved area start address. */
  500. if (fadump_conf->dump_active) {
  501. seq_printf(m, "\nMemory above %#016llx is reserved for saving crash dump\n",
  502. fadump_conf->boot_mem_top);
  503. }
  504. }
  505. static void opal_fadump_trigger(struct fadump_crash_info_header *fdh,
  506. const char *msg)
  507. {
  508. int rc;
  509. /*
  510. * Unlike on pSeries platform, logical CPU number is not provided
  511. * with architected register state data. So, store the crashing
  512. * CPU's PIR instead to plug the appropriate register data for
  513. * crashing CPU in the vmcore file.
  514. */
  515. fdh->crashing_cpu = (u32)mfspr(SPRN_PIR);
  516. rc = opal_cec_reboot2(OPAL_REBOOT_MPIPL, msg);
  517. if (rc == OPAL_UNSUPPORTED) {
  518. pr_emerg("Reboot type %d not supported.\n",
  519. OPAL_REBOOT_MPIPL);
  520. } else if (rc == OPAL_HARDWARE)
  521. pr_emerg("No backend support for MPIPL!\n");
  522. }
  523. static struct fadump_ops opal_fadump_ops = {
  524. .fadump_init_mem_struct = opal_fadump_init_mem_struct,
  525. .fadump_get_metadata_size = opal_fadump_get_metadata_size,
  526. .fadump_setup_metadata = opal_fadump_setup_metadata,
  527. .fadump_get_bootmem_min = opal_fadump_get_bootmem_min,
  528. .fadump_register = opal_fadump_register,
  529. .fadump_unregister = opal_fadump_unregister,
  530. .fadump_invalidate = opal_fadump_invalidate,
  531. .fadump_cleanup = opal_fadump_cleanup,
  532. .fadump_process = opal_fadump_process,
  533. .fadump_region_show = opal_fadump_region_show,
  534. .fadump_trigger = opal_fadump_trigger,
  535. };
  536. void __init opal_fadump_dt_scan(struct fw_dump *fadump_conf, u64 node)
  537. {
  538. const __be32 *prop;
  539. unsigned long dn;
  540. __be64 be_addr;
  541. u64 addr = 0;
  542. int i, len;
  543. s64 ret;
  544. /*
  545. * Check if Firmware-Assisted Dump is supported. if yes, check
  546. * if dump has been initiated on last reboot.
  547. */
  548. dn = of_get_flat_dt_subnode_by_name(node, "dump");
  549. if (dn == -FDT_ERR_NOTFOUND) {
  550. pr_debug("FADump support is missing!\n");
  551. return;
  552. }
  553. if (!of_flat_dt_is_compatible(dn, "ibm,opal-dump")) {
  554. pr_err("Support missing for this f/w version!\n");
  555. return;
  556. }
  557. prop = of_get_flat_dt_prop(dn, "fw-load-area", &len);
  558. if (prop) {
  559. /*
  560. * Each f/w load area is an (address,size) pair,
  561. * 2 cells each, totalling 4 cells per range.
  562. */
  563. for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
  564. u64 base, end;
  565. base = of_read_number(prop + (i * 4) + 0, 2);
  566. end = base;
  567. end += of_read_number(prop + (i * 4) + 2, 2);
  568. if (end > OPAL_FADUMP_MIN_BOOT_MEM) {
  569. pr_err("F/W load area: 0x%llx-0x%llx\n",
  570. base, end);
  571. pr_err("F/W version not supported!\n");
  572. return;
  573. }
  574. }
  575. }
  576. fadump_conf->ops = &opal_fadump_ops;
  577. fadump_conf->fadump_supported = 1;
  578. /*
  579. * Firmware supports 32-bit field for size. Align it to PAGE_SIZE
  580. * and request firmware to copy multiple kernel boot memory regions.
  581. */
  582. fadump_conf->max_copy_size = ALIGN_DOWN(U32_MAX, PAGE_SIZE);
  583. /*
  584. * Check if dump has been initiated on last reboot.
  585. */
  586. prop = of_get_flat_dt_prop(dn, "mpipl-boot", NULL);
  587. if (!prop)
  588. return;
  589. ret = opal_mpipl_query_tag(OPAL_MPIPL_TAG_KERNEL, &be_addr);
  590. if ((ret != OPAL_SUCCESS) || !be_addr) {
  591. pr_err("Failed to get Kernel metadata (%lld)\n", ret);
  592. return;
  593. }
  594. addr = be64_to_cpu(be_addr);
  595. pr_debug("Kernel metadata addr: %llx\n", addr);
  596. opal_fdm_active = __va(addr);
  597. if (opal_fdm_active->version != OPAL_FADUMP_VERSION) {
  598. pr_warn("Supported kernel metadata version: %u, found: %d!\n",
  599. OPAL_FADUMP_VERSION, opal_fdm_active->version);
  600. pr_warn("WARNING: Kernel metadata format mismatch identified! Core file maybe corrupted..\n");
  601. }
  602. /* Kernel regions not registered with f/w for MPIPL */
  603. if (be16_to_cpu(opal_fdm_active->registered_regions) == 0) {
  604. opal_fdm_active = NULL;
  605. return;
  606. }
  607. ret = opal_mpipl_query_tag(OPAL_MPIPL_TAG_CPU, &be_addr);
  608. if (be_addr) {
  609. addr = be64_to_cpu(be_addr);
  610. pr_debug("CPU metadata addr: %llx\n", addr);
  611. opal_cpu_metadata = __va(addr);
  612. }
  613. pr_info("Firmware-assisted dump is active.\n");
  614. fadump_conf->dump_active = 1;
  615. opal_fadump_get_config(fadump_conf, opal_fdm_active);
  616. }
  617. #endif /* !CONFIG_PRESERVE_FA_DUMP */