fadump.c 41 KB

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