GNU Linux-libre 5.10.153-gnu1
[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 int, unsigned long);
25
26 static u32 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 efi_status_t check_platform_features(void)
51 {
52         int ret;
53
54         ret = get_boot_hartid_from_fdt();
55         if (ret) {
56                 efi_err("/chosen/boot-hartid missing or invalid!\n");
57                 return EFI_UNSUPPORTED;
58         }
59         return EFI_SUCCESS;
60 }
61
62 void __noreturn efi_enter_kernel(unsigned long entrypoint, unsigned long fdt,
63                                  unsigned long fdt_size)
64 {
65         unsigned long stext_offset = _start_kernel - _start;
66         unsigned long kernel_entry = entrypoint + stext_offset;
67         jump_kernel_func jump_kernel = (jump_kernel_func)kernel_entry;
68
69         /*
70          * Jump to real kernel here with following constraints.
71          * 1. MMU should be disabled.
72          * 2. a0 should contain hartid
73          * 3. a1 should DT address
74          */
75         csr_write(CSR_SATP, 0);
76         jump_kernel(hartid, fdt);
77 }
78
79 efi_status_t handle_kernel_image(unsigned long *image_addr,
80                                  unsigned long *image_size,
81                                  unsigned long *reserve_addr,
82                                  unsigned long *reserve_size,
83                                  efi_loaded_image_t *image)
84 {
85         unsigned long kernel_size = 0;
86         unsigned long preferred_addr;
87         efi_status_t status;
88
89         kernel_size = _edata - _start;
90         *image_addr = (unsigned long)_start;
91         *image_size = kernel_size + (_end - _edata);
92
93         /*
94          * RISC-V kernel maps PAGE_OFFSET virtual address to the same physical
95          * address where kernel is booted. That's why kernel should boot from
96          * as low as possible to avoid wastage of memory. Currently, dram_base
97          * is occupied by the firmware. So the preferred address for kernel to
98          * boot is next aligned address. If preferred address is not available,
99          * relocate_kernel will fall back to efi_low_alloc_above to allocate
100          * lowest possible memory region as long as the address and size meets
101          * the alignment constraints.
102          */
103         preferred_addr = MIN_KIMG_ALIGN;
104         status = efi_relocate_kernel(image_addr, kernel_size, *image_size,
105                                      preferred_addr, MIN_KIMG_ALIGN, 0x0);
106
107         if (status != EFI_SUCCESS) {
108                 efi_err("Failed to relocate kernel\n");
109                 *image_size = 0;
110         }
111         return status;
112 }