GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / firmware / efi / libstub / riscv-stub.c
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 long, unsigned long);
25
26 static unsigned long hartid;
27
28 static int 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 -EINVAL;
37
38         chosen_node = fdt_path_offset(fdt, "/chosen");
39         if (chosen_node < 0)
40                 return -EINVAL;
41
42         prop = fdt_getprop((void *)fdt, chosen_node, "boot-hartid", &len);
43         if (!prop || len != sizeof(u32))
44                 return -EINVAL;
45
46         hartid = fdt32_to_cpu(*prop);
47         return 0;
48 }
49
50 static efi_status_t get_boot_hartid_from_efi(void)
51 {
52         efi_guid_t boot_protocol_guid = RISCV_EFI_BOOT_PROTOCOL_GUID;
53         struct riscv_efi_boot_protocol *boot_protocol;
54         efi_status_t status;
55
56         status = efi_bs_call(locate_protocol, &boot_protocol_guid, NULL,
57                              (void **)&boot_protocol);
58         if (status != EFI_SUCCESS)
59                 return status;
60         return efi_call_proto(boot_protocol, get_boot_hartid, &hartid);
61 }
62
63 efi_status_t check_platform_features(void)
64 {
65         efi_status_t status;
66         int ret;
67
68         status = get_boot_hartid_from_efi();
69         if (status != EFI_SUCCESS) {
70                 ret = get_boot_hartid_from_fdt();
71                 if (ret) {
72                         efi_err("Failed to get boot hartid!\n");
73                         return EFI_UNSUPPORTED;
74                 }
75         }
76         return EFI_SUCCESS;
77 }
78
79 void __noreturn efi_enter_kernel(unsigned long entrypoint, unsigned long fdt,
80                                  unsigned long fdt_size)
81 {
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;
85
86         /*
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
91          */
92         csr_write(CSR_SATP, 0);
93         jump_kernel(hartid, fdt);
94 }
95
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)
102 {
103         unsigned long kernel_size = 0;
104         unsigned long preferred_addr;
105         efi_status_t status;
106
107         kernel_size = _edata - _start;
108         *image_addr = (unsigned long)_start;
109         *image_size = kernel_size + (_end - _edata);
110
111         /*
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.
120          */
121         preferred_addr = MIN_KIMG_ALIGN;
122         status = efi_relocate_kernel(image_addr, kernel_size, *image_size,
123                                      preferred_addr, MIN_KIMG_ALIGN, 0x0);
124
125         if (status != EFI_SUCCESS) {
126                 efi_err("Failed to relocate kernel\n");
127                 *image_size = 0;
128         }
129         return status;
130 }