spl_atf.c 5.3 KB

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