fadump.c 35 KB

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