Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 1 | /* |
Varun Wadekar | 6077dce | 2016-01-27 11:31:06 -0800 | [diff] [blame] | 2 | * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved. |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <arch_helpers.h> |
| 32 | #include <assert.h> |
| 33 | #include <bl_common.h> |
| 34 | #include <context.h> |
| 35 | #include <context_mgmt.h> |
| 36 | #include <debug.h> |
| 37 | #include <memctrl.h> |
| 38 | #include <mmio.h> |
| 39 | #include <platform.h> |
| 40 | #include <platform_def.h> |
| 41 | #include <pmc.h> |
| 42 | #include <psci.h> |
| 43 | #include <tegra_def.h> |
| 44 | #include <tegra_private.h> |
| 45 | |
| 46 | extern uint64_t tegra_bl31_phys_base; |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 47 | extern uint64_t tegra_sec_entry_point; |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * The following platform setup functions are weakly defined. They |
| 51 | * provide typical implementations that will be overridden by a SoC. |
| 52 | */ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 53 | #pragma weak tegra_soc_pwr_domain_suspend |
| 54 | #pragma weak tegra_soc_pwr_domain_on |
| 55 | #pragma weak tegra_soc_pwr_domain_off |
| 56 | #pragma weak tegra_soc_pwr_domain_on_finish |
Varun Wadekar | 8b82fae | 2015-11-09 17:39:28 -0800 | [diff] [blame] | 57 | #pragma weak tegra_soc_prepare_system_reset |
Varun Wadekar | e5caeed | 2016-01-07 14:04:21 -0800 | [diff] [blame] | 58 | #pragma weak tegra_soc_prepare_system_off |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 59 | |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 60 | int tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 61 | { |
| 62 | return PSCI_E_NOT_SUPPORTED; |
| 63 | } |
| 64 | |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 65 | int tegra_soc_pwr_domain_on(u_register_t mpidr) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 66 | { |
| 67 | return PSCI_E_SUCCESS; |
| 68 | } |
| 69 | |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 70 | int tegra_soc_pwr_domain_off(const psci_power_state_t *target_state) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 71 | { |
| 72 | return PSCI_E_SUCCESS; |
| 73 | } |
| 74 | |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 75 | int tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 76 | { |
| 77 | return PSCI_E_SUCCESS; |
| 78 | } |
| 79 | |
Varun Wadekar | 8b82fae | 2015-11-09 17:39:28 -0800 | [diff] [blame] | 80 | int tegra_soc_prepare_system_reset(void) |
| 81 | { |
| 82 | return PSCI_E_SUCCESS; |
| 83 | } |
| 84 | |
Varun Wadekar | e5caeed | 2016-01-07 14:04:21 -0800 | [diff] [blame] | 85 | __dead2 void tegra_soc_prepare_system_off(void) |
| 86 | { |
| 87 | ERROR("Tegra System Off: operation not handled.\n"); |
| 88 | panic(); |
| 89 | } |
| 90 | |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 91 | /******************************************************************************* |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 92 | * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND` |
| 93 | * call to get the `power_state` parameter. This allows the platform to encode |
| 94 | * the appropriate State-ID field within the `power_state` parameter which can |
| 95 | * be utilized in `pwr_domain_suspend()` to suspend to system affinity level. |
| 96 | ******************************************************************************/ |
| 97 | void tegra_get_sys_suspend_power_state(psci_power_state_t *req_state) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 98 | { |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 99 | /* lower affinities use PLAT_MAX_OFF_STATE */ |
| 100 | for (int i = MPIDR_AFFLVL0; i < PLAT_MAX_PWR_LVL; i++) |
| 101 | req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE; |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 102 | |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 103 | /* max affinity uses system suspend state id */ |
| 104 | req_state->pwr_domain_state[PLAT_MAX_PWR_LVL] = PSTATE_ID_SOC_POWERDN; |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /******************************************************************************* |
| 108 | * Handler called when an affinity instance is about to enter standby. |
| 109 | ******************************************************************************/ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 110 | void tegra_cpu_standby(plat_local_state_t cpu_state) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 111 | { |
| 112 | /* |
| 113 | * Enter standby state |
| 114 | * dsb is good practice before using wfi to enter low power states |
| 115 | */ |
| 116 | dsb(); |
| 117 | wfi(); |
| 118 | } |
| 119 | |
| 120 | /******************************************************************************* |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 121 | * Handler called when an affinity instance is about to be turned on. The |
| 122 | * level and mpidr determine the affinity instance. |
| 123 | ******************************************************************************/ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 124 | int tegra_pwr_domain_on(u_register_t mpidr) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 125 | { |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 126 | return tegra_soc_pwr_domain_on(mpidr); |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /******************************************************************************* |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 130 | * Handler called when a power domain is about to be turned off. The |
| 131 | * target_state encodes the power state that each level should transition to. |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 132 | ******************************************************************************/ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 133 | void tegra_pwr_domain_off(const psci_power_state_t *target_state) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 134 | { |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 135 | tegra_soc_pwr_domain_off(target_state); |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /******************************************************************************* |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 139 | * Handler called when called when a power domain is about to be suspended. The |
| 140 | * target_state encodes the power state that each level should transition to. |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 141 | ******************************************************************************/ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 142 | void tegra_pwr_domain_suspend(const psci_power_state_t *target_state) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 143 | { |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 144 | tegra_soc_pwr_domain_suspend(target_state); |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 145 | |
| 146 | /* disable GICC */ |
| 147 | tegra_gic_cpuif_deactivate(); |
| 148 | } |
| 149 | |
| 150 | /******************************************************************************* |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 151 | * Handler called when a power domain has just been powered on after |
| 152 | * being turned off earlier. The target_state encodes the low power state that |
| 153 | * each level has woken up from. |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 154 | ******************************************************************************/ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 155 | void tegra_pwr_domain_on_finish(const psci_power_state_t *target_state) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 156 | { |
| 157 | plat_params_from_bl2_t *plat_params; |
| 158 | |
| 159 | /* |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 160 | * Initialize the GIC cpu and distributor interfaces |
| 161 | */ |
| 162 | tegra_gic_setup(); |
| 163 | |
| 164 | /* |
| 165 | * Check if we are exiting from deep sleep. |
| 166 | */ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 167 | if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] == |
| 168 | PSTATE_ID_SOC_POWERDN) { |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 169 | |
| 170 | /* |
Varun Wadekar | 6eec6d6 | 2016-03-03 13:28:10 -0800 | [diff] [blame] | 171 | * Restore Memory Controller settings as it loses state |
| 172 | * during system suspend. |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 173 | */ |
Varun Wadekar | 6eec6d6 | 2016-03-03 13:28:10 -0800 | [diff] [blame] | 174 | tegra_memctrl_restore_settings(); |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * Security configuration to allow DRAM/device access. |
| 178 | */ |
| 179 | plat_params = bl31_get_plat_params(); |
Varun Wadekar | 6bb6246 | 2015-10-06 12:49:31 +0530 | [diff] [blame] | 180 | tegra_memctrl_tzdram_setup(plat_params->tzdram_base, |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 181 | plat_params->tzdram_size); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Reset hardware settings. |
| 186 | */ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 187 | tegra_soc_pwr_domain_on_finish(target_state); |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /******************************************************************************* |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 191 | * Handler called when a power domain has just been powered on after |
| 192 | * having been suspended earlier. The target_state encodes the low power state |
| 193 | * that each level has woken up from. |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 194 | ******************************************************************************/ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 195 | void tegra_pwr_domain_suspend_finish(const psci_power_state_t *target_state) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 196 | { |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 197 | tegra_pwr_domain_on_finish(target_state); |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /******************************************************************************* |
| 201 | * Handler called when the system wants to be powered off |
| 202 | ******************************************************************************/ |
| 203 | __dead2 void tegra_system_off(void) |
| 204 | { |
Varun Wadekar | e5caeed | 2016-01-07 14:04:21 -0800 | [diff] [blame] | 205 | INFO("Powering down system...\n"); |
| 206 | |
| 207 | tegra_soc_prepare_system_off(); |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /******************************************************************************* |
| 211 | * Handler called when the system wants to be restarted. |
| 212 | ******************************************************************************/ |
| 213 | __dead2 void tegra_system_reset(void) |
| 214 | { |
Varun Wadekar | e5caeed | 2016-01-07 14:04:21 -0800 | [diff] [blame] | 215 | INFO("Restarting system...\n"); |
| 216 | |
Varun Wadekar | 8b82fae | 2015-11-09 17:39:28 -0800 | [diff] [blame] | 217 | /* per-SoC system reset handler */ |
| 218 | tegra_soc_prepare_system_reset(); |
| 219 | |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 220 | /* |
| 221 | * Program the PMC in order to restart the system. |
| 222 | */ |
| 223 | tegra_pmc_system_reset(); |
| 224 | } |
| 225 | |
| 226 | /******************************************************************************* |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 227 | * Handler called to check the validity of the power state parameter. |
| 228 | ******************************************************************************/ |
| 229 | int32_t tegra_validate_power_state(unsigned int power_state, |
| 230 | psci_power_state_t *req_state) |
| 231 | { |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 232 | assert(req_state); |
| 233 | |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 234 | return tegra_soc_validate_power_state(power_state, req_state); |
| 235 | } |
| 236 | |
| 237 | /******************************************************************************* |
| 238 | * Platform handler called to check the validity of the non secure entrypoint. |
| 239 | ******************************************************************************/ |
| 240 | int tegra_validate_ns_entrypoint(uintptr_t entrypoint) |
| 241 | { |
| 242 | /* |
| 243 | * Check if the non secure entrypoint lies within the non |
| 244 | * secure DRAM. |
| 245 | */ |
| 246 | if ((entrypoint >= TEGRA_DRAM_BASE) && (entrypoint <= TEGRA_DRAM_END)) |
| 247 | return PSCI_E_SUCCESS; |
| 248 | |
| 249 | return PSCI_E_INVALID_ADDRESS; |
| 250 | } |
| 251 | |
| 252 | /******************************************************************************* |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 253 | * Export the platform handlers to enable psci to invoke them |
| 254 | ******************************************************************************/ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 255 | static const plat_psci_ops_t tegra_plat_psci_ops = { |
| 256 | .cpu_standby = tegra_cpu_standby, |
| 257 | .pwr_domain_on = tegra_pwr_domain_on, |
| 258 | .pwr_domain_off = tegra_pwr_domain_off, |
| 259 | .pwr_domain_suspend = tegra_pwr_domain_suspend, |
| 260 | .pwr_domain_on_finish = tegra_pwr_domain_on_finish, |
| 261 | .pwr_domain_suspend_finish = tegra_pwr_domain_suspend_finish, |
| 262 | .system_off = tegra_system_off, |
| 263 | .system_reset = tegra_system_reset, |
| 264 | .validate_power_state = tegra_validate_power_state, |
| 265 | .validate_ns_entrypoint = tegra_validate_ns_entrypoint, |
| 266 | .get_sys_suspend_power_state = tegra_get_sys_suspend_power_state, |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | /******************************************************************************* |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 270 | * Export the platform specific power ops and initialize Power Controller |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 271 | ******************************************************************************/ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 272 | int plat_setup_psci_ops(uintptr_t sec_entrypoint, |
| 273 | const plat_psci_ops_t **psci_ops) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 274 | { |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 275 | psci_power_state_t target_state = { { PSCI_LOCAL_STATE_RUN } }; |
| 276 | |
| 277 | /* |
| 278 | * Flush entrypoint variable to PoC since it will be |
| 279 | * accessed after a reset with the caches turned off. |
| 280 | */ |
| 281 | tegra_sec_entry_point = sec_entrypoint; |
| 282 | flush_dcache_range((uint64_t)&tegra_sec_entry_point, sizeof(uint64_t)); |
| 283 | |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 284 | /* |
| 285 | * Reset hardware settings. |
| 286 | */ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 287 | tegra_soc_pwr_domain_on_finish(&target_state); |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 288 | |
| 289 | /* |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 290 | * Initialize PSCI ops struct |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 291 | */ |
Varun Wadekar | a78bb1b | 2015-08-07 10:03:00 +0530 | [diff] [blame] | 292 | *psci_ops = &tegra_plat_psci_ops; |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 293 | |
| 294 | return 0; |
| 295 | } |