11c33be57SNicolas Pitre /* 21c33be57SNicolas Pitre * arch/arm/common/bL_switcher.c -- big.LITTLE cluster switcher core driver 31c33be57SNicolas Pitre * 41c33be57SNicolas Pitre * Created by: Nicolas Pitre, March 2012 51c33be57SNicolas Pitre * Copyright: (C) 2012-2013 Linaro Limited 61c33be57SNicolas Pitre * 71c33be57SNicolas Pitre * This program is free software; you can redistribute it and/or modify 81c33be57SNicolas Pitre * it under the terms of the GNU General Public License version 2 as 91c33be57SNicolas Pitre * published by the Free Software Foundation. 101c33be57SNicolas Pitre */ 111c33be57SNicolas Pitre 12*0577fee2SDave Martin #include <linux/atomic.h> 131c33be57SNicolas Pitre #include <linux/init.h> 141c33be57SNicolas Pitre #include <linux/kernel.h> 151c33be57SNicolas Pitre #include <linux/module.h> 161c33be57SNicolas Pitre #include <linux/sched.h> 171c33be57SNicolas Pitre #include <linux/interrupt.h> 181c33be57SNicolas Pitre #include <linux/cpu_pm.h> 1971ce1deeSNicolas Pitre #include <linux/cpu.h> 203f09d479SLorenzo Pieralisi #include <linux/cpumask.h> 2171ce1deeSNicolas Pitre #include <linux/kthread.h> 2271ce1deeSNicolas Pitre #include <linux/wait.h> 233f09d479SLorenzo Pieralisi #include <linux/clockchips.h> 243f09d479SLorenzo Pieralisi #include <linux/hrtimer.h> 253f09d479SLorenzo Pieralisi #include <linux/tick.h> 26491990e2SDave Martin #include <linux/notifier.h> 271c33be57SNicolas Pitre #include <linux/mm.h> 28c0f43751SDave Martin #include <linux/mutex.h> 29*0577fee2SDave Martin #include <linux/spinlock.h> 301c33be57SNicolas Pitre #include <linux/string.h> 316b7437aeSNicolas Pitre #include <linux/sysfs.h> 321c33be57SNicolas Pitre #include <linux/irqchip/arm-gic.h> 33c4821c05SNicolas Pitre #include <linux/moduleparam.h> 341c33be57SNicolas Pitre 351c33be57SNicolas Pitre #include <asm/smp_plat.h> 361c33be57SNicolas Pitre #include <asm/suspend.h> 371c33be57SNicolas Pitre #include <asm/mcpm.h> 381c33be57SNicolas Pitre #include <asm/bL_switcher.h> 391c33be57SNicolas Pitre 401c33be57SNicolas Pitre 411c33be57SNicolas Pitre /* 421c33be57SNicolas Pitre * Use our own MPIDR accessors as the generic ones in asm/cputype.h have 431c33be57SNicolas Pitre * __attribute_const__ and we don't want the compiler to assume any 441c33be57SNicolas Pitre * constness here as the value _does_ change along some code paths. 451c33be57SNicolas Pitre */ 461c33be57SNicolas Pitre 471c33be57SNicolas Pitre static int read_mpidr(void) 481c33be57SNicolas Pitre { 491c33be57SNicolas Pitre unsigned int id; 501c33be57SNicolas Pitre asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id)); 511c33be57SNicolas Pitre return id & MPIDR_HWID_BITMASK; 521c33be57SNicolas Pitre } 531c33be57SNicolas Pitre 541c33be57SNicolas Pitre /* 551c33be57SNicolas Pitre * bL switcher core code. 561c33be57SNicolas Pitre */ 571c33be57SNicolas Pitre 581c33be57SNicolas Pitre static void bL_do_switch(void *_unused) 591c33be57SNicolas Pitre { 6038c35d4fSNicolas Pitre unsigned ib_mpidr, ib_cpu, ib_cluster; 611c33be57SNicolas Pitre 621c33be57SNicolas Pitre pr_debug("%s\n", __func__); 631c33be57SNicolas Pitre 6438c35d4fSNicolas Pitre ib_mpidr = cpu_logical_map(smp_processor_id()); 6538c35d4fSNicolas Pitre ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0); 6638c35d4fSNicolas Pitre ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1); 671c33be57SNicolas Pitre 681c33be57SNicolas Pitre /* 691c33be57SNicolas Pitre * Our state has been saved at this point. Let's release our 701c33be57SNicolas Pitre * inbound CPU. 711c33be57SNicolas Pitre */ 7238c35d4fSNicolas Pitre mcpm_set_entry_vector(ib_cpu, ib_cluster, cpu_resume); 731c33be57SNicolas Pitre sev(); 741c33be57SNicolas Pitre 751c33be57SNicolas Pitre /* 761c33be57SNicolas Pitre * From this point, we must assume that our counterpart CPU might 771c33be57SNicolas Pitre * have taken over in its parallel world already, as if execution 781c33be57SNicolas Pitre * just returned from cpu_suspend(). It is therefore important to 791c33be57SNicolas Pitre * be very careful not to make any change the other guy is not 801c33be57SNicolas Pitre * expecting. This is why we need stack isolation. 811c33be57SNicolas Pitre * 821c33be57SNicolas Pitre * Fancy under cover tasks could be performed here. For now 831c33be57SNicolas Pitre * we have none. 841c33be57SNicolas Pitre */ 851c33be57SNicolas Pitre 861c33be57SNicolas Pitre /* Let's put ourself down. */ 871c33be57SNicolas Pitre mcpm_cpu_power_down(); 881c33be57SNicolas Pitre 891c33be57SNicolas Pitre /* should never get here */ 901c33be57SNicolas Pitre BUG(); 911c33be57SNicolas Pitre } 921c33be57SNicolas Pitre 931c33be57SNicolas Pitre /* 94c052de26SNicolas Pitre * Stack isolation. To ensure 'current' remains valid, we just use another 95c052de26SNicolas Pitre * piece of our thread's stack space which should be fairly lightly used. 96c052de26SNicolas Pitre * The selected area starts just above the thread_info structure located 97c052de26SNicolas Pitre * at the very bottom of the stack, aligned to a cache line, and indexed 98c052de26SNicolas Pitre * with the cluster number. 991c33be57SNicolas Pitre */ 100c052de26SNicolas Pitre #define STACK_SIZE 512 1011c33be57SNicolas Pitre extern void call_with_stack(void (*fn)(void *), void *arg, void *sp); 1021c33be57SNicolas Pitre static int bL_switchpoint(unsigned long _arg) 1031c33be57SNicolas Pitre { 1041c33be57SNicolas Pitre unsigned int mpidr = read_mpidr(); 1051c33be57SNicolas Pitre unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1); 106c052de26SNicolas Pitre void *stack = current_thread_info() + 1; 1071c33be57SNicolas Pitre stack = PTR_ALIGN(stack, L1_CACHE_BYTES); 108c052de26SNicolas Pitre stack += clusterid * STACK_SIZE + STACK_SIZE; 1091c33be57SNicolas Pitre call_with_stack(bL_do_switch, (void *)_arg, stack); 1101c33be57SNicolas Pitre BUG(); 1111c33be57SNicolas Pitre } 1121c33be57SNicolas Pitre 1131c33be57SNicolas Pitre /* 1141c33be57SNicolas Pitre * Generic switcher interface 1151c33be57SNicolas Pitre */ 1161c33be57SNicolas Pitre 117ed96762eSNicolas Pitre static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS]; 11838c35d4fSNicolas Pitre static int bL_switcher_cpu_pairing[NR_CPUS]; 119ed96762eSNicolas Pitre 1201c33be57SNicolas Pitre /* 1211c33be57SNicolas Pitre * bL_switch_to - Switch to a specific cluster for the current CPU 1221c33be57SNicolas Pitre * @new_cluster_id: the ID of the cluster to switch to. 1231c33be57SNicolas Pitre * 1241c33be57SNicolas Pitre * This function must be called on the CPU to be switched. 1251c33be57SNicolas Pitre * Returns 0 on success, else a negative status code. 1261c33be57SNicolas Pitre */ 1271c33be57SNicolas Pitre static int bL_switch_to(unsigned int new_cluster_id) 1281c33be57SNicolas Pitre { 12938c35d4fSNicolas Pitre unsigned int mpidr, this_cpu, that_cpu; 13038c35d4fSNicolas Pitre unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster; 1313f09d479SLorenzo Pieralisi struct tick_device *tdev; 1323f09d479SLorenzo Pieralisi enum clock_event_mode tdev_mode; 1331c33be57SNicolas Pitre int ret; 1341c33be57SNicolas Pitre 13538c35d4fSNicolas Pitre this_cpu = smp_processor_id(); 13638c35d4fSNicolas Pitre ob_mpidr = read_mpidr(); 13738c35d4fSNicolas Pitre ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0); 13838c35d4fSNicolas Pitre ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1); 13938c35d4fSNicolas Pitre BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr); 1401c33be57SNicolas Pitre 14138c35d4fSNicolas Pitre if (new_cluster_id == ob_cluster) 1421c33be57SNicolas Pitre return 0; 1431c33be57SNicolas Pitre 14438c35d4fSNicolas Pitre that_cpu = bL_switcher_cpu_pairing[this_cpu]; 14538c35d4fSNicolas Pitre ib_mpidr = cpu_logical_map(that_cpu); 14638c35d4fSNicolas Pitre ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0); 14738c35d4fSNicolas Pitre ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1); 14838c35d4fSNicolas Pitre 14938c35d4fSNicolas Pitre pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n", 15038c35d4fSNicolas Pitre this_cpu, ob_mpidr, ib_mpidr); 1511c33be57SNicolas Pitre 1521c33be57SNicolas Pitre /* Close the gate for our entry vectors */ 15338c35d4fSNicolas Pitre mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL); 15438c35d4fSNicolas Pitre mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL); 1551c33be57SNicolas Pitre 1561c33be57SNicolas Pitre /* 1571c33be57SNicolas Pitre * Let's wake up the inbound CPU now in case it requires some delay 1581c33be57SNicolas Pitre * to come online, but leave it gated in our entry vector code. 1591c33be57SNicolas Pitre */ 16038c35d4fSNicolas Pitre ret = mcpm_cpu_power_up(ib_cpu, ib_cluster); 1611c33be57SNicolas Pitre if (ret) { 1621c33be57SNicolas Pitre pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret); 1631c33be57SNicolas Pitre return ret; 1641c33be57SNicolas Pitre } 1651c33be57SNicolas Pitre 1661c33be57SNicolas Pitre /* 1671c33be57SNicolas Pitre * From this point we are entering the switch critical zone 1681c33be57SNicolas Pitre * and can't take any interrupts anymore. 1691c33be57SNicolas Pitre */ 1701c33be57SNicolas Pitre local_irq_disable(); 1711c33be57SNicolas Pitre local_fiq_disable(); 1721c33be57SNicolas Pitre 1731c33be57SNicolas Pitre /* redirect GIC's SGIs to our counterpart */ 17438c35d4fSNicolas Pitre gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]); 1751c33be57SNicolas Pitre 1761c33be57SNicolas Pitre /* 1771c33be57SNicolas Pitre * Raise a SGI on the inbound CPU to make sure it doesn't stall 1781c33be57SNicolas Pitre * in a possible WFI, such as in mcpm_power_down(). 1791c33be57SNicolas Pitre */ 1801c33be57SNicolas Pitre arch_send_wakeup_ipi_mask(cpumask_of(this_cpu)); 1811c33be57SNicolas Pitre 1823f09d479SLorenzo Pieralisi tdev = tick_get_device(this_cpu); 1833f09d479SLorenzo Pieralisi if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu))) 1843f09d479SLorenzo Pieralisi tdev = NULL; 1853f09d479SLorenzo Pieralisi if (tdev) { 1863f09d479SLorenzo Pieralisi tdev_mode = tdev->evtdev->mode; 1873f09d479SLorenzo Pieralisi clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN); 1883f09d479SLorenzo Pieralisi } 1893f09d479SLorenzo Pieralisi 1901c33be57SNicolas Pitre ret = cpu_pm_enter(); 1911c33be57SNicolas Pitre 1921c33be57SNicolas Pitre /* we can not tolerate errors at this point */ 1931c33be57SNicolas Pitre if (ret) 1941c33be57SNicolas Pitre panic("%s: cpu_pm_enter() returned %d\n", __func__, ret); 1951c33be57SNicolas Pitre 19638c35d4fSNicolas Pitre /* Swap the physical CPUs in the logical map for this logical CPU. */ 19738c35d4fSNicolas Pitre cpu_logical_map(this_cpu) = ib_mpidr; 19838c35d4fSNicolas Pitre cpu_logical_map(that_cpu) = ob_mpidr; 1991c33be57SNicolas Pitre 2001c33be57SNicolas Pitre /* Let's do the actual CPU switch. */ 2011c33be57SNicolas Pitre ret = cpu_suspend(0, bL_switchpoint); 2021c33be57SNicolas Pitre if (ret > 0) 2031c33be57SNicolas Pitre panic("%s: cpu_suspend() returned %d\n", __func__, ret); 2041c33be57SNicolas Pitre 2051c33be57SNicolas Pitre /* We are executing on the inbound CPU at this point */ 2061c33be57SNicolas Pitre mpidr = read_mpidr(); 20738c35d4fSNicolas Pitre pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr); 20838c35d4fSNicolas Pitre BUG_ON(mpidr != ib_mpidr); 2091c33be57SNicolas Pitre 2101c33be57SNicolas Pitre mcpm_cpu_powered_up(); 2111c33be57SNicolas Pitre 2121c33be57SNicolas Pitre ret = cpu_pm_exit(); 2131c33be57SNicolas Pitre 2143f09d479SLorenzo Pieralisi if (tdev) { 2153f09d479SLorenzo Pieralisi clockevents_set_mode(tdev->evtdev, tdev_mode); 2163f09d479SLorenzo Pieralisi clockevents_program_event(tdev->evtdev, 2173f09d479SLorenzo Pieralisi tdev->evtdev->next_event, 1); 2183f09d479SLorenzo Pieralisi } 2193f09d479SLorenzo Pieralisi 2201c33be57SNicolas Pitre local_fiq_enable(); 2211c33be57SNicolas Pitre local_irq_enable(); 2221c33be57SNicolas Pitre 2231c33be57SNicolas Pitre if (ret) 2241c33be57SNicolas Pitre pr_err("%s exiting with error %d\n", __func__, ret); 2251c33be57SNicolas Pitre return ret; 2261c33be57SNicolas Pitre } 2271c33be57SNicolas Pitre 22871ce1deeSNicolas Pitre struct bL_thread { 229*0577fee2SDave Martin spinlock_t lock; 23071ce1deeSNicolas Pitre struct task_struct *task; 23171ce1deeSNicolas Pitre wait_queue_head_t wq; 23271ce1deeSNicolas Pitre int wanted_cluster; 2336b7437aeSNicolas Pitre struct completion started; 234*0577fee2SDave Martin bL_switch_completion_handler completer; 235*0577fee2SDave Martin void *completer_cookie; 2361c33be57SNicolas Pitre }; 2371c33be57SNicolas Pitre 23871ce1deeSNicolas Pitre static struct bL_thread bL_threads[NR_CPUS]; 23971ce1deeSNicolas Pitre 24071ce1deeSNicolas Pitre static int bL_switcher_thread(void *arg) 2411c33be57SNicolas Pitre { 24271ce1deeSNicolas Pitre struct bL_thread *t = arg; 24371ce1deeSNicolas Pitre struct sched_param param = { .sched_priority = 1 }; 24471ce1deeSNicolas Pitre int cluster; 245*0577fee2SDave Martin bL_switch_completion_handler completer; 246*0577fee2SDave Martin void *completer_cookie; 24771ce1deeSNicolas Pitre 24871ce1deeSNicolas Pitre sched_setscheduler_nocheck(current, SCHED_FIFO, ¶m); 2496b7437aeSNicolas Pitre complete(&t->started); 25071ce1deeSNicolas Pitre 25171ce1deeSNicolas Pitre do { 25271ce1deeSNicolas Pitre if (signal_pending(current)) 25371ce1deeSNicolas Pitre flush_signals(current); 25471ce1deeSNicolas Pitre wait_event_interruptible(t->wq, 25571ce1deeSNicolas Pitre t->wanted_cluster != -1 || 25671ce1deeSNicolas Pitre kthread_should_stop()); 257*0577fee2SDave Martin 258*0577fee2SDave Martin spin_lock(&t->lock); 259*0577fee2SDave Martin cluster = t->wanted_cluster; 260*0577fee2SDave Martin completer = t->completer; 261*0577fee2SDave Martin completer_cookie = t->completer_cookie; 262*0577fee2SDave Martin t->wanted_cluster = -1; 263*0577fee2SDave Martin t->completer = NULL; 264*0577fee2SDave Martin spin_unlock(&t->lock); 265*0577fee2SDave Martin 266*0577fee2SDave Martin if (cluster != -1) { 26771ce1deeSNicolas Pitre bL_switch_to(cluster); 268*0577fee2SDave Martin 269*0577fee2SDave Martin if (completer) 270*0577fee2SDave Martin completer(completer_cookie); 271*0577fee2SDave Martin } 27271ce1deeSNicolas Pitre } while (!kthread_should_stop()); 27371ce1deeSNicolas Pitre 27471ce1deeSNicolas Pitre return 0; 27571ce1deeSNicolas Pitre } 27671ce1deeSNicolas Pitre 2776b7437aeSNicolas Pitre static struct task_struct *bL_switcher_thread_create(int cpu, void *arg) 27871ce1deeSNicolas Pitre { 27971ce1deeSNicolas Pitre struct task_struct *task; 28071ce1deeSNicolas Pitre 28171ce1deeSNicolas Pitre task = kthread_create_on_node(bL_switcher_thread, arg, 28271ce1deeSNicolas Pitre cpu_to_node(cpu), "kswitcher_%d", cpu); 28371ce1deeSNicolas Pitre if (!IS_ERR(task)) { 28471ce1deeSNicolas Pitre kthread_bind(task, cpu); 28571ce1deeSNicolas Pitre wake_up_process(task); 28671ce1deeSNicolas Pitre } else 28771ce1deeSNicolas Pitre pr_err("%s failed for CPU %d\n", __func__, cpu); 28871ce1deeSNicolas Pitre return task; 2891c33be57SNicolas Pitre } 2901c33be57SNicolas Pitre 2911c33be57SNicolas Pitre /* 292*0577fee2SDave Martin * bL_switch_request_cb - Switch to a specific cluster for the given CPU, 293*0577fee2SDave Martin * with completion notification via a callback 2941c33be57SNicolas Pitre * 2951c33be57SNicolas Pitre * @cpu: the CPU to switch 2961c33be57SNicolas Pitre * @new_cluster_id: the ID of the cluster to switch to. 297*0577fee2SDave Martin * @completer: switch completion callback. if non-NULL, 298*0577fee2SDave Martin * @completer(@completer_cookie) will be called on completion of 299*0577fee2SDave Martin * the switch, in non-atomic context. 300*0577fee2SDave Martin * @completer_cookie: opaque context argument for @completer. 3011c33be57SNicolas Pitre * 30271ce1deeSNicolas Pitre * This function causes a cluster switch on the given CPU by waking up 30371ce1deeSNicolas Pitre * the appropriate switcher thread. This function may or may not return 30471ce1deeSNicolas Pitre * before the switch has occurred. 305*0577fee2SDave Martin * 306*0577fee2SDave Martin * If a @completer callback function is supplied, it will be called when 307*0577fee2SDave Martin * the switch is complete. This can be used to determine asynchronously 308*0577fee2SDave Martin * when the switch is complete, regardless of when bL_switch_request() 309*0577fee2SDave Martin * returns. When @completer is supplied, no new switch request is permitted 310*0577fee2SDave Martin * for the affected CPU until after the switch is complete, and @completer 311*0577fee2SDave Martin * has returned. 3121c33be57SNicolas Pitre */ 313*0577fee2SDave Martin int bL_switch_request_cb(unsigned int cpu, unsigned int new_cluster_id, 314*0577fee2SDave Martin bL_switch_completion_handler completer, 315*0577fee2SDave Martin void *completer_cookie) 3161c33be57SNicolas Pitre { 31771ce1deeSNicolas Pitre struct bL_thread *t; 3181c33be57SNicolas Pitre 31971ce1deeSNicolas Pitre if (cpu >= ARRAY_SIZE(bL_threads)) { 32071ce1deeSNicolas Pitre pr_err("%s: cpu %d out of bounds\n", __func__, cpu); 32171ce1deeSNicolas Pitre return -EINVAL; 3221c33be57SNicolas Pitre } 3231c33be57SNicolas Pitre 32471ce1deeSNicolas Pitre t = &bL_threads[cpu]; 325*0577fee2SDave Martin 32671ce1deeSNicolas Pitre if (IS_ERR(t->task)) 32771ce1deeSNicolas Pitre return PTR_ERR(t->task); 32871ce1deeSNicolas Pitre if (!t->task) 32971ce1deeSNicolas Pitre return -ESRCH; 33071ce1deeSNicolas Pitre 331*0577fee2SDave Martin spin_lock(&t->lock); 332*0577fee2SDave Martin if (t->completer) { 333*0577fee2SDave Martin spin_unlock(&t->lock); 334*0577fee2SDave Martin return -EBUSY; 335*0577fee2SDave Martin } 336*0577fee2SDave Martin t->completer = completer; 337*0577fee2SDave Martin t->completer_cookie = completer_cookie; 33871ce1deeSNicolas Pitre t->wanted_cluster = new_cluster_id; 339*0577fee2SDave Martin spin_unlock(&t->lock); 34071ce1deeSNicolas Pitre wake_up(&t->wq); 34171ce1deeSNicolas Pitre return 0; 3421c33be57SNicolas Pitre } 343*0577fee2SDave Martin EXPORT_SYMBOL_GPL(bL_switch_request_cb); 34471ce1deeSNicolas Pitre 3459797a0e9SNicolas Pitre /* 3469797a0e9SNicolas Pitre * Activation and configuration code. 3479797a0e9SNicolas Pitre */ 3489797a0e9SNicolas Pitre 349c0f43751SDave Martin static DEFINE_MUTEX(bL_switcher_activation_lock); 350491990e2SDave Martin static BLOCKING_NOTIFIER_HEAD(bL_activation_notifier); 3516b7437aeSNicolas Pitre static unsigned int bL_switcher_active; 35238c35d4fSNicolas Pitre static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS]; 3539797a0e9SNicolas Pitre static cpumask_t bL_switcher_removed_logical_cpus; 3549797a0e9SNicolas Pitre 355491990e2SDave Martin int bL_switcher_register_notifier(struct notifier_block *nb) 356491990e2SDave Martin { 357491990e2SDave Martin return blocking_notifier_chain_register(&bL_activation_notifier, nb); 358491990e2SDave Martin } 359491990e2SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_register_notifier); 360491990e2SDave Martin 361491990e2SDave Martin int bL_switcher_unregister_notifier(struct notifier_block *nb) 362491990e2SDave Martin { 363491990e2SDave Martin return blocking_notifier_chain_unregister(&bL_activation_notifier, nb); 364491990e2SDave Martin } 365491990e2SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_unregister_notifier); 366491990e2SDave Martin 367491990e2SDave Martin static int bL_activation_notify(unsigned long val) 368491990e2SDave Martin { 369491990e2SDave Martin int ret; 370491990e2SDave Martin 371491990e2SDave Martin ret = blocking_notifier_call_chain(&bL_activation_notifier, val, NULL); 372491990e2SDave Martin if (ret & NOTIFY_STOP_MASK) 373491990e2SDave Martin pr_err("%s: notifier chain failed with status 0x%x\n", 374491990e2SDave Martin __func__, ret); 375491990e2SDave Martin return notifier_to_errno(ret); 376491990e2SDave Martin } 377491990e2SDave Martin 3786b7437aeSNicolas Pitre static void bL_switcher_restore_cpus(void) 3799797a0e9SNicolas Pitre { 3809797a0e9SNicolas Pitre int i; 3819797a0e9SNicolas Pitre 3829797a0e9SNicolas Pitre for_each_cpu(i, &bL_switcher_removed_logical_cpus) 3839797a0e9SNicolas Pitre cpu_up(i); 3849797a0e9SNicolas Pitre } 3859797a0e9SNicolas Pitre 3866b7437aeSNicolas Pitre static int bL_switcher_halve_cpus(void) 3879797a0e9SNicolas Pitre { 38838c35d4fSNicolas Pitre int i, j, cluster_0, gic_id, ret; 38938c35d4fSNicolas Pitre unsigned int cpu, cluster, mask; 39038c35d4fSNicolas Pitre cpumask_t available_cpus; 3919797a0e9SNicolas Pitre 39238c35d4fSNicolas Pitre /* First pass to validate what we have */ 39338c35d4fSNicolas Pitre mask = 0; 3949797a0e9SNicolas Pitre for_each_online_cpu(i) { 39538c35d4fSNicolas Pitre cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0); 39638c35d4fSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1); 3979797a0e9SNicolas Pitre if (cluster >= 2) { 3989797a0e9SNicolas Pitre pr_err("%s: only dual cluster systems are supported\n", __func__); 3999797a0e9SNicolas Pitre return -EINVAL; 4009797a0e9SNicolas Pitre } 40138c35d4fSNicolas Pitre if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER)) 40238c35d4fSNicolas Pitre return -EINVAL; 40338c35d4fSNicolas Pitre mask |= (1 << cluster); 4049797a0e9SNicolas Pitre } 40538c35d4fSNicolas Pitre if (mask != 3) { 40638c35d4fSNicolas Pitre pr_err("%s: no CPU pairing possible\n", __func__); 4079797a0e9SNicolas Pitre return -EINVAL; 4089797a0e9SNicolas Pitre } 4099797a0e9SNicolas Pitre 41038c35d4fSNicolas Pitre /* 41138c35d4fSNicolas Pitre * Now let's do the pairing. We match each CPU with another CPU 41238c35d4fSNicolas Pitre * from a different cluster. To get a uniform scheduling behavior 41338c35d4fSNicolas Pitre * without fiddling with CPU topology and compute capacity data, 41438c35d4fSNicolas Pitre * we'll use logical CPUs initially belonging to the same cluster. 41538c35d4fSNicolas Pitre */ 41638c35d4fSNicolas Pitre memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing)); 41738c35d4fSNicolas Pitre cpumask_copy(&available_cpus, cpu_online_mask); 41838c35d4fSNicolas Pitre cluster_0 = -1; 41938c35d4fSNicolas Pitre for_each_cpu(i, &available_cpus) { 42038c35d4fSNicolas Pitre int match = -1; 42138c35d4fSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1); 42238c35d4fSNicolas Pitre if (cluster_0 == -1) 42338c35d4fSNicolas Pitre cluster_0 = cluster; 42438c35d4fSNicolas Pitre if (cluster != cluster_0) 42538c35d4fSNicolas Pitre continue; 42638c35d4fSNicolas Pitre cpumask_clear_cpu(i, &available_cpus); 42738c35d4fSNicolas Pitre for_each_cpu(j, &available_cpus) { 42838c35d4fSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1); 42938c35d4fSNicolas Pitre /* 43038c35d4fSNicolas Pitre * Let's remember the last match to create "odd" 43138c35d4fSNicolas Pitre * pairings on purpose in order for other code not 43238c35d4fSNicolas Pitre * to assume any relation between physical and 43338c35d4fSNicolas Pitre * logical CPU numbers. 43438c35d4fSNicolas Pitre */ 43538c35d4fSNicolas Pitre if (cluster != cluster_0) 43638c35d4fSNicolas Pitre match = j; 43738c35d4fSNicolas Pitre } 43838c35d4fSNicolas Pitre if (match != -1) { 43938c35d4fSNicolas Pitre bL_switcher_cpu_pairing[i] = match; 44038c35d4fSNicolas Pitre cpumask_clear_cpu(match, &available_cpus); 44138c35d4fSNicolas Pitre pr_info("CPU%d paired with CPU%d\n", i, match); 44238c35d4fSNicolas Pitre } 44338c35d4fSNicolas Pitre } 4449797a0e9SNicolas Pitre 44538c35d4fSNicolas Pitre /* 44638c35d4fSNicolas Pitre * Now we disable the unwanted CPUs i.e. everything that has no 44738c35d4fSNicolas Pitre * pairing information (that includes the pairing counterparts). 44838c35d4fSNicolas Pitre */ 44938c35d4fSNicolas Pitre cpumask_clear(&bL_switcher_removed_logical_cpus); 45038c35d4fSNicolas Pitre for_each_online_cpu(i) { 45138c35d4fSNicolas Pitre cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0); 45238c35d4fSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1); 45338c35d4fSNicolas Pitre 454ed96762eSNicolas Pitre /* Let's take note of the GIC ID for this CPU */ 45538c35d4fSNicolas Pitre gic_id = gic_get_cpu_id(i); 456ed96762eSNicolas Pitre if (gic_id < 0) { 457ed96762eSNicolas Pitre pr_err("%s: bad GIC ID for CPU %d\n", __func__, i); 45838c35d4fSNicolas Pitre bL_switcher_restore_cpus(); 459ed96762eSNicolas Pitre return -EINVAL; 460ed96762eSNicolas Pitre } 461ed96762eSNicolas Pitre bL_gic_id[cpu][cluster] = gic_id; 462ed96762eSNicolas Pitre pr_info("GIC ID for CPU %u cluster %u is %u\n", 463ed96762eSNicolas Pitre cpu, cluster, gic_id); 464ed96762eSNicolas Pitre 46538c35d4fSNicolas Pitre if (bL_switcher_cpu_pairing[i] != -1) { 46638c35d4fSNicolas Pitre bL_switcher_cpu_original_cluster[i] = cluster; 4679797a0e9SNicolas Pitre continue; 4689797a0e9SNicolas Pitre } 4699797a0e9SNicolas Pitre 4709797a0e9SNicolas Pitre ret = cpu_down(i); 4719797a0e9SNicolas Pitre if (ret) { 4729797a0e9SNicolas Pitre bL_switcher_restore_cpus(); 4739797a0e9SNicolas Pitre return ret; 4749797a0e9SNicolas Pitre } 4759797a0e9SNicolas Pitre cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus); 4769797a0e9SNicolas Pitre } 4779797a0e9SNicolas Pitre 4789797a0e9SNicolas Pitre return 0; 4799797a0e9SNicolas Pitre } 4809797a0e9SNicolas Pitre 4816b7437aeSNicolas Pitre static int bL_switcher_enable(void) 48271ce1deeSNicolas Pitre { 4839797a0e9SNicolas Pitre int cpu, ret; 48471ce1deeSNicolas Pitre 485c0f43751SDave Martin mutex_lock(&bL_switcher_activation_lock); 4866b7437aeSNicolas Pitre cpu_hotplug_driver_lock(); 4876b7437aeSNicolas Pitre if (bL_switcher_active) { 4886b7437aeSNicolas Pitre cpu_hotplug_driver_unlock(); 489c0f43751SDave Martin mutex_unlock(&bL_switcher_activation_lock); 4906b7437aeSNicolas Pitre return 0; 4919797a0e9SNicolas Pitre } 4929797a0e9SNicolas Pitre 4936b7437aeSNicolas Pitre pr_info("big.LITTLE switcher initializing\n"); 4946b7437aeSNicolas Pitre 495491990e2SDave Martin ret = bL_activation_notify(BL_NOTIFY_PRE_ENABLE); 496491990e2SDave Martin if (ret) 497491990e2SDave Martin goto error; 498491990e2SDave Martin 4999797a0e9SNicolas Pitre ret = bL_switcher_halve_cpus(); 500491990e2SDave Martin if (ret) 501491990e2SDave Martin goto error; 5029797a0e9SNicolas Pitre 50371ce1deeSNicolas Pitre for_each_online_cpu(cpu) { 50471ce1deeSNicolas Pitre struct bL_thread *t = &bL_threads[cpu]; 505*0577fee2SDave Martin spin_lock_init(&t->lock); 50671ce1deeSNicolas Pitre init_waitqueue_head(&t->wq); 5076b7437aeSNicolas Pitre init_completion(&t->started); 50871ce1deeSNicolas Pitre t->wanted_cluster = -1; 50971ce1deeSNicolas Pitre t->task = bL_switcher_thread_create(cpu, t); 51071ce1deeSNicolas Pitre } 5116b7437aeSNicolas Pitre 5126b7437aeSNicolas Pitre bL_switcher_active = 1; 513491990e2SDave Martin bL_activation_notify(BL_NOTIFY_POST_ENABLE); 51471ce1deeSNicolas Pitre pr_info("big.LITTLE switcher initialized\n"); 515491990e2SDave Martin goto out; 516c0f43751SDave Martin 517491990e2SDave Martin error: 518491990e2SDave Martin pr_warn("big.LITTLE switcher initialization failed\n"); 519491990e2SDave Martin bL_activation_notify(BL_NOTIFY_POST_DISABLE); 520491990e2SDave Martin 521491990e2SDave Martin out: 522c0f43751SDave Martin cpu_hotplug_driver_unlock(); 523c0f43751SDave Martin mutex_unlock(&bL_switcher_activation_lock); 524491990e2SDave Martin return ret; 52571ce1deeSNicolas Pitre } 52671ce1deeSNicolas Pitre 5276b7437aeSNicolas Pitre #ifdef CONFIG_SYSFS 5286b7437aeSNicolas Pitre 5296b7437aeSNicolas Pitre static void bL_switcher_disable(void) 5306b7437aeSNicolas Pitre { 53138c35d4fSNicolas Pitre unsigned int cpu, cluster; 5326b7437aeSNicolas Pitre struct bL_thread *t; 5336b7437aeSNicolas Pitre struct task_struct *task; 5346b7437aeSNicolas Pitre 535c0f43751SDave Martin mutex_lock(&bL_switcher_activation_lock); 5366b7437aeSNicolas Pitre cpu_hotplug_driver_lock(); 537491990e2SDave Martin 538491990e2SDave Martin if (!bL_switcher_active) 539491990e2SDave Martin goto out; 540491990e2SDave Martin 541491990e2SDave Martin if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE) != 0) { 542491990e2SDave Martin bL_activation_notify(BL_NOTIFY_POST_ENABLE); 543491990e2SDave Martin goto out; 5446b7437aeSNicolas Pitre } 545491990e2SDave Martin 5466b7437aeSNicolas Pitre bL_switcher_active = 0; 5476b7437aeSNicolas Pitre 5486b7437aeSNicolas Pitre /* 5496b7437aeSNicolas Pitre * To deactivate the switcher, we must shut down the switcher 5506b7437aeSNicolas Pitre * threads to prevent any other requests from being accepted. 5516b7437aeSNicolas Pitre * Then, if the final cluster for given logical CPU is not the 5526b7437aeSNicolas Pitre * same as the original one, we'll recreate a switcher thread 5536b7437aeSNicolas Pitre * just for the purpose of switching the CPU back without any 5546b7437aeSNicolas Pitre * possibility for interference from external requests. 5556b7437aeSNicolas Pitre */ 5566b7437aeSNicolas Pitre for_each_online_cpu(cpu) { 5576b7437aeSNicolas Pitre t = &bL_threads[cpu]; 5586b7437aeSNicolas Pitre task = t->task; 5596b7437aeSNicolas Pitre t->task = NULL; 5606b7437aeSNicolas Pitre if (!task || IS_ERR(task)) 5616b7437aeSNicolas Pitre continue; 5626b7437aeSNicolas Pitre kthread_stop(task); 5636b7437aeSNicolas Pitre /* no more switch may happen on this CPU at this point */ 5646b7437aeSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1); 5656b7437aeSNicolas Pitre if (cluster == bL_switcher_cpu_original_cluster[cpu]) 5666b7437aeSNicolas Pitre continue; 5676b7437aeSNicolas Pitre init_completion(&t->started); 5686b7437aeSNicolas Pitre t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu]; 5696b7437aeSNicolas Pitre task = bL_switcher_thread_create(cpu, t); 5706b7437aeSNicolas Pitre if (!IS_ERR(task)) { 5716b7437aeSNicolas Pitre wait_for_completion(&t->started); 5726b7437aeSNicolas Pitre kthread_stop(task); 5736b7437aeSNicolas Pitre cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1); 5746b7437aeSNicolas Pitre if (cluster == bL_switcher_cpu_original_cluster[cpu]) 5756b7437aeSNicolas Pitre continue; 5766b7437aeSNicolas Pitre } 5776b7437aeSNicolas Pitre /* If execution gets here, we're in trouble. */ 5786b7437aeSNicolas Pitre pr_crit("%s: unable to restore original cluster for CPU %d\n", 5796b7437aeSNicolas Pitre __func__, cpu); 5806b7437aeSNicolas Pitre pr_crit("%s: CPU %d can't be restored\n", 58138c35d4fSNicolas Pitre __func__, bL_switcher_cpu_pairing[cpu]); 58238c35d4fSNicolas Pitre cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu], 58338c35d4fSNicolas Pitre &bL_switcher_removed_logical_cpus); 5846b7437aeSNicolas Pitre } 5856b7437aeSNicolas Pitre 5866b7437aeSNicolas Pitre bL_switcher_restore_cpus(); 587491990e2SDave Martin bL_activation_notify(BL_NOTIFY_POST_DISABLE); 588491990e2SDave Martin 589491990e2SDave Martin out: 5906b7437aeSNicolas Pitre cpu_hotplug_driver_unlock(); 591c0f43751SDave Martin mutex_unlock(&bL_switcher_activation_lock); 5926b7437aeSNicolas Pitre } 5936b7437aeSNicolas Pitre 5946b7437aeSNicolas Pitre static ssize_t bL_switcher_active_show(struct kobject *kobj, 5956b7437aeSNicolas Pitre struct kobj_attribute *attr, char *buf) 5966b7437aeSNicolas Pitre { 5976b7437aeSNicolas Pitre return sprintf(buf, "%u\n", bL_switcher_active); 5986b7437aeSNicolas Pitre } 5996b7437aeSNicolas Pitre 6006b7437aeSNicolas Pitre static ssize_t bL_switcher_active_store(struct kobject *kobj, 6016b7437aeSNicolas Pitre struct kobj_attribute *attr, const char *buf, size_t count) 6026b7437aeSNicolas Pitre { 6036b7437aeSNicolas Pitre int ret; 6046b7437aeSNicolas Pitre 6056b7437aeSNicolas Pitre switch (buf[0]) { 6066b7437aeSNicolas Pitre case '0': 6076b7437aeSNicolas Pitre bL_switcher_disable(); 6086b7437aeSNicolas Pitre ret = 0; 6096b7437aeSNicolas Pitre break; 6106b7437aeSNicolas Pitre case '1': 6116b7437aeSNicolas Pitre ret = bL_switcher_enable(); 6126b7437aeSNicolas Pitre break; 6136b7437aeSNicolas Pitre default: 6146b7437aeSNicolas Pitre ret = -EINVAL; 6156b7437aeSNicolas Pitre } 6166b7437aeSNicolas Pitre 6176b7437aeSNicolas Pitre return (ret >= 0) ? count : ret; 6186b7437aeSNicolas Pitre } 6196b7437aeSNicolas Pitre 6206b7437aeSNicolas Pitre static struct kobj_attribute bL_switcher_active_attr = 6216b7437aeSNicolas Pitre __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store); 6226b7437aeSNicolas Pitre 6236b7437aeSNicolas Pitre static struct attribute *bL_switcher_attrs[] = { 6246b7437aeSNicolas Pitre &bL_switcher_active_attr.attr, 6256b7437aeSNicolas Pitre NULL, 6266b7437aeSNicolas Pitre }; 6276b7437aeSNicolas Pitre 6286b7437aeSNicolas Pitre static struct attribute_group bL_switcher_attr_group = { 6296b7437aeSNicolas Pitre .attrs = bL_switcher_attrs, 6306b7437aeSNicolas Pitre }; 6316b7437aeSNicolas Pitre 6326b7437aeSNicolas Pitre static struct kobject *bL_switcher_kobj; 6336b7437aeSNicolas Pitre 6346b7437aeSNicolas Pitre static int __init bL_switcher_sysfs_init(void) 6356b7437aeSNicolas Pitre { 6366b7437aeSNicolas Pitre int ret; 6376b7437aeSNicolas Pitre 6386b7437aeSNicolas Pitre bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj); 6396b7437aeSNicolas Pitre if (!bL_switcher_kobj) 6406b7437aeSNicolas Pitre return -ENOMEM; 6416b7437aeSNicolas Pitre ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group); 6426b7437aeSNicolas Pitre if (ret) 6436b7437aeSNicolas Pitre kobject_put(bL_switcher_kobj); 6446b7437aeSNicolas Pitre return ret; 6456b7437aeSNicolas Pitre } 6466b7437aeSNicolas Pitre 6476b7437aeSNicolas Pitre #endif /* CONFIG_SYSFS */ 6486b7437aeSNicolas Pitre 649c0f43751SDave Martin bool bL_switcher_get_enabled(void) 650c0f43751SDave Martin { 651c0f43751SDave Martin mutex_lock(&bL_switcher_activation_lock); 652c0f43751SDave Martin 653c0f43751SDave Martin return bL_switcher_active; 654c0f43751SDave Martin } 655c0f43751SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_get_enabled); 656c0f43751SDave Martin 657c0f43751SDave Martin void bL_switcher_put_enabled(void) 658c0f43751SDave Martin { 659c0f43751SDave Martin mutex_unlock(&bL_switcher_activation_lock); 660c0f43751SDave Martin } 661c0f43751SDave Martin EXPORT_SYMBOL_GPL(bL_switcher_put_enabled); 662c0f43751SDave Martin 66327261435SNicolas Pitre /* 66427261435SNicolas Pitre * Veto any CPU hotplug operation on those CPUs we've removed 66527261435SNicolas Pitre * while the switcher is active. 66627261435SNicolas Pitre * We're just not ready to deal with that given the trickery involved. 66727261435SNicolas Pitre */ 66827261435SNicolas Pitre static int bL_switcher_hotplug_callback(struct notifier_block *nfb, 66927261435SNicolas Pitre unsigned long action, void *hcpu) 67027261435SNicolas Pitre { 67127261435SNicolas Pitre if (bL_switcher_active) { 67227261435SNicolas Pitre int pairing = bL_switcher_cpu_pairing[(unsigned long)hcpu]; 67327261435SNicolas Pitre switch (action & 0xf) { 67427261435SNicolas Pitre case CPU_UP_PREPARE: 67527261435SNicolas Pitre case CPU_DOWN_PREPARE: 67627261435SNicolas Pitre if (pairing == -1) 67727261435SNicolas Pitre return NOTIFY_BAD; 67827261435SNicolas Pitre } 67927261435SNicolas Pitre } 68027261435SNicolas Pitre return NOTIFY_DONE; 68127261435SNicolas Pitre } 68227261435SNicolas Pitre 683c4821c05SNicolas Pitre static bool no_bL_switcher; 684c4821c05SNicolas Pitre core_param(no_bL_switcher, no_bL_switcher, bool, 0644); 685c4821c05SNicolas Pitre 6866b7437aeSNicolas Pitre static int __init bL_switcher_init(void) 6876b7437aeSNicolas Pitre { 6886b7437aeSNicolas Pitre int ret; 6896b7437aeSNicolas Pitre 6906b7437aeSNicolas Pitre if (MAX_NR_CLUSTERS != 2) { 6916b7437aeSNicolas Pitre pr_err("%s: only dual cluster systems are supported\n", __func__); 6926b7437aeSNicolas Pitre return -EINVAL; 6936b7437aeSNicolas Pitre } 6946b7437aeSNicolas Pitre 69527261435SNicolas Pitre cpu_notifier(bL_switcher_hotplug_callback, 0); 69627261435SNicolas Pitre 697c4821c05SNicolas Pitre if (!no_bL_switcher) { 6986b7437aeSNicolas Pitre ret = bL_switcher_enable(); 6996b7437aeSNicolas Pitre if (ret) 7006b7437aeSNicolas Pitre return ret; 701c4821c05SNicolas Pitre } 7026b7437aeSNicolas Pitre 7036b7437aeSNicolas Pitre #ifdef CONFIG_SYSFS 7046b7437aeSNicolas Pitre ret = bL_switcher_sysfs_init(); 7056b7437aeSNicolas Pitre if (ret) 7066b7437aeSNicolas Pitre pr_err("%s: unable to create sysfs entry\n", __func__); 7076b7437aeSNicolas Pitre #endif 7086b7437aeSNicolas Pitre 7096b7437aeSNicolas Pitre return 0; 7106b7437aeSNicolas Pitre } 7116b7437aeSNicolas Pitre 71271ce1deeSNicolas Pitre late_initcall(bL_switcher_init); 713