Merge tag 'riscv-for-linus-5.13-mw1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / arch / riscv / mm / init.c
index a2f4c14..4faf8bd 100644 (file)
@@ -2,6 +2,8 @@
 /*
  * Copyright (C) 2012 Regents of the University of California
  * Copyright (C) 2019 Western Digital Corporation or its affiliates.
+ * Copyright (C) 2020 FORTH-ICS/CARV
+ *  Nick Kossifidis <mick@ics.forth.gr>
  */
 
 #include <linux/init.h>
 #include <linux/swap.h>
 #include <linux/sizes.h>
 #include <linux/of_fdt.h>
+#include <linux/of_reserved_mem.h>
 #include <linux/libfdt.h>
 #include <linux/set_memory.h>
 #include <linux/dma-map-ops.h>
+#include <linux/crash_dump.h>
 
 #include <asm/fixmap.h>
 #include <asm/tlbflush.h>
@@ -27,6 +31,9 @@
 
 unsigned long kernel_virt_addr = KERNEL_LINK_ADDR;
 EXPORT_SYMBOL(kernel_virt_addr);
+#ifdef CONFIG_XIP_KERNEL
+#define kernel_virt_addr       (*((unsigned long *)XIP_FIXUP(&kernel_virt_addr)))
+#endif
 
 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
                                                        __page_aligned_bss;
@@ -34,8 +41,8 @@ EXPORT_SYMBOL(empty_zero_page);
 
 extern char _start[];
 #define DTB_EARLY_BASE_VA      PGDIR_SIZE
-void *dtb_early_va __initdata;
-uintptr_t dtb_early_pa __initdata;
+void *_dtb_early_va __initdata;
+uintptr_t _dtb_early_pa __initdata;
 
 struct pt_alloc_ops {
        pte_t *(*get_pte_virt)(phys_addr_t pa);
@@ -109,7 +116,6 @@ void __init mem_init(void)
        high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
        memblock_free_all();
 
-       mem_init_print_info(NULL);
        print_vm_layout();
 }
 
@@ -120,16 +126,25 @@ void __init setup_bootmem(void)
        phys_addr_t dram_end = memblock_end_of_DRAM();
        phys_addr_t max_mapped_addr = __pa(~(ulong)0);
 
+#ifdef CONFIG_XIP_KERNEL
+       vmlinux_start = __pa_symbol(&_sdata);
+#endif
+
        /* The maximal physical memory size is -PAGE_OFFSET. */
        memblock_enforce_memory_limit(-PAGE_OFFSET);
 
        /*
         * Reserve from the start of the kernel to the end of the kernel
-        * and make sure we align the reservation on PMD_SIZE since we will
+        */
+#if defined(CONFIG_64BIT) && defined(CONFIG_STRICT_KERNEL_RWX)
+       /*
+        * Make sure we align the reservation on PMD_SIZE since we will
         * map the kernel in the linear mapping as read-only: we do not want
         * any allocation to happen between _end and the next pmd aligned page.
         */
-       memblock_reserve(vmlinux_start, (vmlinux_end - vmlinux_start + PMD_SIZE - 1) & PMD_MASK);
+       vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK;
+#endif
+       memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
 
        /*
         * memblock allocator is not aware of the fact that last 4K bytes of
@@ -161,17 +176,41 @@ void __init setup_bootmem(void)
        memblock_allow_resize();
 }
 
+#ifdef CONFIG_XIP_KERNEL
+
+extern char _xiprom[], _exiprom[];
+extern char _sdata[], _edata[];
+
+#endif /* CONFIG_XIP_KERNEL */
+
 #ifdef CONFIG_MMU
-static struct pt_alloc_ops pt_ops __ro_after_init;
+static struct pt_alloc_ops _pt_ops __ro_after_init;
+
+#ifdef CONFIG_XIP_KERNEL
+#define pt_ops (*(struct pt_alloc_ops *)XIP_FIXUP(&_pt_ops))
+#else
+#define pt_ops _pt_ops
+#endif
 
 /* Offset between linear mapping virtual address and kernel load address */
 unsigned long va_pa_offset __ro_after_init;
 EXPORT_SYMBOL(va_pa_offset);
