spl_atf.c 5.4 KB

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