1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm/common/bL_switcher.c -- big.LITTLE cluster switcher core driver
5 * Created by: Nicolas Pitre, March 2012
6 * Copyright: (C) 2012-2013 Linaro Limited
9 #include <linux/atomic.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/sched/signal.h>
14 #include <uapi/linux/sched/types.h>
15 #include <linux/interrupt.h>
16 #include <linux/cpu_pm.h>
17 #include <linux/cpu.h>
18 #include <linux/cpumask.h>
19 #include <linux/kthread.h>
20 #include <linux/wait.h>
21 #include <linux/time.h>
22 #include <linux/clockchips.h>
23 #include <linux/hrtimer.h>
24 #include <linux/tick.h>
25 #include <linux/notifier.h>
27 #include <linux/mutex.h>
28 #include <linux/smp.h>
29 #include <linux/spinlock.h>
30 #include <linux/string.h>
31 #include <linux/sysfs.h>
32 #include <linux/irqchip/arm-gic.h>
33 #include <linux/moduleparam.h>
35 #include <asm/smp_plat.h>
36 #include <asm/cputype.h>
37 #include <asm/suspend.h>
39 #include <asm/bL_switcher.h>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/power_cpu_migrate.h>
46 * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
47 * __attribute_const__ and we don't want the compiler to assume any
48 * constness here as the value _does_ change along some code paths.
51 static int read_mpidr(void)
54 asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id
));
55 return id
& MPIDR_HWID_BITMASK
;
59 * bL switcher core code.
62 static void bL_do_switch(void *_arg
)
64 unsigned ib_mpidr
, ib_cpu
, ib_cluster
;
65 long volatile handshake
, **handshake_ptr
= _arg
;
67 pr_debug("%s\n", __func__
);
69 ib_mpidr
= cpu_logical_map(smp_processor_id());
70 ib_cpu
= MPIDR_AFFINITY_LEVEL(ib_mpidr
, 0);
71 ib_cluster
= MPIDR_AFFINITY_LEVEL(ib_mpidr
, 1);
73 /* Advertise our handshake location */
76 *handshake_ptr
= &handshake
;
81 * Our state has been saved at this point. Let's release our
84 mcpm_set_entry_vector(ib_cpu
, ib_cluster
, cpu_resume
);
88 * From this point, we must assume that our counterpart CPU might
89 * have taken over in its parallel world already, as if execution
90 * just returned from cpu_suspend(). It is therefore important to
91 * be very careful not to make any change the other guy is not
92 * expecting. This is why we need stack isolation.
94 * Fancy under cover tasks could be performed here. For now
99 * Let's wait until our inbound is alive.
106 /* Let's put ourself down. */
107 mcpm_cpu_power_down();
109 /* should never get here */
114 * Stack isolation. To ensure 'current' remains valid, we just use another
115 * piece of our thread's stack space which should be fairly lightly used.
116 * The selected area starts just above the thread_info structure located
117 * at the very bottom of the stack, aligned to a cache line, and indexed
118 * with the cluster number.
120 #define STACK_SIZE 512
121 extern void call_with_stack(void (*fn
)(void *), void *arg
, void *sp
);
122 static int bL_switchpoint(unsigned long _arg
)
124 unsigned int mpidr
= read_mpidr();
125 unsigned int clusterid
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
126 void *stack
= current_thread_info() + 1;
127 stack
= PTR_ALIGN(stack
, L1_CACHE_BYTES
);
128 stack
+= clusterid
* STACK_SIZE
+ STACK_SIZE
;
129 call_with_stack(bL_do_switch
, (void *)_arg
, stack
);
134 * Generic switcher interface
137 static unsigned int bL_gic_id
[MAX_CPUS_PER_CLUSTER
][MAX_NR_CLUSTERS
];
138 static int bL_switcher_cpu_pairing
[NR_CPUS
];
141 * bL_switch_to - Switch to a specific cluster for the current CPU
142 * @new_cluster_id: the ID of the cluster to switch to.
144 * This function must be called on the CPU to be switched.
145 * Returns 0 on success, else a negative status code.
147 static int bL_switch_to(unsigned int new_cluster_id
)
149 unsigned int mpidr
, this_cpu
, that_cpu
;
150 unsigned int ob_mpidr
, ob_cpu
, ob_cluster
, ib_mpidr
, ib_cpu
, ib_cluster
;
151 struct completion inbound_alive
;
152 long volatile *handshake_ptr
;
155 this_cpu
= smp_processor_id();
156 ob_mpidr
= read_mpidr();
157 ob_cpu
= MPIDR_AFFINITY_LEVEL(ob_mpidr
, 0);
158 ob_cluster
= MPIDR_AFFINITY_LEVEL(ob_mpidr
, 1);
159 BUG_ON(cpu_logical_map(this_cpu
) != ob_mpidr
);
161 if (new_cluster_id
== ob_cluster
)
164 that_cpu
= bL_switcher_cpu_pairing
[this_cpu
];
165 ib_mpidr
= cpu_logical_map(that_cpu
);
166 ib_cpu
= MPIDR_AFFINITY_LEVEL(ib_mpidr
, 0);
167 ib_cluster
= MPIDR_AFFINITY_LEVEL(ib_mpidr
, 1);
169 pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
170 this_cpu
, ob_mpidr
, ib_mpidr
);
172 this_cpu
= smp_processor_id();
174 /* Close the gate for our entry vectors */
175 mcpm_set_entry_vector(ob_cpu
, ob_cluster
, NULL
);
176 mcpm_set_entry_vector(ib_cpu
, ib_cluster
, NULL
);
178 /* Install our "inbound alive" notifier. */
179 init_completion(&inbound_alive
);
180 ipi_nr
= register_ipi_completion(&inbound_alive
, this_cpu
);
181 ipi_nr
|= ((1 << 16) << bL_gic_id
[ob_cpu
][ob_cluster
]);
182 mcpm_set_early_poke(ib_cpu
, ib_cluster
, gic_get_sgir_physaddr(), ipi_nr
);
185 * Let's wake up the inbound CPU now in case it requires some delay
186 * to come online, but leave it gated in our entry vector code.
188 ret
= mcpm_cpu_power_up(ib_cpu
, ib_cluster
);
190 pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__
, ret
);
195 * Raise a SGI on the inbound CPU to make sure it doesn't stall
196 * in a possible WFI, such as in bL_power_down().
198 gic_send_sgi(bL_gic_id
[ib_cpu
][ib_cluster
], 0);
201 * Wait for the inbound to come up. This allows for other
202 * tasks to be scheduled in the mean time.
204 wait_for_completion(&inbound_alive
);
205 mcpm_set_early_poke(ib_cpu
, ib_cluster
, 0, 0);
208 * From this point we are entering the switch critical zone
209 * and can't take any interrupts anymore.
213 trace_cpu_migrate_begin(ktime_get_real_ns(), ob_mpidr
);
215 /* redirect GIC's SGIs to our counterpart */
216 gic_migrate_target(bL_gic_id
[ib_cpu
][ib_cluster
]);
218 tick_suspend_local();
220 ret
= cpu_pm_enter();
222 /* we can not tolerate errors at this point */
224 panic("%s: cpu_pm_enter() returned %d\n", __func__
, ret
);
226 /* Swap the physical CPUs in the logical map for this logical CPU. */
227 cpu_logical_map(this_cpu
) = ib_mpidr
;
228 cpu_logical_map(that_cpu
) = ob_mpidr
;
230 /* Let's do the actual CPU switch. */
231 ret
= cpu_suspend((unsigned long)&handshake_ptr
, bL_switchpoint
);
233 panic("%s: cpu_suspend() returned %d\n", __func__
, ret
);
235 /* We are executing on the inbound CPU at this point */
236 mpidr
= read_mpidr();
237 pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu
, mpidr
);
238 BUG_ON(mpidr
!= ib_mpidr
);
240 mcpm_cpu_powered_up();
246 trace_cpu_migrate_finish(ktime_get_real_ns(), ib_mpidr
);
254 pr_err("%s exiting with error %d\n", __func__
, ret
);
260 struct task_struct
*task
;
261 wait_queue_head_t wq
;
263 struct completion started
;
264 bL_switch_completion_handler completer
;
265 void *completer_cookie
;
268 static struct bL_thread bL_threads
[NR_CPUS
];
270 static int bL_switcher_thread(void *arg
)
272 struct bL_thread
*t
= arg
;
273 struct sched_param param
= { .sched_priority
= 1 };
275 bL_switch_completion_handler completer
;
276 void *completer_cookie
;
278 sched_setscheduler_nocheck(current
, SCHED_FIFO
, ¶m
);
279 complete(&t
->started
);
282 if (signal_pending(current
))
283 flush_signals(current
);
284 wait_event_interruptible(t
->wq
,
285 t
->wanted_cluster
!= -1 ||
286 kthread_should_stop());
289 cluster
= t
->wanted_cluster
;
290 completer
= t
->completer
;
291 completer_cookie
= t
->completer_cookie
;
292 t
->wanted_cluster
= -1;
294 spin_unlock(&t
->lock
);
297 bL_switch_to(cluster
);
300 completer(completer_cookie
);
302 } while (!kthread_should_stop());
307 static struct task_struct
*bL_switcher_thread_create(int cpu
, void *arg
)
309 struct task_struct
*task
;
311 task
= kthread_create_on_node(bL_switcher_thread
, arg
,
312 cpu_to_node(cpu
), "kswitcher_%d", cpu
);
314 kthread_bind(task
, cpu
);
315 wake_up_process(task
);
317 pr_err("%s failed for CPU %d\n", __func__
, cpu
);
322 * bL_switch_request_cb - Switch to a specific cluster for the given CPU,
323 * with completion notification via a callback
325 * @cpu: the CPU to switch
326 * @new_cluster_id: the ID of the cluster to switch to.
327 * @completer: switch completion callback. if non-NULL,
328 * @completer(@completer_cookie) will be called on completion of
329 * the switch, in non-atomic context.
330 * @completer_cookie: opaque context argument for @completer.
332 * This function causes a cluster switch on the given CPU by waking up
333 * the appropriate switcher thread. This function may or may not return
334 * before the switch has occurred.
336 * If a @completer callback function is supplied, it will be called when
337 * the switch is complete. This can be used to determine asynchronously
338 * when the switch is complete, regardless of when bL_switch_request()
339 * returns. When @completer is supplied, no new switch request is permitted
340 * for the affected CPU until after the switch is complete, and @completer
343 int bL_switch_request_cb(unsigned int cpu
, unsigned int new_cluster_id
,
344 bL_switch_completion_handler completer
,
345 void *completer_cookie
)
349 if (cpu
>= ARRAY_SIZE(bL_threads
)) {
350 pr_err("%s: cpu %d out of bounds\n", __func__
, cpu
);
354 t
= &bL_threads
[cpu
];
357 return PTR_ERR(t
->task
);
363 spin_unlock(&t
->lock
);
366 t
->completer
= completer
;
367 t
->completer_cookie
= completer_cookie
;
368 t
->wanted_cluster
= new_cluster_id
;
369 spin_unlock(&t
->lock
);
373 EXPORT_SYMBOL_GPL(bL_switch_request_cb
);
376 * Activation and configuration code.
379 static DEFINE_MUTEX(bL_switcher_activation_lock
);
380 static BLOCKING_NOTIFIER_HEAD(bL_activation_notifier
);
381 static unsigned int bL_switcher_active
;
382 static unsigned int bL_switcher_cpu_original_cluster
[NR_CPUS
];
383 static cpumask_t bL_switcher_removed_logical_cpus
;
385 int bL_switcher_register_notifier(struct notifier_block
*nb
)
387 return blocking_notifier_chain_register(&bL_activation_notifier
, nb
);
389 EXPORT_SYMBOL_GPL(bL_switcher_register_notifier
);
391 int bL_switcher_unregister_notifier(struct notifier_block
*nb
)
393 return blocking_notifier_chain_unregister(&bL_activation_notifier
, nb
);
395 EXPORT_SYMBOL_GPL(bL_switcher_unregister_notifier
);
397 static int bL_activation_notify(unsigned long val
)
401 ret
= blocking_notifier_call_chain(&bL_activation_notifier
, val
, NULL
);
402 if (ret
& NOTIFY_STOP_MASK
)
403 pr_err("%s: notifier chain failed with status 0x%x\n",
405 return notifier_to_errno(ret
);
408 static void bL_switcher_restore_cpus(void)
412 for_each_cpu(i
, &bL_switcher_removed_logical_cpus
) {
413 struct device
*cpu_dev
= get_cpu_device(i
);
414 int ret
= device_online(cpu_dev
);
416 dev_err(cpu_dev
, "switcher: unable to restore CPU\n");
420 static int bL_switcher_halve_cpus(void)
422 int i
, j
, cluster_0
, gic_id
, ret
;
423 unsigned int cpu
, cluster
, mask
;
424 cpumask_t available_cpus
;
426 /* First pass to validate what we have */
428 for_each_online_cpu(i
) {
429 cpu
= MPIDR_AFFINITY_LEVEL(cpu_logical_map(i
), 0);
430 cluster
= MPIDR_AFFINITY_LEVEL(cpu_logical_map(i
), 1);
432 pr_err("%s: only dual cluster systems are supported\n", __func__
);
435 if (WARN_ON(cpu
>= MAX_CPUS_PER_CLUSTER
))
437 mask
|= (1 << cluster
);
440 pr_err("%s: no CPU pairing possible\n", __func__
);
445 * Now let's do the pairing. We match each CPU with another CPU
446 * from a different cluster. To get a uniform scheduling behavior
447 * without fiddling with CPU topology and compute capacity data,
448 * we'll use logical CPUs initially belonging to the same cluster.
450 memset(bL_switcher_cpu_pairing
, -1, sizeof(bL_switcher_cpu_pairing
));
451 cpumask_copy(&available_cpus
, cpu_online_mask
);
453 for_each_cpu(i
, &available_cpus
) {
455 cluster
= MPIDR_AFFINITY_LEVEL(cpu_logical_map(i
), 1);
458 if (cluster
!= cluster_0
)
460 cpumask_clear_cpu(i
, &available_cpus
);
461 for_each_cpu(j
, &available_cpus
) {
462 cluster
= MPIDR_AFFINITY_LEVEL(cpu_logical_map(j
), 1);
464 * Let's remember the last match to create "odd"
465 * pairings on purpose in order for other code not
466 * to assume any relation between physical and
467 * logical CPU numbers.
469 if (cluster
!= cluster_0
)
473 bL_switcher_cpu_pairing
[i
] = match
;
474 cpumask_clear_cpu(match
, &available_cpus
);
475 pr_info("CPU%d paired with CPU%d\n", i
, match
);
480 * Now we disable the unwanted CPUs i.e. everything that has no
481 * pairing information (that includes the pairing counterparts).
483 cpumask_clear(&bL_switcher_removed_logical_cpus
);
484 for_each_online_cpu(i
) {
485 cpu
= MPIDR_AFFINITY_LEVEL(cpu_logical_map(i
), 0);
486 cluster
= MPIDR_AFFINITY_LEVEL(cpu_logical_map(i
), 1);
488 /* Let's take note of the GIC ID for this CPU */
489 gic_id
= gic_get_cpu_id(i
);
491 pr_err("%s: bad GIC ID for CPU %d\n", __func__
, i
);
492 bL_switcher_restore_cpus();
495 bL_gic_id
[cpu
][cluster
] = gic_id
;
496 pr_info("GIC ID for CPU %u cluster %u is %u\n",
497 cpu
, cluster
, gic_id
);
499 if (bL_switcher_cpu_pairing
[i
] != -1) {
500 bL_switcher_cpu_original_cluster
[i
] = cluster
;
504 ret
= device_offline(get_cpu_device(i
));
506 bL_switcher_restore_cpus();
509 cpumask_set_cpu(i
, &bL_switcher_removed_logical_cpus
);
515 /* Determine the logical CPU a given physical CPU is grouped on. */
516 int bL_switcher_get_logical_index(u32 mpidr
)
520 if (!bL_switcher_active
)
523 mpidr
&= MPIDR_HWID_BITMASK
;
524 for_each_online_cpu(cpu
) {
525 int pairing
= bL_switcher_cpu_pairing
[cpu
];
528 if ((mpidr
== cpu_logical_map(cpu
)) ||
529 (mpidr
== cpu_logical_map(pairing
)))
535 static void bL_switcher_trace_trigger_cpu(void *__always_unused info
)
537 trace_cpu_migrate_current(ktime_get_real_ns(), read_mpidr());
540 int bL_switcher_trace_trigger(void)
544 bL_switcher_trace_trigger_cpu(NULL
);
545 smp_call_function(bL_switcher_trace_trigger_cpu
, NULL
, true);
551 EXPORT_SYMBOL_GPL(bL_switcher_trace_trigger
);
553 static int bL_switcher_enable(void)
557 mutex_lock(&bL_switcher_activation_lock
);
558 lock_device_hotplug();
559 if (bL_switcher_active
) {
560 unlock_device_hotplug();
561 mutex_unlock(&bL_switcher_activation_lock
);
565 pr_info("big.LITTLE switcher initializing\n");
567 ret
= bL_activation_notify(BL_NOTIFY_PRE_ENABLE
);
571 ret
= bL_switcher_halve_cpus();
575 bL_switcher_trace_trigger();
577 for_each_online_cpu(cpu
) {
578 struct bL_thread
*t
= &bL_threads
[cpu
];
579 spin_lock_init(&t
->lock
);
580 init_waitqueue_head(&t
->wq
);
581 init_completion(&t
->started
);
582 t
->wanted_cluster
= -1;
583 t
->task
= bL_switcher_thread_create(cpu
, t
);
586 bL_switcher_active
= 1;
587 bL_activation_notify(BL_NOTIFY_POST_ENABLE
);
588 pr_info("big.LITTLE switcher initialized\n");
592 pr_warn("big.LITTLE switcher initialization failed\n");
593 bL_activation_notify(BL_NOTIFY_POST_DISABLE
);
596 unlock_device_hotplug();
597 mutex_unlock(&bL_switcher_activation_lock
);
603 static void bL_switcher_disable(void)
605 unsigned int cpu
, cluster
;
607 struct task_struct
*task
;
609 mutex_lock(&bL_switcher_activation_lock
);
610 lock_device_hotplug();
612 if (!bL_switcher_active
)
615 if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE
) != 0) {
616 bL_activation_notify(BL_NOTIFY_POST_ENABLE
);
620 bL_switcher_active
= 0;
623 * To deactivate the switcher, we must shut down the switcher
624 * threads to prevent any other requests from being accepted.
625 * Then, if the final cluster for given logical CPU is not the
626 * same as the original one, we'll recreate a switcher thread
627 * just for the purpose of switching the CPU back without any
628 * possibility for interference from external requests.
630 for_each_online_cpu(cpu
) {
631 t
= &bL_threads
[cpu
];
634 if (!task
|| IS_ERR(task
))
637 /* no more switch may happen on this CPU at this point */
638 cluster
= MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu
), 1);
639 if (cluster
== bL_switcher_cpu_original_cluster
[cpu
])
641 init_completion(&t
->started
);
642 t
->wanted_cluster
= bL_switcher_cpu_original_cluster
[cpu
];
643 task
= bL_switcher_thread_create(cpu
, t
);
645 wait_for_completion(&t
->started
);
647 cluster
= MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu
), 1);
648 if (cluster
== bL_switcher_cpu_original_cluster
[cpu
])
651 /* If execution gets here, we're in trouble. */
652 pr_crit("%s: unable to restore original cluster for CPU %d\n",
654 pr_crit("%s: CPU %d can't be restored\n",
655 __func__
, bL_switcher_cpu_pairing
[cpu
]);
656 cpumask_clear_cpu(bL_switcher_cpu_pairing
[cpu
],
657 &bL_switcher_removed_logical_cpus
);
660 bL_switcher_restore_cpus();
661 bL_switcher_trace_trigger();
663 bL_activation_notify(BL_NOTIFY_POST_DISABLE
);
666 unlock_device_hotplug();
667 mutex_unlock(&bL_switcher_activation_lock
);
670 static ssize_t
bL_switcher_active_show(struct kobject
*kobj
,
671 struct kobj_attribute
*attr
, char *buf
)
673 return sprintf(buf
, "%u\n", bL_switcher_active
);
676 static ssize_t
bL_switcher_active_store(struct kobject
*kobj
,
677 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
683 bL_switcher_disable();
687 ret
= bL_switcher_enable();
693 return (ret
>= 0) ? count
: ret
;
696 static ssize_t
bL_switcher_trace_trigger_store(struct kobject
*kobj
,
697 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
699 int ret
= bL_switcher_trace_trigger();
701 return ret
? ret
: count
;
704 static struct kobj_attribute bL_switcher_active_attr
=
705 __ATTR(active
, 0644, bL_switcher_active_show
, bL_switcher_active_store
);
707 static struct kobj_attribute bL_switcher_trace_trigger_attr
=
708 __ATTR(trace_trigger
, 0200, NULL
, bL_switcher_trace_trigger_store
);
710 static struct attribute
*bL_switcher_attrs
[] = {
711 &bL_switcher_active_attr
.attr
,
712 &bL_switcher_trace_trigger_attr
.attr
,
716 static struct attribute_group bL_switcher_attr_group
= {
717 .attrs
= bL_switcher_attrs
,
720 static struct kobject
*bL_switcher_kobj
;
722 static int __init
bL_switcher_sysfs_init(void)
726 bL_switcher_kobj
= kobject_create_and_add("bL_switcher", kernel_kobj
);
727 if (!bL_switcher_kobj
)
729 ret
= sysfs_create_group(bL_switcher_kobj
, &bL_switcher_attr_group
);
731 kobject_put(bL_switcher_kobj
);
735 #endif /* CONFIG_SYSFS */
737 bool bL_switcher_get_enabled(void)
739 mutex_lock(&bL_switcher_activation_lock
);
741 return bL_switcher_active
;
743 EXPORT_SYMBOL_GPL(bL_switcher_get_enabled
);
745 void bL_switcher_put_enabled(void)
747 mutex_unlock(&bL_switcher_activation_lock
);
749 EXPORT_SYMBOL_GPL(bL_switcher_put_enabled
);
752 * Veto any CPU hotplug operation on those CPUs we've removed
753 * while the switcher is active.
754 * We're just not ready to deal with that given the trickery involved.
756 static int bL_switcher_cpu_pre(unsigned int cpu
)
760 if (!bL_switcher_active
)
763 pairing
= bL_switcher_cpu_pairing
[cpu
];
770 static bool no_bL_switcher
;
771 core_param(no_bL_switcher
, no_bL_switcher
, bool, 0644);
773 static int __init
bL_switcher_init(void)
777 if (!mcpm_is_available())
780 cpuhp_setup_state_nocalls(CPUHP_ARM_BL_PREPARE
, "arm/bl:prepare",
781 bL_switcher_cpu_pre
, NULL
);
782 ret
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
, "arm/bl:predown",
783 NULL
, bL_switcher_cpu_pre
);
785 cpuhp_remove_state_nocalls(CPUHP_ARM_BL_PREPARE
);
786 pr_err("bL_switcher: Failed to allocate a hotplug state\n");
789 if (!no_bL_switcher
) {
790 ret
= bL_switcher_enable();
796 ret
= bL_switcher_sysfs_init();
798 pr_err("%s: unable to create sysfs entry\n", __func__
);
804 late_initcall(bL_switcher_init
);