Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 1 | // 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 Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 11 | #include <hang.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 12 | #include <image.h> |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 13 | #include <spl.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 15 | #include <asm/smp.h> |
| 16 | #include <opensbi.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 17 | #include <linux/libfdt.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 18 | #include <linux/printk.h> |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | struct fw_dynamic_info opensbi_info; |
| 23 | |
| 24 | static int spl_opensbi_find_uboot_node(void *blob, int *uboot_node) |
| 25 | { |
| 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 | |
| 38 | if (genimg_get_os_id(fit_os) == IH_OS_U_BOOT) { |
| 39 | *uboot_node = node; |
| 40 | return 0; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return -ENODEV; |
| 45 | } |
| 46 | |
Chanho Park | 8973e37 | 2023-08-29 10:20:14 +0900 | [diff] [blame] | 47 | void __noreturn spl_invoke_opensbi(struct spl_image_info *spl_image) |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 48 | { |
| 49 | int ret, uboot_node; |
| 50 | ulong uboot_entry; |
Chanho Park | 8973e37 | 2023-08-29 10:20:14 +0900 | [diff] [blame] | 51 | typedef void __noreturn (*opensbi_entry_t)(ulong hartid, ulong dtb, ulong info); |
| 52 | opensbi_entry_t opensbi_entry; |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 53 | |
| 54 | if (!spl_image->fdt_addr) { |
| 55 | pr_err("No device tree specified in SPL image\n"); |
| 56 | hang(); |
| 57 | } |
| 58 | |
| 59 | /* Find U-Boot image in /fit-images */ |
| 60 | ret = spl_opensbi_find_uboot_node(spl_image->fdt_addr, &uboot_node); |
| 61 | if (ret) { |
Sean Anderson | 358138f | 2020-06-06 15:26:03 -0400 | [diff] [blame] | 62 | pr_err("Can't find U-Boot node, %d\n", ret); |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 63 | hang(); |
| 64 | } |
| 65 | |
| 66 | /* Get U-Boot entry point */ |
Michal Simek | 9e64a55 | 2020-09-03 11:24:28 +0200 | [diff] [blame] | 67 | ret = fit_image_get_entry(spl_image->fdt_addr, uboot_node, &uboot_entry); |
| 68 | if (ret) |
| 69 | ret = fit_image_get_load(spl_image->fdt_addr, uboot_node, &uboot_entry); |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 70 | |
Nikita Shubin | a9f7ee3 | 2022-08-08 13:24:25 +0300 | [diff] [blame] | 71 | /* Prepare opensbi_info object */ |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 72 | opensbi_info.magic = FW_DYNAMIC_INFO_MAGIC_VALUE; |
| 73 | opensbi_info.version = FW_DYNAMIC_INFO_VERSION; |
| 74 | opensbi_info.next_addr = uboot_entry; |
| 75 | opensbi_info.next_mode = FW_DYNAMIC_INFO_NEXT_MODE_S; |
Nikita Shubin | d3d6806 | 2022-08-08 13:28:52 +0300 | [diff] [blame] | 76 | opensbi_info.options = CONFIG_SPL_OPENSBI_SCRATCH_OPTIONS; |
Lukas Auer | e105b61 | 2019-12-08 23:28:49 +0100 | [diff] [blame] | 77 | opensbi_info.boot_hart = gd->arch.boot_hart; |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 78 | |
Chanho Park | 8973e37 | 2023-08-29 10:20:14 +0900 | [diff] [blame] | 79 | opensbi_entry = (opensbi_entry_t)spl_image->entry_point; |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 80 | invalidate_icache_all(); |
| 81 | |
Bin Meng | b161f90 | 2020-04-16 08:09:30 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_SPL_SMP |
Lukas Auer | fcedb9a | 2019-12-08 23:28:52 +0100 | [diff] [blame] | 83 | /* |
| 84 | * Start OpenSBI on all secondary harts and wait for acknowledgment. |
| 85 | * |
| 86 | * OpenSBI first relocates itself to its link address. This is done by |
| 87 | * the main hart. To make sure no hart is still running U-Boot SPL |
| 88 | * during relocation, we wait for all secondary harts to acknowledge |
| 89 | * the call-function request before entering OpenSBI on the main hart. |
| 90 | * Otherwise, code corruption can occur if the link address ranges of |
| 91 | * U-Boot SPL and OpenSBI overlap. |
| 92 | */ |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 93 | ret = smp_call_function((ulong)spl_image->entry_point, |
| 94 | (ulong)spl_image->fdt_addr, |
Lukas Auer | fcedb9a | 2019-12-08 23:28:52 +0100 | [diff] [blame] | 95 | (ulong)&opensbi_info, 1); |
Lukas Auer | 515b934 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 96 | if (ret) |
| 97 | hang(); |
| 98 | #endif |
| 99 | opensbi_entry(gd->arch.boot_hart, (ulong)spl_image->fdt_addr, |
| 100 | (ulong)&opensbi_info); |
| 101 | } |