spl_atf.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Reference to the ARM TF Project,
  3. * plat/arm/common/arm_bl2_setup.c
  4. * Portions copyright (c) 2013-2016, ARM Limited and Contributors. All rights
  5. * reserved.
  6. * Copyright (C) 2016 Rockchip Electronic Co.,Ltd
  7. * Written by Kever Yang <kever.yang@rock-chips.com>
  8. * Copyright (C) 2017 Theobroma Systems Design und Consulting GmbH
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. #include <common.h>
  13. #include <atf_common.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 bl33_entry)
  32. {
  33. struct entry_point_info *bl33_ep_info;
  34. /*
  35. * Initialise the memory for all the arguments that needs to
  36. * be passed to BL31
  37. */
  38. memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem));
  39. /* Assign memory for TF related information */
  40. bl2_to_bl31_params = &bl31_params_mem.bl31_params;
  41. SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0);
  42. /* Fill BL31 related information */
  43. SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info,
  44. ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
  45. /* Fill BL32 related information if it exists */
  46. #ifdef BL32_BASE
  47. bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
  48. SET_PARAM_HEAD(bl2_to_bl31_params->bl32_ep_info, ATF_PARAM_EP,
  49. ATF_VERSION_1, 0);
  50. bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
  51. SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info,
  52. ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 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. int node;
  120. /*
  121. * Find the U-Boot binary (in /fit-images) load addreess or
  122. * entry point (if different) and pass it as the BL3-3 entry
  123. * point.
  124. * This will need to be extended to support Falcon mode.
  125. */
  126. node = spl_fit_images_find_uboot(blob);
  127. if (node >= 0)
  128. bl33_entry = spl_fit_images_get_entry(blob, node);
  129. /*
  130. * We don't provide a BL3-2 entry yet, but this will be possible
  131. * using similar logic.
  132. */
  133. bl31_entry(spl_image->entry_point, bl33_entry, (uintptr_t)blob);
  134. }