blob: 11394c0ce457c57f47d4066bfada579c75b1a874 [file] [log] [blame]
Varun Wadekar921b9062015-08-25 17:03:14 +05301/*
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +01002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Varun Wadekar921b9062015-08-25 17:03:14 +05303 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Varun Wadekar921b9062015-08-25 17:03:14 +05305 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <assert.h>
8#include <string.h>
9
Varun Wadekarabd153c2015-09-14 09:31:39 +053010#include <arch.h>
11#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <common/bl_common.h>
13#include <common/debug.h>
Varun Wadekara64806a2016-01-05 15:17:41 -080014#include <context.h>
Harvey Hsiehfbdfce12016-11-23 19:13:08 +080015#include <cortex_a57.h>
Varun Wadekar89645092016-02-09 14:55:44 -080016#include <denver.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <lib/el3_runtime/context_mgmt.h>
18#include <lib/psci/psci.h>
19#include <plat/common/platform.h>
20
Varun Wadekarabd153c2015-09-14 09:31:39 +053021#include <mce.h>
Varun Wadekarb8776152016-03-03 13:52:52 -080022#include <smmu.h>
Varun Wadekar1e7250b2017-05-24 08:47:15 -070023#include <stdbool.h>
Varun Wadekar782c83d2017-03-14 14:25:35 -070024#include <t18x_ari.h>
Varun Wadekarfa887672017-11-08 14:45:08 -080025#include <tegra186_private.h>
Varun Wadekar921b9062015-08-25 17:03:14 +053026#include <tegra_private.h>
27
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010028extern void memcpy16(void *dest, const void *src, unsigned int length);
Varun Wadekard66ee542016-02-29 10:24:30 -080029
Varun Wadekar42236572016-01-18 19:03:19 -080030/* state id mask */
Anthony Zhou5d1bb052017-03-03 16:23:08 +080031#define TEGRA186_STATE_ID_MASK 0xFU
Varun Wadekar42236572016-01-18 19:03:19 -080032/* constants to get power state's wake time */
Anthony Zhou5d1bb052017-03-03 16:23:08 +080033#define TEGRA186_WAKE_TIME_MASK 0x0FFFFFF0U
34#define TEGRA186_WAKE_TIME_SHIFT 4U
Varun Wadekar698e7c62016-03-28 15:05:03 -070035/* default core wake mask for CPU_SUSPEND */
Anthony Zhou5d1bb052017-03-03 16:23:08 +080036#define TEGRA186_CORE_WAKE_MASK 0x180cU
Varun Wadekarb8776152016-03-03 13:52:52 -080037/* context size to save during system suspend */
Anthony Zhou5d1bb052017-03-03 16:23:08 +080038#define TEGRA186_SE_CONTEXT_SIZE 3U
Varun Wadekar42236572016-01-18 19:03:19 -080039
Varun Wadekarb8776152016-03-03 13:52:52 -080040static uint32_t se_regs[TEGRA186_SE_CONTEXT_SIZE];
Anthony Zhou5d1bb052017-03-03 16:23:08 +080041static struct tegra_psci_percpu_data {
42 uint32_t wake_time;
43} __aligned(CACHE_WRITEBACK_GRANULE) tegra_percpu_data[PLATFORM_CORE_COUNT];
Varun Wadekar42236572016-01-18 19:03:19 -080044
Anthony Zhou5d1bb052017-03-03 16:23:08 +080045int32_t tegra_soc_validate_power_state(uint32_t power_state,
Varun Wadekarc2c3a2a2016-01-08 17:38:51 -080046 psci_power_state_t *req_state)
Varun Wadekar921b9062015-08-25 17:03:14 +053047{
Anthony Zhou5d1bb052017-03-03 16:23:08 +080048 uint8_t state_id = (uint8_t)psci_get_pstate_id(power_state) & TEGRA186_STATE_ID_MASK;
49 uint32_t cpu = plat_my_core_pos();
50 int32_t ret = PSCI_E_SUCCESS;
Varun Wadekar89645092016-02-09 14:55:44 -080051
Krishna Sitaraman86569d12016-08-18 15:41:21 -070052 /* save the core wake time (in TSC ticks)*/
Anthony Zhou5d1bb052017-03-03 16:23:08 +080053 tegra_percpu_data[cpu].wake_time = (power_state & TEGRA186_WAKE_TIME_MASK)
Krishna Sitaraman86569d12016-08-18 15:41:21 -070054 << TEGRA186_WAKE_TIME_SHIFT;
Varun Wadekar42236572016-01-18 19:03:19 -080055
Mustafa Yigit Bilgenf40bc2c2016-09-02 19:30:22 -070056 /*
57 * Clean percpu_data[cpu] to DRAM. This needs to be done to ensure that
58 * the correct value is read in tegra_soc_pwr_domain_suspend(), which
59 * is called with caches disabled. It is possible to read a stale value
60 * from DRAM in that function, because the L2 cache is not flushed
61 * unless the cluster is entering CC6/CC7.
62 */
Anthony Zhou5d1bb052017-03-03 16:23:08 +080063 clean_dcache_range((uint64_t)&tegra_percpu_data[cpu],
64 sizeof(tegra_percpu_data[cpu]));
Mustafa Yigit Bilgenf40bc2c2016-09-02 19:30:22 -070065
Varun Wadekar42236572016-01-18 19:03:19 -080066 /* Sanity check the requested state id */
67 switch (state_id) {
68 case PSTATE_ID_CORE_IDLE:
69 case PSTATE_ID_CORE_POWERDN:
Varun Wadekar4a0b37a2016-04-09 00:36:42 -070070
71 /* Core powerdown request */
Varun Wadekar42236572016-01-18 19:03:19 -080072 req_state->pwr_domain_state[MPIDR_AFFLVL0] = state_id;
Varun Wadekar4a0b37a2016-04-09 00:36:42 -070073 req_state->pwr_domain_state[MPIDR_AFFLVL1] = state_id;
Varun Wadekar42236572016-01-18 19:03:19 -080074
75 break;
76
77 default:
78 ERROR("%s: unsupported state id (%d)\n", __func__, state_id);
Anthony Zhou5d1bb052017-03-03 16:23:08 +080079 ret = PSCI_E_INVALID_PARAMS;
80 break;
Varun Wadekar42236572016-01-18 19:03:19 -080081 }
82
Anthony Zhou5d1bb052017-03-03 16:23:08 +080083 return ret;
Varun Wadekar42236572016-01-18 19:03:19 -080084}
85
Anthony Zhou5d1bb052017-03-03 16:23:08 +080086int32_t tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state)
Varun Wadekar42236572016-01-18 19:03:19 -080087{
88 const plat_local_state_t *pwr_domain_state;
Anthony Zhou5d1bb052017-03-03 16:23:08 +080089 uint8_t stateid_afflvl0, stateid_afflvl2;
90 uint32_t cpu = plat_my_core_pos();
91 const plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
Varun Wadekar4a0b37a2016-04-09 00:36:42 -070092 mce_cstate_info_t cstate_info = { 0 };
Varun Wadekar93bed2a2016-03-18 13:07:33 -070093 uint64_t smmu_ctx_base;
Varun Wadekarb8776152016-03-03 13:52:52 -080094 uint32_t val;
95
Varun Wadekar42236572016-01-18 19:03:19 -080096 /* get the state ID */
97 pwr_domain_state = target_state->pwr_domain_state;
98 stateid_afflvl0 = pwr_domain_state[MPIDR_AFFLVL0] &
99 TEGRA186_STATE_ID_MASK;
Varun Wadekarb8776152016-03-03 13:52:52 -0800100 stateid_afflvl2 = pwr_domain_state[PLAT_MAX_PWR_LVL] &
101 TEGRA186_STATE_ID_MASK;
Varun Wadekar42236572016-01-18 19:03:19 -0800102
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700103 if ((stateid_afflvl0 == PSTATE_ID_CORE_IDLE) ||
104 (stateid_afflvl0 == PSTATE_ID_CORE_POWERDN)) {
Varun Wadekar42236572016-01-18 19:03:19 -0800105
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700106 /* Enter CPU idle/powerdown */
107 val = (stateid_afflvl0 == PSTATE_ID_CORE_IDLE) ?
Anthony Zhou0e07e452017-07-26 17:16:54 +0800108 (uint32_t)TEGRA_ARI_CORE_C6 : (uint32_t)TEGRA_ARI_CORE_C7;
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800109 (void)mce_command_handler((uint64_t)MCE_CMD_ENTER_CSTATE, (uint64_t)val,
110 tegra_percpu_data[cpu].wake_time, 0U);
Varun Wadekarc2c3a2a2016-01-08 17:38:51 -0800111
Varun Wadekarb8776152016-03-03 13:52:52 -0800112 } else if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) {
113
Varun Wadekarb8776152016-03-03 13:52:52 -0800114 /* save SE registers */
115 se_regs[0] = mmio_read_32(TEGRA_SE0_BASE +
116 SE_MUTEX_WATCHDOG_NS_LIMIT);
117 se_regs[1] = mmio_read_32(TEGRA_RNG1_BASE +
118 RNG_MUTEX_WATCHDOG_NS_LIMIT);
119 se_regs[2] = mmio_read_32(TEGRA_PKA1_BASE +
120 PKA_MUTEX_WATCHDOG_NS_LIMIT);
121
122 /* save 'Secure Boot' Processor Feature Config Register */
123 val = mmio_read_32(TEGRA_MISC_BASE + MISCREG_PFCFG);
Steven Kao186485e2017-10-23 18:22:09 +0800124 mmio_write_32(TEGRA_SCRATCH_BASE + SCRATCH_SECURE_BOOTP_FCFG, val);
Varun Wadekarb8776152016-03-03 13:52:52 -0800125
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700126 /* save SMMU context to TZDRAM */
127 smmu_ctx_base = params_from_bl2->tzdram_base +
Varun Wadekarfa887672017-11-08 14:45:08 -0800128 tegra186_get_smmu_ctx_offset();
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700129 tegra_smmu_save_context((uintptr_t)smmu_ctx_base);
Varun Wadekarb8776152016-03-03 13:52:52 -0800130
131 /* Prepare for system suspend */
Anthony Zhou0e07e452017-07-26 17:16:54 +0800132 cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC7;
133 cstate_info.system = (uint32_t)TEGRA_ARI_SYSTEM_SC7;
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700134 cstate_info.system_state_force = 1;
135 cstate_info.update_wake_mask = 1;
136 mce_update_cstate_info(&cstate_info);
Varun Wadekar2a7d87e2017-11-10 10:26:57 -0800137
Varun Wadekara9002bb2016-03-28 15:11:43 -0700138 /* Loop until system suspend is allowed */
139 do {
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800140 val = (uint32_t)mce_command_handler(
141 (uint64_t)MCE_CMD_IS_SC7_ALLOWED,
Anthony Zhou0e07e452017-07-26 17:16:54 +0800142 (uint64_t)TEGRA_ARI_CORE_C7,
Varun Wadekara9002bb2016-03-28 15:11:43 -0700143 MCE_CORE_SLEEP_TIME_INFINITE,
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800144 0U);
145 } while (val == 0U);
Varun Wadekara9002bb2016-03-28 15:11:43 -0700146
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700147 /* Instruct the MCE to enter system suspend state */
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800148 (void)mce_command_handler((uint64_t)MCE_CMD_ENTER_CSTATE,
Anthony Zhou0e07e452017-07-26 17:16:54 +0800149 (uint64_t)TEGRA_ARI_CORE_C7, MCE_CORE_SLEEP_TIME_INFINITE, 0U);
Varun Wadekar2a7d87e2017-11-10 10:26:57 -0800150
151 /* set system suspend state for house-keeping */
152 tegra186_set_system_suspend_entry();
153
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800154 } else {
155 ; /* do nothing */
Varun Wadekar921b9062015-08-25 17:03:14 +0530156 }
157
158 return PSCI_E_SUCCESS;
159}
Varun Wadekarabd153c2015-09-14 09:31:39 +0530160
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700161/*******************************************************************************
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700162 * Helper function to check if this is the last ON CPU in the cluster
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700163 ******************************************************************************/
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700164static bool tegra_last_cpu_in_cluster(const plat_local_state_t *states,
165 uint32_t ncpu)
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700166{
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700167 plat_local_state_t target;
168 bool last_on_cpu = true;
169 uint32_t num_cpus = ncpu, pos = 0;
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700170
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700171 do {
172 target = states[pos];
173 if (target != PLAT_MAX_OFF_STATE) {
174 last_on_cpu = false;
175 }
176 --num_cpus;
177 pos++;
178 } while (num_cpus != 0U);
179
180 return last_on_cpu;
181}
182
183/*******************************************************************************
184 * Helper function to get target power state for the cluster
185 ******************************************************************************/
186static plat_local_state_t tegra_get_afflvl1_pwr_state(const plat_local_state_t *states,
187 uint32_t ncpu)
188{
189 uint32_t core_pos = (uint32_t)read_mpidr() & (uint32_t)MPIDR_CPU_MASK;
190 uint32_t cpu = plat_my_core_pos();
191 int32_t ret;
192 plat_local_state_t target = states[core_pos];
193 mce_cstate_info_t cstate_info = { 0 };
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700194
195 /* CPU suspend */
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700196 if (target == PSTATE_ID_CORE_POWERDN) {
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700197 /* Program default wake mask */
198 cstate_info.wake_mask = TEGRA186_CORE_WAKE_MASK;
199 cstate_info.update_wake_mask = 1;
200 mce_update_cstate_info(&cstate_info);
201
202 /* Check if CCx state is allowed. */
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800203 ret = mce_command_handler((uint64_t)MCE_CMD_IS_CCX_ALLOWED,
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700204 (uint64_t)TEGRA_ARI_CORE_C7,
205 tegra_percpu_data[cpu].wake_time,
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800206 0U);
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700207 if (ret == 0) {
208 target = PSCI_LOCAL_STATE_RUN;
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800209 }
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700210 }
211
212 /* CPU off */
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700213 if (target == PLAT_MAX_OFF_STATE) {
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700214 /* Enable cluster powerdn from last CPU in the cluster */
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700215 if (tegra_last_cpu_in_cluster(states, ncpu)) {
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700216 /* Enable CC7 state and turn off wake mask */
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700217 cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC7;
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700218 cstate_info.update_wake_mask = 1;
219 mce_update_cstate_info(&cstate_info);
220
221 /* Check if CCx state is allowed. */
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800222 ret = mce_command_handler((uint64_t)MCE_CMD_IS_CCX_ALLOWED,
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700223 (uint64_t)TEGRA_ARI_CORE_C7,
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700224 MCE_CORE_SLEEP_TIME_INFINITE,
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800225 0U);
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700226 if (ret == 0) {
227 target = PSCI_LOCAL_STATE_RUN;
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800228 }
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700229
230 } else {
231
232 /* Turn off wake_mask */
233 cstate_info.update_wake_mask = 1;
234 mce_update_cstate_info(&cstate_info);
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700235 target = PSCI_LOCAL_STATE_RUN;
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700236 }
237 }
238
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700239 return target;
240}
241
242/*******************************************************************************
243 * Platform handler to calculate the proper target power level at the
244 * specified affinity level
245 ******************************************************************************/
Anthony Zhou0e07e452017-07-26 17:16:54 +0800246plat_local_state_t tegra_soc_get_target_pwr_state(uint32_t lvl,
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700247 const plat_local_state_t *states,
248 uint32_t ncpu)
249{
250 plat_local_state_t target = PSCI_LOCAL_STATE_RUN;
Anthony Zhou0e07e452017-07-26 17:16:54 +0800251 uint32_t cpu = plat_my_core_pos();
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700252
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700253 /* System Suspend */
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700254 if ((lvl == (uint32_t)MPIDR_AFFLVL2) &&
255 (states[cpu] == PSTATE_ID_SOC_POWERDN)) {
256 target = PSTATE_ID_SOC_POWERDN;
257 }
258
259 /* CPU off, CPU suspend */
260 if (lvl == (uint32_t)MPIDR_AFFLVL1) {
261 target = tegra_get_afflvl1_pwr_state(states, ncpu);
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800262 }
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700263
Varun Wadekar1e7250b2017-05-24 08:47:15 -0700264 /* target cluster/system state */
265 return target;
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700266}
267
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800268int32_t tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state)
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700269{
270 const plat_local_state_t *pwr_domain_state =
271 target_state->pwr_domain_state;
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800272 const plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
273 uint8_t stateid_afflvl2 = pwr_domain_state[PLAT_MAX_PWR_LVL] &
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700274 TEGRA186_STATE_ID_MASK;
Steven Kao235e9c32016-12-23 15:43:17 +0800275 uint64_t val;
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700276
277 if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) {
278 /*
279 * The TZRAM loses power when we enter system suspend. To
280 * allow graceful exit from system suspend, we need to copy
281 * BL3-1 over to TZDRAM.
282 */
283 val = params_from_bl2->tzdram_base +
Varun Wadekarfa887672017-11-08 14:45:08 -0800284 tegra186_get_cpu_reset_handler_size();
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700285 memcpy16((void *)(uintptr_t)val, (void *)(uintptr_t)BL31_BASE,
Varun Wadekara6c69ab2019-01-11 10:48:47 -0800286 (uintptr_t)BL31_END - (uintptr_t)BL31_BASE);
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700287 }
288
289 return PSCI_E_SUCCESS;
290}
291
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800292int32_t tegra_soc_pwr_domain_on(u_register_t mpidr)
Varun Wadekarabd153c2015-09-14 09:31:39 +0530293{
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800294 int32_t ret = PSCI_E_SUCCESS;
Anthony Zhou5a4ce002017-06-28 16:49:16 +0800295 uint64_t target_cpu = mpidr & MPIDR_CPU_MASK;
296 uint64_t target_cluster = (mpidr & MPIDR_CLUSTER_MASK) >>
297 MPIDR_AFFINITY_BITS;
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800298
Varun Wadekara2dd0b32017-10-17 10:29:24 -0700299 if (target_cluster > ((uint32_t)PLATFORM_CLUSTER_COUNT - 1U)) {
Varun Wadekarabd153c2015-09-14 09:31:39 +0530300
Varun Wadekarabd153c2015-09-14 09:31:39 +0530301 ERROR("%s: unsupported CPU (0x%lx)\n", __func__, mpidr);
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800302 ret = PSCI_E_NOT_PRESENT;
Varun Wadekarabd153c2015-09-14 09:31:39 +0530303
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800304 } else {
305 /* construct the target CPU # */
306 target_cpu |= (target_cluster << 2);
Varun Wadekarabd153c2015-09-14 09:31:39 +0530307
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800308 (void)mce_command_handler((uint64_t)MCE_CMD_ONLINE_CORE, target_cpu, 0U, 0U);
309 }
Varun Wadekarabd153c2015-09-14 09:31:39 +0530310
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800311 return ret;
Varun Wadekarabd153c2015-09-14 09:31:39 +0530312}
313
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800314int32_t tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state)
Varun Wadekarb8776152016-03-03 13:52:52 -0800315{
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800316 uint8_t stateid_afflvl2 = target_state->pwr_domain_state[PLAT_MAX_PWR_LVL];
317 uint8_t stateid_afflvl0 = target_state->pwr_domain_state[MPIDR_AFFLVL0];
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700318 mce_cstate_info_t cstate_info = { 0 };
Harvey Hsiehfbdfce12016-11-23 19:13:08 +0800319 uint64_t impl, val;
320 const plat_params_from_bl2_t *plat_params = bl31_get_plat_params();
321
Anthony Zhou5a4ce002017-06-28 16:49:16 +0800322 impl = (read_midr() >> MIDR_IMPL_SHIFT) & MIDR_IMPL_MASK;
Harvey Hsiehfbdfce12016-11-23 19:13:08 +0800323
324 /*
325 * Enable ECC and Parity Protection for Cortex-A57 CPUs (Tegra186
326 * A02p and beyond).
327 */
Anthony Zhou5a4ce002017-06-28 16:49:16 +0800328 if ((plat_params->l2_ecc_parity_prot_dis != 1) && (impl != DENVER_IMPL)) {
Harvey Hsiehfbdfce12016-11-23 19:13:08 +0800329
330 val = read_l2ctlr_el1();
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800331 val |= CORTEX_A57_L2_ECC_PARITY_PROTECTION_BIT;
Harvey Hsiehfbdfce12016-11-23 19:13:08 +0800332 write_l2ctlr_el1(val);
333 }
Varun Wadekarb8776152016-03-03 13:52:52 -0800334
335 /*
Varun Wadekar5a402562016-04-29 11:25:46 -0700336 * Reset power state info for CPUs when onlining, we set
337 * deepest power when offlining a core but that may not be
338 * requested by non-secure sw which controls idle states. It
339 * will re-init this info from non-secure software when the
340 * core come online.
Varun Wadekard2da47a2016-04-09 00:40:45 -0700341 */
Varun Wadekar5a402562016-04-29 11:25:46 -0700342 if (stateid_afflvl0 == PLAT_MAX_OFF_STATE) {
343
Anthony Zhou0e07e452017-07-26 17:16:54 +0800344 cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC1;
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700345 cstate_info.update_wake_mask = 1;
346 mce_update_cstate_info(&cstate_info);
Varun Wadekar5a402562016-04-29 11:25:46 -0700347 }
Varun Wadekard2da47a2016-04-09 00:40:45 -0700348
349 /*
Varun Wadekarb8776152016-03-03 13:52:52 -0800350 * Check if we are exiting from deep sleep and restore SE
351 * context if we are.
352 */
Varun Wadekar5a402562016-04-29 11:25:46 -0700353 if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) {
354
Varun Wadekarb8776152016-03-03 13:52:52 -0800355 mmio_write_32(TEGRA_SE0_BASE + SE_MUTEX_WATCHDOG_NS_LIMIT,
356 se_regs[0]);
357 mmio_write_32(TEGRA_RNG1_BASE + RNG_MUTEX_WATCHDOG_NS_LIMIT,
358 se_regs[1]);
359 mmio_write_32(TEGRA_PKA1_BASE + PKA_MUTEX_WATCHDOG_NS_LIMIT,
360 se_regs[2]);
361
362 /* Init SMMU */
363 tegra_smmu_init();
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700364
365 /*
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700366 * Reset power state info for the last core doing SC7
367 * entry and exit, we set deepest power state as CC7
368 * and SC7 for SC7 entry which may not be requested by
369 * non-secure SW which controls idle states.
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700370 */
Anthony Zhou0e07e452017-07-26 17:16:54 +0800371 cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC7;
372 cstate_info.system = (uint32_t)TEGRA_ARI_SYSTEM_SC1;
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700373 cstate_info.update_wake_mask = 1;
374 mce_update_cstate_info(&cstate_info);
Varun Wadekarb8776152016-03-03 13:52:52 -0800375 }
376
377 return PSCI_E_SUCCESS;
378}
379
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800380int32_t tegra_soc_pwr_domain_off(const psci_power_state_t *target_state)
Varun Wadekarabd153c2015-09-14 09:31:39 +0530381{
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800382 uint64_t impl = (read_midr() >> MIDR_IMPL_SHIFT) & (uint64_t)MIDR_IMPL_MASK;
383
384 (void)target_state;
Varun Wadekara64806a2016-01-05 15:17:41 -0800385
Varun Wadekare26a55a2016-02-26 11:09:21 -0800386 /* Disable Denver's DCO operations */
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800387 if (impl == DENVER_IMPL) {
Varun Wadekare26a55a2016-02-26 11:09:21 -0800388 denver_disable_dco();
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800389 }
Varun Wadekare26a55a2016-02-26 11:09:21 -0800390
Varun Wadekarabd153c2015-09-14 09:31:39 +0530391 /* Turn off CPU */
Anthony Zhou0e07e452017-07-26 17:16:54 +0800392 (void)mce_command_handler((uint64_t)MCE_CMD_ENTER_CSTATE,
393 (uint64_t)TEGRA_ARI_CORE_C7, MCE_CORE_SLEEP_TIME_INFINITE, 0U);
Varun Wadekar4a0b37a2016-04-09 00:36:42 -0700394
395 return PSCI_E_SUCCESS;
Varun Wadekarabd153c2015-09-14 09:31:39 +0530396}
Varun Wadekar782c83d2017-03-14 14:25:35 -0700397
398__dead2 void tegra_soc_prepare_system_off(void)
399{
Varun Wadekar71d0e8d2017-05-17 14:35:33 -0700400 /* power off the entire system */
Anthony Zhou0e07e452017-07-26 17:16:54 +0800401 mce_enter_ccplex_state((uint32_t)TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_POWER_OFF);
Varun Wadekard66ee542016-02-29 10:24:30 -0800402
403 wfi();
404
405 /* wait for the system to power down */
406 for (;;) {
407 ;
408 }
Varun Wadekar782c83d2017-03-14 14:25:35 -0700409}
Varun Wadekar38020c92016-01-07 14:36:12 -0800410
Anthony Zhou5d1bb052017-03-03 16:23:08 +0800411int32_t tegra_soc_prepare_system_reset(void)
Varun Wadekar38020c92016-01-07 14:36:12 -0800412{
Anthony Zhou0e07e452017-07-26 17:16:54 +0800413 mce_enter_ccplex_state((uint32_t)TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_REBOOT);
Varun Wadekar38020c92016-01-07 14:36:12 -0800414
415 return PSCI_E_SUCCESS;
416}