blob: 395f60f6292b209607a1bb21c54f064482e6f70d [file] [log] [blame]
Nicolas Pitre1c33be52012-04-12 02:56:10 -04001/*
2 * arch/arm/common/bL_switcher.c -- big.LITTLE cluster switcher core driver
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
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>
Nicolas Pitre71ce1de2012-10-26 02:36:17 -040018#include <linux/cpu.h>
Lorenzo Pieralisi3f09d472012-05-16 15:55:54 +010019#include <linux/cpumask.h>
Nicolas Pitre71ce1de2012-10-26 02:36:17 -040020#include <linux/kthread.h>
21#include <linux/wait.h>
Lorenzo Pieralisi3f09d472012-05-16 15:55:54 +010022#include <linux/clockchips.h>
23#include <linux/hrtimer.h>
24#include <linux/tick.h>
Nicolas Pitre1c33be52012-04-12 02:56:10 -040025#include <linux/mm.h>
26#include <linux/string.h>
Nicolas Pitre6b7437a2012-11-22 00:05:07 -050027#include <linux/sysfs.h>
Nicolas Pitre1c33be52012-04-12 02:56:10 -040028#include <linux/irqchip/arm-gic.h>
29
30#include <asm/smp_plat.h>
31#include <asm/suspend.h>
32#include <asm/mcpm.h>
33#include <asm/bL_switcher.h>
34
35
36/*
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.
40 */
41
42static int read_mpidr(void)
43{
44 unsigned int id;
45 asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
46 return id & MPIDR_HWID_BITMASK;
47}
48
49/*
50 * bL switcher core code.
51 */
52
53static void bL_do_switch(void *_unused)
54{
55 unsigned mpidr, cpuid, clusterid, ob_cluster, ib_cluster;
56
Nicolas Pitre1c33be52012-04-12 02:56:10 -040057 pr_debug("%s\n", __func__);
58
59 mpidr = read_mpidr();
60 cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
61 clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
62 ob_cluster = clusterid;
63 ib_cluster = clusterid ^ 1;
64
65 /*
66 * Our state has been saved at this point. Let's release our
67 * inbound CPU.
68 */
69 mcpm_set_entry_vector(cpuid, ib_cluster, cpu_resume);
70 sev();
71
72 /*
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.
78 *
79 * Fancy under cover tasks could be performed here. For now
80 * we have none.
81 */
82
83 /* Let's put ourself down. */
84 mcpm_cpu_power_down();
85
86 /* should never get here */
87 BUG();
88}
89
90/*
Nicolas Pitrec052de22012-11-27 15:55:33 -050091 * 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.
Nicolas Pitre1c33be52012-04-12 02:56:10 -040096 */
Nicolas Pitrec052de22012-11-27 15:55:33 -050097#define STACK_SIZE 512
Nicolas Pitre1c33be52012-04-12 02:56:10 -040098extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
99static int bL_switchpoint(unsigned long _arg)
100{
101 unsigned int mpidr = read_mpidr();
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400102 unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
Nicolas Pitrec052de22012-11-27 15:55:33 -0500103 void *stack = current_thread_info() + 1;
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400104 stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
Nicolas Pitrec052de22012-11-27 15:55:33 -0500105 stack += clusterid * STACK_SIZE + STACK_SIZE;
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400106 call_with_stack(bL_do_switch, (void *)_arg, stack);
107 BUG();
108}
109
110/*
111 * Generic switcher interface
112 */
113
Nicolas Pitreed967622012-07-05 21:33:26 -0400114static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
115
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400116/*
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.
119 *
120 * This function must be called on the CPU to be switched.
121 * Returns 0 on success, else a negative status code.
122 */
123static int bL_switch_to(unsigned int new_cluster_id)
124{
125 unsigned int mpidr, cpuid, clusterid, ob_cluster, ib_cluster, this_cpu;
Lorenzo Pieralisi3f09d472012-05-16 15:55:54 +0100126 struct tick_device *tdev;
127 enum clock_event_mode tdev_mode;
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400128 int ret;
129
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;
135
136 if (new_cluster_id == clusterid)
137 return 0;
138
139 pr_debug("before switch: CPU %d in cluster %d\n", cpuid, clusterid);
140
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);
144
145 /*
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.
148 */
149 ret = mcpm_cpu_power_up(cpuid, ib_cluster);
150 if (ret) {
151 pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
152 return ret;
153 }
154
155 /*
156 * From this point we are entering the switch critical zone
157 * and can't take any interrupts anymore.
158 */
159 local_irq_disable();
160 local_fiq_disable();
161
162 this_cpu = smp_processor_id();
163
164 /* redirect GIC's SGIs to our counterpart */
Nicolas Pitreed967622012-07-05 21:33:26 -0400165 gic_migrate_target(bL_gic_id[cpuid][ib_cluster]);
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400166
167 /*
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().
170 */
171 arch_send_wakeup_ipi_mask(cpumask_of(this_cpu));
172
Lorenzo Pieralisi3f09d472012-05-16 15:55:54 +0100173 tdev = tick_get_device(this_cpu);
174 if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
175 tdev = NULL;
176 if (tdev) {
177 tdev_mode = tdev->evtdev->mode;
178 clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
179 }
180
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400181 ret = cpu_pm_enter();
182
183 /* we can not tolerate errors at this point */
184 if (ret)
185 panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
186
187 /* Flip the cluster in the CPU logical map for this CPU. */
188 cpu_logical_map(this_cpu) ^= (1 << 8);
189
190 /* Let's do the actual CPU switch. */
191 ret = cpu_suspend(0, bL_switchpoint);
192 if (ret > 0)
193 panic("%s: cpu_suspend() returned %d\n", __func__, ret);
194
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);
201
202 mcpm_cpu_powered_up();
203
204 ret = cpu_pm_exit();
205
Lorenzo Pieralisi3f09d472012-05-16 15:55:54 +0100206 if (tdev) {
207 clockevents_set_mode(tdev->evtdev, tdev_mode);
208 clockevents_program_event(tdev->evtdev,
209 tdev->evtdev->next_event, 1);
210 }
211
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400212 local_fiq_enable();
213 local_irq_enable();
214
215 if (ret)
216 pr_err("%s exiting with error %d\n", __func__, ret);
217 return ret;
218}
219
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400220struct bL_thread {
221 struct task_struct *task;
222 wait_queue_head_t wq;
223 int wanted_cluster;
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500224 struct completion started;
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400225};
226
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400227static struct bL_thread bL_threads[NR_CPUS];
228
229static int bL_switcher_thread(void *arg)
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400230{
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400231 struct bL_thread *t = arg;
232 struct sched_param param = { .sched_priority = 1 };
233 int cluster;
234
235 sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500236 complete(&t->started);
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400237
238 do {
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);
245 if (cluster != -1)
246 bL_switch_to(cluster);
247 } while (!kthread_should_stop());
248
249 return 0;
250}
251
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500252static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400253{
254 struct task_struct *task;
255
256 task = kthread_create_on_node(bL_switcher_thread, arg,
257 cpu_to_node(cpu), "kswitcher_%d", cpu);
258 if (!IS_ERR(task)) {
259 kthread_bind(task, cpu);
260 wake_up_process(task);
261 } else
262 pr_err("%s failed for CPU %d\n", __func__, cpu);
263 return task;
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400264}
265
266/*
267 * bL_switch_request - Switch to a specific cluster for the given CPU
268 *
269 * @cpu: the CPU to switch
270 * @new_cluster_id: the ID of the cluster to switch to.
271 *
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400272 * 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.
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400275 */
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400276int bL_switch_request(unsigned int cpu, unsigned int new_cluster_id)
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400277{
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400278 struct bL_thread *t;
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400279
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400280 if (cpu >= ARRAY_SIZE(bL_threads)) {
281 pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
282 return -EINVAL;
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400283 }
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400284
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400285 t = &bL_threads[cpu];
286 if (IS_ERR(t->task))
287 return PTR_ERR(t->task);
288 if (!t->task)
289 return -ESRCH;
290
291 t->wanted_cluster = new_cluster_id;
292 wake_up(&t->wq);
293 return 0;
Nicolas Pitre1c33be52012-04-12 02:56:10 -0400294}
295EXPORT_SYMBOL_GPL(bL_switch_request);
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400296
Nicolas Pitre9797a0e2012-11-21 11:53:27 -0500297/*
298 * Activation and configuration code.
299 */
300
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500301static unsigned int bL_switcher_active;
302static unsigned int bL_switcher_cpu_original_cluster[MAX_CPUS_PER_CLUSTER];
Nicolas Pitre9797a0e2012-11-21 11:53:27 -0500303static cpumask_t bL_switcher_removed_logical_cpus;
304
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500305static void bL_switcher_restore_cpus(void)
Nicolas Pitre9797a0e2012-11-21 11:53:27 -0500306{
307 int i;
308
309 for_each_cpu(i, &bL_switcher_removed_logical_cpus)
310 cpu_up(i);
311}
312
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500313static int bL_switcher_halve_cpus(void)
Nicolas Pitre9797a0e2012-11-21 11:53:27 -0500314{
315 int cpu, cluster, i, ret;
316 cpumask_t cluster_mask[2], common_mask;
317
318 cpumask_clear(&bL_switcher_removed_logical_cpus);
319 cpumask_clear(&cluster_mask[0]);
320 cpumask_clear(&cluster_mask[1]);
321
322 for_each_online_cpu(i) {
323 cpu = cpu_logical_map(i) & 0xff;
324 cluster = (cpu_logical_map(i) >> 8) & 0xff;
325 if (cluster >= 2) {
326 pr_err("%s: only dual cluster systems are supported\n", __func__);
327 return -EINVAL;
328 }
329 cpumask_set_cpu(cpu, &cluster_mask[cluster]);
330 }
331
332 if (!cpumask_and(&common_mask, &cluster_mask[0], &cluster_mask[1])) {
333 pr_err("%s: no common set of CPUs\n", __func__);
334 return -EINVAL;
335 }
336
337 for_each_online_cpu(i) {
338 cpu = cpu_logical_map(i) & 0xff;
339 cluster = (cpu_logical_map(i) >> 8) & 0xff;
340
341 if (cpumask_test_cpu(cpu, &common_mask)) {
Nicolas Pitreed967622012-07-05 21:33:26 -0400342 /* Let's take note of the GIC ID for this CPU */
343 int gic_id = gic_get_cpu_id(i);
344 if (gic_id < 0) {
345 pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
346 return -EINVAL;
347 }
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);
351
Nicolas Pitre9797a0e2012-11-21 11:53:27 -0500352 /*
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.
356 */
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500357 if (cpu == i) {
358 bL_switcher_cpu_original_cluster[cpu] = cluster;
Nicolas Pitre9797a0e2012-11-21 11:53:27 -0500359 continue;
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500360 }
Nicolas Pitre9797a0e2012-11-21 11:53:27 -0500361 }
362
363 ret = cpu_down(i);
364 if (ret) {
365 bL_switcher_restore_cpus();
366 return ret;
367 }
368 cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
369 }
370
371 return 0;
372}
373
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500374static int bL_switcher_enable(void)
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400375{
Nicolas Pitre9797a0e2012-11-21 11:53:27 -0500376 int cpu, ret;
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400377
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500378 cpu_hotplug_driver_lock();
379 if (bL_switcher_active) {
380 cpu_hotplug_driver_unlock();
381 return 0;
Nicolas Pitre9797a0e2012-11-21 11:53:27 -0500382 }
383
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500384 pr_info("big.LITTLE switcher initializing\n");
385
Nicolas Pitre9797a0e2012-11-21 11:53:27 -0500386 ret = bL_switcher_halve_cpus();
387 if (ret) {
388 cpu_hotplug_driver_unlock();
389 return ret;
390 }
391
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400392 for_each_online_cpu(cpu) {
393 struct bL_thread *t = &bL_threads[cpu];
394 init_waitqueue_head(&t->wq);
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500395 init_completion(&t->started);
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400396 t->wanted_cluster = -1;
397 t->task = bL_switcher_thread_create(cpu, t);
398 }
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500399
400 bL_switcher_active = 1;
Nicolas Pitre9797a0e2012-11-21 11:53:27 -0500401 cpu_hotplug_driver_unlock();
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400402
403 pr_info("big.LITTLE switcher initialized\n");
404 return 0;
405}
406
Nicolas Pitre6b7437a2012-11-22 00:05:07 -0500407#ifdef CONFIG_SYSFS
408
409static void bL_switcher_disable(void)
410{
411 unsigned int cpu, cluster, i;
412 struct bL_thread *t;
413 struct task_struct *task;
414
415 cpu_hotplug_driver_lock();
416 if (!bL_switcher_active) {
417 cpu_hotplug_driver_unlock();
418 return;
419 }
420 bL_switcher_active = 0;
421
422 /*
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.
429 */
430 for_each_online_cpu(cpu) {
431 BUG_ON(cpu != (cpu_logical_map(cpu) & 0xff));
432 t = &bL_threads[cpu];
433 task = t->task;
434 t->task = NULL;
435 if (!task || IS_ERR(task))
436 continue;
437 kthread_stop(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])
441 continue;
442 init_completion(&t->started);
443 t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
444 task = bL_switcher_thread_create(cpu, t);
445 if (!IS_ERR(task)) {
446 wait_for_completion(&t->started);
447 kthread_stop(task);
448 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
449 if (cluster == bL_switcher_cpu_original_cluster[cpu])
450 continue;
451 }
452 /* If execution gets here, we're in trouble. */
453 pr_crit("%s: unable to restore original cluster for CPU %d\n",
454 __func__, cpu);
455 for_each_cpu(i, &bL_switcher_removed_logical_cpus) {
456 if ((cpu_logical_map(i) & 0xff) != cpu)
457 continue;
458 pr_crit("%s: CPU %d can't be restored\n",
459 __func__, i);
460 cpumask_clear_cpu(i, &bL_switcher_removed_logical_cpus);
461 break;
462 }
463 }
464
465 bL_switcher_restore_cpus();
466 cpu_hotplug_driver_unlock();
467}
468
469static ssize_t bL_switcher_active_show(struct kobject *kobj,
470 struct kobj_attribute *attr, char *buf)
471{
472 return sprintf(buf, "%u\n", bL_switcher_active);
473}
474
475static ssize_t bL_switcher_active_store(struct kobject *kobj,
476 struct kobj_attribute *attr, const char *buf, size_t count)
477{
478 int ret;
479
480 switch (buf[0]) {
481 case '0':
482 bL_switcher_disable();
483 ret = 0;
484 break;
485 case '1':
486 ret = bL_switcher_enable();
487 break;
488 default:
489 ret = -EINVAL;
490 }
491
492 return (ret >= 0) ? count : ret;
493}
494
495static struct kobj_attribute bL_switcher_active_attr =
496 __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
497
498static struct attribute *bL_switcher_attrs[] = {
499 &bL_switcher_active_attr.attr,
500 NULL,
501};
502
503static struct attribute_group bL_switcher_attr_group = {
504 .attrs = bL_switcher_attrs,
505};
506
507static struct kobject *bL_switcher_kobj;
508
509static int __init bL_switcher_sysfs_init(void)
510{
511 int ret;
512
513 bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
514 if (!bL_switcher_kobj)
515 return -ENOMEM;
516 ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
517 if (ret)
518 kobject_put(bL_switcher_kobj);
519 return ret;
520}
521
522#endif /* CONFIG_SYSFS */
523
524static int __init bL_switcher_init(void)
525{
526 int ret;
527
528 if (MAX_NR_CLUSTERS != 2) {
529 pr_err("%s: only dual cluster systems are supported\n", __func__);
530 return -EINVAL;
531 }
532
533 ret = bL_switcher_enable();
534 if (ret)
535 return ret;
536
537#ifdef CONFIG_SYSFS
538 ret = bL_switcher_sysfs_init();
539 if (ret)
540 pr_err("%s: unable to create sysfs entry\n", __func__);
541#endif
542
543 return 0;
544}
545
Nicolas Pitre71ce1de2012-10-26 02:36:17 -0400546late_initcall(bL_switcher_init);