plat_pm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <platform_def.h>
  9. #include <arch_helpers.h>
  10. #include <common/debug.h>
  11. #include <drivers/console.h>
  12. #include <drivers/delay_timer.h>
  13. #include <lib/psci/psci.h>
  14. #include <plat_private.h>
  15. /* Macros to read the rk power domain state */
  16. #define RK_CORE_PWR_STATE(state) \
  17. ((state)->pwr_domain_state[MPIDR_AFFLVL0])
  18. #define RK_CLUSTER_PWR_STATE(state) \
  19. ((state)->pwr_domain_state[MPIDR_AFFLVL1])
  20. #define RK_SYSTEM_PWR_STATE(state) \
  21. ((state)->pwr_domain_state[PLAT_MAX_PWR_LVL])
  22. static uintptr_t rockchip_sec_entrypoint;
  23. #pragma weak rockchip_soc_cores_pwr_dm_on
  24. #pragma weak rockchip_soc_hlvl_pwr_dm_off
  25. #pragma weak rockchip_soc_cores_pwr_dm_off
  26. #pragma weak rockchip_soc_sys_pwr_dm_suspend
  27. #pragma weak rockchip_soc_cores_pwr_dm_suspend
  28. #pragma weak rockchip_soc_hlvl_pwr_dm_suspend
  29. #pragma weak rockchip_soc_hlvl_pwr_dm_on_finish
  30. #pragma weak rockchip_soc_cores_pwr_dm_on_finish
  31. #pragma weak rockchip_soc_sys_pwr_dm_resume
  32. #pragma weak rockchip_soc_hlvl_pwr_dm_resume
  33. #pragma weak rockchip_soc_cores_pwr_dm_resume
  34. #pragma weak rockchip_soc_soft_reset
  35. #pragma weak rockchip_soc_system_off
  36. #pragma weak rockchip_soc_sys_pd_pwr_dn_wfi
  37. #pragma weak rockchip_soc_cores_pd_pwr_dn_wfi
  38. int rockchip_soc_cores_pwr_dm_on(unsigned long mpidr, uint64_t entrypoint)
  39. {
  40. return PSCI_E_NOT_SUPPORTED;
  41. }
  42. int rockchip_soc_hlvl_pwr_dm_off(uint32_t lvl,
  43. plat_local_state_t lvl_state)
  44. {
  45. return PSCI_E_NOT_SUPPORTED;
  46. }
  47. int rockchip_soc_cores_pwr_dm_off(void)
  48. {
  49. return PSCI_E_NOT_SUPPORTED;
  50. }
  51. int rockchip_soc_sys_pwr_dm_suspend(void)
  52. {
  53. return PSCI_E_NOT_SUPPORTED;
  54. }
  55. int rockchip_soc_cores_pwr_dm_suspend(void)
  56. {
  57. return PSCI_E_NOT_SUPPORTED;
  58. }
  59. int rockchip_soc_hlvl_pwr_dm_suspend(uint32_t lvl,
  60. plat_local_state_t lvl_state)
  61. {
  62. return PSCI_E_NOT_SUPPORTED;
  63. }
  64. int rockchip_soc_hlvl_pwr_dm_on_finish(uint32_t lvl,
  65. plat_local_state_t lvl_state)
  66. {
  67. return PSCI_E_NOT_SUPPORTED;
  68. }
  69. int rockchip_soc_cores_pwr_dm_on_finish(void)
  70. {
  71. return PSCI_E_NOT_SUPPORTED;
  72. }
  73. int rockchip_soc_sys_pwr_dm_resume(void)
  74. {
  75. return PSCI_E_NOT_SUPPORTED;
  76. }
  77. int rockchip_soc_hlvl_pwr_dm_resume(uint32_t lvl,
  78. plat_local_state_t lvl_state)
  79. {
  80. return PSCI_E_NOT_SUPPORTED;
  81. }
  82. int rockchip_soc_cores_pwr_dm_resume(void)
  83. {
  84. return PSCI_E_NOT_SUPPORTED;
  85. }
  86. void __dead2 rockchip_soc_soft_reset(void)
  87. {
  88. while (1)
  89. ;
  90. }
  91. void __dead2 rockchip_soc_system_off(void)
  92. {
  93. while (1)
  94. ;
  95. }
  96. void __dead2 rockchip_soc_cores_pd_pwr_dn_wfi(
  97. const psci_power_state_t *target_state)
  98. {
  99. psci_power_down_wfi();
  100. }
  101. void __dead2 rockchip_soc_sys_pd_pwr_dn_wfi(void)
  102. {
  103. psci_power_down_wfi();
  104. }
  105. /*******************************************************************************
  106. * Rockchip standard platform handler called to check the validity of the power
  107. * state parameter.
  108. ******************************************************************************/
  109. int rockchip_validate_power_state(unsigned int power_state,
  110. psci_power_state_t *req_state)
  111. {
  112. int pstate = psci_get_pstate_type(power_state);
  113. int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
  114. int i;
  115. assert(req_state);
  116. if (pwr_lvl > PLAT_MAX_PWR_LVL)
  117. return PSCI_E_INVALID_PARAMS;
  118. /* Sanity check the requested state */
  119. if (pstate == PSTATE_TYPE_STANDBY) {
  120. /*
  121. * It's probably to enter standby only on power level 0
  122. * ignore any other power level.
  123. */
  124. if (pwr_lvl != MPIDR_AFFLVL0)
  125. return PSCI_E_INVALID_PARAMS;
  126. req_state->pwr_domain_state[MPIDR_AFFLVL0] =
  127. PLAT_MAX_RET_STATE;
  128. } else {
  129. for (i = MPIDR_AFFLVL0; i <= pwr_lvl; i++)
  130. req_state->pwr_domain_state[i] =
  131. PLAT_MAX_OFF_STATE;
  132. for (i = (pwr_lvl + 1); i <= PLAT_MAX_PWR_LVL; i++)
  133. req_state->pwr_domain_state[i] =
  134. PLAT_MAX_RET_STATE;
  135. }
  136. /* We expect the 'state id' to be zero */
  137. if (psci_get_pstate_id(power_state))
  138. return PSCI_E_INVALID_PARAMS;
  139. return PSCI_E_SUCCESS;
  140. }
  141. void rockchip_get_sys_suspend_power_state(psci_power_state_t *req_state)
  142. {
  143. int i;
  144. for (i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
  145. req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
  146. }
  147. /*******************************************************************************
  148. * RockChip handler called when a CPU is about to enter standby.
  149. ******************************************************************************/
  150. void rockchip_cpu_standby(plat_local_state_t cpu_state)
  151. {
  152. u_register_t scr;
  153. assert(cpu_state == PLAT_MAX_RET_STATE);
  154. scr = read_scr_el3();
  155. /* Enable PhysicalIRQ bit for NS world to wake the CPU */
  156. write_scr_el3(scr | SCR_IRQ_BIT);
  157. isb();
  158. dsb();
  159. wfi();
  160. /*
  161. * Restore SCR to the original value, synchronisation of scr_el3 is
  162. * done by eret while el3_exit to save some execution cycles.
  163. */
  164. write_scr_el3(scr);
  165. }
  166. /*******************************************************************************
  167. * RockChip handler called when a power domain is about to be turned on. The
  168. * mpidr determines the CPU to be turned on.
  169. ******************************************************************************/
  170. int rockchip_pwr_domain_on(u_register_t mpidr)
  171. {
  172. return rockchip_soc_cores_pwr_dm_on(mpidr, rockchip_sec_entrypoint);
  173. }
  174. /*******************************************************************************
  175. * RockChip handler called when a power domain is about to be turned off. The
  176. * target_state encodes the power state that each level should transition to.
  177. ******************************************************************************/
  178. void rockchip_pwr_domain_off(const psci_power_state_t *target_state)
  179. {
  180. uint32_t lvl;
  181. plat_local_state_t lvl_state;
  182. int ret;
  183. assert(RK_CORE_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE);
  184. plat_rockchip_gic_cpuif_disable();
  185. if (RK_CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE)
  186. plat_cci_disable();
  187. rockchip_soc_cores_pwr_dm_off();
  188. for (lvl = MPIDR_AFFLVL1; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
  189. lvl_state = target_state->pwr_domain_state[lvl];
  190. ret = rockchip_soc_hlvl_pwr_dm_off(lvl, lvl_state);
  191. if (ret == PSCI_E_NOT_SUPPORTED)
  192. break;
  193. }
  194. }
  195. /*******************************************************************************
  196. * RockChip handler called when a power domain is about to be suspended. The
  197. * target_state encodes the power state that each level should transition to.
  198. ******************************************************************************/
  199. void rockchip_pwr_domain_suspend(const psci_power_state_t *target_state)
  200. {
  201. uint32_t lvl;
  202. plat_local_state_t lvl_state;
  203. int ret;
  204. if (RK_CORE_PWR_STATE(target_state) != PLAT_MAX_OFF_STATE)
  205. return;
  206. /* Prevent interrupts from spuriously waking up this cpu */
  207. plat_rockchip_gic_cpuif_disable();
  208. if (RK_SYSTEM_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE)
  209. rockchip_soc_sys_pwr_dm_suspend();
  210. else
  211. rockchip_soc_cores_pwr_dm_suspend();
  212. /* Perform the common cluster specific operations */
  213. if (RK_CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE)
  214. plat_cci_disable();
  215. if (RK_SYSTEM_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE)
  216. return;
  217. for (lvl = MPIDR_AFFLVL1; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
  218. lvl_state = target_state->pwr_domain_state[lvl];
  219. ret = rockchip_soc_hlvl_pwr_dm_suspend(lvl, lvl_state);
  220. if (ret == PSCI_E_NOT_SUPPORTED)
  221. break;
  222. }
  223. }
  224. /*******************************************************************************
  225. * RockChip handler called when a power domain has just been powered on after
  226. * being turned off earlier. The target_state encodes the low power state that
  227. * each level has woken up from.
  228. ******************************************************************************/
  229. void rockchip_pwr_domain_on_finish(const psci_power_state_t *target_state)
  230. {
  231. uint32_t lvl;
  232. plat_local_state_t lvl_state;
  233. int ret;
  234. assert(RK_CORE_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE);
  235. for (lvl = MPIDR_AFFLVL1; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
  236. lvl_state = target_state->pwr_domain_state[lvl];
  237. ret = rockchip_soc_hlvl_pwr_dm_on_finish(lvl, lvl_state);
  238. if (ret == PSCI_E_NOT_SUPPORTED)
  239. break;
  240. }
  241. rockchip_soc_cores_pwr_dm_on_finish();
  242. /* Perform the common cluster specific operations */
  243. if (RK_CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) {
  244. /* Enable coherency if this cluster was off */
  245. plat_cci_enable();
  246. }
  247. /* Enable the gic cpu interface */
  248. plat_rockchip_gic_pcpu_init();
  249. /* Program the gic per-cpu distributor or re-distributor interface */
  250. plat_rockchip_gic_cpuif_enable();
  251. }
  252. /*******************************************************************************
  253. * RockChip handler called when a power domain has just been powered on after
  254. * having been suspended earlier. The target_state encodes the low power state
  255. * that each level has woken up from.
  256. * TODO: At the moment we reuse the on finisher and reinitialize the secure
  257. * context. Need to implement a separate suspend finisher.
  258. ******************************************************************************/
  259. void rockchip_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
  260. {
  261. uint32_t lvl;
  262. plat_local_state_t lvl_state;
  263. int ret;
  264. /* Nothing to be done on waking up from retention from CPU level */
  265. if (RK_CORE_PWR_STATE(target_state) != PLAT_MAX_OFF_STATE)
  266. return;
  267. if (RK_SYSTEM_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) {
  268. rockchip_soc_sys_pwr_dm_resume();
  269. goto comm_finish;
  270. }
  271. for (lvl = MPIDR_AFFLVL1; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
  272. lvl_state = target_state->pwr_domain_state[lvl];
  273. ret = rockchip_soc_hlvl_pwr_dm_resume(lvl, lvl_state);
  274. if (ret == PSCI_E_NOT_SUPPORTED)
  275. break;
  276. }
  277. rockchip_soc_cores_pwr_dm_resume();
  278. /*
  279. * Program the gic per-cpu distributor or re-distributor interface.
  280. * For sys power domain operation, resuming of the gic needs to operate
  281. * in rockchip_soc_sys_pwr_dm_resume(), according to the sys power mode
  282. * implements.
  283. */
  284. plat_rockchip_gic_cpuif_enable();
  285. comm_finish:
  286. /* Perform the common cluster specific operations */
  287. if (RK_CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) {
  288. /* Enable coherency if this cluster was off */
  289. plat_cci_enable();
  290. }
  291. }
  292. /*******************************************************************************
  293. * RockChip handlers to reboot the system
  294. ******************************************************************************/
  295. static void __dead2 rockchip_system_reset(void)
  296. {
  297. rockchip_soc_soft_reset();
  298. }
  299. /*******************************************************************************
  300. * RockChip handlers to power off the system
  301. ******************************************************************************/
  302. static void __dead2 rockchip_system_poweroff(void)
  303. {
  304. rockchip_soc_system_off();
  305. }
  306. static void __dead2 rockchip_pd_pwr_down_wfi(
  307. const psci_power_state_t *target_state)
  308. {
  309. if (RK_SYSTEM_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE)
  310. rockchip_soc_sys_pd_pwr_dn_wfi();
  311. else
  312. rockchip_soc_cores_pd_pwr_dn_wfi(target_state);
  313. }
  314. /*******************************************************************************
  315. * Export the platform handlers via plat_rockchip_psci_pm_ops. The rockchip
  316. * standard
  317. * platform layer will take care of registering the handlers with PSCI.
  318. ******************************************************************************/
  319. const plat_psci_ops_t plat_rockchip_psci_pm_ops = {
  320. .cpu_standby = rockchip_cpu_standby,
  321. .pwr_domain_on = rockchip_pwr_domain_on,
  322. .pwr_domain_off = rockchip_pwr_domain_off,
  323. .pwr_domain_suspend = rockchip_pwr_domain_suspend,
  324. .pwr_domain_on_finish = rockchip_pwr_domain_on_finish,
  325. .pwr_domain_suspend_finish = rockchip_pwr_domain_suspend_finish,
  326. .pwr_domain_pwr_down_wfi = rockchip_pd_pwr_down_wfi,
  327. .system_reset = rockchip_system_reset,
  328. .system_off = rockchip_system_poweroff,
  329. .validate_power_state = rockchip_validate_power_state,
  330. .get_sys_suspend_power_state = rockchip_get_sys_suspend_power_state
  331. };
  332. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  333. const plat_psci_ops_t **psci_ops)
  334. {
  335. *psci_ops = &plat_rockchip_psci_pm_ops;
  336. rockchip_sec_entrypoint = sec_entrypoint;
  337. return 0;
  338. }
  339. uintptr_t plat_get_sec_entrypoint(void)
  340. {
  341. assert(rockchip_sec_entrypoint);
  342. return rockchip_sec_entrypoint;
  343. }