xref: /rk3399_ARM-atf/lib/psci/psci_setup.c (revision 6b7b0f368680ce657390d0a046871d880d704b2d)
1532ed618SSoby Mathew /*
2*6b7b0f36SAntonio Nino Diaz  * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew  */
6532ed618SSoby Mathew 
7532ed618SSoby Mathew #include <arch.h>
8532ed618SSoby Mathew #include <arch_helpers.h>
9532ed618SSoby Mathew #include <assert.h>
10532ed618SSoby Mathew #include <bl_common.h>
11532ed618SSoby Mathew #include <context.h>
12532ed618SSoby Mathew #include <context_mgmt.h>
1310bcd761SJeenu Viswambharan #include <errata_report.h>
14532ed618SSoby Mathew #include <platform.h>
15532ed618SSoby Mathew #include <stddef.h>
16532ed618SSoby Mathew #include "psci_private.h"
17532ed618SSoby Mathew 
18532ed618SSoby Mathew /*******************************************************************************
19532ed618SSoby Mathew  * Per cpu non-secure contexts used to program the architectural state prior
20532ed618SSoby Mathew  * return to the normal world.
21532ed618SSoby Mathew  * TODO: Use the memory allocator to set aside memory for the contexts instead
22532ed618SSoby Mathew  * of relying on platform defined constants.
23532ed618SSoby Mathew  ******************************************************************************/
24532ed618SSoby Mathew static cpu_context_t psci_ns_context[PLATFORM_CORE_COUNT];
25532ed618SSoby Mathew 
26532ed618SSoby Mathew /******************************************************************************
27532ed618SSoby Mathew  * Define the psci capability variable.
28532ed618SSoby Mathew  *****************************************************************************/
29532ed618SSoby Mathew unsigned int psci_caps;
30532ed618SSoby Mathew 
31532ed618SSoby Mathew /*******************************************************************************
32532ed618SSoby Mathew  * Function which initializes the 'psci_non_cpu_pd_nodes' or the
33532ed618SSoby Mathew  * 'psci_cpu_pd_nodes' corresponding to the power level.
34532ed618SSoby Mathew  ******************************************************************************/
35*6b7b0f36SAntonio Nino Diaz static void psci_init_pwr_domain_node(unsigned char node_idx,
36532ed618SSoby Mathew 					unsigned int parent_idx,
37*6b7b0f36SAntonio Nino Diaz 					unsigned char level)
38532ed618SSoby Mathew {
39532ed618SSoby Mathew 	if (level > PSCI_CPU_PWR_LVL) {
40532ed618SSoby Mathew 		psci_non_cpu_pd_nodes[node_idx].level = level;
41532ed618SSoby Mathew 		psci_lock_init(psci_non_cpu_pd_nodes, node_idx);
42532ed618SSoby Mathew 		psci_non_cpu_pd_nodes[node_idx].parent_node = parent_idx;
43532ed618SSoby Mathew 		psci_non_cpu_pd_nodes[node_idx].local_state =
44532ed618SSoby Mathew 							 PLAT_MAX_OFF_STATE;
45532ed618SSoby Mathew 	} else {
46532ed618SSoby Mathew 		psci_cpu_data_t *svc_cpu_data;
47532ed618SSoby Mathew 
48532ed618SSoby Mathew 		psci_cpu_pd_nodes[node_idx].parent_node = parent_idx;
49532ed618SSoby Mathew 
50532ed618SSoby Mathew 		/* Initialize with an invalid mpidr */
51532ed618SSoby Mathew 		psci_cpu_pd_nodes[node_idx].mpidr = PSCI_INVALID_MPIDR;
52532ed618SSoby Mathew 
53532ed618SSoby Mathew 		svc_cpu_data =
54532ed618SSoby Mathew 			&(_cpu_data_by_index(node_idx)->psci_svc_cpu_data);
55532ed618SSoby Mathew 
56532ed618SSoby Mathew 		/* Set the Affinity Info for the cores as OFF */
57532ed618SSoby Mathew 		svc_cpu_data->aff_info_state = AFF_STATE_OFF;
58532ed618SSoby Mathew 
59532ed618SSoby Mathew 		/* Invalidate the suspend level for the cpu */
60532ed618SSoby Mathew 		svc_cpu_data->target_pwrlvl = PSCI_INVALID_PWR_LVL;
61532ed618SSoby Mathew 
62532ed618SSoby Mathew 		/* Set the power state to OFF state */
63532ed618SSoby Mathew 		svc_cpu_data->local_state = PLAT_MAX_OFF_STATE;
64532ed618SSoby Mathew 
65a10d3632SJeenu Viswambharan 		psci_flush_dcache_range((uintptr_t)svc_cpu_data,
66532ed618SSoby Mathew 						 sizeof(*svc_cpu_data));
67532ed618SSoby Mathew 
68532ed618SSoby Mathew 		cm_set_context_by_index(node_idx,
69532ed618SSoby Mathew 					(void *) &psci_ns_context[node_idx],
70532ed618SSoby Mathew 					NON_SECURE);
71532ed618SSoby Mathew 	}
72532ed618SSoby Mathew }
73532ed618SSoby Mathew 
74532ed618SSoby Mathew /*******************************************************************************
75532ed618SSoby Mathew  * This functions updates cpu_start_idx and ncpus field for each of the node in
76532ed618SSoby Mathew  * psci_non_cpu_pd_nodes[]. It does so by comparing the parent nodes of each of
77532ed618SSoby Mathew  * the CPUs and check whether they match with the parent of the previous
78532ed618SSoby Mathew  * CPU. The basic assumption for this work is that children of the same parent
79532ed618SSoby Mathew  * are allocated adjacent indices. The platform should ensure this though proper
80532ed618SSoby Mathew  * mapping of the CPUs to indices via plat_core_pos_by_mpidr() and
81532ed618SSoby Mathew  * plat_my_core_pos() APIs.
82532ed618SSoby Mathew  *******************************************************************************/
83532ed618SSoby Mathew static void psci_update_pwrlvl_limits(void)
84532ed618SSoby Mathew {
85*6b7b0f36SAntonio Nino Diaz 	int j, cpu_idx;
86532ed618SSoby Mathew 	unsigned int nodes_idx[PLAT_MAX_PWR_LVL] = {0};
87*6b7b0f36SAntonio Nino Diaz 	unsigned int temp_index[PLAT_MAX_PWR_LVL];
88532ed618SSoby Mathew 
89532ed618SSoby Mathew 	for (cpu_idx = 0; cpu_idx < PLATFORM_CORE_COUNT; cpu_idx++) {
90532ed618SSoby Mathew 		psci_get_parent_pwr_domain_nodes(cpu_idx,
91*6b7b0f36SAntonio Nino Diaz 						 (unsigned int)PLAT_MAX_PWR_LVL,
92532ed618SSoby Mathew 						 temp_index);
93*6b7b0f36SAntonio Nino Diaz 		for (j = (int) PLAT_MAX_PWR_LVL - 1; j >= 0; j--) {
94532ed618SSoby Mathew 			if (temp_index[j] != nodes_idx[j]) {
95532ed618SSoby Mathew 				nodes_idx[j] = temp_index[j];
96532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[nodes_idx[j]].cpu_start_idx
97532ed618SSoby Mathew 					= cpu_idx;
98532ed618SSoby Mathew 			}
99532ed618SSoby Mathew 			psci_non_cpu_pd_nodes[nodes_idx[j]].ncpus++;
100532ed618SSoby Mathew 		}
101532ed618SSoby Mathew 	}
102532ed618SSoby Mathew }
103532ed618SSoby Mathew 
104532ed618SSoby Mathew /*******************************************************************************
105532ed618SSoby Mathew  * Core routine to populate the power domain tree. The tree descriptor passed by
106532ed618SSoby Mathew  * the platform is populated breadth-first and the first entry in the map
107532ed618SSoby Mathew  * informs the number of root power domains. The parent nodes of the root nodes
108532ed618SSoby Mathew  * will point to an invalid entry(-1).
109532ed618SSoby Mathew  ******************************************************************************/
110532ed618SSoby Mathew static void populate_power_domain_tree(const unsigned char *topology)
111532ed618SSoby Mathew {
112*6b7b0f36SAntonio Nino Diaz 	unsigned int i, j = 0U, num_nodes_at_lvl = 1U, num_nodes_at_next_lvl;
113*6b7b0f36SAntonio Nino Diaz 	unsigned int node_index = 0U, num_children;
114*6b7b0f36SAntonio Nino Diaz 	int parent_node_index = 0;
115*6b7b0f36SAntonio Nino Diaz 	int level = (int) PLAT_MAX_PWR_LVL;
116532ed618SSoby Mathew 
117532ed618SSoby Mathew 	/*
118532ed618SSoby Mathew 	 * For each level the inputs are:
119532ed618SSoby Mathew 	 * - number of nodes at this level in plat_array i.e. num_nodes_at_level
120532ed618SSoby Mathew 	 *   This is the sum of values of nodes at the parent level.
121532ed618SSoby Mathew 	 * - Index of first entry at this level in the plat_array i.e.
122532ed618SSoby Mathew 	 *   parent_node_index.
123532ed618SSoby Mathew 	 * - Index of first free entry in psci_non_cpu_pd_nodes[] or
124532ed618SSoby Mathew 	 *   psci_cpu_pd_nodes[] i.e. node_index depending upon the level.
125532ed618SSoby Mathew 	 */
126*6b7b0f36SAntonio Nino Diaz 	while (level >= (int) PSCI_CPU_PWR_LVL) {
127*6b7b0f36SAntonio Nino Diaz 		num_nodes_at_next_lvl = 0U;
128532ed618SSoby Mathew 		/*
129532ed618SSoby Mathew 		 * For each entry (parent node) at this level in the plat_array:
130532ed618SSoby Mathew 		 * - Find the number of children
131532ed618SSoby Mathew 		 * - Allocate a node in a power domain array for each child
132532ed618SSoby Mathew 		 * - Set the parent of the child to the parent_node_index - 1
133532ed618SSoby Mathew 		 * - Increment parent_node_index to point to the next parent
134532ed618SSoby Mathew 		 * - Accumulate the number of children at next level.
135532ed618SSoby Mathew 		 */
136*6b7b0f36SAntonio Nino Diaz 		for (i = 0U; i < num_nodes_at_lvl; i++) {
137532ed618SSoby Mathew 			assert(parent_node_index <=
138532ed618SSoby Mathew 					PSCI_NUM_NON_CPU_PWR_DOMAINS);
139532ed618SSoby Mathew 			num_children = topology[parent_node_index];
140532ed618SSoby Mathew 
141532ed618SSoby Mathew 			for (j = node_index;
142*6b7b0f36SAntonio Nino Diaz 				j < (node_index + num_children); j++)
143*6b7b0f36SAntonio Nino Diaz 				psci_init_pwr_domain_node((unsigned char)j,
144532ed618SSoby Mathew 							  parent_node_index - 1,
145*6b7b0f36SAntonio Nino Diaz 							  (unsigned char)level);
146532ed618SSoby Mathew 
147532ed618SSoby Mathew 			node_index = j;
148532ed618SSoby Mathew 			num_nodes_at_next_lvl += num_children;
149532ed618SSoby Mathew 			parent_node_index++;
150532ed618SSoby Mathew 		}
151532ed618SSoby Mathew 
152532ed618SSoby Mathew 		num_nodes_at_lvl = num_nodes_at_next_lvl;
153532ed618SSoby Mathew 		level--;
154532ed618SSoby Mathew 
155532ed618SSoby Mathew 		/* Reset the index for the cpu power domain array */
156*6b7b0f36SAntonio Nino Diaz 		if (level == (int) PSCI_CPU_PWR_LVL)
157532ed618SSoby Mathew 			node_index = 0;
158532ed618SSoby Mathew 	}
159532ed618SSoby Mathew 
160532ed618SSoby Mathew 	/* Validate the sanity of array exported by the platform */
161*6b7b0f36SAntonio Nino Diaz 	assert((int) j == PLATFORM_CORE_COUNT);
162532ed618SSoby Mathew }
163532ed618SSoby Mathew 
164532ed618SSoby Mathew /*******************************************************************************
165cf0b1492SSoby Mathew  * This function does the architectural setup and takes the warm boot
166cf0b1492SSoby Mathew  * entry-point `mailbox_ep` as an argument. The function also initializes the
167cf0b1492SSoby Mathew  * power domain topology tree by querying the platform. The power domain nodes
168cf0b1492SSoby Mathew  * higher than the CPU are populated in the array psci_non_cpu_pd_nodes[] and
169cf0b1492SSoby Mathew  * the CPU power domains are populated in psci_cpu_pd_nodes[]. The platform
170cf0b1492SSoby Mathew  * exports its static topology map through the
171532ed618SSoby Mathew  * populate_power_domain_topology_tree() API. The algorithm populates the
172532ed618SSoby Mathew  * psci_non_cpu_pd_nodes and psci_cpu_pd_nodes iteratively by using this
173cf0b1492SSoby Mathew  * topology map.  On a platform that implements two clusters of 2 cpus each,
174cf0b1492SSoby Mathew  * and supporting 3 domain levels, the populated psci_non_cpu_pd_nodes would
175cf0b1492SSoby Mathew  * look like this:
176532ed618SSoby Mathew  *
177532ed618SSoby Mathew  * ---------------------------------------------------
178532ed618SSoby Mathew  * | system node | cluster 0 node  | cluster 1 node  |
179532ed618SSoby Mathew  * ---------------------------------------------------
180532ed618SSoby Mathew  *
181532ed618SSoby Mathew  * And populated psci_cpu_pd_nodes would look like this :
182532ed618SSoby Mathew  * <-    cpus cluster0   -><-   cpus cluster1   ->
183532ed618SSoby Mathew  * ------------------------------------------------
184532ed618SSoby Mathew  * |   CPU 0   |   CPU 1   |   CPU 2   |   CPU 3  |
185532ed618SSoby Mathew  * ------------------------------------------------
186532ed618SSoby Mathew  ******************************************************************************/
187f426fc05SSoby Mathew int psci_setup(const psci_lib_args_t *lib_args)
188532ed618SSoby Mathew {
189532ed618SSoby Mathew 	const unsigned char *topology_tree;
190532ed618SSoby Mathew 
191f426fc05SSoby Mathew 	assert(VERIFY_PSCI_LIB_ARGS_V1(lib_args));
192f426fc05SSoby Mathew 
193cf0b1492SSoby Mathew 	/* Do the Architectural initialization */
194cf0b1492SSoby Mathew 	psci_arch_setup();
195cf0b1492SSoby Mathew 
196532ed618SSoby Mathew 	/* Query the topology map from the platform */
197532ed618SSoby Mathew 	topology_tree = plat_get_power_domain_tree_desc();
198532ed618SSoby Mathew 
199532ed618SSoby Mathew 	/* Populate the power domain arrays using the platform topology map */
200532ed618SSoby Mathew 	populate_power_domain_tree(topology_tree);
201532ed618SSoby Mathew 
202532ed618SSoby Mathew 	/* Update the CPU limits for each node in psci_non_cpu_pd_nodes */
203532ed618SSoby Mathew 	psci_update_pwrlvl_limits();
204532ed618SSoby Mathew 
205532ed618SSoby Mathew 	/* Populate the mpidr field of cpu node for this CPU */
206532ed618SSoby Mathew 	psci_cpu_pd_nodes[plat_my_core_pos()].mpidr =
207532ed618SSoby Mathew 		read_mpidr() & MPIDR_AFFINITY_MASK;
208532ed618SSoby Mathew 
209532ed618SSoby Mathew 	psci_init_req_local_pwr_states();
210532ed618SSoby Mathew 
211532ed618SSoby Mathew 	/*
212532ed618SSoby Mathew 	 * Set the requested and target state of this CPU and all the higher
213532ed618SSoby Mathew 	 * power domain levels for this CPU to run.
214532ed618SSoby Mathew 	 */
215532ed618SSoby Mathew 	psci_set_pwr_domains_to_run(PLAT_MAX_PWR_LVL);
216532ed618SSoby Mathew 
217*6b7b0f36SAntonio Nino Diaz 	(void) plat_setup_psci_ops((uintptr_t)lib_args->mailbox_ep,
218*6b7b0f36SAntonio Nino Diaz 				   &psci_plat_pm_ops);
219*6b7b0f36SAntonio Nino Diaz 	assert(psci_plat_pm_ops != NULL);
220532ed618SSoby Mathew 
2217a3d4bdeSSoby Mathew 	/*
2227a3d4bdeSSoby Mathew 	 * Flush `psci_plat_pm_ops` as it will be accessed by secondary CPUs
223a10d3632SJeenu Viswambharan 	 * during warm boot, possibly before data cache is enabled.
2247a3d4bdeSSoby Mathew 	 */
225a10d3632SJeenu Viswambharan 	psci_flush_dcache_range((uintptr_t)&psci_plat_pm_ops,
2267a3d4bdeSSoby Mathew 					sizeof(psci_plat_pm_ops));
2277a3d4bdeSSoby Mathew 
228532ed618SSoby Mathew 	/* Initialize the psci capability */
229532ed618SSoby Mathew 	psci_caps = PSCI_GENERIC_CAP;
230532ed618SSoby Mathew 
231*6b7b0f36SAntonio Nino Diaz 	if (psci_plat_pm_ops->pwr_domain_off != NULL)
232532ed618SSoby Mathew 		psci_caps |=  define_psci_cap(PSCI_CPU_OFF);
233*6b7b0f36SAntonio Nino Diaz 	if ((psci_plat_pm_ops->pwr_domain_on != NULL) &&
234*6b7b0f36SAntonio Nino Diaz 	    (psci_plat_pm_ops->pwr_domain_on_finish != NULL))
235532ed618SSoby Mathew 		psci_caps |=  define_psci_cap(PSCI_CPU_ON_AARCH64);
236*6b7b0f36SAntonio Nino Diaz 	if ((psci_plat_pm_ops->pwr_domain_suspend != NULL) &&
237*6b7b0f36SAntonio Nino Diaz 	    (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL)) {
238532ed618SSoby Mathew 		psci_caps |=  define_psci_cap(PSCI_CPU_SUSPEND_AARCH64);
239*6b7b0f36SAntonio Nino Diaz 		if (psci_plat_pm_ops->get_sys_suspend_power_state != NULL)
240532ed618SSoby Mathew 			psci_caps |=  define_psci_cap(PSCI_SYSTEM_SUSPEND_AARCH64);
241532ed618SSoby Mathew 	}
242*6b7b0f36SAntonio Nino Diaz 	if (psci_plat_pm_ops->system_off != NULL)
243532ed618SSoby Mathew 		psci_caps |=  define_psci_cap(PSCI_SYSTEM_OFF);
244*6b7b0f36SAntonio Nino Diaz 	if (psci_plat_pm_ops->system_reset != NULL)
245532ed618SSoby Mathew 		psci_caps |=  define_psci_cap(PSCI_SYSTEM_RESET);
246*6b7b0f36SAntonio Nino Diaz 	if (psci_plat_pm_ops->get_node_hw_state != NULL)
24728d3d614SJeenu Viswambharan 		psci_caps |= define_psci_cap(PSCI_NODE_HW_STATE_AARCH64);
248*6b7b0f36SAntonio Nino Diaz 	if ((psci_plat_pm_ops->read_mem_protect != NULL) &&
249*6b7b0f36SAntonio Nino Diaz 			(psci_plat_pm_ops->write_mem_protect != NULL))
250d4c596beSRoberto Vargas 		psci_caps |= define_psci_cap(PSCI_MEM_PROTECT);
251*6b7b0f36SAntonio Nino Diaz 	if (psci_plat_pm_ops->mem_protect_chk != NULL)
252d4c596beSRoberto Vargas 		psci_caps |= define_psci_cap(PSCI_MEM_CHK_RANGE_AARCH64);
253*6b7b0f36SAntonio Nino Diaz 	if (psci_plat_pm_ops->system_reset2 != NULL)
25436a8f8fdSRoberto Vargas 		psci_caps |= define_psci_cap(PSCI_SYSTEM_RESET2_AARCH64);
255532ed618SSoby Mathew 
256532ed618SSoby Mathew #if ENABLE_PSCI_STAT
257532ed618SSoby Mathew 	psci_caps |=  define_psci_cap(PSCI_STAT_RESIDENCY_AARCH64);
258532ed618SSoby Mathew 	psci_caps |=  define_psci_cap(PSCI_STAT_COUNT_AARCH64);
259532ed618SSoby Mathew #endif
260532ed618SSoby Mathew 
261532ed618SSoby Mathew 	return 0;
262532ed618SSoby Mathew }
263cf0b1492SSoby Mathew 
264cf0b1492SSoby Mathew /*******************************************************************************
265cf0b1492SSoby Mathew  * This duplicates what the primary cpu did after a cold boot in BL1. The same
266cf0b1492SSoby Mathew  * needs to be done when a cpu is hotplugged in. This function could also over-
267cf0b1492SSoby Mathew  * ride any EL3 setup done by BL1 as this code resides in rw memory.
268cf0b1492SSoby Mathew  ******************************************************************************/
269cf0b1492SSoby Mathew void psci_arch_setup(void)
270cf0b1492SSoby Mathew {
271*6b7b0f36SAntonio Nino Diaz #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER)
272cf0b1492SSoby Mathew 	/* Program the counter frequency */
273cf0b1492SSoby Mathew 	write_cntfrq_el0(plat_get_syscnt_freq2());
27486e26835SEtienne Carriere #endif
275cf0b1492SSoby Mathew 
276cf0b1492SSoby Mathew 	/* Initialize the cpu_ops pointer. */
277cf0b1492SSoby Mathew 	init_cpu_ops();
27810bcd761SJeenu Viswambharan 
27910bcd761SJeenu Viswambharan 	/* Having initialized cpu_ops, we can now print errata status */
28010bcd761SJeenu Viswambharan 	print_errata_status();
281cf0b1492SSoby Mathew }
282727e5238SSoby Mathew 
283727e5238SSoby Mathew /******************************************************************************
284727e5238SSoby Mathew  * PSCI Library interface to initialize the cpu context for the next non
285727e5238SSoby Mathew  * secure image during cold boot. The relevant registers in the cpu context
286727e5238SSoby Mathew  * need to be retrieved and programmed on return from this interface.
287727e5238SSoby Mathew  *****************************************************************************/
288727e5238SSoby Mathew void psci_prepare_next_non_secure_ctx(entry_point_info_t *next_image_info)
289727e5238SSoby Mathew {
290727e5238SSoby Mathew 	assert(GET_SECURITY_STATE(next_image_info->h.attr) == NON_SECURE);
291727e5238SSoby Mathew 	cm_init_my_context(next_image_info);
292727e5238SSoby Mathew 	cm_prepare_el3_exit(NON_SECURE);
293727e5238SSoby Mathew }
294