fadump.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /*
  2. * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
  3. * dump with assistance from firmware. This approach does not use kexec,
  4. * instead firmware assists in booting the kdump kernel while preserving
  5. * memory contents. The most of the code implementation has been adapted
  6. * from phyp assisted dump implementation written by Linas Vepstas and
  7. * Manish Ahuja
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * Copyright 2011 IBM Corporation
  24. * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
  25. */
  26. #undef DEBUG
  27. #define pr_fmt(fmt) "fadump: " fmt
  28. #include <linux/string.h>
  29. #include <linux/memblock.h>
  30. #include <linux/delay.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/crash_dump.h>
  34. #include <linux/kobject.h>
  35. #include <linux/sysfs.h>
  36. #include <asm/page.h>
  37. #include <asm/prom.h>
  38. #include <asm/rtas.h>
  39. #include <asm/fadump.h>
  40. #include <asm/debug.h>
  41. #include <asm/setup.h>
  42. static struct fw_dump fw_dump;
  43. static struct fadump_mem_struct fdm;
  44. static const struct fadump_mem_struct *fdm_active;
  45. static DEFINE_MUTEX(fadump_mutex);
  46. struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
  47. int crash_mem_ranges;
  48. /* Scan the Firmware Assisted dump configuration details. */
  49. int __init early_init_dt_scan_fw_dump(unsigned long node,
  50. const char *uname, int depth, void *data)
  51. {
  52. const __be32 *sections;
  53. int i, num_sections;
  54. int size;
  55. const __be32 *token;
  56. if (depth != 1 || strcmp(uname, "rtas") != 0)
  57. return 0;
  58. /*
  59. * Check if Firmware Assisted dump is supported. if yes, check
  60. * if dump has been initiated on last reboot.
  61. */
  62. token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
  63. if (!token)
  64. return 1;
  65. fw_dump.fadump_supported = 1;
  66. fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
  67. /*
  68. * The 'ibm,kernel-dump' rtas node is present only if there is
  69. * dump data waiting for us.
  70. */
  71. fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
  72. if (fdm_active)
  73. fw_dump.dump_active = 1;
  74. /* Get the sizes required to store dump data for the firmware provided
  75. * dump sections.
  76. * For each dump section type supported, a 32bit cell which defines
  77. * the ID of a supported section followed by two 32 bit cells which
  78. * gives teh size of the section in bytes.
  79. */
  80. sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
  81. &size);
  82. if (!sections)
  83. return 1;
  84. num_sections = size / (3 * sizeof(u32));
  85. for (i = 0; i < num_sections; i++, sections += 3) {
  86. u32 type = (u32)of_read_number(sections, 1);
  87. switch (type) {
  88. case FADUMP_CPU_STATE_DATA:
  89. fw_dump.cpu_state_data_size =
  90. of_read_ulong(&sections[1], 2);
  91. break;
  92. case FADUMP_HPTE_REGION:
  93. fw_dump.hpte_region_size =
  94. of_read_ulong(&sections[1], 2);
  95. break;
  96. }
  97. }
  98. return 1;
  99. }
  100. int is_fadump_active(void)
  101. {
  102. return fw_dump.dump_active;
  103. }
  104. /* Print firmware assisted dump configurations for debugging purpose. */
  105. static void fadump_show_config(void)
  106. {
  107. pr_debug("Support for firmware-assisted dump (fadump): %s\n",
  108. (fw_dump.fadump_supported ? "present" : "no support"));
  109. if (!fw_dump.fadump_supported)
  110. return;
  111. pr_debug("Fadump enabled : %s\n",
  112. (fw_dump.fadump_enabled ? "yes" : "no"));
  113. pr_debug("Dump Active : %s\n",
  114. (fw_dump.dump_active ? "yes" : "no"));
  115. pr_debug("Dump section sizes:\n");
  116. pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
  117. pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
  118. pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size);
  119. }
  120. static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
  121. unsigned long addr)
  122. {
  123. if (!fdm)
  124. return 0;
  125. memset(fdm, 0, sizeof(struct fadump_mem_struct));
  126. addr = addr & PAGE_MASK;
  127. fdm->header.dump_format_version = cpu_to_be32(0x00000001);
  128. fdm->header.dump_num_sections = cpu_to_be16(3);
  129. fdm->header.dump_status_flag = 0;
  130. fdm->header.offset_first_dump_section =
  131. cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
  132. /*
  133. * Fields for disk dump option.
  134. * We are not using disk dump option, hence set these fields to 0.
  135. */
  136. fdm->header.dd_block_size = 0;
  137. fdm->header.dd_block_offset = 0;
  138. fdm->header.dd_num_blocks = 0;
  139. fdm->header.dd_offset_disk_path = 0;
  140. /* set 0 to disable an automatic dump-reboot. */
  141. fdm->header.max_time_auto = 0;
  142. /* Kernel dump sections */
  143. /* cpu state data section. */
  144. fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
  145. fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
  146. fdm->cpu_state_data.source_address = 0;
  147. fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
  148. fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
  149. addr += fw_dump.cpu_state_data_size;
  150. /* hpte region section */
  151. fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
  152. fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
  153. fdm->hpte_region.source_address = 0;
  154. fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
  155. fdm->hpte_region.destination_address = cpu_to_be64(addr);
  156. addr += fw_dump.hpte_region_size;
  157. /* RMA region section */
  158. fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
  159. fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
  160. fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
  161. fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
  162. fdm->rmr_region.destination_address = cpu_to_be64(addr);
  163. addr += fw_dump.boot_memory_size;
  164. return addr;
  165. }
  166. /**
  167. * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
  168. *
  169. * Function to find the largest memory size we need to reserve during early
  170. * boot process. This will be the size of the memory that is required for a
  171. * kernel to boot successfully.
  172. *
  173. * This function has been taken from phyp-assisted dump feature implementation.
  174. *
  175. * returns larger of 256MB or 5% rounded down to multiples of 256MB.
  176. *
  177. * TODO: Come up with better approach to find out more accurate memory size
  178. * that is required for a kernel to boot successfully.
  179. *
  180. */
  181. static inline unsigned long fadump_calculate_reserve_size(void)
  182. {
  183. unsigned long size;
  184. /*
  185. * Check if the size is specified through fadump_reserve_mem= cmdline
  186. * option. If yes, then use that.
  187. */
  188. if (fw_dump.reserve_bootvar)
  189. return fw_dump.reserve_bootvar;
  190. /* divide by 20 to get 5% of value */
  191. size = memblock_end_of_DRAM() / 20;
  192. /* round it down in multiples of 256 */
  193. size = size & ~0x0FFFFFFFUL;
  194. /* Truncate to memory_limit. We don't want to over reserve the memory.*/
  195. if (memory_limit && size > memory_limit)
  196. size = memory_limit;
  197. return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
  198. }
  199. /*
  200. * Calculate the total memory size required to be reserved for
  201. * firmware-assisted dump registration.
  202. */
  203. static unsigned long get_fadump_area_size(void)
  204. {
  205. unsigned long size = 0;
  206. size += fw_dump.cpu_state_data_size;
  207. size += fw_dump.hpte_region_size;
  208. size += fw_dump.boot_memory_size;
  209. size += sizeof(struct fadump_crash_info_header);
  210. size += sizeof(struct elfhdr); /* ELF core header.*/
  211. size += sizeof(struct elf_phdr); /* place holder for cpu notes */
  212. /* Program headers for crash memory regions. */
  213. size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
  214. size = PAGE_ALIGN(size);
  215. return size;
  216. }
  217. int __init fadump_reserve_mem(void)
  218. {
  219. unsigned long base, size, memory_boundary;
  220. if (!fw_dump.fadump_enabled)
  221. return 0;
  222. if (!fw_dump.fadump_supported) {
  223. printk(KERN_INFO "Firmware-assisted dump is not supported on"
  224. " this hardware\n");
  225. fw_dump.fadump_enabled = 0;
  226. return 0;
  227. }
  228. /*
  229. * Initialize boot memory size
  230. * If dump is active then we have already calculated the size during
  231. * first kernel.
  232. */
  233. if (fdm_active)
  234. fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
  235. else
  236. fw_dump.boot_memory_size = fadump_calculate_reserve_size();
  237. /*
  238. * Calculate the memory boundary.
  239. * If memory_limit is less than actual memory boundary then reserve
  240. * the memory for fadump beyond the memory_limit and adjust the
  241. * memory_limit accordingly, so that the running kernel can run with
  242. * specified memory_limit.
  243. */
  244. if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
  245. size = get_fadump_area_size();
  246. if ((memory_limit + size) < memblock_end_of_DRAM())
  247. memory_limit += size;
  248. else
  249. memory_limit = memblock_end_of_DRAM();
  250. printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
  251. " dump, now %#016llx\n", memory_limit);
  252. }
  253. if (memory_limit)
  254. memory_boundary = memory_limit;
  255. else
  256. memory_boundary = memblock_end_of_DRAM();
  257. if (fw_dump.dump_active) {
  258. printk(KERN_INFO "Firmware-assisted dump is active.\n");
  259. /*
  260. * If last boot has crashed then reserve all the memory
  261. * above boot_memory_size so that we don't touch it until
  262. * dump is written to disk by userspace tool. This memory
  263. * will be released for general use once the dump is saved.
  264. */
  265. base = fw_dump.boot_memory_size;
  266. size = memory_boundary - base;
  267. memblock_reserve(base, size);
  268. printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
  269. "for saving crash dump\n",
  270. (unsigned long)(size >> 20),
  271. (unsigned long)(base >> 20));
  272. fw_dump.fadumphdr_addr =
  273. be64_to_cpu(fdm_active->rmr_region.destination_address) +
  274. be64_to_cpu(fdm_active->rmr_region.source_len);
  275. pr_debug("fadumphdr_addr = %p\n",
  276. (void *) fw_dump.fadumphdr_addr);
  277. } else {
  278. /* Reserve the memory at the top of memory. */
  279. size = get_fadump_area_size();
  280. base = memory_boundary - size;
  281. memblock_reserve(base, size);
  282. printk(KERN_INFO "Reserved %ldMB of memory at %ldMB "
  283. "for firmware-assisted dump\n",
  284. (unsigned long)(size >> 20),
  285. (unsigned long)(base >> 20));
  286. }
  287. fw_dump.reserve_dump_area_start = base;
  288. fw_dump.reserve_dump_area_size = size;
  289. return 1;
  290. }
  291. unsigned long __init arch_reserved_kernel_pages(void)
  292. {
  293. return memblock_reserved_size() / PAGE_SIZE;
  294. }
  295. /* Look for fadump= cmdline option. */
  296. static int __init early_fadump_param(char *p)
  297. {
  298. if (!p)
  299. return 1;
  300. if (strncmp(p, "on", 2) == 0)
  301. fw_dump.fadump_enabled = 1;
  302. else if (strncmp(p, "off", 3) == 0)
  303. fw_dump.fadump_enabled = 0;
  304. return 0;
  305. }
  306. early_param("fadump", early_fadump_param);
  307. /* Look for fadump_reserve_mem= cmdline option */
  308. static int __init early_fadump_reserve_mem(char *p)
  309. {
  310. if (p)
  311. fw_dump.reserve_bootvar = memparse(p, &p);
  312. return 0;
  313. }
  314. early_param("fadump_reserve_mem", early_fadump_reserve_mem);
  315. static void register_fw_dump(struct fadump_mem_struct *fdm)
  316. {
  317. int rc;
  318. unsigned int wait_time;
  319. pr_debug("Registering for firmware-assisted kernel dump...\n");
  320. /* TODO: Add upper time limit for the delay */
  321. do {
  322. rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
  323. FADUMP_REGISTER, fdm,
  324. sizeof(struct fadump_mem_struct));
  325. wait_time = rtas_busy_delay_time(rc);
  326. if (wait_time)
  327. mdelay(wait_time);
  328. } while (wait_time);
  329. switch (rc) {
  330. case -1:
  331. printk(KERN_ERR "Failed to register firmware-assisted kernel"
  332. " dump. Hardware Error(%d).\n", rc);
  333. break;
  334. case -3:
  335. printk(KERN_ERR "Failed to register firmware-assisted kernel"
  336. " dump. Parameter Error(%d).\n", rc);
  337. break;
  338. case -9:
  339. printk(KERN_ERR "firmware-assisted kernel dump is already "
  340. " registered.");
  341. fw_dump.dump_registered = 1;
  342. break;
  343. case 0:
  344. printk(KERN_INFO "firmware-assisted kernel dump registration"
  345. " is successful\n");
  346. fw_dump.dump_registered = 1;
  347. break;
  348. }
  349. }
  350. void crash_fadump(struct pt_regs *regs, const char *str)
  351. {
  352. struct fadump_crash_info_header *fdh = NULL;
  353. if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
  354. return;
  355. fdh = __va(fw_dump.fadumphdr_addr);
  356. crashing_cpu = smp_processor_id();
  357. fdh->crashing_cpu = crashing_cpu;
  358. crash_save_vmcoreinfo();
  359. if (regs)
  360. fdh->regs = *regs;
  361. else
  362. ppc_save_regs(&fdh->regs);
  363. fdh->online_mask = *cpu_online_mask;
  364. /* Call ibm,os-term rtas call to trigger firmware assisted dump */
  365. rtas_os_term((char *)str);
  366. }
  367. #define GPR_MASK 0xffffff0000000000
  368. static inline int fadump_gpr_index(u64 id)
  369. {
  370. int i = -1;
  371. char str[3];
  372. if ((id & GPR_MASK) == REG_ID("GPR")) {
  373. /* get the digits at the end */
  374. id &= ~GPR_MASK;
  375. id >>= 24;
  376. str[2] = '\0';
  377. str[1] = id & 0xff;
  378. str[0] = (id >> 8) & 0xff;
  379. sscanf(str, "%d", &i);
  380. if (i > 31)
  381. i = -1;
  382. }
  383. return i;
  384. }
  385. static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
  386. u64 reg_val)
  387. {
  388. int i;
  389. i = fadump_gpr_index(reg_id);
  390. if (i >= 0)
  391. regs->gpr[i] = (unsigned long)reg_val;
  392. else if (reg_id == REG_ID("NIA"))
  393. regs->nip = (unsigned long)reg_val;
  394. else if (reg_id == REG_ID("MSR"))
  395. regs->msr = (unsigned long)reg_val;
  396. else if (reg_id == REG_ID("CTR"))
  397. regs->ctr = (unsigned long)reg_val;
  398. else if (reg_id == REG_ID("LR"))
  399. regs->link = (unsigned long)reg_val;
  400. else if (reg_id == REG_ID("XER"))
  401. regs->xer = (unsigned long)reg_val;
  402. else if (reg_id == REG_ID("CR"))
  403. regs->ccr = (unsigned long)reg_val;
  404. else if (reg_id == REG_ID("DAR"))
  405. regs->dar = (unsigned long)reg_val;
  406. else if (reg_id == REG_ID("DSISR"))
  407. regs->dsisr = (unsigned long)reg_val;
  408. }
  409. static struct fadump_reg_entry*
  410. fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
  411. {
  412. memset(regs, 0, sizeof(struct pt_regs));
  413. while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
  414. fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
  415. be64_to_cpu(reg_entry->reg_value));
  416. reg_entry++;
  417. }
  418. reg_entry++;
  419. return reg_entry;
  420. }
  421. static u32 *fadump_append_elf_note(u32 *buf, char *name, unsigned type,
  422. void *data, size_t data_len)
  423. {
  424. struct elf_note note;
  425. note.n_namesz = strlen(name) + 1;
  426. note.n_descsz = data_len;
  427. note.n_type = type;
  428. memcpy(buf, &note, sizeof(note));
  429. buf += (sizeof(note) + 3)/4;
  430. memcpy(buf, name, note.n_namesz);
  431. buf += (note.n_namesz + 3)/4;
  432. memcpy(buf, data, note.n_descsz);
  433. buf += (note.n_descsz + 3)/4;
  434. return buf;
  435. }
  436. static void fadump_final_note(u32 *buf)
  437. {
  438. struct elf_note note;
  439. note.n_namesz = 0;
  440. note.n_descsz = 0;
  441. note.n_type = 0;
  442. memcpy(buf, &note, sizeof(note));
  443. }
  444. static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
  445. {
  446. struct elf_prstatus prstatus;
  447. memset(&prstatus, 0, sizeof(prstatus));
  448. /*
  449. * FIXME: How do i get PID? Do I really need it?
  450. * prstatus.pr_pid = ????
  451. */
  452. elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
  453. buf = fadump_append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
  454. &prstatus, sizeof(prstatus));
  455. return buf;
  456. }
  457. static void fadump_update_elfcore_header(char *bufp)
  458. {
  459. struct elfhdr *elf;
  460. struct elf_phdr *phdr;
  461. elf = (struct elfhdr *)bufp;
  462. bufp += sizeof(struct elfhdr);
  463. /* First note is a place holder for cpu notes info. */
  464. phdr = (struct elf_phdr *)bufp;
  465. if (phdr->p_type == PT_NOTE) {
  466. phdr->p_paddr = fw_dump.cpu_notes_buf;
  467. phdr->p_offset = phdr->p_paddr;
  468. phdr->p_filesz = fw_dump.cpu_notes_buf_size;
  469. phdr->p_memsz = fw_dump.cpu_notes_buf_size;
  470. }
  471. return;
  472. }
  473. static void *fadump_cpu_notes_buf_alloc(unsigned long size)
  474. {
  475. void *vaddr;
  476. struct page *page;
  477. unsigned long order, count, i;
  478. order = get_order(size);
  479. vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
  480. if (!vaddr)
  481. return NULL;
  482. count = 1 << order;
  483. page = virt_to_page(vaddr);
  484. for (i = 0; i < count; i++)
  485. SetPageReserved(page + i);
  486. return vaddr;
  487. }
  488. static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
  489. {
  490. struct page *page;
  491. unsigned long order, count, i;
  492. order = get_order(size);
  493. count = 1 << order;
  494. page = virt_to_page(vaddr);
  495. for (i = 0; i < count; i++)
  496. ClearPageReserved(page + i);
  497. __free_pages(page, order);
  498. }
  499. /*
  500. * Read CPU state dump data and convert it into ELF notes.
  501. * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
  502. * used to access the data to allow for additional fields to be added without
  503. * affecting compatibility. Each list of registers for a CPU starts with
  504. * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
  505. * 8 Byte ASCII identifier and 8 Byte register value. The register entry
  506. * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
  507. * of register value. For more details refer to PAPR document.
  508. *
  509. * Only for the crashing cpu we ignore the CPU dump data and get exact
  510. * state from fadump crash info structure populated by first kernel at the
  511. * time of crash.
  512. */
  513. static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
  514. {
  515. struct fadump_reg_save_area_header *reg_header;
  516. struct fadump_reg_entry *reg_entry;
  517. struct fadump_crash_info_header *fdh = NULL;
  518. void *vaddr;
  519. unsigned long addr;
  520. u32 num_cpus, *note_buf;
  521. struct pt_regs regs;
  522. int i, rc = 0, cpu = 0;
  523. if (!fdm->cpu_state_data.bytes_dumped)
  524. return -EINVAL;
  525. addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
  526. vaddr = __va(addr);
  527. reg_header = vaddr;
  528. if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
  529. printk(KERN_ERR "Unable to read register save area.\n");
  530. return -ENOENT;
  531. }
  532. pr_debug("--------CPU State Data------------\n");
  533. pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
  534. pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
  535. vaddr += be32_to_cpu(reg_header->num_cpu_offset);
  536. num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
  537. pr_debug("NumCpus : %u\n", num_cpus);
  538. vaddr += sizeof(u32);
  539. reg_entry = (struct fadump_reg_entry *)vaddr;
  540. /* Allocate buffer to hold cpu crash notes. */
  541. fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
  542. fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
  543. note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
  544. if (!note_buf) {
  545. printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
  546. "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
  547. return -ENOMEM;
  548. }
  549. fw_dump.cpu_notes_buf = __pa(note_buf);
  550. pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
  551. (num_cpus * sizeof(note_buf_t)), note_buf);
  552. if (fw_dump.fadumphdr_addr)
  553. fdh = __va(fw_dump.fadumphdr_addr);
  554. for (i = 0; i < num_cpus; i++) {
  555. if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
  556. printk(KERN_ERR "Unable to read CPU state data\n");
  557. rc = -ENOENT;
  558. goto error_out;
  559. }
  560. /* Lower 4 bytes of reg_value contains logical cpu id */
  561. cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
  562. if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
  563. SKIP_TO_NEXT_CPU(reg_entry);
  564. continue;
  565. }
  566. pr_debug("Reading register data for cpu %d...\n", cpu);
  567. if (fdh && fdh->crashing_cpu == cpu) {
  568. regs = fdh->regs;
  569. note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
  570. SKIP_TO_NEXT_CPU(reg_entry);
  571. } else {
  572. reg_entry++;
  573. reg_entry = fadump_read_registers(reg_entry, &regs);
  574. note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
  575. }
  576. }
  577. fadump_final_note(note_buf);
  578. if (fdh) {
  579. pr_debug("Updating elfcore header (%llx) with cpu notes\n",
  580. fdh->elfcorehdr_addr);
  581. fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
  582. }
  583. return 0;
  584. error_out:
  585. fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
  586. fw_dump.cpu_notes_buf_size);
  587. fw_dump.cpu_notes_buf = 0;
  588. fw_dump.cpu_notes_buf_size = 0;
  589. return rc;
  590. }
  591. /*
  592. * Validate and process the dump data stored by firmware before exporting
  593. * it through '/proc/vmcore'.
  594. */
  595. static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
  596. {
  597. struct fadump_crash_info_header *fdh;
  598. int rc = 0;
  599. if (!fdm_active || !fw_dump.fadumphdr_addr)
  600. return -EINVAL;
  601. /* Check if the dump data is valid. */
  602. if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
  603. (fdm_active->cpu_state_data.error_flags != 0) ||
  604. (fdm_active->rmr_region.error_flags != 0)) {
  605. printk(KERN_ERR "Dump taken by platform is not valid\n");
  606. return -EINVAL;
  607. }
  608. if ((fdm_active->rmr_region.bytes_dumped !=
  609. fdm_active->rmr_region.source_len) ||
  610. !fdm_active->cpu_state_data.bytes_dumped) {
  611. printk(KERN_ERR "Dump taken by platform is incomplete\n");
  612. return -EINVAL;
  613. }
  614. /* Validate the fadump crash info header */
  615. fdh = __va(fw_dump.fadumphdr_addr);
  616. if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
  617. printk(KERN_ERR "Crash info header is not valid.\n");
  618. return -EINVAL;
  619. }
  620. rc = fadump_build_cpu_notes(fdm_active);
  621. if (rc)
  622. return rc;
  623. /*
  624. * We are done validating dump info and elfcore header is now ready
  625. * to be exported. set elfcorehdr_addr so that vmcore module will
  626. * export the elfcore header through '/proc/vmcore'.
  627. */
  628. elfcorehdr_addr = fdh->elfcorehdr_addr;
  629. return 0;
  630. }
  631. static inline void fadump_add_crash_memory(unsigned long long base,
  632. unsigned long long end)
  633. {
  634. if (base == end)
  635. return;
  636. pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
  637. crash_mem_ranges, base, end - 1, (end - base));
  638. crash_memory_ranges[crash_mem_ranges].base = base;
  639. crash_memory_ranges[crash_mem_ranges].size = end - base;
  640. crash_mem_ranges++;
  641. }
  642. static void fadump_exclude_reserved_area(unsigned long long start,
  643. unsigned long long end)
  644. {
  645. unsigned long long ra_start, ra_end;
  646. ra_start = fw_dump.reserve_dump_area_start;
  647. ra_end = ra_start + fw_dump.reserve_dump_area_size;
  648. if ((ra_start < end) && (ra_end > start)) {
  649. if ((start < ra_start) && (end > ra_end)) {
  650. fadump_add_crash_memory(start, ra_start);
  651. fadump_add_crash_memory(ra_end, end);
  652. } else if (start < ra_start) {
  653. fadump_add_crash_memory(start, ra_start);
  654. } else if (ra_end < end) {
  655. fadump_add_crash_memory(ra_end, end);
  656. }
  657. } else
  658. fadump_add_crash_memory(start, end);
  659. }
  660. static int fadump_init_elfcore_header(char *bufp)
  661. {
  662. struct elfhdr *elf;
  663. elf = (struct elfhdr *) bufp;
  664. bufp += sizeof(struct elfhdr);
  665. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  666. elf->e_ident[EI_CLASS] = ELF_CLASS;
  667. elf->e_ident[EI_DATA] = ELF_DATA;
  668. elf->e_ident[EI_VERSION] = EV_CURRENT;
  669. elf->e_ident[EI_OSABI] = ELF_OSABI;
  670. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  671. elf->e_type = ET_CORE;
  672. elf->e_machine = ELF_ARCH;
  673. elf->e_version = EV_CURRENT;
  674. elf->e_entry = 0;
  675. elf->e_phoff = sizeof(struct elfhdr);
  676. elf->e_shoff = 0;
  677. #if defined(_CALL_ELF)
  678. elf->e_flags = _CALL_ELF;
  679. #else
  680. elf->e_flags = 0;
  681. #endif
  682. elf->e_ehsize = sizeof(struct elfhdr);
  683. elf->e_phentsize = sizeof(struct elf_phdr);
  684. elf->e_phnum = 0;
  685. elf->e_shentsize = 0;
  686. elf->e_shnum = 0;
  687. elf->e_shstrndx = 0;
  688. return 0;
  689. }
  690. /*
  691. * Traverse through memblock structure and setup crash memory ranges. These
  692. * ranges will be used create PT_LOAD program headers in elfcore header.
  693. */
  694. static void fadump_setup_crash_memory_ranges(void)
  695. {
  696. struct memblock_region *reg;
  697. unsigned long long start, end;
  698. pr_debug("Setup crash memory ranges.\n");
  699. crash_mem_ranges = 0;
  700. /*
  701. * add the first memory chunk (RMA_START through boot_memory_size) as
  702. * a separate memory chunk. The reason is, at the time crash firmware
  703. * will move the content of this memory chunk to different location
  704. * specified during fadump registration. We need to create a separate
  705. * program header for this chunk with the correct offset.
  706. */
  707. fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
  708. for_each_memblock(memory, reg) {
  709. start = (unsigned long long)reg->base;
  710. end = start + (unsigned long long)reg->size;
  711. if (start == RMA_START && end >= fw_dump.boot_memory_size)
  712. start = fw_dump.boot_memory_size;
  713. /* add this range excluding the reserved dump area. */
  714. fadump_exclude_reserved_area(start, end);
  715. }
  716. }
  717. /*
  718. * If the given physical address falls within the boot memory region then
  719. * return the relocated address that points to the dump region reserved
  720. * for saving initial boot memory contents.
  721. */
  722. static inline unsigned long fadump_relocate(unsigned long paddr)
  723. {
  724. if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
  725. return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
  726. else
  727. return paddr;
  728. }
  729. static int fadump_create_elfcore_headers(char *bufp)
  730. {
  731. struct elfhdr *elf;
  732. struct elf_phdr *phdr;
  733. int i;
  734. fadump_init_elfcore_header(bufp);
  735. elf = (struct elfhdr *)bufp;
  736. bufp += sizeof(struct elfhdr);
  737. /*
  738. * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
  739. * will be populated during second kernel boot after crash. Hence
  740. * this PT_NOTE will always be the first elf note.
  741. *
  742. * NOTE: Any new ELF note addition should be placed after this note.
  743. */
  744. phdr = (struct elf_phdr *)bufp;
  745. bufp += sizeof(struct elf_phdr);
  746. phdr->p_type = PT_NOTE;
  747. phdr->p_flags = 0;
  748. phdr->p_vaddr = 0;
  749. phdr->p_align = 0;
  750. phdr->p_offset = 0;
  751. phdr->p_paddr = 0;
  752. phdr->p_filesz = 0;
  753. phdr->p_memsz = 0;
  754. (elf->e_phnum)++;
  755. /* setup ELF PT_NOTE for vmcoreinfo */
  756. phdr = (struct elf_phdr *)bufp;
  757. bufp += sizeof(struct elf_phdr);
  758. phdr->p_type = PT_NOTE;
  759. phdr->p_flags = 0;
  760. phdr->p_vaddr = 0;
  761. phdr->p_align = 0;
  762. phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
  763. phdr->p_offset = phdr->p_paddr;
  764. phdr->p_memsz = vmcoreinfo_max_size;
  765. phdr->p_filesz = vmcoreinfo_max_size;
  766. /* Increment number of program headers. */
  767. (elf->e_phnum)++;
  768. /* setup PT_LOAD sections. */
  769. for (i = 0; i < crash_mem_ranges; i++) {
  770. unsigned long long mbase, msize;
  771. mbase = crash_memory_ranges[i].base;
  772. msize = crash_memory_ranges[i].size;
  773. if (!msize)
  774. continue;
  775. phdr = (struct elf_phdr *)bufp;
  776. bufp += sizeof(struct elf_phdr);
  777. phdr->p_type = PT_LOAD;
  778. phdr->p_flags = PF_R|PF_W|PF_X;
  779. phdr->p_offset = mbase;
  780. if (mbase == RMA_START) {
  781. /*
  782. * The entire RMA region will be moved by firmware
  783. * to the specified destination_address. Hence set
  784. * the correct offset.
  785. */
  786. phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
  787. }
  788. phdr->p_paddr = mbase;
  789. phdr->p_vaddr = (unsigned long)__va(mbase);
  790. phdr->p_filesz = msize;
  791. phdr->p_memsz = msize;
  792. phdr->p_align = 0;
  793. /* Increment number of program headers. */
  794. (elf->e_phnum)++;
  795. }
  796. return 0;
  797. }
  798. static unsigned long init_fadump_header(unsigned long addr)
  799. {
  800. struct fadump_crash_info_header *fdh;
  801. if (!addr)
  802. return 0;
  803. fw_dump.fadumphdr_addr = addr;
  804. fdh = __va(addr);
  805. addr += sizeof(struct fadump_crash_info_header);
  806. memset(fdh, 0, sizeof(struct fadump_crash_info_header));
  807. fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
  808. fdh->elfcorehdr_addr = addr;
  809. /* We will set the crashing cpu id in crash_fadump() during crash. */
  810. fdh->crashing_cpu = CPU_UNKNOWN;
  811. return addr;
  812. }
  813. static void register_fadump(void)
  814. {
  815. unsigned long addr;
  816. void *vaddr;
  817. /*
  818. * If no memory is reserved then we can not register for firmware-
  819. * assisted dump.
  820. */
  821. if (!fw_dump.reserve_dump_area_size)
  822. return;
  823. fadump_setup_crash_memory_ranges();
  824. addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
  825. /* Initialize fadump crash info header. */
  826. addr = init_fadump_header(addr);
  827. vaddr = __va(addr);
  828. pr_debug("Creating ELF core headers at %#016lx\n", addr);
  829. fadump_create_elfcore_headers(vaddr);
  830. /* register the future kernel dump with firmware. */
  831. register_fw_dump(&fdm);
  832. }
  833. static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
  834. {
  835. int rc = 0;
  836. unsigned int wait_time;
  837. pr_debug("Un-register firmware-assisted dump\n");
  838. /* TODO: Add upper time limit for the delay */
  839. do {
  840. rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
  841. FADUMP_UNREGISTER, fdm,
  842. sizeof(struct fadump_mem_struct));
  843. wait_time = rtas_busy_delay_time(rc);
  844. if (wait_time)
  845. mdelay(wait_time);
  846. } while (wait_time);
  847. if (rc) {
  848. printk(KERN_ERR "Failed to un-register firmware-assisted dump."
  849. " unexpected error(%d).\n", rc);
  850. return rc;
  851. }
  852. fw_dump.dump_registered = 0;
  853. return 0;
  854. }
  855. static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
  856. {
  857. int rc = 0;
  858. unsigned int wait_time;
  859. pr_debug("Invalidating firmware-assisted dump registration\n");
  860. /* TODO: Add upper time limit for the delay */
  861. do {
  862. rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
  863. FADUMP_INVALIDATE, fdm,
  864. sizeof(struct fadump_mem_struct));
  865. wait_time = rtas_busy_delay_time(rc);
  866. if (wait_time)
  867. mdelay(wait_time);
  868. } while (wait_time);
  869. if (rc) {
  870. pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
  871. return rc;
  872. }
  873. fw_dump.dump_active = 0;
  874. fdm_active = NULL;
  875. return 0;
  876. }
  877. void fadump_cleanup(void)
  878. {
  879. /* Invalidate the registration only if dump is active. */
  880. if (fw_dump.dump_active) {
  881. init_fadump_mem_struct(&fdm,
  882. be64_to_cpu(fdm_active->cpu_state_data.destination_address));
  883. fadump_invalidate_dump(&fdm);
  884. } else if (fw_dump.dump_registered) {
  885. /* Un-register Firmware-assisted dump if it was registered. */
  886. fadump_unregister_dump(&fdm);
  887. }
  888. }
  889. /*
  890. * Release the memory that was reserved in early boot to preserve the memory
  891. * contents. The released memory will be available for general use.
  892. */
  893. static void fadump_release_memory(unsigned long begin, unsigned long end)
  894. {
  895. unsigned long addr;
  896. unsigned long ra_start, ra_end;
  897. ra_start = fw_dump.reserve_dump_area_start;
  898. ra_end = ra_start + fw_dump.reserve_dump_area_size;
  899. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  900. /*
  901. * exclude the dump reserve area. Will reuse it for next
  902. * fadump registration.
  903. */
  904. if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
  905. continue;
  906. free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
  907. }
  908. }
  909. static void fadump_invalidate_release_mem(void)
  910. {
  911. unsigned long reserved_area_start, reserved_area_end;
  912. unsigned long destination_address;
  913. mutex_lock(&fadump_mutex);
  914. if (!fw_dump.dump_active) {
  915. mutex_unlock(&fadump_mutex);
  916. return;
  917. }
  918. destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
  919. fadump_cleanup();
  920. mutex_unlock(&fadump_mutex);
  921. /*
  922. * Save the current reserved memory bounds we will require them
  923. * later for releasing the memory for general use.
  924. */
  925. reserved_area_start = fw_dump.reserve_dump_area_start;
  926. reserved_area_end = reserved_area_start +
  927. fw_dump.reserve_dump_area_size;
  928. /*
  929. * Setup reserve_dump_area_start and its size so that we can
  930. * reuse this reserved memory for Re-registration.
  931. */
  932. fw_dump.reserve_dump_area_start = destination_address;
  933. fw_dump.reserve_dump_area_size = get_fadump_area_size();
  934. fadump_release_memory(reserved_area_start, reserved_area_end);
  935. if (fw_dump.cpu_notes_buf) {
  936. fadump_cpu_notes_buf_free(
  937. (unsigned long)__va(fw_dump.cpu_notes_buf),
  938. fw_dump.cpu_notes_buf_size);
  939. fw_dump.cpu_notes_buf = 0;
  940. fw_dump.cpu_notes_buf_size = 0;
  941. }
  942. /* Initialize the kernel dump memory structure for FAD registration. */
  943. init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
  944. }
  945. static ssize_t fadump_release_memory_store(struct kobject *kobj,
  946. struct kobj_attribute *attr,
  947. const char *buf, size_t count)
  948. {
  949. if (!fw_dump.dump_active)
  950. return -EPERM;
  951. if (buf[0] == '1') {
  952. /*
  953. * Take away the '/proc/vmcore'. We are releasing the dump
  954. * memory, hence it will not be valid anymore.
  955. */
  956. #ifdef CONFIG_PROC_VMCORE
  957. vmcore_cleanup();
  958. #endif
  959. fadump_invalidate_release_mem();
  960. } else
  961. return -EINVAL;
  962. return count;
  963. }
  964. static ssize_t fadump_enabled_show(struct kobject *kobj,
  965. struct kobj_attribute *attr,
  966. char *buf)
  967. {
  968. return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
  969. }
  970. static ssize_t fadump_register_show(struct kobject *kobj,
  971. struct kobj_attribute *attr,
  972. char *buf)
  973. {
  974. return sprintf(buf, "%d\n", fw_dump.dump_registered);
  975. }
  976. static ssize_t fadump_register_store(struct kobject *kobj,
  977. struct kobj_attribute *attr,
  978. const char *buf, size_t count)
  979. {
  980. int ret = 0;
  981. if (!fw_dump.fadump_enabled || fdm_active)
  982. return -EPERM;
  983. mutex_lock(&fadump_mutex);
  984. switch (buf[0]) {
  985. case '0':
  986. if (fw_dump.dump_registered == 0) {
  987. ret = -EINVAL;
  988. goto unlock_out;
  989. }
  990. /* Un-register Firmware-assisted dump */
  991. fadump_unregister_dump(&fdm);
  992. break;
  993. case '1':
  994. if (fw_dump.dump_registered == 1) {
  995. ret = -EINVAL;
  996. goto unlock_out;
  997. }
  998. /* Register Firmware-assisted dump */
  999. register_fadump();
  1000. break;
  1001. default:
  1002. ret = -EINVAL;
  1003. break;
  1004. }
  1005. unlock_out:
  1006. mutex_unlock(&fadump_mutex);
  1007. return ret < 0 ? ret : count;
  1008. }
  1009. static int fadump_region_show(struct seq_file *m, void *private)
  1010. {
  1011. const struct fadump_mem_struct *fdm_ptr;
  1012. if (!fw_dump.fadump_enabled)
  1013. return 0;
  1014. mutex_lock(&fadump_mutex);
  1015. if (fdm_active)
  1016. fdm_ptr = fdm_active;
  1017. else {
  1018. mutex_unlock(&fadump_mutex);
  1019. fdm_ptr = &fdm;
  1020. }
  1021. seq_printf(m,
  1022. "CPU : [%#016llx-%#016llx] %#llx bytes, "
  1023. "Dumped: %#llx\n",
  1024. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
  1025. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
  1026. be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
  1027. be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
  1028. be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
  1029. seq_printf(m,
  1030. "HPTE: [%#016llx-%#016llx] %#llx bytes, "
  1031. "Dumped: %#llx\n",
  1032. be64_to_cpu(fdm_ptr->hpte_region.destination_address),
  1033. be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
  1034. be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
  1035. be64_to_cpu(fdm_ptr->hpte_region.source_len),
  1036. be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
  1037. seq_printf(m,
  1038. "DUMP: [%#016llx-%#016llx] %#llx bytes, "
  1039. "Dumped: %#llx\n",
  1040. be64_to_cpu(fdm_ptr->rmr_region.destination_address),
  1041. be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
  1042. be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
  1043. be64_to_cpu(fdm_ptr->rmr_region.source_len),
  1044. be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
  1045. if (!fdm_active ||
  1046. (fw_dump.reserve_dump_area_start ==
  1047. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
  1048. goto out;
  1049. /* Dump is active. Show reserved memory region. */
  1050. seq_printf(m,
  1051. " : [%#016llx-%#016llx] %#llx bytes, "
  1052. "Dumped: %#llx\n",
  1053. (unsigned long long)fw_dump.reserve_dump_area_start,
  1054. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
  1055. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
  1056. fw_dump.reserve_dump_area_start,
  1057. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
  1058. fw_dump.reserve_dump_area_start);
  1059. out:
  1060. if (fdm_active)
  1061. mutex_unlock(&fadump_mutex);
  1062. return 0;
  1063. }
  1064. static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
  1065. 0200, NULL,
  1066. fadump_release_memory_store);
  1067. static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
  1068. 0444, fadump_enabled_show,
  1069. NULL);
  1070. static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
  1071. 0644, fadump_register_show,
  1072. fadump_register_store);
  1073. static int fadump_region_open(struct inode *inode, struct file *file)
  1074. {
  1075. return single_open(file, fadump_region_show, inode->i_private);
  1076. }
  1077. static const struct file_operations fadump_region_fops = {
  1078. .open = fadump_region_open,
  1079. .read = seq_read,
  1080. .llseek = seq_lseek,
  1081. .release = single_release,
  1082. };
  1083. static void fadump_init_files(void)
  1084. {
  1085. struct dentry *debugfs_file;
  1086. int rc = 0;
  1087. rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
  1088. if (rc)
  1089. printk(KERN_ERR "fadump: unable to create sysfs file"
  1090. " fadump_enabled (%d)\n", rc);
  1091. rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
  1092. if (rc)
  1093. printk(KERN_ERR "fadump: unable to create sysfs file"
  1094. " fadump_registered (%d)\n", rc);
  1095. debugfs_file = debugfs_create_file("fadump_region", 0444,
  1096. powerpc_debugfs_root, NULL,
  1097. &fadump_region_fops);
  1098. if (!debugfs_file)
  1099. printk(KERN_ERR "fadump: unable to create debugfs file"
  1100. " fadump_region\n");
  1101. if (fw_dump.dump_active) {
  1102. rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
  1103. if (rc)
  1104. printk(KERN_ERR "fadump: unable to create sysfs file"
  1105. " fadump_release_mem (%d)\n", rc);
  1106. }
  1107. return;
  1108. }
  1109. /*
  1110. * Prepare for firmware-assisted dump.
  1111. */
  1112. int __init setup_fadump(void)
  1113. {
  1114. if (!fw_dump.fadump_enabled)
  1115. return 0;
  1116. if (!fw_dump.fadump_supported) {
  1117. printk(KERN_ERR "Firmware-assisted dump is not supported on"
  1118. " this hardware\n");
  1119. return 0;
  1120. }
  1121. fadump_show_config();
  1122. /*
  1123. * If dump data is available then see if it is valid and prepare for
  1124. * saving it to the disk.
  1125. */
  1126. if (fw_dump.dump_active) {
  1127. /*
  1128. * if dump process fails then invalidate the registration
  1129. * and release memory before proceeding for re-registration.
  1130. */
  1131. if (process_fadump(fdm_active) < 0)
  1132. fadump_invalidate_release_mem();
  1133. }
  1134. /* Initialize the kernel dump memory structure for FAD registration. */
  1135. else if (fw_dump.reserve_dump_area_size)
  1136. init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
  1137. fadump_init_files();
  1138. return 1;
  1139. }
  1140. subsys_initcall(setup_fadump);