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