aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Martin <dave.martin@linaro.org>2012-07-17 09:25:42 -0400
committerNicolas Pitre <nicolas.pitre@linaro.org>2013-04-24 10:37:00 -0400
commit7fe31d28e839f9565c8176ec584676a045970802 (patch)
treec3fcc8b99461e1fbff13eb08f8dbec891dc1baa4
parent7c2b860534d02d11923dd0504b961f21508173f1 (diff)
ARM: mcpm: introduce helpers for platform coherency exit/setup
This provides helper methods to coordinate between CPUs coming down and CPUs going up, as well as documentation on the used algorithms, so that cluster teardown and setup operations are not done for a cluster simultaneously. For use in the power_down() implementation: * __mcpm_cpu_going_down(unsigned int cluster, unsigned int cpu) * __mcpm_outbound_enter_critical(unsigned int cluster) * __mcpm_outbound_leave_critical(unsigned int cluster) * __mcpm_cpu_down(unsigned int cluster, unsigned int cpu) The power_up_setup() helper should do platform-specific setup in preparation for turning the CPU on, such as invalidating local caches or entering coherency. It must be assembler for now, since it must run before the MMU can be switched on. It is passed the affinity level for which initialization should be performed. Because the mcpm_sync_struct content is looked-up and modified with the cache enabled or disabled depending on the code path, it is crucial to always ensure proper cache maintenance to update main memory right away. The sync_cache_*() helpers are used to that end. Also, in order to prevent a cached writer from interfering with an adjacent non-cached writer, we ensure each state variable is located to a separate cache line. Thanks to Nicolas Pitre and Achin Gupta for the help with this patch. Signed-off-by: Dave Martin <dave.martin@linaro.org> Signed-off-by: Nicolas Pitre <nico@linaro.org> Reviewed-by: Will Deacon <will.deacon@arm.com>
-rw-r--r--Documentation/arm/cluster-pm-race-avoidance.txt498
-rw-r--r--arch/arm/common/mcpm_entry.c150
-rw-r--r--arch/arm/common/mcpm_head.S106
-rw-r--r--arch/arm/include/asm/mcpm.h73
-rw-r--r--arch/arm/kernel/asm-offsets.c3
5 files changed, 828 insertions, 2 deletions
diff --git a/Documentation/arm/cluster-pm-race-avoidance.txt b/Documentation/arm/cluster-pm-race-avoidance.txt
new file mode 100644
index 000000000000..750b6fc24af9
--- /dev/null
+++ b/Documentation/arm/cluster-pm-race-avoidance.txt
@@ -0,0 +1,498 @@
1Cluster-wide Power-up/power-down race avoidance algorithm
2=========================================================
3
4This file documents the algorithm which is used to coordinate CPU and
5cluster setup and teardown operations and to manage hardware coherency
6controls safely.
7
8The section "Rationale" explains what the algorithm is for and why it is
9needed. "Basic model" explains general concepts using a simplified view
10of the system. The other sections explain the actual details of the
11algorithm in use.
12
13
14Rationale
15---------
16
17In a system containing multiple CPUs, it is desirable to have the
18ability to turn off individual CPUs when the system is idle, reducing
19power consumption and thermal dissipation.
20
21In a system containing multiple clusters of CPUs, it is also desirable
22to have the ability to turn off entire clusters.
23
24Turning entire clusters off and on is a risky business, because it
25involves performing potentially destructive operations affecting a group
26of independently running CPUs, while the OS continues to run. This
27means that we need some coordination in order to ensure that critical
28cluster-level operations are only performed when it is truly safe to do
29so.
30
31Simple locking may not be sufficient to solve this problem, because
32mechanisms like Linux spinlocks may rely on coherency mechanisms which
33are not immediately enabled when a cluster powers up. Since enabling or
34disabling those mechanisms may itself be a non-atomic operation (such as
35writing some hardware registers and invalidating large caches), other
36methods of coordination are required in order to guarantee safe
37power-down and power-up at the cluster level.
38
39The mechanism presented in this document describes a coherent memory
40based protocol for performing the needed coordination. It aims to be as
41lightweight as possible, while providing the required safety properties.
42
43
44Basic model
45-----------
46
47Each cluster and CPU is assigned a state, as follows:
48
49 DOWN
50 COMING_UP
51 UP
52 GOING_DOWN
53
54 +---------> UP ----------+
55 | v
56
57 COMING_UP GOING_DOWN
58
59 ^ |
60 +--------- DOWN <--------+
61
62
63DOWN: The CPU or cluster is not coherent, and is either powered off or
64 suspended, or is ready to be powered off or suspended.
65
66COMING_UP: The CPU or cluster has committed to moving to the UP state.
67 It may be part way through the process of initialisation and
68 enabling coherency.
69
70UP: The CPU or cluster is active and coherent at the hardware
71 level. A CPU in this state is not necessarily being used
72 actively by the kernel.
73
74GOING_DOWN: The CPU or cluster has committed to moving to the DOWN
75 state. It may be part way through the process of teardown and
76 coherency exit.
77
78
79Each CPU has one of these states assigned to it at any point in time.
80The CPU states are described in the "CPU state" section, below.
81
82Each cluster is also assigned a state, but it is necessary to split the
83state value into two parts (the "cluster" state and "inbound" state) and
84to introduce additional states in order to avoid races between different
85CPUs in the cluster simultaneously modifying the state. The cluster-
86level states are described in the "Cluster state" section.
87
88To help distinguish the CPU states from cluster states in this
89discussion, the state names are given a CPU_ prefix for the CPU states,
90and a CLUSTER_ or INBOUND_ prefix for the cluster states.
91
92
93CPU state
94---------
95
96In this algorithm, each individual core in a multi-core processor is
97referred to as a "CPU". CPUs are assumed to be single-threaded:
98therefore, a CPU can only be doing one thing at a single point in time.
99
100This means that CPUs fit the basic model closely.
101
102The algorithm defines the following states for each CPU in the system:
103
104 CPU_DOWN
105 CPU_COMING_UP
106 CPU_UP
107 CPU_GOING_DOWN
108
109 cluster setup and
110 CPU setup complete policy decision
111 +-----------> CPU_UP ------------+
112 | v
113
114 CPU_COMING_UP CPU_GOING_DOWN
115
116 ^ |
117 +----------- CPU_DOWN <----------+
118 policy decision CPU teardown complete
119 or hardware event
120
121
122The definitions of the four states correspond closely to the states of
123the basic model.
124
125Transitions between states occur as follows.
126
127A trigger event (spontaneous) means that the CPU can transition to the
128next state as a result of making local progress only, with no
129requirement for any external event to happen.
130
131
132CPU_DOWN:
133
134 A CPU reaches the CPU_DOWN state when it is ready for
135 power-down. On reaching this state, the CPU will typically
136 power itself down or suspend itself, via a WFI instruction or a
137 firmware call.
138
139 Next state: CPU_COMING_UP
140 Conditions: none
141
142 Trigger events:
143
144 a) an explicit hardware power-up operation, resulting
145 from a policy decision on another CPU;
146
147 b) a hardware event, such as an interrupt.
148
149
150CPU_COMING_UP:
151
152 A CPU cannot start participating in hardware coherency until the
153 cluster is set up and coherent. If the cluster is not ready,
154 then the CPU will wait in the CPU_COMING_UP state until the
155 cluster has been set up.
156
157 Next state: CPU_UP
158 Conditions: The CPU's parent cluster must be in CLUSTER_UP.
159 Trigger events: Transition of the parent cluster to CLUSTER_UP.
160
161 Refer to the "Cluster state" section for a description of the
162 CLUSTER_UP state.
163
164
165CPU_UP:
166 When a CPU reaches the CPU_UP state, it is safe for the CPU to
167 start participating in local coherency.
168
169 This is done by jumping to the kernel's CPU resume code.
170
171 Note that the definition of this state is slightly different
172 from the basic model definition: CPU_UP does not mean that the
173 CPU is coherent yet, but it does mean that it is safe to resume
174 the kernel. The kernel handles the rest of the resume
175 procedure, so the remaining steps are not visible as part of the
176 race avoidance algorithm.
177
178 The CPU remains in this state until an explicit policy decision
179 is made to shut down or suspend the CPU.
180
181 Next state: CPU_GOING_DOWN
182 Conditions: none
183 Trigger events: explicit policy decision
184
185
186CPU_GOING_DOWN:
187
188 While in this state, the CPU exits coherency, including any
189 operations required to achieve this (such as cleaning data
190 caches).
191
192 Next state: CPU_DOWN
193 Conditions: local CPU teardown complete
194 Trigger events: (spontaneous)
195
196
197Cluster state
198-------------
199
200A cluster is a group of connected CPUs with some common resources.
201Because a cluster contains multiple CPUs, it can be doing multiple
202things at the same time. This has some implications. In particular, a
203CPU can start up while another CPU is tearing the cluster down.
204
205In this discussion, the "outbound side" is the view of the cluster state
206as seen by a CPU tearing the cluster down. The "inbound side" is the
207view of the cluster state as seen by a CPU setting the CPU up.
208
209In order to enable safe coordination in such situations, it is important
210that a CPU which is setting up the cluster can advertise its state
211independently of the CPU which is tearing down the cluster. For this
212reason, the cluster state is split into two parts:
213
214 "cluster" state: The global state of the cluster; or the state
215 on the outbound side:
216
217 CLUSTER_DOWN
218 CLUSTER_UP
219 CLUSTER_GOING_DOWN
220
221 "inbound" state: The state of the cluster on the inbound side.
222
223 INBOUND_NOT_COMING_UP
224 INBOUND_COMING_UP
225
226
227 The different pairings of these states results in six possible
228 states for the cluster as a whole:
229
230 CLUSTER_UP
231 +==========> INBOUND_NOT_COMING_UP -------------+
232 # |
233 |
234 CLUSTER_UP <----+ |
235 INBOUND_COMING_UP | v
236
237 ^ CLUSTER_GOING_DOWN CLUSTER_GOING_DOWN
238 # INBOUND_COMING_UP <=== INBOUND_NOT_COMING_UP
239
240 CLUSTER_DOWN | |
241 INBOUND_COMING_UP <----+ |
242 |
243 ^ |
244 +=========== CLUSTER_DOWN <------------+
245 INBOUND_NOT_COMING_UP
246
247 Transitions -----> can only be made by the outbound CPU, and
248 only involve changes to the "cluster" state.
249
250 Transitions ===##> can only be made by the inbound CPU, and only
251 involve changes to the "inbound" state, except where there is no
252 further transition possible on the outbound side (i.e., the
253 outbound CPU has put the cluster into the CLUSTER_DOWN state).
254
255 The race avoidance algorithm does not provide a way to determine
256 which exact CPUs within the cluster play these roles. This must
257 be decided in advance by some other means. Refer to the section
258 "Last man and first man selection" for more explanation.
259
260
261 CLUSTER_DOWN/INBOUND_NOT_COMING_UP is the only state where the
262 cluster can actually be powered down.
263
264 The parallelism of the inbound and outbound CPUs is observed by
265 the existence of two different paths from CLUSTER_GOING_DOWN/
266 INBOUND_NOT_COMING_UP (corresponding to GOING_DOWN in the basic
267 model) to CLUSTER_DOWN/INBOUND_COMING_UP (corresponding to
268 COMING_UP in the basic model). The second path avoids cluster
269 teardown completely.
270
271 CLUSTER_UP/INBOUND_COMING_UP is equivalent to UP in the basic
272 model. The final transition to CLUSTER_UP/INBOUND_NOT_COMING_UP
273 is trivial and merely resets the state machine ready for the
274 next cycle.
275
276 Details of the allowable transitions follow.
277
278 The next state in each case is notated
279
280 <cluster state>/<inbound state> (<transitioner>)
281
282 where the <transitioner> is the side on which the transition
283 can occur; either the inbound or the outbound side.
284
285
286CLUSTER_DOWN/INBOUND_NOT_COMING_UP:
287
288 Next state: CLUSTER_DOWN/INBOUND_COMING_UP (inbound)
289 Conditions: none
290 Trigger events:
291
292 a) an explicit hardware power-up operation, resulting
293 from a policy decision on another CPU;
294
295 b) a hardware event, such as an interrupt.
296
297
298CLUSTER_DOWN/INBOUND_COMING_UP:
299
300 In this state, an inbound CPU sets up the cluster, including
301 enabling of hardware coherency at the cluster level and any
302 other operations (such as cache invalidation) which are required
303 in order to achieve this.
304
305 The purpose of this state is to do sufficient cluster-level
306 setup to enable other CPUs in the cluster to enter coherency
307 safely.
308
309 Next state: CLUSTER_UP/INBOUND_COMING_UP (inbound)
310 Conditions: cluster-level setup and hardware coherency complete
311 Trigger events: (spontaneous)
312
313
314CLUSTER_UP/INBOUND_COMING_UP:
315
316 Cluster-level setup is complete and hardware coherency is
317 enabled for the cluster. Other CPUs in the cluster can safely
318 enter coherency.
319
320 This is a transient state, leading immediately to
321 CLUSTER_UP/INBOUND_NOT_COMING_UP. All other CPUs on the cluster
322 should consider treat these two states as equivalent.
323
324 Next state: CLUSTER_UP/INBOUND_NOT_COMING_UP (inbound)
325 Conditions: none
326 Trigger events: (spontaneous)
327
328
329CLUSTER_UP/INBOUND_NOT_COMING_UP:
330
331 Cluster-level setup is complete and hardware coherency is
332 enabled for the cluster. Other CPUs in the cluster can safely
333 enter coherency.
334
335 The cluster will remain in this state until a policy decision is
336 made to power the cluster down.
337
338 Next state: CLUSTER_GOING_DOWN/INBOUND_NOT_COMING_UP (outbound)
339 Conditions: none
340 Trigger events: policy decision to power down the cluster
341
342
343CLUSTER_GOING_DOWN/INBOUND_NOT_COMING_UP:
344
345 An outbound CPU is tearing the cluster down. The selected CPU
346 must wait in this state until all CPUs in the cluster are in the
347 CPU_DOWN state.
348
349 When all CPUs are in the CPU_DOWN state, the cluster can be torn
350 down, for example by cleaning data caches and exiting
351 cluster-level coherency.
352
353 To avoid wasteful unnecessary teardown operations, the outbound
354 should check the inbound cluster state for asynchronous
355 transitions to INBOUND_COMING_UP. Alternatively, individual
356 CPUs can be checked for entry into CPU_COMING_UP or CPU_UP.
357
358
359 Next states:
360
361 CLUSTER_DOWN/INBOUND_NOT_COMING_UP (outbound)
362 Conditions: cluster torn down and ready to power off
363 Trigger events: (spontaneous)
364
365 CLUSTER_GOING_DOWN/INBOUND_COMING_UP (inbound)
366 Conditions: none
367 Trigger events:
368
369 a) an explicit hardware power-up operation,
370 resulting from a policy decision on another
371 CPU;
372
373 b) a hardware event, such as an interrupt.
374
375
376CLUSTER_GOING_DOWN/INBOUND_COMING_UP:
377
378 The cluster is (or was) being torn down, but another CPU has
379 come online in the meantime and is trying to set up the cluster
380 again.
381
382 If the outbound CPU observes this state, it has two choices:
383
384 a) back out of teardown, restoring the cluster to the
385 CLUSTER_UP state;
386
387 b) finish tearing the cluster down and put the cluster
388 in the CLUSTER_DOWN state; the inbound CPU will
389 set up the cluster again from there.
390
391 Choice (a) permits the removal of some latency by avoiding
392 unnecessary teardown and setup operations in situations where
393 the cluster is not really going to be powered down.
394
395
396 Next states:
397
398 CLUSTER_UP/INBOUND_COMING_UP (outbound)
399 Conditions: cluster-level setup and hardware
400 coherency complete
401 Trigger events: (spontaneous)
402
403 CLUSTER_DOWN/INBOUND_COMING_UP (outbound)
404 Conditions: cluster torn down and ready to power off
405 Trigger events: (spontaneous)
406
407
408Last man and First man selection
409--------------------------------
410
411The CPU which performs cluster tear-down operations on the outbound side
412is commonly referred to as the "last man".
413
414The CPU which performs cluster setup on the inbound side is commonly
415referred to as the "first man".
416
417The race avoidance algorithm documented above does not provide a
418mechanism to choose which CPUs should play these roles.
419
420
421Last man:
422
423When shutting down the cluster, all the CPUs involved are initially
424executing Linux and hence coherent. Therefore, ordinary spinlocks can
425be used to select a last man safely, before the CPUs become
426non-coherent.
427
428
429First man:
430
431Because CPUs may power up asynchronously in response to external wake-up
432events, a dynamic mechanism is needed to make sure that only one CPU
433attempts to play the first man role and do the cluster-level
434initialisation: any other CPUs must wait for this to complete before
435proceeding.
436
437Cluster-level initialisation may involve actions such as configuring
438coherency controls in the bus fabric.
439
440The current implementation in mcpm_head.S uses a separate mutual exclusion
441mechanism to do this arbitration. This mechanism is documented in
442detail in vlocks.txt.
443
444
445Features and Limitations
446------------------------
447
448Implementation:
449
450 The current ARM-based implementation is split between
451 arch/arm/common/mcpm_head.S (low-level inbound CPU operations) and
452 arch/arm/common/mcpm_entry.c (everything else):
453
454 __mcpm_cpu_going_down() signals the transition of a CPU to the
455 CPU_GOING_DOWN state.
456
457 __mcpm_cpu_down() signals the transition of a CPU to the CPU_DOWN
458 state.
459
460 A CPU transitions to CPU_COMING_UP and then to CPU_UP via the
461 low-level power-up code in mcpm_head.S. This could
462 involve CPU-specific setup code, but in the current
463 implementation it does not.
464
465 __mcpm_outbound_enter_critical() and __mcpm_outbound_leave_critical()
466 handle transitions from CLUSTER_UP to CLUSTER_GOING_DOWN
467 and from there to CLUSTER_DOWN or back to CLUSTER_UP (in
468 the case of an aborted cluster power-down).
469
470 These functions are more complex than the __mcpm_cpu_*()
471 functions due to the extra inter-CPU coordination which
472 is needed for safe transitions at the cluster level.
473
474 A cluster transitions from CLUSTER_DOWN back to CLUSTER_UP via
475 the low-level power-up code in mcpm_head.S. This
476 typically involves platform-specific setup code,
477 provided by the platform-specific power_up_setup
478 function registered via mcpm_sync_init.
479
480Deep topologies:
481
482 As currently described and implemented, the algorithm does not
483 support CPU topologies involving more than two levels (i.e.,
484 clusters of clusters are not supported). The algorithm could be
485 extended by replicating the cluster-level states for the
486 additional topological levels, and modifying the transition
487 rules for the intermediate (non-outermost) cluster levels.
488
489
490Colophon
491--------
492
493Originally created and documented by Dave Martin for Linaro Limited, in
494collaboration with Nicolas Pitre and Achin Gupta.
495
496Copyright (C) 2012-2013 Linaro Limited
497Distributed under the terms of Version 2 of the GNU General Public
498License, as defined in linux/COPYING.
diff --git a/arch/arm/common/mcpm_entry.c b/arch/arm/common/mcpm_entry.c
index 5d72889a58a4..370236dd1a03 100644
--- a/arch/arm/common/mcpm_entry.c
+++ b/arch/arm/common/mcpm_entry.c
@@ -16,6 +16,7 @@
16#include <asm/mcpm.h> 16#include <asm/mcpm.h>
17#include <asm/cacheflush.h> 17#include <asm/cacheflush.h>
18#include <asm/idmap.h> 18#include <asm/idmap.h>
19#include <asm/cputype.h>
19 20
20extern unsigned long mcpm_entry_vectors[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER]; 21extern unsigned long mcpm_entry_vectors[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER];
21 22
@@ -111,3 +112,152 @@ int mcpm_cpu_powered_up(void)
111 platform_ops->powered_up(); 112 platform_ops->powered_up();
112 return 0; 113 return 0;
113} 114}
115
116struct sync_struct mcpm_sync;
117
118/*
119 * __mcpm_cpu_going_down: Indicates that the cpu is being torn down.
120 * This must be called at the point of committing to teardown of a CPU.
121 * The CPU cache (SCTRL.C bit) is expected to still be active.
122 */
123void __mcpm_cpu_going_down(unsigned int cpu, unsigned int cluster)
124{
125 mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_GOING_DOWN;
126 sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
127}
128
129/*
130 * __mcpm_cpu_down: Indicates that cpu teardown is complete and that the
131 * cluster can be torn down without disrupting this CPU.
132 * To avoid deadlocks, this must be called before a CPU is powered down.
133 * The CPU cache (SCTRL.C bit) is expected to be off.
134 * However L2 cache might or might not be active.
135 */
136void __mcpm_cpu_down(unsigned int cpu, unsigned int cluster)
137{
138 dmb();
139 mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_DOWN;
140 sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
141 dsb_sev();
142}
143
144/*
145 * __mcpm_outbound_leave_critical: Leave the cluster teardown critical section.
146 * @state: the final state of the cluster:
147 * CLUSTER_UP: no destructive teardown was done and the cluster has been
148 * restored to the previous state (CPU cache still active); or
149 * CLUSTER_DOWN: the cluster has been torn-down, ready for power-off
150 * (CPU cache disabled, L2 cache either enabled or disabled).
151 */
152void __mcpm_outbound_leave_critical(unsigned int cluster, int state)
153{
154 dmb();
155 mcpm_sync.clusters[cluster].cluster = state;
156 sync_cache_w(&mcpm_sync.clusters[cluster].cluster);
157 dsb_sev();
158}
159
160/*
161 * __mcpm_outbound_enter_critical: Enter the cluster teardown critical section.
162 * This function should be called by the last man, after local CPU teardown
163 * is complete. CPU cache expected to be active.
164 *
165 * Returns:
166 * false: the critical section was not entered because an inbound CPU was
167 * observed, or the cluster is already being set up;
168 * true: the critical section was entered: it is now safe to tear down the
169 * cluster.
170 */
171bool __mcpm_outbound_enter_critical(unsigned int cpu, unsigned int cluster)
172{
173 unsigned int i;
174 struct mcpm_sync_struct *c = &mcpm_sync.clusters[cluster];
175
176 /* Warn inbound CPUs that the cluster is being torn down: */
177 c->cluster = CLUSTER_GOING_DOWN;
178 sync_cache_w(&c->cluster);
179
180 /* Back out if the inbound cluster is already in the critical region: */
181 sync_cache_r(&c->inbound);
182 if (c->inbound == INBOUND_COMING_UP)
183 goto abort;
184
185 /*
186 * Wait for all CPUs to get out of the GOING_DOWN state, so that local
187 * teardown is complete on each CPU before tearing down the cluster.
188 *
189 * If any CPU has been woken up again from the DOWN state, then we
190 * shouldn't be taking the cluster down at all: abort in that case.
191 */
192 sync_cache_r(&c->cpus);
193 for (i = 0; i < MAX_CPUS_PER_CLUSTER; i++) {
194 int cpustate;
195
196 if (i == cpu)
197 continue;
198
199 while (1) {
200 cpustate = c->cpus[i].cpu;
201 if (cpustate != CPU_GOING_DOWN)
202 break;
203
204 wfe();
205 sync_cache_r(&c->cpus[i].cpu);
206 }
207
208 switch (cpustate) {
209 case CPU_DOWN:
210 continue;
211
212 default:
213 goto abort;
214 }
215 }
216
217 return true;
218
219abort:
220 __mcpm_outbound_leave_critical(cluster, CLUSTER_UP);
221 return false;
222}
223
224int __mcpm_cluster_state(unsigned int cluster)
225{
226 sync_cache_r(&mcpm_sync.clusters[cluster].cluster);
227 return mcpm_sync.clusters[cluster].cluster;
228}
229
230extern unsigned long mcpm_power_up_setup_phys;
231
232int __init mcpm_sync_init(
233 void (*power_up_setup)(unsigned int affinity_level))
234{
235 unsigned int i, j, mpidr, this_cluster;
236
237 BUILD_BUG_ON(MCPM_SYNC_CLUSTER_SIZE * MAX_NR_CLUSTERS != sizeof mcpm_sync);
238 BUG_ON((unsigned long)&mcpm_sync & (__CACHE_WRITEBACK_GRANULE - 1));
239
240 /*
241 * Set initial CPU and cluster states.
242 * Only one cluster is assumed to be active at this point.
243 */
244 for (i = 0; i < MAX_NR_CLUSTERS; i++) {
245 mcpm_sync.clusters[i].cluster = CLUSTER_DOWN;
246 mcpm_sync.clusters[i].inbound = INBOUND_NOT_COMING_UP;
247 for (j = 0; j < MAX_CPUS_PER_CLUSTER; j++)
248 mcpm_sync.clusters[i].cpus[j].cpu = CPU_DOWN;
249 }
250 mpidr = read_cpuid_mpidr();
251 this_cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
252 for_each_online_cpu(i)
253 mcpm_sync.clusters[this_cluster].cpus[i].cpu = CPU_UP;
254 mcpm_sync.clusters[this_cluster].cluster = CLUSTER_UP;
255 sync_cache_w(&mcpm_sync);
256
257 if (power_up_setup) {
258 mcpm_power_up_setup_phys = virt_to_phys(power_up_setup);
259 sync_cache_w(&mcpm_power_up_setup_phys);
260 }
261
262 return 0;
263}
diff --git a/arch/arm/common/mcpm_head.S b/arch/arm/common/mcpm_head.S
index 68c9903075a9..7d729bd72674 100644
--- a/arch/arm/common/mcpm_head.S
+++ b/arch/arm/common/mcpm_head.S
@@ -7,11 +7,19 @@
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as 8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation. 9 * published by the Free Software Foundation.
10 *
11 *
12 * Refer to Documentation/arm/cluster-pm-race-avoidance.txt
13 * for details of the synchronisation algorithms used here.
10 */ 14 */
11 15
12#include <linux/linkage.h> 16#include <linux/linkage.h>
13#include <asm/mcpm.h> 17#include <asm/mcpm.h>
14 18
19.if MCPM_SYNC_CLUSTER_CPUS
20.error "cpus must be the first member of struct mcpm_sync_struct"
21.endif
22
15 .macro pr_dbg string 23 .macro pr_dbg string
16#if defined(CONFIG_DEBUG_LL) && defined(DEBUG) 24#if defined(CONFIG_DEBUG_LL) && defined(DEBUG)
17 b 1901f 25 b 1901f
@@ -57,24 +65,114 @@ ENTRY(mcpm_entry_point)
572: pr_dbg "kernel mcpm_entry_point\n" 652: pr_dbg "kernel mcpm_entry_point\n"
58 66
59 /* 67 /*
60 * MMU is off so we need to get to mcpm_entry_vectors in a 68 * MMU is off so we need to get to various variables in a
61 * position independent way. 69 * position independent way.
62 */ 70 */
63 adr r5, 3f 71 adr r5, 3f
64 ldr r6, [r5] 72 ldmia r5, {r6, r7, r8}
65 add r6, r5, r6 @ r6 = mcpm_entry_vectors 73 add r6, r5, r6 @ r6 = mcpm_entry_vectors
74 ldr r7, [r5, r7] @ r7 = mcpm_power_up_setup_phys
75 add r8, r5, r8 @ r8 = mcpm_sync
76
77 mov r0, #MCPM_SYNC_CLUSTER_SIZE
78 mla r8, r0, r10, r8 @ r8 = sync cluster base
79
80 @ Signal that this CPU is coming UP:
81 mov r0, #CPU_COMING_UP
82 mov r5, #MCPM_SYNC_CPU_SIZE
83 mla r5, r9, r5, r8 @ r5 = sync cpu address
84 strb r0, [r5]
85
86 @ At this point, the cluster cannot unexpectedly enter the GOING_DOWN
87 @ state, because there is at least one active CPU (this CPU).
88
89 @ Note: the following is racy as another CPU might be testing
90 @ the same flag at the same moment. That'll be fixed later.
91 ldrb r0, [r8, #MCPM_SYNC_CLUSTER_CLUSTER]
92 cmp r0, #CLUSTER_UP @ cluster already up?
93 bne mcpm_setup @ if not, set up the cluster
94
95 @ Otherwise, skip setup:
96 b mcpm_setup_complete
97
98mcpm_setup:
99 @ Control dependency implies strb not observable before previous ldrb.
100
101 @ Signal that the cluster is being brought up:
102 mov r0, #INBOUND_COMING_UP
103 strb r0, [r8, #MCPM_SYNC_CLUSTER_INBOUND]
104 dmb
105
106 @ Any CPU trying to take the cluster into CLUSTER_GOING_DOWN from this
107 @ point onwards will observe INBOUND_COMING_UP and abort.
108
109 @ Wait for any previously-pending cluster teardown operations to abort
110 @ or complete:
111mcpm_teardown_wait:
112 ldrb r0, [r8, #MCPM_SYNC_CLUSTER_CLUSTER]
113 cmp r0, #CLUSTER_GOING_DOWN
114 bne first_man_setup
115 wfe
116 b mcpm_teardown_wait
117
118first_man_setup:
119 dmb
120
121 @ If the outbound gave up before teardown started, skip cluster setup:
122
123 cmp r0, #CLUSTER_UP
124 beq mcpm_setup_leave
125
126 @ power_up_setup is now responsible for setting up the cluster:
127
128 cmp r7, #0
129 mov r0, #1 @ second (cluster) affinity level
130 blxne r7 @ Call power_up_setup if defined
131 dmb
132
133 mov r0, #CLUSTER_UP
134 strb r0, [r8, #MCPM_SYNC_CLUSTER_CLUSTER]
135 dmb
136
137mcpm_setup_leave:
138 @ Leave the cluster setup critical section:
139
140 mov r0, #INBOUND_NOT_COMING_UP
141 strb r0, [r8, #MCPM_SYNC_CLUSTER_INBOUND]
142 dsb
143 sev
144
145mcpm_setup_complete:
146 @ If a platform-specific CPU setup hook is needed, it is
147 @ called from here.
148
149 cmp r7, #0
150 mov r0, #0 @ first (CPU) affinity level
151 blxne r7 @ Call power_up_setup if defined
152 dmb
153
154 @ Mark the CPU as up:
155
156 mov r0, #CPU_UP
157 strb r0, [r5]
158
159 @ Observability order of CPU_UP and opening of the gate does not matter.
66 160
67mcpm_entry_gated: 161mcpm_entry_gated:
68 ldr r5, [r6, r4, lsl #2] @ r5 = CPU entry vector 162 ldr r5, [r6, r4, lsl #2] @ r5 = CPU entry vector
69 cmp r5, #0 163 cmp r5, #0
70 wfeeq 164 wfeeq
71 beq mcpm_entry_gated 165 beq mcpm_entry_gated
166 dmb
167
72 pr_dbg "released\n" 168 pr_dbg "released\n"
73 bx r5 169 bx r5
74 170
75 .align 2 171 .align 2
76 172
773: .word mcpm_entry_vectors - . 1733: .word mcpm_entry_vectors - .
174 .word mcpm_power_up_setup_phys - 3b
175 .word mcpm_sync - 3b
78 176
79ENDPROC(mcpm_entry_point) 177ENDPROC(mcpm_entry_point)
80 178
@@ -84,3 +182,7 @@ ENDPROC(mcpm_entry_point)
84 .type mcpm_entry_vectors, #object 182 .type mcpm_entry_vectors, #object
85ENTRY(mcpm_entry_vectors) 183ENTRY(mcpm_entry_vectors)
86 .space 4 * MAX_NR_CLUSTERS * MAX_CPUS_PER_CLUSTER 184 .space 4 * MAX_NR_CLUSTERS * MAX_CPUS_PER_CLUSTER
185
186 .type mcpm_power_up_setup_phys, #object
187ENTRY(mcpm_power_up_setup_phys)
188 .space 4 @ set by mcpm_sync_init()
diff --git a/arch/arm/include/asm/mcpm.h b/arch/arm/include/asm/mcpm.h
index 627761fce780..3046e90210cb 100644
--- a/arch/arm/include/asm/mcpm.h
+++ b/arch/arm/include/asm/mcpm.h
@@ -24,6 +24,9 @@
24 24
25#ifndef __ASSEMBLY__ 25#ifndef __ASSEMBLY__
26 26
27#include <linux/types.h>
28#include <asm/cacheflush.h>
29
27/* 30/*
28 * Platform specific code should use this symbol to set up secondary 31 * Platform specific code should use this symbol to set up secondary
29 * entry location for processors to use when released from reset. 32 * entry location for processors to use when released from reset.
@@ -130,5 +133,75 @@ struct mcpm_platform_ops {
130 */ 133 */
131int __init mcpm_platform_register(const struct mcpm_platform_ops *ops); 134int __init mcpm_platform_register(const struct mcpm_platform_ops *ops);
132 135
136/* Synchronisation structures for coordinating safe cluster setup/teardown: */
137
138/*
139 * When modifying this structure, make sure you update the MCPM_SYNC_ defines
140 * to match.
141 */
142struct mcpm_sync_struct {
143 /* individual CPU states */
144 struct {
145 s8 cpu __aligned(__CACHE_WRITEBACK_GRANULE);
146 } cpus[MAX_CPUS_PER_CLUSTER];
147
148 /* cluster state */
149 s8 cluster __aligned(__CACHE_WRITEBACK_GRANULE);
150
151 /* inbound-side state */
152 s8 inbound __aligned(__CACHE_WRITEBACK_GRANULE);
153};
154
155struct sync_struct {
156 struct mcpm_sync_struct clusters[MAX_NR_CLUSTERS];
157};
158
159extern unsigned long sync_phys; /* physical address of *mcpm_sync */
160
161void __mcpm_cpu_going_down(unsigned int cpu, unsigned int cluster);
162void __mcpm_cpu_down(unsigned int cpu, unsigned int cluster);
163void __mcpm_outbound_leave_critical(unsigned int cluster, int state);
164bool __mcpm_outbound_enter_critical(unsigned int this_cpu, unsigned int cluster);
165int __mcpm_cluster_state(unsigned int cluster);
166
167int __init mcpm_sync_init(
168 void (*power_up_setup)(unsigned int affinity_level));
169
170#else
171
172/*
173 * asm-offsets.h causes trouble when included in .c files, and cacheflush.h
174 * cannot be included in asm files. Let's work around the conflict like this.
175 */
176#include <asm/asm-offsets.h>
177#define __CACHE_WRITEBACK_GRANULE CACHE_WRITEBACK_GRANULE
178
133#endif /* ! __ASSEMBLY__ */ 179#endif /* ! __ASSEMBLY__ */
180
181/* Definitions for mcpm_sync_struct */
182#define CPU_DOWN 0x11
183#define CPU_COMING_UP 0x12
184#define CPU_UP 0x13
185#define CPU_GOING_DOWN 0x14
186
187#define CLUSTER_DOWN 0x21
188#define CLUSTER_UP 0x22
189#define CLUSTER_GOING_DOWN 0x23
190
191#define INBOUND_NOT_COMING_UP 0x31
192#define INBOUND_COMING_UP 0x32
193
194/*
195 * Offsets for the mcpm_sync_struct members, for use in asm.
196 * We don't want to make them global to the kernel via asm-offsets.c.
197 */
198#define MCPM_SYNC_CLUSTER_CPUS 0
199#define MCPM_SYNC_CPU_SIZE __CACHE_WRITEBACK_GRANULE
200#define MCPM_SYNC_CLUSTER_CLUSTER \
201 (MCPM_SYNC_CLUSTER_CPUS + MCPM_SYNC_CPU_SIZE * MAX_CPUS_PER_CLUSTER)
202#define MCPM_SYNC_CLUSTER_INBOUND \
203 (MCPM_SYNC_CLUSTER_CLUSTER + __CACHE_WRITEBACK_GRANULE)
204#define MCPM_SYNC_CLUSTER_SIZE \
205 (MCPM_SYNC_CLUSTER_INBOUND + __CACHE_WRITEBACK_GRANULE)
206
134#endif 207#endif
diff --git a/arch/arm/kernel/asm-offsets.c b/arch/arm/kernel/asm-offsets.c
index 923eec7105cf..1bed82a0a9e0 100644
--- a/arch/arm/kernel/asm-offsets.c
+++ b/arch/arm/kernel/asm-offsets.c
@@ -149,6 +149,9 @@ int main(void)
149 DEFINE(DMA_BIDIRECTIONAL, DMA_BIDIRECTIONAL); 149 DEFINE(DMA_BIDIRECTIONAL, DMA_BIDIRECTIONAL);
150 DEFINE(DMA_TO_DEVICE, DMA_TO_DEVICE); 150 DEFINE(DMA_TO_DEVICE, DMA_TO_DEVICE);
151 DEFINE(DMA_FROM_DEVICE, DMA_FROM_DEVICE); 151 DEFINE(DMA_FROM_DEVICE, DMA_FROM_DEVICE);
152 BLANK();
153 DEFINE(CACHE_WRITEBACK_GRANULE, __CACHE_WRITEBACK_GRANULE);
154 BLANK();
152#ifdef CONFIG_KVM_ARM_HOST 155#ifdef CONFIG_KVM_ARM_HOST
153 DEFINE(VCPU_KVM, offsetof(struct kvm_vcpu, kvm)); 156 DEFINE(VCPU_KVM, offsetof(struct kvm_vcpu, kvm));
154 DEFINE(VCPU_MIDR, offsetof(struct kvm_vcpu, arch.midr)); 157 DEFINE(VCPU_MIDR, offsetof(struct kvm_vcpu, arch.midr));