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