]> Pileus Git - ~andy/linux/blobdiff - arch/powerpc/kernel/fadump.c
ath6kl: fix fw capability parsing
[~andy/linux] / arch / powerpc / kernel / fadump.c
index eb8f782afade14cf85edaed951b3d27ab360e1cc..cfe7a38708c3724d6fc88cc6b110d2917d85b3e9 100644 (file)
@@ -32,6 +32,9 @@
 #include <linux/delay.h>
 #include <linux/debugfs.h>
 #include <linux/seq_file.h>
+#include <linux/crash_dump.h>
+#include <linux/kobject.h>
+#include <linux/sysfs.h>
 
 #include <asm/page.h>
 #include <asm/prom.h>
@@ -43,6 +46,8 @@ static struct fadump_mem_struct fdm;
 static const struct fadump_mem_struct *fdm_active;
 
 static DEFINE_MUTEX(fadump_mutex);
+struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
+int crash_mem_ranges;
 
 /* Scan the Firmware Assisted dump configuration details. */
 int __init early_init_dt_scan_fw_dump(unsigned long node,
@@ -235,6 +240,11 @@ static unsigned long get_fadump_area_size(void)
        size += fw_dump.cpu_state_data_size;
        size += fw_dump.hpte_region_size;
        size += fw_dump.boot_memory_size;
+       size += sizeof(struct fadump_crash_info_header);
+       size += sizeof(struct elfhdr); /* ELF core header.*/
+       size += sizeof(struct elf_phdr); /* place holder for cpu notes */
+       /* Program headers for crash memory regions. */
+       size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
 
        size = PAGE_ALIGN(size);
        return size;
@@ -300,6 +310,12 @@ int __init fadump_reserve_mem(void)
                                "for saving crash dump\n",
                                (unsigned long)(size >> 20),
                                (unsigned long)(base >> 20));
+
+               fw_dump.fadumphdr_addr =
+                               fdm_active->rmr_region.destination_address +
+                               fdm_active->rmr_region.source_len;
+               pr_debug("fadumphdr_addr = %p\n",
+                               (void *) fw_dump.fadumphdr_addr);
        } else {
                /* Reserve the memory at the top of memory. */
                size = get_fadump_area_size();
@@ -380,8 +396,548 @@ static void register_fw_dump(struct fadump_mem_struct *fdm)
        }
 }
 
