Skip to content

Commit 6d7f91d

Browse files
AlexGhitipalmer-dabbelt
authored andcommitted
riscv: Get rid of CONFIG_PHYS_RAM_BASE in kernel physical address conversion
The usage of CONFIG_PHYS_RAM_BASE for all kernel types was a mistake: this value is implementation-specific and this breaks the genericity of the RISC-V kernel. Fix this by introducing a new variable phys_ram_base that holds this value at runtime and use it in the kernel physical address conversion macro. Since this value is used only for XIP kernels, evaluate it only if CONFIG_XIP_KERNEL is set which in addition optimizes this macro for standard kernels at compile-time. Signed-off-by: Alexandre Ghiti <[email protected]> Tested-by: Emil Renner Berthing <[email protected]> Reviewed-by: Jisheng Zhang <[email protected]> Fixes: 44c9225 ("RISC-V: enable XIP") Cc: [email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent a18b14d commit 6d7f91d

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

arch/riscv/include/asm/page.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ struct kernel_mapping {
103103
};
104104

105105
extern struct kernel_mapping kernel_map;
106+
extern phys_addr_t phys_ram_base;
106107

107108
#ifdef CONFIG_64BIT
108109
#define is_kernel_mapping(x) \
@@ -113,9 +114,9 @@ extern struct kernel_mapping kernel_map;
113114
#define linear_mapping_pa_to_va(x) ((void *)((unsigned long)(x) + kernel_map.va_pa_offset))
114115
#define kernel_mapping_pa_to_va(y) ({ \
115116
unsigned long _y = y; \
116-
(_y >= CONFIG_PHYS_RAM_BASE) ? \
117-
(void *)((unsigned long)(_y) + kernel_map.va_kernel_pa_offset + XIP_OFFSET) : \
118-
(void *)((unsigned long)(_y) + kernel_map.va_kernel_xip_pa_offset); \
117+
(IS_ENABLED(CONFIG_XIP_KERNEL) && _y < phys_ram_base) ? \
118+
(void *)((unsigned long)(_y) + kernel_map.va_kernel_xip_pa_offset) : \
119+
(void *)((unsigned long)(_y) + kernel_map.va_kernel_pa_offset + XIP_OFFSET); \
119120
})
120121
#define __pa_to_va_nodebug(x) linear_mapping_pa_to_va(x)
121122

arch/riscv/mm/init.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ EXPORT_SYMBOL(kernel_map);
3636
#define kernel_map (*(struct kernel_mapping *)XIP_FIXUP(&kernel_map))
3737
#endif
3838

39+
phys_addr_t phys_ram_base __ro_after_init;
40+
EXPORT_SYMBOL(phys_ram_base);
41+
3942
#ifdef CONFIG_XIP_KERNEL
4043
extern char _xiprom[], _exiprom[];
4144
#endif
@@ -160,7 +163,7 @@ static void __init setup_bootmem(void)
160163
phys_addr_t vmlinux_end = __pa_symbol(&_end);
161164
phys_addr_t vmlinux_start = __pa_symbol(&_start);
162165
phys_addr_t __maybe_unused max_mapped_addr;
163-
phys_addr_t dram_end;
166+
phys_addr_t phys_ram_end;
164167

165168
#ifdef CONFIG_XIP_KERNEL
166169
vmlinux_start = __pa_symbol(&_sdata);
@@ -181,9 +184,12 @@ static void __init setup_bootmem(void)
181184
#endif
182185
memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
183186

184-
dram_end = memblock_end_of_DRAM();
185187

188+
phys_ram_end = memblock_end_of_DRAM();
186189
#ifndef CONFIG_64BIT
190+
#ifndef CONFIG_XIP_KERNEL
191+
phys_ram_base = memblock_start_of_DRAM();
192+
#endif
187193
/*
188194
* memblock allocator is not aware of the fact that last 4K bytes of
189195
* the addressable memory can not be mapped because of IS_ERR_VALUE
@@ -194,12 +200,12 @@ static void __init setup_bootmem(void)
194200
* be done in create_kernel_page_table.
195201
*/
196202
max_mapped_addr = __pa(~(ulong)0);
197-
if (max_mapped_addr == (dram_end - 1))
203+
if (max_mapped_addr == (phys_ram_end - 1))
198204
memblock_set_current_limit(max_mapped_addr - 4096);
199205
#endif
200206

201-
min_low_pfn = PFN_UP(memblock_start_of_DRAM());
202-
max_low_pfn = max_pfn = PFN_DOWN(dram_end);
207+
min_low_pfn = PFN_UP(phys_ram_base);
208+
max_low_pfn = max_pfn = PFN_DOWN(phys_ram_end);
203209

204210
dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn));
205211
set_max_mapnr(max_low_pfn - ARCH_PFN_OFFSET);
@@ -558,6 +564,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
558564
kernel_map.xiprom = (uintptr_t)CONFIG_XIP_PHYS_ADDR;
559565
kernel_map.xiprom_sz = (uintptr_t)(&_exiprom) - (uintptr_t)(&_xiprom);
560566

567+
phys_ram_base = CONFIG_PHYS_RAM_BASE;
561568
kernel_map.phys_addr = (uintptr_t)CONFIG_PHYS_RAM_BASE;
562569
kernel_map.size = (uintptr_t)(&_end) - (uintptr_t)(&_sdata);
563570

0 commit comments

Comments
 (0)