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 #include <asm/unaligned.h> 12 13 #include "efistub.h" 14 15 /* 16 * RISC-V requires the kernel image to placed 2 MB aligned base for 64 bit and 17 * 4MB for 32 bit. 18 */ 19 #ifdef CONFIG_64BIT 20 #define MIN_KIMG_ALIGN SZ_2M 21 #else 22 #define MIN_KIMG_ALIGN SZ_4M 23 #endif 24 25 typedef void __noreturn (*jump_kernel_func)(unsigned long, unsigned long); 26 27 static unsigned long hartid; 28 29 static int get_boot_hartid_from_fdt(void) 30 { 31 const void *fdt; 32 int chosen_node, len; 33 const void *prop; 34 35 fdt = get_efi_config_table(DEVICE_TREE_GUID); 36 if (!fdt) 37 return -EINVAL; 38 39 chosen_node = fdt_path_offset(fdt, "/chosen"); 40 if (chosen_node < 0) 41 return -EINVAL; 42 43 prop = fdt_getprop((void *)fdt, chosen_node, "boot-hartid", &len); 44 if (!prop) 45 return -EINVAL; 46 47 if (len == sizeof(u32)) 48 hartid = (unsigned long) fdt32_to_cpu(*(fdt32_t *)prop); 49 else if (len == sizeof(u64)) 50 hartid = (unsigned long) fdt64_to_cpu(__get_unaligned_t(fdt64_t, prop)); 51 else 52 return -EINVAL; 53 54 return 0; 55 } 56 57 static efi_status_t get_boot_hartid_from_efi(void) 58 { 59 efi_guid_t boot_protocol_guid = RISCV_EFI_BOOT_PROTOCOL_GUID; 60 struct riscv_efi_boot_protocol *boot_protocol; 61 efi_status_t status; 62 63 status = efi_bs_call(locate_protocol, &boot_protocol_guid, NULL, 64 (void **)&boot_protocol); 65 if (status != EFI_SUCCESS) 66 return status; 67 return efi_call_proto(boot_protocol, get_boot_hartid, &hartid); 68 } 69 70 efi_status_t check_platform_features(void) 71 { 72 efi_status_t status; 73 int ret; 74 75 status = get_boot_hartid_from_efi(); 76 if (status != EFI_SUCCESS) { 77 ret = get_boot_hartid_from_fdt(); 78 if (ret) { 79 efi_err("Failed to get boot hartid!\n"); 80 return EFI_UNSUPPORTED; 81 } 82 } 83 return EFI_SUCCESS; 84 } 85 86 void __noreturn efi_enter_kernel(unsigned long entrypoint, unsigned long fdt, 87 unsigned long fdt_size) 88 { 89 unsigned long stext_offset = _start_kernel - _start; 90 unsigned long kernel_entry = entrypoint + stext_offset; 91 jump_kernel_func jump_kernel = (jump_kernel_func)kernel_entry; 92 93 /* 94 * Jump to real kernel here with following constraints. 95 * 1. MMU should be disabled. 96 * 2. a0 should contain hartid 97 * 3. a1 should DT address 98 */ 99 csr_write(CSR_SATP, 0); 100 jump_kernel(hartid, fdt); 101 } 102 103 efi_status_t handle_kernel_image(unsigned long *image_addr, 104 unsigned long *image_size, 105 unsigned long *reserve_addr, 106 unsigned long *reserve_size, 107 efi_loaded_image_t *image, 108 efi_handle_t image_handle) 109 { 110 unsigned long kernel_size = 0; 111 unsigned long preferred_addr; 112 efi_status_t status; 113 114 kernel_size = _edata - _start; 115 *image_addr = (unsigned long)_start; 116 *image_size = kernel_size + (_end - _edata); 117 118 /* 119 * RISC-V kernel maps PAGE_OFFSET virtual address to the same physical 120 * address where kernel is booted. That's why kernel should boot from 121 * as low as possible to avoid wastage of memory. Currently, dram_base 122 * is occupied by the firmware. So the preferred address for kernel to 123 * boot is next aligned address. If preferred address is not available, 124 * relocate_kernel will fall back to efi_low_alloc_above to allocate 125 * lowest possible memory region as long as the address and size meets 126 * the alignment constraints. 127 */ 128 preferred_addr = MIN_KIMG_ALIGN; 129 status = efi_relocate_kernel(image_addr, kernel_size, *image_size, 130 preferred_addr, MIN_KIMG_ALIGN, 0x0); 131 132 if (status != EFI_SUCCESS) { 133 efi_err("Failed to relocate kernel\n"); 134 *image_size = 0; 135 } 136 return status; 137 } 138