psci_setup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) 2013-2020, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stddef.h>
  8. #include <arch.h>
  9. #include <arch_helpers.h>
  10. #include <common/bl_common.h>
  11. #include <context.h>
  12. #include <lib/cpus/errata.h>
  13. #include <lib/el3_runtime/context_mgmt.h>
  14. #include <plat/common/platform.h>
  15. #include "psci_private.h"
  16. /*
  17. * Check that PLATFORM_CORE_COUNT fits into the number of cores
  18. * that can be represented by PSCI_MAX_CPUS_INDEX.
  19. */
  20. CASSERT(PLATFORM_CORE_COUNT <= (PSCI_MAX_CPUS_INDEX + 1U), assert_psci_cores_overflow);
  21. /*******************************************************************************
  22. * Per cpu non-secure contexts used to program the architectural state prior
  23. * return to the normal world.
  24. * TODO: Use the memory allocator to set aside memory for the contexts instead
  25. * of relying on platform defined constants.
  26. ******************************************************************************/
  27. static cpu_context_t psci_ns_context[PLATFORM_CORE_COUNT];
  28. /******************************************************************************
  29. * Define the psci capability variable.
  30. *****************************************************************************/
  31. unsigned int psci_caps;
  32. /*******************************************************************************
  33. * Function which initializes the 'psci_non_cpu_pd_nodes' or the
  34. * 'psci_cpu_pd_nodes' corresponding to the power level.
  35. ******************************************************************************/
  36. static void __init psci_init_pwr_domain_node(uint16_t node_idx,
  37. unsigned int parent_idx,
  38. unsigned char level)
  39. {
  40. if (level > PSCI_CPU_PWR_LVL) {
  41. assert(node_idx < PSCI_NUM_NON_CPU_PWR_DOMAINS);
  42. psci_non_cpu_pd_nodes[node_idx].level = level;
  43. psci_lock_init(psci_non_cpu_pd_nodes, node_idx);
  44. psci_non_cpu_pd_nodes[node_idx].parent_node = parent_idx;
  45. psci_non_cpu_pd_nodes[node_idx].local_state =
  46. PLAT_MAX_OFF_STATE;
  47. } else {
  48. psci_cpu_data_t *svc_cpu_data;
  49. assert(node_idx < PLATFORM_CORE_COUNT);
  50. psci_cpu_pd_nodes[node_idx].parent_node = parent_idx;
  51. /* Initialize with an invalid mpidr */
  52. psci_cpu_pd_nodes[node_idx].mpidr = PSCI_INVALID_MPIDR;
  53. svc_cpu_data =
  54. &(_cpu_data_by_index(node_idx)->psci_svc_cpu_data);
  55. /* Set the Affinity Info for the cores as OFF */
  56. svc_cpu_data->aff_info_state = AFF_STATE_OFF;
  57. /* Invalidate the suspend level for the cpu */
  58. svc_cpu_data->target_pwrlvl = PSCI_INVALID_PWR_LVL;
  59. /* Set the power state to OFF state */
  60. svc_cpu_data->local_state = PLAT_MAX_OFF_STATE;
  61. psci_flush_dcache_range((uintptr_t)svc_cpu_data,
  62. sizeof(*svc_cpu_data));
  63. cm_set_context_by_index(node_idx,
  64. (void *) &psci_ns_context[node_idx],
  65. NON_SECURE);
  66. }
  67. }
  68. /*******************************************************************************
  69. * This functions updates cpu_start_idx and ncpus field for each of the node in
  70. * psci_non_cpu_pd_nodes[]. It does so by comparing the parent nodes of each of
  71. * the CPUs and check whether they match with the parent of the previous
  72. * CPU. The basic assumption for this work is that children of the same parent
  73. * are allocated adjacent indices. The platform should ensure this though proper
  74. * mapping of the CPUs to indices via plat_core_pos_by_mpidr() and
  75. * plat_my_core_pos() APIs.
  76. *******************************************************************************/
  77. static void __init psci_update_pwrlvl_limits(void)
  78. {
  79. unsigned int cpu_idx;
  80. int j;
  81. unsigned int nodes_idx[PLAT_MAX_PWR_LVL] = {0};
  82. unsigned int temp_index[PLAT_MAX_PWR_LVL];
  83. for (cpu_idx = 0; cpu_idx < psci_plat_core_count; cpu_idx++) {
  84. psci_get_parent_pwr_domain_nodes(cpu_idx,
  85. PLAT_MAX_PWR_LVL,
  86. temp_index);
  87. for (j = (int)PLAT_MAX_PWR_LVL - 1; j >= 0; j--) {
  88. if (temp_index[j] != nodes_idx[j]) {
  89. nodes_idx[j] = temp_index[j];
  90. psci_non_cpu_pd_nodes[nodes_idx[j]].cpu_start_idx
  91. = cpu_idx;
  92. }
  93. psci_non_cpu_pd_nodes[nodes_idx[j]].ncpus++;
  94. }
  95. }
  96. }
  97. /*******************************************************************************
  98. * Core routine to populate the power domain tree. The tree descriptor passed by
  99. * the platform is populated breadth-first and the first entry in the map
  100. * informs the number of root power domains. The parent nodes of the root nodes
  101. * will point to an invalid entry(-1).
  102. ******************************************************************************/
  103. static unsigned int __init populate_power_domain_tree(const unsigned char
  104. *topology)
  105. {
  106. unsigned int i, j = 0U, num_nodes_at_lvl = 1U, num_nodes_at_next_lvl;
  107. unsigned int node_index = 0U, num_children;
  108. unsigned int parent_node_index = 0U;
  109. int level = (int)PLAT_MAX_PWR_LVL;
  110. /*
  111. * For each level the inputs are:
  112. * - number of nodes at this level in plat_array i.e. num_nodes_at_level
  113. * This is the sum of values of nodes at the parent level.
  114. * - Index of first entry at this level in the plat_array i.e.
  115. * parent_node_index.
  116. * - Index of first free entry in psci_non_cpu_pd_nodes[] or
  117. * psci_cpu_pd_nodes[] i.e. node_index depending upon the level.
  118. */
  119. while (level >= (int) PSCI_CPU_PWR_LVL) {
  120. num_nodes_at_next_lvl = 0U;
  121. /*
  122. * For each entry (parent node) at this level in the plat_array:
  123. * - Find the number of children
  124. * - Allocate a node in a power domain array for each child
  125. * - Set the parent of the child to the parent_node_index - 1
  126. * - Increment parent_node_index to point to the next parent
  127. * - Accumulate the number of children at next level.
  128. */
  129. for (i = 0U; i < num_nodes_at_lvl; i++) {
  130. assert(parent_node_index <=
  131. PSCI_NUM_NON_CPU_PWR_DOMAINS);
  132. num_children = topology[parent_node_index];
  133. for (j = node_index;
  134. j < (node_index + num_children); j++)
  135. psci_init_pwr_domain_node((uint16_t)j,
  136. parent_node_index - 1U,
  137. (unsigned char)level);
  138. node_index = j;
  139. num_nodes_at_next_lvl += num_children;
  140. parent_node_index++;
  141. }
  142. num_nodes_at_lvl = num_nodes_at_next_lvl;
  143. level--;
  144. /* Reset the index for the cpu power domain array */
  145. if (level == (int) PSCI_CPU_PWR_LVL)
  146. node_index = 0;
  147. }
  148. /* Validate the sanity of array exported by the platform */
  149. assert(j <= PLATFORM_CORE_COUNT);
  150. return j;
  151. }
  152. /*******************************************************************************
  153. * This function does the architectural setup and takes the warm boot
  154. * entry-point `mailbox_ep` as an argument. The function also initializes the
  155. * power domain topology tree by querying the platform. The power domain nodes
  156. * higher than the CPU are populated in the array psci_non_cpu_pd_nodes[] and
  157. * the CPU power domains are populated in psci_cpu_pd_nodes[]. The platform
  158. * exports its static topology map through the
  159. * populate_power_domain_topology_tree() API. The algorithm populates the
  160. * psci_non_cpu_pd_nodes and psci_cpu_pd_nodes iteratively by using this
  161. * topology map. On a platform that implements two clusters of 2 cpus each,
  162. * and supporting 3 domain levels, the populated psci_non_cpu_pd_nodes would
  163. * look like this:
  164. *
  165. * ---------------------------------------------------
  166. * | system node | cluster 0 node | cluster 1 node |
  167. * ---------------------------------------------------
  168. *
  169. * And populated psci_cpu_pd_nodes would look like this :
  170. * <- cpus cluster0 -><- cpus cluster1 ->
  171. * ------------------------------------------------
  172. * | CPU 0 | CPU 1 | CPU 2 | CPU 3 |
  173. * ------------------------------------------------
  174. ******************************************************************************/
  175. int __init psci_setup(const psci_lib_args_t *lib_args)
  176. {
  177. const unsigned char *topology_tree;
  178. assert(VERIFY_PSCI_LIB_ARGS_V1(lib_args));
  179. /* Do the Architectural initialization */
  180. psci_arch_setup();
  181. /* Query the topology map from the platform */
  182. topology_tree = plat_get_power_domain_tree_desc();
  183. /* Populate the power domain arrays using the platform topology map */
  184. psci_plat_core_count = populate_power_domain_tree(topology_tree);
  185. /* Update the CPU limits for each node in psci_non_cpu_pd_nodes */
  186. psci_update_pwrlvl_limits();
  187. /* Populate the mpidr field of cpu node for this CPU */
  188. psci_cpu_pd_nodes[plat_my_core_pos()].mpidr =
  189. read_mpidr() & MPIDR_AFFINITY_MASK;
  190. psci_init_req_local_pwr_states();
  191. /*
  192. * Set the requested and target state of this CPU and all the higher
  193. * power domain levels for this CPU to run.
  194. */
  195. psci_set_pwr_domains_to_run(PLAT_MAX_PWR_LVL);
  196. (void) plat_setup_psci_ops((uintptr_t)lib_args->mailbox_ep,
  197. &psci_plat_pm_ops);
  198. assert(psci_plat_pm_ops != NULL);
  199. /*
  200. * Flush `psci_plat_pm_ops` as it will be accessed by secondary CPUs
  201. * during warm boot, possibly before data cache is enabled.
  202. */
  203. psci_flush_dcache_range((uintptr_t)&psci_plat_pm_ops,
  204. sizeof(psci_plat_pm_ops));
  205. /* Initialize the psci capability */
  206. psci_caps = PSCI_GENERIC_CAP;
  207. if (psci_plat_pm_ops->pwr_domain_off != NULL)
  208. psci_caps |= define_psci_cap(PSCI_CPU_OFF);
  209. if ((psci_plat_pm_ops->pwr_domain_on != NULL) &&
  210. (psci_plat_pm_ops->pwr_domain_on_finish != NULL))
  211. psci_caps |= define_psci_cap(PSCI_CPU_ON_AARCH64);
  212. if ((psci_plat_pm_ops->pwr_domain_suspend != NULL) &&
  213. (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL)) {
  214. if (psci_plat_pm_ops->validate_power_state != NULL)
  215. psci_caps |= define_psci_cap(PSCI_CPU_SUSPEND_AARCH64);
  216. if (psci_plat_pm_ops->get_sys_suspend_power_state != NULL)
  217. psci_caps |= define_psci_cap(PSCI_SYSTEM_SUSPEND_AARCH64);
  218. #if PSCI_OS_INIT_MODE
  219. psci_caps |= define_psci_cap(PSCI_SET_SUSPEND_MODE);
  220. #endif
  221. }
  222. if (psci_plat_pm_ops->system_off != NULL)
  223. psci_caps |= define_psci_cap(PSCI_SYSTEM_OFF);
  224. if (psci_plat_pm_ops->system_reset != NULL)
  225. psci_caps |= define_psci_cap(PSCI_SYSTEM_RESET);
  226. if (psci_plat_pm_ops->get_node_hw_state != NULL)
  227. psci_caps |= define_psci_cap(PSCI_NODE_HW_STATE_AARCH64);
  228. if ((psci_plat_pm_ops->read_mem_protect != NULL) &&
  229. (psci_plat_pm_ops->write_mem_protect != NULL))
  230. psci_caps |= define_psci_cap(PSCI_MEM_PROTECT);
  231. if (psci_plat_pm_ops->mem_protect_chk != NULL)
  232. psci_caps |= define_psci_cap(PSCI_MEM_CHK_RANGE_AARCH64);
  233. if (psci_plat_pm_ops->system_reset2 != NULL)
  234. psci_caps |= define_psci_cap(PSCI_SYSTEM_RESET2_AARCH64);
  235. #if ENABLE_PSCI_STAT
  236. psci_caps |= define_psci_cap(PSCI_STAT_RESIDENCY_AARCH64);
  237. psci_caps |= define_psci_cap(PSCI_STAT_COUNT_AARCH64);
  238. #endif
  239. return 0;
  240. }
  241. /*******************************************************************************
  242. * This duplicates what the primary cpu did after a cold boot in BL1. The same
  243. * needs to be done when a cpu is hotplugged in. This function could also over-
  244. * ride any EL3 setup done by BL1 as this code resides in rw memory.
  245. ******************************************************************************/
  246. void psci_arch_setup(void)
  247. {
  248. #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER)
  249. /* Program the counter frequency */
  250. write_cntfrq_el0(plat_get_syscnt_freq2());
  251. #endif
  252. /* Initialize the cpu_ops pointer. */
  253. init_cpu_ops();
  254. /* Having initialized cpu_ops, we can now print errata status */
  255. print_errata_status();
  256. #if ENABLE_PAUTH
  257. /* Store APIAKey_EL1 key */
  258. set_cpu_data(apiakey[0], read_apiakeylo_el1());
  259. set_cpu_data(apiakey[1], read_apiakeyhi_el1());
  260. #endif /* ENABLE_PAUTH */
  261. }
  262. /******************************************************************************
  263. * PSCI Library interface to initialize the cpu context for the next non
  264. * secure image during cold boot. The relevant registers in the cpu context
  265. * need to be retrieved and programmed on return from this interface.
  266. *****************************************************************************/
  267. void psci_prepare_next_non_secure_ctx(entry_point_info_t *next_image_info)
  268. {
  269. assert(GET_SECURITY_STATE(next_image_info->h.attr) == NON_SECURE);
  270. cm_init_my_context(next_image_info);
  271. cm_prepare_el3_exit(NON_SECURE);
  272. }