ARM: vexpress/tc2: implement PM suspend method
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-vexpress / tc2_pm.c
1 /*
2  * arch/arm/mach-vexpress/tc2_pm.c - TC2 power management support
3  *
4  * Created by:  Nicolas Pitre, October 2012
5  * Copyright:   (C) 2012  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/kernel.h>
17 #include <linux/spinlock.h>
18 #include <linux/errno.h>
19 #include <linux/irqchip/arm-gic.h>
20
21 #include <asm/mcpm.h>
22 #include <asm/proc-fns.h>
23 #include <asm/cacheflush.h>
24 #include <asm/cputype.h>
25 #include <asm/cp15.h>
26
27 #include <mach/motherboard.h>
28
29 #include <linux/vexpress.h>
30 #include <linux/arm-cci.h>
31
32 /*
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
36  * debugging.
37  */
38 static arch_spinlock_t tc2_pm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
39
40 static int tc2_pm_use_count[3][2];
41
42 static int tc2_pm_power_up(unsigned int cpu, unsigned int cluster)
43 {
44         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
45         if (cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster))
46                 return -EINVAL;
47
48         /*
49          * Since this is called with IRQs enabled, and no arch_spin_lock_irq
50          * variant exists, we need to disable IRQs manually here.
51          */
52         local_irq_disable();
53         arch_spin_lock(&tc2_pm_lock);
54
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);
59
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) {
66                 /*
67                  * The only possible values are:
68                  * 0 = CPU down
69                  * 1 = CPU (still) up
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.
73                  */
74                 BUG();
75         }
76
77         arch_spin_unlock(&tc2_pm_lock);
78         local_irq_enable();
79
80         return 0;
81 }
82
83 static void tc2_pm_down(u64 residency)
84 {
85         unsigned int mpidr, cpu, cluster;
86         bool last_man = false, skip_wfi = false;
87
88         mpidr = read_cpuid_mpidr();
89         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
90         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
91
92         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
93         BUG_ON(cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster));
94
95         __mcpm_cpu_going_down(cpu, cluster);
96
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);
108                         last_man = true;
109                 }
110         } else if (tc2_pm_use_count[cpu][cluster] == 1) {
111                 /*
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.
116                  */
117                 skip_wfi = true;
118         } else
119                 BUG();
120
121         gic_cpu_if_down();
122
123         if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
124                 arch_spin_unlock(&tc2_pm_lock);
125
126                 set_cr(get_cr() & ~CR_C);
127                 flush_cache_all();
128                 asm volatile ("clrex");
129                 set_auxcr(get_auxcr() & ~(1 << 6));
130
131                 disable_cci(cluster);
132
133                 /*
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.
138                  */
139                 cpu_proc_fin();
140
141                 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
142         } else {
143                 /*
144                  * If last man then undo any setup done previously.
145                  */
146                 if (last_man) {
147                         vexpress_spc_powerdown_enable(cluster, 0);
148                         vexpress_spc_set_global_wakeup_intr(0);
149                 }
150
151                 arch_spin_unlock(&tc2_pm_lock);
152
153                 set_cr(get_cr() & ~CR_C);
154                 flush_cache_louis();
155                 asm volatile ("clrex");
156                 set_auxcr(get_auxcr() & ~(1 << 6));
157         }
158
159         __mcpm_cpu_down(cpu, cluster);
160
161         /* Now we are prepared for power-down, do it: */
162         if (!skip_wfi)
163                 wfi();
164
165         /* Not dead at this point?  Let our caller cope. */
166 }
167
168 static void tc2_pm_power_down(void)
169 {
170         tc2_pm_down(0);
171 }
172
173 static void tc2_pm_suspend(u64 residency)
174 {
175         unsigned int mpidr, cpu, cluster;
176
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);
183 }
184
185 static void tc2_pm_powered_up(void)
186 {
187         unsigned int mpidr, cpu, cluster;
188         unsigned long flags;
189
190         mpidr = read_cpuid_mpidr();
191         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
192         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
193
194         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
195         BUG_ON(cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster));
196
197         local_irq_save(flags);
198         arch_spin_lock(&tc2_pm_lock);
199
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);
205         }
206
207         if (!tc2_pm_use_count[cpu][cluster])
208                 tc2_pm_use_count[cpu][cluster] = 1;
209
210         vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 0);
211         vexpress_spc_write_bxaddr_reg(cluster, cpu, 0);
212
213         arch_spin_unlock(&tc2_pm_lock);
214         local_irq_restore(flags);
215 }
216
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,
222 };
223
224 static void __init tc2_pm_usage_count_init(void)
225 {
226         unsigned int mpidr, cpu, cluster;
227
228         mpidr = read_cpuid_mpidr();
229         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
230         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
231
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;
235 }
236
237 extern void tc2_pm_power_up_setup(unsigned int affinity_level);
238
239 static int __init tc2_pm_init(void)
240 {
241         int ret;
242
243         if (!vexpress_spc_check_loaded())
244                 return -ENODEV;
245
246         tc2_pm_usage_count_init();
247
248         ret = mcpm_platform_register(&tc2_pm_power_ops);
249         if (!ret)
250                 ret = mcpm_sync_init(tc2_pm_power_up_setup);
251         if (!ret)
252                 pr_info("TC2 power management initialized\n");
253         return ret;
254 }
255
256 early_initcall(tc2_pm_init);