psci_common.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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. #if HW_ASSISTED_COHERENCY
  1013. /*
  1014. * With hardware-assisted coherency, the CPU drivers only initiate the
  1015. * power down sequence, without performing cache-maintenance operations
  1016. * in software. Data caches enabled both before and after this call.
  1017. */
  1018. prepare_cpu_pwr_dwn(power_level);
  1019. #else
  1020. /*
  1021. * Without hardware-assisted coherency, the CPU drivers disable data
  1022. * caches, then perform cache-maintenance operations in software.
  1023. *
  1024. * This also calls prepare_cpu_pwr_dwn() to initiate power down
  1025. * sequence, but that function will return with data caches disabled.
  1026. * We must ensure that the stack memory is flushed out to memory before
  1027. * we start popping from it again.
  1028. */
  1029. psci_do_pwrdown_cache_maintenance(power_level);
  1030. #endif
  1031. }
  1032. /*******************************************************************************
  1033. * This function invokes the callback 'stop_func()' with the 'mpidr' of each
  1034. * online PE. Caller can pass suitable method to stop a remote core.
  1035. *
  1036. * 'wait_ms' is the timeout value in milliseconds for the other cores to
  1037. * transition to power down state. Passing '0' makes it non-blocking.
  1038. *
  1039. * The function returns 'PSCI_E_DENIED' if some cores failed to stop within the
  1040. * given timeout.
  1041. ******************************************************************************/
  1042. int psci_stop_other_cores(unsigned int wait_ms,
  1043. void (*stop_func)(u_register_t mpidr))
  1044. {
  1045. unsigned int idx, this_cpu_idx;
  1046. this_cpu_idx = plat_my_core_pos();
  1047. /* Invoke stop_func for each core */
  1048. for (idx = 0U; idx < psci_plat_core_count; idx++) {
  1049. /* skip current CPU */
  1050. if (idx == this_cpu_idx) {
  1051. continue;
  1052. }
  1053. /* Check if the CPU is ON */
  1054. if (psci_get_aff_info_state_by_idx(idx) == AFF_STATE_ON) {
  1055. (*stop_func)(psci_cpu_pd_nodes[idx].mpidr);
  1056. }
  1057. }
  1058. /* Need to wait for other cores to shutdown */
  1059. if (wait_ms != 0U) {
  1060. while ((wait_ms-- != 0U) && (!psci_is_last_on_cpu())) {
  1061. mdelay(1U);
  1062. }
  1063. if (!psci_is_last_on_cpu()) {
  1064. WARN("Failed to stop all cores!\n");
  1065. psci_print_power_domain_map();
  1066. return PSCI_E_DENIED;
  1067. }
  1068. }
  1069. return PSCI_E_SUCCESS;
  1070. }
  1071. /*******************************************************************************
  1072. * This function verifies that all the other cores in the system have been
  1073. * turned OFF and the current CPU is the last running CPU in the system.
  1074. * Returns true if the current CPU is the last ON CPU or false otherwise.
  1075. *
  1076. * This API has following differences with psci_is_last_on_cpu
  1077. * 1. PSCI states are locked
  1078. ******************************************************************************/
  1079. bool psci_is_last_on_cpu_safe(void)
  1080. {
  1081. unsigned int this_core = plat_my_core_pos();
  1082. unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
  1083. psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
  1084. psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1085. if (!psci_is_last_on_cpu()) {
  1086. psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1087. return false;
  1088. }
  1089. psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1090. return true;
  1091. }
  1092. /*******************************************************************************
  1093. * This function verifies that all cores in the system have been turned ON.
  1094. * Returns true, if all CPUs are ON or false otherwise.
  1095. *
  1096. * This API has following differences with psci_are_all_cpus_on
  1097. * 1. PSCI states are locked
  1098. ******************************************************************************/
  1099. bool psci_are_all_cpus_on_safe(void)
  1100. {
  1101. unsigned int this_core = plat_my_core_pos();
  1102. unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
  1103. psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
  1104. psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1105. if (!psci_are_all_cpus_on()) {
  1106. psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1107. return false;
  1108. }
  1109. psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
  1110. return true;
  1111. }