Skip to content

Commit d707174

Browse files
atishp04palmer-dabbelt
authored andcommitted
RISC-V: Add EFI stub support.
Add a RISC-V architecture specific stub code that actually copies the actual kernel image to a valid address and jump to it after boot services are terminated. Enable UEFI related kernel configs as well for RISC-V. Signed-off-by: Atish Patra <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ardb: - move hartid fetch into check_platform_features() - use image_size not reserve_size - select ISA_C - do not use dram_base] Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent cb7d2dd commit d707174

File tree

7 files changed

+180
-1
lines changed

7 files changed

+180
-1
lines changed

arch/riscv/Kconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,26 @@ config CMDLINE_FORCE
401401

402402
endchoice
403403

404+
config EFI_STUB
405+
bool
406+
407+
config EFI
408+
bool "UEFI runtime support"
409+
depends on OF
410+
select LIBFDT
411+
select UCS2_STRING
412+
select EFI_PARAMS_FROM_FDT
413+
select EFI_STUB
414+
select EFI_GENERIC_STUB
415+
select RISCV_ISA_C
416+
default y
417+
help
418+
This option provides support for runtime services provided
419+
by UEFI firmware (such as non-volatile variables, realtime
420+
clock, and platform reset). A UEFI stub is also provided to
421+
allow the kernel to be booted as an EFI application. This
422+
is only useful on systems that have UEFI firmware.
423+
404424
endmenu
405425

406426
config BUILTIN_DTB
@@ -413,3 +433,5 @@ menu "Power management options"
413433
source "kernel/power/Kconfig"
414434

415435
endmenu
436+
437+
source "drivers/firmware/Kconfig"

arch/riscv/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ head-y := arch/riscv/kernel/head.o
8080
core-y += arch/riscv/
8181

8282
libs-y += arch/riscv/lib/
83+
libs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
8384

8485
PHONY += vdso_install
8586
vdso_install:

arch/riscv/configs/defconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,4 @@ CONFIG_DEBUG_BLOCK_EXT_DEVT=y
130130
# CONFIG_RUNTIME_TESTING_MENU is not set
131131
CONFIG_MEMTEST=y
132132
# CONFIG_SYSFS_SYSCALL is not set
133+
CONFIG_EFI=y

arch/riscv/include/asm/efi.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2020 Western Digital Corporation or its affiliates.
4+
*/
5+
#ifndef _ASM_EFI_H
6+
#define _ASM_EFI_H
7+
8+
#include <asm/io.h>
9+
#include <asm/mmu_context.h>
10+
#include <asm/ptrace.h>
11+
#include <asm/tlbflush.h>
12+
13+
/* on RISC-V, the FDT may be located anywhere in system RAM */
14+
static inline unsigned long efi_get_max_fdt_addr(unsigned long image_addr)
15+
{
16+
return ULONG_MAX;
17+
}
18+
19+
/* Load initrd at enough distance from DRAM start */
20+
static inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr)
21+
{
22+
return image_addr + SZ_256M;
23+
}
24+
25+
#define alloc_screen_info(x...) (&screen_info)
26+
27+
static inline void free_screen_info(struct screen_info *si)
28+
{
29+
}
30+
31+
static inline void efifb_setup_from_dmi(struct screen_info *si, const char *opt)
32+
{
33+
}
34+
35+
#endif /* _ASM_EFI_H */

drivers/firmware/efi/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ config EFI_GENERIC_STUB
111111

112112
config EFI_ARMSTUB_DTB_LOADER
113113
bool "Enable the DTB loader"
114-
depends on EFI_GENERIC_STUB
114+
depends on EFI_GENERIC_STUB && !RISCV
115115
default y
116116
help
117117
Select this config option to add support for the dtb= command
@@ -128,6 +128,7 @@ config EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER
128128
bool "Enable the command line initrd loader" if !X86
129129
depends on EFI_STUB && (EFI_GENERIC_STUB || X86)
130130
default y
131+
depends on !RISCV
131132
help
132133
Select this config option to add support for the initrd= command
133134
line parameter, allowing an initrd that resides on the same volume

drivers/firmware/efi/libstub/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ cflags-$(CONFIG_ARM64) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
2222
cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
2323
-fno-builtin -fpic \
2424
$(call cc-option,-mno-single-pic-base)
25+
cflags-$(CONFIG_RISCV) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
26+
-fpic
2527

2628
cflags-$(CONFIG_EFI_GENERIC_STUB) += -I$(srctree)/scripts/dtc/libfdt
2729

@@ -63,6 +65,7 @@ lib-$(CONFIG_EFI_GENERIC_STUB) += efi-stub.o fdt.o string.o \
6365
lib-$(CONFIG_ARM) += arm32-stub.o
6466
lib-$(CONFIG_ARM64) += arm64-stub.o
6567
lib-$(CONFIG_X86) += x86-stub.o
68+
lib-$(CONFIG_RISCV) += riscv-stub.o
6669
CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET)
6770
CFLAGS_arm64-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET)
6871

