Merge tag 'v3.10.13' into linux-linaro-lsk
[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 #include <asm/psci.h>
27
28 #include <mach/motherboard.h>
29 #include <mach/tc2.h>
30
31 #include <linux/vexpress.h>
32 #include <linux/arm-cci.h>
33
34 /*
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
38  * debugging.
39  */
40 static arch_spinlock_t tc2_pm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
41
42 static int tc2_pm_use_count[TC2_MAX_CPUS][TC2_MAX_CLUSTERS];
43
44 static int tc2_pm_power_up(unsigned int cpu, unsigned int cluster)
45 {
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))
49                 return -EINVAL;
50
51         /*
52          * Since this is called with IRQs enabled, and no arch_spin_lock_irq
53          * variant exists, we need to disable IRQs manually here.
54          */
55         local_irq_disable();
56         arch_spin_lock(&tc2_pm_lock);
57
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);
62
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) {
69                 /*
70                  * The only possible values are:
71                  * 0 = CPU down
72                  * 1 = CPU (still) up
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.
76                  */
77                 BUG();
78         }
79
80         arch_spin_unlock(&tc2_pm_lock);
81         local_irq_enable();
82
83         return 0;
84 }
85
86 static void tc2_pm_down(u64 residency)
87 {
88         unsigned int mpidr, cpu, cluster;
89         bool last_man = false, skip_wfi = false;
90
91         mpidr = read_cpuid_mpidr();
92         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
93         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
94
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));
98
99         __mcpm_cpu_going_down(cpu, cluster);
100
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);
112                         last_man = true;
113                 }
114         } else if (tc2_pm_use_count[cpu][cluster] == 1) {
115                 /*
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.
120                  */
121                 skip_wfi = true;
122         } else
123                 BUG();
124
125         gic_cpu_if_down();
126
127         if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
128                 arch_spin_unlock(&tc2_pm_lock);
129
130                 set_cr(get_cr() & ~CR_C);
131                 flush_cache_all();
132                 asm volatile ("clrex");
133                 set_auxcr(get_auxcr() & ~(1 << 6));
134
135                 cci_disable_port_by_cpu(mpidr);
136
137                 /*
138                  * Ensure that both C & I bits are disabled in the SCTLR
139                  * before disabling ACE snoops. This ensures that no
140                  * coherency traffic will originate from this cpu after
141                  * ACE snoops are turned off.
142                  */
143                 cpu_proc_fin();
144
145                 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
146         } else {
147                 /*
148                  * If last man then undo any setup done previously.
149                  */
150                 if (last_man) {
151                         vexpress_spc_powerdown_enable(cluster, 0);
152                         vexpress_spc_set_global_wakeup_intr(0);
153                 }
154
155                 arch_spin_unlock(&tc2_pm_lock);
156
157                 set_cr(get_cr() & ~CR_C);
158                 flush_cache_louis();
159                 asm volatile ("clrex");
160                 set_auxcr(get_auxcr() & ~(1 << 6));
161         }
162
163         __mcpm_cpu_down(cpu, cluster);
164
165         /* Now we are prepared for power-down, do it: */
166         if (!skip_wfi)
167                 wfi();
168
169         /* Not dead at this point?  Let our caller cope. */
170 }
171
172 static void tc2_pm_power_down(void)
173 {
174         tc2_pm_down(0);
175 }
176
177 static void tc2_pm_suspend(u64 residency)
178 {
179         extern void tc2_resume(void);
180         unsigned int mpidr, cpu, cluster;
181
182         mpidr = read_cpuid_mpidr();
183         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
184         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
185         vexpress_spc_write_resume_reg(cluster, cpu,
186                                       virt_to_phys(tc2_resume));
187
188         tc2_pm_down(residency);
189 }
190
191 static void tc2_pm_powered_up(void)
192 {
193         unsigned int mpidr, cpu, cluster;
194         unsigned long flags;
195
196         mpidr = read_cpuid_mpidr();
197         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
198         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
199
200         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
201         BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
202                cpu >= vexpress_spc_get_nb_cpus(cluster));
203
204         local_irq_save(flags);
205         arch_spin_lock(&tc2_pm_lock);
206
207         if (!tc2_pm_use_count[0][cluster] &&
208             !tc2_pm_use_count[1][cluster] &&
209             !tc2_pm_use_count[2][cluster]) {
210                 vexpress_spc_powerdown_enable(cluster, 0);
211                 vexpress_spc_set_global_wakeup_intr(0);
212         }
213
214         if (!tc2_pm_use_count[cpu][cluster])
215                 tc2_pm_use_count[cpu][cluster] = 1;
216
217         vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 0);
218         vexpress_spc_write_resume_reg(cluster, cpu, 0);
219
220         arch_spin_unlock(&tc2_pm_lock);
221         local_irq_restore(flags);
222 }
223
224 static const struct mcpm_platform_ops tc2_pm_power_ops = {
225         .power_up       = tc2_pm_power_up,
226         .power_down     = tc2_pm_power_down,
227         .suspend        = tc2_pm_suspend,
228         .powered_up     = tc2_pm_powered_up,
229 };
230
231 static void __init tc2_pm_usage_count_init(void)
232 {
233         unsigned int mpidr, cpu, cluster;
234
235         mpidr = read_cpuid_mpidr();
236         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
237         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
238
239         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
240         BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
241                cpu >= vexpress_spc_get_nb_cpus(cluster));
242
243         tc2_pm_use_count[cpu][cluster] = 1;
244 }
245
246 extern void tc2_pm_power_up_setup(unsigned int affinity_level);
247
248 static int __init tc2_pm_init(void)
249 {
250         int ret;
251
252         ret = psci_probe();
253         if (!ret) {
254                 pr_debug("psci found. Aborting native init\n");
255                 return -ENODEV;
256         }
257
258         if (!vexpress_spc_check_loaded())
259                 return -ENODEV;
260
261         tc2_pm_usage_count_init();
262
263         ret = mcpm_platform_register(&tc2_pm_power_ops);
264         if (!ret)
265                 ret = mcpm_sync_init(tc2_pm_power_up_setup);
266         if (!ret)
267                 pr_info("TC2 power management initialized\n");
268         return ret;
269 }
270
271 early_initcall(tc2_pm_init);