k3_psci.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdbool.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <lib/el3_runtime/cpu_data.h>
  11. #include <lib/psci/psci.h>
  12. #include <plat/common/platform.h>
  13. #include <ti_sci_protocol.h>
  14. #include <k3_gicv3.h>
  15. #include <ti_sci.h>
  16. #define CORE_PWR_STATE(state) ((state)->pwr_domain_state[MPIDR_AFFLVL0])
  17. #define CLUSTER_PWR_STATE(state) ((state)->pwr_domain_state[MPIDR_AFFLVL1])
  18. #define SYSTEM_PWR_STATE(state) ((state)->pwr_domain_state[PLAT_MAX_PWR_LVL])
  19. uintptr_t k3_sec_entrypoint;
  20. static void k3_cpu_standby(plat_local_state_t cpu_state)
  21. {
  22. u_register_t scr;
  23. scr = read_scr_el3();
  24. /* Enable the Non secure interrupt to wake the CPU */
  25. write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
  26. isb();
  27. /* dsb is good practice before using wfi to enter low power states */
  28. dsb();
  29. /* Enter standby state */
  30. wfi();
  31. /* Restore SCR */
  32. write_scr_el3(scr);
  33. }
  34. static int k3_pwr_domain_on(u_register_t mpidr)
  35. {
  36. int core, proc_id, device_id, ret;
  37. core = plat_core_pos_by_mpidr(mpidr);
  38. if (core < 0) {
  39. ERROR("Could not get target core id: %d\n", core);
  40. return PSCI_E_INTERN_FAIL;
  41. }
  42. proc_id = PLAT_PROC_START_ID + core;
  43. device_id = PLAT_PROC_DEVICE_START_ID + core;
  44. ret = ti_sci_proc_request(proc_id);
  45. if (ret) {
  46. ERROR("Request for processor failed: %d\n", ret);
  47. return PSCI_E_INTERN_FAIL;
  48. }
  49. ret = ti_sci_proc_set_boot_cfg(proc_id, k3_sec_entrypoint, 0, 0);
  50. if (ret) {
  51. ERROR("Request to set core boot address failed: %d\n", ret);
  52. return PSCI_E_INTERN_FAIL;
  53. }
  54. /* sanity check these are off before starting a core */
  55. ret = ti_sci_proc_set_boot_ctrl(proc_id,
  56. 0, PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ |
  57. PROC_BOOT_CTRL_FLAG_ARMV8_AINACTS |
  58. PROC_BOOT_CTRL_FLAG_ARMV8_ACINACTM);
  59. if (ret) {
  60. ERROR("Request to clear boot configuration failed: %d\n", ret);
  61. return PSCI_E_INTERN_FAIL;
  62. }
  63. ret = ti_sci_device_get(device_id);
  64. if (ret) {
  65. ERROR("Request to start core failed: %d\n", ret);
  66. return PSCI_E_INTERN_FAIL;
  67. }
  68. return PSCI_E_SUCCESS;
  69. }
  70. void k3_pwr_domain_off(const psci_power_state_t *target_state)
  71. {
  72. int core, cluster, proc_id, device_id, cluster_id, ret;
  73. /* At very least the local core should be powering down */
  74. assert(CORE_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE);
  75. /* Prevent interrupts from spuriously waking up this cpu */
  76. k3_gic_cpuif_disable();
  77. core = plat_my_core_pos();
  78. cluster = MPIDR_AFFLVL1_VAL(read_mpidr_el1());
  79. proc_id = PLAT_PROC_START_ID + core;
  80. device_id = PLAT_PROC_DEVICE_START_ID + core;
  81. cluster_id = PLAT_CLUSTER_DEVICE_START_ID + (cluster * 2);
  82. /*
  83. * If we are the last core in the cluster then we take a reference to
  84. * the cluster device so that it does not get shutdown before we
  85. * execute the entire cluster L2 cleaning sequence below.
  86. */
  87. if (CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) {
  88. ret = ti_sci_device_get(cluster_id);
  89. if (ret) {
  90. ERROR("Request to get cluster failed: %d\n", ret);
  91. return;
  92. }
  93. }
  94. /* Start by sending wait for WFI command */
  95. ret = ti_sci_proc_wait_boot_status_no_wait(proc_id,
  96. /*
  97. * Wait maximum time to give us the best chance to get
  98. * to WFI before this command timeouts
  99. */
  100. UINT8_MAX, 100, UINT8_MAX, UINT8_MAX,
  101. /* Wait for WFI */
  102. PROC_BOOT_STATUS_FLAG_ARMV8_WFI, 0, 0, 0);
  103. if (ret) {
  104. ERROR("Sending wait for WFI failed (%d)\n", ret);
  105. return;
  106. }
  107. /* Now queue up the core shutdown request */
  108. ret = ti_sci_device_put_no_wait(device_id);
  109. if (ret) {
  110. ERROR("Sending core shutdown message failed (%d)\n", ret);
  111. return;
  112. }
  113. /* If our cluster is not going down we stop here */
  114. if (CLUSTER_PWR_STATE(target_state) != PLAT_MAX_OFF_STATE)
  115. return;
  116. /* set AINACTS */
  117. ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id,
  118. PROC_BOOT_CTRL_FLAG_ARMV8_AINACTS, 0);
  119. if (ret) {
  120. ERROR("Sending set control message failed (%d)\n", ret);
  121. return;
  122. }
  123. /* set L2FLUSHREQ */
  124. ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id,
  125. PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ, 0);
  126. if (ret) {
  127. ERROR("Sending set control message failed (%d)\n", ret);
  128. return;
  129. }
  130. /* wait for L2FLUSHDONE*/
  131. ret = ti_sci_proc_wait_boot_status_no_wait(proc_id,
  132. UINT8_MAX, 2, UINT8_MAX, UINT8_MAX,
  133. PROC_BOOT_STATUS_FLAG_ARMV8_L2F_DONE, 0, 0, 0);
  134. if (ret) {
  135. ERROR("Sending wait message failed (%d)\n", ret);
  136. return;
  137. }
  138. /* clear L2FLUSHREQ */
  139. ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id,
  140. 0, PROC_BOOT_CTRL_FLAG_ARMV8_L2FLUSHREQ);
  141. if (ret) {
  142. ERROR("Sending set control message failed (%d)\n", ret);
  143. return;
  144. }
  145. /* set ACINACTM */
  146. ret = ti_sci_proc_set_boot_ctrl_no_wait(proc_id,
  147. PROC_BOOT_CTRL_FLAG_ARMV8_ACINACTM, 0);
  148. if (ret) {
  149. ERROR("Sending set control message failed (%d)\n", ret);
  150. return;
  151. }
  152. /* wait for STANDBYWFIL2 */
  153. ret = ti_sci_proc_wait_boot_status_no_wait(proc_id,
  154. UINT8_MAX, 2, UINT8_MAX, UINT8_MAX,
  155. PROC_BOOT_STATUS_FLAG_ARMV8_STANDBYWFIL2, 0, 0, 0);
  156. if (ret) {
  157. ERROR("Sending wait message failed (%d)\n", ret);
  158. return;
  159. }
  160. /* Now queue up the cluster shutdown request */
  161. ret = ti_sci_device_put_no_wait(cluster_id);
  162. if (ret) {
  163. ERROR("Sending cluster shutdown message failed (%d)\n", ret);
  164. return;
  165. }
  166. }
  167. void k3_pwr_domain_on_finish(const psci_power_state_t *target_state)
  168. {
  169. /* TODO: Indicate to System firmware about completion */
  170. k3_gic_pcpu_init();
  171. k3_gic_cpuif_enable();
  172. }
  173. static void __dead2 k3_system_off(void)
  174. {
  175. int ret;
  176. /* Queue up the system shutdown request */
  177. ret = ti_sci_device_put_no_wait(PLAT_BOARD_DEVICE_ID);
  178. if (ret != 0) {
  179. ERROR("Sending system shutdown message failed (%d)\n", ret);
  180. }
  181. while (true)
  182. wfi();
  183. }
  184. static void __dead2 k3_system_reset(void)
  185. {
  186. /* Send the system reset request to system firmware */
  187. ti_sci_core_reboot();
  188. while (true)
  189. wfi();
  190. }
  191. static int k3_validate_power_state(unsigned int power_state,
  192. psci_power_state_t *req_state)
  193. {
  194. /* TODO: perform the proper validation */
  195. return PSCI_E_SUCCESS;
  196. }
  197. static void k3_pwr_domain_suspend_to_mode(const psci_power_state_t *target_state, uint8_t mode)
  198. {
  199. unsigned int core, proc_id;
  200. core = plat_my_core_pos();
  201. proc_id = PLAT_PROC_START_ID + core;
  202. /* Prevent interrupts from spuriously waking up this cpu */
  203. k3_gic_cpuif_disable();
  204. k3_gic_save_context();
  205. k3_pwr_domain_off(target_state);
  206. ti_sci_enter_sleep(proc_id, mode, k3_sec_entrypoint);
  207. }
  208. static void k3_pwr_domain_suspend_dm_managed(const psci_power_state_t *target_state)
  209. {
  210. uint8_t mode = MSG_VALUE_SLEEP_MODE_DEEP_SLEEP;
  211. int ret;
  212. ret = ti_sci_lpm_get_next_sys_mode(&mode);
  213. if (ret != 0) {
  214. ERROR("Failed to fetch next system mode\n");
  215. }
  216. k3_pwr_domain_suspend_to_mode(target_state, mode);
  217. }
  218. static void k3_pwr_domain_suspend(const psci_power_state_t *target_state)
  219. {
  220. k3_pwr_domain_suspend_to_mode(target_state, MSG_VALUE_SLEEP_MODE_DEEP_SLEEP);
  221. }
  222. static void k3_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
  223. {
  224. k3_gic_restore_context();
  225. k3_gic_cpuif_enable();
  226. }
  227. static void k3_get_sys_suspend_power_state(psci_power_state_t *req_state)
  228. {
  229. unsigned int i;
  230. /* CPU & cluster off, system in retention */
  231. for (i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++) {
  232. req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
  233. }
  234. }
  235. static plat_psci_ops_t k3_plat_psci_ops = {
  236. .cpu_standby = k3_cpu_standby,
  237. .pwr_domain_on = k3_pwr_domain_on,
  238. .pwr_domain_off = k3_pwr_domain_off,
  239. .pwr_domain_on_finish = k3_pwr_domain_on_finish,
  240. .pwr_domain_suspend = k3_pwr_domain_suspend,
  241. .pwr_domain_suspend_finish = k3_pwr_domain_suspend_finish,
  242. .get_sys_suspend_power_state = k3_get_sys_suspend_power_state,
  243. .system_off = k3_system_off,
  244. .system_reset = k3_system_reset,
  245. .validate_power_state = k3_validate_power_state,
  246. };
  247. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  248. const plat_psci_ops_t **psci_ops)
  249. {
  250. uint64_t fw_caps = 0;
  251. int ret;
  252. k3_sec_entrypoint = sec_entrypoint;
  253. ret = ti_sci_query_fw_caps(&fw_caps);
  254. if (ret) {
  255. ERROR("Unable to query firmware capabilities (%d)\n", ret);
  256. }
  257. /* If firmware does not support any known suspend mode */
  258. if (!(fw_caps & (MSG_FLAG_CAPS_LPM_DEEP_SLEEP |
  259. MSG_FLAG_CAPS_LPM_MCU_ONLY |
  260. MSG_FLAG_CAPS_LPM_STANDBY |
  261. MSG_FLAG_CAPS_LPM_PARTIAL_IO))) {
  262. /* Disable PSCI suspend support */
  263. k3_plat_psci_ops.pwr_domain_suspend = NULL;
  264. k3_plat_psci_ops.pwr_domain_suspend_finish = NULL;
  265. k3_plat_psci_ops.get_sys_suspend_power_state = NULL;
  266. } else if (fw_caps & MSG_FLAG_CAPS_LPM_DM_MANAGED) {
  267. k3_plat_psci_ops.pwr_domain_suspend = k3_pwr_domain_suspend_dm_managed;
  268. }
  269. *psci_ops = &k3_plat_psci_ops;
  270. return 0;
  271. }