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