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