fadump.c 35 KB

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