-#ifdef CONFIG_64BIT
+#ifdef CONFIG_XIP_KERNEL
+#define va_pa_offset   (*((unsigned long *)XIP_FIXUP(&va_pa_offset)))
+#endif
 /* Offset between kernel mapping virtual address and kernel load address */
+#ifdef CONFIG_64BIT
 unsigned long va_kernel_pa_offset;
 EXPORT_SYMBOL(va_kernel_pa_offset);
 #endif
+#ifdef CONFIG_XIP_KERNEL
+#define va_kernel_pa_offset    (*((unsigned long *)XIP_FIXUP(&va_kernel_pa_offset)))
+#endif
+unsigned long va_kernel_xip_pa_offset;
+EXPORT_SYMBOL(va_kernel_xip_pa_offset);
+#ifdef CONFIG_XIP_KERNEL
+#define va_kernel_xip_pa_offset        (*((unsigned long *)XIP_FIXUP(&va_kernel_xip_pa_offset)))
+#endif
 unsigned long pfn_base __ro_after_init;
 EXPORT_SYMBOL(pfn_base);
 
@@ -181,6 +220,12 @@ pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
 
 pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
 
+#ifdef CONFIG_XIP_KERNEL
+#define trampoline_pg_dir      ((pgd_t *)XIP_FIXUP(trampoline_pg_dir))
+#define fixmap_pte             ((pte_t *)XIP_FIXUP(fixmap_pte))
+#define early_pg_dir           ((pgd_t *)XIP_FIXUP(early_pg_dir))
+#endif /* CONFIG_XIP_KERNEL */
+
 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
 {
        unsigned long addr = __fix_to_virt(idx);
@@ -256,6 +301,12 @@ pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
 pmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
 pmd_t early_dtb_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
 
+#ifdef CONFIG_XIP_KERNEL
+#define trampoline_pmd ((pmd_t *)XIP_FIXUP(trampoline_pmd))
+#define fixmap_pmd     ((pmd_t *)XIP_FIXUP(fixmap_pmd))
+#define early_pmd      ((pmd_t *)XIP_FIXUP(early_pmd))
+#endif /* CONFIG_XIP_KERNEL */
+
 static pmd_t *__init get_pmd_virt_early(phys_addr_t pa)
 {
        /* Before MMU is enabled */
@@ -372,6 +423,19 @@ static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
        return PMD_SIZE;
 }
 
+#ifdef CONFIG_XIP_KERNEL
+/* called from head.S with MMU off */
+asmlinkage void __init __copy_data(void)
+{
+       void *from = (void *)(&_sdata);
+       void *end = (void *)(&_end);
+       void *to = (void *)CONFIG_PHYS_RAM_BASE;
+       size_t sz = (size_t)(end - from + 1);
+
+       memcpy(to, from, sz);
+}
+#endif
+
 /*
  * setup_vm() is called from head.S with MMU-off.
  *
@@ -391,7 +455,35 @@ static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
 #endif
 
 uintptr_t load_pa, load_sz;
+#ifdef CONFIG_XIP_KERNEL
+#define load_pa        (*((uintptr_t *)XIP_FIXUP(&load_pa)))
+#define load_sz        (*((uintptr_t *)XIP_FIXUP(&load_sz)))
+#endif
+
+#ifdef CONFIG_XIP_KERNEL
+uintptr_t xiprom, xiprom_sz;
+#define xiprom_sz      (*((uintptr_t *)XIP_FIXUP(&xiprom_sz)))
+#define xiprom         (*((uintptr_t *)XIP_FIXUP(&xiprom)))
 
+static void __init create_kernel_page_table(pgd_t *pgdir, uintptr_t map_size)
+{
+       uintptr_t va, end_va;
+
+       /* Map the flash resident part */
+       end_va = kernel_virt_addr + xiprom_sz;
+       for (va = kernel_virt_addr; va < end_va; va += map_size)
+               create_pgd_mapping(pgdir, va,
+                                  xiprom + (va - kernel_virt_addr),
+                                  map_size, PAGE_KERNEL_EXEC);
+
+       /* Map the data in RAM */
+       end_va = kernel_virt_addr + XIP_OFFSET + load_sz;
+       for (va = kernel_virt_addr + XIP_OFFSET; va < end_va; va += map_size)
+               create_pgd_mapping(pgdir, va,
+                                  load_pa + (va - (kernel_virt_addr + XIP_OFFSET)),
+                                  map_size, PAGE_KERNEL);
+}
+#else
 static void __init create_kernel_page_table(pgd_t *pgdir, uintptr_t map_size)
 {
        uintptr_t va, end_va;
@@ -402,16 +494,28 @@ static void __init create_kernel_page_table(pgd_t *pgdir, uintptr_t map_size)
                                   load_pa + (va - kernel_virt_addr),
                                   map_size, PAGE_KERNEL_EXEC);
 }
