rtas-fadump.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Firmware-Assisted Dump support on POWERVM platform.
  4. *
  5. * Copyright 2011, Mahesh Salgaonkar, IBM Corporation.
  6. * Copyright 2019, Hari Bathini, IBM Corporation.
  7. */
  8. #define pr_fmt(fmt) "rtas fadump: " fmt
  9. #include <linux/string.h>
  10. #include <linux/memblock.h>
  11. #include <linux/delay.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/crash_dump.h>
  14. #include <linux/of.h>
  15. #include <linux/of_fdt.h>
  16. #include <asm/page.h>
  17. #include <asm/rtas.h>
  18. #include <asm/fadump.h>
  19. #include <asm/fadump-internal.h>
  20. #include "rtas-fadump.h"
  21. static struct rtas_fadump_mem_struct fdm;
  22. static const struct rtas_fadump_mem_struct *fdm_active;
  23. static void rtas_fadump_update_config(struct fw_dump *fadump_conf,
  24. const struct rtas_fadump_mem_struct *fdm)
  25. {
  26. fadump_conf->boot_mem_dest_addr =
  27. be64_to_cpu(fdm->rmr_region.destination_address);
  28. fadump_conf->fadumphdr_addr = (fadump_conf->boot_mem_dest_addr +
  29. fadump_conf->boot_memory_size);
  30. }
  31. /*
  32. * This function is called in the capture kernel to get configuration details
  33. * setup in the first kernel and passed to the f/w.
  34. */
  35. static void __init rtas_fadump_get_config(struct fw_dump *fadump_conf,
  36. const struct rtas_fadump_mem_struct *fdm)
  37. {
  38. fadump_conf->boot_mem_addr[0] =
  39. be64_to_cpu(fdm->rmr_region.source_address);
  40. fadump_conf->boot_mem_sz[0] = be64_to_cpu(fdm->rmr_region.source_len);
  41. fadump_conf->boot_memory_size = fadump_conf->boot_mem_sz[0];
  42. fadump_conf->boot_mem_top = fadump_conf->boot_memory_size;
  43. fadump_conf->boot_mem_regs_cnt = 1;
  44. /*
  45. * Start address of reserve dump area (permanent reservation) for
  46. * re-registering FADump after dump capture.
  47. */
  48. fadump_conf->reserve_dump_area_start =
  49. be64_to_cpu(fdm->cpu_state_data.destination_address);
  50. rtas_fadump_update_config(fadump_conf, fdm);
  51. }
  52. static u64 rtas_fadump_init_mem_struct(struct fw_dump *fadump_conf)
  53. {
  54. u64 addr = fadump_conf->reserve_dump_area_start;
  55. memset(&fdm, 0, sizeof(struct rtas_fadump_mem_struct));
  56. addr = addr & PAGE_MASK;
  57. fdm.header.dump_format_version = cpu_to_be32(0x00000001);
  58. fdm.header.dump_num_sections = cpu_to_be16(3);
  59. fdm.header.dump_status_flag = 0;
  60. fdm.header.offset_first_dump_section =
  61. cpu_to_be32((u32)offsetof(struct rtas_fadump_mem_struct,
  62. cpu_state_data));
  63. /*
  64. * Fields for disk dump option.
  65. * We are not using disk dump option, hence set these fields to 0.
  66. */
  67. fdm.header.dd_block_size = 0;
  68. fdm.header.dd_block_offset = 0;
  69. fdm.header.dd_num_blocks = 0;
  70. fdm.header.dd_offset_disk_path = 0;
  71. /* set 0 to disable an automatic dump-reboot. */
  72. fdm.header.max_time_auto = 0;
  73. /* Kernel dump sections */
  74. /* cpu state data section. */
  75. fdm.cpu_state_data.request_flag =
  76. cpu_to_be32(RTAS_FADUMP_REQUEST_FLAG);
  77. fdm.cpu_state_data.source_data_type =
  78. cpu_to_be16(RTAS_FADUMP_CPU_STATE_DATA);
  79. fdm.cpu_state_data.source_address = 0;
  80. fdm.cpu_state_data.source_len =
  81. cpu_to_be64(fadump_conf->cpu_state_data_size);
  82. fdm.cpu_state_data.destination_address = cpu_to_be64(addr);
  83. addr += fadump_conf->cpu_state_data_size;
  84. /* hpte region section */
  85. fdm.hpte_region.request_flag = cpu_to_be32(RTAS_FADUMP_REQUEST_FLAG);
  86. fdm.hpte_region.source_data_type =
  87. cpu_to_be16(RTAS_FADUMP_HPTE_REGION);
  88. fdm.hpte_region.source_address = 0;
  89. fdm.hpte_region.source_len =
  90. cpu_to_be64(fadump_conf->hpte_region_size);
  91. fdm.hpte_region.destination_address = cpu_to_be64(addr);
  92. addr += fadump_conf->hpte_region_size;
  93. /*
  94. * Align boot memory area destination address to page boundary to
  95. * be able to mmap read this area in the vmcore.
  96. */
  97. addr = PAGE_ALIGN(addr);
  98. /* RMA region section */
  99. fdm.rmr_region.request_flag = cpu_to_be32(RTAS_FADUMP_REQUEST_FLAG);
  100. fdm.rmr_region.source_data_type =
  101. cpu_to_be16(RTAS_FADUMP_REAL_MODE_REGION);
  102. fdm.rmr_region.source_address = cpu_to_be64(0);
  103. fdm.rmr_region.source_len = cpu_to_be64(fadump_conf->boot_memory_size);
  104. fdm.rmr_region.destination_address = cpu_to_be64(addr);
  105. addr += fadump_conf->boot_memory_size;
  106. rtas_fadump_update_config(fadump_conf, &fdm);
  107. return addr;
  108. }
  109. static u64 rtas_fadump_get_bootmem_min(void)
  110. {
  111. return RTAS_FADUMP_MIN_BOOT_MEM;
  112. }
  113. static int rtas_fadump_register(struct fw_dump *fadump_conf)
  114. {
  115. unsigned int wait_time;
  116. int rc, err = -EIO;
  117. /* TODO: Add upper time limit for the delay */
  118. do {
  119. rc = rtas_call(fadump_conf->ibm_configure_kernel_dump, 3, 1,
  120. NULL, FADUMP_REGISTER, &fdm,
  121. sizeof(struct rtas_fadump_mem_struct));
  122. wait_time = rtas_busy_delay_time(rc);
  123. if (wait_time)
  124. mdelay(wait_time);
  125. } while (wait_time);
  126. switch (rc) {
  127. case 0:
  128. pr_info("Registration is successful!\n");
  129. fadump_conf->dump_registered = 1;
  130. err = 0;
  131. break;
  132. case -1:
  133. pr_err("Failed to register. Hardware Error(%d).\n", rc);
  134. break;
  135. case -3:
  136. if (!is_fadump_boot_mem_contiguous())
  137. pr_err("Can't have holes in boot memory area.\n");
  138. else if (!is_fadump_reserved_mem_contiguous())
  139. pr_err("Can't have holes in reserved memory area.\n");
  140. pr_err("Failed to register. Parameter Error(%d).\n", rc);
  141. err = -EINVAL;
  142. break;
  143. case -9:
  144. pr_err("Already registered!\n");
  145. fadump_conf->dump_registered = 1;
  146. err = -EEXIST;
  147. break;
  148. default:
  149. pr_err("Failed to register. Unknown Error(%d).\n", rc);
  150. break;
  151. }
  152. return err;
  153. }
  154. static int rtas_fadump_unregister(struct fw_dump *fadump_conf)
  155. {
  156. unsigned int wait_time;
  157. int rc;
  158. /* TODO: Add upper time limit for the delay */
  159. do {
  160. rc = rtas_call(fadump_conf->ibm_configure_kernel_dump, 3, 1,
  161. NULL, FADUMP_UNREGISTER, &fdm,
  162. sizeof(struct rtas_fadump_mem_struct));
  163. wait_time = rtas_busy_delay_time(rc);
  164. if (wait_time)
  165. mdelay(wait_time);
  166. } while (wait_time);
  167. if (rc) {
  168. pr_err("Failed to un-register - unexpected error(%d).\n", rc);
  169. return -EIO;
  170. }
  171. fadump_conf->dump_registered = 0;
  172. return 0;
  173. }
  174. static int rtas_fadump_invalidate(struct fw_dump *fadump_conf)
  175. {
  176. unsigned int wait_time;
  177. int rc;
  178. /* TODO: Add upper time limit for the delay */
  179. do {
  180. rc = rtas_call(fadump_conf->ibm_configure_kernel_dump, 3, 1,
  181. NULL, FADUMP_INVALIDATE, fdm_active,
  182. sizeof(struct rtas_fadump_mem_struct));
  183. wait_time = rtas_busy_delay_time(rc);
  184. if (wait_time)
  185. mdelay(wait_time);
  186. } while (wait_time);
  187. if (rc) {
  188. pr_err("Failed to invalidate - unexpected error (%d).\n", rc);
  189. return -EIO;
  190. }
  191. fadump_conf->dump_active = 0;
  192. fdm_active = NULL;
  193. return 0;
  194. }
  195. #define RTAS_FADUMP_GPR_MASK 0xffffff0000000000
  196. static inline int rtas_fadump_gpr_index(u64 id)
  197. {
  198. char str[3];
  199. int i = -1;
  200. if ((id & RTAS_FADUMP_GPR_MASK) == fadump_str_to_u64("GPR")) {
  201. /* get the digits at the end */
  202. id &= ~RTAS_FADUMP_GPR_MASK;
  203. id >>= 24;
  204. str[2] = '\0';
  205. str[1] = id & 0xff;
  206. str[0] = (id >> 8) & 0xff;
  207. if (kstrtoint(str, 10, &i))
  208. i = -EINVAL;
  209. if (i > 31)
  210. i = -1;
  211. }
  212. return i;
  213. }
  214. static void __init rtas_fadump_set_regval(struct pt_regs *regs, u64 reg_id, u64 reg_val)
  215. {
  216. int i;
  217. i = rtas_fadump_gpr_index(reg_id);
  218. if (i >= 0)
  219. regs->gpr[i] = (unsigned long)reg_val;
  220. else if (reg_id == fadump_str_to_u64("NIA"))
  221. regs->nip = (unsigned long)reg_val;
  222. else if (reg_id == fadump_str_to_u64("MSR"))
  223. regs->msr = (unsigned long)reg_val;
  224. else if (reg_id == fadump_str_to_u64("CTR"))
  225. regs->ctr = (unsigned long)reg_val;
  226. else if (reg_id == fadump_str_to_u64("LR"))
  227. regs->link = (unsigned long)reg_val;
  228. else if (reg_id == fadump_str_to_u64("XER"))
  229. regs->xer = (unsigned long)reg_val;
  230. else if (reg_id == fadump_str_to_u64("CR"))
  231. regs->ccr = (unsigned long)reg_val;
  232. else if (reg_id == fadump_str_to_u64("DAR"))
  233. regs->dar = (unsigned long)reg_val;
  234. else if (reg_id == fadump_str_to_u64("DSISR"))
  235. regs->dsisr = (unsigned long)reg_val;
  236. }
  237. static struct rtas_fadump_reg_entry* __init
  238. rtas_fadump_read_regs(struct rtas_fadump_reg_entry *reg_entry,
  239. struct pt_regs *regs)
  240. {
  241. memset(regs, 0, sizeof(struct pt_regs));
  242. while (be64_to_cpu(reg_entry->reg_id) != fadump_str_to_u64("CPUEND")) {
  243. rtas_fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
  244. be64_to_cpu(reg_entry->reg_value));
  245. reg_entry++;
  246. }
  247. reg_entry++;
  248. return reg_entry;
  249. }
  250. /*
  251. * Read CPU state dump data and convert it into ELF notes.
  252. * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
  253. * used to access the data to allow for additional fields to be added without
  254. * affecting compatibility. Each list of registers for a CPU starts with
  255. * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
  256. * 8 Byte ASCII identifier and 8 Byte register value. The register entry
  257. * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
  258. * of register value. For more details refer to PAPR document.
  259. *
  260. * Only for the crashing cpu we ignore the CPU dump data and get exact
  261. * state from fadump crash info structure populated by first kernel at the
  262. * time of crash.
  263. */
  264. static int __init rtas_fadump_build_cpu_notes(struct fw_dump *fadump_conf)
  265. {
  266. struct rtas_fadump_reg_save_area_header *reg_header;
  267. struct fadump_crash_info_header *fdh = NULL;
  268. struct rtas_fadump_reg_entry *reg_entry;
  269. u32 num_cpus, *note_buf;
  270. int i, rc = 0, cpu = 0;
  271. struct pt_regs regs;
  272. unsigned long addr;
  273. void *vaddr;
  274. addr = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
  275. vaddr = __va(addr);
  276. reg_header = vaddr;
  277. if (be64_to_cpu(reg_header->magic_number) !=
  278. fadump_str_to_u64("REGSAVE")) {
  279. pr_err("Unable to read register save area.\n");
  280. return -ENOENT;
  281. }
  282. pr_debug("--------CPU State Data------------\n");
  283. pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
  284. pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
  285. vaddr += be32_to_cpu(reg_header->num_cpu_offset);
  286. num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
  287. pr_debug("NumCpus : %u\n", num_cpus);
  288. vaddr += sizeof(u32);
  289. reg_entry = (struct rtas_fadump_reg_entry *)vaddr;
  290. rc = fadump_setup_cpu_notes_buf(num_cpus);
  291. if (rc != 0)
  292. return rc;
  293. note_buf = (u32 *)fadump_conf->cpu_notes_buf_vaddr;
  294. if (fadump_conf->fadumphdr_addr)
  295. fdh = __va(fadump_conf->fadumphdr_addr);
  296. for (i = 0; i < num_cpus; i++) {
  297. if (be64_to_cpu(reg_entry->reg_id) !=
  298. fadump_str_to_u64("CPUSTRT")) {
  299. pr_err("Unable to read CPU state data\n");
  300. rc = -ENOENT;
  301. goto error_out;
  302. }
  303. /* Lower 4 bytes of reg_value contains logical cpu id */
  304. cpu = (be64_to_cpu(reg_entry->reg_value) &
  305. RTAS_FADUMP_CPU_ID_MASK);
  306. if (fdh && !cpumask_test_cpu(cpu, &fdh->cpu_mask)) {
  307. RTAS_FADUMP_SKIP_TO_NEXT_CPU(reg_entry);
  308. continue;
  309. }
  310. pr_debug("Reading register data for cpu %d...\n", cpu);
  311. if (fdh && fdh->crashing_cpu == cpu) {
  312. regs = fdh->regs;
  313. note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
  314. RTAS_FADUMP_SKIP_TO_NEXT_CPU(reg_entry);
  315. } else {
  316. reg_entry++;
  317. reg_entry = rtas_fadump_read_regs(reg_entry, &regs);
  318. note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
  319. }
  320. }
  321. final_note(note_buf);
  322. if (fdh) {
  323. pr_debug("Updating elfcore header (%llx) with cpu notes\n",
  324. fdh->elfcorehdr_addr);
  325. fadump_update_elfcore_header(__va(fdh->elfcorehdr_addr));
  326. }
  327. return 0;
  328. error_out:
  329. fadump_free_cpu_notes_buf();
  330. return rc;
  331. }
  332. /*
  333. * Validate and process the dump data stored by firmware before exporting
  334. * it through '/proc/vmcore'.
  335. */
  336. static int __init rtas_fadump_process(struct fw_dump *fadump_conf)
  337. {
  338. struct fadump_crash_info_header *fdh;
  339. int rc = 0;
  340. if (!fdm_active || !fadump_conf->fadumphdr_addr)
  341. return -EINVAL;
  342. /* Check if the dump data is valid. */
  343. if ((be16_to_cpu(fdm_active->header.dump_status_flag) ==
  344. RTAS_FADUMP_ERROR_FLAG) ||
  345. (fdm_active->cpu_state_data.error_flags != 0) ||
  346. (fdm_active->rmr_region.error_flags != 0)) {
  347. pr_err("Dump taken by platform is not valid\n");
  348. return -EINVAL;
  349. }
  350. if ((fdm_active->rmr_region.bytes_dumped !=
  351. fdm_active->rmr_region.source_len) ||
  352. !fdm_active->cpu_state_data.bytes_dumped) {
  353. pr_err("Dump taken by platform is incomplete\n");
  354. return -EINVAL;
  355. }
  356. /* Validate the fadump crash info header */
  357. fdh = __va(fadump_conf->fadumphdr_addr);
  358. if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
  359. pr_err("Crash info header is not valid.\n");
  360. return -EINVAL;
  361. }
  362. rc = rtas_fadump_build_cpu_notes(fadump_conf);
  363. if (rc)
  364. return rc;
  365. /*
  366. * We are done validating dump info and elfcore header is now ready
  367. * to be exported. set elfcorehdr_addr so that vmcore module will
  368. * export the elfcore header through '/proc/vmcore'.
  369. */
  370. elfcorehdr_addr = fdh->elfcorehdr_addr;
  371. return 0;
  372. }
  373. static void rtas_fadump_region_show(struct fw_dump *fadump_conf,
  374. struct seq_file *m)
  375. {
  376. const struct rtas_fadump_section *cpu_data_section;
  377. const struct rtas_fadump_mem_struct *fdm_ptr;
  378. if (fdm_active)
  379. fdm_ptr = fdm_active;
  380. else
  381. fdm_ptr = &fdm;
  382. cpu_data_section = &(fdm_ptr->cpu_state_data);
  383. seq_printf(m, "CPU :[%#016llx-%#016llx] %#llx bytes, Dumped: %#llx\n",
  384. be64_to_cpu(cpu_data_section->destination_address),
  385. be64_to_cpu(cpu_data_section->destination_address) +
  386. be64_to_cpu(cpu_data_section->source_len) - 1,
  387. be64_to_cpu(cpu_data_section->source_len),
  388. be64_to_cpu(cpu_data_section->bytes_dumped));
  389. seq_printf(m, "HPTE:[%#016llx-%#016llx] %#llx bytes, Dumped: %#llx\n",
  390. be64_to_cpu(fdm_ptr->hpte_region.destination_address),
  391. be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
  392. be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
  393. be64_to_cpu(fdm_ptr->hpte_region.source_len),
  394. be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
  395. seq_printf(m, "DUMP: Src: %#016llx, Dest: %#016llx, ",
  396. be64_to_cpu(fdm_ptr->rmr_region.source_address),
  397. be64_to_cpu(fdm_ptr->rmr_region.destination_address));
  398. seq_printf(m, "Size: %#llx, Dumped: %#llx bytes\n",
  399. be64_to_cpu(fdm_ptr->rmr_region.source_len),
  400. be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
  401. /* Dump is active. Show preserved area start address. */
  402. if (fdm_active) {
  403. seq_printf(m, "\nMemory above %#016llx is reserved for saving crash dump\n",
  404. fadump_conf->boot_mem_top);
  405. }
  406. }
  407. static void rtas_fadump_trigger(struct fadump_crash_info_header *fdh,
  408. const char *msg)
  409. {
  410. /* Call ibm,os-term rtas call to trigger firmware assisted dump */
  411. rtas_os_term((char *)msg);
  412. }
  413. static struct fadump_ops rtas_fadump_ops = {
  414. .fadump_init_mem_struct = rtas_fadump_init_mem_struct,
  415. .fadump_get_bootmem_min = rtas_fadump_get_bootmem_min,
  416. .fadump_register = rtas_fadump_register,
  417. .fadump_unregister = rtas_fadump_unregister,
  418. .fadump_invalidate = rtas_fadump_invalidate,
  419. .fadump_process = rtas_fadump_process,
  420. .fadump_region_show = rtas_fadump_region_show,
  421. .fadump_trigger = rtas_fadump_trigger,
  422. };
  423. void __init rtas_fadump_dt_scan(struct fw_dump *fadump_conf, u64 node)
  424. {
  425. int i, size, num_sections;
  426. const __be32 *sections;
  427. const __be32 *token;
  428. /*
  429. * Check if Firmware Assisted dump is supported. if yes, check
  430. * if dump has been initiated on last reboot.
  431. */
  432. token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
  433. if (!token)
  434. return;
  435. fadump_conf->ibm_configure_kernel_dump = be32_to_cpu(*token);
  436. fadump_conf->ops = &rtas_fadump_ops;
  437. fadump_conf->fadump_supported = 1;
  438. /* Firmware supports 64-bit value for size, align it to pagesize. */
  439. fadump_conf->max_copy_size = ALIGN_DOWN(U64_MAX, PAGE_SIZE);
  440. /*
  441. * The 'ibm,kernel-dump' rtas node is present only if there is
  442. * dump data waiting for us.
  443. */
  444. fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
  445. if (fdm_active) {
  446. pr_info("Firmware-assisted dump is active.\n");
  447. fadump_conf->dump_active = 1;
  448. rtas_fadump_get_config(fadump_conf, (void *)__pa(fdm_active));
  449. }
  450. /* Get the sizes required to store dump data for the firmware provided
  451. * dump sections.
  452. * For each dump section type supported, a 32bit cell which defines
  453. * the ID of a supported section followed by two 32 bit cells which
  454. * gives the size of the section in bytes.
  455. */
  456. sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
  457. &size);
  458. if (!sections)
  459. return;
  460. num_sections = size / (3 * sizeof(u32));
  461. for (i = 0; i < num_sections; i++, sections += 3) {
  462. u32 type = (u32)of_read_number(sections, 1);
  463. switch (type) {
  464. case RTAS_FADUMP_CPU_STATE_DATA:
  465. fadump_conf->cpu_state_data_size =
  466. of_read_ulong(&sections[1], 2);
  467. break;
  468. case RTAS_FADUMP_HPTE_REGION:
  469. fadump_conf->hpte_region_size =
  470. of_read_ulong(&sections[1], 2);
  471. break;
  472. }
  473. }
  474. }