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>
21 #include <asm/proc-fns.h>
22 #include <asm/cacheflush.h>
23 #include <asm/cputype.h>
26 #include <mach/motherboard.h>
28 #include <linux/vexpress.h>
29 #include <linux/arm-cci.h>
32 * We can't use regular spinlocks. In the switcher case, it is possible
33 * for an outbound CPU to call power_down() after its inbound counterpart
34 * is already live using the same logical CPU number which trips lockdep
37 static arch_spinlock_t tc2_pm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
39 static int tc2_pm_use_count[3][2];
41 static int tc2_pm_power_up(unsigned int cpu, unsigned int cluster)
43 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
44 if (cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster))
48 * Since this is called with IRQs enabled, and no arch_spin_lock_irq
49 * variant exists, we need to disable IRQs manually here.
52 arch_spin_lock(&tc2_pm_lock);
54 if (!tc2_pm_use_count[0][cluster] &&
55 !tc2_pm_use_count[1][cluster] &&
56 !tc2_pm_use_count[2][cluster])
57 vexpress_spc_powerdown_enable(cluster, 0);
59 tc2_pm_use_count[cpu][cluster]++;
60 if (tc2_pm_use_count[cpu][cluster] == 1) {
61 vexpress_spc_write_bxaddr_reg(cluster, cpu,
62 virt_to_phys(mcpm_entry_point));
63 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 1);
64 } else if (tc2_pm_use_count[cpu][cluster] != 2) {
66 * The only possible values are:
69 * 2 = CPU requested to be up before it had a chance
70 * to actually make itself down.
71 * Any other value is a bug.
76 arch_spin_unlock(&tc2_pm_lock);
82 static void tc2_pm_power_down(void)
84 unsigned int mpidr, cpu, cluster;
85 bool last_man = false, skip_wfi = false;
87 mpidr = read_cpuid_mpidr();
88 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
89 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
91 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
92 BUG_ON(cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster));
94 __mcpm_cpu_going_down(cpu, cluster);
96 arch_spin_lock(&tc2_pm_lock);
97 BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
98 tc2_pm_use_count[cpu][cluster]--;
99 if (tc2_pm_use_count[cpu][cluster] == 0) {
100 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 1);
101 if (!tc2_pm_use_count[0][cluster] &&
102 !tc2_pm_use_count[1][cluster] &&
103 !tc2_pm_use_count[2][cluster]) {
104 vexpress_spc_powerdown_enable(cluster, 1);
105 vexpress_spc_set_global_wakeup_intr(1);
108 } else if (tc2_pm_use_count[cpu][cluster] == 1) {
110 * A power_up request went ahead of us.
111 * Even if we do not want to shut this CPU down,
112 * the caller expects a certain state as if the WFI
113 * was aborted. So let's continue with cache cleaning.
119 if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
120 arch_spin_unlock(&tc2_pm_lock);
122 set_cr(get_cr() & ~CR_C);
124 asm volatile ("clrex");
125 set_auxcr(get_auxcr() & ~(1 << 6));
127 disable_cci(cluster);
130 * Ensure that both C & I bits are disabled in the SCTLR
131 * before disabling ACE snoops. This ensures that no
132 * coherency traffic will originate from this cpu after
133 * ACE snoops are turned off.
137 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
140 * If last man then undo any setup done previously.
143 vexpress_spc_powerdown_enable(cluster, 0);
144 vexpress_spc_set_global_wakeup_intr(0);
147 arch_spin_unlock(&tc2_pm_lock);
149 set_cr(get_cr() & ~CR_C);
151 asm volatile ("clrex");
152 set_auxcr(get_auxcr() & ~(1 << 6));
155 __mcpm_cpu_down(cpu, cluster);
157 /* Now we are prepared for power-down, do it: */
161 /* Not dead at this point? Let our caller cope. */
164 static void tc2_pm_powered_up(void)
166 unsigned int mpidr, cpu, cluster;
169 mpidr = read_cpuid_mpidr();
170 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
171 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
173 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
174 BUG_ON(cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster));
176 local_irq_save(flags);
177 arch_spin_lock(&tc2_pm_lock);
179 if (!tc2_pm_use_count[0][cluster] &&
180 !tc2_pm_use_count[1][cluster] &&
181 !tc2_pm_use_count[2][cluster]) {
182 vexpress_spc_powerdown_enable(cluster, 0);
183 vexpress_spc_set_global_wakeup_intr(0);
186 if (!tc2_pm_use_count[cpu][cluster])
187 tc2_pm_use_count[cpu][cluster] = 1;
189 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 0);
190 vexpress_spc_write_bxaddr_reg(cluster, cpu, 0);
192 arch_spin_unlock(&tc2_pm_lock);
193 local_irq_restore(flags);
196 static const struct mcpm_platform_ops tc2_pm_power_ops = {
197 .power_up = tc2_pm_power_up,
198 .power_down = tc2_pm_power_down,
199 .powered_up = tc2_pm_powered_up,
202 static void __init tc2_pm_usage_count_init(void)
204 unsigned int mpidr, cpu, cluster;
206 mpidr = read_cpuid_mpidr();
207 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
208 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
210 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
211 BUG_ON(cpu >= 3 || cluster >= 2);
212 tc2_pm_use_count[cpu][cluster] = 1;
215 extern void tc2_pm_power_up_setup(unsigned int affinity_level);
217 static int __init tc2_pm_init(void)
221 if (!vexpress_spc_check_loaded())
224 tc2_pm_usage_count_init();
226 ret = mcpm_platform_register(&tc2_pm_power_ops);
228 ret = mcpm_sync_init(tc2_pm_power_up_setup);
230 pr_info("TC2 power management initialized\n");
234 early_initcall(tc2_pm_init);