1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park *
4*54fd6939SJiyong Park * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park */
6*54fd6939SJiyong Park
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park #include <stddef.h>
9*54fd6939SJiyong Park
10*54fd6939SJiyong Park #include <arch.h>
11*54fd6939SJiyong Park #include <arch_helpers.h>
12*54fd6939SJiyong Park #include <common/bl_common.h>
13*54fd6939SJiyong Park #include <context.h>
14*54fd6939SJiyong Park #include <lib/el3_runtime/context_mgmt.h>
15*54fd6939SJiyong Park #include <lib/cpus/errata_report.h>
16*54fd6939SJiyong Park #include <plat/common/platform.h>
17*54fd6939SJiyong Park
18*54fd6939SJiyong Park #include "psci_private.h"
19*54fd6939SJiyong Park
20*54fd6939SJiyong Park /*
21*54fd6939SJiyong Park * Check that PLATFORM_CORE_COUNT fits into the number of cores
22*54fd6939SJiyong Park * that can be represented by PSCI_MAX_CPUS_INDEX.
23*54fd6939SJiyong Park */
24*54fd6939SJiyong Park CASSERT(PLATFORM_CORE_COUNT <= (PSCI_MAX_CPUS_INDEX + 1U), assert_psci_cores_overflow);
25*54fd6939SJiyong Park
26*54fd6939SJiyong Park /*******************************************************************************
27*54fd6939SJiyong Park * Per cpu non-secure contexts used to program the architectural state prior
28*54fd6939SJiyong Park * return to the normal world.
29*54fd6939SJiyong Park * TODO: Use the memory allocator to set aside memory for the contexts instead
30*54fd6939SJiyong Park * of relying on platform defined constants.
31*54fd6939SJiyong Park ******************************************************************************/
32*54fd6939SJiyong Park static cpu_context_t psci_ns_context[PLATFORM_CORE_COUNT];
33*54fd6939SJiyong Park
34*54fd6939SJiyong Park /******************************************************************************
35*54fd6939SJiyong Park * Define the psci capability variable.
36*54fd6939SJiyong Park *****************************************************************************/
37*54fd6939SJiyong Park unsigned int psci_caps;
38*54fd6939SJiyong Park
39*54fd6939SJiyong Park /*******************************************************************************
40*54fd6939SJiyong Park * Function which initializes the 'psci_non_cpu_pd_nodes' or the
41*54fd6939SJiyong Park * 'psci_cpu_pd_nodes' corresponding to the power level.
42*54fd6939SJiyong Park ******************************************************************************/
psci_init_pwr_domain_node(uint16_t node_idx,unsigned int parent_idx,unsigned char level)43*54fd6939SJiyong Park static void __init psci_init_pwr_domain_node(uint16_t node_idx,
44*54fd6939SJiyong Park unsigned int parent_idx,
45*54fd6939SJiyong Park unsigned char level)
46*54fd6939SJiyong Park {
47*54fd6939SJiyong Park if (level > PSCI_CPU_PWR_LVL) {
48*54fd6939SJiyong Park assert(node_idx < PSCI_NUM_NON_CPU_PWR_DOMAINS);
49*54fd6939SJiyong Park
50*54fd6939SJiyong Park psci_non_cpu_pd_nodes[node_idx].level = level;
51*54fd6939SJiyong Park psci_lock_init(psci_non_cpu_pd_nodes, node_idx);
52*54fd6939SJiyong Park psci_non_cpu_pd_nodes[node_idx].parent_node = parent_idx;
53*54fd6939SJiyong Park psci_non_cpu_pd_nodes[node_idx].local_state =
54*54fd6939SJiyong Park PLAT_MAX_OFF_STATE;
55*54fd6939SJiyong Park } else {
56*54fd6939SJiyong Park psci_cpu_data_t *svc_cpu_data;
57*54fd6939SJiyong Park
58*54fd6939SJiyong Park assert(node_idx < PLATFORM_CORE_COUNT);
59*54fd6939SJiyong Park
60*54fd6939SJiyong Park psci_cpu_pd_nodes[node_idx].parent_node = parent_idx;
61*54fd6939SJiyong Park
62*54fd6939SJiyong Park /* Initialize with an invalid mpidr */
63*54fd6939SJiyong Park psci_cpu_pd_nodes[node_idx].mpidr = PSCI_INVALID_MPIDR;
64*54fd6939SJiyong Park
65*54fd6939SJiyong Park svc_cpu_data =
66*54fd6939SJiyong Park &(_cpu_data_by_index(node_idx)->psci_svc_cpu_data);
67*54fd6939SJiyong Park
68*54fd6939SJiyong Park /* Set the Affinity Info for the cores as OFF */
69*54fd6939SJiyong Park svc_cpu_data->aff_info_state = AFF_STATE_OFF;
70*54fd6939SJiyong Park
71*54fd6939SJiyong Park /* Invalidate the suspend level for the cpu */
72*54fd6939SJiyong Park svc_cpu_data->target_pwrlvl = PSCI_INVALID_PWR_LVL;
73*54fd6939SJiyong Park
74*54fd6939SJiyong Park /* Set the power state to OFF state */
75*54fd6939SJiyong Park svc_cpu_data->local_state = PLAT_MAX_OFF_STATE;
76*54fd6939SJiyong Park
77*54fd6939SJiyong Park psci_flush_dcache_range((uintptr_t)svc_cpu_data,
78*54fd6939SJiyong Park sizeof(*svc_cpu_data));
79*54fd6939SJiyong Park
80*54fd6939SJiyong Park cm_set_context_by_index(node_idx,
81*54fd6939SJiyong Park (void *) &psci_ns_context[node_idx],
82*54fd6939SJiyong Park NON_SECURE);
83*54fd6939SJiyong Park }
84*54fd6939SJiyong Park }
85*54fd6939SJiyong Park
86*54fd6939SJiyong Park /*******************************************************************************
87*54fd6939SJiyong Park * This functions updates cpu_start_idx and ncpus field for each of the node in
88*54fd6939SJiyong Park * psci_non_cpu_pd_nodes[]. It does so by comparing the parent nodes of each of
89*54fd6939SJiyong Park * the CPUs and check whether they match with the parent of the previous
90*54fd6939SJiyong Park * CPU. The basic assumption for this work is that children of the same parent
91*54fd6939SJiyong Park * are allocated adjacent indices. The platform should ensure this though proper
92*54fd6939SJiyong Park * mapping of the CPUs to indices via plat_core_pos_by_mpidr() and
93*54fd6939SJiyong Park * plat_my_core_pos() APIs.
94*54fd6939SJiyong Park *******************************************************************************/
psci_update_pwrlvl_limits(void)95*54fd6939SJiyong Park static void __init psci_update_pwrlvl_limits(void)
96*54fd6939SJiyong Park {
97*54fd6939SJiyong Park unsigned int cpu_idx;
98*54fd6939SJiyong Park int j;
99*54fd6939SJiyong Park unsigned int nodes_idx[PLAT_MAX_PWR_LVL] = {0};
100*54fd6939SJiyong Park unsigned int temp_index[PLAT_MAX_PWR_LVL];
101*54fd6939SJiyong Park
102*54fd6939SJiyong Park for (cpu_idx = 0; cpu_idx < psci_plat_core_count; cpu_idx++) {
103*54fd6939SJiyong Park psci_get_parent_pwr_domain_nodes(cpu_idx,
104*54fd6939SJiyong Park PLAT_MAX_PWR_LVL,
105*54fd6939SJiyong Park temp_index);
106*54fd6939SJiyong Park for (j = (int)PLAT_MAX_PWR_LVL - 1; j >= 0; j--) {
107*54fd6939SJiyong Park if (temp_index[j] != nodes_idx[j]) {
108*54fd6939SJiyong Park nodes_idx[j] = temp_index[j];
109*54fd6939SJiyong Park psci_non_cpu_pd_nodes[nodes_idx[j]].cpu_start_idx
110*54fd6939SJiyong Park = cpu_idx;
111*54fd6939SJiyong Park }
112*54fd6939SJiyong Park psci_non_cpu_pd_nodes[nodes_idx[j]].ncpus++;
113*54fd6939SJiyong Park }
114*54fd6939SJiyong Park }
115*54fd6939SJiyong Park }
116*54fd6939SJiyong Park
117*54fd6939SJiyong Park /*******************************************************************************
118*54fd6939SJiyong Park * Core routine to populate the power domain tree. The tree descriptor passed by
119*54fd6939SJiyong Park * the platform is populated breadth-first and the first entry in the map
120*54fd6939SJiyong Park * informs the number of root power domains. The parent nodes of the root nodes
121*54fd6939SJiyong Park * will point to an invalid entry(-1).
122*54fd6939SJiyong Park ******************************************************************************/
populate_power_domain_tree(const unsigned char * topology)123*54fd6939SJiyong Park static unsigned int __init populate_power_domain_tree(const unsigned char
124*54fd6939SJiyong Park *topology)
125*54fd6939SJiyong Park {
126*54fd6939SJiyong Park unsigned int i, j = 0U, num_nodes_at_lvl = 1U, num_nodes_at_next_lvl;
127*54fd6939SJiyong Park unsigned int node_index = 0U, num_children;
128*54fd6939SJiyong Park unsigned int parent_node_index = 0U;
129*54fd6939SJiyong Park int level = (int)PLAT_MAX_PWR_LVL;
130*54fd6939SJiyong Park
131*54fd6939SJiyong Park /*
132*54fd6939SJiyong Park * For each level the inputs are:
133*54fd6939SJiyong Park * - number of nodes at this level in plat_array i.e. num_nodes_at_level
134*54fd6939SJiyong Park * This is the sum of values of nodes at the parent level.
135*54fd6939SJiyong Park * - Index of first entry at this level in the plat_array i.e.
136*54fd6939SJiyong Park * parent_node_index.
137*54fd6939SJiyong Park * - Index of first free entry in psci_non_cpu_pd_nodes[] or
138*54fd6939SJiyong Park * psci_cpu_pd_nodes[] i.e. node_index depending upon the level.
139*54fd6939SJiyong Park */
140*54fd6939SJiyong Park while (level >= (int) PSCI_CPU_PWR_LVL) {
141*54fd6939SJiyong Park num_nodes_at_next_lvl = 0U;
142*54fd6939SJiyong Park /*
143*54fd6939SJiyong Park * For each entry (parent node) at this level in the plat_array:
144*54fd6939SJiyong Park * - Find the number of children
145*54fd6939SJiyong Park * - Allocate a node in a power domain array for each child
146*54fd6939SJiyong Park * - Set the parent of the child to the parent_node_index - 1
147*54fd6939SJiyong Park * - Increment parent_node_index to point to the next parent
148*54fd6939SJiyong Park * - Accumulate the number of children at next level.
149*54fd6939SJiyong Park */
150*54fd6939SJiyong Park for (i = 0U; i < num_nodes_at_lvl; i++) {
151*54fd6939SJiyong Park assert(parent_node_index <=
152*54fd6939SJiyong Park PSCI_NUM_NON_CPU_PWR_DOMAINS);
153*54fd6939SJiyong Park num_children = topology[parent_node_index];
154*54fd6939SJiyong Park
155*54fd6939SJiyong Park for (j = node_index;
156*54fd6939SJiyong Park j < (node_index + num_children); j++)
157*54fd6939SJiyong Park psci_init_pwr_domain_node((uint16_t)j,
158*54fd6939SJiyong Park parent_node_index - 1U,
159*54fd6939SJiyong Park (unsigned char)level);
160*54fd6939SJiyong Park
161*54fd6939SJiyong Park node_index = j;
162*54fd6939SJiyong Park num_nodes_at_next_lvl += num_children;
163*54fd6939SJiyong Park parent_node_index++;
164*54fd6939SJiyong Park }
165*54fd6939SJiyong Park
166*54fd6939SJiyong Park num_nodes_at_lvl = num_nodes_at_next_lvl;
167*54fd6939SJiyong Park level--;
168*54fd6939SJiyong Park
169*54fd6939SJiyong Park /* Reset the index for the cpu power domain array */
170*54fd6939SJiyong Park if (level == (int) PSCI_CPU_PWR_LVL)
171*54fd6939SJiyong Park node_index = 0;
172*54fd6939SJiyong Park }
173*54fd6939SJiyong Park
174*54fd6939SJiyong Park /* Validate the sanity of array exported by the platform */
175*54fd6939SJiyong Park assert(j <= PLATFORM_CORE_COUNT);
176*54fd6939SJiyong Park return j;
177*54fd6939SJiyong Park }
178*54fd6939SJiyong Park
179*54fd6939SJiyong Park /*******************************************************************************
180*54fd6939SJiyong Park * This function does the architectural setup and takes the warm boot
181*54fd6939SJiyong Park * entry-point `mailbox_ep` as an argument. The function also initializes the
182*54fd6939SJiyong Park * power domain topology tree by querying the platform. The power domain nodes
183*54fd6939SJiyong Park * higher than the CPU are populated in the array psci_non_cpu_pd_nodes[] and
184*54fd6939SJiyong Park * the CPU power domains are populated in psci_cpu_pd_nodes[]. The platform
185*54fd6939SJiyong Park * exports its static topology map through the
186*54fd6939SJiyong Park * populate_power_domain_topology_tree() API. The algorithm populates the
187*54fd6939SJiyong Park * psci_non_cpu_pd_nodes and psci_cpu_pd_nodes iteratively by using this
188*54fd6939SJiyong Park * topology map. On a platform that implements two clusters of 2 cpus each,
189*54fd6939SJiyong Park * and supporting 3 domain levels, the populated psci_non_cpu_pd_nodes would
190*54fd6939SJiyong Park * look like this:
191*54fd6939SJiyong Park *
192*54fd6939SJiyong Park * ---------------------------------------------------
193*54fd6939SJiyong Park * | system node | cluster 0 node | cluster 1 node |
194*54fd6939SJiyong Park * ---------------------------------------------------
195*54fd6939SJiyong Park *
196*54fd6939SJiyong Park * And populated psci_cpu_pd_nodes would look like this :
197*54fd6939SJiyong Park * <- cpus cluster0 -><- cpus cluster1 ->
198*54fd6939SJiyong Park * ------------------------------------------------
199*54fd6939SJiyong Park * | CPU 0 | CPU 1 | CPU 2 | CPU 3 |
200*54fd6939SJiyong Park * ------------------------------------------------
201*54fd6939SJiyong Park ******************************************************************************/
psci_setup(const psci_lib_args_t * lib_args)202*54fd6939SJiyong Park int __init psci_setup(const psci_lib_args_t *lib_args)
203*54fd6939SJiyong Park {
204*54fd6939SJiyong Park const unsigned char *topology_tree;
205*54fd6939SJiyong Park
206*54fd6939SJiyong Park assert(VERIFY_PSCI_LIB_ARGS_V1(lib_args));
207*54fd6939SJiyong Park
208*54fd6939SJiyong Park /* Do the Architectural initialization */
209*54fd6939SJiyong Park psci_arch_setup();
210*54fd6939SJiyong Park
211*54fd6939SJiyong Park /* Query the topology map from the platform */
212*54fd6939SJiyong Park topology_tree = plat_get_power_domain_tree_desc();
213*54fd6939SJiyong Park
214*54fd6939SJiyong Park /* Populate the power domain arrays using the platform topology map */
215*54fd6939SJiyong Park psci_plat_core_count = populate_power_domain_tree(topology_tree);
216*54fd6939SJiyong Park
217*54fd6939SJiyong Park /* Update the CPU limits for each node in psci_non_cpu_pd_nodes */
218*54fd6939SJiyong Park psci_update_pwrlvl_limits();
219*54fd6939SJiyong Park
220*54fd6939SJiyong Park /* Populate the mpidr field of cpu node for this CPU */
221*54fd6939SJiyong Park psci_cpu_pd_nodes[plat_my_core_pos()].mpidr =
222*54fd6939SJiyong Park read_mpidr() & MPIDR_AFFINITY_MASK;
223*54fd6939SJiyong Park
224*54fd6939SJiyong Park psci_init_req_local_pwr_states();
225*54fd6939SJiyong Park
226*54fd6939SJiyong Park /*
227*54fd6939SJiyong Park * Set the requested and target state of this CPU and all the higher
228*54fd6939SJiyong Park * power domain levels for this CPU to run.
229*54fd6939SJiyong Park */
230*54fd6939SJiyong Park psci_set_pwr_domains_to_run(PLAT_MAX_PWR_LVL);
231*54fd6939SJiyong Park
232*54fd6939SJiyong Park (void) plat_setup_psci_ops((uintptr_t)lib_args->mailbox_ep,
233*54fd6939SJiyong Park &psci_plat_pm_ops);
234*54fd6939SJiyong Park assert(psci_plat_pm_ops != NULL);
235*54fd6939SJiyong Park
236*54fd6939SJiyong Park /*
237*54fd6939SJiyong Park * Flush `psci_plat_pm_ops` as it will be accessed by secondary CPUs
238*54fd6939SJiyong Park * during warm boot, possibly before data cache is enabled.
239*54fd6939SJiyong Park */
240*54fd6939SJiyong Park psci_flush_dcache_range((uintptr_t)&psci_plat_pm_ops,
241*54fd6939SJiyong Park sizeof(psci_plat_pm_ops));
242*54fd6939SJiyong Park
243*54fd6939SJiyong Park /* Initialize the psci capability */
244*54fd6939SJiyong Park psci_caps = PSCI_GENERIC_CAP;
245*54fd6939SJiyong Park
246*54fd6939SJiyong Park if (psci_plat_pm_ops->pwr_domain_off != NULL)
247*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_CPU_OFF);
248*54fd6939SJiyong Park if ((psci_plat_pm_ops->pwr_domain_on != NULL) &&
249*54fd6939SJiyong Park (psci_plat_pm_ops->pwr_domain_on_finish != NULL))
250*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_CPU_ON_AARCH64);
251*54fd6939SJiyong Park if ((psci_plat_pm_ops->pwr_domain_suspend != NULL) &&
252*54fd6939SJiyong Park (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL)) {
253*54fd6939SJiyong Park if (psci_plat_pm_ops->validate_power_state != NULL)
254*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_CPU_SUSPEND_AARCH64);
255*54fd6939SJiyong Park if (psci_plat_pm_ops->get_sys_suspend_power_state != NULL)
256*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_SYSTEM_SUSPEND_AARCH64);
257*54fd6939SJiyong Park }
258*54fd6939SJiyong Park if (psci_plat_pm_ops->system_off != NULL)
259*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_SYSTEM_OFF);
260*54fd6939SJiyong Park if (psci_plat_pm_ops->system_reset != NULL)
261*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_SYSTEM_RESET);
262*54fd6939SJiyong Park if (psci_plat_pm_ops->get_node_hw_state != NULL)
263*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_NODE_HW_STATE_AARCH64);
264*54fd6939SJiyong Park if ((psci_plat_pm_ops->read_mem_protect != NULL) &&
265*54fd6939SJiyong Park (psci_plat_pm_ops->write_mem_protect != NULL))
266*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_MEM_PROTECT);
267*54fd6939SJiyong Park if (psci_plat_pm_ops->mem_protect_chk != NULL)
268*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_MEM_CHK_RANGE_AARCH64);
269*54fd6939SJiyong Park if (psci_plat_pm_ops->system_reset2 != NULL)
270*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_SYSTEM_RESET2_AARCH64);
271*54fd6939SJiyong Park
272*54fd6939SJiyong Park #if ENABLE_PSCI_STAT
273*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_STAT_RESIDENCY_AARCH64);
274*54fd6939SJiyong Park psci_caps |= define_psci_cap(PSCI_STAT_COUNT_AARCH64);
275*54fd6939SJiyong Park #endif
276*54fd6939SJiyong Park
277*54fd6939SJiyong Park return 0;
278*54fd6939SJiyong Park }
279*54fd6939SJiyong Park
280*54fd6939SJiyong Park /*******************************************************************************
281*54fd6939SJiyong Park * This duplicates what the primary cpu did after a cold boot in BL1. The same
282*54fd6939SJiyong Park * needs to be done when a cpu is hotplugged in. This function could also over-
283*54fd6939SJiyong Park * ride any EL3 setup done by BL1 as this code resides in rw memory.
284*54fd6939SJiyong Park ******************************************************************************/
psci_arch_setup(void)285*54fd6939SJiyong Park void psci_arch_setup(void)
286*54fd6939SJiyong Park {
287*54fd6939SJiyong Park #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER)
288*54fd6939SJiyong Park /* Program the counter frequency */
289*54fd6939SJiyong Park write_cntfrq_el0(plat_get_syscnt_freq2());
290*54fd6939SJiyong Park #endif
291*54fd6939SJiyong Park
292*54fd6939SJiyong Park /* Initialize the cpu_ops pointer. */
293*54fd6939SJiyong Park init_cpu_ops();
294*54fd6939SJiyong Park
295*54fd6939SJiyong Park /* Having initialized cpu_ops, we can now print errata status */
296*54fd6939SJiyong Park print_errata_status();
297*54fd6939SJiyong Park
298*54fd6939SJiyong Park #if ENABLE_PAUTH
299*54fd6939SJiyong Park /* Store APIAKey_EL1 key */
300*54fd6939SJiyong Park set_cpu_data(apiakey[0], read_apiakeylo_el1());
301*54fd6939SJiyong Park set_cpu_data(apiakey[1], read_apiakeyhi_el1());
302*54fd6939SJiyong Park #endif /* ENABLE_PAUTH */
303*54fd6939SJiyong Park }
304*54fd6939SJiyong Park
305*54fd6939SJiyong Park /******************************************************************************
306*54fd6939SJiyong Park * PSCI Library interface to initialize the cpu context for the next non
307*54fd6939SJiyong Park * secure image during cold boot. The relevant registers in the cpu context
308*54fd6939SJiyong Park * need to be retrieved and programmed on return from this interface.
309*54fd6939SJiyong Park *****************************************************************************/
psci_prepare_next_non_secure_ctx(entry_point_info_t * next_image_info)310*54fd6939SJiyong Park void psci_prepare_next_non_secure_ctx(entry_point_info_t *next_image_info)
311*54fd6939SJiyong Park {
312*54fd6939SJiyong Park assert(GET_SECURITY_STATE(next_image_info->h.attr) == NON_SECURE);
313*54fd6939SJiyong Park cm_init_my_context(next_image_info);
314*54fd6939SJiyong Park cm_prepare_el3_exit(NON_SECURE);
315*54fd6939SJiyong Park }
316