+void crash_fadump(struct pt_regs *regs, const char *str)
+{
+       struct fadump_crash_info_header *fdh = NULL;
+
+       if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
+               return;
+
+       fdh = __va(fw_dump.fadumphdr_addr);
+       crashing_cpu = smp_processor_id();
+       fdh->crashing_cpu = crashing_cpu;
+       crash_save_vmcoreinfo();
+
+       if (regs)
+               fdh->regs = *regs;
+       else
+               ppc_save_regs(&fdh->regs);
+
+       fdh->cpu_online_mask = *cpu_online_mask;
+
+       /* Call ibm,os-term rtas call to trigger firmware assisted dump */
+       rtas_os_term((char *)str);
+}
+
+#define GPR_MASK       0xffffff0000000000
+static inline int fadump_gpr_index(u64 id)
+{
+       int i = -1;
+       char str[3];
+
+       if ((id & GPR_MASK) == REG_ID("GPR")) {
+               /* get the digits at the end */
+               id &= ~GPR_MASK;
+               id >>= 24;
+               str[2] = '\0';
+               str[1] = id & 0xff;
+               str[0] = (id >> 8) & 0xff;
+               sscanf(str, "%d", &i);
+               if (i > 31)
+                       i = -1;
+       }
+       return i;
+}
+
+static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
+                                                               u64 reg_val)
+{
+       int i;
+
+       i = fadump_gpr_index(reg_id);
+       if (i >= 0)
+               regs->gpr[i] = (unsigned long)reg_val;
+       else if (reg_id == REG_ID("NIA"))
+               regs->nip = (unsigned long)reg_val;
+       else if (reg_id == REG_ID("MSR"))
+               regs->msr = (unsigned long)reg_val;
+       else if (reg_id == REG_ID("CTR"))
+               regs->ctr = (unsigned long)reg_val;
+       else if (reg_id == REG_ID("LR"))
+               regs->link = (unsigned long)reg_val;
+       else if (reg_id == REG_ID("XER"))
+               regs->xer = (unsigned long)reg_val;
+       else if (reg_id == REG_ID("CR"))
+               regs->ccr = (unsigned long)reg_val;
+       else if (reg_id == REG_ID("DAR"))
+               regs->dar = (unsigned long)reg_val;
+       else if (reg_id == REG_ID("DSISR"))
+               regs->dsisr = (unsigned long)reg_val;
+}
+
+static struct fadump_reg_entry*
+fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
+{
+       memset(regs, 0, sizeof(struct pt_regs));
+
+       while (reg_entry->reg_id != REG_ID("CPUEND")) {
+               fadump_set_regval(regs, reg_entry->reg_id,
+                                       reg_entry->reg_value);
+               reg_entry++;
+       }
+       reg_entry++;
+       return reg_entry;
+}
+
+static u32 *fadump_append_elf_note(u32 *buf, char *name, unsigned type,
+                                               void *data, size_t data_len)
+{
+       struct elf_note note;
+
+       note.n_namesz = strlen(name) + 1;
+       note.n_descsz = data_len;
+       note.n_type   = type;
+       memcpy(buf, &note, sizeof(note));
+       buf += (sizeof(note) + 3)/4;
+       memcpy(buf, name, note.n_namesz);
+       buf += (note.n_namesz + 3)/4;
+       memcpy(buf, data, note.n_descsz);
+       buf += (note.n_descsz + 3)/4;
+
+       return buf;
+}
+
+static void fadump_final_note(u32 *buf)
+{
+       struct elf_note note;
+
+       note.n_namesz = 0;
+       note.n_descsz = 0;
+       note.n_type   = 0;
+       memcpy(buf, &note, sizeof(note));
+}
+
+static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
+{
+       struct elf_prstatus prstatus;
+
+       memset(&prstatus, 0, sizeof(prstatus));
+       /*
+        * FIXME: How do i get PID? Do I really need it?
+        * prstatus.pr_pid = ????
+        */
+       elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
+       buf = fadump_append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
+                               &prstatus, sizeof(prstatus));
+       return buf;
+}
+
+static void fadump_update_elfcore_header(char *bufp)
+{
+       struct elfhdr *elf;
+       struct elf_phdr *phdr;
+
+       elf = (struct elfhdr *)bufp;
+       bufp += sizeof(struct elfhdr);
+
+       /* First note is a place holder for cpu notes info. */
+       phdr = (struct elf_phdr *)bufp;
+
+       if (phdr->p_type == PT_NOTE) {
+               phdr->p_paddr = fw_dump.cpu_notes_buf;
+               phdr->p_offset  = phdr->p_paddr;
+               phdr->p_filesz  = fw_dump.cpu_notes_buf_size;
+               phdr->p_memsz = fw_dump.cpu_notes_buf_size;
+       }
+       return;
+}
+
+static void *fadump_cpu_notes_buf_alloc(unsigned long size)
+{
+       void *vaddr;
+       struct page *page;
+       unsigned long order, count, i;
+
+       order = get_order(size);
+       vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
+       if (!vaddr)
+               return NULL;
+
+       count = 1 << order;
+       page = virt_to_page(vaddr);
+       for (i = 0; i < count; i++)
+               SetPageReserved(page + i);
+       return vaddr;
+}
+
+static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
+{
+       struct page *page;
+       unsigned long order, count, i;
+
+       order = get_order(size);
+       count = 1 << order;
+       page = virt_to_page(vaddr);
+       for (i = 0; i < count; i++)
+               ClearPageReserved(page + i);
+       __free_pages(page, order);
+}
+
+/*
+ * Read CPU state dump data and convert it into ELF notes.
+ * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
+ * used to access the data to allow for additional fields to be added without
+ * affecting compatibility. Each list of registers for a CPU starts with
+ * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
+ * 8 Byte ASCII identifier and 8 Byte register value. The register entry
+ * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
+ * of register value. For more details refer to PAPR document.
+ *
+ * Only for the crashing cpu we ignore the CPU dump data and get exact
+ * state from fadump crash info structure populated by first kernel at the
+ * time of crash.
+ */
+static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
+{
+       struct fadump_reg_save_area_header *reg_header;
+       struct fadump_reg_entry *reg_entry;
+       struct fadump_crash_info_header *fdh = NULL;
+       void *vaddr;
+       unsigned long addr;
+       u32 num_cpus, *note_buf;
+       struct pt_regs regs;
+       int i, rc = 0, cpu = 0;
+
+       if (!fdm->cpu_state_data.bytes_dumped)
+               return -EINVAL;
+
+       addr = fdm->cpu_state_data.destination_address;
+       vaddr = __va(addr);
+
+       reg_header = vaddr;
+       if (reg_header->magic_number != REGSAVE_AREA_MAGIC) {
+               printk(KERN_ERR "Unable to read register save area.\n");
+               return -ENOENT;
+       }
+       pr_debug("--------CPU State Data------------\n");
+       pr_debug("Magic Number: %llx\n", reg_header->magic_number);
+       pr_debug("NumCpuOffset: %x\n", reg_header->num_cpu_offset);
+
+       vaddr += reg_header->num_cpu_offset;
+       num_cpus = *((u32 *)(vaddr));
+       pr_debug("NumCpus     : %u\n", num_cpus);
+       vaddr += sizeof(u32);
+       reg_entry = (struct fadump_reg_entry *)vaddr;
+
+       /* Allocate buffer to hold cpu crash notes. */
+       fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
+       fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
+       note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
+       if (!note_buf) {
+               printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
+                       "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
+               return -ENOMEM;
+       }
+       fw_dump.cpu_notes_buf = __pa(note_buf);
+
+       pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
+                       (num_cpus * sizeof(note_buf_t)), note_buf);
+
+       if (fw_dump.fadumphdr_addr)
+               fdh = __va(fw_dump.fadumphdr_addr);
+
+       for (i = 0; i < num_cpus; i++) {
+               if (reg_entry->reg_id != REG_ID("CPUSTRT")) {
+                       printk(KERN_ERR "Unable to read CPU state data\n");
+                       rc = -ENOENT;
+                       goto error_out;
+               }
+               /* Lower 4 bytes of reg_value contains logical cpu id */
+               cpu = reg_entry->reg_value & FADUMP_CPU_ID_MASK;
+               if (!cpumask_test_cpu(cpu, &fdh->cpu_online_mask)) {
+                       SKIP_TO_NEXT_CPU(reg_entry);
+                       continue;
+               }
+               pr_debug("Reading register data for cpu %d...\n", cpu);
+               if (fdh && fdh->crashing_cpu == cpu) {
+                       regs = fdh->regs;
+                       note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
+                       SKIP_TO_NEXT_CPU(reg_entry);
+               } else {
+                       reg_entry++;
+                       reg_entry = fadump_read_registers(reg_entry, &regs);
+                       note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
+               }
+       }
+       fadump_final_note(note_buf);
+
+       pr_debug("Updating elfcore header (%llx) with cpu notes\n",
+                                                       fdh->elfcorehdr_addr);
+       fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
+       return 0;
+
+error_out:
+       fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
+                                       fw_dump.cpu_notes_buf_size);
+       fw_dump.cpu_notes_buf = 0;
+       fw_dump.cpu_notes_buf_size = 0;
+       return rc;
+
+}
+
+/*
+ * Validate and process the dump data stored by firmware before exporting
+ * it through '/proc/vmcore'.
+ */
+static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
+{
+       struct fadump_crash_info_header *fdh;
+       int rc = 0;
+
+       if (!fdm_active || !fw_dump.fadumphdr_addr)
+               return -EINVAL;
+
+       /* Check if the dump data is valid. */
+       if ((fdm_active->header.dump_status_flag == FADUMP_ERROR_FLAG) ||
+                       (fdm_active->cpu_state_data.error_flags != 0) ||
+                       (fdm_active->rmr_region.error_flags != 0)) {
+               printk(KERN_ERR "Dump taken by platform is not valid\n");
+               return -EINVAL;
+       }
+       if ((fdm_active->rmr_region.bytes_dumped !=
+                       fdm_active->rmr_region.source_len) ||
+                       !fdm_active->cpu_state_data.bytes_dumped) {
+               printk(KERN_ERR "Dump taken by platform is incomplete\n");
+               return -EINVAL;
+       }
+
+       /* Validate the fadump crash info header */
+       fdh = __va(fw_dump.fadumphdr_addr);
+       if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
+               printk(KERN_ERR "Crash info header is not valid.\n");
+               return -EINVAL;
+       }
+
+       rc = fadump_build_cpu_notes(fdm_active);
+       if (rc)
+               return rc;
+
+       /*
+        * We are done validating dump info and elfcore header is now ready
+        * to be exported. set elfcorehdr_addr so that vmcore module will
+        * export the elfcore header through '/proc/vmcore'.
+        */
+       elfcorehdr_addr = fdh->elfcorehdr_addr;
+
+       return 0;
+}
+
+static inline void fadump_add_crash_memory(unsigned long long base,
+                                       unsigned long long end)
+{
+       if (base == end)
+               return;
+
+       pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
+               crash_mem_ranges, base, end - 1, (end - base));
+       crash_memory_ranges[crash_mem_ranges].base = base;
+       crash_memory_ranges[crash_mem_ranges].size = end - base;
+       crash_mem_ranges++;
+}
+
+static void fadump_exclude_reserved_area(unsigned long long start,
+                                       unsigned long long end)
+{
+       unsigned long long ra_start, ra_end;
+
+       ra_start = fw_dump.reserve_dump_area_start;
+       ra_end = ra_start + fw_dump.reserve_dump_area_size;
+
+       if ((ra_start < end) && (ra_end > start)) {
+               if ((start < ra_start) && (end > ra_end)) {
+                       fadump_add_crash_memory(start, ra_start);
+                       fadump_add_crash_memory(ra_end, end);
+               } else if (start < ra_start) {
+                       fadump_add_crash_memory(start, ra_start);
+               } else if (ra_end < end) {
+                       fadump_add_crash_memory(ra_end, end);
+               }
+       } else
+               fadump_add_crash_memory(start, end);
+}
+
+static int fadump_init_elfcore_header(char *bufp)
+{
+       struct elfhdr *elf;
+
+       elf = (struct elfhdr *) bufp;
+       bufp += sizeof(struct elfhdr);
+       memcpy(elf->e_ident, ELFMAG, SELFMAG);
+       elf->e_ident[EI_CLASS] = ELF_CLASS;
+       elf->e_ident[EI_DATA] = ELF_DATA;
+       elf->e_ident[EI_VERSION] = EV_CURRENT;
+       elf->e_ident[EI_OSABI] = ELF_OSABI;
+       memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
+       elf->e_type = ET_CORE;
+       elf->e_machine = ELF_ARCH;
+       elf->e_version = EV_CURRENT;
+       elf->e_entry = 0;
+       elf->e_phoff = sizeof(struct elfhdr);
+       elf->e_shoff = 0;
+       elf->e_flags = ELF_CORE_EFLAGS;
+       elf->e_ehsize = sizeof(struct elfhdr);
+       elf->e_phentsize = sizeof(struct elf_phdr);
+       elf->e_phnum = 0;
+       elf->e_shentsize = 0;
+       elf->e_shnum = 0;
+       elf->e_shstrndx = 0;
+
+       return 0;
+}
+
+/*
+ * Traverse through memblock structure and setup crash memory ranges. These
+ * ranges will be used create PT_LOAD program headers in elfcore header.
+ */
+static void fadump_setup_crash_memory_ranges(void)
+{
+       struct memblock_region *reg;
+       unsigned long long start, end;
+
+       pr_debug("Setup crash memory ranges.\n");
+       crash_mem_ranges = 0;
+       /*
+        * add the first memory chunk (RMA_START through boot_memory_size) as
+        * a separate memory chunk. The reason is, at the time crash firmware
+        * will move the content of this memory chunk to different location
+        * specified during fadump registration. We need to create a separate
+        * program header for this chunk with the correct offset.
+        */
+       fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
+
+       for_each_memblock(memory, reg) {
+               start = (unsigned long long)reg->base;
+               end = start + (unsigned long long)reg->size;
+               if (start == RMA_START && end >= fw_dump.boot_memory_size)
+                       start = fw_dump.boot_memory_size;
+
+               /* add this range excluding the reserved dump area. */
+               fadump_exclude_reserved_area(start, end);
+       }
+}
+
+/*
+ * If the given physical address falls within the boot memory region then
+ * return the relocated address that points to the dump region reserved
+ * for saving initial boot memory contents.
+ */
+static inline unsigned long fadump_relocate(unsigned long paddr)
+{
+       if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
+               return fdm.rmr_region.destination_address + paddr;
+       else
+               return paddr;
+}
+
+static int fadump_create_elfcore_headers(char *bufp)
+{
+       struct elfhdr *elf;
+       struct elf_phdr *phdr;
+       int i;
+
+       fadump_init_elfcore_header(bufp);
+       elf = (struct elfhdr *)bufp;
+       bufp += sizeof(struct elfhdr);
+
+       /*
+        * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
+        * will be populated during second kernel boot after crash. Hence
+        * this PT_NOTE will always be the first elf note.
+        *
+        * NOTE: Any new ELF note addition should be placed after this note.
+        */
+       phdr = (struct elf_phdr *)bufp;
+       bufp += sizeof(struct elf_phdr);
+       phdr->p_type = PT_NOTE;
+       phdr->p_flags = 0;
+       phdr->p_vaddr = 0;
+       phdr->p_align = 0;
+
+       phdr->p_offset = 0;
+       phdr->p_paddr = 0;
+       phdr->p_filesz = 0;
+       phdr->p_memsz = 0;
+
+       (elf->e_phnum)++;
+
+       /* setup ELF PT_NOTE for vmcoreinfo */
+       phdr = (struct elf_phdr *)bufp;
+       bufp += sizeof(struct elf_phdr);
+       phdr->p_type    = PT_NOTE;
+       phdr->p_flags   = 0;
+       phdr->p_vaddr   = 0;
+       phdr->p_align   = 0;
+
+       phdr->p_paddr   = fadump_relocate(paddr_vmcoreinfo_note());
+       phdr->p_offset  = phdr->p_paddr;
+       phdr->p_memsz   = vmcoreinfo_max_size;
+       phdr->p_filesz  = vmcoreinfo_max_size;
+
+       /* Increment number of program headers. */
+       (elf->e_phnum)++;
+
+       /* setup PT_LOAD sections. */
+
+       for (i = 0; i < crash_mem_ranges; i++) {
+               unsigned long long mbase, msize;
+               mbase = crash_memory_ranges[i].base;
+               msize = crash_memory_ranges[i].size;
+
+               if (!msize)
+                       continue;
+
+               phdr = (struct elf_phdr *)bufp;
+               bufp += sizeof(struct elf_phdr);
+               phdr->p_type    = PT_LOAD;
+               phdr->p_flags   = PF_R|PF_W|PF_X;
+               phdr->p_offset  = mbase;
+
+               if (mbase == RMA_START) {
+                       /*
+                        * The entire RMA region will be moved by firmware
+                        * to the specified destination_address. Hence set
+                        * the correct offset.
+                        */
+                       phdr->p_offset = fdm.rmr_region.destination_address;
+               }
+
+               phdr->p_paddr = mbase;
+               phdr->p_vaddr = (unsigned long)__va(mbase);
+               phdr->p_filesz = msize;
+               phdr->p_memsz = msize;
+               phdr->p_align = 0;
+
+               /* Increment number of program headers. */
+               (elf->e_phnum)++;
+       }
+       return 0;
+}
+
+static unsigned long init_fadump_header(unsigned long addr)
+{
+       struct fadump_crash_info_header *fdh;
+
+       if (!addr)
+               return 0;
+
+       fw_dump.fadumphdr_addr = addr;
+       fdh = __va(addr);
+       addr += sizeof(struct fadump_crash_info_header);
+
+       memset(fdh, 0, sizeof(struct fadump_crash_info_header));
+       fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
+       fdh->elfcorehdr_addr = addr;
+       /* We will set the crashing cpu id in crash_fadump() during crash. */
+       fdh->crashing_cpu = CPU_UNKNOWN;
+
+       return addr;
+}
+
 static void register_fadump(void)
 {
+       unsigned long addr;
+       void *vaddr;
+
        /*
         * If no memory is reserved then we can not register for firmware-
         * assisted dump.
@@ -389,6 +945,16 @@ static void register_fadump(void)
        if (!fw_dump.reserve_dump_area_size)
                return;
 
+       fadump_setup_crash_memory_ranges();
+
+       addr = fdm.rmr_region.destination_address + fdm.rmr_region.source_len;
+       /* Initialize fadump crash info header. */
+       addr = init_fadump_header(addr);
+       vaddr = __va(addr);
+
+       pr_debug("Creating ELF core headers at %#016lx\n", addr);
+       fadump_create_elfcore_headers(vaddr);
+
        /* register the future kernel dump with firmware. */
        register_fw_dump(&fdm);
 }