@@ -106,6 +109,13 @@ STUBCOPY_FLAGS-$(CONFIG_ARM64) += --prefix-alloc-sections=.init \
106109
--prefix-symbols=__efistub_
107110
STUBCOPY_RELOC-$(CONFIG_ARM64) := R_AARCH64_ABS
108111

112+
# For RISC-V, we don't need anything special other than arm64. Keep all the
113+
# symbols in .init section and make sure that no absolute symbols references
114+
# doesn't exist.
115+
STUBCOPY_FLAGS-$(CONFIG_RISCV) += --prefix-alloc-sections=.init \
116+
--prefix-symbols=__efistub_
117+
STUBCOPY_RELOC-$(CONFIG_RISCV) := R_RISCV_HI20
118+
109119
$(obj)/%.stub.o: $(obj)/%.o FORCE
110120
$(call if_changed,stubcopy)
111121

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2020 Western Digital Corporation or its affiliates.
4+
*/
5+
6+
#include <linux/efi.h>
7+
#include <linux/libfdt.h>
8+
9+
#include <asm/efi.h>
10+
#include <asm/sections.h>
11+
12+
#include "efistub.h"
13+
14+
/*
15+
* RISC-V requires the kernel image to placed 2 MB aligned base for 64 bit and
16+
* 4MB for 32 bit.
17+
*/
18+
#ifdef CONFIG_64BIT
19+
#define MIN_KIMG_ALIGN SZ_2M
20+
#else
21+
#define MIN_KIMG_ALIGN SZ_4M
22+
#endif
23+
24+
typedef void __noreturn (*jump_kernel_func)(unsigned int, unsigned long);
25+
26+
static u32 hartid;
27+
28+
static u32 get_boot_hartid_from_fdt(void)
29+
{
30+
const void *fdt;
31+
int chosen_node, len;
32+
const fdt32_t *prop;
33+
34+
fdt = get_efi_config_table(DEVICE_TREE_GUID);
35+
if (!fdt)
36+
return U32_MAX;
37+
38+
chosen_node = fdt_path_offset(fdt, "/chosen");
39+
if (chosen_node < 0)
40+
return U32_MAX;
41+
42+
prop = fdt_getprop((void *)fdt, chosen_node, "boot-hartid", &len);
43+
if (!prop || len != sizeof(u32))
44+
return U32_MAX;
45+
46+
return fdt32_to_cpu(*prop);
47+
}
48+
49+
efi_status_t check_platform_features(void)
50+
{
51+
hartid = get_boot_hartid_from_fdt();
52+
if (hartid == U32_MAX) {
53+
efi_err("/chosen/boot-hartid missing or invalid!\n");
54+
return EFI_UNSUPPORTED;
55+
}
56+
return EFI_SUCCESS;
57+
}
58+
59+
void __noreturn efi_enter_kernel(unsigned long entrypoint, unsigned long fdt,
60+
unsigned long fdt_size)
61+
{
62+
unsigned long stext_offset = _start_kernel - _start;
63+
unsigned long kernel_entry = entrypoint + stext_offset;
64+
jump_kernel_func jump_kernel = (jump_kernel_func)kernel_entry;
65+
66+
/*
67+
* Jump to real kernel here with following constraints.
68+
* 1. MMU should be disabled.
69+
* 2. a0 should contain hartid
70+
* 3. a1 should DT address
71+
*/
72+
csr_write(CSR_SATP, 0);
73+
jump_kernel(hartid, fdt);
74+
}
75+
76+
efi_status_t handle_kernel_image(unsigned long *image_addr,
77+
unsigned long *image_size,
78+
unsigned long *reserve_addr,
79+
unsigned long *reserve_size,
80+
efi_loaded_image_t *image)
81+
{
82+
unsigned long kernel_size = 0;
83+
unsigned long preferred_addr;
84+
efi_status_t status;
85+
86+
kernel_size = _edata - _start;
87+
*image_addr = (unsigned long)_start;
88+
*image_size = kernel_size + (_end - _edata);
89+
90+
/*
91+
* RISC-V kernel maps PAGE_OFFSET virtual address to the same physical
92+
* address where kernel is booted. That's why kernel should boot from
93+
* as low as possible to avoid wastage of memory. Currently, dram_base
94+
* is occupied by the firmware. So the preferred address for kernel to
95+
* boot is next aligned address. If preferred address is not available,
96+
* relocate_kernel will fall back to efi_low_alloc_above to allocate
97+
* lowest possible memory region as long as the address and size meets
98+
* the alignment constraints.
99+
*/
100+
preferred_addr = MIN_KIMG_ALIGN;
101+
status = efi_relocate_kernel(image_addr, kernel_size, *image_size,
102+
preferred_addr, MIN_KIMG_ALIGN, 0x0);
103+
104+
if (status != EFI_SUCCESS) {
105+
efi_err("Failed to relocate kernel\n");
106+
*image_size = 0;
107+
}
108+
return status;
109+
}

0 commit comments

Comments
 (0)