fadump.c 36 KB

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