riscv: Move setup_bootmem into paging_init
[linux-2.6-microblaze.git] / arch / riscv / kernel / setup.c
index f8f1533..4db4d0b 100644 (file)
 #include <linux/swiotlb.h>
 #include <linux/smp.h>
 #include <linux/efi.h>
+#include <linux/crash_dump.h>
 
 #include <asm/cpu_ops.h>
 #include <asm/early_ioremap.h>
+#include <asm/pgtable.h>
 #include <asm/setup.h>
 #include <asm/set_memory.h>
 #include <asm/sections.h>
@@ -50,7 +52,11 @@ struct screen_info screen_info __section(".data") = {
  * This is used before the kernel initializes the BSS so it can't be in the
  * BSS.
  */
-atomic_t hart_lottery __section(".sdata");
+atomic_t hart_lottery __section(".sdata")
+#ifdef CONFIG_XIP_KERNEL
+= ATOMIC_INIT(0xC001BEEF)
+#endif
+;
 unsigned long boot_cpu_hartid;
 static DEFINE_PER_CPU(struct cpu, cpu_devices);
 
@@ -60,10 +66,14 @@ static DEFINE_PER_CPU(struct cpu, cpu_devices);
  * also add "System RAM" regions for compatibility with other
  * archs, and the rest of the known regions for completeness.
  */
+static struct resource kimage_res = { .name = "Kernel image", };
 static struct resource code_res = { .name = "Kernel code", };
 static struct resource data_res = { .name = "Kernel data", };
 static struct resource rodata_res = { .name = "Kernel rodata", };
 static struct resource bss_res = { .name = "Kernel bss", };
+#ifdef CONFIG_CRASH_DUMP
+static struct resource elfcorehdr_res = { .name = "ELF Core hdr", };
+#endif
 
 static int __init add_resource(struct resource *parent,
                                struct resource *res)
@@ -80,45 +90,54 @@ static int __init add_resource(struct resource *parent,
        return 1;
 }
 
-static int __init add_kernel_resources(struct resource *res)
+static int __init add_kernel_resources(void)
 {
        int ret = 0;
 
        /*
         * The memory region of the kernel image is continuous and
-        * was reserved on setup_bootmem, find it here and register
-        * it as a resource, then register the various segments of
-        * the image as child nodes
+        * was reserved on setup_bootmem, register it here as a
+        * resource, with the various segments of the image as
+        * child nodes.
         */
-       if (!(res->start <= code_res.start && res->end >= data_res.end))
-               return 0;
 
-       res->name = "Kernel image";
-       res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
+       code_res.start = __pa_symbol(_text);
+       code_res.end = __pa_symbol(_etext) - 1;
+       code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
 
-       /*
-        * We removed a part of this region on setup_bootmem so
-        * we need to expand the resource for the bss to fit in.
-        */
-       res->end = bss_res.end;
+       rodata_res.start = __pa_symbol(__start_rodata);
+       rodata_res.end = __pa_symbol(__end_rodata) - 1;
+       rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
 
-       ret = add_resource(&iomem_resource, res);
+       data_res.start = __pa_symbol(_data);
+       data_res.end = __pa_symbol(_edata) - 1;
+       data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
+
+       bss_res.start = __pa_symbol(__bss_start);
+       bss_res.end = __pa_symbol(__bss_stop) - 1;
+       bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
+
+       kimage_res.start = code_res.start;
+       kimage_res.end = bss_res.end;
+       kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
+
+       ret = add_resource(&iomem_resource, &kimage_res);
        if (ret < 0)
                return ret;
 
-       ret = add_resource(res, &code_res);
+       ret = add_resource(&kimage_res, &code_res);
        if (ret < 0)
                return ret;
 
-       ret = add_resource(res, &rodata_res);
+       ret = add_resource(&kimage_res, &rodata_res);
        if (ret < 0)
                return ret;
 
-       ret = add_resource(res, &data_res);
+       ret = add_resource(&kimage_res, &data_res);
        if (ret < 0)
                return ret;
 
-       ret = add_resource(res, &bss_res);
+       ret = add_resource(&kimage_res, &bss_res);
 
        return ret;
 }
@@ -129,54 +148,59 @@ static void __init init_resources(void)
        struct resource *res = NULL;
        struct resource *mem_res = NULL;
        size_t mem_res_sz = 0;
-       int ret = 0, i = 0;
-
-       code_res.start = __pa_symbol(_text);
-       code_res.end = __pa_symbol(_etext) - 1;
-       code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
-
-       rodata_res.start = __pa_symbol(__start_rodata);
-       rodata_res.end = __pa_symbol(__end_rodata) - 1;
-       rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
-
-       data_res.start = __pa_symbol(_data);
-       data_res.end = __pa_symbol(_edata) - 1;
-       data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
-
-       bss_res.start = __pa_symbol(__bss_start);
-       bss_res.end = __pa_symbol(__bss_stop) - 1;
-       bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
+       int num_resources = 0, res_idx = 0;
+       int ret = 0;
 
        /* + 1 as memblock_alloc() might increase memblock.reserved.cnt */
-       mem_res_sz = (memblock.memory.cnt + memblock.reserved.cnt + 1) * sizeof(*mem_res);
+       num_resources = memblock.memory.cnt + memblock.reserved.cnt + 1;
+       res_idx = num_resources - 1;
+
+       mem_res_sz = num_resources * sizeof(*mem_res);
        mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
        if (!mem_res)
                panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
+
        /*
         * Start by adding the reserved regions, if they overlap
         * with /memory regions, insert_resource later on will take
         * care of it.
         */
+       ret = add_kernel_resources();
+       if (ret < 0)
+               goto error;
+
+#ifdef CONFIG_KEXEC_CORE
+       if (crashk_res.start != crashk_res.end) {
+               ret = add_resource(&iomem_resource, &crashk_res);
+               if (ret < 0)
+                       goto error;
+       }
+#endif
+
+#ifdef CONFIG_CRASH_DUMP
+       if (elfcorehdr_size > 0) {
+               elfcorehdr_res.start = elfcorehdr_addr;
+               elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
+               elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
+               add_resource(&iomem_resource, &elfcorehdr_res);
+       }
+#endif
+
        for_each_reserved_mem_region(region) {
-               res = &mem_res[i++];
+               res = &mem_res[res_idx--];
 
                res->name = "Reserved";
                res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
                res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
                res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
 
-               ret = add_kernel_resources(res);
-               if (ret < 0)
-                       goto error;
-               else if (ret)
-                       continue;
-
                /*
                 * Ignore any other reserved regions within
                 * system memory.
                 */
                if (memblock_is_memory(res->start)) {
-                       memblock_free((phys_addr_t) res, sizeof(struct resource));
+                       /* Re-use this pre-allocated resource */
+                       res_idx++;
                        continue;
                }
 
@@ -187,7 +211,7 @@ static void __init init_resources(void)
 
        /* Add /memory regions to the resource tree */
        for_each_mem_region(region) {
-               res = &mem_res[i++];
+               res = &mem_res[res_idx--];
 
                if (unlikely(memblock_is_nomap(region))) {
                        res->name = "Reserved";
@@ -205,6 +229,9 @@ static void __init init_resources(void)
                        goto error;
        }
 
+       /* Clean-up any unused pre-allocated resources */
+       mem_res_sz = (num_resources - res_idx + 1) * sizeof(*mem_res);
+       memblock_free((phys_addr_t) mem_res, mem_res_sz);
        return;
 
  error:
@@ -249,23 +276,25 @@ void __init setup_arch(char **cmdline_p)
        parse_early_param();
 
        efi_init();
-       setup_bootmem();
        paging_init();
-       init_resources();
 #if IS_ENABLED(CONFIG_BUILTIN_DTB)
        unflatten_and_copy_device_tree();
 #else
-       if (early_init_dt_verify(__va(dtb_early_pa)))
+       if (early_init_dt_verify(__va(XIP_FIXUP(dtb_early_pa))))
                unflatten_device_tree();
        else
                pr_err("No DTB found in kernel mappings\n");
 #endif
        misc_mem_init();
 
+       init_resources();
        sbi_init();
 
-       if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
+       if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
                protect_kernel_text_data();
+               protect_kernel_linear_mapping_text_rodata();
+       }
+
 #ifdef CONFIG_SWIOTLB
        swiotlb_init(1);
 #endif