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/global_data.h>
  15. #include <asm/smp.h>
  16. #include <opensbi.h>
  17. #include <linux/libfdt.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. struct fw_dynamic_info opensbi_info;
  20. static int spl_opensbi_find_uboot_node(void *blob, int *uboot_node)
  21. {
  22. int fit_images_node, node;
  23. const char *fit_os;
  24. fit_images_node = fdt_path_offset(blob, "/fit-images");
  25. if (fit_images_node < 0)
  26. return -ENODEV;
  27. fdt_for_each_subnode(node, blob, fit_images_node) {
  28. fit_os = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
  29. if (!fit_os)
  30. continue;
  31. if (genimg_get_os_id(fit_os) == IH_OS_U_BOOT) {
  32. *uboot_node = node;
  33. return 0;
  34. }
  35. }
  36. return -ENODEV;
  37. }
  38. void __noreturn spl_invoke_opensbi(struct spl_image_info *spl_image)
  39. {
  40. int ret, uboot_node;
  41. ulong uboot_entry;
  42. typedef void __noreturn (*opensbi_entry_t)(ulong hartid, ulong dtb, ulong info);
  43. opensbi_entry_t opensbi_entry;
  44. if (!spl_image->fdt_addr) {
  45. pr_err("No device tree specified in SPL image\n");
  46. hang();
  47. }
  48. /* Find U-Boot image in /fit-images */
  49. ret = spl_opensbi_find_uboot_node(spl_image->fdt_addr, &uboot_node);
  50. if (ret) {
  51. pr_err("Can't find U-Boot node, %d\n", ret);
  52. hang();
  53. }
  54. /* Get U-Boot entry point */
  55. ret = fit_image_get_entry(spl_image->fdt_addr, uboot_node, &uboot_entry);
  56. if (ret)
  57. ret = fit_image_get_load(spl_image->fdt_addr, uboot_node, &uboot_entry);
  58. /* Prepare opensbi_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 = CONFIG_SPL_OPENSBI_SCRATCH_OPTIONS;
  64. opensbi_info.boot_hart = gd->arch.boot_hart;
  65. opensbi_entry = (opensbi_entry_t)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. }