psci_common.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /*
  2. * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <arch.h>
  9. #include <arch_features.h>
  10. #include <arch_helpers.h>
  11. #include <common/bl_common.h>
  12. #include <common/debug.h>
  13. #include <context.h>
  14. #include <drivers/delay_timer.h>
  15. #include <lib/el3_runtime/context_mgmt.h>
  16. #include <lib/extensions/spe.h>
  17. #include <lib/utils.h>
  18. #include <plat/common/platform.h>
  19. #include "psci_private.h"
  20. /*
  21. * SPD power management operations, expected to be supplied by the registered
  22. * SPD on successful SP initialization
  23. */
  24. const spd_pm_ops_t *psci_spd_pm;
  25. /*
  26. * PSCI requested local power state map. This array is used to store the local
  27. * power states requested by a CPU for power levels from level 1 to
  28. * PLAT_MAX_PWR_LVL. It does not store the requested local power state for power
  29. * level 0 (PSCI_CPU_PWR_LVL) as the requested and the target power state for a
  30. * CPU are the same.
  31. *
  32. * During state coordination, the platform is passed an array containing the
  33. * local states requested for a particular non cpu power domain by each cpu
  34. * within the domain.
  35. *
  36. * TODO: Dense packing of the requested states will cause cache thrashing
  37. * when multiple power domains write to it. If we allocate the requested
  38. * states at each power level in a cache-line aligned per-domain memory,
  39. * the cache thrashing can be avoided.
  40. */
  41. static plat_local_state_t
  42. psci_req_local_pwr_states[PLAT_MAX_PWR_LVL][PLATFORM_CORE_COUNT];
  43. unsigned int psci_plat_core_count;
  44. /*******************************************************************************
  45. * Arrays that hold the platform's power domain tree information for state
  46. * management of power domains.
  47. * Each node in the array 'psci_non_cpu_pd_nodes' corresponds to a power domain
  48. * which is an ancestor of a CPU power domain.
  49. * Each node in the array 'psci_cpu_pd_nodes' corresponds to a cpu power domain
  50. ******************************************************************************/
  51. non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS]
  52. #if USE_COHERENT_MEM
  53. __section(".tzfw_coherent_mem")
  54. #endif
  55. ;
  56. /* Lock for PSCI state coordination */
  57. DEFINE_PSCI_LOCK(psci_locks[PSCI_NUM_NON_CPU_PWR_DOMAINS]);
  58. cpu_pd_node_t psci_cpu_pd_nodes[PLATFORM_CORE_COUNT];
  59. /*******************************************************************************
  60. * Pointer to functions exported by the platform to complete power mgmt. ops
  61. ******************************************************************************/
  62. const plat_psci_ops_t *psci_plat_pm_ops;
  63. /******************************************************************************
  64. * Check that the maximum power level supported by the platform makes sense
  65. *****************************************************************************/
  66. CASSERT((PLAT_MAX_PWR_LVL <= PSCI_MAX_PWR_LVL) &&
  67. (PLAT_MAX_PWR_LVL >= PSCI_CPU_PWR_LVL),
  68. assert_platform_max_pwrlvl_check);
  69. #if PSCI_OS_INIT_MODE
  70. /*******************************************************************************
  71. * The power state coordination mode used in CPU_SUSPEND.
  72. * Defaults to platform-coordinated mode.
  73. ******************************************************************************/
  74. suspend_mode_t psci_suspend_mode = PLAT_COORD;
  75. #endif
  76. /*
  77. * The plat_local_state used by the platform is one of these types: RUN,
  78. * RETENTION and OFF. The platform can define further sub-states for each type
  79. * apart from RUN. This categorization is done to verify the sanity of the
  80. * psci_power_state passed by the platform and to print debug information. The
  81. * categorization is done on the basis of the following conditions:
  82. *
  83. * 1. If (plat_local_state == 0) then the category is STATE_TYPE_RUN.
  84. *
  85. * 2. If (0 < plat_local_state <= PLAT_MAX_RET_STATE), then the category is
  86. * STATE_TYPE_RETN.
  87. *
  88. * 3. If (plat_local_state > PLAT_MAX_RET_STATE), then the category is
  89. * STATE_TYPE_OFF.
  90. */
  91. typedef enum plat_local_state_type {
  92. STATE_TYPE_RUN = 0,
  93. STATE_TYPE_RETN,
  94. STATE_TYPE_OFF
  95. } plat_local_state_type_t;
  96. /* Function used to categorize plat_local_state. */
  97. static plat_local_state_type_t find_local_state_type(plat_local_state_t state)
  98. {
  99. if (state != 0U) {
  100. if (state > PLAT_MAX_RET_STATE) {
  101. return STATE_TYPE_OFF;
  102. } else {
  103. return STATE_TYPE_RETN;
  104. }
  105. } else {
  106. return STATE_TYPE_RUN;
  107. }
  108. }
  109. /******************************************************************************
  110. * Check that the maximum retention level supported by the platform is less
  111. * than the maximum off level.
  112. *****************************************************************************/
  113. CASSERT(PLAT_MAX_RET_STATE < PLAT_MAX_OFF_STATE,
  114. assert_platform_max_off_and_retn_state_check);
  115. /******************************************************************************
  116. * This function ensures that the power state parameter in a CPU_SUSPEND request
  117. * is valid. If so, it returns the requested states for each power level.
  118. *****************************************************************************/
  119. int psci_validate_power_state(unsigned int power_state,
  120. psci_power_state_t *state_info)
  121. {
  122. /* Check SBZ bits in power state are zero */
  123. if (psci_check_power_state(power_state) != 0U)
  124. return PSCI_E_INVALID_PARAMS;
  125. assert(psci_plat_pm_ops->validate_power_state != NULL);
  126. /* Validate the power_state using platform pm_ops */
  127. return psci_plat_pm_ops->validate_power_state(power_state, state_info);
  128. }
  129. /******************************************************************************
  130. * This function retrieves the `psci_power_state_t` for system suspend from
  131. * the platform.
  132. *****************************************************************************/
  133. void psci_query_sys_suspend_pwrstate(psci_power_state_t *state_info)
  134. {
  135. /*
  136. * Assert that the required pm_ops hook is implemented to ensure that
  137. * the capability detected during psci_setup() is valid.
  138. */
  139. assert(psci_plat_pm_ops->get_sys_suspend_power_state != NULL);
  140. /*
  141. * Query the platform for the power_state required for system suspend
  142. */
  143. psci_plat_pm_ops->get_sys_suspend_power_state(state_info);
  144. }
  145. #if PSCI_OS_INIT_MODE
  146. /*******************************************************************************
  147. * This function verifies that all the other cores at the 'end_pwrlvl' have been
  148. * idled and the current CPU is the last running CPU at the 'end_pwrlvl'.
  149. * Returns 1 (true) if the current CPU is the last ON CPU or 0 (false)
  150. * otherwise.
  151. ******************************************************************************/
  152. static bool psci_is_last_cpu_to_idle_at_pwrlvl(unsigned int end_pwrlvl)
  153. {
  154. unsigned int my_idx, lvl;
  155. unsigned int parent_idx = 0;
  156. unsigned int cpu_start_idx, ncpus, cpu_idx;
  157. plat_local_state_t local_state;
  158. if (end_pwrlvl == PSCI_CPU_PWR_LVL) {
  159. return true;
  160. }
  161. my_idx = plat_my_core_pos();
  162. parent_idx = psci_cpu_pd_nodes[my_idx].parent_node;
  163. for (lvl = PSCI_CPU_PWR_LVL + U(1); lvl < end_pwrlvl; lvl++) {
  164. parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
  165. }
  166. cpu_start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
  167. ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
  168. for (cpu_idx = cpu_start_idx; cpu_idx < cpu_start_idx + ncpus;
  169. cpu_idx++) {
  170. local_state = psci_get_cpu_local_state_by_idx(cpu_idx);
  171. if (cpu_idx == my_idx) {
  172. assert(is_local_state_run(local_state) != 0);
  173. continue;
  174. }
  175. if (is_local_state_run(local_state) != 0) {
  176. return false;
  177. }
  178. }
  179. return true;
  180. }
  181. #endif
  182. /*******************************************************************************
  183. * This function verifies that all the other cores in the system have been
  184. * turned OFF and the current CPU is the last running CPU in the system.
  185. * Returns true, if the current CPU is the last ON CPU or false otherwise.
  186. ******************************************************************************/
  187. bool psci_is_last_on_cpu(void)
  188. {
  189. unsigned int cpu_idx, my_idx = plat_my_core_pos();
  190. for (cpu_idx = 0; cpu_idx < psci_plat_core_count; cpu_idx++) {
  191. if (cpu_idx == my_idx) {
  192. assert(psci_get_aff_info_state() == AFF_STATE_ON);
  193. continue;
  194. }
  195. if (psci_get_aff_info_state_by_idx(cpu_idx) != AFF_STATE_OFF) {
  196. VERBOSE("core=%u other than current core=%u %s\n",
  197. cpu_idx, my_idx, "running in the system");
  198. return false;
  199. }
  200. }
  201. return true;
  202. }
  203. /*******************************************************************************
  204. * This function verifies that all cores in the system have been turned ON.
  205. * Returns true, if all CPUs are ON or false otherwise.
  206. ******************************************************************************/
  207. static bool psci_are_all_cpus_on(void)
  208. {
  209. unsigned int cpu_idx;
  210. for (cpu_idx = 0; cpu_idx < psci_plat_core_count; cpu_idx++) {
  211. if (psci_get_aff_info_state_by_idx(cpu_idx) == AFF_STATE_OFF) {
  212. return false;
  213. }
  214. }
  215. return true;
  216. }
  217. /*******************************************************************************
  218. * Routine to return the maximum power level to traverse to after a cpu has
  219. * been physically powered up. It is expected to be called immediately after
  220. * reset from assembler code.
  221. ******************************************************************************/
  222. static unsigned int get_power_on_target_pwrlvl(void)
  223. {
  224. unsigned int pwrlvl;
  225. /*
  226. * Assume that this cpu was suspended and retrieve its target power
  227. * level. If it is invalid then it could only have been turned off
  228. * earlier. PLAT_MAX_PWR_LVL will be the highest power level a
  229. * cpu can be turned off to.
  230. */
  231. pwrlvl = psci_get_suspend_pwrlvl();
  232. if (pwrlvl == PSCI_INVALID_PWR_LVL)
  233. pwrlvl = PLAT_MAX_PWR_LVL;
  234. assert(pwrlvl < PSCI_INVALID_PWR_LVL);
  235. return pwrlvl;
  236. }
  237. /******************************************************************************
  238. * Helper function to update the requested local power state array. This array
  239. * does not store the requested state for the CPU power level. Hence an
  240. * assertion is added to prevent us from accessing the CPU power level.
  241. *****************************************************************************/
  242. static void psci_set_req_local_pwr_state(unsigned int pwrlvl,
  243. unsigned int cpu_idx,
  244. plat_local_state_t req_pwr_state)
  245. {
  246. assert(pwrlvl > PSCI_CPU_PWR_LVL);
  247. if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) &&
  248. (cpu_idx < psci_plat_core_count)) {
  249. psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx] = req_pwr_state;
  250. }
  251. }
  252. /******************************************************************************
  253. * This function initializes the psci_req_local_pwr_states.
  254. *****************************************************************************/
  255. void __init psci_init_req_local_pwr_states(void)
  256. {
  257. /* Initialize the requested state of all non CPU power domains as OFF */
  258. unsigned int pwrlvl;
  259. unsigned int core;
  260. for (pwrlvl = 0U; pwrlvl < PLAT_MAX_PWR_LVL; pwrlvl++) {
  261. for (core = 0; core < psci_plat_core_count; core++) {
  262. psci_req_local_pwr_states[pwrlvl][core] =
  263. PLAT_MAX_OFF_STATE;
  264. }
  265. }
  266. }
  267. /******************************************************************************
  268. * Helper function to return a reference to an array containing the local power
  269. * states requested by each cpu for a power domain at 'pwrlvl'. The size of the
  270. * array will be the number of cpu power domains of which this power domain is
  271. * an ancestor. These requested states will be used to determine a suitable
  272. * target state for this power domain during psci state coordination. An
  273. * assertion is added to prevent us from accessing the CPU power level.
  274. *****************************************************************************/
  275. static plat_local_state_t *psci_get_req_local_pwr_states(unsigned int pwrlvl,
  276. unsigned int cpu_idx)
  277. {
  278. assert(pwrlvl > PSCI_CPU_PWR_LVL);
  279. if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) &&
  280. (cpu_idx < psci_plat_core_count)) {
  281. return &psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx];
  282. } else
  283. return NULL;
  284. }
  285. #if PSCI_OS_INIT_MODE
  286. /******************************************************************************
  287. * Helper function to save a copy of the psci_req_local_pwr_states (prev) for a
  288. * CPU (cpu_idx), and update psci_req_local_pwr_states with the new requested
  289. * local power states (state_info).
  290. *****************************************************************************/
  291. void psci_update_req_local_pwr_states(unsigned int end_pwrlvl,
  292. unsigned int cpu_idx,
  293. psci_power_state_t *state_info,
  294. plat_local_state_t *prev)
  295. {
  296. unsigned int lvl;
  297. #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL
  298. unsigned int max_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL;
  299. #else
  300. unsigned int max_pwrlvl = PLAT_MAX_PWR_LVL;
  301. #endif
  302. plat_local_state_t req_state;
  303. for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= max_pwrlvl; lvl++) {
  304. /* Save the previous requested local power state */
  305. prev[lvl - 1U] = *psci_get_req_local_pwr_states(lvl, cpu_idx);
  306. /* Update the new requested local power state */
  307. if (lvl <= end_pwrlvl) {
  308. req_state = state_info->pwr_domain_state[lvl];
  309. } else {
  310. req_state = state_info->pwr_domain_state[end_pwrlvl];
  311. }
  312. psci_set_req_local_pwr_state(lvl, cpu_idx, req_state);
  313. }
  314. }
  315. /******************************************************************************
  316. * Helper function to restore the previously saved requested local power states
  317. * (prev) for a CPU (cpu_idx) to psci_req_local_pwr_states.
  318. *****************************************************************************/
  319. void psci_restore_req_local_pwr_states(unsigned int cpu_idx,
  320. plat_local_state_t *prev)
  321. {
  322. unsigned int lvl;
  323. #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL
  324. unsigned int max_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL;
  325. #else
  326. unsigned int max_pwrlvl = PLAT_MAX_PWR_LVL;
  327. #endif
  328. for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= max_pwrlvl; lvl++) {
  329. /* Restore the previous requested local power state */
  330. psci_set_req_local_pwr_state(lvl, cpu_idx, prev[lvl - 1U]);
  331. }
  332. }
  333. #endif
  334. /*
  335. * psci_non_cpu_pd_nodes can be placed either in normal memory or coherent
  336. * memory.
  337. *
  338. * With !USE_COHERENT_MEM, psci_non_cpu_pd_nodes is placed in normal memory,
  339. * it's accessed by both cached and non-cached participants. To serve the common
  340. * minimum, perform a cache flush before read and after write so that non-cached
  341. * participants operate on latest data in main memory.
  342. *
  343. * When USE_COHERENT_MEM is used, psci_non_cpu_pd_nodes is placed in coherent
  344. * memory. With HW_ASSISTED_COHERENCY, all PSCI participants are cache-coherent.
  345. * In both cases, no cache operations are required.
  346. */
  347. /*
  348. * Retrieve local state of non-CPU power domain node from a non-cached CPU,
  349. * after any required cache maintenance operation.
  350. */
  351. static plat_local_state_t get_non_cpu_pd_node_local_state(
  352. unsigned int parent_idx)
  353. {
  354. #if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  355. flush_dcache_range(
  356. (uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
  357. sizeof(psci_non_cpu_pd_nodes[parent_idx]));
  358. #endif
  359. return psci_non_cpu_pd_nodes[parent_idx].local_state;
  360. }
  361. /*
  362. * Update local state of non-CPU power domain node from a cached CPU; perform
  363. * any required cache maintenance operation afterwards.
  364. */
  365. static void set_non_cpu_pd_node_local_state(unsigned int parent_idx,
  366. plat_local_state_t state)
  367. {
  368. psci_non_cpu_pd_nodes[parent_idx].local_state = state;
  369. #if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  370. flush_dcache_range(
  371. (uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
  372. sizeof(psci_non_cpu_pd_nodes[parent_idx]));
  373. #endif
  374. }
  375. /******************************************************************************
  376. * Helper function to return the current local power state of each power domain
  377. * from the current cpu power domain to its ancestor at the 'end_pwrlvl'. This
  378. * function will be called after a cpu is powered on to find the local state
  379. * each power domain has emerged from.
  380. *****************************************************************************/
  381. void psci_get_target_local_pwr_states(unsigned int end_pwrlvl,
  382. psci_power_state_t *target_state)
  383. {
  384. unsigned int parent_idx, lvl;
  385. plat_local_state_t *pd_state = target_state->pwr_domain_state;
  386. pd_state[PSCI_CPU_PWR_LVL] = psci_get_cpu_local_state();
  387. parent_idx = psci_cpu_pd_nodes[plat_my_core_pos()].parent_node;
  388. /* Copy the local power state from node to state_info */
  389. for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
  390. pd_state[lvl] = get_non_cpu_pd_node_local_state(parent_idx);
  391. parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
  392. }
  393. /* Set the the higher levels to RUN */
  394. for (; lvl <= PLAT_MAX_PWR_LVL; lvl++)
  395. target_state->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
  396. }
  397. /******************************************************************************
  398. * Helper function to set the target local power state that each power domain
  399. * from the current cpu power domain to its ancestor at the 'end_pwrlvl' will
  400. * enter. This function will be called after coordination of requested power
  401. * states has been done for each power level.
  402. *****************************************************************************/
  403. void psci_set_target_local_pwr_states(unsigned int end_pwrlvl,
  404. const psci_power_state_t *target_state)
  405. {
  406. unsigned int parent_idx, lvl;
  407. const plat_local_state_t *pd_state = target_state->pwr_domain_state;
  408. psci_set_cpu_local_state(pd_state[PSCI_CPU_PWR_LVL]);
  409. /*
  410. * Need to flush as local_state might be accessed with Data Cache
  411. * disabled during power on
  412. */
  413. psci_flush_cpu_data(psci_svc_cpu_data.local_state);
  414. parent_idx = psci_cpu_pd_nodes[plat_my_core_pos()].parent_node;
  415. /* Copy the local_state from state_info */
  416. for (lvl = 1U; lvl <= end_pwrlvl; lvl++) {
  417. set_non_cpu_pd_node_local_state(parent_idx, pd_state[lvl]);
  418. parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
  419. }
  420. }
  421. /*******************************************************************************
  422. * PSCI helper function to get the parent nodes corresponding to a cpu_index.
  423. ******************************************************************************/
  424. void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx,
  425. unsigned int end_lvl,
  426. unsigned int *node_index)
  427. {
  428. unsigned int parent_node = psci_cpu_pd_nodes[cpu_idx].parent_node;
  429. unsigned int i;
  430. unsigned int *node = node_index;
  431. for (i = PSCI_CPU_PWR_LVL + 1U; i <= end_lvl; i++) {
  432. *node = parent_node;
  433. node++;
  434. parent_node = psci_non_cpu_pd_nodes[parent_node].parent_node;
  435. }
  436. }
  437. /******************************************************************************
  438. * This function is invoked post CPU power up and initialization. It sets the
  439. * affinity info state, target power state and requested power state for the
  440. * current CPU and all its ancestor power domains to RUN.
  441. *****************************************************************************/
  442. void psci_set_pwr_domains_to_run(unsigned int end_pwrlvl)
  443. {
  444. unsigned int parent_idx, cpu_idx = plat_my_core_pos(), lvl;
  445. parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
  446. /* Reset the local_state to RUN for the non cpu power domains. */
  447. for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
  448. set_non_cpu_pd_node_local_state(parent_idx,
  449. PSCI_LOCAL_STATE_RUN);
  450. psci_set_req_local_pwr_state(lvl,
  451. cpu_idx,
  452. PSCI_LOCAL_STATE_RUN);
  453. parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
  454. }
  455. /* Set the affinity info state to ON */
  456. psci_set_aff_info_state(AFF_STATE_ON);
  457. psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
  458. psci_flush_cpu_data(psci_svc_cpu_data);
  459. }
  460. /******************************************************************************
  461. * This function is used in platform-coordinated mode.
  462. *
  463. * This function is passed the local power states requested for each power
  464. * domain (state_info) between the current CPU domain and its ancestors until
  465. * the target power level (end_pwrlvl). It updates the array of requested power
  466. * states with this information.
  467. *
  468. * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
  469. * retrieves the states requested by all the cpus of which the power domain at
  470. * that level is an ancestor. It passes this information to the platform to
  471. * coordinate and return the target power state. If the target state for a level
  472. * is RUN then subsequent levels are not considered. At the CPU level, state
  473. * coordination is not required. Hence, the requested and the target states are
  474. * the same.
  475. *
  476. * The 'state_info' is updated with the target state for each level between the
  477. * CPU and the 'end_pwrlvl' and returned to the caller.
  478. *
  479. * This function will only be invoked with data cache enabled and while
  480. * powering down a core.
  481. *****************************************************************************/
  482. void psci_do_state_coordination(unsigned int end_pwrlvl,
  483. psci_power_state_t *state_info)
  484. {
  485. unsigned int lvl, parent_idx, cpu_idx = plat_my_core_pos();
  486. unsigned int start_idx;
  487. unsigned int ncpus;
  488. plat_local_state_t target_state, *req_states;
  489. assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
  490. parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
  491. /* For level 0, the requested state will be equivalent
  492. to target state */
  493. for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
  494. /* First update the requested power state */
  495. psci_set_req_local_pwr_state(lvl, cpu_idx,
  496. state_info->pwr_domain_state[lvl]);
  497. /* Get the requested power states for this power level */
  498. start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
  499. req_states = psci_get_req_local_pwr_states(lvl, start_idx);
  500. /*
  501. * Let the platform coordinate amongst the requested states at
  502. * this power level and return the target local power state.
  503. */
  504. ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
  505. target_state = plat_get_target_pwr_state(lvl,
  506. req_states,
  507. ncpus);
  508. state_info->pwr_domain_state[lvl] = target_state;
  509. /* Break early if the negotiated target power state is RUN */
  510. if (is_local_state_run(state_info->pwr_domain_state[lvl]) != 0)
  511. break;
  512. parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
  513. }
  514. /*
  515. * This is for cases when we break out of the above loop early because
  516. * the target power state is RUN at a power level < end_pwlvl.
  517. * We update the requested power state from state_info and then
  518. * set the target state as RUN.
  519. */
  520. for (lvl = lvl + 1U; lvl <= end_pwrlvl; lvl++) {
  521. psci_set_req_local_pwr_state(lvl, cpu_idx,
  522. state_info->pwr_domain_state[lvl]);
  523. state_info->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
  524. }
  525. }
  526. #if PSCI_OS_INIT_MODE
  527. /******************************************************************************
  528. * This function is used in OS-initiated mode.
  529. *
  530. * This function is passed the local power states requested for each power
  531. * domain (state_info) between the current CPU domain and its ancestors until
  532. * the target power level (end_pwrlvl), and ensures the requested power states
  533. * are valid. It updates the array of requested power states with this
  534. * information.
  535. *
  536. * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
  537. * retrieves the states requested by all the cpus of which the power domain at
  538. * that level is an ancestor. It passes this information to the platform to
  539. * coordinate and return the target power state. If the requested state does
  540. * not match the target state, the request is denied.
  541. *
  542. * The 'state_info' is not modified.
  543. *
  544. * This function will only be invoked with data cache enabled and while
  545. * powering down a core.
  546. *****************************************************************************/
  547. int psci_validate_state_coordination(unsigned int end_pwrlvl,
  548. psci_power_state_t *state_info)
  549. {
  550. int rc = PSCI_E_SUCCESS;
  551. unsigned int lvl, parent_idx, cpu_idx = plat_my_core_pos();
  552. unsigned int start_idx;
  553. unsigned int ncpus;
  554. plat_local_state_t target_state, *req_states;
  555. plat_local_state_t prev[PLAT_MAX_PWR_LVL];
  556. assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
  557. parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
  558. /*
  559. * Save a copy of the previous requested local power states and update
  560. * the new requested local power states.
  561. */
  562. psci_update_req_local_pwr_states(end_pwrlvl, cpu_idx, state_info, prev);
  563. for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
  564. /* Get the requested power states for this power level */
  565. start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
  566. req_states = psci_get_req_local_pwr_states(lvl, start_idx);
  567. /*
  568. * Let the platform coordinate amongst the requested states at
  569. * this power level and return the target local power state.
  570. */
  571. ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
  572. target_state = plat_get_target_pwr_state(lvl,
  573. req_states,
  574. ncpus);
  575. /*
  576. * Verify that the requested power state matches the target
  577. * local power state.
  578. */
  579. if (state_info->pwr_domain_state[lvl] != target_state) {
  580. if (target_state == PSCI_LOCAL_STATE_RUN) {
  581. rc = PSCI_E_DENIED;
  582. } else {
  583. rc = PSCI_E_INVALID_PARAMS;
  584. }
  585. goto exit;
  586. }
  587. parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
  588. }
  589. /*
  590. * Verify that the current core is the last running core at the
  591. * specified power level.
  592. */
  593. lvl = state_info->last_at_pwrlvl;
  594. if (!psci_is_last_cpu_to_idle_at_pwrlvl(lvl)) {
  595. rc = PSCI_E_DENIED;
  596. }
  597. exit:
  598. if (rc != PSCI_E_SUCCESS) {
  599. /* Restore the previous requested local power states. */
  600. psci_restore_req_local_pwr_states(cpu_idx, prev);
  601. return rc;
  602. }
  603. return rc;
  604. }
  605. #endif
  606. /******************************************************************************
  607. * This function validates a suspend request by making sure that if a standby
  608. * state is requested then no power level is turned off and the highest power
  609. * level is placed in a standby/retention state.
  610. *
  611. * It also ensures that the state level X will enter is not shallower than the
  612. * state level X + 1 will enter.
  613. *
  614. * This validation will be enabled only for DEBUG builds as the platform is
  615. * expected to perform these validations as well.
  616. *****************************************************************************/
  617. int psci_validate_suspend_req(const psci_power_state_t *state_info,
  618. unsigned int is_power_down_state)
  619. {
  620. unsigned int max_off_lvl, target_lvl, max_retn_lvl;
  621. plat_local_state_t state;
  622. plat_local_state_type_t req_state_type, deepest_state_type;
  623. int i;
  624. /* Find the target suspend power level */
  625. target_lvl = psci_find_target_suspend_lvl(state_info);
  626. if (target_lvl == PSCI_INVALID_PWR_LVL)
  627. return PSCI_E_INVALID_PARAMS;
  628. /* All power domain levels are in a RUN state to begin with */
  629. deepest_state_type = STATE_TYPE_RUN;
  630. for (i = (int) target_lvl; i >= (int) PSCI_CPU_PWR_LVL; i--) {
  631. state = state_info->pwr_domain_state[i];
  632. req_state_type = find_local_state_type(state);
  633. /*
  634. * While traversing from the highest power level to the lowest,
  635. * the state requested for lower levels has to be the same or
  636. * deeper i.e. equal to or greater than the state at the higher
  637. * levels. If this condition is true, then the requested state
  638. * becomes the deepest state encountered so far.
  639. */
  640. if (req_state_type < deepest_state_type)
  641. return PSCI_E_INVALID_PARAMS;
  642. deepest_state_type = req_state_type;
  643. }
  644. /* Find the highest off power level */
  645. max_off_lvl = psci_find_max_off_lvl(state_info);
  646. /* The target_lvl is either equal to the max_off_lvl or max_retn_lvl */
  647. max_retn_lvl = PSCI_INVALID_PWR_LVL;
  648. if (target_lvl != max_off_lvl)
  649. max_retn_lvl = target_lvl;
  650. /*
  651. * If this is not a request for a power down state then max off level
  652. * has to be invalid and max retention level has to be a valid power
  653. * level.
  654. */
  655. if ((is_power_down_state == 0U) &&
  656. ((max_off_lvl != PSCI_INVALID_PWR_LVL) ||
  657. (max_retn_lvl == PSCI_INVALID_PWR_LVL)))
  658. return PSCI_E_INVALID_PARAMS;
  659. return PSCI_E_SUCCESS;
  660. }
  661. /******************************************************************************
  662. * This function finds the highest power level which will be powered down
  663. * amongst all the power levels specified in the 'state_info' structure
  664. *****************************************************************************/
  665. unsigned int psci_find_max_off_lvl(const psci_power_state_t *state_info)
  666. {
  667. int i;
  668. for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) {
  669. if (is_local_state_off(state_info->pwr_domain_state[i]) != 0)
  670. return (unsigned int) i;
  671. }
  672. return PSCI_INVALID_PWR_LVL;
  673. }
  674. /******************************************************************************
  675. * This functions finds the level of the highest power domain which will be
  676. * placed in a low power state during a suspend operation.
  677. *****************************************************************************/
  678. unsigned int psci_find_target_suspend_lvl(const psci_power_state_t *state_info)
  679. {
  680. int i;
  681. for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) {
  682. if (is_local_state_run(state_info->pwr_domain_state[i]) == 0)
  683. return (unsigned int) i;
  684. }
  685. return PSCI_INVALID_PWR_LVL;
  686. }
  687. /*******************************************************************************
  688. * This function is passed the highest level in the topology tree that the
  689. * operation should be applied to and a list of node indexes. It picks up locks
  690. * from the node index list in order of increasing power domain level in the
  691. * range specified.
  692. ******************************************************************************/
  693. void psci_acquire_pwr_domain_locks(unsigned int end_pwrlvl,
  694. const unsigned int *parent_nodes)
  695. {
  696. unsigned int parent_idx;
  697. unsigned int level;
  698. /* No locking required for level 0. Hence start locking from level 1 */
  699. for (level = PSCI_CPU_PWR_LVL + 1U; level <= end_pwrlvl; level++) {
  700. parent_idx = parent_nodes[level - 1U];
  701. psci_lock_get(&psci_non_cpu_pd_nodes[parent_idx]);
  702. }
  703. }
  704. /*******************************************************************************
  705. * This function is passed the highest level in the topology tree that the
  706. * operation should be applied to and a list of node indexes. It releases the
  707. * locks in order of decreasing power domain level in the range specified.
  708. ******************************************************************************/
  709. void psci_release_pwr_domain_locks(unsigned int end_pwrlvl,
  710. const unsigned int *parent_nodes)
  711. {
  712. unsigned int parent_idx;
  713. unsigned int level;
  714. /* Unlock top down. No unlocking required for level 0. */
  715. for (level = end_pwrlvl; level >= (PSCI_CPU_PWR_LVL + 1U); level--) {
  716. parent_idx = parent_nodes[level - 1U];
  717. psci_lock_release(&psci_non_cpu_pd_nodes[parent_idx]);
  718. }
  719. }
  720. /*******************************************************************************
  721. * This function determines the full entrypoint information for the requested
  722. * PSCI entrypoint on power on/resume and returns it.
  723. ******************************************************************************/
  724. #ifdef __aarch64__
  725. static int psci_get_ns_ep_info(entry_point_info_t *ep,
  726. uintptr_t entrypoint,
  727. u_register_t context_id)
  728. {
  729. u_register_t ep_attr, sctlr;
  730. unsigned int daif, ee, mode;
  731. u_register_t ns_scr_el3 = read_scr_el3();
  732. u_register_t ns_sctlr_el1 = read_sctlr_el1();
  733. sctlr = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ?
  734. read_sctlr_el2() : ns_sctlr_el1;
  735. ee = 0;
  736. ep_attr = NON_SECURE | EP_ST_DISABLE;
  737. if ((sctlr & SCTLR_EE_BIT) != 0U) {
  738. ep_attr |= EP_EE_BIG;
  739. ee = 1;
  740. }
  741. SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
  742. ep->pc = entrypoint;
  743. zeromem(&ep->args, sizeof(ep->args));
  744. ep->args.arg0 = context_id;
  745. /*
  746. * Figure out whether the cpu enters the non-secure address space
  747. * in aarch32 or aarch64
  748. */
  749. if ((ns_scr_el3 & SCR_RW_BIT) != 0U) {
  750. /*
  751. * Check whether a Thumb entry point has been provided for an
  752. * aarch64 EL
  753. */
  754. if ((entrypoint & 0x1UL) != 0UL)
  755. return PSCI_E_INVALID_ADDRESS;
  756. mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ? MODE_EL2 : MODE_EL1;
  757. ep->spsr = SPSR_64((uint64_t)mode, MODE_SP_ELX,
  758. DISABLE_ALL_EXCEPTIONS);
  759. } else {
  760. mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ?
  761. MODE32_hyp : MODE32_svc;
  762. /*
  763. * TODO: Choose async. exception bits if HYP mode is not
  764. * implemented according to the values of SCR.{AW, FW} bits
  765. */
  766. daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT;
  767. ep->spsr = SPSR_MODE32((uint64_t)mode, entrypoint & 0x1, ee,
  768. daif);
  769. }
  770. return PSCI_E_SUCCESS;
  771. }
  772. #else /* !__aarch64__ */
  773. static int psci_get_ns_ep_info(entry_point_info_t *ep,
  774. uintptr_t entrypoint,
  775. u_register_t context_id)
  776. {
  777. u_register_t ep_attr;
  778. unsigned int aif, ee, mode;
  779. u_register_t scr = read_scr();
  780. u_register_t ns_sctlr, sctlr;
  781. /* Switch to non secure state */
  782. write_scr(scr | SCR_NS_BIT);
  783. isb();
  784. ns_sctlr = read_sctlr();
  785. sctlr = scr & SCR_HCE_BIT ? read_hsctlr() : ns_sctlr;
  786. /* Return to original state */
  787. write_scr(scr);
  788. isb();
  789. ee = 0;
  790. ep_attr = NON_SECURE | EP_ST_DISABLE;
  791. if (sctlr & SCTLR_EE_BIT) {
  792. ep_attr |= EP_EE_BIG;
  793. ee = 1;
  794. }
  795. SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
  796. ep->pc = entrypoint;
  797. zeromem(&ep->args, sizeof(ep->args));
  798. ep->args.arg0 = context_id;
  799. mode = scr & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;
  800. /*
  801. * TODO: Choose async. exception bits if HYP mode is not
  802. * implemented according to the values of SCR.{AW, FW} bits
  803. */
  804. aif = SPSR_ABT_BIT | SPSR_IRQ_BIT | SPSR_FIQ_BIT;
  805. ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, aif);
  806. return PSCI_E_SUCCESS;
  807. }
  808. #endif /* __aarch64__ */
  809. /*******************************************************************************
  810. * This function validates the entrypoint with the platform layer if the
  811. * appropriate pm_ops hook is exported by the platform and returns the
  812. * 'entry_point_info'.
  813. ******************************************************************************/
  814. int psci_validate_entry_point(entry_point_info_t *ep,
  815. uintptr_t entrypoint,
  816. u_register_t context_id)
  817. {
  818. int rc;
  819. /* Validate the entrypoint using platform psci_ops */
  820. if (psci_plat_pm_ops->validate_ns_entrypoint != NULL) {
  821. rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint);
  822. if (rc != PSCI_E_SUCCESS)
  823. return PSCI_E_INVALID_ADDRESS;
  824. }
  825. /*
  826. * Verify and derive the re-entry information for
  827. * the non-secure world from the non-secure state from
  828. * where this call originated.
  829. */
  830. rc = psci_get_ns_ep_info(ep, entrypoint, context_id);
  831. return rc;
  832. }
  833. /*******************************************************************************
  834. * Generic handler which is called when a cpu is physically powered on. It
  835. * traverses the node information and finds the highest power level powered
  836. * off and performs generic, architectural, platform setup and state management
  837. * to power on that power level and power levels below it.
  838. * e.g. For a cpu that's been powered on, it will call the platform specific
  839. * code to enable the gic cpu interface and for a cluster it will enable
  840. * coherency at the interconnect level in addition to gic cpu interface.
  841. ******************************************************************************/
  842. void psci_warmboot_entrypoint(void)
  843. {
  844. unsigned int end_pwrlvl;
  845. unsigned int cpu_idx = plat_my_core_pos();
  846. unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
  847. psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
  848. /* Init registers that never change for the lifetime of TF-A */
  849. cm_manage_extensions_el3();
  850. /*
  851. * Verify that we have been explicitly turned ON or resumed from
  852. * suspend.
  853. */
  854. if (psci_get_aff_info_state() == AFF_STATE_OFF) {
  855. ERROR("Unexpected affinity info state.\n");
  856. panic();
  857. }
  858. /*
  859. * Get the maximum power domain level to traverse to after this cpu
  860. * has been physically powered up.
  861. */
  862. end_pwrlvl = get_power_on_target_pwrlvl();
  863. /* Get the parent nodes */
  864. psci_get_parent_pwr_domain_nodes(cpu_idx, end_pwrlvl, parent_nodes);
  865. /*
  866. * This function acquires the lock corresponding to each power level so
  867. * that by the time all locks are taken, the system topology is snapshot
  868. * and state management can be done safely.
  869. */
  870. psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
  871. psci_get_target_local_pwr_states(end_pwrlvl, &state_info);
  872. #if ENABLE_PSCI_STAT
  873. plat_psci_stat_accounting_stop(&state_info);
  874. #endif
  875. /*
  876. * This CPU could be resuming from suspend or it could have just been
  877. * turned on. To distinguish between these 2 cases, we examine the
  878. * affinity state of the CPU:
  879. * - If the affinity state is ON_PENDING then it has just been
  880. * turned on.
  881. * - Else it is resuming from suspend.
  882. *
  883. * Depending on the type of warm reset identified, choose the right set
  884. * of power management handler and perform the generic, architecture
  885. * and platform specific handling.
  886. */
  887. if (psci_get_aff_info_state() == AFF_STATE_ON_PENDING)
  888. psci_cpu_on_finish(cpu_idx, &state_info);
  889. else
  890. psci_cpu_suspend_finish(cpu_idx, &state_info);
  891. /*
  892. * Generic management: Now we just need to retrieve the
  893. * information that we had stashed away during the cpu_on
  894. * call to set this cpu on its way.
  895. */
  896. cm_prepare_el3_exit_ns();
  897. /*
  898. * Set the requested and target state of this CPU and all the higher
  899. * power domains which are ancestors of this CPU to run.
  900. */
  901. psci_set_pwr_domains_to_run(end_pwrlvl);
  902. #if ENABLE_PSCI_STAT
  903. /*
  904. * Update PSCI stats.
  905. * Caches are off when writing stats data on the power down path.
  906. * Since caches are now enabled, it's necessary to do cache
  907. * maintenance before reading that same data.
  908. */
  909. psci_stats_update_pwr_up(end_pwrlvl, &state_info);
  910. #endif
  911. /*
  912. * This loop releases the lock corresponding to each power level
  913. * in the reverse order to which they were acquired.
  914. */
  915. psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
  916. }
  917. /*******************************************************************************
  918. * This function initializes the set of hooks that PSCI invokes as part of power
  919. * management operation. The power management hooks are expected to be provided
  920. * by the SPD, after it finishes all its initialization
  921. ******************************************************************************/
  922. void psci_register_spd_pm_hook(const spd_pm_ops_t *pm)
  923. {
  924. assert(pm != NULL);
  925. psci_spd_pm = pm;
  926. if (pm->svc_migrate != NULL)
  927. psci_caps |= define_psci_cap(PSCI_MIG_AARCH64);
  928. if (pm->svc_migrate_info != NULL)
  929. psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64)
  930. | define_psci_cap(PSCI_MIG_INFO_TYPE);
  931. }
  932. /*******************************************************************************
  933. * This function invokes the migrate info hook in the spd_pm_ops. It performs
  934. * the necessary return value validation. If the Secure Payload is UP and
  935. * migrate capable, it returns the mpidr of the CPU on which the Secure payload
  936. * is resident through the mpidr parameter. Else the value of the parameter on
  937. * return is undefined.
  938. ******************************************************************************/
  939. int psci_spd_migrate_info(u_register_t *mpidr)
  940. {
  941. int rc;
  942. if ((psci_spd_pm == NULL) || (psci_spd_pm->svc_migrate_info == NULL))
  943. return PSCI_E_NOT_SUPPORTED;
  944. rc = psci_spd_pm->svc_migrate_info(mpidr);
  945. assert((rc == PSCI_TOS_UP_MIG_CAP) || (rc == PSCI_TOS_NOT_UP_MIG_CAP) ||
  946. (rc == PSCI_TOS_NOT_PRESENT_MP) || (rc == PSCI_E_NOT_SUPPORTED));
  947. return rc;
  948. }
  949. /*******************************************************************************
  950. * This function prints the state of all power domains present in the
  951. * system
  952. ******************************************************************************/
  953. void psci_print_power_domain_map(void)
  954. {
  955. #if LOG_LEVEL >= LOG_LEVEL_INFO
  956. unsigned int idx;
  957. plat_local_state_t state;
  958. plat_local_state_type_t state_type;
  959. /* This array maps to the PSCI_STATE_X definitions in psci.h */
  960. static const char * const psci_state_type_str[] = {
  961. "ON",
  962. "RETENTION",
  963. "OFF",
  964. };
  965. INFO("PSCI Power Domain Map:\n");
  966. for (idx = 0; idx < (PSCI_NUM_PWR_DOMAINS - psci_plat_core_count);
  967. idx++) {
  968. state_type = find_local_state_type(
  969. psci_non_cpu_pd_nodes[idx].local_state);
  970. INFO(" Domain Node : Level %u, parent_node %u,"
  971. " State %s (0x%x)\n",
  972. psci_non_cpu_pd_nodes[idx].level,
  973. psci_non_cpu_pd_nodes[idx].parent_node,
  974. psci_state_type_str[state_type],
  975. psci_non_cpu_pd_nodes[idx].local_state);
  976. }
  977. for (idx = 0; idx < psci_plat_core_count; idx++) {
  978. state = psci_get_cpu_local_state_by_idx(idx);
  979. state_type = find_local_state_type(state);
  980. INFO(" CPU Node : MPID 0x%llx, parent_node %u,"
  981. " State %s (0x%x)\n",
  982. (unsigned long long)psci_cpu_pd_nodes[idx].mpidr,
  983. psci_cpu_pd_nodes[idx].parent_node,
  984. psci_state_type_str[state_type],
  985. psci_get_cpu_local_state_by_idx(idx));
  986. }
  987. #endif
  988. }
  989. /******************************************************************************
  990. * Return whether any secondaries were powered up with CPU_ON call. A CPU that
  991. * have ever been powered up would have set its MPDIR value to something other
  992. * than PSCI_INVALID_MPIDR. Note that MPDIR isn't reset back to
  993. * PSCI_INVALID_MPIDR when a CPU is powered down later, so the return value is
  994. * meaningful only when called on the primary CPU during early boot.
  995. *****************************************************************************/
  996. int psci_secondaries_brought_up(void)
  997. {
  998. unsigned int idx, n_valid = 0U;
  999. for (idx = 0U; idx < ARRAY_SIZE(psci_cpu_pd_nodes); idx++) {
  1000. if (psci_cpu_pd_nodes[idx].mpidr != PSCI_INVALID_MPIDR)
  1001. n_valid++;
  1002. }
  1003. assert(n_valid > 0U);
  1004. return (n_valid > 1U) ? 1 : 0;
  1005. }
  1006. /*******************************************************************************
  1007. * Initiate power down sequence, by calling power down operations registered for
  1008. * this CPU.
  1009. ******************************************************************************/
  1010. void psci_pwrdown_cpu(unsigned int power_level)
  1011. {
  1012. psci_do_manage_extensions();
  1013. #if HW_ASSISTED_COHERENCY
  1014. /*
  1015. * With hardware-assisted coherency, the CPU drivers only initiate the
  1016. * power down sequence, without performing cache-maintenance operations
  1017. * in software. Data caches enabled both before and after this call.
  1018. */
  1019. prepare_cpu_pwr_dwn(power_level);
  1020. #else
  1021. /*
  1022. * Without hardware-assisted coherency, the CPU drivers disable data
  1023. * caches, then perform cache-maintenance operations in software.
  1024. *
  1025. * This also calls prepare_cpu_pwr_dwn() to initiate power down
  1026. * sequence, but that function will return with data caches disabled.
  1027. * We must ensure that the stack memory is flushed out to memory before
  1028. * we start popping from it again.
  1029. */
  1030. psci_do_pwrdown_cache_maintenance(power_level);
  1031. #endif
  1032. }
  1033. /*******************************************************************************
  1034. * This function invokes the callback 'stop_func()' with the 'mpidr' of each
  1035. * online PE. Caller can pass suitable method to stop a remote core.
  1036. *
  1037. * 'wait_ms' is the timeout value in milliseconds for the other cores to
  1038. * transition to power down state. Passing '0' makes it non-blocking.
  1039. *
  1040. * The function returns 'PSCI_E_DENIED' if some cores failed to stop within the
  1041. * given timeout.
  1042. ******************************************************************************/
  1043. int psci_stop_other_cores(unsigned int wait_ms,
  1044. void (*stop_func)(u_register_t mpidr))
  1045. {
  1046. unsigned int idx, this_cpu_idx;
  1047. this_cpu_idx = plat_my_core_pos();
  1048. /* Invoke stop_func for each core */
  1049. for (idx = 0U; idx < psci_plat_core_count; idx++) {
  1050. /* skip current CPU */
  1051. if (idx == this_cpu_idx) {
  1052. continue;
  1053. }
  1054. /* Check if the CPU is ON */
  1055. if (psci_get_aff_info_state_by_idx(idx) == AFF_STATE_ON) {
  1056. (*stop_func)(psci_cpu_pd_nodes[idx].mpidr);
  1057. }
  1058. }
  1059. /* Need to wait for other cores to shutdown */
  1060. if (wait_ms != 0U) {
  1061. while ((wait_ms-- != 0U) && (!psci_is_last_on_cpu())) {
  1062. mdelay(1U);
  1063. }
  1064. if (!psci_is_last_on_cpu()) {
  1065. WARN("Failed to stop all cores!\n");
  1066. psci_print_power_domain_map();
  1067. return PSCI_E_DENIED;
  1068. }
  1069. }
  1070. return PSCI_E_SUCCESS;
  1071. }
  1072. /*******************************************************************************
  1073. * This function verifies that all the other cores in the system have been
  1074. * turned OFF and the current CPU is the last running CPU in the system.
  1075. * Returns true if the current CPU is the last ON CPU or false otherwise.
  1076. *
  1077. * This API has following differences with psci_is_last_on_cpu
  1078. * 1. PSCI states are locked
  1079. ******************************************************************************/
  1080. bool psci_is_last_on_cpu_safe(void)
  1081. {
  1082. unsigned int this_core = plat_my_core_pos();
  1083. unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
  1084. psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
  1085. psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1086. if (!psci_is_last_on_cpu()) {
  1087. psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1088. return false;
  1089. }
  1090. psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1091. return true;
  1092. }
  1093. /*******************************************************************************
  1094. * This function verifies that all cores in the system have been turned ON.
  1095. * Returns true, if all CPUs are ON or false otherwise.
  1096. *
  1097. * This API has following differences with psci_are_all_cpus_on
  1098. * 1. PSCI states are locked
  1099. ******************************************************************************/
  1100. bool psci_are_all_cpus_on_safe(void)
  1101. {
  1102. unsigned int this_core = plat_my_core_pos();
  1103. unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
  1104. psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
  1105. psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1106. if (!psci_are_all_cpus_on()) {
  1107. psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1108. return false;
  1109. }
  1110. psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1111. return true;
  1112. }
  1113. /*******************************************************************************
  1114. * This function performs architectural feature specific management.
  1115. * It ensures the architectural features are disabled during cpu
  1116. * power off/suspend operations.
  1117. ******************************************************************************/
  1118. void psci_do_manage_extensions(void)
  1119. {
  1120. /*
  1121. * On power down we need to disable statistical profiling extensions
  1122. * before exiting coherency.
  1123. */
  1124. if (is_feat_spe_supported()) {
  1125. spe_stop();
  1126. }
  1127. }