2 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
4 * SPDX-License-Identifier: BSD-3-Clause
8 #include <arch_helpers.h>
10 #include <bl_common.h>
12 #include <context_mgmt.h>
21 #include <tegra_private.h>
23 extern void memcpy16(void *dest
, const void *src
, unsigned int length
);
25 extern void prepare_cpu_pwr_dwn(void);
26 extern void tegra186_cpu_reset_handler(void);
27 extern uint32_t __tegra186_cpu_reset_handler_end
,
28 __tegra186_smmu_context
;
31 #define TEGRA186_STATE_ID_MASK 0xF
32 /* constants to get power state's wake time */
33 #define TEGRA186_WAKE_TIME_MASK 0x0FFFFFF0
34 #define TEGRA186_WAKE_TIME_SHIFT 4
35 /* default core wake mask for CPU_SUSPEND */
36 #define TEGRA186_CORE_WAKE_MASK 0x180c
37 /* context size to save during system suspend */
38 #define TEGRA186_SE_CONTEXT_SIZE 3
40 static uint32_t se_regs
[TEGRA186_SE_CONTEXT_SIZE
];
41 static struct t18x_psci_percpu_data
{
42 unsigned int wake_time
;
43 } __aligned(CACHE_WRITEBACK_GRANULE
) percpu_data
[PLATFORM_CORE_COUNT
];
45 /* System power down state */
46 uint32_t tegra186_system_powerdn_state
= TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_POWER_OFF
;
48 int32_t tegra_soc_validate_power_state(unsigned int power_state
,
49 psci_power_state_t
*req_state
)
51 int state_id
= psci_get_pstate_id(power_state
) & TEGRA186_STATE_ID_MASK
;
52 int cpu
= plat_my_core_pos();
54 /* save the core wake time (in TSC ticks)*/
55 percpu_data
[cpu
].wake_time
= (power_state
& TEGRA186_WAKE_TIME_MASK
)
56 << TEGRA186_WAKE_TIME_SHIFT
;
59 * Clean percpu_data[cpu] to DRAM. This needs to be done to ensure that
60 * the correct value is read in tegra_soc_pwr_domain_suspend(), which
61 * is called with caches disabled. It is possible to read a stale value
62 * from DRAM in that function, because the L2 cache is not flushed
63 * unless the cluster is entering CC6/CC7.
65 clean_dcache_range((uint64_t)&percpu_data
[cpu
],
66 sizeof(percpu_data
[cpu
]));
68 /* Sanity check the requested state id */
70 case PSTATE_ID_CORE_IDLE
:
71 case PSTATE_ID_CORE_POWERDN
:
73 /* Core powerdown request */
74 req_state
->pwr_domain_state
[MPIDR_AFFLVL0
] = state_id
;
75 req_state
->pwr_domain_state
[MPIDR_AFFLVL1
] = state_id
;
80 ERROR("%s: unsupported state id (%d)\n", __func__
, state_id
);
81 return PSCI_E_INVALID_PARAMS
;
84 return PSCI_E_SUCCESS
;
87 int tegra_soc_pwr_domain_suspend(const psci_power_state_t
*target_state
)
89 const plat_local_state_t
*pwr_domain_state
;
90 unsigned int stateid_afflvl0
, stateid_afflvl2
;
91 int cpu
= plat_my_core_pos();
92 plat_params_from_bl2_t
*params_from_bl2
= bl31_get_plat_params();
93 mce_cstate_info_t cstate_info
= { 0 };
94 uint64_t smmu_ctx_base
;
97 /* get the state ID */
98 pwr_domain_state
= target_state
->pwr_domain_state
;
99 stateid_afflvl0
= pwr_domain_state
[MPIDR_AFFLVL0
] &
100 TEGRA186_STATE_ID_MASK
;
101 stateid_afflvl2
= pwr_domain_state
[PLAT_MAX_PWR_LVL
] &
102 TEGRA186_STATE_ID_MASK
;
104 if ((stateid_afflvl0
== PSTATE_ID_CORE_IDLE
) ||
105 (stateid_afflvl0
== PSTATE_ID_CORE_POWERDN
)) {
107 /* Enter CPU idle/powerdown */
108 val
= (stateid_afflvl0
== PSTATE_ID_CORE_IDLE
) ?
109 TEGRA_ARI_CORE_C6
: TEGRA_ARI_CORE_C7
;
110 (void)mce_command_handler(MCE_CMD_ENTER_CSTATE
, val
,
111 percpu_data
[cpu
].wake_time
, 0);
113 } else if (stateid_afflvl2
== PSTATE_ID_SOC_POWERDN
) {
115 /* save SE registers */
116 se_regs
[0] = mmio_read_32(TEGRA_SE0_BASE
+
117 SE_MUTEX_WATCHDOG_NS_LIMIT
);
118 se_regs
[1] = mmio_read_32(TEGRA_RNG1_BASE
+
119 RNG_MUTEX_WATCHDOG_NS_LIMIT
);
120 se_regs
[2] = mmio_read_32(TEGRA_PKA1_BASE
+
121 PKA_MUTEX_WATCHDOG_NS_LIMIT
);
123 /* save 'Secure Boot' Processor Feature Config Register */
124 val
= mmio_read_32(TEGRA_MISC_BASE
+ MISCREG_PFCFG
);
125 mmio_write_32(TEGRA_SCRATCH_BASE
+ SECURE_SCRATCH_RSV6
, val
);
127 /* save SMMU context to TZDRAM */
128 smmu_ctx_base
= params_from_bl2
->tzdram_base
+
129 ((uintptr_t)&__tegra186_smmu_context
-
130 (uintptr_t)tegra186_cpu_reset_handler
);
131 tegra_smmu_save_context((uintptr_t)smmu_ctx_base
);
133 /* Prepare for system suspend */
134 cstate_info
.cluster
= TEGRA_ARI_CLUSTER_CC7
;
135 cstate_info
.system
= TEGRA_ARI_SYSTEM_SC7
;
136 cstate_info
.system_state_force
= 1;
137 cstate_info
.update_wake_mask
= 1;
138 mce_update_cstate_info(&cstate_info
);
140 /* Loop until system suspend is allowed */
142 val
= mce_command_handler(MCE_CMD_IS_SC7_ALLOWED
,
144 MCE_CORE_SLEEP_TIME_INFINITE
,
148 /* Instruct the MCE to enter system suspend state */
149 (void)mce_command_handler(MCE_CMD_ENTER_CSTATE
,
150 TEGRA_ARI_CORE_C7
, MCE_CORE_SLEEP_TIME_INFINITE
, 0);
153 return PSCI_E_SUCCESS
;
156 /*******************************************************************************
157 * Platform handler to calculate the proper target power level at the
158 * specified affinity level
159 ******************************************************************************/
160 plat_local_state_t
tegra_soc_get_target_pwr_state(unsigned int lvl
,
161 const plat_local_state_t
*states
,
164 plat_local_state_t target
= *states
;
165 int cpu
= plat_my_core_pos(), ret
, cluster_powerdn
= 1;
166 int core_pos
= read_mpidr() & MPIDR_CPU_MASK
;
167 mce_cstate_info_t cstate_info
= { 0 };
169 /* get the power state at this level */
170 if (lvl
== MPIDR_AFFLVL1
)
171 target
= *(states
+ core_pos
);
172 if (lvl
== MPIDR_AFFLVL2
)
173 target
= *(states
+ cpu
);
176 if (lvl
== MPIDR_AFFLVL1
&& target
== PSTATE_ID_CORE_POWERDN
) {
178 /* Program default wake mask */
179 cstate_info
.wake_mask
= TEGRA186_CORE_WAKE_MASK
;
180 cstate_info
.update_wake_mask
= 1;
181 mce_update_cstate_info(&cstate_info
);
183 /* Check if CCx state is allowed. */
184 ret
= mce_command_handler(MCE_CMD_IS_CCX_ALLOWED
,
185 TEGRA_ARI_CORE_C7
, percpu_data
[cpu
].wake_time
,
188 return PSTATE_ID_CORE_POWERDN
;
192 if (lvl
== MPIDR_AFFLVL1
&& target
== PLAT_MAX_OFF_STATE
) {
194 /* find out the number of ON cpus in the cluster */
197 if (target
!= PLAT_MAX_OFF_STATE
)
201 /* Enable cluster powerdn from last CPU in the cluster */
202 if (cluster_powerdn
) {
204 /* Enable CC7 state and turn off wake mask */
205 cstate_info
.cluster
= TEGRA_ARI_CLUSTER_CC7
;
206 cstate_info
.update_wake_mask
= 1;
207 mce_update_cstate_info(&cstate_info
);
209 /* Check if CCx state is allowed. */
210 ret
= mce_command_handler(MCE_CMD_IS_CCX_ALLOWED
,
212 MCE_CORE_SLEEP_TIME_INFINITE
,
215 return PSTATE_ID_CORE_POWERDN
;
219 /* Turn off wake_mask */
220 cstate_info
.update_wake_mask
= 1;
221 mce_update_cstate_info(&cstate_info
);
226 if (((lvl
== MPIDR_AFFLVL2
) || (lvl
== MPIDR_AFFLVL1
)) &&
227 (target
== PSTATE_ID_SOC_POWERDN
))
228 return PSTATE_ID_SOC_POWERDN
;
231 return PSCI_LOCAL_STATE_RUN
;
234 int tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t
*target_state
)
236 const plat_local_state_t
*pwr_domain_state
=
237 target_state
->pwr_domain_state
;
238 plat_params_from_bl2_t
*params_from_bl2
= bl31_get_plat_params();
239 unsigned int stateid_afflvl2
= pwr_domain_state
[PLAT_MAX_PWR_LVL
] &
240 TEGRA186_STATE_ID_MASK
;
243 if (stateid_afflvl2
== PSTATE_ID_SOC_POWERDN
) {
245 * The TZRAM loses power when we enter system suspend. To
246 * allow graceful exit from system suspend, we need to copy
247 * BL3-1 over to TZDRAM.
249 val
= params_from_bl2
->tzdram_base
+
250 ((uintptr_t)&__tegra186_cpu_reset_handler_end
-
251 (uintptr_t)tegra186_cpu_reset_handler
);
252 memcpy16((void *)(uintptr_t)val
, (void *)(uintptr_t)BL31_BASE
,
253 (uintptr_t)&__BL31_END__
- (uintptr_t)BL31_BASE
);
256 return PSCI_E_SUCCESS
;
259 int tegra_soc_pwr_domain_on(u_register_t mpidr
)
261 uint32_t target_cpu
= mpidr
& MPIDR_CPU_MASK
;
262 uint32_t target_cluster
= (mpidr
& MPIDR_CLUSTER_MASK
) >>
265 if (target_cluster
> MPIDR_AFFLVL1
) {
266 ERROR("%s: unsupported CPU (0x%lx)\n", __func__
, mpidr
);
267 return PSCI_E_NOT_PRESENT
;
270 /* construct the target CPU # */
271 target_cpu
|= (target_cluster
<< 2);
273 mce_command_handler(MCE_CMD_ONLINE_CORE
, target_cpu
, 0, 0);
275 return PSCI_E_SUCCESS
;
278 int tegra_soc_pwr_domain_on_finish(const psci_power_state_t
*target_state
)
280 int stateid_afflvl2
= target_state
->pwr_domain_state
[PLAT_MAX_PWR_LVL
];
281 int stateid_afflvl0
= target_state
->pwr_domain_state
[MPIDR_AFFLVL0
];
282 mce_cstate_info_t cstate_info
= { 0 };
285 * Reset power state info for CPUs when onlining, we set
286 * deepest power when offlining a core but that may not be
287 * requested by non-secure sw which controls idle states. It
288 * will re-init this info from non-secure software when the
291 if (stateid_afflvl0
== PLAT_MAX_OFF_STATE
) {
293 cstate_info
.cluster
= TEGRA_ARI_CLUSTER_CC1
;
294 cstate_info
.update_wake_mask
= 1;
295 mce_update_cstate_info(&cstate_info
);
299 * Check if we are exiting from deep sleep and restore SE
302 if (stateid_afflvl2
== PSTATE_ID_SOC_POWERDN
) {
304 mmio_write_32(TEGRA_SE0_BASE
+ SE_MUTEX_WATCHDOG_NS_LIMIT
,
306 mmio_write_32(TEGRA_RNG1_BASE
+ RNG_MUTEX_WATCHDOG_NS_LIMIT
,
308 mmio_write_32(TEGRA_PKA1_BASE
+ PKA_MUTEX_WATCHDOG_NS_LIMIT
,
315 * Reset power state info for the last core doing SC7
316 * entry and exit, we set deepest power state as CC7
317 * and SC7 for SC7 entry which may not be requested by
318 * non-secure SW which controls idle states.
320 cstate_info
.cluster
= TEGRA_ARI_CLUSTER_CC7
;
321 cstate_info
.system
= TEGRA_ARI_SYSTEM_SC1
;
322 cstate_info
.update_wake_mask
= 1;
323 mce_update_cstate_info(&cstate_info
);
326 return PSCI_E_SUCCESS
;
329 int tegra_soc_pwr_domain_off(const psci_power_state_t
*target_state
)
331 int impl
= (read_midr() >> MIDR_IMPL_SHIFT
) & MIDR_IMPL_MASK
;
333 /* Disable Denver's DCO operations */
334 if (impl
== DENVER_IMPL
)
335 denver_disable_dco();
338 (void)mce_command_handler(MCE_CMD_ENTER_CSTATE
, TEGRA_ARI_CORE_C7
,
339 MCE_CORE_SLEEP_TIME_INFINITE
, 0);
341 return PSCI_E_SUCCESS
;
344 __dead2
void tegra_soc_prepare_system_off(void)
346 mce_cstate_info_t cstate_info
= { 0 };
349 if (tegra186_system_powerdn_state
== TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_POWER_OFF
) {
351 /* power off the entire system */
352 mce_enter_ccplex_state(tegra186_system_powerdn_state
);
354 } else if (tegra186_system_powerdn_state
== TEGRA_ARI_SYSTEM_SC8
) {
356 /* Prepare for quasi power down */
357 cstate_info
.cluster
= TEGRA_ARI_CLUSTER_CC7
;
358 cstate_info
.system
= TEGRA_ARI_SYSTEM_SC8
;
359 cstate_info
.system_state_force
= 1;
360 cstate_info
.update_wake_mask
= 1;
361 mce_update_cstate_info(&cstate_info
);
363 /* loop until other CPUs power down */
365 val
= mce_command_handler(MCE_CMD_IS_SC7_ALLOWED
,
367 MCE_CORE_SLEEP_TIME_INFINITE
,
371 /* Enter quasi power down state */
372 (void)mce_command_handler(MCE_CMD_ENTER_CSTATE
,
373 TEGRA_ARI_CORE_C7
, MCE_CORE_SLEEP_TIME_INFINITE
, 0);
376 tegra_gic_cpuif_deactivate();
378 /* power down core */
379 prepare_cpu_pwr_dwn();
381 /* flush L1/L2 data caches */
385 ERROR("%s: unsupported power down state (%d)\n", __func__
,
386 tegra186_system_powerdn_state
);
391 /* wait for the system to power down */
397 int tegra_soc_prepare_system_reset(void)
399 mce_enter_ccplex_state(TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_REBOOT
);
401 return PSCI_E_SUCCESS
;