ARM: TC2: basic PM support
[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
20 #include <asm/mcpm.h>
21 #include <asm/proc-fns.h>
22 #include <asm/cacheflush.h>
23 #include <asm/cputype.h>
24 #include <asm/cp15.h>
25
26 #include <mach/motherboard.h>
27
28 #include <linux/vexpress.h>
29 #include <linux/arm-cci.h>
30
31 /*
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
35  * debugging.
36  */
37 static arch_spinlock_t tc2_pm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
38
39 static int tc2_pm_use_count[3][2];
40
41 static int tc2_pm_power_up(unsigned int cpu, unsigned int cluster)
42 {
43         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
44         if (cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster))
45                 return -EINVAL;
46
47         /*
48          * Since this is called with IRQs enabled, and no arch_spin_lock_irq
49          * variant exists, we need to disable IRQs manually here.
50          */
51         local_irq_disable();
52         arch_spin_lock(&tc2_pm_lock);
53
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);
58
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) {
65                 /*
66                  * The only possible values are:
67                  * 0 = CPU down
68                  * 1 = CPU (still) up
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.
72                  */
73                 BUG();
74         }
75
76         arch_spin_unlock(&tc2_pm_lock);
77         local_irq_enable();
78
79         return 0;
80 }
81
82 static void tc2_pm_power_down(void)
83 {
84         unsigned int mpidr, cpu, cluster;
85         bool last_man = false, skip_wfi = false;
86
87         mpidr = read_cpuid_mpidr();
88         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
89         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
90
91         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
92         BUG_ON(cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster));
93
94         __mcpm_cpu_going_down(cpu, cluster);
95
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);
106                         last_man = true;
107                 }
108         } else if (tc2_pm_use_count[cpu][cluster] == 1) {
109                 /*
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.
114                  */
115                 skip_wfi = true;
116         } else
117                 BUG();
118
119         if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
120                 arch_spin_unlock(&tc2_pm_lock);
121
122                 set_cr(get_cr() & ~CR_C);
123                 flush_cache_all();
124                 asm volatile ("clrex");
125                 set_auxcr(get_auxcr() & ~(1 << 6));
126
127                 disable_cci(cluster);
128
129                 /*
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.
134                  */
135                 cpu_proc_fin();
136
137                 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
138         } else {
139                 /*
140                  * If last man then undo any setup done previously.
141                  */
142                 if (last_man) {
143                         vexpress_spc_powerdown_enable(cluster, 0);
144                         vexpress_spc_set_global_wakeup_intr(0);
145                 }
146
147                 arch_spin_unlock(&tc2_pm_lock);
148
149                 set_cr(get_cr() & ~CR_C);
150                 flush_cache_louis();
151                 asm volatile ("clrex");
152                 set_auxcr(get_auxcr() & ~(1 << 6));
153         }
154
155         __mcpm_cpu_down(cpu, cluster);
156
157         /* Now we are prepared for power-down, do it: */
158         if (!skip_wfi)
159                 wfi();
160
161         /* Not dead at this point?  Let our caller cope. */
162 }
163
164 static void tc2_pm_powered_up(void)
165 {
166         unsigned int mpidr, cpu, cluster;
167         unsigned long flags;
168
169         mpidr = read_cpuid_mpidr();
170         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
171         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
172
173         pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
174         BUG_ON(cluster >= 2 || cpu >= vexpress_spc_get_nb_cpus(cluster));
175
176         local_irq_save(flags);
177         arch_spin_lock(&tc2_pm_lock);
178
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);
184         }
185
186         if (!tc2_pm_use_count[cpu][cluster])
187                 tc2_pm_use_count[cpu][cluster] = 1;
188
189         vexpress_spc_set_cpu_wakeup_irq(cpu, cluster, 0);
190         vexpress_spc_write_bxaddr_reg(cluster, cpu, 0);
191
192         arch_spin_unlock(&tc2_pm_lock);
193         local_irq_restore(flags);
194 }
195
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,
200 };
201
202 static void __init tc2_pm_usage_count_init(void)
203 {
204         unsigned int mpidr, cpu, cluster;
205
206         mpidr = read_cpuid_mpidr();
207         cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
208         cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
209
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;
213 }
214
215 extern void tc2_pm_power_up_setup(unsigned int affinity_level);
216
217 static int __init tc2_pm_init(void)
218 {
219         int ret;
220
221         if (!vexpress_spc_check_loaded())
222                 return -ENODEV;
223
224         tc2_pm_usage_count_init();
225
226         ret = mcpm_platform_register(&tc2_pm_power_ops);
227         if (!ret)
228                 ret = mcpm_sync_init(tc2_pm_power_up_setup);
229         if (!ret)
230                 pr_info("TC2 power management initialized\n");
231         return ret;
232 }
233
234 early_initcall(tc2_pm_init);