2 * arch/arm/common/bL_switcher.c -- big.LITTLE cluster switcher core driver
4 * Created by: Nicolas Pitre, March 2012
5 * Copyright: (C) 2012-2013 Linaro Limited
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.
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>
18 #include <linux/workqueue.h>
20 #include <linux/string.h>
21 #include <linux/irqchip/arm-gic.h>
23 #include <asm/smp_plat.h>
24 #include <asm/suspend.h>
26 #include <asm/bL_switcher.h>
30 * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
31 * __attribute_const__ and we don't want the compiler to assume any
32 * constness here as the value _does_ change along some code paths.
35 static int read_mpidr(void)
38 asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id
));
39 return id
& MPIDR_HWID_BITMASK
;
43 * bL switcher core code.
46 static void bL_do_switch(void *_unused
)
48 unsigned mpidr
, cpuid
, clusterid
, ob_cluster
, ib_cluster
;
51 * We now have a piece of stack borrowed from the init task's.
52 * Let's also switch to init_mm right away to match it.
54 cpu_switch_mm(init_mm
.pgd
, &init_mm
);
56 pr_debug("%s\n", __func__
);
59 cpuid
= MPIDR_AFFINITY_LEVEL(mpidr
, 0);
60 clusterid
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
61 ob_cluster
= clusterid
;
62 ib_cluster
= clusterid
^ 1;
65 * Our state has been saved at this point. Let's release our
68 mcpm_set_entry_vector(cpuid
, ib_cluster
, cpu_resume
);
72 * From this point, we must assume that our counterpart CPU might
73 * have taken over in its parallel world already, as if execution
74 * just returned from cpu_suspend(). It is therefore important to
75 * be very careful not to make any change the other guy is not
76 * expecting. This is why we need stack isolation.
78 * Fancy under cover tasks could be performed here. For now
82 /* Let's put ourself down. */
83 mcpm_cpu_power_down();
85 /* should never get here */
90 * Stack isolation. To ensure 'current' remains valid, we just borrow
91 * a slice of the init/idle task which should be fairly lightly used.
92 * The borrowed area starts just above the thread_info structure located
93 * at the very bottom of the stack, aligned to a cache line.
95 #define STACK_SIZE 256
96 extern void call_with_stack(void (*fn
)(void *), void *arg
, void *sp
);
97 static int bL_switchpoint(unsigned long _arg
)
99 unsigned int mpidr
= read_mpidr();
100 unsigned int cpuid
= MPIDR_AFFINITY_LEVEL(mpidr
, 0);
101 unsigned int clusterid
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
102 unsigned int cpu_index
= cpuid
+ clusterid
* MAX_CPUS_PER_CLUSTER
;
103 void *stack
= &init_thread_info
+ 1;
104 stack
= PTR_ALIGN(stack
, L1_CACHE_BYTES
);
105 stack
+= cpu_index
* STACK_SIZE
+ STACK_SIZE
;
106 call_with_stack(bL_do_switch
, (void *)_arg
, stack
);
111 * Generic switcher interface
115 * bL_switch_to - Switch to a specific cluster for the current CPU
116 * @new_cluster_id: the ID of the cluster to switch to.
118 * This function must be called on the CPU to be switched.
119 * Returns 0 on success, else a negative status code.
121 static int bL_switch_to(unsigned int new_cluster_id
)
123 unsigned int mpidr
, cpuid
, clusterid
, ob_cluster
, ib_cluster
, this_cpu
;
126 mpidr
= read_mpidr();
127 cpuid
= MPIDR_AFFINITY_LEVEL(mpidr
, 0);
128 clusterid
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
129 ob_cluster
= clusterid
;
130 ib_cluster
= clusterid
^ 1;
132 if (new_cluster_id
== clusterid
)
135 pr_debug("before switch: CPU %d in cluster %d\n", cpuid
, clusterid
);
137 /* Close the gate for our entry vectors */
138 mcpm_set_entry_vector(cpuid
, ob_cluster
, NULL
);
139 mcpm_set_entry_vector(cpuid
, ib_cluster
, NULL
);
142 * Let's wake up the inbound CPU now in case it requires some delay
143 * to come online, but leave it gated in our entry vector code.
145 ret
= mcpm_cpu_power_up(cpuid
, ib_cluster
);
147 pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__
, ret
);
152 * From this point we are entering the switch critical zone
153 * and can't take any interrupts anymore.
158 this_cpu
= smp_processor_id();
160 /* redirect GIC's SGIs to our counterpart */
161 gic_migrate_target(cpuid
+ ib_cluster
*4);
164 * Raise a SGI on the inbound CPU to make sure it doesn't stall
165 * in a possible WFI, such as in mcpm_power_down().
167 arch_send_wakeup_ipi_mask(cpumask_of(this_cpu
));
169 ret
= cpu_pm_enter();
171 /* we can not tolerate errors at this point */
173 panic("%s: cpu_pm_enter() returned %d\n", __func__
, ret
);
175 /* Flip the cluster in the CPU logical map for this CPU. */
176 cpu_logical_map(this_cpu
) ^= (1 << 8);
178 /* Let's do the actual CPU switch. */
179 ret
= cpu_suspend(0, bL_switchpoint
);
181 panic("%s: cpu_suspend() returned %d\n", __func__
, ret
);
183 /* We are executing on the inbound CPU at this point */
184 mpidr
= read_mpidr();
185 cpuid
= MPIDR_AFFINITY_LEVEL(mpidr
, 0);
186 clusterid
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
187 pr_debug("after switch: CPU %d in cluster %d\n", cpuid
, clusterid
);
188 BUG_ON(clusterid
!= ib_cluster
);
190 mcpm_cpu_powered_up();
198 pr_err("%s exiting with error %d\n", __func__
, ret
);
203 unsigned int cluster
;
204 struct work_struct work
;
207 static void __bL_switch_to(struct work_struct
*work
)
209 struct switch_args
*args
= container_of(work
, struct switch_args
, work
);
210 bL_switch_to(args
->cluster
);
214 * bL_switch_request - Switch to a specific cluster for the given CPU
216 * @cpu: the CPU to switch
217 * @new_cluster_id: the ID of the cluster to switch to.
219 * This function causes a cluster switch on the given CPU. If the given
220 * CPU is the same as the calling CPU then the switch happens right away.
221 * Otherwise the request is put on a work queue to be scheduled on the
224 void bL_switch_request(unsigned int cpu
, unsigned int new_cluster_id
)
226 unsigned int this_cpu
= get_cpu();
227 struct switch_args args
;
229 if (cpu
== this_cpu
) {
230 bL_switch_to(new_cluster_id
);
236 args
.cluster
= new_cluster_id
;
237 INIT_WORK_ONSTACK(&args
.work
, __bL_switch_to
);
238 schedule_work_on(cpu
, &args
.work
);
239 flush_work(&args
.work
);
241 EXPORT_SYMBOL_GPL(bL_switch_request
);
This page took 0.047586 seconds and 6 git commands to generate.