2 * arch/arm/mach-vexpress/tc2_pm.c - TC2 power management support
4 * Created by: Nicolas Pitre, October 2012
5 * Copyright: (C) 2012 Linaro Limited
7 * Some portions of this file were originally written by Achin Gupta
8 * Copyright: (C) 2012 ARM Limited
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.
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/spinlock.h>
18 #include <linux/errno.h>
19 #include <linux/irqchip/arm-gic.h>
22 #include <asm/proc-fns.h>
23 #include <asm/cacheflush.h>
24 #include <asm/cputype.h>
27 #include <mach/motherboard.h>
30 #include <linux/vexpress.h>
31 #include <linux/arm-cci.h>
34 * We can't use regular spinlocks. In the switcher case, it is possible
35 * for an outbound CPU to call power_down() after its inbound counterpart
36 * is already live using the same logical CPU number which trips lockdep
39 static arch_spinlock_t tc2_pm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
41 static int tc2_pm_use_count[TC2_MAX_CPUS][TC2_MAX_CLUSTERS];
43 static int tc2_pm_power_up(unsigned int cpu, unsigned int cluster)
45 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
46 if (cluster >= TC2_MAX_CLUSTERS ||
47 cpu >= vexpress_spc_get_nb_cpus(cluster))
51 * Since this is called with IRQs enabled, and no arch_spin_lock_irq
52 * variant exists, we need to disable IRQs manually here.
55 arch_spin_lock(&tc2_pm_lock);
57 if (!tc2_pm_use_count[0][cluster] &&
58 !tc2_pm_use_count[1][cluster] &&
59 !tc2_pm_use_count[2][cluster])
60 vexpress_spc_powerdown_enable(cluster, 0);
62 tc2_pm_use_count[cpu][cluster]++;
63 if (tc2_pm_use_count[cpu][cluster] == 1) {
64 vexpress_spc_write_resume_reg(cluster, cpu,
65 virt_to_phys(mcpm_entry_point));
66 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 1);
67 } else if (tc2_pm_use_count[cpu][cluster] != 2) {
69 * The only possible values are:
72 * 2 = CPU requested to be up before it had a chance
73 * to actually make itself down.
74 * Any other value is a bug.
79 arch_spin_unlock(&tc2_pm_lock);
85 static void tc2_pm_down(u64 residency)
87 unsigned int mpidr, cpu, cluster;
88 bool last_man = false, skip_wfi = false;
90 mpidr = read_cpuid_mpidr();
91 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
92 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
94 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
95 BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
96 cpu >= vexpress_spc_get_nb_cpus(cluster));
98 __mcpm_cpu_going_down(cpu, cluster);
100 arch_spin_lock(&tc2_pm_lock);
101 BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
102 tc2_pm_use_count[cpu][cluster]--;
103 if (tc2_pm_use_count[cpu][cluster] == 0) {
104 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 1);
105 if (!tc2_pm_use_count[0][cluster] &&
106 !tc2_pm_use_count[1][cluster] &&
107 !tc2_pm_use_count[2][cluster] &&
108 (!residency || residency > 5000)) {
109 vexpress_spc_powerdown_enable(cluster, 1);
110 vexpress_spc_set_global_wakeup_intr(1);
113 } else if (tc2_pm_use_count[cpu][cluster] == 1) {
115 * A power_up request went ahead of us.
116 * Even if we do not want to shut this CPU down,
117 * the caller expects a certain state as if the WFI
118 * was aborted. So let's continue with cache cleaning.
126 if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
127 arch_spin_unlock(&tc2_pm_lock);
129 set_cr(get_cr() & ~CR_C);
131 asm volatile ("clrex");
132 set_auxcr(get_auxcr() & ~(1 << 6));
134 cci_disable_port_by_cpu(mpidr);
137 * Ensure that both C & I bits are disabled in the SCTLR
138 * before disabling ACE snoops. This ensures that no
139 * coherency traffic will originate from this cpu after
140 * ACE snoops are turned off.
144 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
147 * If last man then undo any setup done previously.
150 vexpress_spc_powerdown_enable(cluster, 0);
151 vexpress_spc_set_global_wakeup_intr(0);
154 arch_spin_unlock(&tc2_pm_lock);
156 set_cr(get_cr() & ~CR_C);
158 asm volatile ("clrex");
159 set_auxcr(get_auxcr() & ~(1 << 6));
162 __mcpm_cpu_down(cpu, cluster);
164 /* Now we are prepared for power-down, do it: */
168 /* Not dead at this point? Let our caller cope. */
171 static void tc2_pm_power_down(void)
176 static void tc2_pm_suspend(u64 residency)
178 extern void tc2_resume(void);
179 unsigned int mpidr, cpu, cluster;
181 mpidr = read_cpuid_mpidr();
182 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
183 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
184 vexpress_spc_write_resume_reg(cluster, cpu,
185 virt_to_phys(tc2_resume));
187 tc2_pm_down(residency);
190 static void tc2_pm_powered_up(void)
192 unsigned int mpidr, cpu, cluster;
195 mpidr = read_cpuid_mpidr();
196 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
197 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
199 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
200 BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
201 cpu >= vexpress_spc_get_nb_cpus(cluster));
203 local_irq_save(flags);
204 arch_spin_lock(&tc2_pm_lock);
206 if (!tc2_pm_use_count[0][cluster] &&
207 !tc2_pm_use_count[1][cluster] &&
208 !tc2_pm_use_count[2][cluster]) {
209 vexpress_spc_powerdown_enable(cluster, 0);
210 vexpress_spc_set_global_wakeup_intr(0);
213 if (!tc2_pm_use_count[cpu][cluster])
214 tc2_pm_use_count[cpu][cluster] = 1;
216 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 0);
217 vexpress_spc_write_resume_reg(cluster, cpu, 0);
219 arch_spin_unlock(&tc2_pm_lock);
220 local_irq_restore(flags);
223 static const struct mcpm_platform_ops tc2_pm_power_ops = {
224 .power_up = tc2_pm_power_up,
225 .power_down = tc2_pm_power_down,
226 .suspend = tc2_pm_suspend,
227 .powered_up = tc2_pm_powered_up,
230 static void __init tc2_pm_usage_count_init(void)
232 unsigned int mpidr, cpu, cluster;
234 mpidr = read_cpuid_mpidr();
235 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
236 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
238 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
239 BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
240 cpu >= vexpress_spc_get_nb_cpus(cluster));
242 tc2_pm_use_count[cpu][cluster] = 1;
245 extern void tc2_pm_power_up_setup(unsigned int affinity_level);
247 static int __init tc2_pm_init(void)
251 if (!vexpress_spc_check_loaded())
254 tc2_pm_usage_count_init();
256 ret = mcpm_platform_register(&tc2_pm_power_ops);
258 ret = mcpm_sync_init(tc2_pm_power_up_setup);
260 pr_info("TC2 power management initialized\n");
264 early_initcall(tc2_pm_init);