Lukas Auer | 5e30e45 | 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 | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | db41d65 | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 11 | #include <hang.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 12 | #include <image.h> |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 13 | #include <spl.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 15 | #include <asm/smp.h> |
| 16 | #include <opensbi.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 17 | #include <linux/libfdt.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 18 | #include <linux/printk.h> |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | struct fw_dynamic_info opensbi_info; |
| 23 | |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 24 | static int spl_opensbi_find_os_node(void *blob, int *uboot_node, int os_type) |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 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 | |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 38 | if (genimg_get_os_id(fit_os) == os_type) { |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 39 | *uboot_node = node; |
| 40 | return 0; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return -ENODEV; |
| 45 | } |
| 46 | |
Chanho Park | ef08687 | 2023-08-29 10:20:14 +0900 | [diff] [blame] | 47 | void __noreturn spl_invoke_opensbi(struct spl_image_info *spl_image) |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 48 | { |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 49 | int ret, os_node; |
| 50 | ulong os_entry; |
| 51 | int os_type; |
Chanho Park | ef08687 | 2023-08-29 10:20:14 +0900 | [diff] [blame] | 52 | typedef void __noreturn (*opensbi_entry_t)(ulong hartid, ulong dtb, ulong info); |
| 53 | opensbi_entry_t opensbi_entry; |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 54 | |
| 55 | if (!spl_image->fdt_addr) { |
| 56 | pr_err("No device tree specified in SPL image\n"); |
| 57 | hang(); |
| 58 | } |
| 59 | |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 60 | /* |
| 61 | * Find next os image in /fit-images |
Randolph | 58fa2a5 | 2023-10-12 14:35:07 +0800 | [diff] [blame] | 62 | * The next os image default is u-boot proper, once enable |
| 63 | * OpenSBI OS boot mode, the OS image should be linux. |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 64 | */ |
Randolph | 58fa2a5 | 2023-10-12 14:35:07 +0800 | [diff] [blame] | 65 | if (CONFIG_IS_ENABLED(LOAD_FIT_OPENSBI_OS_BOOT)) |
| 66 | os_type = IH_OS_LINUX; |
| 67 | else |
| 68 | os_type = IH_OS_U_BOOT; |
| 69 | |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 70 | ret = spl_opensbi_find_os_node(spl_image->fdt_addr, &os_node, os_type); |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 71 | if (ret) { |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 72 | pr_err("Can't find %s node for opensbi, %d\n", |
| 73 | genimg_get_os_name(os_type), ret); |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 74 | hang(); |
| 75 | } |
| 76 | |
| 77 | /* Get U-Boot entry point */ |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 78 | ret = fit_image_get_entry(spl_image->fdt_addr, os_node, &os_entry); |
Michal Simek | caa7fc2 | 2020-09-03 11:24:28 +0200 | [diff] [blame] | 79 | if (ret) |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 80 | ret = fit_image_get_load(spl_image->fdt_addr, os_node, &os_entry); |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 81 | |
Nikita Shubin | 48da0ca | 2022-08-08 13:24:25 +0300 | [diff] [blame] | 82 | /* Prepare opensbi_info object */ |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 83 | opensbi_info.magic = FW_DYNAMIC_INFO_MAGIC_VALUE; |
| 84 | opensbi_info.version = FW_DYNAMIC_INFO_VERSION; |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 85 | opensbi_info.next_addr = os_entry; |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 86 | opensbi_info.next_mode = FW_DYNAMIC_INFO_NEXT_MODE_S; |
Nikita Shubin | aa0eda1 | 2022-08-08 13:28:52 +0300 | [diff] [blame] | 87 | opensbi_info.options = CONFIG_SPL_OPENSBI_SCRATCH_OPTIONS; |
Lukas Auer | b86f6d1 | 2019-12-08 23:28:49 +0100 | [diff] [blame] | 88 | opensbi_info.boot_hart = gd->arch.boot_hart; |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 89 | |
Chanho Park | ef08687 | 2023-08-29 10:20:14 +0900 | [diff] [blame] | 90 | opensbi_entry = (opensbi_entry_t)spl_image->entry_point; |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 91 | invalidate_icache_all(); |
| 92 | |
Bin Meng | 191636e | 2020-04-16 08:09:30 -0700 | [diff] [blame] | 93 | #ifdef CONFIG_SPL_SMP |
Lukas Auer | 0e1233c | 2019-12-08 23:28:52 +0100 | [diff] [blame] | 94 | /* |
| 95 | * Start OpenSBI on all secondary harts and wait for acknowledgment. |
| 96 | * |
| 97 | * OpenSBI first relocates itself to its link address. This is done by |
| 98 | * the main hart. To make sure no hart is still running U-Boot SPL |
| 99 | * during relocation, we wait for all secondary harts to acknowledge |
| 100 | * the call-function request before entering OpenSBI on the main hart. |
| 101 | * Otherwise, code corruption can occur if the link address ranges of |
| 102 | * U-Boot SPL and OpenSBI overlap. |
| 103 | */ |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 104 | ret = smp_call_function((ulong)spl_image->entry_point, |
| 105 | (ulong)spl_image->fdt_addr, |
Lukas Auer | 0e1233c | 2019-12-08 23:28:52 +0100 | [diff] [blame] | 106 | (ulong)&opensbi_info, 1); |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 107 | if (ret) |
| 108 | hang(); |
| 109 | #endif |
| 110 | opensbi_entry(gd->arch.boot_hart, (ulong)spl_image->fdt_addr, |
| 111 | (ulong)&opensbi_info); |
| 112 | } |