psci_on.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2013-2022, 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 <common/debug.h>
  12. #include <lib/el3_runtime/context_mgmt.h>
  13. #include <lib/el3_runtime/pubsub_events.h>
  14. #include <plat/common/platform.h>
  15. #include "psci_private.h"
  16. /*
  17. * Helper functions for the CPU level spinlocks
  18. */
  19. static inline void psci_spin_lock_cpu(unsigned int idx)
  20. {
  21. spin_lock(&psci_cpu_pd_nodes[idx].cpu_lock);
  22. }
  23. static inline void psci_spin_unlock_cpu(unsigned int idx)
  24. {
  25. spin_unlock(&psci_cpu_pd_nodes[idx].cpu_lock);
  26. }
  27. /*******************************************************************************
  28. * This function checks whether a cpu which has been requested to be turned on
  29. * is OFF to begin with.
  30. ******************************************************************************/
  31. static int cpu_on_validate_state(aff_info_state_t aff_state)
  32. {
  33. if (aff_state == AFF_STATE_ON)
  34. return PSCI_E_ALREADY_ON;
  35. if (aff_state == AFF_STATE_ON_PENDING)
  36. return PSCI_E_ON_PENDING;
  37. assert(aff_state == AFF_STATE_OFF);
  38. return PSCI_E_SUCCESS;
  39. }
  40. /*******************************************************************************
  41. * Generic handler which is called to physically power on a cpu identified by
  42. * its mpidr. It performs the generic, architectural, platform setup and state
  43. * management to power on the target cpu e.g. it will ensure that
  44. * enough information is stashed for it to resume execution in the non-secure
  45. * security state.
  46. *
  47. * The state of all the relevant power domains are changed after calling the
  48. * platform handler as it can return error.
  49. ******************************************************************************/
  50. int psci_cpu_on_start(u_register_t target_cpu,
  51. const entry_point_info_t *ep)
  52. {
  53. int rc;
  54. aff_info_state_t target_aff_state;
  55. int ret = plat_core_pos_by_mpidr(target_cpu);
  56. unsigned int target_idx;
  57. /* Calling function must supply valid input arguments */
  58. assert(ret >= 0);
  59. assert((unsigned int)ret < PLATFORM_CORE_COUNT);
  60. assert(ep != NULL);
  61. target_idx = (unsigned int)ret;
  62. /*
  63. * This function must only be called on platforms where the
  64. * CPU_ON platform hooks have been implemented.
  65. */
  66. assert((psci_plat_pm_ops->pwr_domain_on != NULL) &&
  67. (psci_plat_pm_ops->pwr_domain_on_finish != NULL));
  68. /* Protect against multiple CPUs trying to turn ON the same target CPU */
  69. psci_spin_lock_cpu(target_idx);
  70. /*
  71. * Generic management: Ensure that the cpu is off to be
  72. * turned on.
  73. * Perform cache maintanence ahead of reading the target CPU state to
  74. * ensure that the data is not stale.
  75. * There is a theoretical edge case where the cache may contain stale
  76. * data for the target CPU data - this can occur under the following
  77. * conditions:
  78. * - the target CPU is in another cluster from the current
  79. * - the target CPU was the last CPU to shutdown on its cluster
  80. * - the cluster was removed from coherency as part of the CPU shutdown
  81. *
  82. * In this case the cache maintenace that was performed as part of the
  83. * target CPUs shutdown was not seen by the current CPU's cluster. And
  84. * so the cache may contain stale data for the target CPU.
  85. */
  86. flush_cpu_data_by_index(target_idx,
  87. psci_svc_cpu_data.aff_info_state);
  88. rc = cpu_on_validate_state(psci_get_aff_info_state_by_idx(target_idx));
  89. if (rc != PSCI_E_SUCCESS)
  90. goto exit;
  91. /*
  92. * Call the cpu on handler registered by the Secure Payload Dispatcher
  93. * to let it do any bookeeping. If the handler encounters an error, it's
  94. * expected to assert within
  95. */
  96. if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_on != NULL))
  97. psci_spd_pm->svc_on(target_cpu);
  98. /*
  99. * Set the Affinity info state of the target cpu to ON_PENDING.
  100. * Flush aff_info_state as it will be accessed with caches
  101. * turned OFF.
  102. */
  103. psci_set_aff_info_state_by_idx(target_idx, AFF_STATE_ON_PENDING);
  104. flush_cpu_data_by_index(target_idx,
  105. psci_svc_cpu_data.aff_info_state);
  106. /*
  107. * The cache line invalidation by the target CPU after setting the
  108. * state to OFF (see psci_do_cpu_off()), could cause the update to
  109. * aff_info_state to be invalidated. Retry the update if the target
  110. * CPU aff_info_state is not ON_PENDING.
  111. */
  112. target_aff_state = psci_get_aff_info_state_by_idx(target_idx);
  113. if (target_aff_state != AFF_STATE_ON_PENDING) {
  114. assert(target_aff_state == AFF_STATE_OFF);
  115. psci_set_aff_info_state_by_idx(target_idx, AFF_STATE_ON_PENDING);
  116. flush_cpu_data_by_index(target_idx,
  117. psci_svc_cpu_data.aff_info_state);
  118. assert(psci_get_aff_info_state_by_idx(target_idx) ==
  119. AFF_STATE_ON_PENDING);
  120. }
  121. /*
  122. * Perform generic, architecture and platform specific handling.
  123. */
  124. /*
  125. * Plat. management: Give the platform the current state
  126. * of the target cpu to allow it to perform the necessary
  127. * steps to power on.
  128. */
  129. rc = psci_plat_pm_ops->pwr_domain_on(target_cpu);
  130. assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
  131. if (rc == PSCI_E_SUCCESS)
  132. /* Store the re-entry information for the non-secure world. */
  133. cm_init_context_by_index(target_idx, ep);
  134. else {
  135. /* Restore the state on error. */
  136. psci_set_aff_info_state_by_idx(target_idx, AFF_STATE_OFF);
  137. flush_cpu_data_by_index(target_idx,
  138. psci_svc_cpu_data.aff_info_state);
  139. }
  140. exit:
  141. psci_spin_unlock_cpu(target_idx);
  142. return rc;
  143. }
  144. /*******************************************************************************
  145. * The following function finish an earlier power on request. They
  146. * are called by the common finisher routine in psci_common.c. The `state_info`
  147. * is the psci_power_state from which this CPU has woken up from.
  148. ******************************************************************************/
  149. void psci_cpu_on_finish(unsigned int cpu_idx, const psci_power_state_t *state_info)
  150. {
  151. /*
  152. * Plat. management: Perform the platform specific actions
  153. * for this cpu e.g. enabling the gic or zeroing the mailbox
  154. * register. The actual state of this cpu has already been
  155. * changed.
  156. */
  157. psci_plat_pm_ops->pwr_domain_on_finish(state_info);
  158. #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  159. /*
  160. * Arch. management: Enable data cache and manage stack memory
  161. */
  162. psci_do_pwrup_cache_maintenance();
  163. #endif
  164. /*
  165. * Plat. management: Perform any platform specific actions which
  166. * can only be done with the cpu and the cluster guaranteed to
  167. * be coherent.
  168. */
  169. if (psci_plat_pm_ops->pwr_domain_on_finish_late != NULL)
  170. psci_plat_pm_ops->pwr_domain_on_finish_late(state_info);
  171. /*
  172. * All the platform specific actions for turning this cpu
  173. * on have completed. Perform enough arch.initialization
  174. * to run in the non-secure address space.
  175. */
  176. psci_arch_setup();
  177. /*
  178. * Lock the CPU spin lock to make sure that the context initialization
  179. * is done. Since the lock is only used in this function to create
  180. * a synchronization point with cpu_on_start(), it can be released
  181. * immediately.
  182. */
  183. psci_spin_lock_cpu(cpu_idx);
  184. psci_spin_unlock_cpu(cpu_idx);
  185. /* Ensure we have been explicitly woken up by another cpu */
  186. assert(psci_get_aff_info_state() == AFF_STATE_ON_PENDING);
  187. /*
  188. * Call the cpu on finish handler registered by the Secure Payload
  189. * Dispatcher to let it do any bookeeping. If the handler encounters an
  190. * error, it's expected to assert within
  191. */
  192. if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_on_finish != NULL))
  193. psci_spd_pm->svc_on_finish(0);
  194. PUBLISH_EVENT(psci_cpu_on_finish);
  195. /* Populate the mpidr field within the cpu node array */
  196. /* This needs to be done only once */
  197. psci_cpu_pd_nodes[cpu_idx].mpidr = read_mpidr() & MPIDR_AFFINITY_MASK;
  198. }