Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2020 Western Digital Corporation or its affiliates.
0004  */
0005 
0006 #include <linux/efi.h>
0007 #include <linux/libfdt.h>
0008 
0009 #include <asm/efi.h>
0010 #include <asm/sections.h>
0011 #include <asm/unaligned.h>
0012 
0013 #include "efistub.h"
0014 
0015 /*
0016  * RISC-V requires the kernel image to placed 2 MB aligned base for 64 bit and
0017  * 4MB for 32 bit.
0018  */
0019 #ifdef CONFIG_64BIT
0020 #define MIN_KIMG_ALIGN      SZ_2M
0021 #else
0022 #define MIN_KIMG_ALIGN      SZ_4M
0023 #endif
0024 
0025 typedef void __noreturn (*jump_kernel_func)(unsigned long, unsigned long);
0026 
0027 static unsigned long hartid;
0028 
0029 static int get_boot_hartid_from_fdt(void)
0030 {
0031     const void *fdt;
0032     int chosen_node, len;
0033     const void *prop;
0034 
0035     fdt = get_efi_config_table(DEVICE_TREE_GUID);
0036     if (!fdt)
0037         return -EINVAL;
0038 
0039     chosen_node = fdt_path_offset(fdt, "/chosen");
0040     if (chosen_node < 0)
0041         return -EINVAL;
0042 
0043     prop = fdt_getprop((void *)fdt, chosen_node, "boot-hartid", &len);
0044     if (!prop)
0045         return -EINVAL;
0046 
0047     if (len == sizeof(u32))
0048         hartid = (unsigned long) fdt32_to_cpu(*(fdt32_t *)prop);
0049     else if (len == sizeof(u64))
0050         hartid = (unsigned long) fdt64_to_cpu(__get_unaligned_t(fdt64_t, prop));
0051     else
0052         return -EINVAL;
0053 
0054     return 0;
0055 }
0056 
0057 static efi_status_t get_boot_hartid_from_efi(void)
0058 {
0059     efi_guid_t boot_protocol_guid = RISCV_EFI_BOOT_PROTOCOL_GUID;
0060     struct riscv_efi_boot_protocol *boot_protocol;
0061     efi_status_t status;
0062 
0063     status = efi_bs_call(locate_protocol, &boot_protocol_guid, NULL,
0064                  (void **)&boot_protocol);
0065     if (status != EFI_SUCCESS)
0066         return status;
0067     return efi_call_proto(boot_protocol, get_boot_hartid, &hartid);
0068 }
0069 
0070 efi_status_t check_platform_features(void)
0071 {
0072     efi_status_t status;
0073     int ret;
0074 
0075     status = get_boot_hartid_from_efi();
0076     if (status != EFI_SUCCESS) {
0077         ret = get_boot_hartid_from_fdt();
0078         if (ret) {
0079             efi_err("Failed to get boot hartid!\n");
0080             return EFI_UNSUPPORTED;
0081         }
0082     }
0083     return EFI_SUCCESS;
0084 }
0085 
0086 void __noreturn efi_enter_kernel(unsigned long entrypoint, unsigned long fdt,
0087                  unsigned long fdt_size)
0088 {
0089     unsigned long stext_offset = _start_kernel - _start;
0090     unsigned long kernel_entry = entrypoint + stext_offset;
0091     jump_kernel_func jump_kernel = (jump_kernel_func)kernel_entry;
0092 
0093     /*
0094      * Jump to real kernel here with following constraints.
0095      * 1. MMU should be disabled.
0096      * 2. a0 should contain hartid
0097      * 3. a1 should DT address
0098      */
0099     csr_write(CSR_SATP, 0);
0100     jump_kernel(hartid, fdt);
0101 }
0102 
0103 efi_status_t handle_kernel_image(unsigned long *image_addr,
0104                  unsigned long *image_size,
0105                  unsigned long *reserve_addr,
0106                  unsigned long *reserve_size,
0107                  efi_loaded_image_t *image,
0108                  efi_handle_t image_handle)
0109 {
0110     unsigned long kernel_size = 0;
0111     unsigned long preferred_addr;
0112     efi_status_t status;
0113 
0114     kernel_size = _edata - _start;
0115     *image_addr = (unsigned long)_start;
0116     *image_size = kernel_size + (_end - _edata);
0117 
0118     /*
0119      * RISC-V kernel maps PAGE_OFFSET virtual address to the same physical
0120      * address where kernel is booted. That's why kernel should boot from
0121      * as low as possible to avoid wastage of memory. Currently, dram_base
0122      * is occupied by the firmware. So the preferred address for kernel to
0123      * boot is next aligned address. If preferred address is not available,
0124      * relocate_kernel will fall back to efi_low_alloc_above to allocate
0125      * lowest possible memory region as long as the address and size meets
0126      * the alignment constraints.
0127      */
0128     preferred_addr = MIN_KIMG_ALIGN;
0129     status = efi_relocate_kernel(image_addr, kernel_size, *image_size,
0130                      preferred_addr, MIN_KIMG_ALIGN, 0x0);
0131 
0132     if (status != EFI_SUCCESS) {
0133         efi_err("Failed to relocate kernel\n");
0134         *image_size = 0;
0135     }
0136     return status;
0137 }