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 <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. ret = fit_image_get_entry(spl_image->fdt_addr, uboot_node, &uboot_entry);
  54. if (ret)
  55. ret = fit_image_get_load(spl_image->fdt_addr, uboot_node, &uboot_entry);
  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_SPL_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. }