blob: 1b1b82b37ce035f97e17e40be6d81d913ee27e61 [file] [log] [blame]
Nicolas Pitree8db2882012-04-12 02:45:22 -04001/*
2 * arch/arm/common/mcpm_entry.c -- entry point for multi-cluster PM
3 *
4 * Created by: Nicolas Pitre, March 2012
5 * Copyright: (C) 2012-2013 Linaro Limited
6 *
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
9 * published by the Free Software Foundation.
10 */
11
Arnd Bergmann73acc032018-05-28 17:44:36 +020012#include <linux/export.h>
Nicolas Pitre7c2b8602012-09-20 16:05:37 -040013#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/irqflags.h>
Nicolas Pitre37219242014-06-24 18:32:51 +010016#include <linux/cpu_pm.h>
Nicolas Pitre7c2b8602012-09-20 16:05:37 -040017
Nicolas Pitree8db2882012-04-12 02:45:22 -040018#include <asm/mcpm.h>
19#include <asm/cacheflush.h>
Nicolas Pitre7c2b8602012-09-20 16:05:37 -040020#include <asm/idmap.h>
Dave Martin7fe31d22012-07-17 14:25:42 +010021#include <asm/cputype.h>
Nicolas Pitre37219242014-06-24 18:32:51 +010022#include <asm/suspend.h>
Nicolas Pitree8db2882012-04-12 02:45:22 -040023
Nicolas Pitre1c2c7d52015-04-28 14:51:36 -040024/*
25 * The public API for this code is documented in arch/arm/include/asm/mcpm.h.
26 * For a comprehensive description of the main algorithm used here, please
27 * see Documentation/arm/cluster-pm-race-avoidance.txt.
28 */
Nicolas Pitre7cc8b992015-04-28 14:11:07 -040029
30struct sync_struct mcpm_sync;
31
32/*
33 * __mcpm_cpu_going_down: Indicates that the cpu is being torn down.
34 * This must be called at the point of committing to teardown of a CPU.
35 * The CPU cache (SCTRL.C bit) is expected to still be active.
36 */
37static void __mcpm_cpu_going_down(unsigned int cpu, unsigned int cluster)
38{
39 mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_GOING_DOWN;
40 sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
41}
42
43/*
44 * __mcpm_cpu_down: Indicates that cpu teardown is complete and that the
45 * cluster can be torn down without disrupting this CPU.
46 * To avoid deadlocks, this must be called before a CPU is powered down.
47 * The CPU cache (SCTRL.C bit) is expected to be off.
48 * However L2 cache might or might not be active.
49 */
50static void __mcpm_cpu_down(unsigned int cpu, unsigned int cluster)
51{
52 dmb();
53 mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_DOWN;
54 sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
55 sev();
56}
57
58/*
59 * __mcpm_outbound_leave_critical: Leave the cluster teardown critical section.
60 * @state: the final state of the cluster:
61 * CLUSTER_UP: no destructive teardown was done and the cluster has been
62 * restored to the previous state (CPU cache still active); or
63 * CLUSTER_DOWN: the cluster has been torn-down, ready for power-off
64 * (CPU cache disabled, L2 cache either enabled or disabled).
65 */
66static void __mcpm_outbound_leave_critical(unsigned int cluster, int state)
67{
68 dmb();
69 mcpm_sync.clusters[cluster].cluster = state;
70 sync_cache_w(&mcpm_sync.clusters[cluster].cluster);
71 sev();
72}
73
74/*
75 * __mcpm_outbound_enter_critical: Enter the cluster teardown critical section.
76 * This function should be called by the last man, after local CPU teardown
77 * is complete. CPU cache expected to be active.
78 *
79 * Returns:
80 * false: the critical section was not entered because an inbound CPU was
81 * observed, or the cluster is already being set up;
82 * true: the critical section was entered: it is now safe to tear down the
83 * cluster.
84 */
85static bool __mcpm_outbound_enter_critical(unsigned int cpu, unsigned int cluster)
86{
87 unsigned int i;
88 struct mcpm_sync_struct *c = &mcpm_sync.clusters[cluster];
89
90 /* Warn inbound CPUs that the cluster is being torn down: */
91 c->cluster = CLUSTER_GOING_DOWN;
92 sync_cache_w(&c->cluster);
93
94 /* Back out if the inbound cluster is already in the critical region: */
95 sync_cache_r(&c->inbound);
96 if (c->inbound == INBOUND_COMING_UP)
97 goto abort;
98
99 /*
100 * Wait for all CPUs to get out of the GOING_DOWN state, so that local
101 * teardown is complete on each CPU before tearing down the cluster.
102 *
103 * If any CPU has been woken up again from the DOWN state, then we
104 * shouldn't be taking the cluster down at all: abort in that case.
105 */
106 sync_cache_r(&c->cpus);
107 for (i = 0; i < MAX_CPUS_PER_CLUSTER; i++) {
108 int cpustate;
109
110 if (i == cpu)
111 continue;
112
113 while (1) {
114 cpustate = c->cpus[i].cpu;
115 if (cpustate != CPU_GOING_DOWN)
116 break;
117
118 wfe();
119 sync_cache_r(&c->cpus[i].cpu);
120 }
121
122 switch (cpustate) {
123 case CPU_DOWN:
124 continue;
125
126 default:
127 goto abort;
128 }
129 }
130
131 return true;
132
133abort:
134 __mcpm_outbound_leave_critical(cluster, CLUSTER_UP);
135 return false;
136}
137
138static int __mcpm_cluster_state(unsigned int cluster)
139{
140 sync_cache_r(&mcpm_sync.clusters[cluster].cluster);
141 return mcpm_sync.clusters[cluster].cluster;
142}
143
Nicolas Pitree8db2882012-04-12 02:45:22 -0400144extern unsigned long mcpm_entry_vectors[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER];
145
146void mcpm_set_entry_vector(unsigned cpu, unsigned cluster, void *ptr)
147{
Florian Fainelli64fc2a92017-01-15 03:59:29 +0100148 unsigned long val = ptr ? __pa_symbol(ptr) : 0;
Nicolas Pitree8db2882012-04-12 02:45:22 -0400149 mcpm_entry_vectors[cluster][cpu] = val;
150 sync_cache_w(&mcpm_entry_vectors[cluster][cpu]);
151}
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400152
Nicolas Pitrede885d12012-11-27 23:11:20 -0500153extern unsigned long mcpm_entry_early_pokes[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER][2];
154
155void mcpm_set_early_poke(unsigned cpu, unsigned cluster,
156 unsigned long poke_phys_addr, unsigned long poke_val)
157{
158 unsigned long *poke = &mcpm_entry_early_pokes[cluster][cpu][0];
159 poke[0] = poke_phys_addr;
160 poke[1] = poke_val;
Nicolas Pitreefcfc462013-12-09 16:10:18 +0100161 __sync_cache_range_w(poke, 2 * sizeof(*poke));
Nicolas Pitrede885d12012-11-27 23:11:20 -0500162}
163
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400164static const struct mcpm_platform_ops *platform_ops;
165
166int __init mcpm_platform_register(const struct mcpm_platform_ops *ops)
167{
168 if (platform_ops)
169 return -EBUSY;
170 platform_ops = ops;
171 return 0;
172}
173
Nicolas Pitre4530e4b2014-04-22 00:25:35 +0100174bool mcpm_is_available(void)
175{
176 return (platform_ops) ? true : false;
177}
Arnd Bergmann73acc032018-05-28 17:44:36 +0200178EXPORT_SYMBOL_GPL(mcpm_is_available);
Nicolas Pitre4530e4b2014-04-22 00:25:35 +0100179
Nicolas Pitred3a87542015-03-11 18:16:13 -0400180/*
181 * We can't use regular spinlocks. In the switcher case, it is possible
182 * for an outbound CPU to call power_down() after its inbound counterpart
183 * is already live using the same logical CPU number which trips lockdep
184 * debugging.
185 */
186static arch_spinlock_t mcpm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
187
188static int mcpm_cpu_use_count[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER];
189
190static inline bool mcpm_cluster_unused(unsigned int cluster)
191{
192 int i, cnt;
193 for (i = 0, cnt = 0; i < MAX_CPUS_PER_CLUSTER; i++)
194 cnt |= mcpm_cpu_use_count[cluster][i];
195 return !cnt;
196}
197
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400198int mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster)
199{
Nicolas Pitred3a87542015-03-11 18:16:13 -0400200 bool cpu_is_down, cluster_is_down;
201 int ret = 0;
202
Nicolas Pitre77404d82015-04-28 13:44:00 -0400203 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400204 if (!platform_ops)
205 return -EUNATCH; /* try not to shadow power_up errors */
206 might_sleep();
Nicolas Pitred3a87542015-03-11 18:16:13 -0400207
Nicolas Pitred3a87542015-03-11 18:16:13 -0400208 /*
209 * Since this is called with IRQs enabled, and no arch_spin_lock_irq
210 * variant exists, we need to disable IRQs manually here.
211 */
212 local_irq_disable();
213 arch_spin_lock(&mcpm_lock);
214
215 cpu_is_down = !mcpm_cpu_use_count[cluster][cpu];
216 cluster_is_down = mcpm_cluster_unused(cluster);
217
218 mcpm_cpu_use_count[cluster][cpu]++;
219 /*
220 * The only possible values are:
221 * 0 = CPU down
222 * 1 = CPU (still) up
223 * 2 = CPU requested to be up before it had a chance
224 * to actually make itself down.
225 * Any other value is a bug.
226 */
227 BUG_ON(mcpm_cpu_use_count[cluster][cpu] != 1 &&
228 mcpm_cpu_use_count[cluster][cpu] != 2);
229
230 if (cluster_is_down)
231 ret = platform_ops->cluster_powerup(cluster);
232 if (cpu_is_down && !ret)
233 ret = platform_ops->cpu_powerup(cpu, cluster);
234
235 arch_spin_unlock(&mcpm_lock);
236 local_irq_enable();
237 return ret;
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400238}
239
Sudeep Holla29226b12017-05-18 17:09:59 +0100240typedef typeof(cpu_reset) phys_reset_t;
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400241
242void mcpm_cpu_power_down(void)
243{
Nicolas Pitred3a87542015-03-11 18:16:13 -0400244 unsigned int mpidr, cpu, cluster;
245 bool cpu_going_down, last_man;
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400246 phys_reset_t phys_reset;
247
Nicolas Pitred3a87542015-03-11 18:16:13 -0400248 mpidr = read_cpuid_mpidr();
249 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
250 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
251 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
Nicolas Pitre77404d82015-04-28 13:44:00 -0400252 if (WARN_ON_ONCE(!platform_ops))
253 return;
254 BUG_ON(!irqs_disabled());
255
256 setup_mm_for_reboot();
Nicolas Pitred3a87542015-03-11 18:16:13 -0400257
258 __mcpm_cpu_going_down(cpu, cluster);
Nicolas Pitred3a87542015-03-11 18:16:13 -0400259 arch_spin_lock(&mcpm_lock);
260 BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
261
262 mcpm_cpu_use_count[cluster][cpu]--;
263 BUG_ON(mcpm_cpu_use_count[cluster][cpu] != 0 &&
264 mcpm_cpu_use_count[cluster][cpu] != 1);
265 cpu_going_down = !mcpm_cpu_use_count[cluster][cpu];
266 last_man = mcpm_cluster_unused(cluster);
267
268 if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
269 platform_ops->cpu_powerdown_prepare(cpu, cluster);
270 platform_ops->cluster_powerdown_prepare(cluster);
271 arch_spin_unlock(&mcpm_lock);
272 platform_ops->cluster_cache_disable();
273 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
274 } else {
275 if (cpu_going_down)
276 platform_ops->cpu_powerdown_prepare(cpu, cluster);
277 arch_spin_unlock(&mcpm_lock);
278 /*
279 * If cpu_going_down is false here, that means a power_up
280 * request raced ahead of us. Even if we do not want to
281 * shut this CPU down, the caller still expects execution
282 * to return through the system resume entry path, like
283 * when the WFI is aborted due to a new IRQ or the like..
284 * So let's continue with cache cleaning in all cases.
285 */
286 platform_ops->cpu_cache_disable();
287 }
288
289 __mcpm_cpu_down(cpu, cluster);
290
291 /* Now we are prepared for power-down, do it: */
292 if (cpu_going_down)
293 wfi();
294
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400295 /*
296 * It is possible for a power_up request to happen concurrently
297 * with a power_down request for the same CPU. In this case the
Nicolas Pitred3a87542015-03-11 18:16:13 -0400298 * CPU might not be able to actually enter a powered down state
299 * with the WFI instruction if the power_up request has removed
300 * the required reset condition. We must perform a re-entry in
301 * the kernel as if the power_up method just had deasserted reset
302 * on the CPU.
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400303 */
Florian Fainelli64fc2a92017-01-15 03:59:29 +0100304 phys_reset = (phys_reset_t)(unsigned long)__pa_symbol(cpu_reset);
Sudeep Holla29226b12017-05-18 17:09:59 +0100305 phys_reset(__pa_symbol(mcpm_entry_point), false);
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400306
307 /* should never get here */
308 BUG();
309}
310
Dave Martin166aaf32014-04-17 16:58:39 +0100311int mcpm_wait_for_cpu_powerdown(unsigned int cpu, unsigned int cluster)
Dave Martin0de0d642013-10-01 19:58:17 +0100312{
313 int ret;
314
Dave Martin166aaf32014-04-17 16:58:39 +0100315 if (WARN_ON_ONCE(!platform_ops || !platform_ops->wait_for_powerdown))
Dave Martin0de0d642013-10-01 19:58:17 +0100316 return -EUNATCH;
317
Dave Martin166aaf32014-04-17 16:58:39 +0100318 ret = platform_ops->wait_for_powerdown(cpu, cluster);
Dave Martin0de0d642013-10-01 19:58:17 +0100319 if (ret)
320 pr_warn("%s: cpu %u, cluster %u failed to power down (%d)\n",
321 __func__, cpu, cluster, ret);
322
323 return ret;
324}
325
Nicolas Pitre7895f732015-04-28 15:51:19 -0400326void mcpm_cpu_suspend(void)
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400327{
Nicolas Pitred3a87542015-03-11 18:16:13 -0400328 if (WARN_ON_ONCE(!platform_ops))
Nicolas Pitred0cdef62013-09-25 23:26:24 +0100329 return;
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400330
Nicolas Pitred3a87542015-03-11 18:16:13 -0400331 /* Some platforms might have to enable special resume modes, etc. */
332 if (platform_ops->cpu_suspend_prepare) {
333 unsigned int mpidr = read_cpuid_mpidr();
334 unsigned int cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
335 unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
336 arch_spin_lock(&mcpm_lock);
337 platform_ops->cpu_suspend_prepare(cpu, cluster);
338 arch_spin_unlock(&mcpm_lock);
339 }
340 mcpm_cpu_power_down();
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400341}
342
343int mcpm_cpu_powered_up(void)
344{
Nicolas Pitred3a87542015-03-11 18:16:13 -0400345 unsigned int mpidr, cpu, cluster;
346 bool cpu_was_down, first_man;
347 unsigned long flags;
348
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400349 if (!platform_ops)
350 return -EUNATCH;
Nicolas Pitred3a87542015-03-11 18:16:13 -0400351
Nicolas Pitred3a87542015-03-11 18:16:13 -0400352 mpidr = read_cpuid_mpidr();
353 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
354 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
355 local_irq_save(flags);
356 arch_spin_lock(&mcpm_lock);
357
358 cpu_was_down = !mcpm_cpu_use_count[cluster][cpu];
359 first_man = mcpm_cluster_unused(cluster);
360
361 if (first_man && platform_ops->cluster_is_up)
362 platform_ops->cluster_is_up(cluster);
363 if (cpu_was_down)
364 mcpm_cpu_use_count[cluster][cpu] = 1;
365 if (platform_ops->cpu_is_up)
366 platform_ops->cpu_is_up(cpu, cluster);
367
368 arch_spin_unlock(&mcpm_lock);
369 local_irq_restore(flags);
370
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400371 return 0;
372}
Dave Martin7fe31d22012-07-17 14:25:42 +0100373
Nicolas Pitre37219242014-06-24 18:32:51 +0100374#ifdef CONFIG_ARM_CPU_SUSPEND
375
376static int __init nocache_trampoline(unsigned long _arg)
377{
378 void (*cache_disable)(void) = (void *)_arg;
379 unsigned int mpidr = read_cpuid_mpidr();
380 unsigned int cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
381 unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
382 phys_reset_t phys_reset;
383
Marek Szyprowskica70ea42019-02-18 09:31:41 +0100384 mcpm_set_entry_vector(cpu, cluster, cpu_resume_no_hyp);
Nicolas Pitre37219242014-06-24 18:32:51 +0100385 setup_mm_for_reboot();
386
387 __mcpm_cpu_going_down(cpu, cluster);
388 BUG_ON(!__mcpm_outbound_enter_critical(cpu, cluster));
389 cache_disable();
390 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
391 __mcpm_cpu_down(cpu, cluster);
392
Florian Fainelli64fc2a92017-01-15 03:59:29 +0100393 phys_reset = (phys_reset_t)(unsigned long)__pa_symbol(cpu_reset);
Sudeep Holla29226b12017-05-18 17:09:59 +0100394 phys_reset(__pa_symbol(mcpm_entry_point), false);
Nicolas Pitre37219242014-06-24 18:32:51 +0100395 BUG();
396}
397
398int __init mcpm_loopback(void (*cache_disable)(void))
399{
400 int ret;
401
402 /*
403 * We're going to soft-restart the current CPU through the
404 * low-level MCPM code by leveraging the suspend/resume
405 * infrastructure. Let's play it safe by using cpu_pm_enter()
406 * in case the CPU init code path resets the VFP or similar.
407 */
408 local_irq_disable();
409 local_fiq_disable();
410 ret = cpu_pm_enter();
411 if (!ret) {
412 ret = cpu_suspend((unsigned long)cache_disable, nocache_trampoline);
413 cpu_pm_exit();
414 }
415 local_fiq_enable();
416 local_irq_enable();
417 if (ret)
418 pr_err("%s returned %d\n", __func__, ret);
419 return ret;
420}
421
422#endif
423
Dave Martin7fe31d22012-07-17 14:25:42 +0100424extern unsigned long mcpm_power_up_setup_phys;
425
426int __init mcpm_sync_init(
427 void (*power_up_setup)(unsigned int affinity_level))
428{
429 unsigned int i, j, mpidr, this_cluster;
430
431 BUILD_BUG_ON(MCPM_SYNC_CLUSTER_SIZE * MAX_NR_CLUSTERS != sizeof mcpm_sync);
432 BUG_ON((unsigned long)&mcpm_sync & (__CACHE_WRITEBACK_GRANULE - 1));
433
434 /*
435 * Set initial CPU and cluster states.
436 * Only one cluster is assumed to be active at this point.
437 */
438 for (i = 0; i < MAX_NR_CLUSTERS; i++) {
439 mcpm_sync.clusters[i].cluster = CLUSTER_DOWN;
440 mcpm_sync.clusters[i].inbound = INBOUND_NOT_COMING_UP;
441 for (j = 0; j < MAX_CPUS_PER_CLUSTER; j++)
442 mcpm_sync.clusters[i].cpus[j].cpu = CPU_DOWN;
443 }
444 mpidr = read_cpuid_mpidr();
445 this_cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
Nicolas Pitred3a87542015-03-11 18:16:13 -0400446 for_each_online_cpu(i) {
447 mcpm_cpu_use_count[this_cluster][i] = 1;
Dave Martin7fe31d22012-07-17 14:25:42 +0100448 mcpm_sync.clusters[this_cluster].cpus[i].cpu = CPU_UP;
Nicolas Pitred3a87542015-03-11 18:16:13 -0400449 }
Dave Martin7fe31d22012-07-17 14:25:42 +0100450 mcpm_sync.clusters[this_cluster].cluster = CLUSTER_UP;
451 sync_cache_w(&mcpm_sync);
452
453 if (power_up_setup) {
Florian Fainelli64fc2a92017-01-15 03:59:29 +0100454 mcpm_power_up_setup_phys = __pa_symbol(power_up_setup);
Dave Martin7fe31d22012-07-17 14:25:42 +0100455 sync_cache_w(&mcpm_power_up_setup_phys);
456 }
457
458 return 0;
459}