blob: 1c0abf8553a87e9a714c6d8c38ce69cd3b36cef1 [file] [log] [blame]
Lukas Auer5e30e452019-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 */
8#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07009#include <cpu_func.h>
Lukas Auer5e30e452019-08-21 21:14:44 +020010#include <errno.h>
Simon Glassdb41d652019-12-28 10:45:07 -070011#include <hang.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060012#include <image.h>
Lukas Auer5e30e452019-08-21 21:14:44 +020013#include <spl.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Lukas Auer5e30e452019-08-21 21:14:44 +020015#include <asm/smp.h>
16#include <opensbi.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060017#include <linux/libfdt.h>
Lukas Auer5e30e452019-08-21 21:14:44 +020018
19DECLARE_GLOBAL_DATA_PTR;
20
21struct fw_dynamic_info opensbi_info;
22
23static int spl_opensbi_find_uboot_node(void *blob, int *uboot_node)
24{
25 int fit_images_node, node;
26 const char *fit_os;
27
28 fit_images_node = fdt_path_offset(blob, "/fit-images");
29 if (fit_images_node < 0)
30 return -ENODEV;
31
32 fdt_for_each_subnode(node, blob, fit_images_node) {
33 fit_os = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
34 if (!fit_os)
35 continue;
36
37 if (genimg_get_os_id(fit_os) == IH_OS_U_BOOT) {
38 *uboot_node = node;
39 return 0;
40 }
41 }
42
43 return -ENODEV;
44}
45
46void spl_invoke_opensbi(struct spl_image_info *spl_image)
47{
48 int ret, uboot_node;
49 ulong uboot_entry;
50 void (*opensbi_entry)(ulong hartid, ulong dtb, ulong info);
51
52 if (!spl_image->fdt_addr) {
53 pr_err("No device tree specified in SPL image\n");
54 hang();
55 }
56
57 /* Find U-Boot image in /fit-images */
58 ret = spl_opensbi_find_uboot_node(spl_image->fdt_addr, &uboot_node);
59 if (ret) {
Sean Anderson79849222020-06-06 15:26:03 -040060 pr_err("Can't find U-Boot node, %d\n", ret);
Lukas Auer5e30e452019-08-21 21:14:44 +020061 hang();
62 }
63
64 /* Get U-Boot entry point */
Michal Simekcaa7fc22020-09-03 11:24:28 +020065 ret = fit_image_get_entry(spl_image->fdt_addr, uboot_node, &uboot_entry);
66 if (ret)
67 ret = fit_image_get_load(spl_image->fdt_addr, uboot_node, &uboot_entry);
Lukas Auer5e30e452019-08-21 21:14:44 +020068
69 /* Prepare obensbi_info object */
70 opensbi_info.magic = FW_DYNAMIC_INFO_MAGIC_VALUE;
71 opensbi_info.version = FW_DYNAMIC_INFO_VERSION;
72 opensbi_info.next_addr = uboot_entry;
73 opensbi_info.next_mode = FW_DYNAMIC_INFO_NEXT_MODE_S;
74 opensbi_info.options = SBI_SCRATCH_NO_BOOT_PRINTS;
Lukas Auerb86f6d12019-12-08 23:28:49 +010075 opensbi_info.boot_hart = gd->arch.boot_hart;
Lukas Auer5e30e452019-08-21 21:14:44 +020076
77 opensbi_entry = (void (*)(ulong, ulong, ulong))spl_image->entry_point;
78 invalidate_icache_all();
79
Bin Meng191636e2020-04-16 08:09:30 -070080#ifdef CONFIG_SPL_SMP
Lukas Auer0e1233c2019-12-08 23:28:52 +010081 /*
82 * Start OpenSBI on all secondary harts and wait for acknowledgment.
83 *
84 * OpenSBI first relocates itself to its link address. This is done by
85 * the main hart. To make sure no hart is still running U-Boot SPL
86 * during relocation, we wait for all secondary harts to acknowledge
87 * the call-function request before entering OpenSBI on the main hart.
88 * Otherwise, code corruption can occur if the link address ranges of
89 * U-Boot SPL and OpenSBI overlap.
90 */
Lukas Auer5e30e452019-08-21 21:14:44 +020091 ret = smp_call_function((ulong)spl_image->entry_point,
92 (ulong)spl_image->fdt_addr,
Lukas Auer0e1233c2019-12-08 23:28:52 +010093 (ulong)&opensbi_info, 1);
Lukas Auer5e30e452019-08-21 21:14:44 +020094 if (ret)
95 hang();
96#endif
97 opensbi_entry(gd->arch.boot_hart, (ulong)spl_image->fdt_addr,
98 (ulong)&opensbi_info);
99}