+#endif
 
 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
 {
-       uintptr_t pa;
+       uintptr_t __maybe_unused pa;
        uintptr_t map_size;
 #ifndef __PAGETABLE_PMD_FOLDED
        pmd_t fix_bmap_spmd, fix_bmap_epmd;
 #endif
+
+#ifdef CONFIG_XIP_KERNEL
+       xiprom = (uintptr_t)CONFIG_XIP_PHYS_ADDR;
+       xiprom_sz = (uintptr_t)(&_exiprom) - (uintptr_t)(&_xiprom);
+
+       load_pa = (uintptr_t)CONFIG_PHYS_RAM_BASE;
+       load_sz = (uintptr_t)(&_end) - (uintptr_t)(&_sdata);
+
+       va_kernel_xip_pa_offset = kernel_virt_addr - xiprom;
+#else
        load_pa = (uintptr_t)(&_start);
        load_sz = (uintptr_t)(&_end) - load_pa;
+#endif
 
        va_pa_offset = PAGE_OFFSET - load_pa;
 #ifdef CONFIG_64BIT
@@ -447,8 +551,13 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
        /* Setup trampoline PGD and PMD */
        create_pgd_mapping(trampoline_pg_dir, kernel_virt_addr,
                           (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE);
+#ifdef CONFIG_XIP_KERNEL
+       create_pmd_mapping(trampoline_pmd, kernel_virt_addr,
+                          xiprom, PMD_SIZE, PAGE_KERNEL_EXEC);
+#else
        create_pmd_mapping(trampoline_pmd, kernel_virt_addr,
                           load_pa, PMD_SIZE, PAGE_KERNEL_EXEC);
+#endif
 #else
        /* Setup trampoline PGD */
        create_pgd_mapping(trampoline_pg_dir, kernel_virt_addr,
@@ -481,7 +590,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
         * whereas dtb_early_va will be used before setup_vm_final installs
         * the linear mapping.
         */
-       dtb_early_va = kernel_mapping_pa_to_va(dtb_pa);
+       dtb_early_va = kernel_mapping_pa_to_va(XIP_FIXUP(dtb_pa));
 #else
        dtb_early_va = __va(dtb_pa);
 #endif /* CONFIG_64BIT */
@@ -497,7 +606,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
        dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PGDIR_SIZE - 1));
 #else /* CONFIG_BUILTIN_DTB */
 #ifdef CONFIG_64BIT
-       dtb_early_va = kernel_mapping_pa_to_va(dtb_pa);
+       dtb_early_va = kernel_mapping_pa_to_va(XIP_FIXUP(dtb_pa));
 #else
        dtb_early_va = __va(dtb_pa);
 #endif /* CONFIG_64BIT */
@@ -536,7 +645,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
 #endif
 }
 
