blob: 932a03946a7b8b93e868674a23ef0ef8c60d9c58 [file] [log] [blame]
Soby Mathewb48349e2015-06-29 16:30:12 +01001/*
Boyan Karatotev9b1e8002024-10-10 08:11:09 +01002 * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved.
Varun Wadekar6cf4ae92023-04-25 14:03:27 +01003 * Copyright (c) 2023, NVIDIA Corporation. All rights reserved.
Soby Mathewb48349e2015-06-29 16:30:12 +01004 *
dp-arm82cb2c12017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewb48349e2015-06-29 16:30:12 +01006 */
7
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00008#include <assert.h>
9#include <string.h>
10
Soby Mathewb48349e2015-06-29 16:30:12 +010011#include <arch.h>
12#include <arch_helpers.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000013#include <common/debug.h>
14#include <lib/pmf/pmf.h>
15#include <lib/runtime_instr.h>
16#include <plat/common/platform.h>
17
Soby Mathewb48349e2015-06-29 16:30:12 +010018#include "psci_private.h"
19
Soby Mathew6590ce22015-06-30 11:00:24 +010020/******************************************************************************
Soby Mathew8ee24982015-04-07 12:16:56 +010021 * Construct the psci_power_state to request power OFF at all power levels.
22 ******************************************************************************/
23static void psci_set_power_off_state(psci_power_state_t *state_info)
24{
Varun Wadekar6311f632017-06-07 09:57:42 -070025 unsigned int lvl;
Soby Mathew8ee24982015-04-07 12:16:56 +010026
27 for (lvl = PSCI_CPU_PWR_LVL; lvl <= PLAT_MAX_PWR_LVL; lvl++)
28 state_info->pwr_domain_state[lvl] = PLAT_MAX_OFF_STATE;
29}
30
31/******************************************************************************
Soby Mathewb48349e2015-06-29 16:30:12 +010032 * Top level handler which is called when a cpu wants to power itself down.
Soby Mathew4067dc32015-05-05 16:33:16 +010033 * It's assumed that along with turning the cpu power domain off, power
34 * domains at higher levels will be turned off as far as possible. It finds
35 * the highest level where a domain has to be powered off by traversing the
36 * node information and then performs generic, architectural, platform setup
37 * and state management required to turn OFF that power domain and domains
38 * below it. e.g. For a cpu that's to be powered OFF, it could mean programming
39 * the power controller whereas for a cluster that's to be powered off, it will
40 * call the platform specific code which will disable coherency at the
41 * interconnect level if the cpu is the last in the cluster and also the
42 * program the power controller.
Soby Mathewb48349e2015-06-29 16:30:12 +010043 ******************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +010044int psci_do_cpu_off(unsigned int end_pwrlvl)
Soby Mathewb48349e2015-06-29 16:30:12 +010045{
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +010046 int rc = PSCI_E_SUCCESS;
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -060047 unsigned int idx = plat_my_core_pos();
Soby Mathew8ee24982015-04-07 12:16:56 +010048 psci_power_state_t state_info;
Andrew F. Davis74d27d02019-06-04 10:46:54 -040049 unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
Soby Mathewb48349e2015-06-29 16:30:12 +010050
51 /*
52 * This function must only be called on platforms where the
53 * CPU_OFF platform hooks have been implemented.
54 */
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +010055 assert(psci_plat_pm_ops->pwr_domain_off != NULL);
Soby Mathewb48349e2015-06-29 16:30:12 +010056
Roberto Vargas216e58a2017-09-04 16:49:41 +010057 /* Construct the psci_power_state for CPU_OFF */
58 psci_set_power_off_state(&state_info);
59
Soby Mathewb48349e2015-06-29 16:30:12 +010060 /*
Varun Wadekar6cf4ae92023-04-25 14:03:27 +010061 * Call the platform provided early CPU_OFF handler to allow
62 * platforms to perform any housekeeping activities before
63 * actually powering the CPU off. PSCI_E_DENIED indicates that
64 * the CPU off sequence should be aborted at this time.
65 */
66 if (psci_plat_pm_ops->pwr_domain_off_early) {
67 rc = psci_plat_pm_ops->pwr_domain_off_early(&state_info);
68 if (rc == PSCI_E_DENIED) {
69 return rc;
70 }
71 }
72
73 /*
Andrew F. Davis74d27d02019-06-04 10:46:54 -040074 * Get the parent nodes here, this is important to do before we
75 * initiate the power down sequence as after that point the core may
76 * have exited coherency and its cache may be disabled, any access to
77 * shared memory after that (such as the parent node lookup in
78 * psci_cpu_pd_nodes) can cause coherency issues on some platforms.
79 */
80 psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes);
81
82 /*
Soby Mathew4067dc32015-05-05 16:33:16 +010083 * This function acquires the lock corresponding to each power
Soby Mathewb48349e2015-06-29 16:30:12 +010084 * level so that by the time all locks are taken, the system topology
85 * is snapshot and state management can be done safely.
86 */
Andrew F. Davis74d27d02019-06-04 10:46:54 -040087 psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
Soby Mathewb48349e2015-06-29 16:30:12 +010088
89 /*
90 * Call the cpu off handler registered by the Secure Payload Dispatcher
91 * to let it do any bookkeeping. Assume that the SPD always reports an
92 * E_DENIED error if SP refuse to power down
93 */
Antonio Nino Diaz621d64f2018-07-16 23:19:25 +010094 if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_off != NULL)) {
Soby Mathewb48349e2015-06-29 16:30:12 +010095 rc = psci_spd_pm->svc_off(0);
Boyan Karatotev45c73282024-09-20 13:37:51 +010096 if (rc != PSCI_E_SUCCESS)
Maheedhar Bollapalli0839cfc2024-04-19 16:21:29 +053097 goto off_exit;
Soby Mathewb48349e2015-06-29 16:30:12 +010098 }
99
Soby Mathew8ee24982015-04-07 12:16:56 +0100100 /*
101 * This function is passed the requested state info and
102 * it returns the negotiated state info for each power level upto
103 * the end level specified.
104 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000105 psci_do_state_coordination(idx, end_pwrlvl, &state_info);
Soby Mathewb48349e2015-06-29 16:30:12 +0100106
Wing Lid3488612023-05-04 08:31:19 -0700107 /* Update the target state in the power domain nodes */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000108 psci_set_target_local_pwr_states(idx, end_pwrlvl, &state_info);
Wing Lid3488612023-05-04 08:31:19 -0700109
Yatharth Kochar170fb932016-05-09 18:26:35 +0100110#if ENABLE_PSCI_STAT
111 /* Update the last cpu for each level till end_pwrlvl */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000112 psci_stats_update_pwr_down(idx, end_pwrlvl, &state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100113#endif
114
Soby Mathew6590ce22015-06-30 11:00:24 +0100115 /*
Jeenu Viswambharanb0408e82017-01-05 11:01:02 +0000116 * Arch. management. Initiate power down sequence.
Soby Mathew6590ce22015-06-30 11:00:24 +0100117 */
Boyan Karatotev2b5e00d2024-12-19 16:07:29 +0000118 psci_pwrdown_cpu_start(psci_find_max_off_lvl(&state_info));
Soby Mathewb48349e2015-06-29 16:30:12 +0100119
120 /*
Soby Mathew6590ce22015-06-30 11:00:24 +0100121 * Plat. management: Perform platform specific actions to turn this
122 * cpu off e.g. exit cpu coherency, program the power controller etc.
Soby Mathewb48349e2015-06-29 16:30:12 +0100123 */
Soby Mathew8ee24982015-04-07 12:16:56 +0100124 psci_plat_pm_ops->pwr_domain_off(&state_info);
Soby Mathewb48349e2015-06-29 16:30:12 +0100125
Yatharth Kochar170fb932016-05-09 18:26:35 +0100126#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000127 plat_psci_stat_accounting_start(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100128#endif
129
Maheedhar Bollapalli0839cfc2024-04-19 16:21:29 +0530130off_exit:
Soby Mathewb48349e2015-06-29 16:30:12 +0100131 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100132 * Release the locks corresponding to each power level in the
Soby Mathewb48349e2015-06-29 16:30:12 +0100133 * reverse order to which they were acquired.
134 */
Andrew F. Davis74d27d02019-06-04 10:46:54 -0400135 psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
Soby Mathewb48349e2015-06-29 16:30:12 +0100136
137 /*
Soby Mathewb48349e2015-06-29 16:30:12 +0100138 * Check if all actions needed to safely power down this cpu have
Soby Mathew16e05cd2015-10-01 16:46:06 +0100139 * successfully completed.
Soby Mathewb48349e2015-06-29 16:30:12 +0100140 */
Soby Mathew16e05cd2015-10-01 16:46:06 +0100141 if (rc == PSCI_E_SUCCESS) {
142 /*
Jeenu Viswambharana10d3632017-01-06 14:58:11 +0000143 * Set the affinity info state to OFF. When caches are disabled,
144 * this writes directly to main memory, so cache maintenance is
Soby Mathew16e05cd2015-10-01 16:46:06 +0100145 * required to ensure that later cached reads of aff_info_state
Jeenu Viswambharana10d3632017-01-06 14:58:11 +0000146 * return AFF_STATE_OFF. A dsbish() ensures ordering of the
Soby Mathew203cdfe2016-01-26 11:47:53 +0000147 * update to the affinity info state prior to cache line
148 * invalidation.
Soby Mathew16e05cd2015-10-01 16:46:06 +0100149 */
Jeenu Viswambharana10d3632017-01-06 14:58:11 +0000150 psci_flush_cpu_data(psci_svc_cpu_data.aff_info_state);
Soby Mathew16e05cd2015-10-01 16:46:06 +0100151 psci_set_aff_info_state(AFF_STATE_OFF);
Jeenu Viswambharana10d3632017-01-06 14:58:11 +0000152 psci_dsbish();
153 psci_inv_cpu_data(psci_svc_cpu_data.aff_info_state);
Soby Mathew16e05cd2015-10-01 16:46:06 +0100154
dp-arm872be882016-09-19 11:18:44 +0100155#if ENABLE_RUNTIME_INSTRUMENTATION
dp-arm872be882016-09-19 11:18:44 +0100156 /*
157 * Update the timestamp with cache off. We assume this
158 * timestamp can only be read from the current CPU and the
159 * timestamp cache line will be flushed before return to
160 * normal world on wakeup.
161 */
162 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
163 RT_INSTR_ENTER_HW_LOW_PWR,
164 PMF_NO_CACHE_MAINT);
165#endif
Boyan Karatotevdb5fe4f2024-10-08 17:34:45 +0100166 if (psci_plat_pm_ops->pwr_domain_pwr_down != NULL) {
Boyan Karatotev2b5e00d2024-12-19 16:07:29 +0000167 /* This function may not return */
Boyan Karatotevdb5fe4f2024-10-08 17:34:45 +0100168 psci_plat_pm_ops->pwr_domain_pwr_down(&state_info);
Soby Mathewac1cc8e2016-04-27 14:46:28 +0100169 }
Boyan Karatotev2b5e00d2024-12-19 16:07:29 +0000170
171 psci_pwrdown_cpu_end_terminal();
Soby Mathew16e05cd2015-10-01 16:46:06 +0100172 }
Soby Mathewb48349e2015-06-29 16:30:12 +0100173
174 return rc;
175}