spl_atf.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <errno.h>
  14. #include <spl.h>
  15. static struct bl2_to_bl31_params_mem bl31_params_mem;
  16. static struct bl31_params *bl2_to_bl31_params;
  17. /**
  18. * bl2_plat_get_bl31_params() - prepare params for bl31.
  19. *
  20. * This function assigns a pointer to the memory that the platform has kept
  21. * aside to pass platform specific and trusted firmware related information
  22. * to BL31. This memory is allocated by allocating memory to
  23. * bl2_to_bl31_params_mem structure which is a superset of all the
  24. * structure whose information is passed to BL31
  25. * NOTE: This function should be called only once and should be done
  26. * before generating params to BL31
  27. *
  28. * @return bl31 params structure pointer
  29. */
  30. static struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl32_entry,
  31. uintptr_t bl33_entry,
  32. uintptr_t fdt_addr)
  33. {
  34. struct entry_point_info *bl32_ep_info;
  35. struct entry_point_info *bl33_ep_info;
  36. /*
  37. * Initialise the memory for all the arguments that needs to
  38. * be passed to BL31
  39. */
  40. memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem));
  41. /* Assign memory for TF related information */
  42. bl2_to_bl31_params = &bl31_params_mem.bl31_params;
  43. SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0);
  44. /* Fill BL31 related information */
  45. bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
  46. SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info,
  47. ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
  48. /* Fill BL32 related information */
  49. bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
  50. bl32_ep_info = &bl31_params_mem.bl32_ep_info;
  51. SET_PARAM_HEAD(bl32_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
  52. ATF_EP_SECURE);
  53. /* secure payload is optional, so set pc to 0 if absent */
  54. bl32_ep_info->args.arg3 = fdt_addr;
  55. bl32_ep_info->pc = bl32_entry ? bl32_entry : 0;
  56. bl32_ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
  57. DISABLE_ALL_EXECPTIONS);
  58. bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
  59. SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info,
  60. ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
  61. /* Fill BL33 related information */
  62. bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
  63. bl33_ep_info = &bl31_params_mem.bl33_ep_info;
  64. SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
  65. ATF_EP_NON_SECURE);
  66. /* BL33 expects to receive the primary CPU MPID (through x0) */
  67. bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
  68. bl33_ep_info->pc = bl33_entry;
  69. bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
  70. DISABLE_ALL_EXECPTIONS);
  71. bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
  72. SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info,
  73. ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
  74. return bl2_to_bl31_params;
  75. }
  76. static inline void raw_write_daif(unsigned int daif)
  77. {
  78. __asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory");
  79. }
  80. typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params);
  81. static void bl31_entry(uintptr_t bl31_entry, uintptr_t bl32_entry,
  82. uintptr_t bl33_entry, uintptr_t fdt_addr)
  83. {
  84. struct bl31_params *bl31_params;
  85. atf_entry_t atf_entry = (atf_entry_t)bl31_entry;
  86. bl31_params = bl2_plat_get_bl31_params(bl32_entry, bl33_entry,
  87. fdt_addr);
  88. raw_write_daif(SPSR_EXCEPTION_MASK);
  89. dcache_disable();
  90. atf_entry((void *)bl31_params, (void *)fdt_addr);
  91. }
  92. static int spl_fit_images_find(void *blob, int os)
  93. {
  94. int parent, node, ndepth;
  95. const void *data;
  96. if (!blob)
  97. return -FDT_ERR_BADMAGIC;
  98. parent = fdt_path_offset(blob, "/fit-images");
  99. if (parent < 0)
  100. return -FDT_ERR_NOTFOUND;
  101. for (node = fdt_next_node(blob, parent, &ndepth);
  102. (node >= 0) && (ndepth > 0);
  103. node = fdt_next_node(blob, node, &ndepth)) {
  104. if (ndepth != 1)
  105. continue;
  106. data = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
  107. if (!data)
  108. continue;
  109. if (genimg_get_os_id(data) == os)
  110. return node;
  111. };
  112. return -FDT_ERR_NOTFOUND;
  113. }
  114. uintptr_t spl_fit_images_get_entry(void *blob, int node)
  115. {
  116. ulong val;
  117. val = fdt_getprop_u32(blob, node, "entry-point");
  118. if (val == FDT_ERROR)
  119. val = fdt_getprop_u32(blob, node, "load-addr");
  120. debug("%s: entry point 0x%lx\n", __func__, val);
  121. return val;
  122. }
  123. void spl_invoke_atf(struct spl_image_info *spl_image)
  124. {
  125. uintptr_t bl32_entry = 0;
  126. uintptr_t bl33_entry = CONFIG_SYS_TEXT_BASE;
  127. void *blob = spl_image->fdt_addr;
  128. uintptr_t platform_param = (uintptr_t)blob;
  129. int node;
  130. /*
  131. * Find the OP-TEE binary (in /fit-images) load address or
  132. * entry point (if different) and pass it as the BL3-2 entry
  133. * point, this is optional.
  134. */
  135. node = spl_fit_images_find(blob, IH_OS_TEE);
  136. if (node >= 0)
  137. bl32_entry = spl_fit_images_get_entry(blob, node);
  138. /*
  139. * Find the U-Boot binary (in /fit-images) load addreess or
  140. * entry point (if different) and pass it as the BL3-3 entry
  141. * point.
  142. * This will need to be extended to support Falcon mode.
  143. */
  144. node = spl_fit_images_find(blob, IH_OS_U_BOOT);
  145. if (node >= 0)
  146. bl33_entry = spl_fit_images_get_entry(blob, node);
  147. /*
  148. * If ATF_NO_PLATFORM_PARAM is set, we override the platform
  149. * parameter and always pass 0. This is a workaround for
  150. * older ATF versions that have insufficiently robust (or
  151. * overzealous) argument validation.
  152. */
  153. if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM))
  154. platform_param = 0;
  155. /*
  156. * We don't provide a BL3-2 entry yet, but this will be possible
  157. * using similar logic.
  158. */
  159. bl31_entry(spl_image->entry_point, bl32_entry,
  160. bl33_entry, platform_param);
  161. }