-#ifdef CONFIG_64BIT
+#if defined(CONFIG_64BIT) && defined(CONFIG_STRICT_KERNEL_RWX)
 void protect_kernel_linear_mapping_text_rodata(void)
 {
        unsigned long text_start = (unsigned long)lm_alias(_start);
@@ -658,6 +767,103 @@ void mark_rodata_ro(void)
 }
 #endif
 
+#ifdef CONFIG_KEXEC_CORE
+/*
+ * reserve_crashkernel() - reserves memory for crash kernel
+ *
+ * This function reserves memory area given in "crashkernel=" kernel command
+ * line parameter. The memory reserved is used by dump capture kernel when
+ * primary kernel is crashing.
+ */
+static void __init reserve_crashkernel(void)
+{
+       unsigned long long crash_base = 0;
+       unsigned long long crash_size = 0;
+       unsigned long search_start = memblock_start_of_DRAM();
+       unsigned long search_end = memblock_end_of_DRAM();
+
+       int ret = 0;
+
+       /*
+        * Don't reserve a region for a crash kernel on a crash kernel
+        * since it doesn't make much sense and we have limited memory
+        * resources.
+        */
+#ifdef CONFIG_CRASH_DUMP
+       if (is_kdump_kernel()) {
+               pr_info("crashkernel: ignoring reservation request\n");
+               return;
+       }
+#endif
+
+       ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
+                               &crash_size, &crash_base);
+       if (ret || !crash_size)
+               return;
+
+       crash_size = PAGE_ALIGN(crash_size);
+
+       if (crash_base == 0) {
+               /*
+                * Current riscv boot protocol requires 2MB alignment for
+                * RV64 and 4MB alignment for RV32 (hugepage size)
+                */
+               crash_base = memblock_find_in_range(search_start, search_end,
+                                                   crash_size, PMD_SIZE);
+
+               if (crash_base == 0) {
+                       pr_warn("crashkernel: couldn't allocate %lldKB\n",
+                               crash_size >> 10);
+                       return;
+               }
+       } else {
+               /* User specifies base address explicitly. */
+               if (!memblock_is_region_memory(crash_base, crash_size)) {
+                       pr_warn("crashkernel: requested region is not memory\n");
+                       return;
+               }
+
+               if (memblock_is_region_reserved(crash_base, crash_size)) {
+                       pr_warn("crashkernel: requested region is reserved\n");
+                       return;
+               }
+
+
+               if (!IS_ALIGNED(crash_base, PMD_SIZE)) {
+                       pr_warn("crashkernel: requested region is misaligned\n");
+                       return;
+               }
+       }
+       memblock_reserve(crash_base, crash_size);
+
+       pr_info("crashkernel: reserved 0x%016llx - 0x%016llx (%lld MB)\n",
+               crash_base, crash_base + crash_size, crash_size >> 20);
+
+       crashk_res.start = crash_base;
+       crashk_res.end = crash_base + crash_size - 1;
+}
+#endif /* CONFIG_KEXEC_CORE */
+
+#ifdef CONFIG_CRASH_DUMP
+/*
+ * We keep track of the ELF core header of the crashed
+ * kernel with a reserved-memory region with compatible
+ * string "linux,elfcorehdr". Here we register a callback
+ * to populate elfcorehdr_addr/size when this region is
+ * present. Note that this region will be marked as
+ * reserved once we call early_init_fdt_scan_reserved_mem()
+ * later on.
+ */
+static int elfcore_hdr_setup(struct reserved_mem *rmem)
+{
+       elfcorehdr_addr = rmem->base;
+       elfcorehdr_size = rmem->size;
+       return 0;
+}
+
+RESERVEDMEM_OF_DECLARE(elfcorehdr, "linux,elfcorehdr", elfcore_hdr_setup);
+#endif
+
 void __init paging_init(void)
 {
        setup_vm_final();
@@ -670,6 +876,9 @@ void __init misc_mem_init(void)
        arch_numa_init();
        sparse_init();
        zone_sizes_init();
+#ifdef CONFIG_KEXEC_CORE
+       reserve_crashkernel();
+#endif
        memblock_dump_all();
 }