spl_atf.c 5.8 KB

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