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/sysfs.h>
28 #include <linux/irqchip/arm-gic.h>
29 #include <linux/moduleparam.h>
31 #include <asm/smp_plat.h>
32 #include <asm/suspend.h>
34 #include <asm/bL_switcher.h>
38 * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
39 * __attribute_const__ and we don't want the compiler to assume any
40 * constness here as the value _does_ change along some code paths.
43 static int read_mpidr(void)
46 asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
47 return id & MPIDR_HWID_BITMASK;
51 * bL switcher core code.
54 static void bL_do_switch(void *_unused)
56 unsigned mpidr, cpuid, clusterid, ob_cluster, ib_cluster;
58 pr_debug("%s\n", __func__);
61 cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
62 clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
63 ob_cluster = clusterid;
64 ib_cluster = clusterid ^ 1;
67 * Our state has been saved at this point. Let's release our
70 mcpm_set_entry_vector(cpuid, ib_cluster, cpu_resume);
74 * From this point, we must assume that our counterpart CPU might
75 * have taken over in its parallel world already, as if execution
76 * just returned from cpu_suspend(). It is therefore important to
77 * be very careful not to make any change the other guy is not
78 * expecting. This is why we need stack isolation.
80 * Fancy under cover tasks could be performed here. For now
84 /* Let's put ourself down. */
85 mcpm_cpu_power_down();
87 /* should never get here */
92 * Stack isolation. To ensure 'current' remains valid, we just use another
93 * piece of our thread's stack space which should be fairly lightly used.
94 * The selected area starts just above the thread_info structure located
95 * at the very bottom of the stack, aligned to a cache line, and indexed
96 * with the cluster number.
98 #define STACK_SIZE 512
99 extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
100 static int bL_switchpoint(unsigned long _arg)
102 unsigned int mpidr = read_mpidr();
103 unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
104 void *stack = current_thread_info() + 1;
105 stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
106 stack += clusterid * STACK_SIZE + STACK_SIZE;
107 call_with_stack(bL_do_switch, (void *)_arg, stack);
112 * Generic switcher interface
115 static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
118 * bL_switch_to - Switch to a specific cluster for the current CPU
119 * @new_cluster_id: the ID of the cluster to switch to.
121 * This function must be called on the CPU to be switched.
122 * Returns 0 on success, else a negative status code.
124 static int bL_switch_to(unsigned int new_cluster_id)
126 unsigned int mpidr, cpuid, clusterid, ob_cluster, ib_cluster, this_cpu;
127 struct tick_device *tdev;
128 enum clock_event_mode tdev_mode;
131 mpidr = read_mpidr();
132 cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
133 clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
134 ob_cluster = clusterid;
135 ib_cluster = clusterid ^ 1;
137 if (new_cluster_id == clusterid)
140 pr_debug("before switch: CPU %d in cluster %d\n", cpuid, clusterid);
142 /* Close the gate for our entry vectors */
143 mcpm_set_entry_vector(cpuid, ob_cluster, NULL);
144 mcpm_set_entry_vector(cpuid, ib_cluster, NULL);
147 * Let's wake up the inbound CPU now in case it requires some delay
148 * to come online, but leave it gated in our entry vector code.
150 ret = mcpm_cpu_power_up(cpuid, ib_cluster);
152 pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
157 * From this point we are entering the switch critical zone
158 * and can't take any interrupts anymore.
163 this_cpu = smp_processor_id();
165 /* redirect GIC's SGIs to our counterpart */
166 gic_migrate_target(bL_gic_id[cpuid][ib_cluster]);
169 * Raise a SGI on the inbound CPU to make sure it doesn't stall
170 * in a possible WFI, such as in mcpm_power_down().
172 arch_send_wakeup_ipi_mask(cpumask_of(this_cpu));
174 tdev = tick_get_device(this_cpu);
175 if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
178 tdev_mode = tdev->evtdev->mode;
179 clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
182 ret = cpu_pm_enter();
184 /* we can not tolerate errors at this point */
186 panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
188 /* Flip the cluster in the CPU logical map for this CPU. */
189 cpu_logical_map(this_cpu) ^= (1 << 8);
191 /* Let's do the actual CPU switch. */
192 ret = cpu_suspend(0, bL_switchpoint);
194 panic("%s: cpu_suspend() returned %d\n", __func__, ret);
196 /* We are executing on the inbound CPU at this point */
197 mpidr = read_mpidr();
198 cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
199 clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
200 pr_debug("after switch: CPU %d in cluster %d\n", cpuid, clusterid);
201 BUG_ON(clusterid != ib_cluster);
203 mcpm_cpu_powered_up();
208 clockevents_set_mode(tdev->evtdev, tdev_mode);
209 clockevents_program_event(tdev->evtdev,
210 tdev->evtdev->next_event, 1);
217 pr_err("%s exiting with error %d\n", __func__, ret);
222 struct task_struct *task;
223 wait_queue_head_t wq;
225 struct completion started;
228 static struct bL_thread bL_threads[NR_CPUS];
230 static int bL_switcher_thread(void *arg)
232 struct bL_thread *t = arg;
233 struct sched_param param = { .sched_priority = 1 };
236 sched_setscheduler_nocheck(current, SCHED_FIFO, ¶m);
237 complete(&t->started);
240 if (signal_pending(current))
241 flush_signals(current);
242 wait_event_interruptible(t->wq,
243 t->wanted_cluster != -1 ||
244 kthread_should_stop());
245 cluster = xchg(&t->wanted_cluster, -1);
247 bL_switch_to(cluster);
248 } while (!kthread_should_stop());
253 static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
255 struct task_struct *task;
257 task = kthread_create_on_node(bL_switcher_thread, arg,
258 cpu_to_node(cpu), "kswitcher_%d", cpu);
260 kthread_bind(task, cpu);
261 wake_up_process(task);
263 pr_err("%s failed for CPU %d\n", __func__, cpu);
268 * bL_switch_request - Switch to a specific cluster for the given CPU
270 * @cpu: the CPU to switch
271 * @new_cluster_id: the ID of the cluster to switch to.
273 * This function causes a cluster switch on the given CPU by waking up
274 * the appropriate switcher thread. This function may or may not return
275 * before the switch has occurred.
277 int bL_switch_request(unsigned int cpu, unsigned int new_cluster_id)
281 if (cpu >= ARRAY_SIZE(bL_threads)) {
282 pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
286 t = &bL_threads[cpu];
288 return PTR_ERR(t->task);
292 t->wanted_cluster = new_cluster_id;
296 EXPORT_SYMBOL_GPL(bL_switch_request);
299 * Activation and configuration code.
302 static unsigned int bL_switcher_active;
303 static unsigned int bL_switcher_cpu_original_cluster[MAX_CPUS_PER_CLUSTER];
304 static cpumask_t bL_switcher_removed_logical_cpus;
306 static void bL_switcher_restore_cpus(void)
310 for_each_cpu(i, &bL_switcher_removed_logical_cpus)
314 static int bL_switcher_halve_cpus(void)
316 int cpu, cluster, i, ret;
317 cpumask_t cluster_mask[2], common_mask;
319 cpumask_clear(&bL_switcher_removed_logical_cpus);
320 cpumask_clear(&cluster_mask[0]);
321 cpumask_clear(&cluster_mask[1]);
323 for_each_online_cpu(i) {
324 cpu = cpu_logical_map(i) & 0xff;
325 cluster = (cpu_logical_map(i) >> 8) & 0xff;
327 pr_err("%s: only dual cluster systems are supported\n", __func__);
330 cpumask_set_cpu(cpu, &cluster_mask[cluster]);
333 if (!cpumask_and(&common_mask, &cluster_mask[0], &cluster_mask[1])) {
334 pr_err("%s: no common set of CPUs\n", __func__);
338 for_each_online_cpu(i) {
339 cpu = cpu_logical_map(i) & 0xff;
340 cluster = (cpu_logical_map(i) >> 8) & 0xff;
342 if (cpumask_test_cpu(cpu, &common_mask)) {
343 /* Let's take note of the GIC ID for this CPU */
344 int gic_id = gic_get_cpu_id(i);
346 pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
349 bL_gic_id[cpu][cluster] = gic_id;
350 pr_info("GIC ID for CPU %u cluster %u is %u\n",
351 cpu, cluster, gic_id);
354 * We keep only those logical CPUs which number
355 * is equal to their physical CPU number. This is
356 * not perfect but good enough for now.
359 bL_switcher_cpu_original_cluster[cpu] = cluster;
366 bL_switcher_restore_cpus();
369 cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
375 static int bL_switcher_enable(void)
379 cpu_hotplug_driver_lock();
380 if (bL_switcher_active) {
381 cpu_hotplug_driver_unlock();
385 pr_info("big.LITTLE switcher initializing\n");
387 ret = bL_switcher_halve_cpus();
389 cpu_hotplug_driver_unlock();
393 for_each_online_cpu(cpu) {
394 struct bL_thread *t = &bL_threads[cpu];
395 init_waitqueue_head(&t->wq);
396 init_completion(&t->started);
397 t->wanted_cluster = -1;
398 t->task = bL_switcher_thread_create(cpu, t);
401 bL_switcher_active = 1;
402 cpu_hotplug_driver_unlock();
404 pr_info("big.LITTLE switcher initialized\n");
410 static void bL_switcher_disable(void)
412 unsigned int cpu, cluster, i;
414 struct task_struct *task;
416 cpu_hotplug_driver_lock();
417 if (!bL_switcher_active) {
418 cpu_hotplug_driver_unlock();
421 bL_switcher_active = 0;
424 * To deactivate the switcher, we must shut down the switcher
425 * threads to prevent any other requests from being accepted.
426 * Then, if the final cluster for given logical CPU is not the
427 * same as the original one, we'll recreate a switcher thread
428 * just for the purpose of switching the CPU back without any
429 * possibility for interference from external requests.
431 for_each_online_cpu(cpu) {
432 BUG_ON(cpu != (cpu_logical_map(cpu) & 0xff));
433 t = &bL_threads[cpu];
436 if (!task || IS_ERR(task))
439 /* no more switch may happen on this CPU at this point */
440 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
441 if (cluster == bL_switcher_cpu_original_cluster[cpu])
443 init_completion(&t->started);
444 t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
445 task = bL_switcher_thread_create(cpu, t);
447 wait_for_completion(&t->started);
449 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
450 if (cluster == bL_switcher_cpu_original_cluster[cpu])
453 /* If execution gets here, we're in trouble. */
454 pr_crit("%s: unable to restore original cluster for CPU %d\n",
456 for_each_cpu(i, &bL_switcher_removed_logical_cpus) {
457 if ((cpu_logical_map(i) & 0xff) != cpu)
459 pr_crit("%s: CPU %d can't be restored\n",
461 cpumask_clear_cpu(i, &bL_switcher_removed_logical_cpus);
466 bL_switcher_restore_cpus();
467 cpu_hotplug_driver_unlock();
470 static ssize_t bL_switcher_active_show(struct kobject *kobj,
471 struct kobj_attribute *attr, char *buf)
473 return sprintf(buf, "%u\n", bL_switcher_active);
476 static ssize_t bL_switcher_active_store(struct kobject *kobj,
477 struct kobj_attribute *attr, const char *buf, size_t count)
483 bL_switcher_disable();
487 ret = bL_switcher_enable();
493 return (ret >= 0) ? count : ret;
496 static struct kobj_attribute bL_switcher_active_attr =
497 __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
499 static struct attribute *bL_switcher_attrs[] = {
500 &bL_switcher_active_attr.attr,
504 static struct attribute_group bL_switcher_attr_group = {
505 .attrs = bL_switcher_attrs,
508 static struct kobject *bL_switcher_kobj;
510 static int __init bL_switcher_sysfs_init(void)
514 bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
515 if (!bL_switcher_kobj)
517 ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
519 kobject_put(bL_switcher_kobj);
523 #endif /* CONFIG_SYSFS */
525 static bool no_bL_switcher;
526 core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
528 static int __init bL_switcher_init(void)
532 if (MAX_NR_CLUSTERS != 2) {
533 pr_err("%s: only dual cluster systems are supported\n", __func__);
537 if (!no_bL_switcher) {
538 ret = bL_switcher_enable();
544 ret = bL_switcher_sysfs_init();
546 pr_err("%s: unable to create sysfs entry\n", __func__);
552 late_initcall(bL_switcher_init);