blob: 6c03d0152e7f8ceefc76f9d0d0b09722128f45f9 [file] [log] [blame]
Nicolas Pitree8db2882012-04-12 02:45:22 -04001/*
2 * arch/arm/common/mcpm_entry.c -- entry point for multi-cluster PM
3 *
4 * Created by: Nicolas Pitre, March 2012
5 * Copyright: (C) 2012-2013 Linaro Limited
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Nicolas Pitre7c2b8602012-09-20 16:05:37 -040012#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/irqflags.h>
15
Nicolas Pitree8db2882012-04-12 02:45:22 -040016#include <asm/mcpm.h>
17#include <asm/cacheflush.h>
Nicolas Pitre7c2b8602012-09-20 16:05:37 -040018#include <asm/idmap.h>
Dave Martin7fe31d22012-07-17 14:25:42 +010019#include <asm/cputype.h>
Nicolas Pitree8db2882012-04-12 02:45:22 -040020
21extern unsigned long mcpm_entry_vectors[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER];
22
23void mcpm_set_entry_vector(unsigned cpu, unsigned cluster, void *ptr)
24{
25 unsigned long val = ptr ? virt_to_phys(ptr) : 0;
26 mcpm_entry_vectors[cluster][cpu] = val;
27 sync_cache_w(&mcpm_entry_vectors[cluster][cpu]);
28}
Nicolas Pitre7c2b8602012-09-20 16:05:37 -040029
30static const struct mcpm_platform_ops *platform_ops;
31
32int __init mcpm_platform_register(const struct mcpm_platform_ops *ops)
33{
34 if (platform_ops)
35 return -EBUSY;
36 platform_ops = ops;
37 return 0;
38}
39
40int mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster)
41{
42 if (!platform_ops)
43 return -EUNATCH; /* try not to shadow power_up errors */
44 might_sleep();
45 return platform_ops->power_up(cpu, cluster);
46}
47
48typedef void (*phys_reset_t)(unsigned long);
49
50void mcpm_cpu_power_down(void)
51{
52 phys_reset_t phys_reset;
53
Nicolas Pitred0cdef62013-09-25 23:26:24 +010054 if (WARN_ON_ONCE(!platform_ops || !platform_ops->power_down))
55 return;
Nicolas Pitre7c2b8602012-09-20 16:05:37 -040056 BUG_ON(!irqs_disabled());
57
58 /*
59 * Do this before calling into the power_down method,
60 * as it might not always be safe to do afterwards.
61 */
62 setup_mm_for_reboot();
63
64 platform_ops->power_down();
65
66 /*
67 * It is possible for a power_up request to happen concurrently
68 * with a power_down request for the same CPU. In this case the
69 * power_down method might not be able to actually enter a
70 * powered down state with the WFI instruction if the power_up
71 * method has removed the required reset condition. The
72 * power_down method is then allowed to return. We must perform
73 * a re-entry in the kernel as if the power_up method just had
74 * deasserted reset on the CPU.
75 *
76 * To simplify race issues, the platform specific implementation
77 * must accommodate for the possibility of unordered calls to
78 * power_down and power_up with a usage count. Therefore, if a
79 * call to power_up is issued for a CPU that is not down, then
80 * the next call to power_down must not attempt a full shutdown
81 * but only do the minimum (normally disabling L1 cache and CPU
82 * coherency) and return just as if a concurrent power_up request
83 * had happened as described above.
84 */
85
86 phys_reset = (phys_reset_t)(unsigned long)virt_to_phys(cpu_reset);
87 phys_reset(virt_to_phys(mcpm_entry_point));
88
89 /* should never get here */
90 BUG();
91}
92
Dave Martin0de0d642013-10-01 19:58:17 +010093int mcpm_cpu_power_down_finish(unsigned int cpu, unsigned int cluster)
94{
95 int ret;
96
97 if (WARN_ON_ONCE(!platform_ops || !platform_ops->power_down_finish))
98 return -EUNATCH;
99
100 ret = platform_ops->power_down_finish(cpu, cluster);
101 if (ret)
102 pr_warn("%s: cpu %u, cluster %u failed to power down (%d)\n",
103 __func__, cpu, cluster, ret);
104
105 return ret;
106}
107
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400108void mcpm_cpu_suspend(u64 expected_residency)
109{
110 phys_reset_t phys_reset;
111
Nicolas Pitred0cdef62013-09-25 23:26:24 +0100112 if (WARN_ON_ONCE(!platform_ops || !platform_ops->suspend))
113 return;
Nicolas Pitre7c2b8602012-09-20 16:05:37 -0400114 BUG_ON(!irqs_disabled());
115
116 /* Very similar to mcpm_cpu_power_down() */
117 setup_mm_for_reboot();
118 platform_ops->suspend(expected_residency);
119 phys_reset = (phys_reset_t)(unsigned long)virt_to_phys(cpu_reset);
120 phys_reset(virt_to_phys(mcpm_entry_point));
121 BUG();
122}
123
124int mcpm_cpu_powered_up(void)
125{
126 if (!platform_ops)
127 return -EUNATCH;
128 if (platform_ops->powered_up)
129 platform_ops->powered_up();
130 return 0;
131}
Dave Martin7fe31d22012-07-17 14:25:42 +0100132
133struct sync_struct mcpm_sync;
134
135/*
136 * __mcpm_cpu_going_down: Indicates that the cpu is being torn down.
137 * This must be called at the point of committing to teardown of a CPU.
138 * The CPU cache (SCTRL.C bit) is expected to still be active.
139 */
140void __mcpm_cpu_going_down(unsigned int cpu, unsigned int cluster)
141{
142 mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_GOING_DOWN;
143 sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
144}
145
146/*
147 * __mcpm_cpu_down: Indicates that cpu teardown is complete and that the
148 * cluster can be torn down without disrupting this CPU.
149 * To avoid deadlocks, this must be called before a CPU is powered down.
150 * The CPU cache (SCTRL.C bit) is expected to be off.
151 * However L2 cache might or might not be active.
152 */
153void __mcpm_cpu_down(unsigned int cpu, unsigned int cluster)
154{
155 dmb();
156 mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_DOWN;
157 sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
158 dsb_sev();
159}
160
161/*
162 * __mcpm_outbound_leave_critical: Leave the cluster teardown critical section.
163 * @state: the final state of the cluster:
164 * CLUSTER_UP: no destructive teardown was done and the cluster has been
165 * restored to the previous state (CPU cache still active); or
166 * CLUSTER_DOWN: the cluster has been torn-down, ready for power-off
167 * (CPU cache disabled, L2 cache either enabled or disabled).
168 */
169void __mcpm_outbound_leave_critical(unsigned int cluster, int state)
170{
171 dmb();
172 mcpm_sync.clusters[cluster].cluster = state;
173 sync_cache_w(&mcpm_sync.clusters[cluster].cluster);
174 dsb_sev();
175}
176
177/*
178 * __mcpm_outbound_enter_critical: Enter the cluster teardown critical section.
179 * This function should be called by the last man, after local CPU teardown
180 * is complete. CPU cache expected to be active.
181 *
182 * Returns:
183 * false: the critical section was not entered because an inbound CPU was
184 * observed, or the cluster is already being set up;
185 * true: the critical section was entered: it is now safe to tear down the
186 * cluster.
187 */
188bool __mcpm_outbound_enter_critical(unsigned int cpu, unsigned int cluster)
189{
190 unsigned int i;
191 struct mcpm_sync_struct *c = &mcpm_sync.clusters[cluster];
192
193 /* Warn inbound CPUs that the cluster is being torn down: */
194 c->cluster = CLUSTER_GOING_DOWN;
195 sync_cache_w(&c->cluster);
196
197 /* Back out if the inbound cluster is already in the critical region: */
198 sync_cache_r(&c->inbound);
199 if (c->inbound == INBOUND_COMING_UP)
200 goto abort;
201
202 /*
203 * Wait for all CPUs to get out of the GOING_DOWN state, so that local
204 * teardown is complete on each CPU before tearing down the cluster.
205 *
206 * If any CPU has been woken up again from the DOWN state, then we
207 * shouldn't be taking the cluster down at all: abort in that case.
208 */
209 sync_cache_r(&c->cpus);
210 for (i = 0; i < MAX_CPUS_PER_CLUSTER; i++) {
211 int cpustate;
212
213 if (i == cpu)
214 continue;
215
216 while (1) {
217 cpustate = c->cpus[i].cpu;
218 if (cpustate != CPU_GOING_DOWN)
219 break;
220
221 wfe();
222 sync_cache_r(&c->cpus[i].cpu);
223 }
224
225 switch (cpustate) {
226 case CPU_DOWN:
227 continue;
228
229 default:
230 goto abort;
231 }
232 }
233
234 return true;
235
236abort:
237 __mcpm_outbound_leave_critical(cluster, CLUSTER_UP);
238 return false;
239}
240
241int __mcpm_cluster_state(unsigned int cluster)
242{
243 sync_cache_r(&mcpm_sync.clusters[cluster].cluster);
244 return mcpm_sync.clusters[cluster].cluster;
245}
246
247extern unsigned long mcpm_power_up_setup_phys;
248
249int __init mcpm_sync_init(
250 void (*power_up_setup)(unsigned int affinity_level))
251{
252 unsigned int i, j, mpidr, this_cluster;
253
254 BUILD_BUG_ON(MCPM_SYNC_CLUSTER_SIZE * MAX_NR_CLUSTERS != sizeof mcpm_sync);
255 BUG_ON((unsigned long)&mcpm_sync & (__CACHE_WRITEBACK_GRANULE - 1));
256
257 /*
258 * Set initial CPU and cluster states.
259 * Only one cluster is assumed to be active at this point.
260 */
261 for (i = 0; i < MAX_NR_CLUSTERS; i++) {
262 mcpm_sync.clusters[i].cluster = CLUSTER_DOWN;
263 mcpm_sync.clusters[i].inbound = INBOUND_NOT_COMING_UP;
264 for (j = 0; j < MAX_CPUS_PER_CLUSTER; j++)
265 mcpm_sync.clusters[i].cpus[j].cpu = CPU_DOWN;
266 }
267 mpidr = read_cpuid_mpidr();
268 this_cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
269 for_each_online_cpu(i)
270 mcpm_sync.clusters[this_cluster].cpus[i].cpu = CPU_UP;
271 mcpm_sync.clusters[this_cluster].cluster = CLUSTER_UP;
272 sync_cache_w(&mcpm_sync);
273
274 if (power_up_setup) {
275 mcpm_power_up_setup_phys = virt_to_phys(power_up_setup);
276 sync_cache_w(&mcpm_power_up_setup_phys);
277 }
278
279 return 0;
280}