tegra_pm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch_helpers.h>
  7. #include <assert.h>
  8. #include <bl_common.h>
  9. #include <context.h>
  10. #include <context_mgmt.h>
  11. #include <console.h>
  12. #include <debug.h>
  13. #include <memctrl.h>
  14. #include <mmio.h>
  15. #include <platform.h>
  16. #include <platform_def.h>
  17. #include <pmc.h>
  18. #include <psci.h>
  19. #include <tegra_def.h>
  20. #include <tegra_private.h>
  21. extern uint64_t tegra_bl31_phys_base;
  22. extern uint64_t tegra_sec_entry_point;
  23. extern uint64_t tegra_console_base;
  24. /*
  25. * tegra_fake_system_suspend acts as a boolean var controlling whether
  26. * we are going to take fake system suspend code or normal system suspend code
  27. * path. This variable is set inside the sip call handlers,when the kernel
  28. * requests a SIP call to set the suspend debug flags.
  29. */
  30. uint8_t tegra_fake_system_suspend;
  31. /*
  32. * The following platform setup functions are weakly defined. They
  33. * provide typical implementations that will be overridden by a SoC.
  34. */
  35. #pragma weak tegra_soc_pwr_domain_suspend
  36. #pragma weak tegra_soc_pwr_domain_on
  37. #pragma weak tegra_soc_pwr_domain_off
  38. #pragma weak tegra_soc_pwr_domain_on_finish
  39. #pragma weak tegra_soc_pwr_domain_power_down_wfi
  40. #pragma weak tegra_soc_prepare_system_reset
  41. #pragma weak tegra_soc_prepare_system_off
  42. #pragma weak tegra_soc_get_target_pwr_state
  43. int tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state)
  44. {
  45. return PSCI_E_NOT_SUPPORTED;
  46. }
  47. int tegra_soc_pwr_domain_on(u_register_t mpidr)
  48. {
  49. return PSCI_E_SUCCESS;
  50. }
  51. int tegra_soc_pwr_domain_off(const psci_power_state_t *target_state)
  52. {
  53. return PSCI_E_SUCCESS;
  54. }
  55. int tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state)
  56. {
  57. return PSCI_E_SUCCESS;
  58. }
  59. int tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state)
  60. {
  61. return PSCI_E_SUCCESS;
  62. }
  63. int tegra_soc_prepare_system_reset(void)
  64. {
  65. return PSCI_E_SUCCESS;
  66. }
  67. __dead2 void tegra_soc_prepare_system_off(void)
  68. {
  69. ERROR("Tegra System Off: operation not handled.\n");
  70. panic();
  71. }
  72. plat_local_state_t tegra_soc_get_target_pwr_state(unsigned int lvl,
  73. const plat_local_state_t *states,
  74. unsigned int ncpu)
  75. {
  76. plat_local_state_t target = PLAT_MAX_OFF_STATE, temp;
  77. assert(ncpu);
  78. do {
  79. temp = *states++;
  80. if ((temp < target))
  81. target = temp;
  82. } while (--ncpu);
  83. return target;
  84. }
  85. /*******************************************************************************
  86. * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND`
  87. * call to get the `power_state` parameter. This allows the platform to encode
  88. * the appropriate State-ID field within the `power_state` parameter which can
  89. * be utilized in `pwr_domain_suspend()` to suspend to system affinity level.
  90. ******************************************************************************/
  91. void tegra_get_sys_suspend_power_state(psci_power_state_t *req_state)
  92. {
  93. /* all affinities use system suspend state id */
  94. for (uint32_t i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
  95. req_state->pwr_domain_state[i] = PSTATE_ID_SOC_POWERDN;
  96. }
  97. /*******************************************************************************
  98. * Handler called when an affinity instance is about to enter standby.
  99. ******************************************************************************/
  100. void tegra_cpu_standby(plat_local_state_t cpu_state)
  101. {
  102. /*
  103. * Enter standby state
  104. * dsb is good practice before using wfi to enter low power states
  105. */
  106. dsb();
  107. wfi();
  108. }
  109. /*******************************************************************************
  110. * Handler called when an affinity instance is about to be turned on. The
  111. * level and mpidr determine the affinity instance.
  112. ******************************************************************************/
  113. int tegra_pwr_domain_on(u_register_t mpidr)
  114. {
  115. return tegra_soc_pwr_domain_on(mpidr);
  116. }
  117. /*******************************************************************************
  118. * Handler called when a power domain is about to be turned off. The
  119. * target_state encodes the power state that each level should transition to.
  120. ******************************************************************************/
  121. void tegra_pwr_domain_off(const psci_power_state_t *target_state)
  122. {
  123. tegra_soc_pwr_domain_off(target_state);
  124. }
  125. /*******************************************************************************
  126. * Handler called when a power domain is about to be suspended. The
  127. * target_state encodes the power state that each level should transition to.
  128. ******************************************************************************/
  129. void tegra_pwr_domain_suspend(const psci_power_state_t *target_state)
  130. {
  131. tegra_soc_pwr_domain_suspend(target_state);
  132. /* Disable console if we are entering deep sleep. */
  133. if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
  134. PSTATE_ID_SOC_POWERDN)
  135. console_uninit();
  136. /* disable GICC */
  137. tegra_gic_cpuif_deactivate();
  138. }
  139. /*******************************************************************************
  140. * Handler called at the end of the power domain suspend sequence. The
  141. * target_state encodes the power state that each level should transition to.
  142. ******************************************************************************/
  143. __dead2 void tegra_pwr_domain_power_down_wfi(const psci_power_state_t
  144. *target_state)
  145. {
  146. uint8_t pwr_state = target_state->pwr_domain_state[PLAT_MAX_PWR_LVL];
  147. uint64_t rmr_el3 = 0;
  148. /* call the chip's power down handler */
  149. tegra_soc_pwr_domain_power_down_wfi(target_state);
  150. /*
  151. * If we are in fake system suspend mode, ensure we start doing
  152. * procedures that help in looping back towards system suspend exit
  153. * instead of calling WFI by requesting a warm reset.
  154. * Else, just call WFI to enter low power state.
  155. */
  156. if ((tegra_fake_system_suspend != 0U) &&
  157. (pwr_state == (uint8_t)PSTATE_ID_SOC_POWERDN)) {
  158. /* warm reboot */
  159. rmr_el3 = read_rmr_el3();
  160. write_rmr_el3(rmr_el3 | RMR_WARM_RESET_CPU);
  161. } else {
  162. /* enter power down state */
  163. wfi();
  164. }
  165. /* we can never reach here */
  166. panic();
  167. }
  168. /*******************************************************************************
  169. * Handler called when a power domain has just been powered on after
  170. * being turned off earlier. The target_state encodes the low power state that
  171. * each level has woken up from.
  172. ******************************************************************************/
  173. void tegra_pwr_domain_on_finish(const psci_power_state_t *target_state)
  174. {
  175. plat_params_from_bl2_t *plat_params;
  176. /*
  177. * Initialize the GIC cpu and distributor interfaces
  178. */
  179. plat_gic_setup();
  180. /*
  181. * Check if we are exiting from deep sleep.
  182. */
  183. if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
  184. PSTATE_ID_SOC_POWERDN) {
  185. /* Initialize the runtime console */
  186. if (tegra_console_base != (uint64_t)0) {
  187. console_init(tegra_console_base, TEGRA_BOOT_UART_CLK_IN_HZ,
  188. TEGRA_CONSOLE_BAUDRATE);
  189. }
  190. /*
  191. * Restore Memory Controller settings as it loses state
  192. * during system suspend.
  193. */
  194. tegra_memctrl_restore_settings();
  195. /*
  196. * Security configuration to allow DRAM/device access.
  197. */
  198. plat_params = bl31_get_plat_params();
  199. tegra_memctrl_tzdram_setup(plat_params->tzdram_base,
  200. plat_params->tzdram_size);
  201. /*
  202. * Set up the TZRAM memory aperture to allow only secure world
  203. * access
  204. */
  205. tegra_memctrl_tzram_setup(TEGRA_TZRAM_BASE, TEGRA_TZRAM_SIZE);
  206. }
  207. /*
  208. * Reset hardware settings.
  209. */
  210. tegra_soc_pwr_domain_on_finish(target_state);
  211. }
  212. /*******************************************************************************
  213. * Handler called when a power domain has just been powered on after
  214. * having been suspended earlier. The target_state encodes the low power state
  215. * that each level has woken up from.
  216. ******************************************************************************/
  217. void tegra_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
  218. {
  219. tegra_pwr_domain_on_finish(target_state);
  220. }
  221. /*******************************************************************************
  222. * Handler called when the system wants to be powered off
  223. ******************************************************************************/
  224. __dead2 void tegra_system_off(void)
  225. {
  226. INFO("Powering down system...\n");
  227. tegra_soc_prepare_system_off();
  228. }
  229. /*******************************************************************************
  230. * Handler called when the system wants to be restarted.
  231. ******************************************************************************/
  232. __dead2 void tegra_system_reset(void)
  233. {
  234. INFO("Restarting system...\n");
  235. /* per-SoC system reset handler */
  236. tegra_soc_prepare_system_reset();
  237. /*
  238. * Program the PMC in order to restart the system.
  239. */
  240. tegra_pmc_system_reset();
  241. }
  242. /*******************************************************************************
  243. * Handler called to check the validity of the power state parameter.
  244. ******************************************************************************/
  245. int32_t tegra_validate_power_state(unsigned int power_state,
  246. psci_power_state_t *req_state)
  247. {
  248. assert(req_state);
  249. return tegra_soc_validate_power_state(power_state, req_state);
  250. }
  251. /*******************************************************************************
  252. * Platform handler called to check the validity of the non secure entrypoint.
  253. ******************************************************************************/
  254. int tegra_validate_ns_entrypoint(uintptr_t entrypoint)
  255. {
  256. /*
  257. * Check if the non secure entrypoint lies within the non
  258. * secure DRAM.
  259. */
  260. if ((entrypoint >= TEGRA_DRAM_BASE) && (entrypoint <= TEGRA_DRAM_END))
  261. return PSCI_E_SUCCESS;
  262. return PSCI_E_INVALID_ADDRESS;
  263. }
  264. /*******************************************************************************
  265. * Export the platform handlers to enable psci to invoke them
  266. ******************************************************************************/
  267. static const plat_psci_ops_t tegra_plat_psci_ops = {
  268. .cpu_standby = tegra_cpu_standby,
  269. .pwr_domain_on = tegra_pwr_domain_on,
  270. .pwr_domain_off = tegra_pwr_domain_off,
  271. .pwr_domain_suspend = tegra_pwr_domain_suspend,
  272. .pwr_domain_on_finish = tegra_pwr_domain_on_finish,
  273. .pwr_domain_suspend_finish = tegra_pwr_domain_suspend_finish,
  274. .pwr_domain_pwr_down_wfi = tegra_pwr_domain_power_down_wfi,
  275. .system_off = tegra_system_off,
  276. .system_reset = tegra_system_reset,
  277. .validate_power_state = tegra_validate_power_state,
  278. .validate_ns_entrypoint = tegra_validate_ns_entrypoint,
  279. .get_sys_suspend_power_state = tegra_get_sys_suspend_power_state,
  280. };
  281. /*******************************************************************************
  282. * Export the platform specific power ops and initialize Power Controller
  283. ******************************************************************************/
  284. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  285. const plat_psci_ops_t **psci_ops)
  286. {
  287. psci_power_state_t target_state = { { PSCI_LOCAL_STATE_RUN } };
  288. /*
  289. * Flush entrypoint variable to PoC since it will be
  290. * accessed after a reset with the caches turned off.
  291. */
  292. tegra_sec_entry_point = sec_entrypoint;
  293. flush_dcache_range((uint64_t)&tegra_sec_entry_point, sizeof(uint64_t));
  294. /*
  295. * Reset hardware settings.
  296. */
  297. tegra_soc_pwr_domain_on_finish(&target_state);
  298. /*
  299. * Initialize PSCI ops struct
  300. */
  301. *psci_ops = &tegra_plat_psci_ops;
  302. return 0;
  303. }
  304. /*******************************************************************************
  305. * Platform handler to calculate the proper target power level at the
  306. * specified affinity level
  307. ******************************************************************************/
  308. plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
  309. const plat_local_state_t *states,
  310. unsigned int ncpu)
  311. {
  312. return tegra_soc_get_target_pwr_state(lvl, states, ncpu);
  313. }