Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 1 | /* |
Deepika Bhavnani | 4287c0c | 2019-12-13 10:23:18 -0600 | [diff] [blame] | 2 | * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 8 | #include <string.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
| 10 | #include <arch.h> |
| 11 | #include <arch_helpers.h> |
| 12 | #include <common/debug.h> |
| 13 | #include <lib/pmf/pmf.h> |
| 14 | #include <lib/runtime_instr.h> |
| 15 | #include <plat/common/platform.h> |
| 16 | |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 17 | #include "psci_private.h" |
| 18 | |
Soby Mathew | 6b8b302 | 2015-06-30 11:00:24 +0100 | [diff] [blame] | 19 | /****************************************************************************** |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 20 | * Construct the psci_power_state to request power OFF at all power levels. |
| 21 | ******************************************************************************/ |
| 22 | static void psci_set_power_off_state(psci_power_state_t *state_info) |
| 23 | { |
Varun Wadekar | 66231d1 | 2017-06-07 09:57:42 -0700 | [diff] [blame] | 24 | unsigned int lvl; |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 25 | |
| 26 | for (lvl = PSCI_CPU_PWR_LVL; lvl <= PLAT_MAX_PWR_LVL; lvl++) |
| 27 | state_info->pwr_domain_state[lvl] = PLAT_MAX_OFF_STATE; |
| 28 | } |
| 29 | |
| 30 | /****************************************************************************** |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 31 | * Top level handler which is called when a cpu wants to power itself down. |
Soby Mathew | 3a9e8bf | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 32 | * It's assumed that along with turning the cpu power domain off, power |
| 33 | * domains at higher levels will be turned off as far as possible. It finds |
| 34 | * the highest level where a domain has to be powered off by traversing the |
| 35 | * node information and then performs generic, architectural, platform setup |
| 36 | * and state management required to turn OFF that power domain and domains |
| 37 | * below it. e.g. For a cpu that's to be powered OFF, it could mean programming |
| 38 | * the power controller whereas for a cluster that's to be powered off, it will |
| 39 | * call the platform specific code which will disable coherency at the |
| 40 | * interconnect level if the cpu is the last in the cluster and also the |
| 41 | * program the power controller. |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 42 | ******************************************************************************/ |
Soby Mathew | 011ca18 | 2015-07-29 17:05:03 +0100 | [diff] [blame] | 43 | int psci_do_cpu_off(unsigned int end_pwrlvl) |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 44 | { |
Antonio Nino Diaz | 56a0e8e | 2018-07-16 23:19:25 +0100 | [diff] [blame] | 45 | int rc = PSCI_E_SUCCESS; |
Deepika Bhavnani | 4287c0c | 2019-12-13 10:23:18 -0600 | [diff] [blame] | 46 | unsigned int idx = plat_my_core_pos(); |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 47 | psci_power_state_t state_info; |
Andrew F. Davis | 74e8978 | 2019-06-04 10:46:54 -0400 | [diff] [blame] | 48 | unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0}; |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * This function must only be called on platforms where the |
| 52 | * CPU_OFF platform hooks have been implemented. |
| 53 | */ |
Antonio Nino Diaz | 56a0e8e | 2018-07-16 23:19:25 +0100 | [diff] [blame] | 54 | assert(psci_plat_pm_ops->pwr_domain_off != NULL); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 55 | |
Roberto Vargas | 3cb73cb | 2017-09-04 16:49:41 +0100 | [diff] [blame] | 56 | /* Construct the psci_power_state for CPU_OFF */ |
| 57 | psci_set_power_off_state(&state_info); |
| 58 | |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 59 | /* |
Andrew F. Davis | 74e8978 | 2019-06-04 10:46:54 -0400 | [diff] [blame] | 60 | * Get the parent nodes here, this is important to do before we |
| 61 | * initiate the power down sequence as after that point the core may |
| 62 | * have exited coherency and its cache may be disabled, any access to |
| 63 | * shared memory after that (such as the parent node lookup in |
| 64 | * psci_cpu_pd_nodes) can cause coherency issues on some platforms. |
| 65 | */ |
| 66 | psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes); |
| 67 | |
| 68 | /* |
Soby Mathew | 3a9e8bf | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 69 | * This function acquires the lock corresponding to each power |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 70 | * level so that by the time all locks are taken, the system topology |
| 71 | * is snapshot and state management can be done safely. |
| 72 | */ |
Andrew F. Davis | 74e8978 | 2019-06-04 10:46:54 -0400 | [diff] [blame] | 73 | psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Call the cpu off handler registered by the Secure Payload Dispatcher |
| 77 | * to let it do any bookkeeping. Assume that the SPD always reports an |
| 78 | * E_DENIED error if SP refuse to power down |
| 79 | */ |
Antonio Nino Diaz | 56a0e8e | 2018-07-16 23:19:25 +0100 | [diff] [blame] | 80 | if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_off != NULL)) { |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 81 | rc = psci_spd_pm->svc_off(0); |
Antonio Nino Diaz | 56a0e8e | 2018-07-16 23:19:25 +0100 | [diff] [blame] | 82 | if (rc != 0) |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 83 | goto exit; |
| 84 | } |
| 85 | |
| 86 | /* |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 87 | * This function is passed the requested state info and |
| 88 | * it returns the negotiated state info for each power level upto |
| 89 | * the end level specified. |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 90 | */ |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 91 | psci_do_state_coordination(end_pwrlvl, &state_info); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 92 | |
Yatharth Kochar | 241ec6c | 2016-05-09 18:26:35 +0100 | [diff] [blame] | 93 | #if ENABLE_PSCI_STAT |
| 94 | /* Update the last cpu for each level till end_pwrlvl */ |
| 95 | psci_stats_update_pwr_down(end_pwrlvl, &state_info); |
| 96 | #endif |
| 97 | |
dp-arm | 2d92de6 | 2016-11-15 13:25:30 +0000 | [diff] [blame] | 98 | #if ENABLE_RUNTIME_INSTRUMENTATION |
| 99 | |
| 100 | /* |
| 101 | * Flush cache line so that even if CPU power down happens |
| 102 | * the timestamp update is reflected in memory. |
| 103 | */ |
| 104 | PMF_CAPTURE_TIMESTAMP(rt_instr_svc, |
| 105 | RT_INSTR_ENTER_CFLUSH, |
| 106 | PMF_CACHE_MAINT); |
| 107 | #endif |
| 108 | |
Soby Mathew | 6b8b302 | 2015-06-30 11:00:24 +0100 | [diff] [blame] | 109 | /* |
Jeenu Viswambharan | 346bfd8 | 2017-01-05 11:01:02 +0000 | [diff] [blame] | 110 | * Arch. management. Initiate power down sequence. |
Soby Mathew | 6b8b302 | 2015-06-30 11:00:24 +0100 | [diff] [blame] | 111 | */ |
Jeenu Viswambharan | 346bfd8 | 2017-01-05 11:01:02 +0000 | [diff] [blame] | 112 | psci_do_pwrdown_sequence(psci_find_max_off_lvl(&state_info)); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 113 | |
dp-arm | 2d92de6 | 2016-11-15 13:25:30 +0000 | [diff] [blame] | 114 | #if ENABLE_RUNTIME_INSTRUMENTATION |
| 115 | PMF_CAPTURE_TIMESTAMP(rt_instr_svc, |
| 116 | RT_INSTR_EXIT_CFLUSH, |
| 117 | PMF_NO_CACHE_MAINT); |
| 118 | #endif |
| 119 | |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 120 | /* |
Soby Mathew | 6b8b302 | 2015-06-30 11:00:24 +0100 | [diff] [blame] | 121 | * Plat. management: Perform platform specific actions to turn this |
| 122 | * cpu off e.g. exit cpu coherency, program the power controller etc. |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 123 | */ |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 124 | psci_plat_pm_ops->pwr_domain_off(&state_info); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 125 | |
Yatharth Kochar | 241ec6c | 2016-05-09 18:26:35 +0100 | [diff] [blame] | 126 | #if ENABLE_PSCI_STAT |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 127 | plat_psci_stat_accounting_start(&state_info); |
Yatharth Kochar | 241ec6c | 2016-05-09 18:26:35 +0100 | [diff] [blame] | 128 | #endif |
| 129 | |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 130 | exit: |
| 131 | /* |
Soby Mathew | 3a9e8bf | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 132 | * Release the locks corresponding to each power level in the |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 133 | * reverse order to which they were acquired. |
| 134 | */ |
Andrew F. Davis | 74e8978 | 2019-06-04 10:46:54 -0400 | [diff] [blame] | 135 | psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 136 | |
| 137 | /* |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 138 | * Check if all actions needed to safely power down this cpu have |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 139 | * successfully completed. |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 140 | */ |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 141 | if (rc == PSCI_E_SUCCESS) { |
| 142 | /* |
Jeenu Viswambharan | 0b56d6f | 2017-01-06 14:58:11 +0000 | [diff] [blame] | 143 | * Set the affinity info state to OFF. When caches are disabled, |
| 144 | * this writes directly to main memory, so cache maintenance is |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 145 | * required to ensure that later cached reads of aff_info_state |
Jeenu Viswambharan | 0b56d6f | 2017-01-06 14:58:11 +0000 | [diff] [blame] | 146 | * return AFF_STATE_OFF. A dsbish() ensures ordering of the |
Soby Mathew | ca37050 | 2016-01-26 11:47:53 +0000 | [diff] [blame] | 147 | * update to the affinity info state prior to cache line |
| 148 | * invalidation. |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 149 | */ |
Jeenu Viswambharan | 0b56d6f | 2017-01-06 14:58:11 +0000 | [diff] [blame] | 150 | psci_flush_cpu_data(psci_svc_cpu_data.aff_info_state); |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 151 | psci_set_aff_info_state(AFF_STATE_OFF); |
Jeenu Viswambharan | 0b56d6f | 2017-01-06 14:58:11 +0000 | [diff] [blame] | 152 | psci_dsbish(); |
| 153 | psci_inv_cpu_data(psci_svc_cpu_data.aff_info_state); |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 154 | |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 155 | #if ENABLE_RUNTIME_INSTRUMENTATION |
| 156 | |
| 157 | /* |
| 158 | * Update the timestamp with cache off. We assume this |
| 159 | * timestamp can only be read from the current CPU and the |
| 160 | * timestamp cache line will be flushed before return to |
| 161 | * normal world on wakeup. |
| 162 | */ |
| 163 | PMF_CAPTURE_TIMESTAMP(rt_instr_svc, |
| 164 | RT_INSTR_ENTER_HW_LOW_PWR, |
| 165 | PMF_NO_CACHE_MAINT); |
| 166 | #endif |
| 167 | |
Antonio Nino Diaz | 56a0e8e | 2018-07-16 23:19:25 +0100 | [diff] [blame] | 168 | if (psci_plat_pm_ops->pwr_domain_pwr_down_wfi != NULL) { |
Soby Mathew | 6a81641 | 2016-04-27 14:46:28 +0100 | [diff] [blame] | 169 | /* This function must not return */ |
| 170 | psci_plat_pm_ops->pwr_domain_pwr_down_wfi(&state_info); |
| 171 | } else { |
| 172 | /* |
| 173 | * Enter a wfi loop which will allow the power |
| 174 | * controller to physically power down this cpu. |
| 175 | */ |
| 176 | psci_power_down_wfi(); |
| 177 | } |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 178 | } |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 179 | |
| 180 | return rc; |
| 181 | } |