blob: 9095e75d169151384061f04be6e0083cadb424be [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
2 * Copyright (c) 2013, ARM Limited. All rights reserved.
3 *
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>
38
39/*******************************************************************************
40 * Routines for retrieving the node corresponding to an affinity level instance
41 * in the mpidr. The first one uses binary search to find the node corresponding
42 * to the mpidr (key) at a particular affinity level. The second routine decides
43 * extents of the binary search at each affinity level.
44 ******************************************************************************/
45static int psci_aff_map_get_idx(unsigned long key,
46 int min_idx,
47 int max_idx)
48{
49 int mid;
50
51 /*
52 * Terminating condition: If the max and min indices have crossed paths
53 * during the binary search then the key has not been found.
54 */
55 if (max_idx < min_idx)
56 return PSCI_E_INVALID_PARAMS;
57
58 /*
59 * Bisect the array around 'mid' and then recurse into the array chunk
60 * where the key is likely to be found. The mpidrs in each node in the
61 * 'psci_aff_map' for a given affinity level are stored in an ascending
62 * order which makes the binary search possible.
63 */
64 mid = min_idx + ((max_idx - min_idx) >> 1); /* Divide by 2 */
65 if (psci_aff_map[mid].mpidr > key)
66 return psci_aff_map_get_idx(key, min_idx, mid - 1);
67 else if (psci_aff_map[mid].mpidr < key)
68 return psci_aff_map_get_idx(key, mid + 1, max_idx);
69 else
70 return mid;
71}
72
73aff_map_node *psci_get_aff_map_node(unsigned long mpidr, int aff_lvl)
74{
75 int rc;
76
77 /* Right shift the mpidr to the required affinity level */
78 mpidr = mpidr_mask_lower_afflvls(mpidr, aff_lvl);
79
80 rc = psci_aff_map_get_idx(mpidr,
81 psci_aff_limits[aff_lvl].min,
82 psci_aff_limits[aff_lvl].max);
83 if (rc >= 0)
84 return &psci_aff_map[rc];
85 else
86 return NULL;
87}
88
89/*******************************************************************************
90 * Function which initializes the 'aff_map_node' corresponding to an affinity
91 * level instance. Each node has a unique mpidr, level and bakery lock. The data
92 * field is opaque and holds affinity level specific data e.g. for affinity
93 * level 0 it contains the index into arrays that hold the secure/non-secure
94 * state for a cpu that's been turned on/off
95 ******************************************************************************/
96static void psci_init_aff_map_node(unsigned long mpidr,
97 int level,
98 unsigned int idx)
99{
100 unsigned char state;
101 psci_aff_map[idx].mpidr = mpidr;
102 psci_aff_map[idx].level = level;
103 bakery_lock_init(&psci_aff_map[idx].lock);
104
105 /*
106 * If an affinity instance is present then mark it as OFF to begin with.
107 */
108 state = plat_get_aff_state(level, mpidr);
109 psci_aff_map[idx].state = state;
110 if (state & PSCI_AFF_PRESENT) {
111 psci_set_state(psci_aff_map[idx].state, PSCI_STATE_OFF);
112 }
113
114 if (level == MPIDR_AFFLVL0) {
115 /* Ensure that we have not overflowed the psci_ns_einfo array */
116 assert(psci_ns_einfo_idx < PSCI_NUM_AFFS);
117
118 psci_aff_map[idx].data = psci_ns_einfo_idx;
119 psci_ns_einfo_idx++;
120 }
121
122 return;
123}
124
125/*******************************************************************************
126 * Core routine used by the Breadth-First-Search algorithm to populate the
127 * affinity tree. Each level in the tree corresponds to an affinity level. This
128 * routine's aim is to traverse to the target affinity level and populate nodes
129 * in the 'psci_aff_map' for all the siblings at that level. It uses the current
130 * affinity level to keep track of how many levels from the root of the tree
131 * have been traversed. If the current affinity level != target affinity level,
132 * then the platform is asked to return the number of children that each
133 * affinity instance has at the current affinity level. Traversal is then done
134 * for each child at the next lower level i.e. current affinity level - 1.
135 *
136 * CAUTION: This routine assumes that affinity instance ids are allocated in a
137 * monotonically increasing manner at each affinity level in a mpidr starting
138 * from 0. If the platform breaks this assumption then this code will have to
139 * be reworked accordingly.
140 ******************************************************************************/
141static unsigned int psci_init_aff_map(unsigned long mpidr,
142 unsigned int affmap_idx,
143 int cur_afflvl,
144 int tgt_afflvl)
145{
146 unsigned int ctr, aff_count;
147
148 assert(cur_afflvl >= tgt_afflvl);
149
150 /*
151 * Find the number of siblings at the current affinity level &
152 * assert if there are none 'cause then we have been invoked with
153 * an invalid mpidr.
154 */
155 aff_count = plat_get_aff_count(cur_afflvl, mpidr);
156 assert(aff_count);
157
158 if (tgt_afflvl < cur_afflvl) {
159 for (ctr = 0; ctr < aff_count; ctr++) {
160 mpidr = mpidr_set_aff_inst(mpidr, ctr, cur_afflvl);
161 affmap_idx = psci_init_aff_map(mpidr,
162 affmap_idx,
163 cur_afflvl - 1,
164 tgt_afflvl);
165 }
166 } else {
167 for (ctr = 0; ctr < aff_count; ctr++, affmap_idx++) {
168 mpidr = mpidr_set_aff_inst(mpidr, ctr, cur_afflvl);
169 psci_init_aff_map_node(mpidr, cur_afflvl, affmap_idx);
170 }
171
172 /* affmap_idx is 1 greater than the max index of cur_afflvl */
173 psci_aff_limits[cur_afflvl].max = affmap_idx - 1;
174 }
175
176 return affmap_idx;
177}
178
179/*******************************************************************************
180 * This function initializes the topology tree by querying the platform. To do
181 * so, it's helper routines implement a Breadth-First-Search. At each affinity
182 * level the platform conveys the number of affinity instances that exist i.e.
183 * the affinity count. The algorithm populates the psci_aff_map recursively
184 * using this information. On a platform that implements two clusters of 4 cpus
185 * each, the populated aff_map_array would look like this:
186 *
187 * <- cpus cluster0 -><- cpus cluster1 ->
188 * ---------------------------------------------------
189 * | 0 | 1 | 0 | 1 | 2 | 3 | 0 | 1 | 2 | 3 |
190 * ---------------------------------------------------
191 * ^ ^
192 * cluster __| cpu __|
193 * limit limit
194 *
195 * The first 2 entries are of the cluster nodes. The next 4 entries are of cpus
196 * within cluster 0. The last 4 entries are of cpus within cluster 1.
197 * The 'psci_aff_limits' array contains the max & min index of each affinity
198 * level within the 'psci_aff_map' array. This allows restricting search of a
199 * node at an affinity level between the indices in the limits array.
200 ******************************************************************************/
201void psci_setup(unsigned long mpidr)
202{
203 int afflvl, affmap_idx, rc, max_afflvl;
204 aff_map_node *node;
205
206 /* Initialize psci's internal state */
207 memset(psci_aff_map, 0, sizeof(psci_aff_map));
208 memset(psci_aff_limits, 0, sizeof(psci_aff_limits));
209 memset(psci_ns_entry_info, 0, sizeof(psci_ns_entry_info));
210 psci_ns_einfo_idx = 0;
211 psci_plat_pm_ops = NULL;
212
213 /* Find out the maximum affinity level that the platform implements */
214 max_afflvl = get_max_afflvl();
215 assert(max_afflvl <= MPIDR_MAX_AFFLVL);
216
217 /*
218 * This call traverses the topology tree with help from the platform and
219 * populates the affinity map using a breadth-first-search recursively.
220 * We assume that the platform allocates affinity instance ids from 0
221 * onwards at each affinity level in the mpidr. FIRST_MPIDR = 0.0.0.0
222 */
223 affmap_idx = 0;
224 for (afflvl = max_afflvl; afflvl >= MPIDR_AFFLVL0; afflvl--) {
225 affmap_idx = psci_init_aff_map(FIRST_MPIDR,
226 affmap_idx,
227 max_afflvl,
228 afflvl);
229 }
230
231 /*
232 * Set the bounds for the affinity counts of each level in the map. Also
233 * flush out the entire array so that it's visible to subsequent power
234 * management operations. The 'psci_aff_map' array is allocated in
235 * coherent memory so does not need flushing. The 'psci_aff_limits'
236 * array is allocated in normal memory. It will be accessed when the mmu
237 * is off e.g. after reset. Hence it needs to be flushed.
238 */
239 for (afflvl = MPIDR_AFFLVL0; afflvl < max_afflvl; afflvl++) {
240 psci_aff_limits[afflvl].min =
241 psci_aff_limits[afflvl + 1].max + 1;
242 }
243
244 flush_dcache_range((unsigned long) psci_aff_limits,
245 sizeof(psci_aff_limits));
246
247 /*
248 * Mark the affinity instances in our mpidr as ON. No need to lock as
249 * this is the primary cpu.
250 */
251 mpidr &= MPIDR_AFFINITY_MASK;
252 for (afflvl = max_afflvl; afflvl >= MPIDR_AFFLVL0; afflvl--) {
253
254 node = psci_get_aff_map_node(mpidr, afflvl);
255 assert(node);
256
257 /* Mark each present node as ON. */
258 if (node->state & PSCI_AFF_PRESENT) {
259 psci_set_state(node->state, PSCI_STATE_ON);
260 }
261 }
262
263 rc = platform_setup_pm(&psci_plat_pm_ops);
264 assert(rc == 0);
265 assert(psci_plat_pm_ops);
266
267 return;
268}