blob: a136073fdbb4b6e29182c4b28e7a613a467a7001 [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 */
8#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Lukas Auer515b9342019-08-21 21:14:44 +020010#include <errno.h>
Simon Glassf11478f2019-12-28 10:45:07 -070011#include <hang.h>
Lukas Auer515b9342019-08-21 21:14:44 +020012#include <spl.h>
13#include <asm/smp.h>
14#include <opensbi.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18struct fw_dynamic_info opensbi_info;
19
20static int spl_opensbi_find_uboot_node(void *blob, int *uboot_node)
21{
22 int fit_images_node, node;
23 const char *fit_os;
24
25 fit_images_node = fdt_path_offset(blob, "/fit-images");
26 if (fit_images_node < 0)
27 return -ENODEV;
28
29 fdt_for_each_subnode(node, blob, fit_images_node) {
30 fit_os = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
31 if (!fit_os)
32 continue;
33
34 if (genimg_get_os_id(fit_os) == IH_OS_U_BOOT) {
35 *uboot_node = node;
36 return 0;
37 }
38 }
39
40 return -ENODEV;
41}
42
43void spl_invoke_opensbi(struct spl_image_info *spl_image)
44{
45 int ret, uboot_node;
46 ulong uboot_entry;
47 void (*opensbi_entry)(ulong hartid, ulong dtb, ulong info);
48
49 if (!spl_image->fdt_addr) {
50 pr_err("No device tree specified in SPL image\n");
51 hang();
52 }
53
54 /* Find U-Boot image in /fit-images */
55 ret = spl_opensbi_find_uboot_node(spl_image->fdt_addr, &uboot_node);
56 if (ret) {
57 pr_err("Can't find U-Boot node, %d", ret);
58 hang();
59 }
60
61 /* Get U-Boot entry point */
62 uboot_entry = fdt_getprop_u32(spl_image->fdt_addr, uboot_node,
63 "entry-point");
64 if (uboot_entry == FDT_ERROR)
65 uboot_entry = fdt_getprop_u32(spl_image->fdt_addr, uboot_node,
66 "load-addr");
67
68 /* Prepare obensbi_info object */
69 opensbi_info.magic = FW_DYNAMIC_INFO_MAGIC_VALUE;
70 opensbi_info.version = FW_DYNAMIC_INFO_VERSION;
71 opensbi_info.next_addr = uboot_entry;
72 opensbi_info.next_mode = FW_DYNAMIC_INFO_NEXT_MODE_S;
73 opensbi_info.options = SBI_SCRATCH_NO_BOOT_PRINTS;
Lukas Auere105b612019-12-08 23:28:49 +010074 opensbi_info.boot_hart = gd->arch.boot_hart;
Lukas Auer515b9342019-08-21 21:14:44 +020075
76 opensbi_entry = (void (*)(ulong, ulong, ulong))spl_image->entry_point;
77 invalidate_icache_all();
78
79#ifdef CONFIG_SMP
Lukas Auerfcedb9a2019-12-08 23:28:52 +010080 /*
81 * Start OpenSBI on all secondary harts and wait for acknowledgment.
82 *
83 * OpenSBI first relocates itself to its link address. This is done by
84 * the main hart. To make sure no hart is still running U-Boot SPL
85 * during relocation, we wait for all secondary harts to acknowledge
86 * the call-function request before entering OpenSBI on the main hart.
87 * Otherwise, code corruption can occur if the link address ranges of
88 * U-Boot SPL and OpenSBI overlap.
89 */
Lukas Auer515b9342019-08-21 21:14:44 +020090 ret = smp_call_function((ulong)spl_image->entry_point,
91 (ulong)spl_image->fdt_addr,
Lukas Auerfcedb9a2019-12-08 23:28:52 +010092 (ulong)&opensbi_info, 1);
Lukas Auer515b9342019-08-21 21:14:44 +020093 if (ret)
94 hang();
95#endif
96 opensbi_entry(gd->arch.boot_hart, (ulong)spl_image->fdt_addr,
97 (ulong)&opensbi_info);
98}