blob: 5f7376be70a4724e84df0f2d7dd1e5a3713f269e [file] [log] [blame]
Soby Mathewb48349e2015-06-29 16:30:12 +01001/*
Soby Mathew4067dc32015-05-05 16:33:16 +01002 * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved.
Soby Mathewb48349e2015-06-29 16:30:12 +01003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <arch.h>
32#include <arch_helpers.h>
33#include <assert.h>
34#include <bl_common.h>
35#include <context.h>
36#include <context_mgmt.h>
37#include <platform.h>
38#include <stddef.h>
39#include "psci_private.h"
40
41/*******************************************************************************
42 * Per cpu non-secure contexts used to program the architectural state prior
43 * return to the normal world.
44 * TODO: Use the memory allocator to set aside memory for the contexts instead
Soby Mathew82dcc032015-04-08 17:42:06 +010045 * of relying on platform defined constants.
Soby Mathewb48349e2015-06-29 16:30:12 +010046 ******************************************************************************/
47static cpu_context_t psci_ns_context[PLATFORM_CORE_COUNT];
48
Soby Mathewb48349e2015-06-29 16:30:12 +010049/******************************************************************************
50 * Define the psci capability variable.
51 *****************************************************************************/
52uint32_t psci_caps;
53
Soby Mathewb48349e2015-06-29 16:30:12 +010054/*******************************************************************************
Soby Mathew82dcc032015-04-08 17:42:06 +010055 * Function which initializes the 'psci_non_cpu_pd_nodes' or the
56 * 'psci_cpu_pd_nodes' corresponding to the power level.
Soby Mathewb48349e2015-06-29 16:30:12 +010057 ******************************************************************************/
Soby Mathew82dcc032015-04-08 17:42:06 +010058static void psci_init_pwr_domain_node(int node_idx, int parent_idx, int level)
Soby Mathewb48349e2015-06-29 16:30:12 +010059{
Soby Mathew82dcc032015-04-08 17:42:06 +010060 if (level > PSCI_CPU_PWR_LVL) {
61 psci_non_cpu_pd_nodes[node_idx].level = level;
62 psci_lock_init(psci_non_cpu_pd_nodes, node_idx);
63 psci_non_cpu_pd_nodes[node_idx].parent_node = parent_idx;
Soby Mathewb48349e2015-06-29 16:30:12 +010064 } else {
Soby Mathewb48349e2015-06-29 16:30:12 +010065
Soby Mathew82dcc032015-04-08 17:42:06 +010066 psci_cpu_pd_nodes[node_idx].parent_node = parent_idx;
67
68 /* Initialize with an invalid mpidr */
69 psci_cpu_pd_nodes[node_idx].mpidr = PSCI_INVALID_MPIDR;
70
71 /*
72 * Mark the cpu as OFF.
73 */
74 set_cpu_data_by_index(node_idx,
75 psci_svc_cpu_data.psci_state,
76 PSCI_STATE_OFF);
77
78 /* Invalidate the suspend context for the node */
79 set_cpu_data_by_index(node_idx,
80 psci_svc_cpu_data.power_state,
81 PSCI_INVALID_DATA);
82
83 flush_cpu_data_by_index(node_idx, psci_svc_cpu_data);
84
85 cm_set_context_by_index(node_idx,
86 (void *) &psci_ns_context[node_idx],
87 NON_SECURE);
Soby Mathewb48349e2015-06-29 16:30:12 +010088 }
Soby Mathewb48349e2015-06-29 16:30:12 +010089}
90
91/*******************************************************************************
Soby Mathew82dcc032015-04-08 17:42:06 +010092 * This functions updates cpu_start_idx and ncpus field for each of the node in
93 * psci_non_cpu_pd_nodes[]. It does so by comparing the parent nodes of each of
94 * the CPUs and check whether they match with the parent of the previous
95 * CPU. The basic assumption for this work is that children of the same parent
96 * are allocated adjacent indices. The platform should ensure this though proper
97 * mapping of the CPUs to indices via plat_core_pos_by_mpidr() and
98 * plat_my_core_pos() APIs.
99 *******************************************************************************/
100static void psci_update_pwrlvl_limits(void)
101{
102 int cpu_idx, j;
103 unsigned int nodes_idx[PLAT_MAX_PWR_LVL] = {0};
104 unsigned int temp_index[PLAT_MAX_PWR_LVL];
105
106 for (cpu_idx = 0; cpu_idx < PLATFORM_CORE_COUNT; cpu_idx++) {
107 psci_get_parent_pwr_domain_nodes(cpu_idx,
108 PLAT_MAX_PWR_LVL,
109 temp_index);
110 for (j = PLAT_MAX_PWR_LVL - 1; j >= 0; j--) {
111 if (temp_index[j] != nodes_idx[j]) {
112 nodes_idx[j] = temp_index[j];
113 psci_non_cpu_pd_nodes[nodes_idx[j]].cpu_start_idx
114 = cpu_idx;
115 }
116 psci_non_cpu_pd_nodes[nodes_idx[j]].ncpus++;
117 }
118 }
119}
120
121/*******************************************************************************
122 * Core routine to populate the power domain tree. The tree descriptor passed by
123 * the platform is populated breadth-first and the first entry in the map
124 * informs the number of root power domains. The parent nodes of the root nodes
125 * will point to an invalid entry(-1).
126 ******************************************************************************/
127static void populate_power_domain_tree(const unsigned char *topology)
128{
129 unsigned int i, j = 0, num_nodes_at_lvl = 1, num_nodes_at_next_lvl;
130 unsigned int node_index = 0, parent_node_index = 0, num_children;
131 int level = PLAT_MAX_PWR_LVL;
132
133 /*
134 * For each level the inputs are:
135 * - number of nodes at this level in plat_array i.e. num_nodes_at_level
136 * This is the sum of values of nodes at the parent level.
137 * - Index of first entry at this level in the plat_array i.e.
138 * parent_node_index.
139 * - Index of first free entry in psci_non_cpu_pd_nodes[] or
140 * psci_cpu_pd_nodes[] i.e. node_index depending upon the level.
141 */
142 while (level >= PSCI_CPU_PWR_LVL) {
143 num_nodes_at_next_lvl = 0;
144 /*
145 * For each entry (parent node) at this level in the plat_array:
146 * - Find the number of children
147 * - Allocate a node in a power domain array for each child
148 * - Set the parent of the child to the parent_node_index - 1
149 * - Increment parent_node_index to point to the next parent
150 * - Accumulate the number of children at next level.
151 */
152 for (i = 0; i < num_nodes_at_lvl; i++) {
153 assert(parent_node_index <=
154 PSCI_NUM_NON_CPU_PWR_DOMAINS);
155 num_children = topology[parent_node_index];
156
157 for (j = node_index;
158 j < node_index + num_children; j++)
159 psci_init_pwr_domain_node(j,
160 parent_node_index - 1,
161 level);
162
163 node_index = j;
164 num_nodes_at_next_lvl += num_children;
165 parent_node_index++;
166 }
167
168 num_nodes_at_lvl = num_nodes_at_next_lvl;
169 level--;
170
171 /* Reset the index for the cpu power domain array */
172 if (level == PSCI_CPU_PWR_LVL)
173 node_index = 0;
174 }
175
176 /* Validate the sanity of array exported by the platform */
177 assert(j == PLATFORM_CORE_COUNT);
178
179#if !USE_COHERENT_MEM
180 /* Flush the non CPU power domain data to memory */
181 flush_dcache_range((uint64_t) &psci_non_cpu_pd_nodes,
182 sizeof(psci_non_cpu_pd_nodes));
183#endif
184}
185
186/*******************************************************************************
187 * This function initializes the power domain topology tree by querying the
188 * platform. The power domain nodes higher than the CPU are populated in the
189 * array psci_non_cpu_pd_nodes[] and the CPU power domains are populated in
190 * psci_cpu_pd_nodes[]. The platform exports its static topology map through the
191 * populate_power_domain_topology_tree() API. The algorithm populates the
192 * psci_non_cpu_pd_nodes and psci_cpu_pd_nodes iteratively by using this
193 * topology map. On a platform that implements two clusters of 2 cpus each, and
194 * supporting 3 domain levels, the populated psci_non_cpu_pd_nodes would look
195 * like this:
Soby Mathewb48349e2015-06-29 16:30:12 +0100196 *
Soby Mathewb48349e2015-06-29 16:30:12 +0100197 * ---------------------------------------------------
Soby Mathew82dcc032015-04-08 17:42:06 +0100198 * | system node | cluster 0 node | cluster 1 node |
Soby Mathewb48349e2015-06-29 16:30:12 +0100199 * ---------------------------------------------------
Soby Mathewb48349e2015-06-29 16:30:12 +0100200 *
Soby Mathew82dcc032015-04-08 17:42:06 +0100201 * And populated psci_cpu_pd_nodes would look like this :
202 * <- cpus cluster0 -><- cpus cluster1 ->
203 * ------------------------------------------------
204 * | CPU 0 | CPU 1 | CPU 2 | CPU 3 |
205 * ------------------------------------------------
Soby Mathewb48349e2015-06-29 16:30:12 +0100206 ******************************************************************************/
207int32_t psci_setup(void)
208{
Soby Mathew82dcc032015-04-08 17:42:06 +0100209 const unsigned char *topology_tree;
Soby Mathewb48349e2015-06-29 16:30:12 +0100210
Soby Mathew82dcc032015-04-08 17:42:06 +0100211 /* Query the topology map from the platform */
212 topology_tree = plat_get_power_domain_tree_desc();
Soby Mathewb48349e2015-06-29 16:30:12 +0100213
Soby Mathew82dcc032015-04-08 17:42:06 +0100214 /* Populate the power domain arrays using the platform topology map */
215 populate_power_domain_tree(topology_tree);
Soby Mathewb48349e2015-06-29 16:30:12 +0100216
Soby Mathew82dcc032015-04-08 17:42:06 +0100217 /* Update the CPU limits for each node in psci_non_cpu_pd_nodes */
218 psci_update_pwrlvl_limits();
219
220 /* Populate the mpidr field of cpu node for this CPU */
221 psci_cpu_pd_nodes[plat_my_core_pos()].mpidr =
222 read_mpidr() & MPIDR_AFFINITY_MASK;
Soby Mathewb48349e2015-06-29 16:30:12 +0100223
224#if !USE_COHERENT_MEM
225 /*
Soby Mathew82dcc032015-04-08 17:42:06 +0100226 * The psci_non_cpu_pd_nodes only needs flushing when it's not allocated in
227 * coherent memory.
Soby Mathewb48349e2015-06-29 16:30:12 +0100228 */
Soby Mathew82dcc032015-04-08 17:42:06 +0100229 flush_dcache_range((uint64_t) &psci_non_cpu_pd_nodes,
230 sizeof(psci_non_cpu_pd_nodes));
Soby Mathewb48349e2015-06-29 16:30:12 +0100231#endif
232
Soby Mathew82dcc032015-04-08 17:42:06 +0100233 flush_dcache_range((uint64_t) &psci_cpu_pd_nodes,
234 sizeof(psci_cpu_pd_nodes));
Soby Mathewb48349e2015-06-29 16:30:12 +0100235
236 /*
Soby Mathew82dcc032015-04-08 17:42:06 +0100237 * Mark the current CPU and its parent power domains as ON. No need to lock
238 * as the system is UP on the primary at this stage of boot.
Soby Mathewb48349e2015-06-29 16:30:12 +0100239 */
Soby Mathew82dcc032015-04-08 17:42:06 +0100240 psci_do_state_coordination(PLAT_MAX_PWR_LVL, plat_my_core_pos(),
241 PSCI_STATE_ON);
Soby Mathewb48349e2015-06-29 16:30:12 +0100242
243 platform_setup_pm(&psci_plat_pm_ops);
244 assert(psci_plat_pm_ops);
245
246 /* Initialize the psci capability */
247 psci_caps = PSCI_GENERIC_CAP;
248
Soby Mathew4067dc32015-05-05 16:33:16 +0100249 if (psci_plat_pm_ops->pwr_domain_off)
Soby Mathewb48349e2015-06-29 16:30:12 +0100250 psci_caps |= define_psci_cap(PSCI_CPU_OFF);
Soby Mathew4067dc32015-05-05 16:33:16 +0100251 if (psci_plat_pm_ops->pwr_domain_on &&
252 psci_plat_pm_ops->pwr_domain_on_finish)
Soby Mathewb48349e2015-06-29 16:30:12 +0100253 psci_caps |= define_psci_cap(PSCI_CPU_ON_AARCH64);
Soby Mathew4067dc32015-05-05 16:33:16 +0100254 if (psci_plat_pm_ops->pwr_domain_suspend &&
255 psci_plat_pm_ops->pwr_domain_suspend_finish) {
Soby Mathewb48349e2015-06-29 16:30:12 +0100256 psci_caps |= define_psci_cap(PSCI_CPU_SUSPEND_AARCH64);
257 if (psci_plat_pm_ops->get_sys_suspend_power_state)
258 psci_caps |= define_psci_cap(PSCI_SYSTEM_SUSPEND_AARCH64);
259 }
260 if (psci_plat_pm_ops->system_off)
261 psci_caps |= define_psci_cap(PSCI_SYSTEM_OFF);
262 if (psci_plat_pm_ops->system_reset)
263 psci_caps |= define_psci_cap(PSCI_SYSTEM_RESET);
264
265 return 0;
266}