spl_atf.c 5.1 KB

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