fadump.c 35 KB

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