Skip to content

Commit c6fe811

Browse files
mickflemmpalmer-dabbelt
authored andcommitted
RISC-V: relocate DTB if it's outside memory region
In case the DTB provided by the bootloader/BootROM is before the kernel image or outside /memory, we won't be able to access it through the linear mapping, and get a segfault on setup_arch(). Currently OpenSBI relocates DTB but that's not always the case (e.g. if FW_JUMP_FDT_ADDR is not specified), and it's also not the most portable approach since the default FW_JUMP_FDT_ADDR of the generic platform relocates the DTB at a specific offset that may not be available. To avoid this situation copy DTB so that it's visible through the linear mapping. Signed-off-by: Nick Kossifidis <mick@ics.forth.gr> Link: https://lore.kernel.org/r/20220322132839.3653682-1-mick@ics.forth.gr Tested-by: Conor Dooley <conor.dooley@microchip.com> Fixes: f105aa9 ("riscv: add BUILTIN_DTB support for MMU-enabled targets") Cc: stable@vger.kernel.org Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent ac0280a commit c6fe811

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

arch/riscv/mm/init.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,25 @@ static void __init setup_bootmem(void)
208208
* early_init_fdt_reserve_self() since __pa() does
209209
* not work for DTB pointers that are fixmap addresses
210210
*/
211-
if (!IS_ENABLED(CONFIG_BUILTIN_DTB))
212-
memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
211+
if (!IS_ENABLED(CONFIG_BUILTIN_DTB)) {
212+
/*
213+
* In case the DTB is not located in a memory region we won't
214+
* be able to locate it later on via the linear mapping and
215+
* get a segfault when accessing it via __va(dtb_early_pa).
216+
* To avoid this situation copy DTB to a memory region.
217+
* Note that memblock_phys_alloc will also reserve DTB region.
218+
*/
219+
if (!memblock_is_memory(dtb_early_pa)) {
220+
size_t fdt_size = fdt_totalsize(dtb_early_va);
221+
phys_addr_t new_dtb_early_pa = memblock_phys_alloc(fdt_size, PAGE_SIZE);
222+
void *new_dtb_early_va = early_memremap(new_dtb_early_pa, fdt_size);
223+
224+
memcpy(new_dtb_early_va, dtb_early_va, fdt_size);
225+
early_memunmap(new_dtb_early_va, fdt_size);
226+
_dtb_early_pa = new_dtb_early_pa;
227+
} else
228+
memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
229+
}
213230

214231
early_init_fdt_scan_reserved_mem();
215232
dma_contiguous_reserve(dma32_phys_limit);

0 commit comments

Comments
 (0)