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>
30 #include <asm/smp_plat.h>
31 #include <asm/suspend.h>
33 #include <asm/bL_switcher.h>
37 * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
38 * __attribute_const__ and we don't want the compiler to assume any
39 * constness here as the value _does_ change along some code paths.
42 static int read_mpidr(void)
45 asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
46 return id & MPIDR_HWID_BITMASK;
50 * bL switcher core code.
53 static void bL_do_switch(void *_unused)
55 unsigned mpidr, cpuid, clusterid, ob_cluster, ib_cluster;
57 pr_debug("%s\n", __func__);
60 cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
61 clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
62 ob_cluster = clusterid;
63 ib_cluster = clusterid ^ 1;
66 * Our state has been saved at this point. Let's release our
69 mcpm_set_entry_vector(cpuid, ib_cluster, cpu_resume);
73 * From this point, we must assume that our counterpart CPU might
74 * have taken over in its parallel world already, as if execution
75 * just returned from cpu_suspend(). It is therefore important to
76 * be very careful not to make any change the other guy is not
77 * expecting. This is why we need stack isolation.
79 * Fancy under cover tasks could be performed here. For now
83 /* Let's put ourself down. */
84 mcpm_cpu_power_down();
86 /* should never get here */
91 * Stack isolation. To ensure 'current' remains valid, we just use another
92 * piece of our thread's stack space which should be fairly lightly used.
93 * The selected area starts just above the thread_info structure located
94 * at the very bottom of the stack, aligned to a cache line, and indexed
95 * with the cluster number.
97 #define STACK_SIZE 512
98 extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
99 static int bL_switchpoint(unsigned long _arg)
101 unsigned int mpidr = read_mpidr();
102 unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
103 void *stack = current_thread_info() + 1;
104 stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
105 stack += clusterid * STACK_SIZE + STACK_SIZE;
106 call_with_stack(bL_do_switch, (void *)_arg, stack);
111 * Generic switcher interface
114 static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
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, cpuid, clusterid, ob_cluster, ib_cluster, this_cpu;
126 struct tick_device *tdev;
127 enum clock_event_mode tdev_mode;
130 mpidr = read_mpidr();
131 cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
132 clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
133 ob_cluster = clusterid;
134 ib_cluster = clusterid ^ 1;
136 if (new_cluster_id == clusterid)
139 pr_debug("before switch: CPU %d in cluster %d\n", cpuid, clusterid);
141 /* Close the gate for our entry vectors */
142 mcpm_set_entry_vector(cpuid, ob_cluster, NULL);
143 mcpm_set_entry_vector(cpuid, ib_cluster, NULL);
146 * Let's wake up the inbound CPU now in case it requires some delay
147 * to come online, but leave it gated in our entry vector code.
149 ret = mcpm_cpu_power_up(cpuid, ib_cluster);
151 pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
156 * From this point we are entering the switch critical zone
157 * and can't take any interrupts anymore.
162 this_cpu = smp_processor_id();
164 /* redirect GIC's SGIs to our counterpart */
165 gic_migrate_target(bL_gic_id[cpuid][ib_cluster]);
168 * Raise a SGI on the inbound CPU to make sure it doesn't stall
169 * in a possible WFI, such as in mcpm_power_down().
171 arch_send_wakeup_ipi_mask(cpumask_of(this_cpu));
173 tdev = tick_get_device(this_cpu);
174 if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
177 tdev_mode = tdev->evtdev->mode;
178 clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
181 ret = cpu_pm_enter();
183 /* we can not tolerate errors at this point */
185 panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
187 /* Flip the cluster in the CPU logical map for this CPU. */
188 cpu_logical_map(this_cpu) ^= (1 << 8);
190 /* Let's do the actual CPU switch. */
191 ret = cpu_suspend(0, bL_switchpoint);
193 panic("%s: cpu_suspend() returned %d\n", __func__, ret);
195 /* We are executing on the inbound CPU at this point */
196 mpidr = read_mpidr();
197 cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
198 clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
199 pr_debug("after switch: CPU %d in cluster %d\n", cpuid, clusterid);
200 BUG_ON(clusterid != ib_cluster);
202 mcpm_cpu_powered_up();
207 clockevents_set_mode(tdev->evtdev, tdev_mode);
208 clockevents_program_event(tdev->evtdev,
209 tdev->evtdev->next_event, 1);
216 pr_err("%s exiting with error %d\n", __func__, ret);
221 struct task_struct *task;
222 wait_queue_head_t wq;
224 struct completion started;
227 static struct bL_thread bL_threads[NR_CPUS];
229 static int bL_switcher_thread(void *arg)
231 struct bL_thread *t = arg;
232 struct sched_param param = { .sched_priority = 1 };
235 sched_setscheduler_nocheck(current, SCHED_FIFO, ¶m);
236 complete(&t->started);
239 if (signal_pending(current))
240 flush_signals(current);
241 wait_event_interruptible(t->wq,
242 t->wanted_cluster != -1 ||
243 kthread_should_stop());
244 cluster = xchg(&t->wanted_cluster, -1);
246 bL_switch_to(cluster);
247 } while (!kthread_should_stop());
252 static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
254 struct task_struct *task;
256 task = kthread_create_on_node(bL_switcher_thread, arg,
257 cpu_to_node(cpu), "kswitcher_%d", cpu);
259 kthread_bind(task, cpu);
260 wake_up_process(task);
262 pr_err("%s failed for CPU %d\n", __func__, cpu);
267 * bL_switch_request - Switch to a specific cluster for the given CPU
269 * @cpu: the CPU to switch
270 * @new_cluster_id: the ID of the cluster to switch to.
272 * This function causes a cluster switch on the given CPU by waking up
273 * the appropriate switcher thread. This function may or may not return
274 * before the switch has occurred.
276 int bL_switch_request(unsigned int cpu, unsigned int new_cluster_id)
280 if (cpu >= ARRAY_SIZE(bL_threads)) {
281 pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
285 t = &bL_threads[cpu];
287 return PTR_ERR(t->task);
291 t->wanted_cluster = new_cluster_id;
295 EXPORT_SYMBOL_GPL(bL_switch_request);
298 * Activation and configuration code.
301 static unsigned int bL_switcher_active;
302 static unsigned int bL_switcher_cpu_original_cluster[MAX_CPUS_PER_CLUSTER];
303 static cpumask_t bL_switcher_removed_logical_cpus;
305 static void bL_switcher_restore_cpus(void)
309 for_each_cpu(i, &bL_switcher_removed_logical_cpus)
313 static int bL_switcher_halve_cpus(void)
315 int cpu, cluster, i, ret;
316 cpumask_t cluster_mask[2], common_mask;
318 cpumask_clear(&bL_switcher_removed_logical_cpus);
319 cpumask_clear(&cluster_mask[0]);
320 cpumask_clear(&cluster_mask[1]);
322 for_each_online_cpu(i) {
323 cpu = cpu_logical_map(i) & 0xff;
324 cluster = (cpu_logical_map(i) >> 8) & 0xff;
326 pr_err("%s: only dual cluster systems are supported\n", __func__);
329 cpumask_set_cpu(cpu, &cluster_mask[cluster]);
332 if (!cpumask_and(&common_mask, &cluster_mask[0], &cluster_mask[1])) {
333 pr_err("%s: no common set of CPUs\n", __func__);
337 for_each_online_cpu(i) {
338 cpu = cpu_logical_map(i) & 0xff;
339 cluster = (cpu_logical_map(i) >> 8) & 0xff;
341 if (cpumask_test_cpu(cpu, &common_mask)) {
342 /* Let's take note of the GIC ID for this CPU */
343 int gic_id = gic_get_cpu_id(i);
345 pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
348 bL_gic_id[cpu][cluster] = gic_id;
349 pr_info("GIC ID for CPU %u cluster %u is %u\n",
350 cpu, cluster, gic_id);
353 * We keep only those logical CPUs which number
354 * is equal to their physical CPU number. This is
355 * not perfect but good enough for now.
358 bL_switcher_cpu_original_cluster[cpu] = cluster;
365 bL_switcher_restore_cpus();
368 cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
374 static int bL_switcher_enable(void)
378 cpu_hotplug_driver_lock();
379 if (bL_switcher_active) {
380 cpu_hotplug_driver_unlock();
384 pr_info("big.LITTLE switcher initializing\n");
386 ret = bL_switcher_halve_cpus();
388 cpu_hotplug_driver_unlock();
392 for_each_online_cpu(cpu) {
393 struct bL_thread *t = &bL_threads[cpu];
394 init_waitqueue_head(&t->wq);
395 init_completion(&t->started);
396 t->wanted_cluster = -1;
397 t->task = bL_switcher_thread_create(cpu, t);
400 bL_switcher_active = 1;
401 cpu_hotplug_driver_unlock();
403 pr_info("big.LITTLE switcher initialized\n");
409 static void bL_switcher_disable(void)
411 unsigned int cpu, cluster, i;
413 struct task_struct *task;
415 cpu_hotplug_driver_lock();
416 if (!bL_switcher_active) {
417 cpu_hotplug_driver_unlock();
420 bL_switcher_active = 0;
423 * To deactivate the switcher, we must shut down the switcher
424 * threads to prevent any other requests from being accepted.
425 * Then, if the final cluster for given logical CPU is not the
426 * same as the original one, we'll recreate a switcher thread
427 * just for the purpose of switching the CPU back without any
428 * possibility for interference from external requests.
430 for_each_online_cpu(cpu) {
431 BUG_ON(cpu != (cpu_logical_map(cpu) & 0xff));
432 t = &bL_threads[cpu];
435 if (!task || IS_ERR(task))
438 /* no more switch may happen on this CPU at this point */
439 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
440 if (cluster == bL_switcher_cpu_original_cluster[cpu])
442 init_completion(&t->started);
443 t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
444 task = bL_switcher_thread_create(cpu, t);
446 wait_for_completion(&t->started);
448 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
449 if (cluster == bL_switcher_cpu_original_cluster[cpu])
452 /* If execution gets here, we're in trouble. */
453 pr_crit("%s: unable to restore original cluster for CPU %d\n",
455 for_each_cpu(i, &bL_switcher_removed_logical_cpus) {
456 if ((cpu_logical_map(i) & 0xff) != cpu)
458 pr_crit("%s: CPU %d can't be restored\n",
460 cpumask_clear_cpu(i, &bL_switcher_removed_logical_cpus);
465 bL_switcher_restore_cpus();
466 cpu_hotplug_driver_unlock();
469 static ssize_t bL_switcher_active_show(struct kobject *kobj,
470 struct kobj_attribute *attr, char *buf)
472 return sprintf(buf, "%u\n", bL_switcher_active);
475 static ssize_t bL_switcher_active_store(struct kobject *kobj,
476 struct kobj_attribute *attr, const char *buf, size_t count)
482 bL_switcher_disable();
486 ret = bL_switcher_enable();
492 return (ret >= 0) ? count : ret;
495 static struct kobj_attribute bL_switcher_active_attr =
496 __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
498 static struct attribute *bL_switcher_attrs[] = {
499 &bL_switcher_active_attr.attr,
503 static struct attribute_group bL_switcher_attr_group = {
504 .attrs = bL_switcher_attrs,
507 static struct kobject *bL_switcher_kobj;
509 static int __init bL_switcher_sysfs_init(void)
513 bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
514 if (!bL_switcher_kobj)
516 ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
518 kobject_put(bL_switcher_kobj);
522 #endif /* CONFIG_SYSFS */
524 static int __init bL_switcher_init(void)
528 if (MAX_NR_CLUSTERS != 2) {
529 pr_err("%s: only dual cluster systems are supported\n", __func__);
533 ret = bL_switcher_enable();
538 ret = bL_switcher_sysfs_init();
540 pr_err("%s: unable to create sysfs entry\n", __func__);
546 late_initcall(bL_switcher_init);