psci_common.c 37 KB

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