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> |
Randolph | 10c4ab8 | 2023-12-29 16:32:22 +0800 | [diff] [blame] | 19 | #include <mapmem.h> |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
| 23 | struct fw_dynamic_info opensbi_info; |
| 24 | |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 25 | 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] | 26 | { |
| 27 | int fit_images_node, node; |
| 28 | const char *fit_os; |
| 29 | |
| 30 | fit_images_node = fdt_path_offset(blob, "/fit-images"); |
| 31 | if (fit_images_node < 0) |
| 32 | return -ENODEV; |
| 33 | |
| 34 | fdt_for_each_subnode(node, blob, fit_images_node) { |
| 35 | fit_os = fdt_getprop(blob, node, FIT_OS_PROP, NULL); |
| 36 | if (!fit_os) |
| 37 | continue; |
| 38 | |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 39 | if (genimg_get_os_id(fit_os) == os_type) { |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 40 | *uboot_node = node; |
| 41 | return 0; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return -ENODEV; |
| 46 | } |
| 47 | |
Chanho Park | ef08687 | 2023-08-29 10:20:14 +0900 | [diff] [blame] | 48 | void __noreturn spl_invoke_opensbi(struct spl_image_info *spl_image) |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 49 | { |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 50 | int ret, os_node; |
| 51 | ulong os_entry; |
| 52 | int os_type; |
Chanho Park | ef08687 | 2023-08-29 10:20:14 +0900 | [diff] [blame] | 53 | typedef void __noreturn (*opensbi_entry_t)(ulong hartid, ulong dtb, ulong info); |
| 54 | opensbi_entry_t opensbi_entry; |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 55 | |
| 56 | if (!spl_image->fdt_addr) { |
| 57 | pr_err("No device tree specified in SPL image\n"); |
| 58 | hang(); |
| 59 | } |
| 60 | |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 61 | /* |
Randolph | 10c4ab8 | 2023-12-29 16:32:22 +0800 | [diff] [blame] | 62 | * Originally, u-boot-spl will place DTB directly after the kernel, |
| 63 | * but the size of the kernel did not include the BSS section, which |
| 64 | * means u-boot-spl will place the DTB in the kernel BSS section |
| 65 | * causing the DTB to be cleared by kernel BSS initializtion. |
| 66 | * Moving DTB in front of the kernel can avoid the error. |
| 67 | */ |
| 68 | #if CONFIG_IS_ENABLED(LOAD_FIT_OPENSBI_OS_BOOT) && \ |
| 69 | CONFIG_IS_ENABLED(PAYLOAD_ARGS_ADDR) |
| 70 | memcpy((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, spl_image->fdt_addr, |
| 71 | fdt_totalsize(spl_image->fdt_addr)); |
| 72 | spl_image->fdt_addr = map_sysmem(CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0); |
| 73 | #endif |
| 74 | |
| 75 | /* |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 76 | * Find next os image in /fit-images |
Randolph | 58fa2a5 | 2023-10-12 14:35:07 +0800 | [diff] [blame] | 77 | * The next os image default is u-boot proper, once enable |
| 78 | * OpenSBI OS boot mode, the OS image should be linux. |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 79 | */ |
Randolph | 58fa2a5 | 2023-10-12 14:35:07 +0800 | [diff] [blame] | 80 | if (CONFIG_IS_ENABLED(LOAD_FIT_OPENSBI_OS_BOOT)) |
| 81 | os_type = IH_OS_LINUX; |
| 82 | else |
| 83 | os_type = IH_OS_U_BOOT; |
| 84 | |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 85 | 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] | 86 | if (ret) { |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 87 | pr_err("Can't find %s node for opensbi, %d\n", |
| 88 | genimg_get_os_name(os_type), ret); |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 89 | hang(); |
| 90 | } |
| 91 | |
| 92 | /* Get U-Boot entry point */ |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 93 | 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] | 94 | if (ret) |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 95 | 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] | 96 | |
Nikita Shubin | 48da0ca | 2022-08-08 13:24:25 +0300 | [diff] [blame] | 97 | /* Prepare opensbi_info object */ |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 98 | opensbi_info.magic = FW_DYNAMIC_INFO_MAGIC_VALUE; |
| 99 | opensbi_info.version = FW_DYNAMIC_INFO_VERSION; |
Randolph | 5367b9e | 2023-10-12 14:35:03 +0800 | [diff] [blame] | 100 | opensbi_info.next_addr = os_entry; |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 101 | opensbi_info.next_mode = FW_DYNAMIC_INFO_NEXT_MODE_S; |
Nikita Shubin | aa0eda1 | 2022-08-08 13:28:52 +0300 | [diff] [blame] | 102 | opensbi_info.options = CONFIG_SPL_OPENSBI_SCRATCH_OPTIONS; |
Lukas Auer | b86f6d1 | 2019-12-08 23:28:49 +0100 | [diff] [blame] | 103 | opensbi_info.boot_hart = gd->arch.boot_hart; |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 104 | |
Chanho Park | ef08687 | 2023-08-29 10:20:14 +0900 | [diff] [blame] | 105 | opensbi_entry = (opensbi_entry_t)spl_image->entry_point; |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 106 | invalidate_icache_all(); |
| 107 | |
Bin Meng | 191636e | 2020-04-16 08:09:30 -0700 | [diff] [blame] | 108 | #ifdef CONFIG_SPL_SMP |
Lukas Auer | 0e1233c | 2019-12-08 23:28:52 +0100 | [diff] [blame] | 109 | /* |
| 110 | * Start OpenSBI on all secondary harts and wait for acknowledgment. |
| 111 | * |
| 112 | * OpenSBI first relocates itself to its link address. This is done by |
| 113 | * the main hart. To make sure no hart is still running U-Boot SPL |
| 114 | * during relocation, we wait for all secondary harts to acknowledge |
| 115 | * the call-function request before entering OpenSBI on the main hart. |
| 116 | * Otherwise, code corruption can occur if the link address ranges of |
| 117 | * U-Boot SPL and OpenSBI overlap. |
| 118 | */ |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 119 | ret = smp_call_function((ulong)spl_image->entry_point, |
| 120 | (ulong)spl_image->fdt_addr, |
Lukas Auer | 0e1233c | 2019-12-08 23:28:52 +0100 | [diff] [blame] | 121 | (ulong)&opensbi_info, 1); |
Lukas Auer | 5e30e45 | 2019-08-21 21:14:44 +0200 | [diff] [blame] | 122 | if (ret) |
| 123 | hang(); |
| 124 | #endif |
| 125 | opensbi_entry(gd->arch.boot_hart, (ulong)spl_image->fdt_addr, |
| 126 | (ulong)&opensbi_info); |
| 127 | } |