xref: /rk3399_ARM-atf/lib/psci/psci_setup.c (revision 28d3d614b57730bdf364e49259d3c42599d26145)
1532ed618SSoby Mathew /*
2532ed618SSoby Mathew  * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
4532ed618SSoby Mathew  * Redistribution and use in source and binary forms, with or without
5532ed618SSoby Mathew  * modification, are permitted provided that the following conditions are met:
6532ed618SSoby Mathew  *
7532ed618SSoby Mathew  * Redistributions of source code must retain the above copyright notice, this
8532ed618SSoby Mathew  * list of conditions and the following disclaimer.
9532ed618SSoby Mathew  *
10532ed618SSoby Mathew  * Redistributions in binary form must reproduce the above copyright notice,
11532ed618SSoby Mathew  * this list of conditions and the following disclaimer in the documentation
12532ed618SSoby Mathew  * and/or other materials provided with the distribution.
13532ed618SSoby Mathew  *
14532ed618SSoby Mathew  * Neither the name of ARM nor the names of its contributors may be used
15532ed618SSoby Mathew  * to endorse or promote products derived from this software without specific
16532ed618SSoby Mathew  * prior written permission.
17532ed618SSoby Mathew  *
18532ed618SSoby Mathew  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19532ed618SSoby Mathew  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20532ed618SSoby Mathew  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21532ed618SSoby Mathew  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22532ed618SSoby Mathew  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23532ed618SSoby Mathew  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24532ed618SSoby Mathew  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25532ed618SSoby Mathew  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26532ed618SSoby Mathew  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27532ed618SSoby Mathew  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28532ed618SSoby Mathew  * POSSIBILITY OF SUCH DAMAGE.
29532ed618SSoby Mathew  */
30532ed618SSoby Mathew 
31532ed618SSoby Mathew #include <arch.h>
32532ed618SSoby Mathew #include <arch_helpers.h>
33532ed618SSoby Mathew #include <assert.h>
34532ed618SSoby Mathew #include <bl_common.h>
35532ed618SSoby Mathew #include <context.h>
36532ed618SSoby Mathew #include <context_mgmt.h>
37532ed618SSoby Mathew #include <platform.h>
38532ed618SSoby Mathew #include <stddef.h>
39532ed618SSoby Mathew #include "psci_private.h"
40532ed618SSoby Mathew 
41532ed618SSoby Mathew /*******************************************************************************
42532ed618SSoby Mathew  * Per cpu non-secure contexts used to program the architectural state prior
43532ed618SSoby Mathew  * return to the normal world.
44532ed618SSoby Mathew  * TODO: Use the memory allocator to set aside memory for the contexts instead
45532ed618SSoby Mathew  * of relying on platform defined constants.
46532ed618SSoby Mathew  ******************************************************************************/
47532ed618SSoby Mathew static cpu_context_t psci_ns_context[PLATFORM_CORE_COUNT];
48532ed618SSoby Mathew 
49532ed618SSoby Mathew /******************************************************************************
50532ed618SSoby Mathew  * Define the psci capability variable.
51532ed618SSoby Mathew  *****************************************************************************/
52532ed618SSoby Mathew unsigned int psci_caps;
53532ed618SSoby Mathew 
54532ed618SSoby Mathew /*******************************************************************************
55532ed618SSoby Mathew  * Function which initializes the 'psci_non_cpu_pd_nodes' or the
56532ed618SSoby Mathew  * 'psci_cpu_pd_nodes' corresponding to the power level.
57532ed618SSoby Mathew  ******************************************************************************/
58532ed618SSoby Mathew static void psci_init_pwr_domain_node(unsigned int node_idx,
59532ed618SSoby Mathew 					unsigned int parent_idx,
60532ed618SSoby Mathew 					unsigned int level)
61532ed618SSoby Mathew {
62532ed618SSoby Mathew 	if (level > PSCI_CPU_PWR_LVL) {
63532ed618SSoby Mathew 		psci_non_cpu_pd_nodes[node_idx].level = level;
64532ed618SSoby Mathew 		psci_lock_init(psci_non_cpu_pd_nodes, node_idx);
65532ed618SSoby Mathew 		psci_non_cpu_pd_nodes[node_idx].parent_node = parent_idx;
66532ed618SSoby Mathew 		psci_non_cpu_pd_nodes[node_idx].local_state =
67532ed618SSoby Mathew 							 PLAT_MAX_OFF_STATE;
68532ed618SSoby Mathew 	} else {
69532ed618SSoby Mathew 		psci_cpu_data_t *svc_cpu_data;
70532ed618SSoby Mathew 
71532ed618SSoby Mathew 		psci_cpu_pd_nodes[node_idx].parent_node = parent_idx;
72532ed618SSoby Mathew 
73532ed618SSoby Mathew 		/* Initialize with an invalid mpidr */
74532ed618SSoby Mathew 		psci_cpu_pd_nodes[node_idx].mpidr = PSCI_INVALID_MPIDR;
75532ed618SSoby Mathew 
76532ed618SSoby Mathew 		svc_cpu_data =
77532ed618SSoby Mathew 			&(_cpu_data_by_index(node_idx)->psci_svc_cpu_data);
78532ed618SSoby Mathew 
79532ed618SSoby Mathew 		/* Set the Affinity Info for the cores as OFF */
80532ed618SSoby Mathew 		svc_cpu_data->aff_info_state = AFF_STATE_OFF;
81532ed618SSoby Mathew 
82532ed618SSoby Mathew 		/* Invalidate the suspend level for the cpu */
83532ed618SSoby Mathew 		svc_cpu_data->target_pwrlvl = PSCI_INVALID_PWR_LVL;
84532ed618SSoby Mathew 
85532ed618SSoby Mathew 		/* Set the power state to OFF state */
86532ed618SSoby Mathew 		svc_cpu_data->local_state = PLAT_MAX_OFF_STATE;
87532ed618SSoby Mathew 
88532ed618SSoby Mathew 		flush_dcache_range((uintptr_t)svc_cpu_data,
89532ed618SSoby Mathew 						 sizeof(*svc_cpu_data));
90532ed618SSoby Mathew 
91532ed618SSoby Mathew 		cm_set_context_by_index(node_idx,
92532ed618SSoby Mathew 					(void *) &psci_ns_context[node_idx],
93532ed618SSoby Mathew 					NON_SECURE);
94532ed618SSoby Mathew 	}
95532ed618SSoby Mathew }
96532ed618SSoby Mathew 
97532ed618SSoby Mathew /*******************************************************************************
98532ed618SSoby Mathew  * This functions updates cpu_start_idx and ncpus field for each of the node in
99532ed618SSoby Mathew  * psci_non_cpu_pd_nodes[]. It does so by comparing the parent nodes of each of
100532ed618SSoby Mathew  * the CPUs and check whether they match with the parent of the previous
101532ed618SSoby Mathew  * CPU. The basic assumption for this work is that children of the same parent
102532ed618SSoby Mathew  * are allocated adjacent indices. The platform should ensure this though proper
103532ed618SSoby Mathew  * mapping of the CPUs to indices via plat_core_pos_by_mpidr() and
104532ed618SSoby Mathew  * plat_my_core_pos() APIs.
105532ed618SSoby Mathew  *******************************************************************************/
106532ed618SSoby Mathew static void psci_update_pwrlvl_limits(void)
107532ed618SSoby Mathew {
108532ed618SSoby Mathew 	int j;
109532ed618SSoby Mathew 	unsigned int nodes_idx[PLAT_MAX_PWR_LVL] = {0};
110532ed618SSoby Mathew 	unsigned int temp_index[PLAT_MAX_PWR_LVL], cpu_idx;
111532ed618SSoby Mathew 
112532ed618SSoby Mathew 	for (cpu_idx = 0; cpu_idx < PLATFORM_CORE_COUNT; cpu_idx++) {
113532ed618SSoby Mathew 		psci_get_parent_pwr_domain_nodes(cpu_idx,
114532ed618SSoby Mathew 						 PLAT_MAX_PWR_LVL,
115532ed618SSoby Mathew 						 temp_index);
116532ed618SSoby Mathew 		for (j = PLAT_MAX_PWR_LVL - 1; j >= 0; j--) {
117532ed618SSoby Mathew 			if (temp_index[j] != nodes_idx[j]) {
118532ed618SSoby Mathew 				nodes_idx[j] = temp_index[j];
119532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[nodes_idx[j]].cpu_start_idx
120532ed618SSoby Mathew 					= cpu_idx;
121532ed618SSoby Mathew 			}
122532ed618SSoby Mathew 			psci_non_cpu_pd_nodes[nodes_idx[j]].ncpus++;
123532ed618SSoby Mathew 		}
124532ed618SSoby Mathew 	}
125532ed618SSoby Mathew }
126532ed618SSoby Mathew 
127532ed618SSoby Mathew /*******************************************************************************
128532ed618SSoby Mathew  * Core routine to populate the power domain tree. The tree descriptor passed by
129532ed618SSoby Mathew  * the platform is populated breadth-first and the first entry in the map
130532ed618SSoby Mathew  * informs the number of root power domains. The parent nodes of the root nodes
131532ed618SSoby Mathew  * will point to an invalid entry(-1).
132532ed618SSoby Mathew  ******************************************************************************/
133532ed618SSoby Mathew static void populate_power_domain_tree(const unsigned char *topology)
134532ed618SSoby Mathew {
135532ed618SSoby Mathew 	unsigned int i, j = 0, num_nodes_at_lvl = 1, num_nodes_at_next_lvl;
136532ed618SSoby Mathew 	unsigned int node_index = 0, parent_node_index = 0, num_children;
137532ed618SSoby Mathew 	int level = PLAT_MAX_PWR_LVL;
138532ed618SSoby Mathew 
139532ed618SSoby Mathew 	/*
140532ed618SSoby Mathew 	 * For each level the inputs are:
141532ed618SSoby Mathew 	 * - number of nodes at this level in plat_array i.e. num_nodes_at_level
142532ed618SSoby Mathew 	 *   This is the sum of values of nodes at the parent level.
143532ed618SSoby Mathew 	 * - Index of first entry at this level in the plat_array i.e.
144532ed618SSoby Mathew 	 *   parent_node_index.
145532ed618SSoby Mathew 	 * - Index of first free entry in psci_non_cpu_pd_nodes[] or
146532ed618SSoby Mathew 	 *   psci_cpu_pd_nodes[] i.e. node_index depending upon the level.
147532ed618SSoby Mathew 	 */
148532ed618SSoby Mathew 	while (level >= PSCI_CPU_PWR_LVL) {
149532ed618SSoby Mathew 		num_nodes_at_next_lvl = 0;
150532ed618SSoby Mathew 		/*
151532ed618SSoby Mathew 		 * For each entry (parent node) at this level in the plat_array:
152532ed618SSoby Mathew 		 * - Find the number of children
153532ed618SSoby Mathew 		 * - Allocate a node in a power domain array for each child
154532ed618SSoby Mathew 		 * - Set the parent of the child to the parent_node_index - 1
155532ed618SSoby Mathew 		 * - Increment parent_node_index to point to the next parent
156532ed618SSoby Mathew 		 * - Accumulate the number of children at next level.
157532ed618SSoby Mathew 		 */
158532ed618SSoby Mathew 		for (i = 0; i < num_nodes_at_lvl; i++) {
159532ed618SSoby Mathew 			assert(parent_node_index <=
160532ed618SSoby Mathew 					PSCI_NUM_NON_CPU_PWR_DOMAINS);
161532ed618SSoby Mathew 			num_children = topology[parent_node_index];
162532ed618SSoby Mathew 
163532ed618SSoby Mathew 			for (j = node_index;
164532ed618SSoby Mathew 				j < node_index + num_children; j++)
165532ed618SSoby Mathew 				psci_init_pwr_domain_node(j,
166532ed618SSoby Mathew 							  parent_node_index - 1,
167532ed618SSoby Mathew 							  level);
168532ed618SSoby Mathew 
169532ed618SSoby Mathew 			node_index = j;
170532ed618SSoby Mathew 			num_nodes_at_next_lvl += num_children;
171532ed618SSoby Mathew 			parent_node_index++;
172532ed618SSoby Mathew 		}
173532ed618SSoby Mathew 
174532ed618SSoby Mathew 		num_nodes_at_lvl = num_nodes_at_next_lvl;
175532ed618SSoby Mathew 		level--;
176532ed618SSoby Mathew 
177532ed618SSoby Mathew 		/* Reset the index for the cpu power domain array */
178532ed618SSoby Mathew 		if (level == PSCI_CPU_PWR_LVL)
179532ed618SSoby Mathew 			node_index = 0;
180532ed618SSoby Mathew 	}
181532ed618SSoby Mathew 
182532ed618SSoby Mathew 	/* Validate the sanity of array exported by the platform */
183532ed618SSoby Mathew 	assert(j == PLATFORM_CORE_COUNT);
184532ed618SSoby Mathew }
185532ed618SSoby Mathew 
186532ed618SSoby Mathew /*******************************************************************************
187cf0b1492SSoby Mathew  * This function does the architectural setup and takes the warm boot
188cf0b1492SSoby Mathew  * entry-point `mailbox_ep` as an argument. The function also initializes the
189cf0b1492SSoby Mathew  * power domain topology tree by querying the platform. The power domain nodes
190cf0b1492SSoby Mathew  * higher than the CPU are populated in the array psci_non_cpu_pd_nodes[] and
191cf0b1492SSoby Mathew  * the CPU power domains are populated in psci_cpu_pd_nodes[]. The platform
192cf0b1492SSoby Mathew  * exports its static topology map through the
193532ed618SSoby Mathew  * populate_power_domain_topology_tree() API. The algorithm populates the
194532ed618SSoby Mathew  * psci_non_cpu_pd_nodes and psci_cpu_pd_nodes iteratively by using this
195cf0b1492SSoby Mathew  * topology map.  On a platform that implements two clusters of 2 cpus each,
196cf0b1492SSoby Mathew  * and supporting 3 domain levels, the populated psci_non_cpu_pd_nodes would
197cf0b1492SSoby Mathew  * look like this:
198532ed618SSoby Mathew  *
199532ed618SSoby Mathew  * ---------------------------------------------------
200532ed618SSoby Mathew  * | system node | cluster 0 node  | cluster 1 node  |
201532ed618SSoby Mathew  * ---------------------------------------------------
202532ed618SSoby Mathew  *
203532ed618SSoby Mathew  * And populated psci_cpu_pd_nodes would look like this :
204532ed618SSoby Mathew  * <-    cpus cluster0   -><-   cpus cluster1   ->
205532ed618SSoby Mathew  * ------------------------------------------------
206532ed618SSoby Mathew  * |   CPU 0   |   CPU 1   |   CPU 2   |   CPU 3  |
207532ed618SSoby Mathew  * ------------------------------------------------
208532ed618SSoby Mathew  ******************************************************************************/
209cf0b1492SSoby Mathew int psci_setup(uintptr_t mailbox_ep)
210532ed618SSoby Mathew {
211532ed618SSoby Mathew 	const unsigned char *topology_tree;
212532ed618SSoby Mathew 
213cf0b1492SSoby Mathew 	/* Do the Architectural initialization */
214cf0b1492SSoby Mathew 	psci_arch_setup();
215cf0b1492SSoby Mathew 
216532ed618SSoby Mathew 	/* Query the topology map from the platform */
217532ed618SSoby Mathew 	topology_tree = plat_get_power_domain_tree_desc();
218532ed618SSoby Mathew 
219532ed618SSoby Mathew 	/* Populate the power domain arrays using the platform topology map */
220532ed618SSoby Mathew 	populate_power_domain_tree(topology_tree);
221532ed618SSoby Mathew 
222532ed618SSoby Mathew 	/* Update the CPU limits for each node in psci_non_cpu_pd_nodes */
223532ed618SSoby Mathew 	psci_update_pwrlvl_limits();
224532ed618SSoby Mathew 
225532ed618SSoby Mathew 	/* Populate the mpidr field of cpu node for this CPU */
226532ed618SSoby Mathew 	psci_cpu_pd_nodes[plat_my_core_pos()].mpidr =
227532ed618SSoby Mathew 		read_mpidr() & MPIDR_AFFINITY_MASK;
228532ed618SSoby Mathew 
229532ed618SSoby Mathew 	psci_init_req_local_pwr_states();
230532ed618SSoby Mathew 
231532ed618SSoby Mathew 	/*
232532ed618SSoby Mathew 	 * Set the requested and target state of this CPU and all the higher
233532ed618SSoby Mathew 	 * power domain levels for this CPU to run.
234532ed618SSoby Mathew 	 */
235532ed618SSoby Mathew 	psci_set_pwr_domains_to_run(PLAT_MAX_PWR_LVL);
236532ed618SSoby Mathew 
237cf0b1492SSoby Mathew 	assert(mailbox_ep);
238cf0b1492SSoby Mathew 	plat_setup_psci_ops(mailbox_ep, &psci_plat_pm_ops);
239532ed618SSoby Mathew 	assert(psci_plat_pm_ops);
240532ed618SSoby Mathew 
241532ed618SSoby Mathew 	/* Initialize the psci capability */
242532ed618SSoby Mathew 	psci_caps = PSCI_GENERIC_CAP;
243532ed618SSoby Mathew 
244532ed618SSoby Mathew 	if (psci_plat_pm_ops->pwr_domain_off)
245532ed618SSoby Mathew 		psci_caps |=  define_psci_cap(PSCI_CPU_OFF);
246532ed618SSoby Mathew 	if (psci_plat_pm_ops->pwr_domain_on &&
247532ed618SSoby Mathew 			psci_plat_pm_ops->pwr_domain_on_finish)
248532ed618SSoby Mathew 		psci_caps |=  define_psci_cap(PSCI_CPU_ON_AARCH64);
249532ed618SSoby Mathew 	if (psci_plat_pm_ops->pwr_domain_suspend &&
250532ed618SSoby Mathew 			psci_plat_pm_ops->pwr_domain_suspend_finish) {
251532ed618SSoby Mathew 		psci_caps |=  define_psci_cap(PSCI_CPU_SUSPEND_AARCH64);
252532ed618SSoby Mathew 		if (psci_plat_pm_ops->get_sys_suspend_power_state)
253532ed618SSoby Mathew 			psci_caps |=  define_psci_cap(PSCI_SYSTEM_SUSPEND_AARCH64);
254532ed618SSoby Mathew 	}
255532ed618SSoby Mathew 	if (psci_plat_pm_ops->system_off)
256532ed618SSoby Mathew 		psci_caps |=  define_psci_cap(PSCI_SYSTEM_OFF);
257532ed618SSoby Mathew 	if (psci_plat_pm_ops->system_reset)
258532ed618SSoby Mathew 		psci_caps |=  define_psci_cap(PSCI_SYSTEM_RESET);
259*28d3d614SJeenu Viswambharan 	if (psci_plat_pm_ops->get_node_hw_state)
260*28d3d614SJeenu Viswambharan 		psci_caps |= define_psci_cap(PSCI_NODE_HW_STATE_AARCH64);
261532ed618SSoby Mathew 
262532ed618SSoby Mathew #if ENABLE_PSCI_STAT
263532ed618SSoby Mathew 	psci_caps |=  define_psci_cap(PSCI_STAT_RESIDENCY_AARCH64);
264532ed618SSoby Mathew 	psci_caps |=  define_psci_cap(PSCI_STAT_COUNT_AARCH64);
265532ed618SSoby Mathew #endif
266532ed618SSoby Mathew 
267532ed618SSoby Mathew 	return 0;
268532ed618SSoby Mathew }
269cf0b1492SSoby Mathew 
270cf0b1492SSoby Mathew /*******************************************************************************
271cf0b1492SSoby Mathew  * This duplicates what the primary cpu did after a cold boot in BL1. The same
272cf0b1492SSoby Mathew  * needs to be done when a cpu is hotplugged in. This function could also over-
273cf0b1492SSoby Mathew  * ride any EL3 setup done by BL1 as this code resides in rw memory.
274cf0b1492SSoby Mathew  ******************************************************************************/
275cf0b1492SSoby Mathew void psci_arch_setup(void)
276cf0b1492SSoby Mathew {
277cf0b1492SSoby Mathew 	/* Program the counter frequency */
278cf0b1492SSoby Mathew 	write_cntfrq_el0(plat_get_syscnt_freq2());
279cf0b1492SSoby Mathew 
280cf0b1492SSoby Mathew 	/* Initialize the cpu_ops pointer. */
281cf0b1492SSoby Mathew 	init_cpu_ops();
282cf0b1492SSoby Mathew }
283727e5238SSoby Mathew 
284727e5238SSoby Mathew /******************************************************************************
285727e5238SSoby Mathew  * PSCI Library interface to initialize the cpu context for the next non
286727e5238SSoby Mathew  * secure image during cold boot. The relevant registers in the cpu context
287727e5238SSoby Mathew  * need to be retrieved and programmed on return from this interface.
288727e5238SSoby Mathew  *****************************************************************************/
289727e5238SSoby Mathew void psci_prepare_next_non_secure_ctx(entry_point_info_t *next_image_info)
290727e5238SSoby Mathew {
291727e5238SSoby Mathew 	assert(GET_SECURITY_STATE(next_image_info->h.attr) == NON_SECURE);
292727e5238SSoby Mathew 	cm_init_my_context(next_image_info);
293727e5238SSoby Mathew 	cm_prepare_el3_exit(NON_SECURE);
294727e5238SSoby Mathew }
295