spl_opensbi.c 2.6 KB

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