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>
28 #include <mach/motherboard.h>
31 #include <linux/vexpress.h>
32 #include <linux/arm-cci.h>
35 * We can't use regular spinlocks. In the switcher case, it is possible
36 * for an outbound CPU to call power_down() after its inbound counterpart
37 * is already live using the same logical CPU number which trips lockdep
40 static arch_spinlock_t tc2_pm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
42 static int tc2_pm_use_count[TC2_MAX_CPUS][TC2_MAX_CLUSTERS];
44 static int tc2_pm_power_up(unsigned int cpu, unsigned int cluster)
46 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
47 if (cluster >= TC2_MAX_CLUSTERS ||
48 cpu >= vexpress_spc_get_nb_cpus(cluster))
52 * Since this is called with IRQs enabled, and no arch_spin_lock_irq
53 * variant exists, we need to disable IRQs manually here.
56 arch_spin_lock(&tc2_pm_lock);
58 if (!tc2_pm_use_count[0][cluster] &&
59 !tc2_pm_use_count[1][cluster] &&
60 !tc2_pm_use_count[2][cluster])
61 vexpress_spc_powerdown_enable(cluster, 0);
63 tc2_pm_use_count[cpu][cluster]++;
64 if (tc2_pm_use_count[cpu][cluster] == 1) {
65 vexpress_spc_write_resume_reg(cluster, cpu,
66 virt_to_phys(mcpm_entry_point));
67 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 1);
68 } else if (tc2_pm_use_count[cpu][cluster] != 2) {
70 * The only possible values are:
73 * 2 = CPU requested to be up before it had a chance
74 * to actually make itself down.
75 * Any other value is a bug.
80 arch_spin_unlock(&tc2_pm_lock);
86 static void tc2_pm_down(u64 residency)
88 unsigned int mpidr, cpu, cluster;
89 bool last_man = false, skip_wfi = false;
91 mpidr = read_cpuid_mpidr();
92 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
93 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
95 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
96 BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
97 cpu >= vexpress_spc_get_nb_cpus(cluster));
99 __mcpm_cpu_going_down(cpu, cluster);
101 arch_spin_lock(&tc2_pm_lock);
102 BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
103 tc2_pm_use_count[cpu][cluster]--;
104 if (tc2_pm_use_count[cpu][cluster] == 0) {
105 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 1);
106 if (!tc2_pm_use_count[0][cluster] &&
107 !tc2_pm_use_count[1][cluster] &&
108 !tc2_pm_use_count[2][cluster] &&
109 (!residency || residency > 5000)) {
110 vexpress_spc_powerdown_enable(cluster, 1);
111 vexpress_spc_set_global_wakeup_intr(1);
114 } else if (tc2_pm_use_count[cpu][cluster] == 1) {
116 * A power_up request went ahead of us.
117 * Even if we do not want to shut this CPU down,
118 * the caller expects a certain state as if the WFI
119 * was aborted. So let's continue with cache cleaning.
126 * If the CPU is committed to power down, make sure
127 * the power controller will be in charge of waking it
128 * up upon IRQ, ie IRQ lines are cut from GIC CPU IF
129 * to the CPU by disabling the GIC CPU IF to prevent wfi
130 * from completing execution behind power controller back
135 if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
136 arch_spin_unlock(&tc2_pm_lock);
138 set_cr(get_cr() & ~CR_C);
140 asm volatile ("clrex");
141 set_auxcr(get_auxcr() & ~(1 << 6));
143 cci_disable_port_by_cpu(mpidr);
146 * Ensure that both C & I bits are disabled in the SCTLR
147 * before disabling ACE snoops. This ensures that no
148 * coherency traffic will originate from this cpu after
149 * ACE snoops are turned off.
153 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
156 * If last man then undo any setup done previously.
159 vexpress_spc_powerdown_enable(cluster, 0);
160 vexpress_spc_set_global_wakeup_intr(0);
163 arch_spin_unlock(&tc2_pm_lock);
165 set_cr(get_cr() & ~CR_C);
167 asm volatile ("clrex");
168 set_auxcr(get_auxcr() & ~(1 << 6));
171 __mcpm_cpu_down(cpu, cluster);
173 /* Now we are prepared for power-down, do it: */
177 /* Not dead at this point? Let our caller cope. */
180 static void tc2_pm_power_down(void)
185 static void tc2_pm_suspend(u64 residency)
187 extern void tc2_resume(void);
188 unsigned int mpidr, cpu, cluster;
190 mpidr = read_cpuid_mpidr();
191 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
192 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
193 vexpress_spc_write_resume_reg(cluster, cpu,
194 virt_to_phys(tc2_resume));
196 tc2_pm_down(residency);
199 static void tc2_pm_powered_up(void)
201 unsigned int mpidr, cpu, cluster;
204 mpidr = read_cpuid_mpidr();
205 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
206 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
208 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
209 BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
210 cpu >= vexpress_spc_get_nb_cpus(cluster));
212 local_irq_save(flags);
213 arch_spin_lock(&tc2_pm_lock);
215 if (!tc2_pm_use_count[0][cluster] &&
216 !tc2_pm_use_count[1][cluster] &&
217 !tc2_pm_use_count[2][cluster]) {
218 vexpress_spc_powerdown_enable(cluster, 0);
219 vexpress_spc_set_global_wakeup_intr(0);
222 if (!tc2_pm_use_count[cpu][cluster])
223 tc2_pm_use_count[cpu][cluster] = 1;
225 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 0);
226 vexpress_spc_write_resume_reg(cluster, cpu, 0);
228 arch_spin_unlock(&tc2_pm_lock);
229 local_irq_restore(flags);
232 static const struct mcpm_platform_ops tc2_pm_power_ops = {
233 .power_up = tc2_pm_power_up,
234 .power_down = tc2_pm_power_down,
235 .suspend = tc2_pm_suspend,
236 .powered_up = tc2_pm_powered_up,
239 static void __init tc2_pm_usage_count_init(void)
241 unsigned int mpidr, cpu, cluster;
243 mpidr = read_cpuid_mpidr();
244 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
245 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
247 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
248 BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
249 cpu >= vexpress_spc_get_nb_cpus(cluster));
251 tc2_pm_use_count[cpu][cluster] = 1;
254 extern void tc2_pm_power_up_setup(unsigned int affinity_level);
256 static int __init tc2_pm_init(void)
262 pr_debug("psci found. Aborting native init\n");
266 if (!vexpress_spc_check_loaded())
269 tc2_pm_usage_count_init();
271 ret = mcpm_platform_register(&tc2_pm_power_ops);
273 ret = mcpm_sync_init(tc2_pm_power_up_setup);
275 pr_info("TC2 power management initialized\n");
279 early_initcall(tc2_pm_init);