spl_atf.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. /*
  3. * Reference to the ARM TF Project,
  4. * plat/arm/common/arm_bl2_setup.c
  5. * Portions copyright (c) 2013-2016, ARM Limited and Contributors. All rights
  6. * reserved.
  7. * Copyright (C) 2016 Rockchip Electronic Co.,Ltd
  8. * Written by Kever Yang <kever.yang@rock-chips.com>
  9. * Copyright (C) 2017 Theobroma Systems Design und Consulting GmbH
  10. */
  11. #include <common.h>
  12. #include <atf_common.h>
  13. #include <cpu_func.h>
  14. #include <errno.h>
  15. #include <spl.h>
  16. static struct bl2_to_bl31_params_mem bl31_params_mem;
  17. static struct bl31_params *bl2_to_bl31_params;
  18. __weak struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl32_entry,
  19. uintptr_t bl33_entry,
  20. uintptr_t fdt_addr)
  21. {
  22. struct entry_point_info *bl32_ep_info;
  23. struct entry_point_info *bl33_ep_info;
  24. /*
  25. * Initialise the memory for all the arguments that needs to
  26. * be passed to BL31
  27. */
  28. memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem));
  29. /* Assign memory for TF related information */
  30. bl2_to_bl31_params = &bl31_params_mem.bl31_params;
  31. SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0);
  32. /* Fill BL31 related information */
  33. bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
  34. SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info,
  35. ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
  36. /* Fill BL32 related information */
  37. bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
  38. bl32_ep_info = &bl31_params_mem.bl32_ep_info;
  39. SET_PARAM_HEAD(bl32_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
  40. ATF_EP_SECURE);
  41. /* secure payload is optional, so set pc to 0 if absent */
  42. bl32_ep_info->args.arg3 = fdt_addr;
  43. bl32_ep_info->pc = bl32_entry ? bl32_entry : 0;
  44. bl32_ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
  45. DISABLE_ALL_EXECPTIONS);
  46. bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
  47. SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info,
  48. ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
  49. /* Fill BL33 related information */
  50. bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
  51. bl33_ep_info = &bl31_params_mem.bl33_ep_info;
  52. SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
  53. ATF_EP_NON_SECURE);
  54. /* BL33 expects to receive the primary CPU MPID (through x0) */
  55. bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
  56. bl33_ep_info->pc = bl33_entry;
  57. bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
  58. DISABLE_ALL_EXECPTIONS);
  59. bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
  60. SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info,
  61. ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
  62. return bl2_to_bl31_params;
  63. }
  64. static inline void raw_write_daif(unsigned int daif)
  65. {
  66. __asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory");
  67. }
  68. typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params);
  69. static void bl31_entry(uintptr_t bl31_entry, uintptr_t bl32_entry,
  70. uintptr_t bl33_entry, uintptr_t fdt_addr)
  71. {
  72. struct bl31_params *bl31_params;
  73. atf_entry_t atf_entry = (atf_entry_t)bl31_entry;
  74. bl31_params = bl2_plat_get_bl31_params(bl32_entry, bl33_entry,
  75. fdt_addr);
  76. raw_write_daif(SPSR_EXCEPTION_MASK);
  77. dcache_disable();
  78. atf_entry((void *)bl31_params, (void *)fdt_addr);
  79. }
  80. static int spl_fit_images_find(void *blob, int os)
  81. {
  82. int parent, node, ndepth = 0;
  83. const void *data;
  84. if (!blob)
  85. return -FDT_ERR_BADMAGIC;
  86. parent = fdt_path_offset(blob, "/fit-images");
  87. if (parent < 0)
  88. return -FDT_ERR_NOTFOUND;
  89. for (node = fdt_next_node(blob, parent, &ndepth);
  90. (node >= 0) && (ndepth > 0);
  91. node = fdt_next_node(blob, node, &ndepth)) {
  92. if (ndepth != 1)
  93. continue;
  94. data = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
  95. if (!data)
  96. continue;
  97. if (genimg_get_os_id(data) == os)
  98. return node;
  99. };
  100. return -FDT_ERR_NOTFOUND;
  101. }
  102. uintptr_t spl_fit_images_get_entry(void *blob, int node)
  103. {
  104. ulong val;
  105. val = fdt_getprop_u32(blob, node, "entry-point");
  106. if (val == FDT_ERROR)
  107. val = fdt_getprop_u32(blob, node, "load-addr");
  108. debug("%s: entry point 0x%lx\n", __func__, val);
  109. return val;
  110. }
  111. void spl_invoke_atf(struct spl_image_info *spl_image)
  112. {
  113. uintptr_t bl32_entry = 0;
  114. uintptr_t bl33_entry = CONFIG_SYS_TEXT_BASE;
  115. void *blob = spl_image->fdt_addr;
  116. uintptr_t platform_param = (uintptr_t)blob;
  117. int node;
  118. /*
  119. * Find the OP-TEE binary (in /fit-images) load address or
  120. * entry point (if different) and pass it as the BL3-2 entry
  121. * point, this is optional.
  122. */
  123. node = spl_fit_images_find(blob, IH_OS_TEE);
  124. if (node >= 0)
  125. bl32_entry = spl_fit_images_get_entry(blob, node);
  126. /*
  127. * Find the U-Boot binary (in /fit-images) load addreess or
  128. * entry point (if different) and pass it as the BL3-3 entry
  129. * point.
  130. * This will need to be extended to support Falcon mode.
  131. */
  132. node = spl_fit_images_find(blob, IH_OS_U_BOOT);
  133. if (node >= 0)
  134. bl33_entry = spl_fit_images_get_entry(blob, node);
  135. /*
  136. * If ATF_NO_PLATFORM_PARAM is set, we override the platform
  137. * parameter and always pass 0. This is a workaround for
  138. * older ATF versions that have insufficiently robust (or
  139. * overzealous) argument validation.
  140. */
  141. if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM))
  142. platform_param = 0;
  143. /*
  144. * We don't provide a BL3-2 entry yet, but this will be possible
  145. * using similar logic.
  146. */
  147. bl31_entry(spl_image->entry_point, bl32_entry,
  148. bl33_entry, platform_param);
  149. }