spl_opensbi.c 2.7 KB

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