Merge branch 'linux-linaro-lsk' into linux-linaro-lsk-android
[firefly-linux-kernel-4.4.55.git] / arch / arm / common / bL_switcher.c
1 /*
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  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/atomic.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/interrupt.h>
18 #include <linux/cpu_pm.h>
19 #include <linux/cpu.h>
20 #include <linux/cpumask.h>
21 #include <linux/kthread.h>
22 #include <linux/wait.h>
23 #include <linux/time.h>
24 #include <linux/clockchips.h>
25 #include <linux/hrtimer.h>
26 #include <linux/tick.h>
27 #include <linux/notifier.h>
28 #include <linux/mm.h>
29 #include <linux/mutex.h>
30 #include <linux/smp.h>
31 #include <linux/spinlock.h>
32 #include <linux/string.h>
33 #include <linux/sysfs.h>
34 #include <linux/irqchip/arm-gic.h>
35 #include <linux/moduleparam.h>
36
37 #include <asm/smp_plat.h>
38 #include <asm/cacheflush.h>
39 #include <asm/cputype.h>
40 #include <asm/suspend.h>
41 #include <asm/mcpm.h>
42 #include <asm/bL_switcher.h>
43
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/power_cpu_migrate.h>
46
47
48 /*
49  * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
50  * __attribute_const__ and we don't want the compiler to assume any
51  * constness here as the value _does_ change along some code paths.
52  */
53
54 static int read_mpidr(void)
55 {
56         unsigned int id;
57         asm volatile ("mrc\tp15, 0, %0, c0, c0, 5" : "=r" (id));
58         return id & MPIDR_HWID_BITMASK;
59 }
60
61 /*
62  * Get a global nanosecond time stamp for tracing.
63  */
64 static s64 get_ns(void)
65 {
66         struct timespec ts;
67         getnstimeofday(&ts);
68         return timespec_to_ns(&ts);
69 }
70
71 /*
72  * bL switcher core code.
73  */
74
75 static void bL_do_switch(void *_arg)
76 {
77         unsigned ib_mpidr, ib_cpu, ib_cluster;
78         long volatile handshake, **handshake_ptr = _arg;
79
80         pr_debug("%s\n", __func__);
81
82         ib_mpidr = cpu_logical_map(smp_processor_id());
83         ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
84         ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
85
86         /* Advertise our handshake location */
87         if (handshake_ptr) {
88                 handshake = 0;
89                 *handshake_ptr = &handshake;
90         } else
91                 handshake = -1;
92
93         /*
94          * Our state has been saved at this point.  Let's release our
95          * inbound CPU.
96          */
97         mcpm_set_entry_vector(ib_cpu, ib_cluster, cpu_resume);
98         sev();
99
100         /*
101          * From this point, we must assume that our counterpart CPU might
102          * have taken over in its parallel world already, as if execution
103          * just returned from cpu_suspend().  It is therefore important to
104          * be very careful not to make any change the other guy is not
105          * expecting.  This is why we need stack isolation.
106          *
107          * Fancy under cover tasks could be performed here.  For now
108          * we have none.
109          */
110
111         /*
112          * Let's wait until our inbound is alive.
113          */
114         while (!handshake) {
115                 wfe();
116                 smp_mb();
117         }
118
119         /* Let's put ourself down. */
120         mcpm_cpu_power_down();
121
122         /* should never get here */
123         BUG();
124 }
125
126 /*
127  * Stack isolation.  To ensure 'current' remains valid, we just use another
128  * piece of our thread's stack space which should be fairly lightly used.
129  * The selected area starts just above the thread_info structure located
130  * at the very bottom of the stack, aligned to a cache line, and indexed
131  * with the cluster number.
132  */
133 #define STACK_SIZE 512
134 extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
135 static int bL_switchpoint(unsigned long _arg)
136 {
137         unsigned int mpidr = read_mpidr();
138         unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
139         void *stack = current_thread_info() + 1;
140         stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
141         stack += clusterid * STACK_SIZE + STACK_SIZE;
142         call_with_stack(bL_do_switch, (void *)_arg, stack);
143         BUG();
144 }
145
146 /*
147  * Generic switcher interface
148  */
149
150 static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
151 static int bL_switcher_cpu_pairing[NR_CPUS];
152
153 /*
154  * bL_switch_to - Switch to a specific cluster for the current CPU
155  * @new_cluster_id: the ID of the cluster to switch to.
156  *
157  * This function must be called on the CPU to be switched.
158  * Returns 0 on success, else a negative status code.
159  */
160 static int bL_switch_to(unsigned int new_cluster_id)
161 {
162         unsigned int mpidr, this_cpu, that_cpu;
163         unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster;
164         struct completion inbound_alive;
165         struct tick_device *tdev;
166         enum clock_event_mode tdev_mode;
167         long volatile *handshake_ptr;
168         int ipi_nr, ret;
169
170         this_cpu = smp_processor_id();
171         ob_mpidr = read_mpidr();
172         ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0);
173         ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1);
174         BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr);
175
176         if (new_cluster_id == ob_cluster)
177                 return 0;
178
179         that_cpu = bL_switcher_cpu_pairing[this_cpu];
180         ib_mpidr = cpu_logical_map(that_cpu);
181         ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
182         ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
183
184         pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
185                  this_cpu, ob_mpidr, ib_mpidr);
186
187         this_cpu = smp_processor_id();
188
189         /* Close the gate for our entry vectors */
190         mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
191         mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
192
193         /* Install our "inbound alive" notifier. */
194         init_completion(&inbound_alive);
195         ipi_nr = register_ipi_completion(&inbound_alive, this_cpu);
196         ipi_nr |= ((1 << 16) << bL_gic_id[ob_cpu][ob_cluster]);
197         mcpm_set_early_poke(ib_cpu, ib_cluster, gic_get_sgir_physaddr(), ipi_nr);
198
199         /*
200          * Let's wake up the inbound CPU now in case it requires some delay
201          * to come online, but leave it gated in our entry vector code.
202          */
203         ret = mcpm_cpu_power_up(ib_cpu, ib_cluster);
204         if (ret) {
205                 pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
206                 return ret;
207         }
208
209         /*
210          * Raise a SGI on the inbound CPU to make sure it doesn't stall
211          * in a possible WFI, such as in bL_power_down().
212          */
213         gic_send_sgi(bL_gic_id[ib_cpu][ib_cluster], 0);
214
215         /*
216          * Wait for the inbound to come up.  This allows for other
217          * tasks to be scheduled in the mean time.
218          */
219         wait_for_completion(&inbound_alive);
220         mcpm_set_early_poke(ib_cpu, ib_cluster, 0, 0);
221
222         /*
223          * From this point we are entering the switch critical zone
224          * and can't sleep/schedule anymore.
225          */
226         local_irq_disable();
227         local_fiq_disable();
228         trace_cpu_migrate_begin(get_ns(), ob_mpidr);
229
230         /* redirect GIC's SGIs to our counterpart */
231         gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
232
233         tdev = tick_get_device(this_cpu);
234         if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
235                 tdev = NULL;
236         if (tdev) {
237                 tdev_mode = tdev->evtdev->mode;
238                 clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
239         }
240
241         ret = cpu_pm_enter();
242
243         /* we can not tolerate errors at this point */
244         if (ret)
245                 panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
246
247         /*
248          * Swap the physical CPUs in the logical map for this logical CPU.
249          * This must be flushed to RAM as the resume code
250          * needs to access it while the caches are still disabled.
251          */
252         cpu_logical_map(this_cpu) = ib_mpidr;
253         cpu_logical_map(that_cpu) = ob_mpidr;
254         sync_cache_w(&cpu_logical_map(this_cpu));
255
256         /* Let's do the actual CPU switch. */
257         ret = cpu_suspend((unsigned long)&handshake_ptr, bL_switchpoint);
258         if (ret > 0)
259                 panic("%s: cpu_suspend() returned %d\n", __func__, ret);
260
261         /* We are executing on the inbound CPU at this point */
262         mpidr = read_mpidr();
263         pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr);
264         BUG_ON(mpidr != ib_mpidr);
265
266         mcpm_cpu_powered_up();
267
268         ret = cpu_pm_exit();
269
270         if (tdev) {
271                 clockevents_set_mode(tdev->evtdev, tdev_mode);
272                 clockevents_program_event(tdev->evtdev,
273                                           tdev->evtdev->next_event, 1);
274         }
275
276         trace_cpu_migrate_finish(get_ns(), ib_mpidr);
277         local_fiq_enable();
278         local_irq_enable();
279
280         *handshake_ptr = 1;
281         dsb_sev();
282
283         if (ret)
284                 pr_err("%s exiting with error %d\n", __func__, ret);
285         return ret;
286 }
287
288 struct bL_thread {
289         spinlock_t lock;
290         struct task_struct *task;
291         wait_queue_head_t wq;
292         int wanted_cluster;
293         struct completion started;
294         bL_switch_completion_handler completer;
295         void *completer_cookie;
296 };
297
298 static struct bL_thread bL_threads[NR_CPUS];
299
300 static int bL_switcher_thread(void *arg)
301 {
302         struct bL_thread *t = arg;
303         struct sched_param param = { .sched_priority = 1 };
304         int cluster;
305         bL_switch_completion_handler completer;
306         void *completer_cookie;
307
308         sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
309         complete(&t->started);
310
311         do {
312                 if (signal_pending(current))
313                         flush_signals(current);
314                 wait_event_interruptible(t->wq,
315                                 t->wanted_cluster != -1 ||
316                                 kthread_should_stop());
317
318                 spin_lock(&t->lock);
319                 cluster = t->wanted_cluster;
320                 completer = t->completer;
321                 completer_cookie = t->completer_cookie;
322                 t->wanted_cluster = -1;
323                 t->completer = NULL;
324                 spin_unlock(&t->lock);
325
326                 if (cluster != -1) {
327                         bL_switch_to(cluster);
328
329                         if (completer)
330                                 completer(completer_cookie);
331                 }
332         } while (!kthread_should_stop());
333
334         return 0;
335 }
336
337 static struct task_struct * bL_switcher_thread_create(int cpu, void *arg)
338 {
339         struct task_struct *task;
340
341         task = kthread_create_on_node(bL_switcher_thread, arg,
342                                       cpu_to_node(cpu), "kswitcher_%d", cpu);
343         if (!IS_ERR(task)) {
344                 kthread_bind(task, cpu);
345                 wake_up_process(task);
346         } else
347                 pr_err("%s failed for CPU %d\n", __func__, cpu);
348         return task;
349 }
350
351 /*
352  * bL_switch_request_cb - Switch to a specific cluster for the given CPU,
353  *      with completion notification via a callback
354  *
355  * @cpu: the CPU to switch
356  * @new_cluster_id: the ID of the cluster to switch to.
357  * @completer: switch completion callback.  if non-NULL,
358  *      @completer(@completer_cookie) will be called on completion of
359  *      the switch, in non-atomic context.
360  * @completer_cookie: opaque context argument for @completer.
361  *
362  * This function causes a cluster switch on the given CPU by waking up
363  * the appropriate switcher thread.  This function may or may not return
364  * before the switch has occurred.
365  *
366  * If a @completer callback function is supplied, it will be called when
367  * the switch is complete.  This can be used to determine asynchronously
368  * when the switch is complete, regardless of when bL_switch_request()
369  * returns.  When @completer is supplied, no new switch request is permitted
370  * for the affected CPU until after the switch is complete, and @completer
371  * has returned.
372  */
373 int bL_switch_request_cb(unsigned int cpu, unsigned int new_cluster_id,
374                          bL_switch_completion_handler completer,
375                          void *completer_cookie)
376 {
377         struct bL_thread *t;
378
379         if (cpu >= ARRAY_SIZE(bL_threads)) {
380                 pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
381                 return -EINVAL;
382         }
383
384         t = &bL_threads[cpu];
385
386         if (IS_ERR(t->task))
387                 return PTR_ERR(t->task);
388         if (!t->task)
389                 return -ESRCH;
390
391         spin_lock(&t->lock);
392         if (t->completer) {
393                 spin_unlock(&t->lock);
394                 return -EBUSY;
395         }
396         t->completer = completer;
397         t->completer_cookie = completer_cookie;
398         t->wanted_cluster = new_cluster_id;
399         spin_unlock(&t->lock);
400         wake_up(&t->wq);
401         return 0;
402 }
403
404 EXPORT_SYMBOL_GPL(bL_switch_request_cb);
405
406 /*
407  * Detach an outstanding switch request.
408  *
409  * The switcher will continue with the switch request in the background,
410  * but the completer function will not be called.
411  *
412  * This may be necessary if the completer is in a kernel module which is
413  * about to be unloaded.
414  */
415 void bL_switch_request_detach(unsigned int cpu,
416                               bL_switch_completion_handler completer)
417 {
418         struct bL_thread *t;
419
420         if (cpu >= ARRAY_SIZE(bL_threads)) {
421                 pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
422                 return;
423         }
424
425         t = &bL_threads[cpu];
426
427         if (IS_ERR(t->task) || !t->task)
428                 return;
429
430         spin_lock(&t->lock);
431         if (t->completer == completer)
432                 t->completer = NULL;
433         spin_unlock(&t->lock);
434 }
435
436 EXPORT_SYMBOL_GPL(bL_switch_request_detach);
437
438 /*
439  * Activation and configuration code.
440  */
441
442 static DEFINE_MUTEX(bL_switcher_activation_lock);
443 static BLOCKING_NOTIFIER_HEAD(bL_activation_notifier);
444 static unsigned int bL_switcher_active;
445 static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
446 static cpumask_t bL_switcher_removed_logical_cpus;
447
448 int bL_switcher_register_notifier(struct notifier_block *nb)
449 {
450         return blocking_notifier_chain_register(&bL_activation_notifier, nb);
451 }
452 EXPORT_SYMBOL_GPL(bL_switcher_register_notifier);
453
454 int bL_switcher_unregister_notifier(struct notifier_block *nb)
455 {
456         return blocking_notifier_chain_unregister(&bL_activation_notifier, nb);
457 }
458 EXPORT_SYMBOL_GPL(bL_switcher_unregister_notifier);
459
460 static int bL_activation_notify(unsigned long val)
461 {
462         int ret;
463        
464         ret = blocking_notifier_call_chain(&bL_activation_notifier, val, NULL);
465         if (ret & NOTIFY_STOP_MASK)
466                 pr_err("%s: notifier chain failed with status 0x%x\n",
467                         __func__, ret);
468         return notifier_to_errno(ret);
469 }
470
471 static void bL_switcher_restore_cpus(void)
472 {
473         int i;
474
475         for_each_cpu(i, &bL_switcher_removed_logical_cpus)
476                 cpu_up(i);
477 }
478
479 static int bL_switcher_halve_cpus(void)
480 {
481         int i, j, cluster_0, gic_id, ret;
482         unsigned int cpu, cluster, mask;
483         cpumask_t available_cpus;
484
485         /* First pass to validate what we have */
486         mask = 0;
487         for_each_online_cpu(i) {
488                 cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
489                 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
490                 if (cluster >= 2) {
491                         pr_err("%s: only dual cluster systems are supported\n", __func__);
492                         return -EINVAL;
493                 }
494                 if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
495                         return -EINVAL;
496                 mask |= (1 << cluster);
497         }
498         if (mask != 3) {
499                 pr_err("%s: no CPU pairing possible\n", __func__);
500                 return -EINVAL;
501         }
502
503         /*
504          * Now let's do the pairing.  We match each CPU with another CPU
505          * from a different cluster.  To get a uniform scheduling behavior
506          * without fiddling with CPU topology and compute capacity data,
507          * we'll use logical CPUs initially belonging to the same cluster.
508          */
509         memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
510         cpumask_copy(&available_cpus, cpu_online_mask);
511         cluster_0 = -1;
512         for_each_cpu(i, &available_cpus) {
513                 int match = -1;
514                 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
515                 if (cluster_0 == -1)
516                         cluster_0 = cluster;
517                 if (cluster != cluster_0)
518                         continue;
519                 cpumask_clear_cpu(i, &available_cpus);
520                 for_each_cpu(j, &available_cpus) {
521                         cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
522                         /*
523                          * Let's remember the last match to create "odd"
524                          * pairing on purpose in order for other code not
525                          * to assume any relation between physical and
526                          * logical CPU numbers.
527                          */
528                         if (cluster != cluster_0)
529                                 match = j;
530                 }
531                 if (match != -1) {
532                         bL_switcher_cpu_pairing[i] = match;
533                         cpumask_clear_cpu(match, &available_cpus);
534                         pr_info("CPU%d paired with CPU%d\n", i, match);
535                 }
536         }
537
538         /*
539          * Now we disable the unwanted CPUs i.e. everything that has no
540          * pairing information (that includes the pairing counterparts).
541          */ 
542         cpumask_clear(&bL_switcher_removed_logical_cpus);
543         for_each_online_cpu(i) {
544                 cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
545                 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
546
547                 /* Let's take note of the GIC ID for this CPU */
548                 gic_id = gic_get_cpu_id(i);
549                 if (gic_id < 0) {
550                         pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
551                         bL_switcher_restore_cpus();
552                         return -EINVAL;
553                 }
554                 bL_gic_id[cpu][cluster] = gic_id;
555                 pr_info("GIC ID for CPU %u cluster %u is %u\n",
556                         cpu, cluster, gic_id);
557
558                 if (bL_switcher_cpu_pairing[i] != -1) {
559                         bL_switcher_cpu_original_cluster[i] = cluster;
560                         continue;
561                 }
562
563                 ret = cpu_down(i);
564                 if (ret) {
565                         bL_switcher_restore_cpus();
566                         return ret;
567                 }
568                 cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
569         }
570
571         return 0;
572 }
573
574 /* Determine the logical CPU a given physical CPU is grouped on. */
575 int bL_switcher_get_logical_index(u32 mpidr)
576 {
577         int cpu;
578
579         if (!bL_switcher_active)
580                 return -EUNATCH;
581
582         mpidr &= MPIDR_HWID_BITMASK;
583         for_each_online_cpu(cpu) {
584                 int pairing = bL_switcher_cpu_pairing[cpu];
585                 if (pairing == -1)
586                         continue;
587                 if ((mpidr == cpu_logical_map(cpu)) ||
588                     (mpidr == cpu_logical_map(pairing)))
589                         return cpu;
590         }
591         return -EINVAL;
592 }
593
594 static void bL_switcher_trace_trigger_cpu(void *__always_unused info)
595 {
596         trace_cpu_migrate_current(get_ns(), read_mpidr());
597 }
598
599 int bL_switcher_trace_trigger(void)
600 {
601         int ret;
602
603         preempt_disable();
604
605         bL_switcher_trace_trigger_cpu(NULL);
606         ret = smp_call_function(bL_switcher_trace_trigger_cpu, NULL, true);
607
608         preempt_enable();
609
610         return ret;
611 }
612 EXPORT_SYMBOL_GPL(bL_switcher_trace_trigger);
613
614 static int bL_switcher_enable(void)
615 {
616         int cpu, ret;
617
618         mutex_lock(&bL_switcher_activation_lock);
619         cpu_hotplug_driver_lock();
620         if (bL_switcher_active) {
621                 cpu_hotplug_driver_unlock();
622                 mutex_unlock(&bL_switcher_activation_lock);
623                 return 0;
624         }
625
626         pr_info("big.LITTLE switcher initializing\n");
627
628         ret = bL_activation_notify(BL_NOTIFY_PRE_ENABLE);
629         if (ret)
630                 goto error;
631
632         ret = bL_switcher_halve_cpus();
633         if (ret)
634                 goto error;
635
636         bL_switcher_trace_trigger();
637
638         for_each_online_cpu(cpu) {
639                 struct bL_thread *t = &bL_threads[cpu];
640                 spin_lock_init(&t->lock);
641                 init_waitqueue_head(&t->wq);
642                 init_completion(&t->started);
643                 t->wanted_cluster = -1;
644                 t->task = bL_switcher_thread_create(cpu, t);
645         }
646
647         bL_switcher_active = 1;
648         bL_activation_notify(BL_NOTIFY_POST_ENABLE);
649         pr_info("big.LITTLE switcher initialized\n");
650         goto out;
651
652 error:
653         pr_warning("big.LITTLE switcher initialization failed\n");
654         bL_activation_notify(BL_NOTIFY_POST_DISABLE);
655
656 out:
657         cpu_hotplug_driver_unlock();
658         mutex_unlock(&bL_switcher_activation_lock);
659         return ret;
660 }
661
662 #ifdef CONFIG_SYSFS
663
664 static void bL_switcher_disable(void)
665 {
666         unsigned int cpu, cluster;
667         struct bL_thread *t;
668         struct task_struct *task;
669
670         mutex_lock(&bL_switcher_activation_lock);
671         cpu_hotplug_driver_lock();
672
673         if (!bL_switcher_active)
674                 goto out;
675
676         if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE) != 0) {
677                 bL_activation_notify(BL_NOTIFY_POST_ENABLE);
678                 goto out;
679         }
680
681         bL_switcher_active = 0;
682
683         /*
684          * To deactivate the switcher, we must shut down the switcher
685          * threads to prevent any other requests from being accepted.
686          * Then, if the final cluster for given logical CPU is not the
687          * same as the original one, we'll recreate a switcher thread
688          * just for the purpose of switching the CPU back without any
689          * possibility for interference from external requests.
690          */
691         for_each_online_cpu(cpu) {
692                 t = &bL_threads[cpu];
693                 task = t->task;
694                 t->task = NULL;
695                 if (!task || IS_ERR(task))
696                         continue;
697                 kthread_stop(task);
698                 /* no more switch may happen on this CPU at this point */
699                 cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
700                 if (cluster == bL_switcher_cpu_original_cluster[cpu])
701                         continue;
702                 init_completion(&t->started);
703                 t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
704                 task = bL_switcher_thread_create(cpu, t);
705                 if (!IS_ERR(task)) {
706                         wait_for_completion(&t->started);
707                         kthread_stop(task);
708                         cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
709                         if (cluster == bL_switcher_cpu_original_cluster[cpu])
710                                 continue;
711                 }
712                 /* If execution gets here, we're in trouble. */
713                 pr_crit("%s: unable to restore original cluster for CPU %d\n",
714                         __func__, cpu);
715                 pr_crit("%s: CPU %d can't be restored\n",
716                         __func__, bL_switcher_cpu_pairing[cpu]);
717                 cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
718                                   &bL_switcher_removed_logical_cpus);
719         }
720
721         bL_switcher_restore_cpus();
722         bL_switcher_trace_trigger();
723
724         bL_activation_notify(BL_NOTIFY_POST_DISABLE);
725
726 out:
727         cpu_hotplug_driver_unlock();
728         mutex_unlock(&bL_switcher_activation_lock);
729 }
730
731 static ssize_t bL_switcher_active_show(struct kobject *kobj,
732                 struct kobj_attribute *attr, char *buf)
733 {
734         return sprintf(buf, "%u\n", bL_switcher_active);
735 }
736
737 static ssize_t bL_switcher_active_store(struct kobject *kobj,
738                 struct kobj_attribute *attr, const char *buf, size_t count)
739 {
740         int ret;
741
742         switch (buf[0]) {
743         case '0':
744                 bL_switcher_disable();
745                 ret = 0;
746                 break;
747         case '1':
748                 ret = bL_switcher_enable();
749                 break;
750         default:
751                 ret = -EINVAL;
752         }
753
754         return (ret >= 0) ? count : ret;
755 }
756
757 static ssize_t bL_switcher_trace_trigger_store(struct kobject *kobj,
758                 struct kobj_attribute *attr, const char *buf, size_t count)
759 {
760         int ret = bL_switcher_trace_trigger();
761
762         return ret ? ret : count;
763 }
764
765 static struct kobj_attribute bL_switcher_active_attr =
766         __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
767
768 static struct kobj_attribute bL_switcher_trace_trigger_attr =
769         __ATTR(trace_trigger, 0200, NULL, bL_switcher_trace_trigger_store);
770
771 static struct attribute *bL_switcher_attrs[] = {
772         &bL_switcher_active_attr.attr,
773         &bL_switcher_trace_trigger_attr.attr,
774         NULL,
775 };
776
777 static struct attribute_group bL_switcher_attr_group = {
778         .attrs = bL_switcher_attrs,
779 };
780
781 static struct kobject *bL_switcher_kobj;
782
783 static int __init bL_switcher_sysfs_init(void)
784 {
785         int ret;
786
787         bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
788         if (!bL_switcher_kobj)
789                 return -ENOMEM;
790         ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
791         if (ret)
792                 kobject_put(bL_switcher_kobj);
793         return ret;
794 }
795
796 #endif  /* CONFIG_SYSFS */
797
798 bool bL_switcher_get_enabled(void)
799 {
800         mutex_lock(&bL_switcher_activation_lock);
801
802         return bL_switcher_active;
803 }
804 EXPORT_SYMBOL_GPL(bL_switcher_get_enabled);
805
806 void bL_switcher_put_enabled(void)
807 {
808         mutex_unlock(&bL_switcher_activation_lock);
809 }
810 EXPORT_SYMBOL_GPL(bL_switcher_put_enabled);
811
812 /*
813  * Veto any CPU hotplug operation while the switcher is active.
814  * We're just not ready to deal with that given the trickery involved.
815  */
816 static int bL_switcher_hotplug_callback(struct notifier_block *nfb,
817                                         unsigned long action, void *hcpu)
818 {
819         switch (action) {
820         case CPU_UP_PREPARE:
821         case CPU_DOWN_PREPARE:
822                 if (bL_switcher_active)
823                         return NOTIFY_BAD;
824         }
825         return NOTIFY_DONE;
826 }
827
828 static struct notifier_block bL_switcher_hotplug_notifier =
829         { &bL_switcher_hotplug_callback, NULL, 0 };
830
831 #ifdef CONFIG_SCHED_HMP
832 static bool no_bL_switcher = true;
833 #else
834 static bool no_bL_switcher;
835 #endif
836 core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
837
838 static int __init bL_switcher_init(void)
839 {
840         int ret;
841
842         if (MAX_NR_CLUSTERS != 2) {
843                 pr_err("%s: only dual cluster systems are supported\n", __func__);
844                 return -EINVAL;
845         }
846
847         register_cpu_notifier(&bL_switcher_hotplug_notifier);
848
849         if (!no_bL_switcher) {
850                 ret = bL_switcher_enable();
851                 if (ret)
852                         return ret;
853         }
854
855 #ifdef CONFIG_SYSFS
856         ret = bL_switcher_sysfs_init();
857         if (ret)
858                 pr_err("%s: unable to create sysfs entry\n", __func__);
859 #endif
860
861         return 0;
862 }
863
864 late_initcall(bL_switcher_init);