Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Dave Martin and committed by
Nicolas Pitre
7fe31d28 7c2b8605

+828 -2
+498
Documentation/arm/cluster-pm-race-avoidance.txt
··· 1 + Cluster-wide Power-up/power-down race avoidance algorithm 2 + ========================================================= 3 + 4 + This file documents the algorithm which is used to coordinate CPU and 5 + cluster setup and teardown operations and to manage hardware coherency 6 + controls safely. 7 + 8 + The section "Rationale" explains what the algorithm is for and why it is 9 + needed. "Basic model" explains general concepts using a simplified view 10 + of the system. The other sections explain the actual details of the 11 + algorithm in use. 12 + 13 + 14 + Rationale 15 + --------- 16 + 17 + In a system containing multiple CPUs, it is desirable to have the 18 + ability to turn off individual CPUs when the system is idle, reducing 19 + power consumption and thermal dissipation. 20 + 21 + In a system containing multiple clusters of CPUs, it is also desirable 22 + to have the ability to turn off entire clusters. 23 + 24 + Turning entire clusters off and on is a risky business, because it 25 + involves performing potentially destructive operations affecting a group 26 + of independently running CPUs, while the OS continues to run. This 27 + means that we need some coordination in order to ensure that critical 28 + cluster-level operations are only performed when it is truly safe to do 29 + so. 30 + 31 + Simple locking may not be sufficient to solve this problem, because 32 + mechanisms like Linux spinlocks may rely on coherency mechanisms which 33 + are not immediately enabled when a cluster powers up. Since enabling or 34 + disabling those mechanisms may itself be a non-atomic operation (such as 35 + writing some hardware registers and invalidating large caches), other 36 + methods of coordination are required in order to guarantee safe 37 + power-down and power-up at the cluster level. 38 + 39 + The mechanism presented in this document describes a coherent memory 40 + based protocol for performing the needed coordination. It aims to be as 41 + lightweight as possible, while providing the required safety properties. 42 + 43 + 44 + Basic model 45 + ----------- 46 + 47 + Each 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 + 63 + DOWN: 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 + 66 + COMING_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 + 70 + UP: 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 + 74 + GOING_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 + 79 + Each CPU has one of these states assigned to it at any point in time. 80 + The CPU states are described in the "CPU state" section, below. 81 + 82 + Each cluster is also assigned a state, but it is necessary to split the 83 + state value into two parts (the "cluster" state and "inbound" state) and 84 + to introduce additional states in order to avoid races between different 85 + CPUs in the cluster simultaneously modifying the state. The cluster- 86 + level states are described in the "Cluster state" section. 87 + 88 + To help distinguish the CPU states from cluster states in this 89 + discussion, the state names are given a CPU_ prefix for the CPU states, 90 + and a CLUSTER_ or INBOUND_ prefix for the cluster states. 91 + 92 + 93 + CPU state 94 + --------- 95 + 96 + In this algorithm, each individual core in a multi-core processor is 97 + referred to as a "CPU". CPUs are assumed to be single-threaded: 98 + therefore, a CPU can only be doing one thing at a single point in time. 99 + 100 + This means that CPUs fit the basic model closely. 101 + 102 + The 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 + 122 + The definitions of the four states correspond closely to the states of 123 + the basic model. 124 + 125 + Transitions between states occur as follows. 126 + 127 + A trigger event (spontaneous) means that the CPU can transition to the 128 + next state as a result of making local progress only, with no 129 + requirement for any external event to happen. 130 + 131 + 132 + CPU_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 + 150 + CPU_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 + 165 + CPU_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 + 186 + CPU_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 + 197 + Cluster state 198 + ------------- 199 + 200 + A cluster is a group of connected CPUs with some common resources. 201 + Because a cluster contains multiple CPUs, it can be doing multiple 202 + things at the same time. This has some implications. In particular, a 203 + CPU can start up while another CPU is tearing the cluster down. 204 + 205 + In this discussion, the "outbound side" is the view of the cluster state 206 + as seen by a CPU tearing the cluster down. The "inbound side" is the 207 + view of the cluster state as seen by a CPU setting the CPU up. 208 + 209 + In order to enable safe coordination in such situations, it is important 210 + that a CPU which is setting up the cluster can advertise its state 211 + independently of the CPU which is tearing down the cluster. For this 212 + reason, 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 + 286 + CLUSTER_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 + 298 + CLUSTER_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 + 314 + CLUSTER_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 + 329 + CLUSTER_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 + 343 + CLUSTER_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 + 376 + CLUSTER_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 + 408 + Last man and First man selection 409 + -------------------------------- 410 + 411 + The CPU which performs cluster tear-down operations on the outbound side 412 + is commonly referred to as the "last man". 413 + 414 + The CPU which performs cluster setup on the inbound side is commonly 415 + referred to as the "first man". 416 + 417 + The race avoidance algorithm documented above does not provide a 418 + mechanism to choose which CPUs should play these roles. 419 + 420 + 421 + Last man: 422 + 423 + When shutting down the cluster, all the CPUs involved are initially 424 + executing Linux and hence coherent. Therefore, ordinary spinlocks can 425 + be used to select a last man safely, before the CPUs become 426 + non-coherent. 427 + 428 + 429 + First man: 430 + 431 + Because CPUs may power up asynchronously in response to external wake-up 432 + events, a dynamic mechanism is needed to make sure that only one CPU 433 + attempts to play the first man role and do the cluster-level 434 + initialisation: any other CPUs must wait for this to complete before 435 + proceeding. 436 + 437 + Cluster-level initialisation may involve actions such as configuring 438 + coherency controls in the bus fabric. 439 + 440 + The current implementation in mcpm_head.S uses a separate mutual exclusion 441 + mechanism to do this arbitration. This mechanism is documented in 442 + detail in vlocks.txt. 443 + 444 + 445 + Features and Limitations 446 + ------------------------ 447 + 448 + Implementation: 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 + 480 + Deep 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 + 490 + Colophon 491 + -------- 492 + 493 + Originally created and documented by Dave Martin for Linaro Limited, in 494 + collaboration with Nicolas Pitre and Achin Gupta. 495 + 496 + Copyright (C) 2012-2013 Linaro Limited 497 + Distributed under the terms of Version 2 of the GNU General Public 498 + License, as defined in linux/COPYING.
+150
arch/arm/common/mcpm_entry.c
··· 16 16 #include <asm/mcpm.h> 17 17 #include <asm/cacheflush.h> 18 18 #include <asm/idmap.h> 19 + #include <asm/cputype.h> 19 20 20 21 extern unsigned long mcpm_entry_vectors[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER]; 21 22 ··· 110 109 return -EUNATCH; 111 110 if (platform_ops->powered_up) 112 111 platform_ops->powered_up(); 112 + return 0; 113 + } 114 + 115 + struct sync_struct mcpm_sync; 116 + 117 + /* 118 + * __mcpm_cpu_going_down: Indicates that the cpu is being torn down. 119 + * This must be called at the point of committing to teardown of a CPU. 120 + * The CPU cache (SCTRL.C bit) is expected to still be active. 121 + */ 122 + void __mcpm_cpu_going_down(unsigned int cpu, unsigned int cluster) 123 + { 124 + mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_GOING_DOWN; 125 + sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu); 126 + } 127 + 128 + /* 129 + * __mcpm_cpu_down: Indicates that cpu teardown is complete and that the 130 + * cluster can be torn down without disrupting this CPU. 131 + * To avoid deadlocks, this must be called before a CPU is powered down. 132 + * The CPU cache (SCTRL.C bit) is expected to be off. 133 + * However L2 cache might or might not be active. 134 + */ 135 + void __mcpm_cpu_down(unsigned int cpu, unsigned int cluster) 136 + { 137 + dmb(); 138 + mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_DOWN; 139 + sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu); 140 + dsb_sev(); 141 + } 142 + 143 + /* 144 + * __mcpm_outbound_leave_critical: Leave the cluster teardown critical section. 145 + * @state: the final state of the cluster: 146 + * CLUSTER_UP: no destructive teardown was done and the cluster has been 147 + * restored to the previous state (CPU cache still active); or 148 + * CLUSTER_DOWN: the cluster has been torn-down, ready for power-off 149 + * (CPU cache disabled, L2 cache either enabled or disabled). 150 + */ 151 + void __mcpm_outbound_leave_critical(unsigned int cluster, int state) 152 + { 153 + dmb(); 154 + mcpm_sync.clusters[cluster].cluster = state; 155 + sync_cache_w(&mcpm_sync.clusters[cluster].cluster); 156 + dsb_sev(); 157 + } 158 + 159 + /* 160 + * __mcpm_outbound_enter_critical: Enter the cluster teardown critical section. 161 + * This function should be called by the last man, after local CPU teardown 162 + * is complete. CPU cache expected to be active. 163 + * 164 + * Returns: 165 + * false: the critical section was not entered because an inbound CPU was 166 + * observed, or the cluster is already being set up; 167 + * true: the critical section was entered: it is now safe to tear down the 168 + * cluster. 169 + */ 170 + bool __mcpm_outbound_enter_critical(unsigned int cpu, unsigned int cluster) 171 + { 172 + unsigned int i; 173 + struct mcpm_sync_struct *c = &mcpm_sync.clusters[cluster]; 174 + 175 + /* Warn inbound CPUs that the cluster is being torn down: */ 176 + c->cluster = CLUSTER_GOING_DOWN; 177 + sync_cache_w(&c->cluster); 178 + 179 + /* Back out if the inbound cluster is already in the critical region: */ 180 + sync_cache_r(&c->inbound); 181 + if (c->inbound == INBOUND_COMING_UP) 182 + goto abort; 183 + 184 + /* 185 + * Wait for all CPUs to get out of the GOING_DOWN state, so that local 186 + * teardown is complete on each CPU before tearing down the cluster. 187 + * 188 + * If any CPU has been woken up again from the DOWN state, then we 189 + * shouldn't be taking the cluster down at all: abort in that case. 190 + */ 191 + sync_cache_r(&c->cpus); 192 + for (i = 0; i < MAX_CPUS_PER_CLUSTER; i++) { 193 + int cpustate; 194 + 195 + if (i == cpu) 196 + continue; 197 + 198 + while (1) { 199 + cpustate = c->cpus[i].cpu; 200 + if (cpustate != CPU_GOING_DOWN) 201 + break; 202 + 203 + wfe(); 204 + sync_cache_r(&c->cpus[i].cpu); 205 + } 206 + 207 + switch (cpustate) { 208 + case CPU_DOWN: 209 + continue; 210 + 211 + default: 212 + goto abort; 213 + } 214 + } 215 + 216 + return true; 217 + 218 + abort: 219 + __mcpm_outbound_leave_critical(cluster, CLUSTER_UP); 220 + return false; 221 + } 222 + 223 + int __mcpm_cluster_state(unsigned int cluster) 224 + { 225 + sync_cache_r(&mcpm_sync.clusters[cluster].cluster); 226 + return mcpm_sync.clusters[cluster].cluster; 227 + } 228 + 229 + extern unsigned long mcpm_power_up_setup_phys; 230 + 231 + int __init mcpm_sync_init( 232 + void (*power_up_setup)(unsigned int affinity_level)) 233 + { 234 + unsigned int i, j, mpidr, this_cluster; 235 + 236 + BUILD_BUG_ON(MCPM_SYNC_CLUSTER_SIZE * MAX_NR_CLUSTERS != sizeof mcpm_sync); 237 + BUG_ON((unsigned long)&mcpm_sync & (__CACHE_WRITEBACK_GRANULE - 1)); 238 + 239 + /* 240 + * Set initial CPU and cluster states. 241 + * Only one cluster is assumed to be active at this point. 242 + */ 243 + for (i = 0; i < MAX_NR_CLUSTERS; i++) { 244 + mcpm_sync.clusters[i].cluster = CLUSTER_DOWN; 245 + mcpm_sync.clusters[i].inbound = INBOUND_NOT_COMING_UP; 246 + for (j = 0; j < MAX_CPUS_PER_CLUSTER; j++) 247 + mcpm_sync.clusters[i].cpus[j].cpu = CPU_DOWN; 248 + } 249 + mpidr = read_cpuid_mpidr(); 250 + this_cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1); 251 + for_each_online_cpu(i) 252 + mcpm_sync.clusters[this_cluster].cpus[i].cpu = CPU_UP; 253 + mcpm_sync.clusters[this_cluster].cluster = CLUSTER_UP; 254 + sync_cache_w(&mcpm_sync); 255 + 256 + if (power_up_setup) { 257 + mcpm_power_up_setup_phys = virt_to_phys(power_up_setup); 258 + sync_cache_w(&mcpm_power_up_setup_phys); 259 + } 260 + 113 261 return 0; 114 262 }
+104 -2
arch/arm/common/mcpm_head.S
··· 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License version 2 as 9 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 16 #include <linux/linkage.h> 13 17 #include <asm/mcpm.h> 18 + 19 + .if MCPM_SYNC_CLUSTER_CPUS 20 + .error "cpus must be the first member of struct mcpm_sync_struct" 21 + .endif 14 22 15 23 .macro pr_dbg string 16 24 #if defined(CONFIG_DEBUG_LL) && defined(DEBUG) ··· 65 57 2: pr_dbg "kernel mcpm_entry_point\n" 66 58 67 59 /* 68 - * MMU is off so we need to get to mcpm_entry_vectors in a 60 + * MMU is off so we need to get to various variables in a 69 61 * position independent way. 70 62 */ 71 63 adr r5, 3f 72 - ldr r6, [r5] 64 + ldmia r5, {r6, r7, r8} 73 65 add r6, r5, r6 @ r6 = mcpm_entry_vectors 66 + ldr r7, [r5, r7] @ r7 = mcpm_power_up_setup_phys 67 + add r8, r5, r8 @ r8 = mcpm_sync 68 + 69 + mov r0, #MCPM_SYNC_CLUSTER_SIZE 70 + mla r8, r0, r10, r8 @ r8 = sync cluster base 71 + 72 + @ Signal that this CPU is coming UP: 73 + mov r0, #CPU_COMING_UP 74 + mov r5, #MCPM_SYNC_CPU_SIZE 75 + mla r5, r9, r5, r8 @ r5 = sync cpu address 76 + strb r0, [r5] 77 + 78 + @ At this point, the cluster cannot unexpectedly enter the GOING_DOWN 79 + @ state, because there is at least one active CPU (this CPU). 80 + 81 + @ Note: the following is racy as another CPU might be testing 82 + @ the same flag at the same moment. That'll be fixed later. 83 + ldrb r0, [r8, #MCPM_SYNC_CLUSTER_CLUSTER] 84 + cmp r0, #CLUSTER_UP @ cluster already up? 85 + bne mcpm_setup @ if not, set up the cluster 86 + 87 + @ Otherwise, skip setup: 88 + b mcpm_setup_complete 89 + 90 + mcpm_setup: 91 + @ Control dependency implies strb not observable before previous ldrb. 92 + 93 + @ Signal that the cluster is being brought up: 94 + mov r0, #INBOUND_COMING_UP 95 + strb r0, [r8, #MCPM_SYNC_CLUSTER_INBOUND] 96 + dmb 97 + 98 + @ Any CPU trying to take the cluster into CLUSTER_GOING_DOWN from this 99 + @ point onwards will observe INBOUND_COMING_UP and abort. 100 + 101 + @ Wait for any previously-pending cluster teardown operations to abort 102 + @ or complete: 103 + mcpm_teardown_wait: 104 + ldrb r0, [r8, #MCPM_SYNC_CLUSTER_CLUSTER] 105 + cmp r0, #CLUSTER_GOING_DOWN 106 + bne first_man_setup 107 + wfe 108 + b mcpm_teardown_wait 109 + 110 + first_man_setup: 111 + dmb 112 + 113 + @ If the outbound gave up before teardown started, skip cluster setup: 114 + 115 + cmp r0, #CLUSTER_UP 116 + beq mcpm_setup_leave 117 + 118 + @ power_up_setup is now responsible for setting up the cluster: 119 + 120 + cmp r7, #0 121 + mov r0, #1 @ second (cluster) affinity level 122 + blxne r7 @ Call power_up_setup if defined 123 + dmb 124 + 125 + mov r0, #CLUSTER_UP 126 + strb r0, [r8, #MCPM_SYNC_CLUSTER_CLUSTER] 127 + dmb 128 + 129 + mcpm_setup_leave: 130 + @ Leave the cluster setup critical section: 131 + 132 + mov r0, #INBOUND_NOT_COMING_UP 133 + strb r0, [r8, #MCPM_SYNC_CLUSTER_INBOUND] 134 + dsb 135 + sev 136 + 137 + mcpm_setup_complete: 138 + @ If a platform-specific CPU setup hook is needed, it is 139 + @ called from here. 140 + 141 + cmp r7, #0 142 + mov r0, #0 @ first (CPU) affinity level 143 + blxne r7 @ Call power_up_setup if defined 144 + dmb 145 + 146 + @ Mark the CPU as up: 147 + 148 + mov r0, #CPU_UP 149 + strb r0, [r5] 150 + 151 + @ Observability order of CPU_UP and opening of the gate does not matter. 74 152 75 153 mcpm_entry_gated: 76 154 ldr r5, [r6, r4, lsl #2] @ r5 = CPU entry vector 77 155 cmp r5, #0 78 156 wfeeq 79 157 beq mcpm_entry_gated 158 + dmb 159 + 80 160 pr_dbg "released\n" 81 161 bx r5 82 162 83 163 .align 2 84 164 85 165 3: .word mcpm_entry_vectors - . 166 + .word mcpm_power_up_setup_phys - 3b 167 + .word mcpm_sync - 3b 86 168 87 169 ENDPROC(mcpm_entry_point) 88 170 ··· 182 84 .type mcpm_entry_vectors, #object 183 85 ENTRY(mcpm_entry_vectors) 184 86 .space 4 * MAX_NR_CLUSTERS * MAX_CPUS_PER_CLUSTER 87 + 88 + .type mcpm_power_up_setup_phys, #object 89 + ENTRY(mcpm_power_up_setup_phys) 90 + .space 4 @ set by mcpm_sync_init()
+73
arch/arm/include/asm/mcpm.h
··· 24 24 25 25 #ifndef __ASSEMBLY__ 26 26 27 + #include <linux/types.h> 28 + #include <asm/cacheflush.h> 29 + 27 30 /* 28 31 * Platform specific code should use this symbol to set up secondary 29 32 * entry location for processors to use when released from reset. ··· 133 130 */ 134 131 int __init mcpm_platform_register(const struct mcpm_platform_ops *ops); 135 132 133 + /* Synchronisation structures for coordinating safe cluster setup/teardown: */ 134 + 135 + /* 136 + * When modifying this structure, make sure you update the MCPM_SYNC_ defines 137 + * to match. 138 + */ 139 + struct mcpm_sync_struct { 140 + /* individual CPU states */ 141 + struct { 142 + s8 cpu __aligned(__CACHE_WRITEBACK_GRANULE); 143 + } cpus[MAX_CPUS_PER_CLUSTER]; 144 + 145 + /* cluster state */ 146 + s8 cluster __aligned(__CACHE_WRITEBACK_GRANULE); 147 + 148 + /* inbound-side state */ 149 + s8 inbound __aligned(__CACHE_WRITEBACK_GRANULE); 150 + }; 151 + 152 + struct sync_struct { 153 + struct mcpm_sync_struct clusters[MAX_NR_CLUSTERS]; 154 + }; 155 + 156 + extern unsigned long sync_phys; /* physical address of *mcpm_sync */ 157 + 158 + void __mcpm_cpu_going_down(unsigned int cpu, unsigned int cluster); 159 + void __mcpm_cpu_down(unsigned int cpu, unsigned int cluster); 160 + void __mcpm_outbound_leave_critical(unsigned int cluster, int state); 161 + bool __mcpm_outbound_enter_critical(unsigned int this_cpu, unsigned int cluster); 162 + int __mcpm_cluster_state(unsigned int cluster); 163 + 164 + int __init mcpm_sync_init( 165 + void (*power_up_setup)(unsigned int affinity_level)); 166 + 167 + #else 168 + 169 + /* 170 + * asm-offsets.h causes trouble when included in .c files, and cacheflush.h 171 + * cannot be included in asm files. Let's work around the conflict like this. 172 + */ 173 + #include <asm/asm-offsets.h> 174 + #define __CACHE_WRITEBACK_GRANULE CACHE_WRITEBACK_GRANULE 175 + 136 176 #endif /* ! __ASSEMBLY__ */ 177 + 178 + /* Definitions for mcpm_sync_struct */ 179 + #define CPU_DOWN 0x11 180 + #define CPU_COMING_UP 0x12 181 + #define CPU_UP 0x13 182 + #define CPU_GOING_DOWN 0x14 183 + 184 + #define CLUSTER_DOWN 0x21 185 + #define CLUSTER_UP 0x22 186 + #define CLUSTER_GOING_DOWN 0x23 187 + 188 + #define INBOUND_NOT_COMING_UP 0x31 189 + #define INBOUND_COMING_UP 0x32 190 + 191 + /* 192 + * Offsets for the mcpm_sync_struct members, for use in asm. 193 + * We don't want to make them global to the kernel via asm-offsets.c. 194 + */ 195 + #define MCPM_SYNC_CLUSTER_CPUS 0 196 + #define MCPM_SYNC_CPU_SIZE __CACHE_WRITEBACK_GRANULE 197 + #define MCPM_SYNC_CLUSTER_CLUSTER \ 198 + (MCPM_SYNC_CLUSTER_CPUS + MCPM_SYNC_CPU_SIZE * MAX_CPUS_PER_CLUSTER) 199 + #define MCPM_SYNC_CLUSTER_INBOUND \ 200 + (MCPM_SYNC_CLUSTER_CLUSTER + __CACHE_WRITEBACK_GRANULE) 201 + #define MCPM_SYNC_CLUSTER_SIZE \ 202 + (MCPM_SYNC_CLUSTER_INBOUND + __CACHE_WRITEBACK_GRANULE) 203 + 137 204 #endif
+3
arch/arm/kernel/asm-offsets.c
··· 149 149 DEFINE(DMA_BIDIRECTIONAL, DMA_BIDIRECTIONAL); 150 150 DEFINE(DMA_TO_DEVICE, DMA_TO_DEVICE); 151 151 DEFINE(DMA_FROM_DEVICE, DMA_FROM_DEVICE); 152 + BLANK(); 153 + DEFINE(CACHE_WRITEBACK_GRANULE, __CACHE_WRITEBACK_GRANULE); 154 + BLANK(); 152 155 #ifdef CONFIG_KVM_ARM_HOST 153 156 DEFINE(VCPU_KVM, offsetof(struct kvm_vcpu, kvm)); 154 157 DEFINE(VCPU_MIDR, offsetof(struct kvm_vcpu, arch.midr));