2 * arch/arm/common/bL_switcher.c -- big.LITTLE cluster switcher core driver
4 * Created by: Nicolas Pitre, March 2012
5 * Copyright: (C) 2012-2013 Linaro Limited
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.
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/interrupt.h>
17 #include <linux/cpu_pm.h>
18 #include <linux/cpu.h>
19 #include <linux/cpumask.h>
20 #include <linux/kthread.h>
21 #include <linux/wait.h>
22 #include <linux/clockchips.h>
23 #include <linux/hrtimer.h>
24 #include <linux/tick.h>
26 #include <linux/string.h>
27 #include <linux/irqchip/arm-gic.h>
29 #include <asm/smp_plat.h>
30 #include <asm/suspend.h>
32 #include <asm/bL_switcher.h>
36 * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
37 * __attribute_const__ and we don't want the compiler to assume any
38 * constness here as the value _does_ change along some code paths.
41 static int read_mpidr(void)
44 asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
45 return id & MPIDR_HWID_BITMASK;
49 * bL switcher core code.
52 static void bL_do_switch(void *_unused)
54 unsigned mpidr, cpuid, clusterid, ob_cluster, ib_cluster;
56 pr_debug("%s\n", __func__);
59 cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
60 clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
61 ob_cluster = clusterid;
62 ib_cluster = clusterid ^ 1;
65 * Our state has been saved at this point. Let's release our
68 mcpm_set_entry_vector(cpuid, ib_cluster, cpu_resume);
72 * From this point, we must assume that our counterpart CPU might
73 * have taken over in its parallel world already, as if execution
74 * just returned from cpu_suspend(). It is therefore important to
75 * be very careful not to make any change the other guy is not
76 * expecting. This is why we need stack isolation.
78 * Fancy under cover tasks could be performed here. For now
82 /* Let's put ourself down. */
83 mcpm_cpu_power_down();
85 /* should never get here */
90 * Stack isolation. To ensure 'current' remains valid, we just use another
91 * piece of our thread's stack space which should be fairly lightly used.
92 * The selected area starts just above the thread_info structure located
93 * at the very bottom of the stack, aligned to a cache line, and indexed
94 * with the cluster number.
96 #define STACK_SIZE 512
97 extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
98 static int bL_switchpoint(unsigned long _arg)
100 unsigned int mpidr = read_mpidr();
101 unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
102 void *stack = current_thread_info() + 1;
103 stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
104 stack += clusterid * STACK_SIZE + STACK_SIZE;
105 call_with_stack(bL_do_switch, (void *)_arg, stack);
110 * Generic switcher interface
113 static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
116 * bL_switch_to - Switch to a specific cluster for the current CPU
117 * @new_cluster_id: the ID of the cluster to switch to.
119 * This function must be called on the CPU to be switched.
120 * Returns 0 on success, else a negative status code.
122 static int bL_switch_to(unsigned int new_cluster_id)
124 unsigned int mpidr, cpuid, clusterid, ob_cluster, ib_cluster, this_cpu;
125 struct tick_device *tdev;
126 enum clock_event_mode tdev_mode;
129 mpidr = read_mpidr();
130 cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
131 clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
132 ob_cluster = clusterid;
133 ib_cluster = clusterid ^ 1;
135 if (new_cluster_id == clusterid)
138 pr_debug("before switch: CPU %d in cluster %d\n", cpuid, clusterid);
140 /* Close the gate for our entry vectors */
141 mcpm_set_entry_vector(cpuid, ob_cluster, NULL);
142 mcpm_set_entry_vector(cpuid, ib_cluster, NULL);
145 * Let's wake up the inbound CPU now in case it requires some delay
146 * to come online, but leave it gated in our entry vector code.
148 ret = mcpm_cpu_power_up(cpuid, ib_cluster);
150 pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
155 * From this point we are entering the switch critical zone
156 * and can't take any interrupts anymore.
161 this_cpu = smp_processor_id();
163 /* redirect GIC's SGIs to our counterpart */
164 gic_migrate_target(bL_gic_id[cpuid][ib_cluster]);
167 * Raise a SGI on the inbound CPU to make sure it doesn't stall
168 * in a possible WFI, such as in mcpm_power_down().
170 arch_send_wakeup_ipi_mask(cpumask_of(this_cpu));
172 tdev = tick_get_device(this_cpu);
173 if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
176 tdev_mode = tdev->evtdev->mode;
177 clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
180 ret = cpu_pm_enter();
182 /* we can not tolerate errors at this point */
184 panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
186 /* Flip the cluster in the CPU logical map for this CPU. */
187 cpu_logical_map(this_cpu) ^= (1 << 8);
189 /* Let's do the actual CPU switch. */
190 ret = cpu_suspend(0, bL_switchpoint);
192 panic("%s: cpu_suspend() returned %d\n", __func__, ret);
194 /* We are executing on the inbound CPU at this point */
195 mpidr = read_mpidr();
196 cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
197 clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
198 pr_debug("after switch: CPU %d in cluster %d\n", cpuid, clusterid);
199 BUG_ON(clusterid != ib_cluster);
201 mcpm_cpu_powered_up();
206 clockevents_set_mode(tdev->evtdev, tdev_mode);
207 clockevents_program_event(tdev->evtdev,
208 tdev->evtdev->next_event, 1);
215 pr_err("%s exiting with error %d\n", __func__, ret);
220 struct task_struct *task;
221 wait_queue_head_t wq;
225 static struct bL_thread bL_threads[NR_CPUS];
227 static int bL_switcher_thread(void *arg)
229 struct bL_thread *t = arg;
230 struct sched_param param = { .sched_priority = 1 };
233 sched_setscheduler_nocheck(current, SCHED_FIFO, ¶m);
236 if (signal_pending(current))
237 flush_signals(current);
238 wait_event_interruptible(t->wq,
239 t->wanted_cluster != -1 ||
240 kthread_should_stop());
241 cluster = xchg(&t->wanted_cluster, -1);
243 bL_switch_to(cluster);
244 } while (!kthread_should_stop());
249 static struct task_struct * __init bL_switcher_thread_create(int cpu, void *arg)
251 struct task_struct *task;
253 task = kthread_create_on_node(bL_switcher_thread, arg,
254 cpu_to_node(cpu), "kswitcher_%d", cpu);
256 kthread_bind(task, cpu);
257 wake_up_process(task);
259 pr_err("%s failed for CPU %d\n", __func__, cpu);
264 * bL_switch_request - Switch to a specific cluster for the given CPU
266 * @cpu: the CPU to switch
267 * @new_cluster_id: the ID of the cluster to switch to.
269 * This function causes a cluster switch on the given CPU by waking up
270 * the appropriate switcher thread. This function may or may not return
271 * before the switch has occurred.
273 int bL_switch_request(unsigned int cpu, unsigned int new_cluster_id)
277 if (cpu >= ARRAY_SIZE(bL_threads)) {
278 pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
282 t = &bL_threads[cpu];
284 return PTR_ERR(t->task);
288 t->wanted_cluster = new_cluster_id;
292 EXPORT_SYMBOL_GPL(bL_switch_request);
295 * Activation and configuration code.
298 static cpumask_t bL_switcher_removed_logical_cpus;
300 static void __init bL_switcher_restore_cpus(void)
304 for_each_cpu(i, &bL_switcher_removed_logical_cpus)
308 static int __init bL_switcher_halve_cpus(void)
310 int cpu, cluster, i, ret;
311 cpumask_t cluster_mask[2], common_mask;
313 cpumask_clear(&bL_switcher_removed_logical_cpus);
314 cpumask_clear(&cluster_mask[0]);
315 cpumask_clear(&cluster_mask[1]);
317 for_each_online_cpu(i) {
318 cpu = cpu_logical_map(i) & 0xff;
319 cluster = (cpu_logical_map(i) >> 8) & 0xff;
321 pr_err("%s: only dual cluster systems are supported\n", __func__);
324 cpumask_set_cpu(cpu, &cluster_mask[cluster]);
327 if (!cpumask_and(&common_mask, &cluster_mask[0], &cluster_mask[1])) {
328 pr_err("%s: no common set of CPUs\n", __func__);
332 for_each_online_cpu(i) {
333 cpu = cpu_logical_map(i) & 0xff;
334 cluster = (cpu_logical_map(i) >> 8) & 0xff;
336 if (cpumask_test_cpu(cpu, &common_mask)) {
337 /* Let's take note of the GIC ID for this CPU */
338 int gic_id = gic_get_cpu_id(i);
340 pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
343 bL_gic_id[cpu][cluster] = gic_id;
344 pr_info("GIC ID for CPU %u cluster %u is %u\n",
345 cpu, cluster, gic_id);
348 * We keep only those logical CPUs which number
349 * is equal to their physical CPU number. This is
350 * not perfect but good enough for now.
358 bL_switcher_restore_cpus();
361 cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
367 static int __init bL_switcher_init(void)
371 pr_info("big.LITTLE switcher initializing\n");
373 if (MAX_NR_CLUSTERS != 2) {
374 pr_err("%s: only dual cluster systems are supported\n", __func__);
378 cpu_hotplug_driver_lock();
379 ret = bL_switcher_halve_cpus();
381 cpu_hotplug_driver_unlock();
385 for_each_online_cpu(cpu) {
386 struct bL_thread *t = &bL_threads[cpu];
387 init_waitqueue_head(&t->wq);
388 t->wanted_cluster = -1;
389 t->task = bL_switcher_thread_create(cpu, t);
391 cpu_hotplug_driver_unlock();
393 pr_info("big.LITTLE switcher initialized\n");
397 late_initcall(bL_switcher_init);