psci_main.c 14 KB

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