@@ -420,6 +986,132 @@ static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
        return 0;
 }
 
+static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
+{
+       int rc = 0;
+       unsigned int wait_time;
+
+       pr_debug("Invalidating firmware-assisted dump registration\n");
+
+       /* TODO: Add upper time limit for the delay */
+       do {
+               rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
+                       FADUMP_INVALIDATE, fdm,
+                       sizeof(struct fadump_mem_struct));
+
+               wait_time = rtas_busy_delay_time(rc);
+               if (wait_time)
+                       mdelay(wait_time);
+       } while (wait_time);
+
+       if (rc) {
+               printk(KERN_ERR "Failed to invalidate firmware-assisted dump "
+                       "rgistration. unexpected error(%d).\n", rc);
+               return rc;
+       }
+       fw_dump.dump_active = 0;
+       fdm_active = NULL;
+       return 0;
+}
+
+void fadump_cleanup(void)
+{
+       /* Invalidate the registration only if dump is active. */
+       if (fw_dump.dump_active) {
+               init_fadump_mem_struct(&fdm,
+                       fdm_active->cpu_state_data.destination_address);
+               fadump_invalidate_dump(&fdm);
+       }
+}
+
+/*
+ * Release the memory that was reserved in early boot to preserve the memory
+ * contents. The released memory will be available for general use.
+ */
+static void fadump_release_memory(unsigned long begin, unsigned long end)
+{
+       unsigned long addr;
+       unsigned long ra_start, ra_end;
+
+       ra_start = fw_dump.reserve_dump_area_start;
+       ra_end = ra_start + fw_dump.reserve_dump_area_size;
+
+       for (addr = begin; addr < end; addr += PAGE_SIZE) {
+               /*
+                * exclude the dump reserve area. Will reuse it for next
+                * fadump registration.
+                */
+               if (addr <= ra_end && ((addr + PAGE_SIZE) > ra_start))
+                       continue;
+
+               ClearPageReserved(pfn_to_page(addr >> PAGE_SHIFT));
+               init_page_count(pfn_to_page(addr >> PAGE_SHIFT));
+               free_page((unsigned long)__va(addr));
+               totalram_pages++;
+       }
+}
+
+static void fadump_invalidate_release_mem(void)
+{
+       unsigned long reserved_area_start, reserved_area_end;
+       unsigned long destination_address;
+
+       mutex_lock(&fadump_mutex);
+       if (!fw_dump.dump_active) {
+               mutex_unlock(&fadump_mutex);
+               return;
+       }
+
+       destination_address = fdm_active->cpu_state_data.destination_address;
+       fadump_cleanup();
+       mutex_unlock(&fadump_mutex);
+
+       /*
+        * Save the current reserved memory bounds we will require them
+        * later for releasing the memory for general use.
+        */
+       reserved_area_start = fw_dump.reserve_dump_area_start;
+       reserved_area_end = reserved_area_start +
+                       fw_dump.reserve_dump_area_size;
+       /*
+        * Setup reserve_dump_area_start and its size so that we can
+        * reuse this reserved memory for Re-registration.
+        */
+       fw_dump.reserve_dump_area_start = destination_address;
+       fw_dump.reserve_dump_area_size = get_fadump_area_size();
+
+       fadump_release_memory(reserved_area_start, reserved_area_end);
+       if (fw_dump.cpu_notes_buf) {
+               fadump_cpu_notes_buf_free(
+                               (unsigned long)__va(fw_dump.cpu_notes_buf),
+                               fw_dump.cpu_notes_buf_size);
+               fw_dump.cpu_notes_buf = 0;
+               fw_dump.cpu_notes_buf_size = 0;
+       }
+       /* Initialize the kernel dump memory structure for FAD registration. */
+       init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
+}
+
+static ssize_t fadump_release_memory_store(struct kobject *kobj,
+                                       struct kobj_attribute *attr,
+                                       const char *buf, size_t count)
+{
+       if (!fw_dump.dump_active)
+               return -EPERM;
+
+       if (buf[0] == '1') {
+               /*
+                * Take away the '/proc/vmcore'. We are releasing the dump
+                * memory, hence it will not be valid anymore.
+                */
+               vmcore_cleanup();
+               fadump_invalidate_release_mem();
+
+       } else
+               return -EINVAL;
+       return count;
+}
+
 static ssize_t fadump_enabled_show(struct kobject *kobj,
                                        struct kobj_attribute *attr,
                                        char *buf)
