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
get_boot_hartid_from_fdt(void)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
check_platform_features(void)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
efi_enter_kernel(unsigned long entrypoint,unsigned long fdt,unsigned long fdt_size)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
handle_kernel_image(unsigned long * image_addr,unsigned long * image_size,unsigned long * reserve_addr,unsigned long * reserve_size,efi_loaded_image_t * image)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 }
110