#define PTE_FMT "%08lx"
 #endif
 
+#ifdef CONFIG_64BIT
+/*
+ * We override this value as its generic definition uses __pa too early in
+ * the boot process (before kernel_map.va_pa_offset is set).
+ */
+#define MIN_MEMBLOCK_ADDR      0
+#endif
+
 #ifdef CONFIG_MMU
 #define ARCH_PFN_OFFSET                (PFN_DOWN((unsigned long)phys_ram_base))
 #else
 #define is_linear_mapping(x)   \
        ((x) >= PAGE_OFFSET && (!IS_ENABLED(CONFIG_64BIT) || (x) < PAGE_OFFSET + KERN_VIRT_SIZE))
 
+#ifndef CONFIG_DEBUG_VIRTUAL
 #define linear_mapping_pa_to_va(x)     ((void *)((unsigned long)(x) + kernel_map.va_pa_offset))
+#else
+void *linear_mapping_pa_to_va(unsigned long x);
+#endif
 #define kernel_mapping_pa_to_va(y)     ({                                      \
        unsigned long _y = (unsigned long)(y);                                  \
        (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < phys_ram_base) ?                 \
        })
 #define __pa_to_va_nodebug(x)          linear_mapping_pa_to_va(x)
 
+#ifndef CONFIG_DEBUG_VIRTUAL
 #define linear_mapping_va_to_pa(x)     ((unsigned long)(x) - kernel_map.va_pa_offset)
+#else
+phys_addr_t linear_mapping_va_to_pa(unsigned long x);
+#endif
 #define kernel_mapping_va_to_pa(y) ({                                          \
        unsigned long _y = (unsigned long)(y);                                  \
        (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < kernel_map.virt_addr + XIP_OFFSET) ? \
 
        phys_ram_end = memblock_end_of_DRAM();
        if (!IS_ENABLED(CONFIG_XIP_KERNEL))
                phys_ram_base = memblock_start_of_DRAM();
+
+       /*
+        * In 64-bit, any use of __va/__pa before this point is wrong as we
+        * did not know the start of DRAM before.
+        */
+       if (IS_ENABLED(CONFIG_64BIT))
+               kernel_map.va_pa_offset = PAGE_OFFSET - phys_ram_base;
+
        /*
         * memblock allocator is not aware of the fact that last 4K bytes of
         * the addressable memory can not be mapped because of IS_ERR_VALUE
 
 static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
 {
-       /* Upgrade to PMD_SIZE mappings whenever possible */
-       base &= PMD_SIZE - 1;
-       if (!base && size >= PMD_SIZE)
+       if (!(base & (PGDIR_SIZE - 1)) && size >= PGDIR_SIZE)
+               return PGDIR_SIZE;
+
+       if (!(base & (P4D_SIZE - 1)) && size >= P4D_SIZE)
+               return P4D_SIZE;
+
+       if (!(base & (PUD_SIZE - 1)) && size >= PUD_SIZE)
+               return PUD_SIZE;
+
+       if (!(base & (PMD_SIZE - 1)) && size >= PMD_SIZE)
                return PMD_SIZE;
 
        return PAGE_SIZE;
        set_satp_mode();
 #endif
 
-       kernel_map.va_pa_offset = PAGE_OFFSET - kernel_map.phys_addr;
+       /*
+        * In 64-bit, we defer the setup of va_pa_offset to setup_bootmem,
+        * where we have the system memory layout: this allows us to align
+        * the physical and virtual mappings and then make use of PUD/P4D/PGD
+        * for the linear mapping. This is only possible because the kernel
+        * mapping lies outside the linear mapping.
+        * In 32-bit however, as the kernel resides in the linear mapping,
+        * setup_vm_final can not change the mapping established here,
+        * otherwise the same kernel addresses would get mapped to different
+        * physical addresses (if the start of dram is different from the
+        * kernel physical address start).
+        */
+       kernel_map.va_pa_offset = IS_ENABLED(CONFIG_64BIT) ?
+                               0UL : PAGE_OFFSET - kernel_map.phys_addr;
        kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr;
 
-       phys_ram_base = kernel_map.phys_addr;
-
        /*
         * The default maximal physical memory size is KERN_VIRT_SIZE for 32-bit
         * kernel, whereas for 64-bit kernel, the end of the virtual address
        phys_addr_t start, end;
        u64 i;
 
+#ifdef CONFIG_STRICT_KERNEL_RWX
+       phys_addr_t ktext_start = __pa_symbol(_start);
+       phys_addr_t ktext_size = __init_data_begin - _start;
+       phys_addr_t krodata_start = __pa_symbol(__start_rodata);
+       phys_addr_t krodata_size = _data - __start_rodata;
+
+       /* Isolate kernel text and rodata so they don't get mapped with a PUD */
+       memblock_mark_nomap(ktext_start,  ktext_size);
+       memblock_mark_nomap(krodata_start, krodata_size);
+#endif
+
        /* Map all memory banks in the linear mapping */
        for_each_mem_range(i, &start, &end) {
                if (start >= end)
 
                create_linear_mapping_range(start, end);
        }
+
+#ifdef CONFIG_STRICT_KERNEL_RWX
+       create_linear_mapping_range(ktext_start, ktext_start + ktext_size);
+       create_linear_mapping_range(krodata_start,
+                                   krodata_start + krodata_size);
+
+       memblock_clear_nomap(ktext_start,  ktext_size);
+       memblock_clear_nomap(krodata_start, krodata_size);
+#endif
 }
 
 static void __init setup_vm_final(void)
 
 static void __early_init_dt_declare_initrd(unsigned long start,
                                           unsigned long end)
 {
-       /* ARM64 would cause a BUG to occur here when CONFIG_DEBUG_VM is
-        * enabled since __va() is called too early. ARM64 does make use
-        * of phys_initrd_start/phys_initrd_size so we can skip this
-        * conversion.
+       /*
+        * __va() is not yet available this early on some platforms. In that
+        * case, the platform uses phys_initrd_start/phys_initrd_size instead
+        * and does the VA conversion itself.
         */
-       if (!IS_ENABLED(CONFIG_ARM64)) {
+       if (!IS_ENABLED(CONFIG_ARM64) &&
+           !(IS_ENABLED(CONFIG_RISCV) && IS_ENABLED(CONFIG_64BIT))) {
                initrd_start = (unsigned long)__va(start);
                initrd_end = (unsigned long)__va(end);
                initrd_below_start_ok = 1;