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 ib_mpidr, ib_cpu, ib_cluster;
58 pr_debug("%s\n", __func__);
60 ib_mpidr = cpu_logical_map(smp_processor_id());
61 ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
62 ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
65 * Our state has been saved at this point. Let's release our
68 mcpm_set_entry_vector(ib_cpu, 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];
114 static int bL_switcher_cpu_pairing[NR_CPUS];
117 * bL_switch_to - Switch to a specific cluster for the current CPU
118 * @new_cluster_id: the ID of the cluster to switch to.
120 * This function must be called on the CPU to be switched.
121 * Returns 0 on success, else a negative status code.
123 static int bL_switch_to(unsigned int new_cluster_id)
125 unsigned int mpidr, this_cpu, that_cpu;
126 unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster;
127 struct tick_device *tdev;
128 enum clock_event_mode tdev_mode;
131 this_cpu = smp_processor_id();
132 ob_mpidr = read_mpidr();
133 ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0);
134 ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1);
135 BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr);
137 if (new_cluster_id == ob_cluster)
140 that_cpu = bL_switcher_cpu_pairing[this_cpu];
141 ib_mpidr = cpu_logical_map(that_cpu);
142 ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
143 ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
145 pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
146 this_cpu, ob_mpidr, ib_mpidr);
148 /* Close the gate for our entry vectors */
149 mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
150 mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
153 * Let's wake up the inbound CPU now in case it requires some delay
154 * to come online, but leave it gated in our entry vector code.
156 ret = mcpm_cpu_power_up(ib_cpu, ib_cluster);
158 pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
163 * From this point we are entering the switch critical zone
164 * and can't take any interrupts anymore.
169 /* redirect GIC's SGIs to our counterpart */
170 gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
173 * Raise a SGI on the inbound CPU to make sure it doesn't stall
174 * in a possible WFI, such as in mcpm_power_down().
176 arch_send_wakeup_ipi_mask(cpumask_of(this_cpu));
178 tdev = tick_get_device(this_cpu);
179 if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
182 tdev_mode = tdev->evtdev->mode;
183 clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
186 ret = cpu_pm_enter();
188 /* we can not tolerate errors at this point */
190 panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
192 /* Swap the physical CPUs in the logical map for this logical CPU. */
193 cpu_logical_map(this_cpu) = ib_mpidr;
194 cpu_logical_map(that_cpu) = ob_mpidr;
196 /* Let's do the actual CPU switch. */
197 ret = cpu_suspend(0, bL_switchpoint);
199 panic("%s: cpu_suspend() returned %d\n", __func__, ret);
201 /* We are executing on the inbound CPU at this point */
202 mpidr = read_mpidr();
203 pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr);
204 BUG_ON(mpidr != ib_mpidr);
206 mcpm_cpu_powered_up();
211 clockevents_set_mode(tdev->evtdev, tdev_mode);
212 clockevents_program_event(tdev->evtdev,
213 tdev->evtdev->next_event, 1);
220 pr_err("%s exiting with error %d\n", __func__, ret);
225 struct task_struct *task;
226 wait_queue_head_t wq;
228 struct completion started;
231 static struct bL_thread bL_threads[NR_CPUS];
233 static int bL_switcher_thread(void *arg)
235 struct bL_thread *t = arg;
236 struct sched_param param = { .sched_priority = 1 };
239 sched_setscheduler_nocheck(current, SCHED_FIFO, ¶m);
240 complete(&t->started);
243 if (signal_pending(current))
244 flush_signals(current);
245 wait_event_interruptible(t->wq,
246 t->wanted_cluster != -1 ||
247 kthread_should_stop());
248 cluster = xchg(&t->wanted_cluster, -1);
250 bL_switch_to(cluster);
251 } while (!kthread_should_stop());
256 static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
258 struct task_struct *task;
260 task = kthread_create_on_node(bL_switcher_thread, arg,
261 cpu_to_node(cpu), "kswitcher_%d", cpu);
263 kthread_bind(task, cpu);
264 wake_up_process(task);
266 pr_err("%s failed for CPU %d\n", __func__, cpu);
271 * bL_switch_request - Switch to a specific cluster for the given CPU
273 * @cpu: the CPU to switch
274 * @new_cluster_id: the ID of the cluster to switch to.
276 * This function causes a cluster switch on the given CPU by waking up
277 * the appropriate switcher thread. This function may or may not return
278 * before the switch has occurred.
280 int bL_switch_request(unsigned int cpu, unsigned int new_cluster_id)
284 if (cpu >= ARRAY_SIZE(bL_threads)) {
285 pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
289 t = &bL_threads[cpu];
291 return PTR_ERR(t->task);
295 t->wanted_cluster = new_cluster_id;
299 EXPORT_SYMBOL_GPL(bL_switch_request);
302 * Activation and configuration code.
305 static unsigned int bL_switcher_active;
306 static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
307 static cpumask_t bL_switcher_removed_logical_cpus;
309 static void bL_switcher_restore_cpus(void)
313 for_each_cpu(i, &bL_switcher_removed_logical_cpus)
317 static int bL_switcher_halve_cpus(void)
319 int i, j, cluster_0, gic_id, ret;
320 unsigned int cpu, cluster, mask;
321 cpumask_t available_cpus;
323 /* First pass to validate what we have */
325 for_each_online_cpu(i) {
326 cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
327 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
329 pr_err("%s: only dual cluster systems are supported\n", __func__);
332 if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
334 mask |= (1 << cluster);
337 pr_err("%s: no CPU pairing possible\n", __func__);
342 * Now let's do the pairing. We match each CPU with another CPU
343 * from a different cluster. To get a uniform scheduling behavior
344 * without fiddling with CPU topology and compute capacity data,
345 * we'll use logical CPUs initially belonging to the same cluster.
347 memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
348 cpumask_copy(&available_cpus, cpu_online_mask);
350 for_each_cpu(i, &available_cpus) {
352 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
355 if (cluster != cluster_0)
357 cpumask_clear_cpu(i, &available_cpus);
358 for_each_cpu(j, &available_cpus) {
359 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
361 * Let's remember the last match to create "odd"
362 * pairings on purpose in order for other code not
363 * to assume any relation between physical and
364 * logical CPU numbers.
366 if (cluster != cluster_0)
370 bL_switcher_cpu_pairing[i] = match;
371 cpumask_clear_cpu(match, &available_cpus);
372 pr_info("CPU%d paired with CPU%d\n", i, match);
377 * Now we disable the unwanted CPUs i.e. everything that has no
378 * pairing information (that includes the pairing counterparts).
380 cpumask_clear(&bL_switcher_removed_logical_cpus);
381 for_each_online_cpu(i) {
382 cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
383 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
385 /* Let's take note of the GIC ID for this CPU */
386 gic_id = gic_get_cpu_id(i);
388 pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
389 bL_switcher_restore_cpus();
392 bL_gic_id[cpu][cluster] = gic_id;
393 pr_info("GIC ID for CPU %u cluster %u is %u\n",
394 cpu, cluster, gic_id);
396 if (bL_switcher_cpu_pairing[i] != -1) {
397 bL_switcher_cpu_original_cluster[i] = cluster;
403 bL_switcher_restore_cpus();
406 cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
412 static int bL_switcher_enable(void)
416 cpu_hotplug_driver_lock();
417 if (bL_switcher_active) {
418 cpu_hotplug_driver_unlock();
422 pr_info("big.LITTLE switcher initializing\n");
424 ret = bL_switcher_halve_cpus();
426 cpu_hotplug_driver_unlock();
430 for_each_online_cpu(cpu) {
431 struct bL_thread *t = &bL_threads[cpu];
432 init_waitqueue_head(&t->wq);
433 init_completion(&t->started);
434 t->wanted_cluster = -1;
435 t->task = bL_switcher_thread_create(cpu, t);
438 bL_switcher_active = 1;
439 cpu_hotplug_driver_unlock();
441 pr_info("big.LITTLE switcher initialized\n");
447 static void bL_switcher_disable(void)
449 unsigned int cpu, cluster;
451 struct task_struct *task;
453 cpu_hotplug_driver_lock();
454 if (!bL_switcher_active) {
455 cpu_hotplug_driver_unlock();
458 bL_switcher_active = 0;
461 * To deactivate the switcher, we must shut down the switcher
462 * threads to prevent any other requests from being accepted.
463 * Then, if the final cluster for given logical CPU is not the
464 * same as the original one, we'll recreate a switcher thread
465 * just for the purpose of switching the CPU back without any
466 * possibility for interference from external requests.
468 for_each_online_cpu(cpu) {
469 t = &bL_threads[cpu];
472 if (!task || IS_ERR(task))
475 /* no more switch may happen on this CPU at this point */
476 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
477 if (cluster == bL_switcher_cpu_original_cluster[cpu])
479 init_completion(&t->started);
480 t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
481 task = bL_switcher_thread_create(cpu, t);
483 wait_for_completion(&t->started);
485 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
486 if (cluster == bL_switcher_cpu_original_cluster[cpu])
489 /* If execution gets here, we're in trouble. */
490 pr_crit("%s: unable to restore original cluster for CPU %d\n",
492 pr_crit("%s: CPU %d can't be restored\n",
493 __func__, bL_switcher_cpu_pairing[cpu]);
494 cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
495 &bL_switcher_removed_logical_cpus);
498 bL_switcher_restore_cpus();
499 cpu_hotplug_driver_unlock();
502 static ssize_t bL_switcher_active_show(struct kobject *kobj,
503 struct kobj_attribute *attr, char *buf)
505 return sprintf(buf, "%u\n", bL_switcher_active);
508 static ssize_t bL_switcher_active_store(struct kobject *kobj,
509 struct kobj_attribute *attr, const char *buf, size_t count)
515 bL_switcher_disable();
519 ret = bL_switcher_enable();
525 return (ret >= 0) ? count : ret;
528 static struct kobj_attribute bL_switcher_active_attr =
529 __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
531 static struct attribute *bL_switcher_attrs[] = {
532 &bL_switcher_active_attr.attr,
536 static struct attribute_group bL_switcher_attr_group = {
537 .attrs = bL_switcher_attrs,
540 static struct kobject *bL_switcher_kobj;
542 static int __init bL_switcher_sysfs_init(void)
546 bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
547 if (!bL_switcher_kobj)
549 ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
551 kobject_put(bL_switcher_kobj);
555 #endif /* CONFIG_SYSFS */
558 * Veto any CPU hotplug operation on those CPUs we've removed
559 * while the switcher is active.
560 * We're just not ready to deal with that given the trickery involved.
562 static int bL_switcher_hotplug_callback(struct notifier_block *nfb,
563 unsigned long action, void *hcpu)
565 if (bL_switcher_active) {
566 int pairing = bL_switcher_cpu_pairing[(unsigned long)hcpu];
567 switch (action & 0xf) {
569 case CPU_DOWN_PREPARE:
577 static bool no_bL_switcher;
578 core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
580 static int __init bL_switcher_init(void)
584 if (MAX_NR_CLUSTERS != 2) {
585 pr_err("%s: only dual cluster systems are supported\n", __func__);
589 cpu_notifier(bL_switcher_hotplug_callback, 0);
591 if (!no_bL_switcher) {
592 ret = bL_switcher_enable();
598 ret = bL_switcher_sysfs_init();
600 pr_err("%s: unable to create sysfs entry\n", __func__);
606 late_initcall(bL_switcher_init);