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>
29 #include <linux/vexpress.h>
30 #include <linux/arm-cci.h>
33 * We can't use regular spinlocks. In the switcher case, it is possible
34 * for an outbound CPU to call power_down() after its inbound counterpart
35 * is already live using the same logical CPU number which trips lockdep
38 static arch_spinlock_t tc2_pm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
40 static int tc2_pm_use_count[3][2];
42 static int tc2_pm_power_up(unsigned int cpu, unsigned int cluster)
44 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
45 if (cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster))
49 * Since this is called with IRQs enabled, and no arch_spin_lock_irq
50 * variant exists, we need to disable IRQs manually here.
53 arch_spin_lock(&tc2_pm_lock);
55 if (!tc2_pm_use_count[0][cluster] &&
56 !tc2_pm_use_count[1][cluster] &&
57 !tc2_pm_use_count[2][cluster])
58 vexpress_spc_powerdown_enable(cluster, 0);
60 tc2_pm_use_count[cpu][cluster]++;
61 if (tc2_pm_use_count[cpu][cluster] == 1) {
62 vexpress_spc_write_bxaddr_reg(cluster, cpu,
63 virt_to_phys(mcpm_entry_point));
64 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 1);
65 } else if (tc2_pm_use_count[cpu][cluster] != 2) {
67 * The only possible values are:
70 * 2 = CPU requested to be up before it had a chance
71 * to actually make itself down.
72 * Any other value is a bug.
77 arch_spin_unlock(&tc2_pm_lock);
83 static void tc2_pm_down(u64 residency)
85 unsigned int mpidr, cpu, cluster;
86 bool last_man = false, skip_wfi = false;
88 mpidr = read_cpuid_mpidr();
89 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
90 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
92 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
93 BUG_ON(cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster));
95 __mcpm_cpu_going_down(cpu, cluster);
97 arch_spin_lock(&tc2_pm_lock);
98 BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
99 tc2_pm_use_count[cpu][cluster]--;
100 if (tc2_pm_use_count[cpu][cluster] == 0) {
101 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 1);
102 if (!tc2_pm_use_count[0][cluster] &&
103 !tc2_pm_use_count[1][cluster] &&
104 !tc2_pm_use_count[2][cluster] &&
105 (!residency || residency > 5000)) {
106 vexpress_spc_powerdown_enable(cluster, 1);
107 vexpress_spc_set_global_wakeup_intr(1);
110 } else if (tc2_pm_use_count[cpu][cluster] == 1) {
112 * A power_up request went ahead of us.
113 * Even if we do not want to shut this CPU down,
114 * the caller expects a certain state as if the WFI
115 * was aborted. So let's continue with cache cleaning.
123 if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
124 arch_spin_unlock(&tc2_pm_lock);
126 set_cr(get_cr() & ~CR_C);
128 asm volatile ("clrex");
129 set_auxcr(get_auxcr() & ~(1 << 6));
131 disable_cci(cluster);
134 * Ensure that both C & I bits are disabled in the SCTLR
135 * before disabling ACE snoops. This ensures that no
136 * coherency traffic will originate from this cpu after
137 * ACE snoops are turned off.
141 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
144 * If last man then undo any setup done previously.
147 vexpress_spc_powerdown_enable(cluster, 0);
148 vexpress_spc_set_global_wakeup_intr(0);
151 arch_spin_unlock(&tc2_pm_lock);
153 set_cr(get_cr() & ~CR_C);
155 asm volatile ("clrex");
156 set_auxcr(get_auxcr() & ~(1 << 6));
159 __mcpm_cpu_down(cpu, cluster);
161 /* Now we are prepared for power-down, do it: */
165 /* Not dead at this point? Let our caller cope. */
168 static void tc2_pm_power_down(void)
173 static void tc2_pm_suspend(u64 residency)
175 unsigned int mpidr, cpu, cluster;
177 mpidr = read_cpuid_mpidr();
178 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
179 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
180 vexpress_spc_write_bxaddr_reg(cluster, cpu,
181 virt_to_phys(mcpm_entry_point));
182 tc2_pm_down(residency);
185 static void tc2_pm_powered_up(void)
187 unsigned int mpidr, cpu, cluster;
190 mpidr = read_cpuid_mpidr();
191 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
192 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
194 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
195 BUG_ON(cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster));
197 local_irq_save(flags);
198 arch_spin_lock(&tc2_pm_lock);
200 if (!tc2_pm_use_count[0][cluster] &&
201 !tc2_pm_use_count[1][cluster] &&
202 !tc2_pm_use_count[2][cluster]) {
203 vexpress_spc_powerdown_enable(cluster, 0);
204 vexpress_spc_set_global_wakeup_intr(0);
207 if (!tc2_pm_use_count[cpu][cluster])
208 tc2_pm_use_count[cpu][cluster] = 1;
210 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 0);
211 vexpress_spc_write_bxaddr_reg(cluster, cpu, 0);
213 arch_spin_unlock(&tc2_pm_lock);
214 local_irq_restore(flags);
217 static const struct mcpm_platform_ops tc2_pm_power_ops = {
218 .power_up = tc2_pm_power_up,
219 .power_down = tc2_pm_power_down,
220 .suspend = tc2_pm_suspend,
221 .powered_up = tc2_pm_powered_up,
224 static void __init tc2_pm_usage_count_init(void)
226 unsigned int mpidr, cpu, cluster;
228 mpidr = read_cpuid_mpidr();
229 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
230 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
232 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
233 BUG_ON(cpu >= 3 || cluster >= 2);
234 tc2_pm_use_count[cpu][cluster] = 1;
237 extern void tc2_pm_power_up_setup(unsigned int affinity_level);
239 static int __init tc2_pm_init(void)
243 if (!vexpress_spc_check_loaded())
246 tc2_pm_usage_count_init();
248 ret = mcpm_platform_register(&tc2_pm_power_ops);
250 ret = mcpm_sync_init(tc2_pm_power_up_setup);
252 pr_info("TC2 power management initialized\n");
256 early_initcall(tc2_pm_init);