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