plat_psci_handlers.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <stdbool.h>
  9. #include <string.h>
  10. #include <arch.h>
  11. #include <arch_helpers.h>
  12. #include <common/bl_common.h>
  13. #include <common/debug.h>
  14. #include <context.h>
  15. #include <cortex_a57.h>
  16. #include <denver.h>
  17. #include <lib/el3_runtime/context_mgmt.h>
  18. #include <lib/psci/psci.h>
  19. #include <plat/common/platform.h>
  20. #include <bpmp_ipc.h>
  21. #include <mce.h>
  22. #include <memctrl_v2.h>
  23. #include <security_engine.h>
  24. #include <smmu.h>
  25. #include <t18x_ari.h>
  26. #include <tegra186_private.h>
  27. #include <tegra_private.h>
  28. extern void memcpy16(void *dest, const void *src, unsigned int length);
  29. /* state id mask */
  30. #define TEGRA186_STATE_ID_MASK 0xFU
  31. /* constants to get power state's wake time */
  32. #define TEGRA186_WAKE_TIME_MASK 0x0FFFFFF0U
  33. #define TEGRA186_WAKE_TIME_SHIFT 4U
  34. /* default core wake mask for CPU_SUSPEND */
  35. #define TEGRA186_CORE_WAKE_MASK 0x180cU
  36. /* context size to save during system suspend */
  37. #define TEGRA186_SE_CONTEXT_SIZE 3U
  38. static uint32_t se_regs[TEGRA186_SE_CONTEXT_SIZE];
  39. static struct tegra_psci_percpu_data {
  40. uint32_t wake_time;
  41. } __aligned(CACHE_WRITEBACK_GRANULE) tegra_percpu_data[PLATFORM_CORE_COUNT];
  42. int32_t tegra_soc_validate_power_state(uint32_t power_state,
  43. psci_power_state_t *req_state)
  44. {
  45. uint8_t state_id = (uint8_t)psci_get_pstate_id(power_state) & TEGRA186_STATE_ID_MASK;
  46. uint32_t cpu = plat_my_core_pos();
  47. int32_t ret = PSCI_E_SUCCESS;
  48. /* save the core wake time (in TSC ticks)*/
  49. tegra_percpu_data[cpu].wake_time = (power_state & TEGRA186_WAKE_TIME_MASK)
  50. << TEGRA186_WAKE_TIME_SHIFT;
  51. /*
  52. * Clean percpu_data[cpu] to DRAM. This needs to be done to ensure that
  53. * the correct value is read in tegra_soc_pwr_domain_suspend(), which
  54. * is called with caches disabled. It is possible to read a stale value
  55. * from DRAM in that function, because the L2 cache is not flushed
  56. * unless the cluster is entering CC6/CC7.
  57. */
  58. clean_dcache_range((uint64_t)&tegra_percpu_data[cpu],
  59. sizeof(tegra_percpu_data[cpu]));
  60. /* Sanity check the requested state id */
  61. switch (state_id) {
  62. case PSTATE_ID_CORE_IDLE:
  63. case PSTATE_ID_CORE_POWERDN:
  64. if (psci_get_pstate_type(power_state) != PSTATE_TYPE_POWERDOWN) {
  65. ret = PSCI_E_INVALID_PARAMS;
  66. break;
  67. }
  68. /* Core powerdown request */
  69. req_state->pwr_domain_state[MPIDR_AFFLVL0] = state_id;
  70. req_state->pwr_domain_state[MPIDR_AFFLVL1] = state_id;
  71. break;
  72. default:
  73. ERROR("%s: unsupported state id (%d)\n", __func__, state_id);
  74. ret = PSCI_E_INVALID_PARAMS;
  75. break;
  76. }
  77. return ret;
  78. }
  79. int32_t tegra_soc_cpu_standby(plat_local_state_t cpu_state)
  80. {
  81. (void)cpu_state;
  82. return PSCI_E_SUCCESS;
  83. }
  84. int32_t tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state)
  85. {
  86. const plat_local_state_t *pwr_domain_state;
  87. uint8_t stateid_afflvl0, stateid_afflvl2;
  88. uint32_t cpu = plat_my_core_pos();
  89. const plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
  90. mce_cstate_info_t cstate_info = { 0 };
  91. uint64_t mc_ctx_base;
  92. uint32_t val;
  93. /* get the state ID */
  94. pwr_domain_state = target_state->pwr_domain_state;
  95. stateid_afflvl0 = pwr_domain_state[MPIDR_AFFLVL0] &
  96. TEGRA186_STATE_ID_MASK;
  97. stateid_afflvl2 = pwr_domain_state[PLAT_MAX_PWR_LVL] &
  98. TEGRA186_STATE_ID_MASK;
  99. if ((stateid_afflvl0 == PSTATE_ID_CORE_IDLE) ||
  100. (stateid_afflvl0 == PSTATE_ID_CORE_POWERDN)) {
  101. /* Enter CPU idle/powerdown */
  102. val = (stateid_afflvl0 == PSTATE_ID_CORE_IDLE) ?
  103. (uint32_t)TEGRA_ARI_CORE_C6 : (uint32_t)TEGRA_ARI_CORE_C7;
  104. (void)mce_command_handler((uint64_t)MCE_CMD_ENTER_CSTATE, (uint64_t)val,
  105. tegra_percpu_data[cpu].wake_time, 0U);
  106. } else if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) {
  107. /* save SE registers */
  108. se_regs[0] = mmio_read_32(TEGRA_SE0_BASE +
  109. SE_MUTEX_WATCHDOG_NS_LIMIT);
  110. se_regs[1] = mmio_read_32(TEGRA_RNG1_BASE +
  111. RNG_MUTEX_WATCHDOG_NS_LIMIT);
  112. se_regs[2] = mmio_read_32(TEGRA_PKA1_BASE +
  113. PKA_MUTEX_WATCHDOG_NS_LIMIT);
  114. /* save 'Secure Boot' Processor Feature Config Register */
  115. val = mmio_read_32(TEGRA_MISC_BASE + MISCREG_PFCFG);
  116. mmio_write_32(TEGRA_SCRATCH_BASE + SCRATCH_SECURE_BOOTP_FCFG, val);
  117. /* save MC context to TZDRAM */
  118. mc_ctx_base = params_from_bl2->tzdram_base;
  119. tegra_mc_save_context((uintptr_t)mc_ctx_base);
  120. /* Prepare for system suspend */
  121. cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC7;
  122. cstate_info.system = (uint32_t)TEGRA_ARI_SYSTEM_SC7;
  123. cstate_info.system_state_force = 1;
  124. cstate_info.update_wake_mask = 1;
  125. mce_update_cstate_info(&cstate_info);
  126. /* Loop until system suspend is allowed */
  127. do {
  128. val = (uint32_t)mce_command_handler(
  129. (uint64_t)MCE_CMD_IS_SC7_ALLOWED,
  130. (uint64_t)TEGRA_ARI_CORE_C7,
  131. MCE_CORE_SLEEP_TIME_INFINITE,
  132. 0U);
  133. } while (val == 0U);
  134. /* Instruct the MCE to enter system suspend state */
  135. (void)mce_command_handler((uint64_t)MCE_CMD_ENTER_CSTATE,
  136. (uint64_t)TEGRA_ARI_CORE_C7, MCE_CORE_SLEEP_TIME_INFINITE, 0U);
  137. } else {
  138. ; /* do nothing */
  139. }
  140. return PSCI_E_SUCCESS;
  141. }
  142. /*******************************************************************************
  143. * Helper function to check if this is the last ON CPU in the cluster
  144. ******************************************************************************/
  145. static bool tegra_last_cpu_in_cluster(const plat_local_state_t *states,
  146. uint32_t ncpu)
  147. {
  148. plat_local_state_t target;
  149. bool last_on_cpu = true;
  150. uint32_t num_cpus = ncpu, pos = 0;
  151. do {
  152. target = states[pos];
  153. if (target != PLAT_MAX_OFF_STATE) {
  154. last_on_cpu = false;
  155. }
  156. --num_cpus;
  157. pos++;
  158. } while (num_cpus != 0U);
  159. return last_on_cpu;
  160. }
  161. /*******************************************************************************
  162. * Helper function to get target power state for the cluster
  163. ******************************************************************************/
  164. static plat_local_state_t tegra_get_afflvl1_pwr_state(const plat_local_state_t *states,
  165. uint32_t ncpu)
  166. {
  167. uint32_t core_pos = (uint32_t)read_mpidr() & (uint32_t)MPIDR_CPU_MASK;
  168. uint32_t cpu = plat_my_core_pos();
  169. int32_t ret;
  170. plat_local_state_t target = states[core_pos];
  171. mce_cstate_info_t cstate_info = { 0 };
  172. /* CPU suspend */
  173. if (target == PSTATE_ID_CORE_POWERDN) {
  174. /* Program default wake mask */
  175. cstate_info.wake_mask = TEGRA186_CORE_WAKE_MASK;
  176. cstate_info.update_wake_mask = 1;
  177. mce_update_cstate_info(&cstate_info);
  178. /* Check if CCx state is allowed. */
  179. ret = mce_command_handler((uint64_t)MCE_CMD_IS_CCX_ALLOWED,
  180. (uint64_t)TEGRA_ARI_CORE_C7,
  181. tegra_percpu_data[cpu].wake_time,
  182. 0U);
  183. if (ret == 0) {
  184. target = PSCI_LOCAL_STATE_RUN;
  185. }
  186. }
  187. /* CPU off */
  188. if (target == PLAT_MAX_OFF_STATE) {
  189. /* Enable cluster powerdn from last CPU in the cluster */
  190. if (tegra_last_cpu_in_cluster(states, ncpu)) {
  191. /* Enable CC7 state and turn off wake mask */
  192. cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC7;
  193. cstate_info.update_wake_mask = 1;
  194. mce_update_cstate_info(&cstate_info);
  195. /* Check if CCx state is allowed. */
  196. ret = mce_command_handler((uint64_t)MCE_CMD_IS_CCX_ALLOWED,
  197. (uint64_t)TEGRA_ARI_CORE_C7,
  198. MCE_CORE_SLEEP_TIME_INFINITE,
  199. 0U);
  200. if (ret == 0) {
  201. target = PSCI_LOCAL_STATE_RUN;
  202. }
  203. } else {
  204. /* Turn off wake_mask */
  205. cstate_info.update_wake_mask = 1;
  206. mce_update_cstate_info(&cstate_info);
  207. target = PSCI_LOCAL_STATE_RUN;
  208. }
  209. }
  210. return target;
  211. }
  212. /*******************************************************************************
  213. * Platform handler to calculate the proper target power level at the
  214. * specified affinity level
  215. ******************************************************************************/
  216. plat_local_state_t tegra_soc_get_target_pwr_state(uint32_t lvl,
  217. const plat_local_state_t *states,
  218. uint32_t ncpu)
  219. {
  220. plat_local_state_t target = PSCI_LOCAL_STATE_RUN;
  221. uint32_t cpu = plat_my_core_pos();
  222. /* System Suspend */
  223. if ((lvl == (uint32_t)MPIDR_AFFLVL2) &&
  224. (states[cpu] == PSTATE_ID_SOC_POWERDN)) {
  225. target = PSTATE_ID_SOC_POWERDN;
  226. }
  227. /* CPU off, CPU suspend */
  228. if (lvl == (uint32_t)MPIDR_AFFLVL1) {
  229. target = tegra_get_afflvl1_pwr_state(states, ncpu);
  230. }
  231. /* target cluster/system state */
  232. return target;
  233. }
  234. int32_t tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state)
  235. {
  236. const plat_local_state_t *pwr_domain_state =
  237. target_state->pwr_domain_state;
  238. const plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
  239. uint8_t stateid_afflvl2 = pwr_domain_state[PLAT_MAX_PWR_LVL] &
  240. TEGRA186_STATE_ID_MASK;
  241. uint64_t val;
  242. uint64_t src_len_in_bytes = (uint64_t)(((uintptr_t)(&__BL31_END__) -
  243. (uintptr_t)BL31_BASE));
  244. int32_t ret;
  245. if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) {
  246. val = params_from_bl2->tzdram_base +
  247. tegra186_get_mc_ctx_size();
  248. /* Initialise communication channel with BPMP */
  249. assert(tegra_bpmp_ipc_init() == 0);
  250. /* Enable SE clock */
  251. ret = tegra_bpmp_ipc_enable_clock(TEGRA186_CLK_SE);
  252. if (ret != 0) {
  253. ERROR("Failed to enable clock\n");
  254. return ret;
  255. }
  256. /*
  257. * Generate/save SHA256 of ATF during SC7 entry
  258. */
  259. if (tegra_se_save_sha256_hash(BL31_BASE,
  260. (uint32_t)src_len_in_bytes) != 0) {
  261. ERROR("Hash calculation failed. Reboot\n");
  262. (void)tegra_soc_prepare_system_reset();
  263. }
  264. /*
  265. * The TZRAM loses power when we enter system suspend. To
  266. * allow graceful exit from system suspend, we need to copy
  267. * BL3-1 over to TZDRAM.
  268. */
  269. val = params_from_bl2->tzdram_base +
  270. tegra186_get_mc_ctx_size();
  271. memcpy16((void *)(uintptr_t)val, (void *)(uintptr_t)BL31_BASE,
  272. (uintptr_t)BL31_END - (uintptr_t)BL31_BASE);
  273. /*
  274. * Save code base and size; this would be used by SC7-RF to
  275. * verify binary
  276. */
  277. mmio_write_32(TEGRA_SCRATCH_BASE + SECURE_SCRATCH_RSV68_LO,
  278. (uint32_t)val);
  279. mmio_write_32(TEGRA_SCRATCH_BASE + SECURE_SCRATCH_RSV0_HI,
  280. (uint32_t)src_len_in_bytes);
  281. ret = tegra_bpmp_ipc_disable_clock(TEGRA186_CLK_SE);
  282. if (ret != 0) {
  283. ERROR("Failed to disable clock\n");
  284. return ret;
  285. }
  286. }
  287. return PSCI_E_SUCCESS;
  288. }
  289. int32_t tegra_soc_pwr_domain_suspend_pwrdown_early(const psci_power_state_t *target_state)
  290. {
  291. return PSCI_E_NOT_SUPPORTED;
  292. }
  293. int32_t tegra_soc_pwr_domain_on(u_register_t mpidr)
  294. {
  295. int32_t ret = PSCI_E_SUCCESS;
  296. uint64_t target_cpu = mpidr & MPIDR_CPU_MASK;
  297. uint64_t target_cluster = (mpidr & MPIDR_CLUSTER_MASK) >>
  298. MPIDR_AFFINITY_BITS;
  299. if (target_cluster > ((uint32_t)PLATFORM_CLUSTER_COUNT - 1U)) {
  300. ERROR("%s: unsupported CPU (0x%lx)\n", __func__, mpidr);
  301. ret = PSCI_E_NOT_PRESENT;
  302. } else {
  303. /* construct the target CPU # */
  304. target_cpu |= (target_cluster << 2);
  305. (void)mce_command_handler((uint64_t)MCE_CMD_ONLINE_CORE, target_cpu, 0U, 0U);
  306. }
  307. return ret;
  308. }
  309. int32_t tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state)
  310. {
  311. uint8_t stateid_afflvl2 = target_state->pwr_domain_state[PLAT_MAX_PWR_LVL];
  312. uint8_t stateid_afflvl0 = target_state->pwr_domain_state[MPIDR_AFFLVL0];
  313. mce_cstate_info_t cstate_info = { 0 };
  314. uint64_t impl, val;
  315. const plat_params_from_bl2_t *plat_params = bl31_get_plat_params();
  316. impl = (read_midr() >> MIDR_IMPL_SHIFT) & MIDR_IMPL_MASK;
  317. /*
  318. * Enable ECC and Parity Protection for Cortex-A57 CPUs (Tegra186
  319. * A02p and beyond).
  320. */
  321. if ((plat_params->l2_ecc_parity_prot_dis != 1) && (impl != DENVER_IMPL)) {
  322. val = read_l2ctlr_el1();
  323. val |= CORTEX_A57_L2_ECC_PARITY_PROTECTION_BIT;
  324. write_l2ctlr_el1(val);
  325. }
  326. /*
  327. * Reset power state info for CPUs when onlining, we set
  328. * deepest power when offlining a core but that may not be
  329. * requested by non-secure sw which controls idle states. It
  330. * will re-init this info from non-secure software when the
  331. * core come online.
  332. */
  333. if (stateid_afflvl0 == PLAT_MAX_OFF_STATE) {
  334. cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC1;
  335. cstate_info.update_wake_mask = 1;
  336. mce_update_cstate_info(&cstate_info);
  337. }
  338. /*
  339. * Check if we are exiting from deep sleep and restore SE
  340. * context if we are.
  341. */
  342. if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) {
  343. mmio_write_32(TEGRA_SE0_BASE + SE_MUTEX_WATCHDOG_NS_LIMIT,
  344. se_regs[0]);
  345. mmio_write_32(TEGRA_RNG1_BASE + RNG_MUTEX_WATCHDOG_NS_LIMIT,
  346. se_regs[1]);
  347. mmio_write_32(TEGRA_PKA1_BASE + PKA_MUTEX_WATCHDOG_NS_LIMIT,
  348. se_regs[2]);
  349. /* Init SMMU */
  350. tegra_smmu_init();
  351. /*
  352. * Reset power state info for the last core doing SC7
  353. * entry and exit, we set deepest power state as CC7
  354. * and SC7 for SC7 entry which may not be requested by
  355. * non-secure SW which controls idle states.
  356. */
  357. cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC7;
  358. cstate_info.system = (uint32_t)TEGRA_ARI_SYSTEM_SC1;
  359. cstate_info.update_wake_mask = 1;
  360. mce_update_cstate_info(&cstate_info);
  361. }
  362. return PSCI_E_SUCCESS;
  363. }
  364. int32_t tegra_soc_pwr_domain_off_early(const psci_power_state_t *target_state)
  365. {
  366. /* Do not power off the boot CPU */
  367. if (plat_is_my_cpu_primary()) {
  368. return PSCI_E_DENIED;
  369. }
  370. return PSCI_E_SUCCESS;
  371. }
  372. int32_t tegra_soc_pwr_domain_off(const psci_power_state_t *target_state)
  373. {
  374. uint64_t impl = (read_midr() >> MIDR_IMPL_SHIFT) & (uint64_t)MIDR_IMPL_MASK;
  375. (void)target_state;
  376. /* Disable Denver's DCO operations */
  377. if (impl == DENVER_IMPL) {
  378. denver_disable_dco();
  379. }
  380. /* Turn off CPU */
  381. (void)mce_command_handler((uint64_t)MCE_CMD_ENTER_CSTATE,
  382. (uint64_t)TEGRA_ARI_CORE_C7, MCE_CORE_SLEEP_TIME_INFINITE, 0U);
  383. return PSCI_E_SUCCESS;
  384. }
  385. __dead2 void tegra_soc_prepare_system_off(void)
  386. {
  387. /* power off the entire system */
  388. mce_enter_ccplex_state((uint32_t)TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_POWER_OFF);
  389. wfi();
  390. /* wait for the system to power down */
  391. for (;;) {
  392. ;
  393. }
  394. }
  395. int32_t tegra_soc_prepare_system_reset(void)
  396. {
  397. mce_enter_ccplex_state((uint32_t)TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_REBOOT);
  398. return PSCI_E_SUCCESS;
  399. }