blob: d38130aba4644f206baab0e8d8f9a4b0e3ab88ee [file] [log] [blame]
Nicolas Pitre11b277e2013-08-06 19:10:08 +01001/*
2 * arch/arm/mach-vexpress/tc2_pm.c - TC2 power management support
3 *
4 * Created by: Nicolas Pitre, October 2012
5 * Copyright: (C) 2012-2013 Linaro Limited
6 *
7 * Some portions of this file were originally written by Achin Gupta
8 * Copyright: (C) 2012 ARM Limited
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/init.h>
16#include <linux/io.h>
17#include <linux/kernel.h>
18#include <linux/of_address.h>
Sudeep KarkadaNageshaf7cd2d82013-10-29 12:18:37 +000019#include <linux/of_irq.h>
Nicolas Pitre11b277e2013-08-06 19:10:08 +010020#include <linux/spinlock.h>
21#include <linux/errno.h>
Lorenzo Pieralisi9ee2ee02013-07-24 12:05:01 +010022#include <linux/irqchip/arm-gic.h>
Nicolas Pitre11b277e2013-08-06 19:10:08 +010023
24#include <asm/mcpm.h>
25#include <asm/proc-fns.h>
26#include <asm/cacheflush.h>
27#include <asm/cputype.h>
28#include <asm/cp15.h>
29
30#include <linux/arm-cci.h>
31
32#include "spc.h"
33
34/* SCC conf registers */
35#define A15_CONF 0x400
36#define A7_CONF 0x500
37#define SYS_INFO 0x700
38#define SPC_BASE 0xb00
39
40/*
41 * We can't use regular spinlocks. In the switcher case, it is possible
42 * for an outbound CPU to call power_down() after its inbound counterpart
43 * is already live using the same logical CPU number which trips lockdep
44 * debugging.
45 */
46static arch_spinlock_t tc2_pm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
47
48#define TC2_CLUSTERS 2
49#define TC2_MAX_CPUS_PER_CLUSTER 3
50
51static unsigned int tc2_nr_cpus[TC2_CLUSTERS];
52
53/* Keep per-cpu usage count to cope with unordered up/down requests */
54static int tc2_pm_use_count[TC2_MAX_CPUS_PER_CLUSTER][TC2_CLUSTERS];
55
56#define tc2_cluster_unused(cluster) \
57 (!tc2_pm_use_count[0][cluster] && \
58 !tc2_pm_use_count[1][cluster] && \
59 !tc2_pm_use_count[2][cluster])
60
61static int tc2_pm_power_up(unsigned int cpu, unsigned int cluster)
62{
63 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
64 if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster])
65 return -EINVAL;
66
67 /*
68 * Since this is called with IRQs enabled, and no arch_spin_lock_irq
69 * variant exists, we need to disable IRQs manually here.
70 */
71 local_irq_disable();
72 arch_spin_lock(&tc2_pm_lock);
73
74 if (tc2_cluster_unused(cluster))
75 ve_spc_powerdown(cluster, false);
76
77 tc2_pm_use_count[cpu][cluster]++;
78 if (tc2_pm_use_count[cpu][cluster] == 1) {
79 ve_spc_set_resume_addr(cluster, cpu,
80 virt_to_phys(mcpm_entry_point));
81 ve_spc_cpu_wakeup_irq(cluster, cpu, true);
82 } else if (tc2_pm_use_count[cpu][cluster] != 2) {
83 /*
84 * The only possible values are:
85 * 0 = CPU down
86 * 1 = CPU (still) up
87 * 2 = CPU requested to be up before it had a chance
88 * to actually make itself down.
89 * Any other value is a bug.
90 */
91 BUG();
92 }
93
94 arch_spin_unlock(&tc2_pm_lock);
95 local_irq_enable();
96
97 return 0;
98}
99
Nicolas Pitree607b0f2012-12-10 00:22:06 -0500100static void tc2_pm_down(u64 residency)
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100101{
102 unsigned int mpidr, cpu, cluster;
103 bool last_man = false, skip_wfi = false;
104
105 mpidr = read_cpuid_mpidr();
106 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
107 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
108
109 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
110 BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
111
112 __mcpm_cpu_going_down(cpu, cluster);
113
114 arch_spin_lock(&tc2_pm_lock);
115 BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
116 tc2_pm_use_count[cpu][cluster]--;
117 if (tc2_pm_use_count[cpu][cluster] == 0) {
118 ve_spc_cpu_wakeup_irq(cluster, cpu, true);
119 if (tc2_cluster_unused(cluster)) {
120 ve_spc_powerdown(cluster, true);
121 ve_spc_global_wakeup_irq(true);
122 last_man = true;
123 }
124 } else if (tc2_pm_use_count[cpu][cluster] == 1) {
125 /*
126 * A power_up request went ahead of us.
127 * Even if we do not want to shut this CPU down,
128 * the caller expects a certain state as if the WFI
129 * was aborted. So let's continue with cache cleaning.
130 */
131 skip_wfi = true;
132 } else
133 BUG();
134
Lorenzo Pieralisi64270d82013-09-27 16:54:42 +0100135 /*
136 * If the CPU is committed to power down, make sure
137 * the power controller will be in charge of waking it
138 * up upon IRQ, ie IRQ lines are cut from GIC CPU IF
139 * to the CPU by disabling the GIC CPU IF to prevent wfi
140 * from completing execution behind power controller back
141 */
142 if (!skip_wfi)
143 gic_cpu_if_down();
144
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100145 if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
146 arch_spin_unlock(&tc2_pm_lock);
147
148 if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A15) {
149 /*
150 * On the Cortex-A15 we need to disable
151 * L2 prefetching before flushing the cache.
152 */
153 asm volatile(
154 "mcr p15, 1, %0, c15, c0, 3 \n\t"
155 "isb \n\t"
156 "dsb "
157 : : "r" (0x400) );
158 }
159
160 /*
161 * We need to disable and flush the whole (L1 and L2) cache.
162 * Let's do it in the safest possible way i.e. with
163 * no memory access within the following sequence
164 * including the stack.
Nicolas Pitrefac2e5772013-08-14 10:25:14 -0400165 *
166 * Note: fp is preserved to the stack explicitly prior doing
167 * this since adding it to the clobber list is incompatible
168 * with having CONFIG_FRAME_POINTER=y.
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100169 */
170 asm volatile(
Nicolas Pitrefac2e5772013-08-14 10:25:14 -0400171 "str fp, [sp, #-4]! \n\t"
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100172 "mrc p15, 0, r0, c1, c0, 0 @ get CR \n\t"
173 "bic r0, r0, #"__stringify(CR_C)" \n\t"
174 "mcr p15, 0, r0, c1, c0, 0 @ set CR \n\t"
175 "isb \n\t"
176 "bl v7_flush_dcache_all \n\t"
177 "clrex \n\t"
178 "mrc p15, 0, r0, c1, c0, 1 @ get AUXCR \n\t"
179 "bic r0, r0, #(1 << 6) @ disable local coherency \n\t"
180 "mcr p15, 0, r0, c1, c0, 1 @ set AUXCR \n\t"
181 "isb \n\t"
Nicolas Pitrefac2e5772013-08-14 10:25:14 -0400182 "dsb \n\t"
183 "ldr fp, [sp], #4"
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100184 : : : "r0","r1","r2","r3","r4","r5","r6","r7",
Nicolas Pitrefac2e5772013-08-14 10:25:14 -0400185 "r9","r10","lr","memory");
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100186
187 cci_disable_port_by_cpu(mpidr);
188
189 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
190 } else {
191 /*
192 * If last man then undo any setup done previously.
193 */
194 if (last_man) {
195 ve_spc_powerdown(cluster, false);
196 ve_spc_global_wakeup_irq(false);
197 }
198
199 arch_spin_unlock(&tc2_pm_lock);
200
201 /*
202 * We need to disable and flush only the L1 cache.
203 * Let's do it in the safest possible way as above.
204 */
205 asm volatile(
Nicolas Pitrefac2e5772013-08-14 10:25:14 -0400206 "str fp, [sp, #-4]! \n\t"
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100207 "mrc p15, 0, r0, c1, c0, 0 @ get CR \n\t"
208 "bic r0, r0, #"__stringify(CR_C)" \n\t"
209 "mcr p15, 0, r0, c1, c0, 0 @ set CR \n\t"
210 "isb \n\t"
211 "bl v7_flush_dcache_louis \n\t"
212 "clrex \n\t"
213 "mrc p15, 0, r0, c1, c0, 1 @ get AUXCR \n\t"
214 "bic r0, r0, #(1 << 6) @ disable local coherency \n\t"
215 "mcr p15, 0, r0, c1, c0, 1 @ set AUXCR \n\t"
216 "isb \n\t"
Nicolas Pitrefac2e5772013-08-14 10:25:14 -0400217 "dsb \n\t"
218 "ldr fp, [sp], #4"
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100219 : : : "r0","r1","r2","r3","r4","r5","r6","r7",
Nicolas Pitrefac2e5772013-08-14 10:25:14 -0400220 "r9","r10","lr","memory");
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100221 }
222
223 __mcpm_cpu_down(cpu, cluster);
224
225 /* Now we are prepared for power-down, do it: */
226 if (!skip_wfi)
227 wfi();
228
229 /* Not dead at this point? Let our caller cope. */
230}
231
Nicolas Pitree607b0f2012-12-10 00:22:06 -0500232static void tc2_pm_power_down(void)
233{
234 tc2_pm_down(0);
235}
236
237static void tc2_pm_suspend(u64 residency)
238{
239 unsigned int mpidr, cpu, cluster;
240
241 mpidr = read_cpuid_mpidr();
242 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
243 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
244 ve_spc_set_resume_addr(cluster, cpu, virt_to_phys(mcpm_entry_point));
245 tc2_pm_down(residency);
246}
247
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100248static void tc2_pm_powered_up(void)
249{
250 unsigned int mpidr, cpu, cluster;
251 unsigned long flags;
252
253 mpidr = read_cpuid_mpidr();
254 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
255 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
256
257 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
258 BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
259
260 local_irq_save(flags);
261 arch_spin_lock(&tc2_pm_lock);
262
263 if (tc2_cluster_unused(cluster)) {
264 ve_spc_powerdown(cluster, false);
265 ve_spc_global_wakeup_irq(false);
266 }
267
268 if (!tc2_pm_use_count[cpu][cluster])
269 tc2_pm_use_count[cpu][cluster] = 1;
270
271 ve_spc_cpu_wakeup_irq(cluster, cpu, false);
272 ve_spc_set_resume_addr(cluster, cpu, 0);
273
274 arch_spin_unlock(&tc2_pm_lock);
275 local_irq_restore(flags);
276}
277
278static const struct mcpm_platform_ops tc2_pm_power_ops = {
279 .power_up = tc2_pm_power_up,
280 .power_down = tc2_pm_power_down,
Nicolas Pitree607b0f2012-12-10 00:22:06 -0500281 .suspend = tc2_pm_suspend,
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100282 .powered_up = tc2_pm_powered_up,
283};
284
285static bool __init tc2_pm_usage_count_init(void)
286{
287 unsigned int mpidr, cpu, cluster;
288
289 mpidr = read_cpuid_mpidr();
290 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
291 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
292
293 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
294 if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster]) {
295 pr_err("%s: boot CPU is out of bound!\n", __func__);
296 return false;
297 }
298 tc2_pm_use_count[cpu][cluster] = 1;
299 return true;
300}
301
302/*
303 * Enable cluster-level coherency, in preparation for turning on the MMU.
304 */
305static void __naked tc2_pm_power_up_setup(unsigned int affinity_level)
306{
307 asm volatile (" \n"
308" cmp r0, #1 \n"
309" bxne lr \n"
310" b cci_enable_port_for_self ");
311}
312
313static int __init tc2_pm_init(void)
314{
Sudeep KarkadaNageshaf7cd2d82013-10-29 12:18:37 +0000315 int ret, irq;
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100316 void __iomem *scc;
317 u32 a15_cluster_id, a7_cluster_id, sys_info;
318 struct device_node *np;
319
320 /*
321 * The power management-related features are hidden behind
322 * SCC registers. We need to extract runtime information like
323 * cluster ids and number of CPUs really available in clusters.
324 */
325 np = of_find_compatible_node(NULL, NULL,
326 "arm,vexpress-scc,v2p-ca15_a7");
327 scc = of_iomap(np, 0);
328 if (!scc)
329 return -ENODEV;
330
331 a15_cluster_id = readl_relaxed(scc + A15_CONF) & 0xf;
332 a7_cluster_id = readl_relaxed(scc + A7_CONF) & 0xf;
333 if (a15_cluster_id >= TC2_CLUSTERS || a7_cluster_id >= TC2_CLUSTERS)
334 return -EINVAL;
335
336 sys_info = readl_relaxed(scc + SYS_INFO);
337 tc2_nr_cpus[a15_cluster_id] = (sys_info >> 16) & 0xf;
338 tc2_nr_cpus[a7_cluster_id] = (sys_info >> 20) & 0xf;
339
Sudeep KarkadaNageshaf7cd2d82013-10-29 12:18:37 +0000340 irq = irq_of_parse_and_map(np, 0);
341
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100342 /*
343 * A subset of the SCC registers is also used to communicate
344 * with the SPC (power controller). We need to be able to
345 * drive it very early in the boot process to power up
346 * processors, so we initialize the SPC driver here.
347 */
Sudeep KarkadaNageshaf7cd2d82013-10-29 12:18:37 +0000348 ret = ve_spc_init(scc + SPC_BASE, a15_cluster_id, irq);
Nicolas Pitre11b277e2013-08-06 19:10:08 +0100349 if (ret)
350 return ret;
351
352 if (!cci_probed())
353 return -ENODEV;
354
355 if (!tc2_pm_usage_count_init())
356 return -EINVAL;
357
358 ret = mcpm_platform_register(&tc2_pm_power_ops);
359 if (!ret) {
360 mcpm_sync_init(tc2_pm_power_up_setup);
361 pr_info("TC2 power management initialized\n");
362 }
363 return ret;
364}
365
366early_initcall(tc2_pm_init);