Varun Wadekar | 921b906 | 2015-08-25 17:03:14 +0530 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 4b32e62 | 2018-08-16 16:52:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. |
Varun Wadekar | 921b906 | 2015-08-25 17:03:14 +0530 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Varun Wadekar | 921b906 | 2015-08-25 17:03:14 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | #include <string.h> |
| 9 | |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 10 | #include <arch.h> |
| 11 | #include <arch_helpers.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <common/bl_common.h> |
| 13 | #include <common/debug.h> |
Varun Wadekar | a64806a | 2016-01-05 15:17:41 -0800 | [diff] [blame] | 14 | #include <context.h> |
Harvey Hsieh | fbdfce1 | 2016-11-23 19:13:08 +0800 | [diff] [blame] | 15 | #include <cortex_a57.h> |
Varun Wadekar | 8964509 | 2016-02-09 14:55:44 -0800 | [diff] [blame] | 16 | #include <denver.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 17 | #include <lib/el3_runtime/context_mgmt.h> |
| 18 | #include <lib/psci/psci.h> |
| 19 | #include <plat/common/platform.h> |
| 20 | |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 21 | #include <mce.h> |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 22 | #include <smmu.h> |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 23 | #include <stdbool.h> |
Varun Wadekar | 782c83d | 2017-03-14 14:25:35 -0700 | [diff] [blame] | 24 | #include <t18x_ari.h> |
Varun Wadekar | fa88767 | 2017-11-08 14:45:08 -0800 | [diff] [blame] | 25 | #include <tegra186_private.h> |
Varun Wadekar | 921b906 | 2015-08-25 17:03:14 +0530 | [diff] [blame] | 26 | #include <tegra_private.h> |
| 27 | |
Antonio Nino Diaz | 4b32e62 | 2018-08-16 16:52:57 +0100 | [diff] [blame] | 28 | extern void memcpy16(void *dest, const void *src, unsigned int length); |
Varun Wadekar | d66ee54 | 2016-02-29 10:24:30 -0800 | [diff] [blame] | 29 | |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 30 | /* state id mask */ |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 31 | #define TEGRA186_STATE_ID_MASK 0xFU |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 32 | /* constants to get power state's wake time */ |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 33 | #define TEGRA186_WAKE_TIME_MASK 0x0FFFFFF0U |
| 34 | #define TEGRA186_WAKE_TIME_SHIFT 4U |
Varun Wadekar | 698e7c6 | 2016-03-28 15:05:03 -0700 | [diff] [blame] | 35 | /* default core wake mask for CPU_SUSPEND */ |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 36 | #define TEGRA186_CORE_WAKE_MASK 0x180cU |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 37 | /* context size to save during system suspend */ |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 38 | #define TEGRA186_SE_CONTEXT_SIZE 3U |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 39 | |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 40 | static uint32_t se_regs[TEGRA186_SE_CONTEXT_SIZE]; |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 41 | static struct tegra_psci_percpu_data { |
| 42 | uint32_t wake_time; |
| 43 | } __aligned(CACHE_WRITEBACK_GRANULE) tegra_percpu_data[PLATFORM_CORE_COUNT]; |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 44 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 45 | int32_t tegra_soc_validate_power_state(uint32_t power_state, |
Varun Wadekar | c2c3a2a | 2016-01-08 17:38:51 -0800 | [diff] [blame] | 46 | psci_power_state_t *req_state) |
Varun Wadekar | 921b906 | 2015-08-25 17:03:14 +0530 | [diff] [blame] | 47 | { |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 48 | 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 Wadekar | 8964509 | 2016-02-09 14:55:44 -0800 | [diff] [blame] | 51 | |
Krishna Sitaraman | 86569d1 | 2016-08-18 15:41:21 -0700 | [diff] [blame] | 52 | /* save the core wake time (in TSC ticks)*/ |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 53 | tegra_percpu_data[cpu].wake_time = (power_state & TEGRA186_WAKE_TIME_MASK) |
Krishna Sitaraman | 86569d1 | 2016-08-18 15:41:21 -0700 | [diff] [blame] | 54 | << TEGRA186_WAKE_TIME_SHIFT; |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 55 | |
Mustafa Yigit Bilgen | f40bc2c | 2016-09-02 19:30:22 -0700 | [diff] [blame] | 56 | /* |
| 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 Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 63 | clean_dcache_range((uint64_t)&tegra_percpu_data[cpu], |
| 64 | sizeof(tegra_percpu_data[cpu])); |
Mustafa Yigit Bilgen | f40bc2c | 2016-09-02 19:30:22 -0700 | [diff] [blame] | 65 | |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 66 | /* Sanity check the requested state id */ |
| 67 | switch (state_id) { |
| 68 | case PSTATE_ID_CORE_IDLE: |
| 69 | case PSTATE_ID_CORE_POWERDN: |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 70 | |
| 71 | /* Core powerdown request */ |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 72 | req_state->pwr_domain_state[MPIDR_AFFLVL0] = state_id; |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 73 | req_state->pwr_domain_state[MPIDR_AFFLVL1] = state_id; |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 74 | |
| 75 | break; |
| 76 | |
| 77 | default: |
| 78 | ERROR("%s: unsupported state id (%d)\n", __func__, state_id); |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 79 | ret = PSCI_E_INVALID_PARAMS; |
| 80 | break; |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 83 | return ret; |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 86 | int32_t tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state) |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 87 | { |
| 88 | const plat_local_state_t *pwr_domain_state; |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 89 | 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 Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 92 | mce_cstate_info_t cstate_info = { 0 }; |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 93 | uint64_t smmu_ctx_base; |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 94 | uint32_t val; |
| 95 | |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 96 | /* 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 Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 100 | stateid_afflvl2 = pwr_domain_state[PLAT_MAX_PWR_LVL] & |
| 101 | TEGRA186_STATE_ID_MASK; |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 102 | |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 103 | if ((stateid_afflvl0 == PSTATE_ID_CORE_IDLE) || |
| 104 | (stateid_afflvl0 == PSTATE_ID_CORE_POWERDN)) { |
Varun Wadekar | 4223657 | 2016-01-18 19:03:19 -0800 | [diff] [blame] | 105 | |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 106 | /* Enter CPU idle/powerdown */ |
| 107 | val = (stateid_afflvl0 == PSTATE_ID_CORE_IDLE) ? |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 108 | (uint32_t)TEGRA_ARI_CORE_C6 : (uint32_t)TEGRA_ARI_CORE_C7; |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 109 | (void)mce_command_handler((uint64_t)MCE_CMD_ENTER_CSTATE, (uint64_t)val, |
| 110 | tegra_percpu_data[cpu].wake_time, 0U); |
Varun Wadekar | c2c3a2a | 2016-01-08 17:38:51 -0800 | [diff] [blame] | 111 | |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 112 | } else if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) { |
| 113 | |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 114 | /* 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 Kao | 186485e | 2017-10-23 18:22:09 +0800 | [diff] [blame] | 124 | mmio_write_32(TEGRA_SCRATCH_BASE + SCRATCH_SECURE_BOOTP_FCFG, val); |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 125 | |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 126 | /* save SMMU context to TZDRAM */ |
| 127 | smmu_ctx_base = params_from_bl2->tzdram_base + |
Varun Wadekar | fa88767 | 2017-11-08 14:45:08 -0800 | [diff] [blame] | 128 | tegra186_get_smmu_ctx_offset(); |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 129 | tegra_smmu_save_context((uintptr_t)smmu_ctx_base); |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 130 | |
| 131 | /* Prepare for system suspend */ |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 132 | cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC7; |
| 133 | cstate_info.system = (uint32_t)TEGRA_ARI_SYSTEM_SC7; |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 134 | cstate_info.system_state_force = 1; |
| 135 | cstate_info.update_wake_mask = 1; |
| 136 | mce_update_cstate_info(&cstate_info); |
Varun Wadekar | 2a7d87e | 2017-11-10 10:26:57 -0800 | [diff] [blame] | 137 | |
Varun Wadekar | a9002bb | 2016-03-28 15:11:43 -0700 | [diff] [blame] | 138 | /* Loop until system suspend is allowed */ |
| 139 | do { |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 140 | val = (uint32_t)mce_command_handler( |
| 141 | (uint64_t)MCE_CMD_IS_SC7_ALLOWED, |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 142 | (uint64_t)TEGRA_ARI_CORE_C7, |
Varun Wadekar | a9002bb | 2016-03-28 15:11:43 -0700 | [diff] [blame] | 143 | MCE_CORE_SLEEP_TIME_INFINITE, |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 144 | 0U); |
| 145 | } while (val == 0U); |
Varun Wadekar | a9002bb | 2016-03-28 15:11:43 -0700 | [diff] [blame] | 146 | |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 147 | /* Instruct the MCE to enter system suspend state */ |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 148 | (void)mce_command_handler((uint64_t)MCE_CMD_ENTER_CSTATE, |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 149 | (uint64_t)TEGRA_ARI_CORE_C7, MCE_CORE_SLEEP_TIME_INFINITE, 0U); |
Varun Wadekar | 2a7d87e | 2017-11-10 10:26:57 -0800 | [diff] [blame] | 150 | |
| 151 | /* set system suspend state for house-keeping */ |
| 152 | tegra186_set_system_suspend_entry(); |
| 153 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 154 | } else { |
| 155 | ; /* do nothing */ |
Varun Wadekar | 921b906 | 2015-08-25 17:03:14 +0530 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return PSCI_E_SUCCESS; |
| 159 | } |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 160 | |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 161 | /******************************************************************************* |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 162 | * Helper function to check if this is the last ON CPU in the cluster |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 163 | ******************************************************************************/ |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 164 | static bool tegra_last_cpu_in_cluster(const plat_local_state_t *states, |
| 165 | uint32_t ncpu) |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 166 | { |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 167 | plat_local_state_t target; |
| 168 | bool last_on_cpu = true; |
| 169 | uint32_t num_cpus = ncpu, pos = 0; |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 170 | |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 171 | 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 | ******************************************************************************/ |
| 186 | static 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 Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 194 | |
| 195 | /* CPU suspend */ |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 196 | if (target == PSTATE_ID_CORE_POWERDN) { |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 197 | /* 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 Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 203 | ret = mce_command_handler((uint64_t)MCE_CMD_IS_CCX_ALLOWED, |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 204 | (uint64_t)TEGRA_ARI_CORE_C7, |
| 205 | tegra_percpu_data[cpu].wake_time, |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 206 | 0U); |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 207 | if (ret == 0) { |
| 208 | target = PSCI_LOCAL_STATE_RUN; |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 209 | } |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* CPU off */ |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 213 | if (target == PLAT_MAX_OFF_STATE) { |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 214 | /* Enable cluster powerdn from last CPU in the cluster */ |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 215 | if (tegra_last_cpu_in_cluster(states, ncpu)) { |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 216 | /* Enable CC7 state and turn off wake mask */ |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 217 | cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC7; |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 218 | cstate_info.update_wake_mask = 1; |
| 219 | mce_update_cstate_info(&cstate_info); |
| 220 | |
| 221 | /* Check if CCx state is allowed. */ |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 222 | ret = mce_command_handler((uint64_t)MCE_CMD_IS_CCX_ALLOWED, |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 223 | (uint64_t)TEGRA_ARI_CORE_C7, |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 224 | MCE_CORE_SLEEP_TIME_INFINITE, |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 225 | 0U); |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 226 | if (ret == 0) { |
| 227 | target = PSCI_LOCAL_STATE_RUN; |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 228 | } |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 229 | |
| 230 | } else { |
| 231 | |
| 232 | /* Turn off wake_mask */ |
| 233 | cstate_info.update_wake_mask = 1; |
| 234 | mce_update_cstate_info(&cstate_info); |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 235 | target = PSCI_LOCAL_STATE_RUN; |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 239 | return target; |
| 240 | } |
| 241 | |
| 242 | /******************************************************************************* |
| 243 | * Platform handler to calculate the proper target power level at the |
| 244 | * specified affinity level |
| 245 | ******************************************************************************/ |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 246 | plat_local_state_t tegra_soc_get_target_pwr_state(uint32_t lvl, |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 247 | const plat_local_state_t *states, |
| 248 | uint32_t ncpu) |
| 249 | { |
| 250 | plat_local_state_t target = PSCI_LOCAL_STATE_RUN; |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 251 | uint32_t cpu = plat_my_core_pos(); |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 252 | |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 253 | /* System Suspend */ |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 254 | 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 Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 262 | } |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 263 | |
Varun Wadekar | 1e7250b | 2017-05-24 08:47:15 -0700 | [diff] [blame] | 264 | /* target cluster/system state */ |
| 265 | return target; |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 268 | int32_t tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state) |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 269 | { |
| 270 | const plat_local_state_t *pwr_domain_state = |
| 271 | target_state->pwr_domain_state; |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 272 | 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 Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 274 | TEGRA186_STATE_ID_MASK; |
Steven Kao | 235e9c3 | 2016-12-23 15:43:17 +0800 | [diff] [blame] | 275 | uint64_t val; |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 276 | |
| 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 Wadekar | fa88767 | 2017-11-08 14:45:08 -0800 | [diff] [blame] | 284 | tegra186_get_cpu_reset_handler_size(); |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 285 | memcpy16((void *)(uintptr_t)val, (void *)(uintptr_t)BL31_BASE, |
Varun Wadekar | a6c69ab | 2019-01-11 10:48:47 -0800 | [diff] [blame] | 286 | (uintptr_t)BL31_END - (uintptr_t)BL31_BASE); |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | return PSCI_E_SUCCESS; |
| 290 | } |
| 291 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 292 | int32_t tegra_soc_pwr_domain_on(u_register_t mpidr) |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 293 | { |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 294 | int32_t ret = PSCI_E_SUCCESS; |
Anthony Zhou | 5a4ce00 | 2017-06-28 16:49:16 +0800 | [diff] [blame] | 295 | uint64_t target_cpu = mpidr & MPIDR_CPU_MASK; |
| 296 | uint64_t target_cluster = (mpidr & MPIDR_CLUSTER_MASK) >> |
| 297 | MPIDR_AFFINITY_BITS; |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 298 | |
Varun Wadekar | a2dd0b3 | 2017-10-17 10:29:24 -0700 | [diff] [blame] | 299 | if (target_cluster > ((uint32_t)PLATFORM_CLUSTER_COUNT - 1U)) { |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 300 | |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 301 | ERROR("%s: unsupported CPU (0x%lx)\n", __func__, mpidr); |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 302 | ret = PSCI_E_NOT_PRESENT; |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 303 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 304 | } else { |
| 305 | /* construct the target CPU # */ |
| 306 | target_cpu |= (target_cluster << 2); |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 307 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 308 | (void)mce_command_handler((uint64_t)MCE_CMD_ONLINE_CORE, target_cpu, 0U, 0U); |
| 309 | } |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 310 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 311 | return ret; |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 312 | } |
| 313 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 314 | int32_t tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state) |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 315 | { |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 316 | 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 Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 318 | mce_cstate_info_t cstate_info = { 0 }; |
Harvey Hsieh | fbdfce1 | 2016-11-23 19:13:08 +0800 | [diff] [blame] | 319 | uint64_t impl, val; |
| 320 | const plat_params_from_bl2_t *plat_params = bl31_get_plat_params(); |
| 321 | |
Anthony Zhou | 5a4ce00 | 2017-06-28 16:49:16 +0800 | [diff] [blame] | 322 | impl = (read_midr() >> MIDR_IMPL_SHIFT) & MIDR_IMPL_MASK; |
Harvey Hsieh | fbdfce1 | 2016-11-23 19:13:08 +0800 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * Enable ECC and Parity Protection for Cortex-A57 CPUs (Tegra186 |
| 326 | * A02p and beyond). |
| 327 | */ |
Anthony Zhou | 5a4ce00 | 2017-06-28 16:49:16 +0800 | [diff] [blame] | 328 | if ((plat_params->l2_ecc_parity_prot_dis != 1) && (impl != DENVER_IMPL)) { |
Harvey Hsieh | fbdfce1 | 2016-11-23 19:13:08 +0800 | [diff] [blame] | 329 | |
| 330 | val = read_l2ctlr_el1(); |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 331 | val |= CORTEX_A57_L2_ECC_PARITY_PROTECTION_BIT; |
Harvey Hsieh | fbdfce1 | 2016-11-23 19:13:08 +0800 | [diff] [blame] | 332 | write_l2ctlr_el1(val); |
| 333 | } |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 334 | |
| 335 | /* |
Varun Wadekar | 5a40256 | 2016-04-29 11:25:46 -0700 | [diff] [blame] | 336 | * 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 Wadekar | d2da47a | 2016-04-09 00:40:45 -0700 | [diff] [blame] | 341 | */ |
Varun Wadekar | 5a40256 | 2016-04-29 11:25:46 -0700 | [diff] [blame] | 342 | if (stateid_afflvl0 == PLAT_MAX_OFF_STATE) { |
| 343 | |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 344 | cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC1; |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 345 | cstate_info.update_wake_mask = 1; |
| 346 | mce_update_cstate_info(&cstate_info); |
Varun Wadekar | 5a40256 | 2016-04-29 11:25:46 -0700 | [diff] [blame] | 347 | } |
Varun Wadekar | d2da47a | 2016-04-09 00:40:45 -0700 | [diff] [blame] | 348 | |
| 349 | /* |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 350 | * Check if we are exiting from deep sleep and restore SE |
| 351 | * context if we are. |
| 352 | */ |
Varun Wadekar | 5a40256 | 2016-04-29 11:25:46 -0700 | [diff] [blame] | 353 | if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) { |
| 354 | |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 355 | 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 Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 364 | |
| 365 | /* |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 366 | * 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 Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 370 | */ |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 371 | cstate_info.cluster = (uint32_t)TEGRA_ARI_CLUSTER_CC7; |
| 372 | cstate_info.system = (uint32_t)TEGRA_ARI_SYSTEM_SC1; |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 373 | cstate_info.update_wake_mask = 1; |
| 374 | mce_update_cstate_info(&cstate_info); |
Varun Wadekar | b877615 | 2016-03-03 13:52:52 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | return PSCI_E_SUCCESS; |
| 378 | } |
| 379 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 380 | int32_t tegra_soc_pwr_domain_off(const psci_power_state_t *target_state) |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 381 | { |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 382 | uint64_t impl = (read_midr() >> MIDR_IMPL_SHIFT) & (uint64_t)MIDR_IMPL_MASK; |
| 383 | |
| 384 | (void)target_state; |
Varun Wadekar | a64806a | 2016-01-05 15:17:41 -0800 | [diff] [blame] | 385 | |
Varun Wadekar | e26a55a | 2016-02-26 11:09:21 -0800 | [diff] [blame] | 386 | /* Disable Denver's DCO operations */ |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 387 | if (impl == DENVER_IMPL) { |
Varun Wadekar | e26a55a | 2016-02-26 11:09:21 -0800 | [diff] [blame] | 388 | denver_disable_dco(); |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 389 | } |
Varun Wadekar | e26a55a | 2016-02-26 11:09:21 -0800 | [diff] [blame] | 390 | |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 391 | /* Turn off CPU */ |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 392 | (void)mce_command_handler((uint64_t)MCE_CMD_ENTER_CSTATE, |
| 393 | (uint64_t)TEGRA_ARI_CORE_C7, MCE_CORE_SLEEP_TIME_INFINITE, 0U); |
Varun Wadekar | 4a0b37a | 2016-04-09 00:36:42 -0700 | [diff] [blame] | 394 | |
| 395 | return PSCI_E_SUCCESS; |
Varun Wadekar | abd153c | 2015-09-14 09:31:39 +0530 | [diff] [blame] | 396 | } |
Varun Wadekar | 782c83d | 2017-03-14 14:25:35 -0700 | [diff] [blame] | 397 | |
| 398 | __dead2 void tegra_soc_prepare_system_off(void) |
| 399 | { |
Varun Wadekar | 71d0e8d | 2017-05-17 14:35:33 -0700 | [diff] [blame] | 400 | /* power off the entire system */ |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 401 | mce_enter_ccplex_state((uint32_t)TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_POWER_OFF); |
Varun Wadekar | d66ee54 | 2016-02-29 10:24:30 -0800 | [diff] [blame] | 402 | |
| 403 | wfi(); |
| 404 | |
| 405 | /* wait for the system to power down */ |
| 406 | for (;;) { |
| 407 | ; |
| 408 | } |
Varun Wadekar | 782c83d | 2017-03-14 14:25:35 -0700 | [diff] [blame] | 409 | } |
Varun Wadekar | 38020c9 | 2016-01-07 14:36:12 -0800 | [diff] [blame] | 410 | |
Anthony Zhou | 5d1bb05 | 2017-03-03 16:23:08 +0800 | [diff] [blame] | 411 | int32_t tegra_soc_prepare_system_reset(void) |
Varun Wadekar | 38020c9 | 2016-01-07 14:36:12 -0800 | [diff] [blame] | 412 | { |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 413 | mce_enter_ccplex_state((uint32_t)TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_REBOOT); |
Varun Wadekar | 38020c9 | 2016-01-07 14:36:12 -0800 | [diff] [blame] | 414 | |
| 415 | return PSCI_E_SUCCESS; |
| 416 | } |