bL_switcher.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/sched.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/clockchips.h>
  22. #include <linux/hrtimer.h>
  23. #include <linux/tick.h>
  24. #include <linux/mm.h>
  25. #include <linux/string.h>
  26. #include <linux/irqchip/arm-gic.h>
  27. #include <asm/smp_plat.h>
  28. #include <asm/suspend.h>
  29. #include <asm/mcpm.h>
  30. #include <asm/bL_switcher.h>
  31. /*
  32. * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
  33. * __attribute_const__ and we don't want the compiler to assume any
  34. * constness here as the value _does_ change along some code paths.
  35. */
  36. static int read_mpidr(void)
  37. {
  38. unsigned int id;
  39. asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
  40. return id & MPIDR_HWID_BITMASK;
  41. }
  42. /*
  43. * bL switcher core code.
  44. */
  45. static void bL_do_switch(void *_unused)
  46. {
  47. unsigned mpidr, cpuid, clusterid, ob_cluster, ib_cluster;
  48. pr_debug("%s\n", __func__);
  49. mpidr = read_mpidr();
  50. cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  51. clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  52. ob_cluster = clusterid;
  53. ib_cluster = clusterid ^ 1;
  54. /*
  55. * Our state has been saved at this point. Let's release our
  56. * inbound CPU.
  57. */
  58. mcpm_set_entry_vector(cpuid, ib_cluster, cpu_resume);
  59. sev();
  60. /*
  61. * From this point, we must assume that our counterpart CPU might
  62. * have taken over in its parallel world already, as if execution
  63. * just returned from cpu_suspend(). It is therefore important to
  64. * be very careful not to make any change the other guy is not
  65. * expecting. This is why we need stack isolation.
  66. *
  67. * Fancy under cover tasks could be performed here. For now
  68. * we have none.
  69. */
  70. /* Let's put ourself down. */
  71. mcpm_cpu_power_down();
  72. /* should never get here */
  73. BUG();
  74. }
  75. /*
  76. * Stack isolation. To ensure 'current' remains valid, we just use another
  77. * piece of our thread's stack space which should be fairly lightly used.
  78. * The selected area starts just above the thread_info structure located
  79. * at the very bottom of the stack, aligned to a cache line, and indexed
  80. * with the cluster number.
  81. */
  82. #define STACK_SIZE 512
  83. extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
  84. static int bL_switchpoint(unsigned long _arg)
  85. {
  86. unsigned int mpidr = read_mpidr();
  87. unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  88. void *stack = current_thread_info() + 1;
  89. stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
  90. stack += clusterid * STACK_SIZE + STACK_SIZE;
  91. call_with_stack(bL_do_switch, (void *)_arg, stack);
  92. BUG();
  93. }
  94. /*
  95. * Generic switcher interface
  96. */
  97. /*
  98. * bL_switch_to - Switch to a specific cluster for the current CPU
  99. * @new_cluster_id: the ID of the cluster to switch to.
  100. *
  101. * This function must be called on the CPU to be switched.
  102. * Returns 0 on success, else a negative status code.
  103. */
  104. static int bL_switch_to(unsigned int new_cluster_id)
  105. {
  106. unsigned int mpidr, cpuid, clusterid, ob_cluster, ib_cluster, this_cpu;
  107. struct tick_device *tdev;
  108. enum clock_event_mode tdev_mode;
  109. int ret;
  110. mpidr = read_mpidr();
  111. cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  112. clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  113. ob_cluster = clusterid;
  114. ib_cluster = clusterid ^ 1;
  115. if (new_cluster_id == clusterid)
  116. return 0;
  117. pr_debug("before switch: CPU %d in cluster %d\n", cpuid, clusterid);
  118. /* Close the gate for our entry vectors */
  119. mcpm_set_entry_vector(cpuid, ob_cluster, NULL);
  120. mcpm_set_entry_vector(cpuid, ib_cluster, NULL);
  121. /*
  122. * Let's wake up the inbound CPU now in case it requires some delay
  123. * to come online, but leave it gated in our entry vector code.
  124. */
  125. ret = mcpm_cpu_power_up(cpuid, ib_cluster);
  126. if (ret) {
  127. pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
  128. return ret;
  129. }
  130. /*
  131. * From this point we are entering the switch critical zone
  132. * and can't take any interrupts anymore.
  133. */
  134. local_irq_disable();
  135. local_fiq_disable();
  136. this_cpu = smp_processor_id();
  137. /* redirect GIC's SGIs to our counterpart */
  138. gic_migrate_target(cpuid + ib_cluster*4);
  139. /*
  140. * Raise a SGI on the inbound CPU to make sure it doesn't stall
  141. * in a possible WFI, such as in mcpm_power_down().
  142. */
  143. arch_send_wakeup_ipi_mask(cpumask_of(this_cpu));
  144. tdev = tick_get_device(this_cpu);
  145. if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
  146. tdev = NULL;
  147. if (tdev) {
  148. tdev_mode = tdev->evtdev->mode;
  149. clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
  150. }
  151. ret = cpu_pm_enter();
  152. /* we can not tolerate errors at this point */
  153. if (ret)
  154. panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
  155. /* Flip the cluster in the CPU logical map for this CPU. */
  156. cpu_logical_map(this_cpu) ^= (1 << 8);
  157. /* Let's do the actual CPU switch. */
  158. ret = cpu_suspend(0, bL_switchpoint);
  159. if (ret > 0)
  160. panic("%s: cpu_suspend() returned %d\n", __func__, ret);
  161. /* We are executing on the inbound CPU at this point */
  162. mpidr = read_mpidr();
  163. cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  164. clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  165. pr_debug("after switch: CPU %d in cluster %d\n", cpuid, clusterid);
  166. BUG_ON(clusterid != ib_cluster);
  167. mcpm_cpu_powered_up();
  168. ret = cpu_pm_exit();
  169. if (tdev) {
  170. clockevents_set_mode(tdev->evtdev, tdev_mode);
  171. clockevents_program_event(tdev->evtdev,
  172. tdev->evtdev->next_event, 1);
  173. }
  174. local_fiq_enable();
  175. local_irq_enable();
  176. if (ret)
  177. pr_err("%s exiting with error %d\n", __func__, ret);
  178. return ret;
  179. }
  180. struct bL_thread {
  181. struct task_struct *task;
  182. wait_queue_head_t wq;
  183. int wanted_cluster;
  184. };
  185. static struct bL_thread bL_threads[NR_CPUS];
  186. static int bL_switcher_thread(void *arg)
  187. {
  188. struct bL_thread *t = arg;
  189. struct sched_param param = { .sched_priority = 1 };
  190. int cluster;
  191. sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
  192. do {
  193. if (signal_pending(current))
  194. flush_signals(current);
  195. wait_event_interruptible(t->wq,
  196. t->wanted_cluster != -1 ||
  197. kthread_should_stop());
  198. cluster = xchg(&t->wanted_cluster, -1);
  199. if (cluster != -1)
  200. bL_switch_to(cluster);
  201. } while (!kthread_should_stop());
  202. return 0;
  203. }
  204. static struct task_struct * __init bL_switcher_thread_create(int cpu, void *arg)
  205. {
  206. struct task_struct *task;
  207. task = kthread_create_on_node(bL_switcher_thread, arg,
  208. cpu_to_node(cpu), "kswitcher_%d", cpu);
  209. if (!IS_ERR(task)) {
  210. kthread_bind(task, cpu);
  211. wake_up_process(task);
  212. } else
  213. pr_err("%s failed for CPU %d\n", __func__, cpu);
  214. return task;
  215. }
  216. /*
  217. * bL_switch_request - Switch to a specific cluster for the given CPU
  218. *
  219. * @cpu: the CPU to switch
  220. * @new_cluster_id: the ID of the cluster to switch to.
  221. *
  222. * This function causes a cluster switch on the given CPU by waking up
  223. * the appropriate switcher thread. This function may or may not return
  224. * before the switch has occurred.
  225. */
  226. int bL_switch_request(unsigned int cpu, unsigned int new_cluster_id)
  227. {
  228. struct bL_thread *t;
  229. if (cpu >= ARRAY_SIZE(bL_threads)) {
  230. pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
  231. return -EINVAL;
  232. }
  233. t = &bL_threads[cpu];
  234. if (IS_ERR(t->task))
  235. return PTR_ERR(t->task);
  236. if (!t->task)
  237. return -ESRCH;
  238. t->wanted_cluster = new_cluster_id;
  239. wake_up(&t->wq);
  240. return 0;
  241. }
  242. EXPORT_SYMBOL_GPL(bL_switch_request);
  243. /*
  244. * Activation and configuration code.
  245. */
  246. static cpumask_t bL_switcher_removed_logical_cpus;
  247. static void __init bL_switcher_restore_cpus(void)
  248. {
  249. int i;
  250. for_each_cpu(i, &bL_switcher_removed_logical_cpus)
  251. cpu_up(i);
  252. }
  253. static int __init bL_switcher_halve_cpus(void)
  254. {
  255. int cpu, cluster, i, ret;
  256. cpumask_t cluster_mask[2], common_mask;
  257. cpumask_clear(&bL_switcher_removed_logical_cpus);
  258. cpumask_clear(&cluster_mask[0]);
  259. cpumask_clear(&cluster_mask[1]);
  260. for_each_online_cpu(i) {
  261. cpu = cpu_logical_map(i) & 0xff;
  262. cluster = (cpu_logical_map(i) >> 8) & 0xff;
  263. if (cluster >= 2) {
  264. pr_err("%s: only dual cluster systems are supported\n", __func__);
  265. return -EINVAL;
  266. }
  267. cpumask_set_cpu(cpu, &cluster_mask[cluster]);
  268. }
  269. if (!cpumask_and(&common_mask, &cluster_mask[0], &cluster_mask[1])) {
  270. pr_err("%s: no common set of CPUs\n", __func__);
  271. return -EINVAL;
  272. }
  273. for_each_online_cpu(i) {
  274. cpu = cpu_logical_map(i) & 0xff;
  275. cluster = (cpu_logical_map(i) >> 8) & 0xff;
  276. if (cpumask_test_cpu(cpu, &common_mask)) {
  277. /*
  278. * We keep only those logical CPUs which number
  279. * is equal to their physical CPU number. This is
  280. * not perfect but good enough for now.
  281. */
  282. if (cpu == i)
  283. continue;
  284. }
  285. ret = cpu_down(i);
  286. if (ret) {
  287. bL_switcher_restore_cpus();
  288. return ret;
  289. }
  290. cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
  291. }
  292. return 0;
  293. }
  294. static int __init bL_switcher_init(void)
  295. {
  296. int cpu, ret;
  297. pr_info("big.LITTLE switcher initializing\n");
  298. if (MAX_NR_CLUSTERS != 2) {
  299. pr_err("%s: only dual cluster systems are supported\n", __func__);
  300. return -EINVAL;
  301. }
  302. cpu_hotplug_driver_lock();
  303. ret = bL_switcher_halve_cpus();
  304. if (ret) {
  305. cpu_hotplug_driver_unlock();
  306. return ret;
  307. }
  308. for_each_online_cpu(cpu) {
  309. struct bL_thread *t = &bL_threads[cpu];
  310. init_waitqueue_head(&t->wq);
  311. t->wanted_cluster = -1;
  312. t->task = bL_switcher_thread_create(cpu, t);
  313. }
  314. cpu_hotplug_driver_unlock();
  315. pr_info("big.LITTLE switcher initialized\n");
  316. return 0;
  317. }
  318. late_initcall(bL_switcher_init);