Nicolas Pitre | 1c33be5 | 2012-04-12 02:56:10 -0400 | [diff] [blame] | 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-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> |
Lorenzo Pieralisi | 3f09d47 | 2012-05-16 15:55:54 +0100 | [diff] [blame^] | 18 | #include <linux/cpumask.h> |
Nicolas Pitre | 1c33be5 | 2012-04-12 02:56:10 -0400 | [diff] [blame] | 19 | #include <linux/workqueue.h> |
Lorenzo Pieralisi | 3f09d47 | 2012-05-16 15:55:54 +0100 | [diff] [blame^] | 20 | #include <linux/clockchips.h> |
| 21 | #include <linux/hrtimer.h> |
| 22 | #include <linux/tick.h> |
Nicolas Pitre | 1c33be5 | 2012-04-12 02:56:10 -0400 | [diff] [blame] | 23 | #include <linux/mm.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/irqchip/arm-gic.h> |
| 26 | |
| 27 | #include <asm/smp_plat.h> |
| 28 | #include <asm/suspend.h> |
| 29 | #include <asm/mcpm.h> |
| 30 | #include <asm/bL_switcher.h> |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | * Use our own MPIDR accessors as the generic ones in asm/cputype.h have |
| 35 | * __attribute_const__ and we don't want the compiler to assume any |
| 36 | * constness here as the value _does_ change along some code paths. |
| 37 | */ |
| 38 | |
| 39 | static int read_mpidr(void) |
| 40 | { |
| 41 | unsigned int id; |
| 42 | asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id)); |
| 43 | return id & MPIDR_HWID_BITMASK; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * bL switcher core code. |
| 48 | */ |
| 49 | |
| 50 | static void bL_do_switch(void *_unused) |
| 51 | { |
| 52 | unsigned mpidr, cpuid, clusterid, ob_cluster, ib_cluster; |
| 53 | |
| 54 | /* |
| 55 | * We now have a piece of stack borrowed from the init task's. |
| 56 | * Let's also switch to init_mm right away to match it. |
| 57 | */ |
| 58 | cpu_switch_mm(init_mm.pgd, &init_mm); |
| 59 | |
| 60 | pr_debug("%s\n", __func__); |
| 61 | |
| 62 | mpidr = read_mpidr(); |
| 63 | cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0); |
| 64 | clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1); |
| 65 | ob_cluster = clusterid; |
| 66 | ib_cluster = clusterid ^ 1; |
| 67 | |
| 68 | /* |
| 69 | * Our state has been saved at this point. Let's release our |
| 70 | * inbound CPU. |
| 71 | */ |
| 72 | mcpm_set_entry_vector(cpuid, ib_cluster, cpu_resume); |
| 73 | sev(); |
| 74 | |
| 75 | /* |
| 76 | * From this point, we must assume that our counterpart CPU might |
| 77 | * have taken over in its parallel world already, as if execution |
| 78 | * just returned from cpu_suspend(). It is therefore important to |
| 79 | * be very careful not to make any change the other guy is not |
| 80 | * expecting. This is why we need stack isolation. |
| 81 | * |
| 82 | * Fancy under cover tasks could be performed here. For now |
| 83 | * we have none. |
| 84 | */ |
| 85 | |
| 86 | /* Let's put ourself down. */ |
| 87 | mcpm_cpu_power_down(); |
| 88 | |
| 89 | /* should never get here */ |
| 90 | BUG(); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Stack isolation. To ensure 'current' remains valid, we just borrow |
| 95 | * a slice of the init/idle task which should be fairly lightly used. |
| 96 | * The borrowed area starts just above the thread_info structure located |
| 97 | * at the very bottom of the stack, aligned to a cache line. |
| 98 | */ |
| 99 | #define STACK_SIZE 256 |
| 100 | extern void call_with_stack(void (*fn)(void *), void *arg, void *sp); |
| 101 | static int bL_switchpoint(unsigned long _arg) |
| 102 | { |
| 103 | unsigned int mpidr = read_mpidr(); |
| 104 | unsigned int cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0); |
| 105 | unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1); |
| 106 | unsigned int cpu_index = cpuid + clusterid * MAX_CPUS_PER_CLUSTER; |
| 107 | void *stack = &init_thread_info + 1; |
| 108 | stack = PTR_ALIGN(stack, L1_CACHE_BYTES); |
| 109 | stack += cpu_index * STACK_SIZE + STACK_SIZE; |
| 110 | call_with_stack(bL_do_switch, (void *)_arg, stack); |
| 111 | BUG(); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Generic switcher interface |
| 116 | */ |
| 117 | |
| 118 | /* |
| 119 | * bL_switch_to - Switch to a specific cluster for the current CPU |
| 120 | * @new_cluster_id: the ID of the cluster to switch to. |
| 121 | * |
| 122 | * This function must be called on the CPU to be switched. |
| 123 | * Returns 0 on success, else a negative status code. |
| 124 | */ |
| 125 | static int bL_switch_to(unsigned int new_cluster_id) |
| 126 | { |
| 127 | unsigned int mpidr, cpuid, clusterid, ob_cluster, ib_cluster, this_cpu; |
Lorenzo Pieralisi | 3f09d47 | 2012-05-16 15:55:54 +0100 | [diff] [blame^] | 128 | struct tick_device *tdev; |
| 129 | enum clock_event_mode tdev_mode; |
Nicolas Pitre | 1c33be5 | 2012-04-12 02:56:10 -0400 | [diff] [blame] | 130 | int ret; |
| 131 | |
| 132 | mpidr = read_mpidr(); |
| 133 | cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0); |
| 134 | clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1); |
| 135 | ob_cluster = clusterid; |
| 136 | ib_cluster = clusterid ^ 1; |
| 137 | |
| 138 | if (new_cluster_id == clusterid) |
| 139 | return 0; |
| 140 | |
| 141 | pr_debug("before switch: CPU %d in cluster %d\n", cpuid, clusterid); |
| 142 | |
| 143 | /* Close the gate for our entry vectors */ |
| 144 | mcpm_set_entry_vector(cpuid, ob_cluster, NULL); |
| 145 | mcpm_set_entry_vector(cpuid, ib_cluster, NULL); |
| 146 | |
| 147 | /* |
| 148 | * Let's wake up the inbound CPU now in case it requires some delay |
| 149 | * to come online, but leave it gated in our entry vector code. |
| 150 | */ |
| 151 | ret = mcpm_cpu_power_up(cpuid, ib_cluster); |
| 152 | if (ret) { |
| 153 | pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret); |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * From this point we are entering the switch critical zone |
| 159 | * and can't take any interrupts anymore. |
| 160 | */ |
| 161 | local_irq_disable(); |
| 162 | local_fiq_disable(); |
| 163 | |
| 164 | this_cpu = smp_processor_id(); |
| 165 | |
| 166 | /* redirect GIC's SGIs to our counterpart */ |
| 167 | gic_migrate_target(cpuid + ib_cluster*4); |
| 168 | |
| 169 | /* |
| 170 | * Raise a SGI on the inbound CPU to make sure it doesn't stall |
| 171 | * in a possible WFI, such as in mcpm_power_down(). |
| 172 | */ |
| 173 | arch_send_wakeup_ipi_mask(cpumask_of(this_cpu)); |
| 174 | |
Lorenzo Pieralisi | 3f09d47 | 2012-05-16 15:55:54 +0100 | [diff] [blame^] | 175 | tdev = tick_get_device(this_cpu); |
| 176 | if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu))) |
| 177 | tdev = NULL; |
| 178 | if (tdev) { |
| 179 | tdev_mode = tdev->evtdev->mode; |
| 180 | clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN); |
| 181 | } |
| 182 | |
Nicolas Pitre | 1c33be5 | 2012-04-12 02:56:10 -0400 | [diff] [blame] | 183 | ret = cpu_pm_enter(); |
| 184 | |
| 185 | /* we can not tolerate errors at this point */ |
| 186 | if (ret) |
| 187 | panic("%s: cpu_pm_enter() returned %d\n", __func__, ret); |
| 188 | |
| 189 | /* Flip the cluster in the CPU logical map for this CPU. */ |
| 190 | cpu_logical_map(this_cpu) ^= (1 << 8); |
| 191 | |
| 192 | /* Let's do the actual CPU switch. */ |
| 193 | ret = cpu_suspend(0, bL_switchpoint); |
| 194 | if (ret > 0) |
| 195 | panic("%s: cpu_suspend() returned %d\n", __func__, ret); |
| 196 | |
| 197 | /* We are executing on the inbound CPU at this point */ |
| 198 | mpidr = read_mpidr(); |
| 199 | cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0); |
| 200 | clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1); |
| 201 | pr_debug("after switch: CPU %d in cluster %d\n", cpuid, clusterid); |
| 202 | BUG_ON(clusterid != ib_cluster); |
| 203 | |
| 204 | mcpm_cpu_powered_up(); |
| 205 | |
| 206 | ret = cpu_pm_exit(); |
| 207 | |
Lorenzo Pieralisi | 3f09d47 | 2012-05-16 15:55:54 +0100 | [diff] [blame^] | 208 | if (tdev) { |
| 209 | clockevents_set_mode(tdev->evtdev, tdev_mode); |
| 210 | clockevents_program_event(tdev->evtdev, |
| 211 | tdev->evtdev->next_event, 1); |
| 212 | } |
| 213 | |
Nicolas Pitre | 1c33be5 | 2012-04-12 02:56:10 -0400 | [diff] [blame] | 214 | local_fiq_enable(); |
| 215 | local_irq_enable(); |
| 216 | |
| 217 | if (ret) |
| 218 | pr_err("%s exiting with error %d\n", __func__, ret); |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | struct switch_args { |
| 223 | unsigned int cluster; |
| 224 | struct work_struct work; |
| 225 | }; |
| 226 | |
| 227 | static void __bL_switch_to(struct work_struct *work) |
| 228 | { |
| 229 | struct switch_args *args = container_of(work, struct switch_args, work); |
| 230 | bL_switch_to(args->cluster); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * bL_switch_request - Switch to a specific cluster for the given CPU |
| 235 | * |
| 236 | * @cpu: the CPU to switch |
| 237 | * @new_cluster_id: the ID of the cluster to switch to. |
| 238 | * |
| 239 | * This function causes a cluster switch on the given CPU. If the given |
| 240 | * CPU is the same as the calling CPU then the switch happens right away. |
| 241 | * Otherwise the request is put on a work queue to be scheduled on the |
| 242 | * remote CPU. |
| 243 | */ |
| 244 | void bL_switch_request(unsigned int cpu, unsigned int new_cluster_id) |
| 245 | { |
| 246 | unsigned int this_cpu = get_cpu(); |
| 247 | struct switch_args args; |
| 248 | |
| 249 | if (cpu == this_cpu) { |
| 250 | bL_switch_to(new_cluster_id); |
| 251 | put_cpu(); |
| 252 | return; |
| 253 | } |
| 254 | put_cpu(); |
| 255 | |
| 256 | args.cluster = new_cluster_id; |
| 257 | INIT_WORK_ONSTACK(&args.work, __bL_switch_to); |
| 258 | schedule_work_on(cpu, &args.work); |
| 259 | flush_work(&args.work); |
| 260 | } |
| 261 | EXPORT_SYMBOL_GPL(bL_switch_request); |