blob: 5a26d7c31a43b24baf27cb7701973851bb61d77b [file] [log] [blame]
Lukas Auer515b9342019-08-21 21:14:44 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Fraunhofer AISEC,
4 * Lukas Auer <lukas.auer@aisec.fraunhofer.de>
5 *
6 * Based on common/spl/spl_atf.c
7 */
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Lukas Auer515b9342019-08-21 21:14:44 +02009#include <errno.h>
Simon Glassf11478f2019-12-28 10:45:07 -070010#include <hang.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060011#include <image.h>
Lukas Auer515b9342019-08-21 21:14:44 +020012#include <spl.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Lukas Auer515b9342019-08-21 21:14:44 +020014#include <asm/smp.h>
15#include <opensbi.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060016#include <linux/libfdt.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060017#include <linux/printk.h>
Randolphb1933972023-12-29 16:32:22 +080018#include <mapmem.h>
Lukas Auer515b9342019-08-21 21:14:44 +020019
20DECLARE_GLOBAL_DATA_PTR;
21
22struct fw_dynamic_info opensbi_info;
23
Randolph0a8063c2023-10-12 14:35:03 +080024static int spl_opensbi_find_os_node(void *blob, int *uboot_node, int os_type)
Lukas Auer515b9342019-08-21 21:14:44 +020025{
26 int fit_images_node, node;
27 const char *fit_os;
28
29 fit_images_node = fdt_path_offset(blob, "/fit-images");
30 if (fit_images_node < 0)
31 return -ENODEV;
32
33 fdt_for_each_subnode(node, blob, fit_images_node) {
34 fit_os = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
35 if (!fit_os)
36 continue;
37
Randolph0a8063c2023-10-12 14:35:03 +080038 if (genimg_get_os_id(fit_os) == os_type) {
Lukas Auer515b9342019-08-21 21:14:44 +020039 *uboot_node = node;
40 return 0;
41 }
42 }
43
44 return -ENODEV;
45}
46
Chanho Park8973e372023-08-29 10:20:14 +090047void __noreturn spl_invoke_opensbi(struct spl_image_info *spl_image)
Lukas Auer515b9342019-08-21 21:14:44 +020048{
Randolph0a8063c2023-10-12 14:35:03 +080049 int ret, os_node;
50 ulong os_entry;
51 int os_type;
Chanho Park8973e372023-08-29 10:20:14 +090052 typedef void __noreturn (*opensbi_entry_t)(ulong hartid, ulong dtb, ulong info);
53 opensbi_entry_t opensbi_entry;
Lukas Auer515b9342019-08-21 21:14:44 +020054
55 if (!spl_image->fdt_addr) {
56 pr_err("No device tree specified in SPL image\n");
57 hang();
58 }
59
Randolph0a8063c2023-10-12 14:35:03 +080060 /*
Randolphb1933972023-12-29 16:32:22 +080061 * Originally, u-boot-spl will place DTB directly after the kernel,
62 * but the size of the kernel did not include the BSS section, which
63 * means u-boot-spl will place the DTB in the kernel BSS section
64 * causing the DTB to be cleared by kernel BSS initializtion.
65 * Moving DTB in front of the kernel can avoid the error.
66 */
67#if CONFIG_IS_ENABLED(LOAD_FIT_OPENSBI_OS_BOOT) && \
Randolphc05e9e72024-03-22 19:36:37 +080068 CONFIG_VAL(PAYLOAD_ARGS_ADDR)
Randolphb1933972023-12-29 16:32:22 +080069 memcpy((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, spl_image->fdt_addr,
70 fdt_totalsize(spl_image->fdt_addr));
71 spl_image->fdt_addr = map_sysmem(CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
72#endif
73
74 /*
Randolph0a8063c2023-10-12 14:35:03 +080075 * Find next os image in /fit-images
Randolph263e2ae2023-10-12 14:35:07 +080076 * The next os image default is u-boot proper, once enable
77 * OpenSBI OS boot mode, the OS image should be linux.
Randolph0a8063c2023-10-12 14:35:03 +080078 */
Randolph263e2ae2023-10-12 14:35:07 +080079 if (CONFIG_IS_ENABLED(LOAD_FIT_OPENSBI_OS_BOOT))
80 os_type = IH_OS_LINUX;
81 else
82 os_type = IH_OS_U_BOOT;
83
Randolph0a8063c2023-10-12 14:35:03 +080084 ret = spl_opensbi_find_os_node(spl_image->fdt_addr, &os_node, os_type);
Lukas Auer515b9342019-08-21 21:14:44 +020085 if (ret) {
Randolph0a8063c2023-10-12 14:35:03 +080086 pr_err("Can't find %s node for opensbi, %d\n",
87 genimg_get_os_name(os_type), ret);
Lukas Auer515b9342019-08-21 21:14:44 +020088 hang();
89 }
90
91 /* Get U-Boot entry point */
Randolph0a8063c2023-10-12 14:35:03 +080092 ret = fit_image_get_entry(spl_image->fdt_addr, os_node, &os_entry);
Michal Simek9e64a552020-09-03 11:24:28 +020093 if (ret)
Randolph0a8063c2023-10-12 14:35:03 +080094 ret = fit_image_get_load(spl_image->fdt_addr, os_node, &os_entry);
Lukas Auer515b9342019-08-21 21:14:44 +020095
Nikita Shubina9f7ee32022-08-08 13:24:25 +030096 /* Prepare opensbi_info object */
Lukas Auer515b9342019-08-21 21:14:44 +020097 opensbi_info.magic = FW_DYNAMIC_INFO_MAGIC_VALUE;
98 opensbi_info.version = FW_DYNAMIC_INFO_VERSION;
Randolph0a8063c2023-10-12 14:35:03 +080099 opensbi_info.next_addr = os_entry;
Lukas Auer515b9342019-08-21 21:14:44 +0200100 opensbi_info.next_mode = FW_DYNAMIC_INFO_NEXT_MODE_S;
Nikita Shubind3d68062022-08-08 13:28:52 +0300101 opensbi_info.options = CONFIG_SPL_OPENSBI_SCRATCH_OPTIONS;
Lukas Auere105b612019-12-08 23:28:49 +0100102 opensbi_info.boot_hart = gd->arch.boot_hart;
Lukas Auer515b9342019-08-21 21:14:44 +0200103
Chanho Park8973e372023-08-29 10:20:14 +0900104 opensbi_entry = (opensbi_entry_t)spl_image->entry_point;
Lukas Auer515b9342019-08-21 21:14:44 +0200105 invalidate_icache_all();
106
Bin Mengb161f902020-04-16 08:09:30 -0700107#ifdef CONFIG_SPL_SMP
Lukas Auerfcedb9a2019-12-08 23:28:52 +0100108 /*
109 * Start OpenSBI on all secondary harts and wait for acknowledgment.
110 *
111 * OpenSBI first relocates itself to its link address. This is done by
112 * the main hart. To make sure no hart is still running U-Boot SPL
113 * during relocation, we wait for all secondary harts to acknowledge
114 * the call-function request before entering OpenSBI on the main hart.
115 * Otherwise, code corruption can occur if the link address ranges of
116 * U-Boot SPL and OpenSBI overlap.
117 */
Lukas Auer515b9342019-08-21 21:14:44 +0200118 ret = smp_call_function((ulong)spl_image->entry_point,
119 (ulong)spl_image->fdt_addr,
Lukas Auerfcedb9a2019-12-08 23:28:52 +0100120 (ulong)&opensbi_info, 1);
Lukas Auer515b9342019-08-21 21:14:44 +0200121 if (ret)
122 hang();
123#endif
124 opensbi_entry(gd->arch.boot_hart, (ulong)spl_image->fdt_addr,
125 (ulong)&opensbi_info);
126}