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