tegra_pm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2020-2023, NVIDIA Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <platform_def.h>
  9. #include <arch_helpers.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <context.h>
  13. #include <drivers/console.h>
  14. #include <lib/el3_runtime/context_mgmt.h>
  15. #include <lib/mmio.h>
  16. #include <lib/psci/psci.h>
  17. #include <plat/common/platform.h>
  18. #include <memctrl.h>
  19. #include <pmc.h>
  20. #include <tegra_def.h>
  21. #include <tegra_platform.h>
  22. #include <tegra_private.h>
  23. extern uint64_t tegra_bl31_phys_base;
  24. extern uint64_t tegra_sec_entry_point;
  25. /*******************************************************************************
  26. * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND`
  27. * call to get the `power_state` parameter. This allows the platform to encode
  28. * the appropriate State-ID field within the `power_state` parameter which can
  29. * be utilized in `pwr_domain_suspend()` to suspend to system affinity level.
  30. ******************************************************************************/
  31. static void tegra_get_sys_suspend_power_state(psci_power_state_t *req_state)
  32. {
  33. /* all affinities use system suspend state id */
  34. for (uint32_t i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++) {
  35. req_state->pwr_domain_state[i] = PSTATE_ID_SOC_POWERDN;
  36. }
  37. }
  38. /*******************************************************************************
  39. * Handler called when an affinity instance is about to enter standby.
  40. ******************************************************************************/
  41. static void tegra_cpu_standby(plat_local_state_t cpu_state)
  42. {
  43. u_register_t saved_scr_el3;
  44. (void)cpu_state;
  45. /* Tegra SoC specific handler */
  46. if (tegra_soc_cpu_standby(cpu_state) != PSCI_E_SUCCESS)
  47. ERROR("%s failed\n", __func__);
  48. saved_scr_el3 = read_scr_el3();
  49. /*
  50. * As per ARM ARM D1.17.2, any physical IRQ interrupt received by the
  51. * PE will be treated as a wake-up event, if SCR_EL3.IRQ is set to '1',
  52. * irrespective of the value of the PSTATE.I bit value.
  53. */
  54. write_scr_el3(saved_scr_el3 | SCR_IRQ_BIT);
  55. /*
  56. * Enter standby state
  57. *
  58. * dsb & isb is good practice before using wfi to enter low power states
  59. */
  60. dsb();
  61. isb();
  62. wfi();
  63. /*
  64. * Restore saved scr_el3 that has IRQ bit cleared as we don't want EL3
  65. * handling any further interrupts
  66. */
  67. write_scr_el3(saved_scr_el3);
  68. }
  69. /*******************************************************************************
  70. * Handler called when an affinity instance is about to be turned on. The
  71. * level and mpidr determine the affinity instance.
  72. ******************************************************************************/
  73. static int32_t tegra_pwr_domain_on(u_register_t mpidr)
  74. {
  75. return tegra_soc_pwr_domain_on(mpidr);
  76. }
  77. /*******************************************************************************
  78. * Handler called when a power domain is about to be turned off. The
  79. * target_state encodes the power state that each level should transition to.
  80. * Return error if CPU off sequence is not allowed for the current core.
  81. ******************************************************************************/
  82. static int tegra_pwr_domain_off_early(const psci_power_state_t *target_state)
  83. {
  84. return tegra_soc_pwr_domain_off_early(target_state);
  85. }
  86. /*******************************************************************************
  87. * Handler called when a power domain is about to be turned off. The
  88. * target_state encodes the power state that each level should transition to.
  89. ******************************************************************************/
  90. static void tegra_pwr_domain_off(const psci_power_state_t *target_state)
  91. {
  92. (void)tegra_soc_pwr_domain_off(target_state);
  93. /* disable GICC */
  94. tegra_gic_cpuif_deactivate();
  95. }
  96. /*******************************************************************************
  97. * Handler called when a power domain is about to be suspended. The
  98. * target_state encodes the power state that each level should transition to.
  99. * This handler is called with SMP and data cache enabled, when
  100. * HW_ASSISTED_COHERENCY = 0
  101. ******************************************************************************/
  102. void tegra_pwr_domain_suspend_pwrdown_early(const psci_power_state_t *target_state)
  103. {
  104. tegra_soc_pwr_domain_suspend_pwrdown_early(target_state);
  105. }
  106. /*******************************************************************************
  107. * Handler called when a power domain is about to be suspended. The
  108. * target_state encodes the power state that each level should transition to.
  109. ******************************************************************************/
  110. static void tegra_pwr_domain_suspend(const psci_power_state_t *target_state)
  111. {
  112. (void)tegra_soc_pwr_domain_suspend(target_state);
  113. /* disable GICC */
  114. tegra_gic_cpuif_deactivate();
  115. }
  116. /*******************************************************************************
  117. * Handler called at the end of the power domain suspend sequence. The
  118. * target_state encodes the power state that each level should transition to.
  119. ******************************************************************************/
  120. static __dead2 void tegra_pwr_domain_power_down_wfi(const psci_power_state_t
  121. *target_state)
  122. {
  123. /* call the chip's power down handler */
  124. (void)tegra_soc_pwr_domain_power_down_wfi(target_state);
  125. /* Disable console if we are entering deep sleep. */
  126. if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
  127. PSTATE_ID_SOC_POWERDN) {
  128. INFO("%s: complete. Entering System Suspend...\n", __func__);
  129. console_flush();
  130. console_switch_state(0);
  131. }
  132. wfi();
  133. panic();
  134. }
  135. /*******************************************************************************
  136. * Handler called when a power domain has just been powered on after
  137. * being turned off earlier. The target_state encodes the low power state that
  138. * each level has woken up from.
  139. ******************************************************************************/
  140. static void tegra_pwr_domain_on_finish(const psci_power_state_t *target_state)
  141. {
  142. const plat_params_from_bl2_t *plat_params;
  143. /*
  144. * Check if we are exiting from deep sleep.
  145. */
  146. if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
  147. PSTATE_ID_SOC_POWERDN) {
  148. /*
  149. * On entering System Suspend state, the GIC loses power
  150. * completely. Initialize the GIC global distributor and
  151. * GIC cpu interfaces.
  152. */
  153. tegra_gic_init();
  154. /* Restart console output. */
  155. console_switch_state(CONSOLE_FLAG_RUNTIME);
  156. /*
  157. * Restore Memory Controller settings as it loses state
  158. * during system suspend.
  159. */
  160. tegra_memctrl_restore_settings();
  161. /*
  162. * Security configuration to allow DRAM/device access.
  163. */
  164. plat_params = bl31_get_plat_params();
  165. tegra_memctrl_tzdram_setup(plat_params->tzdram_base,
  166. (uint32_t)plat_params->tzdram_size);
  167. } else {
  168. /*
  169. * Initialize the GIC cpu and distributor interfaces
  170. */
  171. tegra_gic_pcpu_init();
  172. }
  173. /*
  174. * Reset hardware settings.
  175. */
  176. (void)tegra_soc_pwr_domain_on_finish(target_state);
  177. }
  178. /*******************************************************************************
  179. * Handler called when a power domain has just been powered on after
  180. * having been suspended earlier. The target_state encodes the low power state
  181. * that each level has woken up from.
  182. ******************************************************************************/
  183. static void tegra_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
  184. {
  185. tegra_pwr_domain_on_finish(target_state);
  186. }
  187. /*******************************************************************************
  188. * Handler called when the system wants to be powered off
  189. ******************************************************************************/
  190. static __dead2 void tegra_system_off(void)
  191. {
  192. INFO("Powering down system...\n");
  193. tegra_soc_prepare_system_off();
  194. }
  195. /*******************************************************************************
  196. * Handler called when the system wants to be restarted.
  197. ******************************************************************************/
  198. static __dead2 void tegra_system_reset(void)
  199. {
  200. INFO("Restarting system...\n");
  201. /* per-SoC system reset handler */
  202. (void)tegra_soc_prepare_system_reset();
  203. /* wait for the system to reset */
  204. for (;;) {
  205. ;
  206. }
  207. }
  208. /*******************************************************************************
  209. * Handler called to check the validity of the power state parameter.
  210. ******************************************************************************/
  211. static int32_t tegra_validate_power_state(uint32_t power_state,
  212. psci_power_state_t *req_state)
  213. {
  214. assert(req_state != NULL);
  215. return tegra_soc_validate_power_state(power_state, req_state);
  216. }
  217. /*******************************************************************************
  218. * Platform handler called to check the validity of the non secure entrypoint.
  219. ******************************************************************************/
  220. static int32_t tegra_validate_ns_entrypoint(uintptr_t entrypoint)
  221. {
  222. int32_t ret = PSCI_E_INVALID_ADDRESS;
  223. /*
  224. * Check if the non secure entrypoint lies within the non
  225. * secure DRAM.
  226. */
  227. if ((entrypoint >= TEGRA_DRAM_BASE) && (entrypoint <= TEGRA_DRAM_END)) {
  228. ret = PSCI_E_SUCCESS;
  229. }
  230. return ret;
  231. }
  232. /*******************************************************************************
  233. * Export the platform handlers to enable psci to invoke them
  234. ******************************************************************************/
  235. static plat_psci_ops_t tegra_plat_psci_ops = {
  236. .cpu_standby = tegra_cpu_standby,
  237. .pwr_domain_on = tegra_pwr_domain_on,
  238. .pwr_domain_off_early = tegra_pwr_domain_off_early,
  239. .pwr_domain_off = tegra_pwr_domain_off,
  240. .pwr_domain_suspend_pwrdown_early = tegra_pwr_domain_suspend_pwrdown_early,
  241. .pwr_domain_suspend = tegra_pwr_domain_suspend,
  242. .pwr_domain_on_finish = tegra_pwr_domain_on_finish,
  243. .pwr_domain_suspend_finish = tegra_pwr_domain_suspend_finish,
  244. .pwr_domain_pwr_down_wfi = tegra_pwr_domain_power_down_wfi,
  245. .system_off = tegra_system_off,
  246. .system_reset = tegra_system_reset,
  247. .validate_power_state = tegra_validate_power_state,
  248. .validate_ns_entrypoint = tegra_validate_ns_entrypoint,
  249. .get_sys_suspend_power_state = tegra_get_sys_suspend_power_state,
  250. };
  251. /*******************************************************************************
  252. * Export the platform specific power ops and initialize Power Controller
  253. ******************************************************************************/
  254. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  255. const plat_psci_ops_t **psci_ops)
  256. {
  257. psci_power_state_t target_state = { { PSCI_LOCAL_STATE_RUN } };
  258. /*
  259. * Flush entrypoint variable to PoC since it will be
  260. * accessed after a reset with the caches turned off.
  261. */
  262. tegra_sec_entry_point = sec_entrypoint;
  263. flush_dcache_range((uint64_t)&tegra_sec_entry_point, sizeof(uint64_t));
  264. /*
  265. * Reset hardware settings.
  266. */
  267. (void)tegra_soc_pwr_domain_on_finish(&target_state);
  268. /*
  269. * Disable System Suspend if the platform does not
  270. * support it
  271. */
  272. if (!plat_supports_system_suspend()) {
  273. tegra_plat_psci_ops.get_sys_suspend_power_state = NULL;
  274. }
  275. /*
  276. * Initialize PSCI ops struct
  277. */
  278. *psci_ops = &tegra_plat_psci_ops;
  279. return 0;
  280. }
  281. /*******************************************************************************
  282. * Platform handler to calculate the proper target power level at the
  283. * specified affinity level
  284. ******************************************************************************/
  285. plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
  286. const plat_local_state_t *states,
  287. unsigned int ncpu)
  288. {
  289. return tegra_soc_get_target_pwr_state(lvl, states, ncpu);
  290. }