@@ -479,10 +1171,13 @@ static int fadump_region_show(struct seq_file *m, void *private)
        if (!fw_dump.fadump_enabled)
                return 0;
 
+       mutex_lock(&fadump_mutex);
        if (fdm_active)
                fdm_ptr = fdm_active;
-       else
+       else {
+               mutex_unlock(&fadump_mutex);
                fdm_ptr = &fdm;
+       }
 
        seq_printf(m,
                        "CPU : [%#016llx-%#016llx] %#llx bytes, "
@@ -512,7 +1207,7 @@ static int fadump_region_show(struct seq_file *m, void *private)
        if (!fdm_active ||
                (fw_dump.reserve_dump_area_start ==
                fdm_ptr->cpu_state_data.destination_address))
-               return 0;
+               goto out;
 
        /* Dump is active. Show reserved memory region. */
        seq_printf(m,
@@ -524,9 +1219,15 @@ static int fadump_region_show(struct seq_file *m, void *private)
                        fw_dump.reserve_dump_area_start,
                        fdm_ptr->cpu_state_data.destination_address -
                        fw_dump.reserve_dump_area_start);
+out:
+       if (fdm_active)
+               mutex_unlock(&fadump_mutex);
        return 0;
 }
 
+static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
+                                               0200, NULL,
+                                               fadump_release_memory_store);
 static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
                                                0444, fadump_enabled_show,
                                                NULL);
@@ -567,6 +1268,13 @@ static void fadump_init_files(void)
        if (!debugfs_file)
                printk(KERN_ERR "fadump: unable to create debugfs file"
                                " fadump_region\n");
+
+       if (fw_dump.dump_active) {
+               rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
+               if (rc)
+                       printk(KERN_ERR "fadump: unable to create sysfs file"
+                               " fadump_release_mem (%d)\n", rc);
+       }
        return;
 }
 
@@ -585,8 +1293,20 @@ int __init setup_fadump(void)
        }
 
        fadump_show_config();
+       /*
+        * If dump data is available then see if it is valid and prepare for
+        * saving it to the disk.
+        */
+       if (fw_dump.dump_active) {
+               /*
+                * if dump process fails then invalidate the registration
+                * and release memory before proceeding for re-registration.
+                */
+               if (process_fadump(fdm_active) < 0)
+                       fadump_invalidate_release_mem();
+       }
        /* Initialize the kernel dump memory structure for FAD registration. */
-       if (fw_dump.reserve_dump_area_size)
+       else if (fw_dump.reserve_dump_area_size)
                init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
        fadump_init_files();