1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Western Digital Corporation or its affiliates.
7 #include <linux/libfdt.h>
10 #include <asm/sections.h>
15 * RISC-V requires the kernel image to placed 2 MB aligned base for 64 bit and
19 #define MIN_KIMG_ALIGN SZ_2M
21 #define MIN_KIMG_ALIGN SZ_4M
24 typedef void __noreturn (*jump_kernel_func)(unsigned long, unsigned long);
26 static unsigned long hartid;
28 static int get_boot_hartid_from_fdt(void)
34 fdt = get_efi_config_table(DEVICE_TREE_GUID);
38 chosen_node = fdt_path_offset(fdt, "/chosen");
42 prop = fdt_getprop((void *)fdt, chosen_node, "boot-hartid", &len);
43 if (!prop || len != sizeof(u32))
46 hartid = fdt32_to_cpu(*prop);
50 static efi_status_t get_boot_hartid_from_efi(void)
52 efi_guid_t boot_protocol_guid = RISCV_EFI_BOOT_PROTOCOL_GUID;
53 struct riscv_efi_boot_protocol *boot_protocol;
56 status = efi_bs_call(locate_protocol, &boot_protocol_guid, NULL,
57 (void **)&boot_protocol);
58 if (status != EFI_SUCCESS)
60 return efi_call_proto(boot_protocol, get_boot_hartid, &hartid);
63 efi_status_t check_platform_features(void)
68 status = get_boot_hartid_from_efi();
69 if (status != EFI_SUCCESS) {
70 ret = get_boot_hartid_from_fdt();
72 efi_err("Failed to get boot hartid!\n");
73 return EFI_UNSUPPORTED;
79 void __noreturn efi_enter_kernel(unsigned long entrypoint, unsigned long fdt,
80 unsigned long fdt_size)
82 unsigned long stext_offset = _start_kernel - _start;
83 unsigned long kernel_entry = entrypoint + stext_offset;
84 jump_kernel_func jump_kernel = (jump_kernel_func)kernel_entry;
87 * Jump to real kernel here with following constraints.
88 * 1. MMU should be disabled.
89 * 2. a0 should contain hartid
90 * 3. a1 should DT address
92 csr_write(CSR_SATP, 0);
93 jump_kernel(hartid, fdt);
96 efi_status_t handle_kernel_image(unsigned long *image_addr,
97 unsigned long *image_size,
98 unsigned long *reserve_addr,
99 unsigned long *reserve_size,
100 efi_loaded_image_t *image,
101 efi_handle_t image_handle)
103 unsigned long kernel_size = 0;
104 unsigned long preferred_addr;
107 kernel_size = _edata - _start;
108 *image_addr = (unsigned long)_start;
109 *image_size = kernel_size + (_end - _edata);
112 * RISC-V kernel maps PAGE_OFFSET virtual address to the same physical
113 * address where kernel is booted. That's why kernel should boot from
114 * as low as possible to avoid wastage of memory. Currently, dram_base
115 * is occupied by the firmware. So the preferred address for kernel to
116 * boot is next aligned address. If preferred address is not available,
117 * relocate_kernel will fall back to efi_low_alloc_above to allocate
118 * lowest possible memory region as long as the address and size meets
119 * the alignment constraints.
121 preferred_addr = MIN_KIMG_ALIGN;
122 status = efi_relocate_kernel(image_addr, kernel_size, *image_size,
123 preferred_addr, MIN_KIMG_ALIGN, 0x0);
125 if (status != EFI_SUCCESS) {
126 efi_err("Failed to relocate kernel\n");