blob: 91de1ab776054d5fe85eec9d5831d93e991dda77 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +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 <stdio.h>
32#include <string.h>
33#include <assert.h>
34#include <arch_helpers.h>
35#include <console.h>
36#include <platform.h>
37#include <psci_private.h>
Achin Guptaef7a28c2014-02-01 08:59:56 +000038#include <context_mgmt.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010039
40/*******************************************************************************
Achin Guptaef7a28c2014-02-01 08:59:56 +000041 * Per cpu non-secure contexts used to program the architectural state prior
42 * return to the normal world.
43 * TODO: Use the memory allocator to set aside memory for the contexts instead
44 * of relying on platform defined constants. Using PSCI_NUM_AFFS will be an
45 * overkill.
46 ******************************************************************************/
47static cpu_context psci_ns_context[PLATFORM_CORE_COUNT];
48
49/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +010050 * Routines for retrieving the node corresponding to an affinity level instance
51 * in the mpidr. The first one uses binary search to find the node corresponding
52 * to the mpidr (key) at a particular affinity level. The second routine decides
53 * extents of the binary search at each affinity level.
54 ******************************************************************************/
55static int psci_aff_map_get_idx(unsigned long key,
56 int min_idx,
57 int max_idx)
58{
59 int mid;
60
61 /*
62 * Terminating condition: If the max and min indices have crossed paths
63 * during the binary search then the key has not been found.
64 */
65 if (max_idx < min_idx)
66 return PSCI_E_INVALID_PARAMS;
67
68 /*
69 * Bisect the array around 'mid' and then recurse into the array chunk
70 * where the key is likely to be found. The mpidrs in each node in the
71 * 'psci_aff_map' for a given affinity level are stored in an ascending
72 * order which makes the binary search possible.
73 */
74 mid = min_idx + ((max_idx - min_idx) >> 1); /* Divide by 2 */
75 if (psci_aff_map[mid].mpidr > key)
76 return psci_aff_map_get_idx(key, min_idx, mid - 1);
77 else if (psci_aff_map[mid].mpidr < key)
78 return psci_aff_map_get_idx(key, mid + 1, max_idx);
79 else
80 return mid;
81}
82
83aff_map_node *psci_get_aff_map_node(unsigned long mpidr, int aff_lvl)
84{
85 int rc;
86
87 /* Right shift the mpidr to the required affinity level */
88 mpidr = mpidr_mask_lower_afflvls(mpidr, aff_lvl);
89
90 rc = psci_aff_map_get_idx(mpidr,
91 psci_aff_limits[aff_lvl].min,
92 psci_aff_limits[aff_lvl].max);
93 if (rc >= 0)
94 return &psci_aff_map[rc];
95 else
96 return NULL;
97}
98
99/*******************************************************************************
Achin Gupta0959db52013-12-02 17:33:04 +0000100 * This function populates an array with nodes corresponding to a given range of
101 * affinity levels in an mpidr. It returns successfully only when the affinity
102 * levels are correct, the mpidr is valid i.e. no affinity level is absent from
103 * the topology tree & the affinity instance at level 0 is not absent.
104 ******************************************************************************/
105int psci_get_aff_map_nodes(unsigned long mpidr,
106 int start_afflvl,
107 int end_afflvl,
108 mpidr_aff_map_nodes mpidr_nodes)
109{
110 int rc = PSCI_E_INVALID_PARAMS, level;
111 aff_map_node *node;
112
113 rc = psci_check_afflvl_range(start_afflvl, end_afflvl);
114 if (rc != PSCI_E_SUCCESS)
115 return rc;
116
117 for (level = start_afflvl; level <= end_afflvl; level++) {
118
119 /*
120 * Grab the node for each affinity level. No affinity level
121 * can be missing as that would mean that the topology tree
122 * is corrupted.
123 */
124 node = psci_get_aff_map_node(mpidr, level);
125 if (node == NULL) {
126 rc = PSCI_E_INVALID_PARAMS;
127 break;
128 }
129
130 /*
131 * Skip absent affinity levels unless it's afffinity level 0.
132 * An absent cpu means that the mpidr is invalid. Save the
133 * pointer to the node for the present affinity level
134 */
135 if (!(node->state & PSCI_AFF_PRESENT)) {
136 if (level == MPIDR_AFFLVL0) {
137 rc = PSCI_E_INVALID_PARAMS;
138 break;
139 }
140
141 mpidr_nodes[level] = NULL;
142 } else
143 mpidr_nodes[level] = node;
144 }
145
146 return rc;
147}
148
149/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +0100150 * Function which initializes the 'aff_map_node' corresponding to an affinity
151 * level instance. Each node has a unique mpidr, level and bakery lock. The data
152 * field is opaque and holds affinity level specific data e.g. for affinity
153 * level 0 it contains the index into arrays that hold the secure/non-secure
154 * state for a cpu that's been turned on/off
155 ******************************************************************************/
156static void psci_init_aff_map_node(unsigned long mpidr,
157 int level,
158 unsigned int idx)
159{
160 unsigned char state;
Achin Guptaef7a28c2014-02-01 08:59:56 +0000161 uint32_t linear_id;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100162 psci_aff_map[idx].mpidr = mpidr;
163 psci_aff_map[idx].level = level;
164 bakery_lock_init(&psci_aff_map[idx].lock);
165
166 /*
167 * If an affinity instance is present then mark it as OFF to begin with.
168 */
169 state = plat_get_aff_state(level, mpidr);
170 psci_aff_map[idx].state = state;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100171
172 if (level == MPIDR_AFFLVL0) {
Achin Gupta75f73672013-12-05 16:33:10 +0000173
174 /*
175 * Mark the cpu as OFF. Higher affinity level reference counts
176 * have already been memset to 0
177 */
178 if (state & PSCI_AFF_PRESENT)
179 psci_set_state(&psci_aff_map[idx], PSCI_STATE_OFF);
180
Achin Gupta4f6ad662013-10-25 09:08:21 +0100181 /* Ensure that we have not overflowed the psci_ns_einfo array */
182 assert(psci_ns_einfo_idx < PSCI_NUM_AFFS);
183
184 psci_aff_map[idx].data = psci_ns_einfo_idx;
185 psci_ns_einfo_idx++;
Achin Guptaef7a28c2014-02-01 08:59:56 +0000186
187 /*
188 * Associate a non-secure context with this affinity
189 * instance through the context management library.
190 */
191 linear_id = platform_get_core_pos(mpidr);
192 assert(linear_id < PLATFORM_CORE_COUNT);
193
194 cm_set_context(mpidr,
195 (void *) &psci_ns_context[linear_id],
196 NON_SECURE);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100197 }
198
199 return;
200}
201
202/*******************************************************************************
203 * Core routine used by the Breadth-First-Search algorithm to populate the
204 * affinity tree. Each level in the tree corresponds to an affinity level. This
205 * routine's aim is to traverse to the target affinity level and populate nodes
206 * in the 'psci_aff_map' for all the siblings at that level. It uses the current
207 * affinity level to keep track of how many levels from the root of the tree
208 * have been traversed. If the current affinity level != target affinity level,
209 * then the platform is asked to return the number of children that each
210 * affinity instance has at the current affinity level. Traversal is then done
211 * for each child at the next lower level i.e. current affinity level - 1.
212 *
213 * CAUTION: This routine assumes that affinity instance ids are allocated in a
214 * monotonically increasing manner at each affinity level in a mpidr starting
215 * from 0. If the platform breaks this assumption then this code will have to
216 * be reworked accordingly.
217 ******************************************************************************/
218static unsigned int psci_init_aff_map(unsigned long mpidr,
219 unsigned int affmap_idx,
220 int cur_afflvl,
221 int tgt_afflvl)
222{
223 unsigned int ctr, aff_count;
224
225 assert(cur_afflvl >= tgt_afflvl);
226
227 /*
228 * Find the number of siblings at the current affinity level &
229 * assert if there are none 'cause then we have been invoked with
230 * an invalid mpidr.
231 */
232 aff_count = plat_get_aff_count(cur_afflvl, mpidr);
233 assert(aff_count);
234
235 if (tgt_afflvl < cur_afflvl) {
236 for (ctr = 0; ctr < aff_count; ctr++) {
237 mpidr = mpidr_set_aff_inst(mpidr, ctr, cur_afflvl);
238 affmap_idx = psci_init_aff_map(mpidr,
239 affmap_idx,
240 cur_afflvl - 1,
241 tgt_afflvl);
242 }
243 } else {
244 for (ctr = 0; ctr < aff_count; ctr++, affmap_idx++) {
245 mpidr = mpidr_set_aff_inst(mpidr, ctr, cur_afflvl);
246 psci_init_aff_map_node(mpidr, cur_afflvl, affmap_idx);
247 }
248
249 /* affmap_idx is 1 greater than the max index of cur_afflvl */
250 psci_aff_limits[cur_afflvl].max = affmap_idx - 1;
251 }
252
253 return affmap_idx;
254}
255
256/*******************************************************************************
257 * This function initializes the topology tree by querying the platform. To do
258 * so, it's helper routines implement a Breadth-First-Search. At each affinity
259 * level the platform conveys the number of affinity instances that exist i.e.
260 * the affinity count. The algorithm populates the psci_aff_map recursively
261 * using this information. On a platform that implements two clusters of 4 cpus
262 * each, the populated aff_map_array would look like this:
263 *
264 * <- cpus cluster0 -><- cpus cluster1 ->
265 * ---------------------------------------------------
266 * | 0 | 1 | 0 | 1 | 2 | 3 | 0 | 1 | 2 | 3 |
267 * ---------------------------------------------------
268 * ^ ^
269 * cluster __| cpu __|
270 * limit limit
271 *
272 * The first 2 entries are of the cluster nodes. The next 4 entries are of cpus
273 * within cluster 0. The last 4 entries are of cpus within cluster 1.
274 * The 'psci_aff_limits' array contains the max & min index of each affinity
275 * level within the 'psci_aff_map' array. This allows restricting search of a
276 * node at an affinity level between the indices in the limits array.
277 ******************************************************************************/
278void psci_setup(unsigned long mpidr)
279{
James Morrissey40a6f642014-02-10 14:24:36 +0000280 int afflvl, affmap_idx, max_afflvl;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100281 aff_map_node *node;
282
Achin Gupta4f6ad662013-10-25 09:08:21 +0100283 psci_ns_einfo_idx = 0;
284 psci_plat_pm_ops = NULL;
285
286 /* Find out the maximum affinity level that the platform implements */
287 max_afflvl = get_max_afflvl();
288 assert(max_afflvl <= MPIDR_MAX_AFFLVL);
289
290 /*
291 * This call traverses the topology tree with help from the platform and
292 * populates the affinity map using a breadth-first-search recursively.
293 * We assume that the platform allocates affinity instance ids from 0
294 * onwards at each affinity level in the mpidr. FIRST_MPIDR = 0.0.0.0
295 */
296 affmap_idx = 0;
297 for (afflvl = max_afflvl; afflvl >= MPIDR_AFFLVL0; afflvl--) {
298 affmap_idx = psci_init_aff_map(FIRST_MPIDR,
299 affmap_idx,
300 max_afflvl,
301 afflvl);
302 }
303
304 /*
305 * Set the bounds for the affinity counts of each level in the map. Also
306 * flush out the entire array so that it's visible to subsequent power
307 * management operations. The 'psci_aff_map' array is allocated in
308 * coherent memory so does not need flushing. The 'psci_aff_limits'
309 * array is allocated in normal memory. It will be accessed when the mmu
310 * is off e.g. after reset. Hence it needs to be flushed.
311 */
312 for (afflvl = MPIDR_AFFLVL0; afflvl < max_afflvl; afflvl++) {
313 psci_aff_limits[afflvl].min =
314 psci_aff_limits[afflvl + 1].max + 1;
315 }
316
317 flush_dcache_range((unsigned long) psci_aff_limits,
318 sizeof(psci_aff_limits));
319
320 /*
321 * Mark the affinity instances in our mpidr as ON. No need to lock as
322 * this is the primary cpu.
323 */
324 mpidr &= MPIDR_AFFINITY_MASK;
Achin Gupta75f73672013-12-05 16:33:10 +0000325 for (afflvl = MPIDR_AFFLVL0; afflvl <= max_afflvl; afflvl++) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100326
327 node = psci_get_aff_map_node(mpidr, afflvl);
328 assert(node);
329
330 /* Mark each present node as ON. */
Achin Gupta75f73672013-12-05 16:33:10 +0000331 if (node->state & PSCI_AFF_PRESENT)
332 psci_set_state(node, PSCI_STATE_ON);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100333 }
334
James Morrissey40a6f642014-02-10 14:24:36 +0000335 platform_setup_pm(&psci_plat_pm_ops);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100336 assert(psci_plat_pm_ops);
337
338 return;
339}