psci_on.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. unsigned int target_idx = (unsigned int)plat_core_pos_by_mpidr(target_cpu);
  56. /*
  57. * This function must only be called on platforms where the
  58. * CPU_ON platform hooks have been implemented.
  59. */
  60. assert((psci_plat_pm_ops->pwr_domain_on != NULL) &&
  61. (psci_plat_pm_ops->pwr_domain_on_finish != NULL));
  62. /* Protect against multiple CPUs trying to turn ON the same target CPU */
  63. psci_spin_lock_cpu(target_idx);
  64. /*
  65. * Generic management: Ensure that the cpu is off to be
  66. * turned on.
  67. * Perform cache maintanence ahead of reading the target CPU state to
  68. * ensure that the data is not stale.
  69. * There is a theoretical edge case where the cache may contain stale
  70. * data for the target CPU data - this can occur under the following
  71. * conditions:
  72. * - the target CPU is in another cluster from the current
  73. * - the target CPU was the last CPU to shutdown on its cluster
  74. * - the cluster was removed from coherency as part of the CPU shutdown
  75. *
  76. * In this case the cache maintenace that was performed as part of the
  77. * target CPUs shutdown was not seen by the current CPU's cluster. And
  78. * so the cache may contain stale data for the target CPU.
  79. */
  80. flush_cpu_data_by_index(target_idx,
  81. psci_svc_cpu_data.aff_info_state);
  82. rc = cpu_on_validate_state(psci_get_aff_info_state_by_idx(target_idx));
  83. if (rc != PSCI_E_SUCCESS)
  84. goto exit;
  85. /*
  86. * Call the cpu on handler registered by the Secure Payload Dispatcher
  87. * to let it do any bookeeping. If the handler encounters an error, it's
  88. * expected to assert within
  89. */
  90. if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_on != NULL))
  91. psci_spd_pm->svc_on(target_cpu);
  92. /*
  93. * Set the Affinity info state of the target cpu to ON_PENDING.
  94. * Flush aff_info_state as it will be accessed with caches
  95. * turned OFF.
  96. */
  97. psci_set_aff_info_state_by_idx(target_idx, AFF_STATE_ON_PENDING);
  98. flush_cpu_data_by_index(target_idx,
  99. psci_svc_cpu_data.aff_info_state);
  100. /*
  101. * The cache line invalidation by the target CPU after setting the
  102. * state to OFF (see psci_do_cpu_off()), could cause the update to
  103. * aff_info_state to be invalidated. Retry the update if the target
  104. * CPU aff_info_state is not ON_PENDING.
  105. */
  106. target_aff_state = psci_get_aff_info_state_by_idx(target_idx);
  107. if (target_aff_state != AFF_STATE_ON_PENDING) {
  108. assert(target_aff_state == AFF_STATE_OFF);
  109. psci_set_aff_info_state_by_idx(target_idx, AFF_STATE_ON_PENDING);
  110. flush_cpu_data_by_index(target_idx,
  111. psci_svc_cpu_data.aff_info_state);
  112. assert(psci_get_aff_info_state_by_idx(target_idx) ==
  113. AFF_STATE_ON_PENDING);
  114. }
  115. /*
  116. * Perform generic, architecture and platform specific handling.
  117. */
  118. /*
  119. * Plat. management: Give the platform the current state
  120. * of the target cpu to allow it to perform the necessary
  121. * steps to power on.
  122. */
  123. rc = psci_plat_pm_ops->pwr_domain_on(target_cpu);
  124. assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
  125. if (rc == PSCI_E_SUCCESS)
  126. /* Store the re-entry information for the non-secure world. */
  127. cm_init_context_by_index(target_idx, ep);
  128. else {
  129. /* Restore the state on error. */
  130. psci_set_aff_info_state_by_idx(target_idx, AFF_STATE_OFF);
  131. flush_cpu_data_by_index(target_idx,
  132. psci_svc_cpu_data.aff_info_state);
  133. }
  134. exit:
  135. psci_spin_unlock_cpu(target_idx);
  136. return rc;
  137. }
  138. /*******************************************************************************
  139. * The following function finish an earlier power on request. They
  140. * are called by the common finisher routine in psci_common.c. The `state_info`
  141. * is the psci_power_state from which this CPU has woken up from.
  142. ******************************************************************************/
  143. void psci_cpu_on_finish(unsigned int cpu_idx, const psci_power_state_t *state_info)
  144. {
  145. /*
  146. * Plat. management: Perform the platform specific actions
  147. * for this cpu e.g. enabling the gic or zeroing the mailbox
  148. * register. The actual state of this cpu has already been
  149. * changed.
  150. */
  151. psci_plat_pm_ops->pwr_domain_on_finish(state_info);
  152. #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  153. /*
  154. * Arch. management: Enable data cache and manage stack memory
  155. */
  156. psci_do_pwrup_cache_maintenance();
  157. #endif
  158. /*
  159. * Plat. management: Perform any platform specific actions which
  160. * can only be done with the cpu and the cluster guaranteed to
  161. * be coherent.
  162. */
  163. if (psci_plat_pm_ops->pwr_domain_on_finish_late != NULL)
  164. psci_plat_pm_ops->pwr_domain_on_finish_late(state_info);
  165. /*
  166. * All the platform specific actions for turning this cpu
  167. * on have completed. Perform enough arch.initialization
  168. * to run in the non-secure address space.
  169. */
  170. psci_arch_setup();
  171. /*
  172. * Lock the CPU spin lock to make sure that the context initialization
  173. * is done. Since the lock is only used in this function to create
  174. * a synchronization point with cpu_on_start(), it can be released
  175. * immediately.
  176. */
  177. psci_spin_lock_cpu(cpu_idx);
  178. psci_spin_unlock_cpu(cpu_idx);
  179. /* Ensure we have been explicitly woken up by another cpu */
  180. assert(psci_get_aff_info_state() == AFF_STATE_ON_PENDING);
  181. /*
  182. * Call the cpu on finish handler registered by the Secure Payload
  183. * Dispatcher to let it do any bookeeping. If the handler encounters an
  184. * error, it's expected to assert within
  185. */
  186. if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_on_finish != NULL))
  187. psci_spd_pm->svc_on_finish(0);
  188. PUBLISH_EVENT(psci_cpu_on_finish);
  189. /* Populate the mpidr field within the cpu node array */
  190. /* This needs to be done only once */
  191. psci_cpu_pd_nodes[cpu_idx].mpidr = read_mpidr() & MPIDR_AFFINITY_MASK;
  192. }