psci_main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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/debug.h>
  11. #include <lib/pmf/pmf.h>
  12. #include <lib/runtime_instr.h>
  13. #include <lib/smccc.h>
  14. #include <plat/common/platform.h>
  15. #include <services/arm_arch_svc.h>
  16. #include "psci_private.h"
  17. /*******************************************************************************
  18. * PSCI frontend api for servicing SMCs. Described in the PSCI spec.
  19. ******************************************************************************/
  20. int psci_cpu_on(u_register_t target_cpu,
  21. uintptr_t entrypoint,
  22. u_register_t context_id)
  23. {
  24. int rc;
  25. entry_point_info_t ep;
  26. /* Validate the target CPU */
  27. if (!is_valid_mpidr(target_cpu))
  28. return PSCI_E_INVALID_PARAMS;
  29. /* Validate the entry point and get the entry_point_info */
  30. rc = psci_validate_entry_point(&ep, entrypoint, context_id);
  31. if (rc != PSCI_E_SUCCESS)
  32. return rc;
  33. /*
  34. * To turn this cpu on, specify which power
  35. * levels need to be turned on
  36. */
  37. return psci_cpu_on_start(target_cpu, &ep);
  38. }
  39. unsigned int psci_version(void)
  40. {
  41. return PSCI_MAJOR_VER | PSCI_MINOR_VER;
  42. }
  43. int psci_cpu_suspend(unsigned int power_state,
  44. uintptr_t entrypoint,
  45. u_register_t context_id)
  46. {
  47. int rc;
  48. unsigned int target_pwrlvl, is_power_down_state;
  49. entry_point_info_t ep;
  50. psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
  51. plat_local_state_t cpu_pd_state;
  52. #if PSCI_OS_INIT_MODE
  53. unsigned int cpu_idx = plat_my_core_pos();
  54. plat_local_state_t prev[PLAT_MAX_PWR_LVL];
  55. #endif
  56. /* Validate the power_state parameter */
  57. rc = psci_validate_power_state(power_state, &state_info);
  58. if (rc != PSCI_E_SUCCESS) {
  59. assert(rc == PSCI_E_INVALID_PARAMS);
  60. return rc;
  61. }
  62. /*
  63. * Get the value of the state type bit from the power state parameter.
  64. */
  65. is_power_down_state = psci_get_pstate_type(power_state);
  66. /* Sanity check the requested suspend levels */
  67. assert(psci_validate_suspend_req(&state_info, is_power_down_state)
  68. == PSCI_E_SUCCESS);
  69. target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
  70. if (target_pwrlvl == PSCI_INVALID_PWR_LVL) {
  71. ERROR("Invalid target power level for suspend operation\n");
  72. panic();
  73. }
  74. /* Fast path for CPU standby.*/
  75. if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
  76. if (psci_plat_pm_ops->cpu_standby == NULL)
  77. return PSCI_E_INVALID_PARAMS;
  78. /*
  79. * Set the state of the CPU power domain to the platform
  80. * specific retention state and enter the standby state.
  81. */
  82. cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
  83. psci_set_cpu_local_state(cpu_pd_state);
  84. #if PSCI_OS_INIT_MODE
  85. /*
  86. * If in OS-initiated mode, save a copy of the previous
  87. * requested local power states and update the new requested
  88. * local power states for this CPU.
  89. */
  90. if (psci_suspend_mode == OS_INIT) {
  91. psci_update_req_local_pwr_states(target_pwrlvl, cpu_idx,
  92. &state_info, prev);
  93. }
  94. #endif
  95. #if ENABLE_PSCI_STAT
  96. plat_psci_stat_accounting_start(&state_info);
  97. #endif
  98. #if ENABLE_RUNTIME_INSTRUMENTATION
  99. PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
  100. RT_INSTR_ENTER_HW_LOW_PWR,
  101. PMF_NO_CACHE_MAINT);
  102. #endif
  103. psci_plat_pm_ops->cpu_standby(cpu_pd_state);
  104. /* Upon exit from standby, set the state back to RUN. */
  105. psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
  106. #if PSCI_OS_INIT_MODE
  107. /*
  108. * If in OS-initiated mode, restore the previous requested
  109. * local power states for this CPU.
  110. */
  111. if (psci_suspend_mode == OS_INIT) {
  112. psci_restore_req_local_pwr_states(cpu_idx, prev);
  113. }
  114. #endif
  115. #if ENABLE_RUNTIME_INSTRUMENTATION
  116. PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
  117. RT_INSTR_EXIT_HW_LOW_PWR,
  118. PMF_NO_CACHE_MAINT);
  119. #endif
  120. #if ENABLE_PSCI_STAT
  121. plat_psci_stat_accounting_stop(&state_info);
  122. /* Update PSCI stats */
  123. psci_stats_update_pwr_up(PSCI_CPU_PWR_LVL, &state_info);
  124. #endif
  125. return PSCI_E_SUCCESS;
  126. }
  127. /*
  128. * If a power down state has been requested, we need to verify entry
  129. * point and program entry information.
  130. */
  131. if (is_power_down_state != 0U) {
  132. rc = psci_validate_entry_point(&ep, entrypoint, context_id);
  133. if (rc != PSCI_E_SUCCESS)
  134. return rc;
  135. }
  136. /*
  137. * Do what is needed to enter the power down state. Upon success,
  138. * enter the final wfi which will power down this CPU. This function
  139. * might return if the power down was abandoned for any reason, e.g.
  140. * arrival of an interrupt
  141. */
  142. rc = psci_cpu_suspend_start(&ep,
  143. target_pwrlvl,
  144. &state_info,
  145. is_power_down_state);
  146. return rc;
  147. }
  148. int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
  149. {
  150. int rc;
  151. psci_power_state_t state_info;
  152. entry_point_info_t ep;
  153. /* Check if the current CPU is the last ON CPU in the system */
  154. if (!psci_is_last_on_cpu())
  155. return PSCI_E_DENIED;
  156. /* Validate the entry point and get the entry_point_info */
  157. rc = psci_validate_entry_point(&ep, entrypoint, context_id);
  158. if (rc != PSCI_E_SUCCESS)
  159. return rc;
  160. /* Query the psci_power_state for system suspend */
  161. psci_query_sys_suspend_pwrstate(&state_info);
  162. /*
  163. * Check if platform allows suspend to Highest power level
  164. * (System level)
  165. */
  166. if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL)
  167. return PSCI_E_DENIED;
  168. /* Ensure that the psci_power_state makes sense */
  169. assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
  170. == PSCI_E_SUCCESS);
  171. assert(is_local_state_off(
  172. state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
  173. /*
  174. * Do what is needed to enter the system suspend state. This function
  175. * might return if the power down was abandoned for any reason, e.g.
  176. * arrival of an interrupt
  177. */
  178. rc = psci_cpu_suspend_start(&ep,
  179. PLAT_MAX_PWR_LVL,
  180. &state_info,
  181. PSTATE_TYPE_POWERDOWN);
  182. return rc;
  183. }
  184. int psci_cpu_off(void)
  185. {
  186. int rc;
  187. unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
  188. /*
  189. * Do what is needed to power off this CPU and possible higher power
  190. * levels if it able to do so. Upon success, enter the final wfi
  191. * which will power down this CPU.
  192. */
  193. rc = psci_do_cpu_off(target_pwrlvl);
  194. /*
  195. * The only error cpu_off can return is E_DENIED. So check if that's
  196. * indeed the case.
  197. */
  198. assert(rc == PSCI_E_DENIED);
  199. return rc;
  200. }
  201. int psci_affinity_info(u_register_t target_affinity,
  202. unsigned int lowest_affinity_level)
  203. {
  204. unsigned int target_idx;
  205. /* Validate the target affinity */
  206. if (!is_valid_mpidr(target_affinity))
  207. return PSCI_E_INVALID_PARAMS;
  208. /* We dont support level higher than PSCI_CPU_PWR_LVL */
  209. if (lowest_affinity_level > PSCI_CPU_PWR_LVL)
  210. return PSCI_E_INVALID_PARAMS;
  211. /* Calculate the cpu index of the target */
  212. target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity);
  213. /*
  214. * Generic management:
  215. * Perform cache maintanence ahead of reading the target CPU state to
  216. * ensure that the data is not stale.
  217. * There is a theoretical edge case where the cache may contain stale
  218. * data for the target CPU data - this can occur under the following
  219. * conditions:
  220. * - the target CPU is in another cluster from the current
  221. * - the target CPU was the last CPU to shutdown on its cluster
  222. * - the cluster was removed from coherency as part of the CPU shutdown
  223. *
  224. * In this case the cache maintenace that was performed as part of the
  225. * target CPUs shutdown was not seen by the current CPU's cluster. And
  226. * so the cache may contain stale data for the target CPU.
  227. */
  228. flush_cpu_data_by_index(target_idx,
  229. psci_svc_cpu_data.aff_info_state);
  230. return psci_get_aff_info_state_by_idx(target_idx);
  231. }
  232. int psci_migrate(u_register_t target_cpu)
  233. {
  234. int rc;
  235. u_register_t resident_cpu_mpidr;
  236. /* Validate the target cpu */
  237. if (!is_valid_mpidr(target_cpu))
  238. return PSCI_E_INVALID_PARAMS;
  239. rc = psci_spd_migrate_info(&resident_cpu_mpidr);
  240. if (rc != PSCI_TOS_UP_MIG_CAP)
  241. return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
  242. PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
  243. /*
  244. * Migrate should only be invoked on the CPU where
  245. * the Secure OS is resident.
  246. */
  247. if (resident_cpu_mpidr != read_mpidr_el1())
  248. return PSCI_E_NOT_PRESENT;
  249. /* Check the validity of the specified target cpu */
  250. if (!is_valid_mpidr(target_cpu))
  251. return PSCI_E_INVALID_PARAMS;
  252. assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
  253. rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
  254. assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
  255. return rc;
  256. }
  257. int psci_migrate_info_type(void)
  258. {
  259. u_register_t resident_cpu_mpidr;
  260. return psci_spd_migrate_info(&resident_cpu_mpidr);
  261. }
  262. u_register_t psci_migrate_info_up_cpu(void)
  263. {
  264. u_register_t resident_cpu_mpidr;
  265. int rc;
  266. /*
  267. * Return value of this depends upon what
  268. * psci_spd_migrate_info() returns.
  269. */
  270. rc = psci_spd_migrate_info(&resident_cpu_mpidr);
  271. if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP))
  272. return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
  273. return resident_cpu_mpidr;
  274. }
  275. int psci_node_hw_state(u_register_t target_cpu,
  276. unsigned int power_level)
  277. {
  278. int rc;
  279. /* Validate target_cpu */
  280. if (!is_valid_mpidr(target_cpu))
  281. return PSCI_E_INVALID_PARAMS;
  282. /* Validate power_level against PLAT_MAX_PWR_LVL */
  283. if (power_level > PLAT_MAX_PWR_LVL)
  284. return PSCI_E_INVALID_PARAMS;
  285. /*
  286. * Dispatch this call to platform to query power controller, and pass on
  287. * to the caller what it returns
  288. */
  289. assert(psci_plat_pm_ops->get_node_hw_state != NULL);
  290. rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
  291. assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
  292. || (rc == PSCI_E_NOT_SUPPORTED)
  293. || (rc == PSCI_E_INVALID_PARAMS));
  294. return rc;
  295. }
  296. int psci_features(unsigned int psci_fid)
  297. {
  298. unsigned int local_caps = psci_caps;
  299. if (psci_fid == SMCCC_VERSION)
  300. return PSCI_E_SUCCESS;
  301. /* Check if it is a 64 bit function */
  302. if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64)
  303. local_caps &= PSCI_CAP_64BIT_MASK;
  304. /* Check for invalid fid */
  305. if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
  306. && is_psci_fid(psci_fid)))
  307. return PSCI_E_NOT_SUPPORTED;
  308. /* Check if the psci fid is supported or not */
  309. if ((local_caps & define_psci_cap(psci_fid)) == 0U)
  310. return PSCI_E_NOT_SUPPORTED;
  311. /* Format the feature flags */
  312. if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
  313. (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
  314. unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
  315. (FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
  316. return (int)ret;
  317. }
  318. /* Return 0 for all other fid's */
  319. return PSCI_E_SUCCESS;
  320. }
  321. #if PSCI_OS_INIT_MODE
  322. int psci_set_suspend_mode(unsigned int mode)
  323. {
  324. if (psci_suspend_mode == mode) {
  325. return PSCI_E_SUCCESS;
  326. }
  327. if (mode == PLAT_COORD) {
  328. /* Check if the current CPU is the last ON CPU in the system */
  329. if (!psci_is_last_on_cpu_safe()) {
  330. return PSCI_E_DENIED;
  331. }
  332. }
  333. if (mode == OS_INIT) {
  334. /*
  335. * Check if all CPUs in the system are ON or if the current
  336. * CPU is the last ON CPU in the system.
  337. */
  338. if (!(psci_are_all_cpus_on_safe() ||
  339. psci_is_last_on_cpu_safe())) {
  340. return PSCI_E_DENIED;
  341. }
  342. }
  343. psci_suspend_mode = mode;
  344. psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
  345. sizeof(psci_suspend_mode));
  346. return PSCI_E_SUCCESS;
  347. }
  348. #endif
  349. /*******************************************************************************
  350. * PSCI top level handler for servicing SMCs.
  351. ******************************************************************************/
  352. u_register_t psci_smc_handler(uint32_t smc_fid,
  353. u_register_t x1,
  354. u_register_t x2,
  355. u_register_t x3,
  356. u_register_t x4,
  357. void *cookie,
  358. void *handle,
  359. u_register_t flags)
  360. {
  361. u_register_t ret;
  362. if (is_caller_secure(flags))
  363. return (u_register_t)SMC_UNK;
  364. /* Check the fid against the capabilities */
  365. if ((psci_caps & define_psci_cap(smc_fid)) == 0U)
  366. return (u_register_t)SMC_UNK;
  367. if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
  368. /* 32-bit PSCI function, clear top parameter bits */
  369. uint32_t r1 = (uint32_t)x1;
  370. uint32_t r2 = (uint32_t)x2;
  371. uint32_t r3 = (uint32_t)x3;
  372. switch (smc_fid) {
  373. case PSCI_VERSION:
  374. ret = (u_register_t)psci_version();
  375. break;
  376. case PSCI_CPU_OFF:
  377. ret = (u_register_t)psci_cpu_off();
  378. break;
  379. case PSCI_CPU_SUSPEND_AARCH32:
  380. ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
  381. break;
  382. case PSCI_CPU_ON_AARCH32:
  383. ret = (u_register_t)psci_cpu_on(r1, r2, r3);
  384. break;
  385. case PSCI_AFFINITY_INFO_AARCH32:
  386. ret = (u_register_t)psci_affinity_info(r1, r2);
  387. break;
  388. case PSCI_MIG_AARCH32:
  389. ret = (u_register_t)psci_migrate(r1);
  390. break;
  391. case PSCI_MIG_INFO_TYPE:
  392. ret = (u_register_t)psci_migrate_info_type();
  393. break;
  394. case PSCI_MIG_INFO_UP_CPU_AARCH32:
  395. ret = psci_migrate_info_up_cpu();
  396. break;
  397. case PSCI_NODE_HW_STATE_AARCH32:
  398. ret = (u_register_t)psci_node_hw_state(r1, r2);
  399. break;
  400. case PSCI_SYSTEM_SUSPEND_AARCH32:
  401. ret = (u_register_t)psci_system_suspend(r1, r2);
  402. break;
  403. case PSCI_SYSTEM_OFF:
  404. psci_system_off();
  405. /* We should never return from psci_system_off() */
  406. break;
  407. case PSCI_SYSTEM_RESET:
  408. psci_system_reset();
  409. /* We should never return from psci_system_reset() */
  410. break;
  411. case PSCI_FEATURES:
  412. ret = (u_register_t)psci_features(r1);
  413. break;
  414. #if PSCI_OS_INIT_MODE
  415. case PSCI_SET_SUSPEND_MODE:
  416. ret = (u_register_t)psci_set_suspend_mode(r1);
  417. break;
  418. #endif
  419. #if ENABLE_PSCI_STAT
  420. case PSCI_STAT_RESIDENCY_AARCH32:
  421. ret = psci_stat_residency(r1, r2);
  422. break;
  423. case PSCI_STAT_COUNT_AARCH32:
  424. ret = psci_stat_count(r1, r2);
  425. break;
  426. #endif
  427. case PSCI_MEM_PROTECT:
  428. ret = psci_mem_protect(r1);
  429. break;
  430. case PSCI_MEM_CHK_RANGE_AARCH32:
  431. ret = psci_mem_chk_range(r1, r2);
  432. break;
  433. case PSCI_SYSTEM_RESET2_AARCH32:
  434. /* We should never return from psci_system_reset2() */
  435. ret = psci_system_reset2(r1, r2);
  436. break;
  437. default:
  438. WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
  439. ret = (u_register_t)SMC_UNK;
  440. break;
  441. }
  442. } else {
  443. /* 64-bit PSCI function */
  444. switch (smc_fid) {
  445. case PSCI_CPU_SUSPEND_AARCH64:
  446. ret = (u_register_t)
  447. psci_cpu_suspend((unsigned int)x1, x2, x3);
  448. break;
  449. case PSCI_CPU_ON_AARCH64:
  450. ret = (u_register_t)psci_cpu_on(x1, x2, x3);
  451. break;
  452. case PSCI_AFFINITY_INFO_AARCH64:
  453. ret = (u_register_t)
  454. psci_affinity_info(x1, (unsigned int)x2);
  455. break;
  456. case PSCI_MIG_AARCH64:
  457. ret = (u_register_t)psci_migrate(x1);
  458. break;
  459. case PSCI_MIG_INFO_UP_CPU_AARCH64:
  460. ret = psci_migrate_info_up_cpu();
  461. break;
  462. case PSCI_NODE_HW_STATE_AARCH64:
  463. ret = (u_register_t)psci_node_hw_state(
  464. x1, (unsigned int) x2);
  465. break;
  466. case PSCI_SYSTEM_SUSPEND_AARCH64:
  467. ret = (u_register_t)psci_system_suspend(x1, x2);
  468. break;
  469. #if ENABLE_PSCI_STAT
  470. case PSCI_STAT_RESIDENCY_AARCH64:
  471. ret = psci_stat_residency(x1, (unsigned int) x2);
  472. break;
  473. case PSCI_STAT_COUNT_AARCH64:
  474. ret = psci_stat_count(x1, (unsigned int) x2);
  475. break;
  476. #endif
  477. case PSCI_MEM_CHK_RANGE_AARCH64:
  478. ret = psci_mem_chk_range(x1, x2);
  479. break;
  480. case PSCI_SYSTEM_RESET2_AARCH64:
  481. /* We should never return from psci_system_reset2() */
  482. ret = psci_system_reset2((uint32_t) x1, x2);
  483. break;
  484. default:
  485. WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
  486. ret = (u_register_t)SMC_UNK;
  487. break;
  488. }
  489. }
  490. return ret;
  491. }