ARM: TC2: replace hard coded cluster and cpu values with constants
[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 #include <mach/tc2.h>
29
30 #include <linux/vexpress.h>
31 #include <linux/arm-cci.h>
32
33 /*
34  * We can't use regular spinlocks. In the switcher case, it is possible
35  * for an outbound CPU to call power_down() after its inbound counterpart
36  * is already live using the same logical CPU number which trips lockdep
37  * debugging.
38  */
39 static arch_spinlock_t tc2_pm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
40
41 static int tc2_pm_use_count[TC2_MAX_CPUS][TC2_MAX_CLUSTERS];
42
43 static int tc2_pm_power_up(unsigned int cpu, unsigned int cluster)
44 {
45         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
46         if (cluster >= TC2_MAX_CLUSTERS ||
47             cpu >= vexpress_spc_get_nb_cpus(cluster))
48                 return -EINVAL;
49
50         /*
51          * Since this is called with IRQs enabled, and no arch_spin_lock_irq
52          * variant exists, we need to disable IRQs manually here.
53          */
54         local_irq_disable();
55         arch_spin_lock(&tc2_pm_lock);
56
57         if (!tc2_pm_use_count[0][cluster] &&
58             !tc2_pm_use_count[1][cluster] &&
59             !tc2_pm_use_count[2][cluster])
60                 vexpress_spc_powerdown_enable(cluster, 0);
61
62         tc2_pm_use_count[cpu][cluster]++;
63         if (tc2_pm_use_count[cpu][cluster] == 1) {
64                 vexpress_spc_write_resume_reg(cluster, cpu,
65                                               virt_to_phys(mcpm_entry_point));
66                 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 1);
67         } else if (tc2_pm_use_count[cpu][cluster] != 2) {
68                 /*
69                  * The only possible values are:
70                  * 0 = CPU down
71                  * 1 = CPU (still) up
72                  * 2 = CPU requested to be up before it had a chance
73                  *     to actually make itself down.
74                  * Any other value is a bug.
75                  */
76                 BUG();
77         }
78
79         arch_spin_unlock(&tc2_pm_lock);
80         local_irq_enable();
81
82         return 0;
83 }
84
85 static void tc2_pm_down(u64 residency)
86 {
87         unsigned int mpidr, cpu, cluster;
88         bool last_man = false, skip_wfi = false;
89
90         mpidr = read_cpuid_mpidr();
91         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
92         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
93
94         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
95         BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
96                cpu >= vexpress_spc_get_nb_cpus(cluster));
97
98         __mcpm_cpu_going_down(cpu, cluster);
99
100         arch_spin_lock(&tc2_pm_lock);
101         BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
102         tc2_pm_use_count[cpu][cluster]--;
103         if (tc2_pm_use_count[cpu][cluster] == 0) {
104                 vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 1);
105                 if (!tc2_pm_use_count[0][cluster] &&
106                     !tc2_pm_use_count[1][cluster] &&
107                     !tc2_pm_use_count[2][cluster] &&
108                     (!residency || residency > 5000)) {
109                         vexpress_spc_powerdown_enable(cluster, 1);
110                         vexpress_spc_set_global_wakeup_intr(1);
111                         last_man = true;
112                 }
113         } else if (tc2_pm_use_count[cpu][cluster] == 1) {
114                 /*
115                  * A power_up request went ahead of us.
116                  * Even if we do not want to shut this CPU down,
117                  * the caller expects a certain state as if the WFI
118                  * was aborted.  So let's continue with cache cleaning.
119                  */
120                 skip_wfi = true;
121         } else
122                 BUG();
123
124         gic_cpu_if_down();
125
126         if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
127                 arch_spin_unlock(&tc2_pm_lock);
128
129                 set_cr(get_cr() & ~CR_C);
130                 flush_cache_all();
131                 asm volatile ("clrex");
132                 set_auxcr(get_auxcr() & ~(1 << 6));
133
134                 cci_disable_port_by_cpu(mpidr);
135
136                 /*
137                  * Ensure that both C & I bits are disabled in the SCTLR
138                  * before disabling ACE snoops. This ensures that no
139                  * coherency traffic will originate from this cpu after
140                  * ACE snoops are turned off.
141                  */
142                 cpu_proc_fin();
143
144                 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
145         } else {
146                 /*
147                  * If last man then undo any setup done previously.
148                  */
149                 if (last_man) {
150                         vexpress_spc_powerdown_enable(cluster, 0);
151                         vexpress_spc_set_global_wakeup_intr(0);
152                 }
153
154                 arch_spin_unlock(&tc2_pm_lock);
155
156                 set_cr(get_cr() & ~CR_C);
157                 flush_cache_louis();
158                 asm volatile ("clrex");
159                 set_auxcr(get_auxcr() & ~(1 << 6));
160         }
161
162         __mcpm_cpu_down(cpu, cluster);
163
164         /* Now we are prepared for power-down, do it: */
165         if (!skip_wfi)
166                 wfi();
167
168         /* Not dead at this point?  Let our caller cope. */
169 }
170
171 static void tc2_pm_power_down(void)
172 {
173         tc2_pm_down(0);
174 }
175
176 static void tc2_pm_suspend(u64 residency)
177 {
178         extern void tc2_resume(void);
179         unsigned int mpidr, cpu, cluster;
180
181         mpidr = read_cpuid_mpidr();
182         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
183         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
184         vexpress_spc_write_resume_reg(cluster, cpu,
185                                       virt_to_phys(tc2_resume));
186
187         tc2_pm_down(residency);
188 }
189
190 static void tc2_pm_powered_up(void)
191 {
192         unsigned int mpidr, cpu, cluster;
193         unsigned long flags;
194
195         mpidr = read_cpuid_mpidr();
196         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
197         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
198
199         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
200         BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
201                cpu >= vexpress_spc_get_nb_cpus(cluster));
202
203         local_irq_save(flags);
204         arch_spin_lock(&tc2_pm_lock);
205
206         if (!tc2_pm_use_count[0][cluster] &&
207             !tc2_pm_use_count[1][cluster] &&
208             !tc2_pm_use_count[2][cluster]) {
209                 vexpress_spc_powerdown_enable(cluster, 0);
210                 vexpress_spc_set_global_wakeup_intr(0);
211         }
212
213         if (!tc2_pm_use_count[cpu][cluster])
214                 tc2_pm_use_count[cpu][cluster] = 1;
215
216         vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 0);
217         vexpress_spc_write_resume_reg(cluster, cpu, 0);
218
219         arch_spin_unlock(&tc2_pm_lock);
220         local_irq_restore(flags);
221 }
222
223 static const struct mcpm_platform_ops tc2_pm_power_ops = {
224         .power_up       = tc2_pm_power_up,
225         .power_down     = tc2_pm_power_down,
226         .suspend        = tc2_pm_suspend,
227         .powered_up     = tc2_pm_powered_up,
228 };
229
230 static void __init tc2_pm_usage_count_init(void)
231 {
232         unsigned int mpidr, cpu, cluster;
233
234         mpidr = read_cpuid_mpidr();
235         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
236         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
237
238         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
239         BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
240                cpu >= vexpress_spc_get_nb_cpus(cluster));
241
242         tc2_pm_use_count[cpu][cluster] = 1;
243 }
244
245 extern void tc2_pm_power_up_setup(unsigned int affinity_level);
246
247 static int __init tc2_pm_init(void)
248 {
249         int ret;
250
251         if (!vexpress_spc_check_loaded())
252                 return -ENODEV;
253
254         tc2_pm_usage_count_init();
255
256         ret = mcpm_platform_register(&tc2_pm_power_ops);
257         if (!ret)
258                 ret = mcpm_sync_init(tc2_pm_power_up_setup);
259         if (!ret)
260                 pr_info("TC2 power management initialized\n");
261         return ret;
262 }
263
264 early_initcall(tc2_pm_init);