bL_switcher.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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/sysfs.h>
  27. #include <linux/irqchip/arm-gic.h>
  28. #include <linux/moduleparam.h>
  29. #include <asm/smp_plat.h>
  30. #include <asm/suspend.h>
  31. #include <asm/mcpm.h>
  32. #include <asm/bL_switcher.h>
  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. static int read_mpidr(void)
  39. {
  40. unsigned int id;
  41. asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
  42. return id & MPIDR_HWID_BITMASK;
  43. }
  44. /*
  45. * bL switcher core code.
  46. */
  47. static void bL_do_switch(void *_unused)
  48. {
  49. unsigned ib_mpidr, ib_cpu, ib_cluster;
  50. pr_debug("%s\n", __func__);
  51. ib_mpidr = cpu_logical_map(smp_processor_id());
  52. ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
  53. ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
  54. /*
  55. * Our state has been saved at this point. Let's release our
  56. * inbound CPU.
  57. */
  58. mcpm_set_entry_vector(ib_cpu, 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. static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
  98. static int bL_switcher_cpu_pairing[NR_CPUS];
  99. /*
  100. * bL_switch_to - Switch to a specific cluster for the current CPU
  101. * @new_cluster_id: the ID of the cluster to switch to.
  102. *
  103. * This function must be called on the CPU to be switched.
  104. * Returns 0 on success, else a negative status code.
  105. */
  106. static int bL_switch_to(unsigned int new_cluster_id)
  107. {
  108. unsigned int mpidr, this_cpu, that_cpu;
  109. unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster;
  110. struct tick_device *tdev;
  111. enum clock_event_mode tdev_mode;
  112. int ret;
  113. this_cpu = smp_processor_id();
  114. ob_mpidr = read_mpidr();
  115. ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0);
  116. ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1);
  117. BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr);
  118. if (new_cluster_id == ob_cluster)
  119. return 0;
  120. that_cpu = bL_switcher_cpu_pairing[this_cpu];
  121. ib_mpidr = cpu_logical_map(that_cpu);
  122. ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
  123. ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
  124. pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
  125. this_cpu, ob_mpidr, ib_mpidr);
  126. /* Close the gate for our entry vectors */
  127. mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
  128. mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
  129. /*
  130. * Let's wake up the inbound CPU now in case it requires some delay
  131. * to come online, but leave it gated in our entry vector code.
  132. */
  133. ret = mcpm_cpu_power_up(ib_cpu, ib_cluster);
  134. if (ret) {
  135. pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
  136. return ret;
  137. }
  138. /*
  139. * From this point we are entering the switch critical zone
  140. * and can't take any interrupts anymore.
  141. */
  142. local_irq_disable();
  143. local_fiq_disable();
  144. /* redirect GIC's SGIs to our counterpart */
  145. gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
  146. /*
  147. * Raise a SGI on the inbound CPU to make sure it doesn't stall
  148. * in a possible WFI, such as in mcpm_power_down().
  149. */
  150. arch_send_wakeup_ipi_mask(cpumask_of(this_cpu));
  151. tdev = tick_get_device(this_cpu);
  152. if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
  153. tdev = NULL;
  154. if (tdev) {
  155. tdev_mode = tdev->evtdev->mode;
  156. clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
  157. }
  158. ret = cpu_pm_enter();
  159. /* we can not tolerate errors at this point */
  160. if (ret)
  161. panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
  162. /* Swap the physical CPUs in the logical map for this logical CPU. */
  163. cpu_logical_map(this_cpu) = ib_mpidr;
  164. cpu_logical_map(that_cpu) = ob_mpidr;
  165. /* Let's do the actual CPU switch. */
  166. ret = cpu_suspend(0, bL_switchpoint);
  167. if (ret > 0)
  168. panic("%s: cpu_suspend() returned %d\n", __func__, ret);
  169. /* We are executing on the inbound CPU at this point */
  170. mpidr = read_mpidr();
  171. pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr);
  172. BUG_ON(mpidr != ib_mpidr);
  173. mcpm_cpu_powered_up();
  174. ret = cpu_pm_exit();
  175. if (tdev) {
  176. clockevents_set_mode(tdev->evtdev, tdev_mode);
  177. clockevents_program_event(tdev->evtdev,
  178. tdev->evtdev->next_event, 1);
  179. }
  180. local_fiq_enable();
  181. local_irq_enable();
  182. if (ret)
  183. pr_err("%s exiting with error %d\n", __func__, ret);
  184. return ret;
  185. }
  186. struct bL_thread {
  187. struct task_struct *task;
  188. wait_queue_head_t wq;
  189. int wanted_cluster;
  190. struct completion started;
  191. };
  192. static struct bL_thread bL_threads[NR_CPUS];
  193. static int bL_switcher_thread(void *arg)
  194. {
  195. struct bL_thread *t = arg;
  196. struct sched_param param = { .sched_priority = 1 };
  197. int cluster;
  198. sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
  199. complete(&t->started);
  200. do {
  201. if (signal_pending(current))
  202. flush_signals(current);
  203. wait_event_interruptible(t->wq,
  204. t->wanted_cluster != -1 ||
  205. kthread_should_stop());
  206. cluster = xchg(&t->wanted_cluster, -1);
  207. if (cluster != -1)
  208. bL_switch_to(cluster);
  209. } while (!kthread_should_stop());
  210. return 0;
  211. }
  212. static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
  213. {
  214. struct task_struct *task;
  215. task = kthread_create_on_node(bL_switcher_thread, arg,
  216. cpu_to_node(cpu), "kswitcher_%d", cpu);
  217. if (!IS_ERR(task)) {
  218. kthread_bind(task, cpu);
  219. wake_up_process(task);
  220. } else
  221. pr_err("%s failed for CPU %d\n", __func__, cpu);
  222. return task;
  223. }
  224. /*
  225. * bL_switch_request - Switch to a specific cluster for the given CPU
  226. *
  227. * @cpu: the CPU to switch
  228. * @new_cluster_id: the ID of the cluster to switch to.
  229. *
  230. * This function causes a cluster switch on the given CPU by waking up
  231. * the appropriate switcher thread. This function may or may not return
  232. * before the switch has occurred.
  233. */
  234. int bL_switch_request(unsigned int cpu, unsigned int new_cluster_id)
  235. {
  236. struct bL_thread *t;
  237. if (cpu >= ARRAY_SIZE(bL_threads)) {
  238. pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
  239. return -EINVAL;
  240. }
  241. t = &bL_threads[cpu];
  242. if (IS_ERR(t->task))
  243. return PTR_ERR(t->task);
  244. if (!t->task)
  245. return -ESRCH;
  246. t->wanted_cluster = new_cluster_id;
  247. wake_up(&t->wq);
  248. return 0;
  249. }
  250. EXPORT_SYMBOL_GPL(bL_switch_request);
  251. /*
  252. * Activation and configuration code.
  253. */
  254. static unsigned int bL_switcher_active;
  255. static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
  256. static cpumask_t bL_switcher_removed_logical_cpus;
  257. static void bL_switcher_restore_cpus(void)
  258. {
  259. int i;
  260. for_each_cpu(i, &bL_switcher_removed_logical_cpus)
  261. cpu_up(i);
  262. }
  263. static int bL_switcher_halve_cpus(void)
  264. {
  265. int i, j, cluster_0, gic_id, ret;
  266. unsigned int cpu, cluster, mask;
  267. cpumask_t available_cpus;
  268. /* First pass to validate what we have */
  269. mask = 0;
  270. for_each_online_cpu(i) {
  271. cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
  272. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  273. if (cluster >= 2) {
  274. pr_err("%s: only dual cluster systems are supported\n", __func__);
  275. return -EINVAL;
  276. }
  277. if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
  278. return -EINVAL;
  279. mask |= (1 << cluster);
  280. }
  281. if (mask != 3) {
  282. pr_err("%s: no CPU pairing possible\n", __func__);
  283. return -EINVAL;
  284. }
  285. /*
  286. * Now let's do the pairing. We match each CPU with another CPU
  287. * from a different cluster. To get a uniform scheduling behavior
  288. * without fiddling with CPU topology and compute capacity data,
  289. * we'll use logical CPUs initially belonging to the same cluster.
  290. */
  291. memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
  292. cpumask_copy(&available_cpus, cpu_online_mask);
  293. cluster_0 = -1;
  294. for_each_cpu(i, &available_cpus) {
  295. int match = -1;
  296. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  297. if (cluster_0 == -1)
  298. cluster_0 = cluster;
  299. if (cluster != cluster_0)
  300. continue;
  301. cpumask_clear_cpu(i, &available_cpus);
  302. for_each_cpu(j, &available_cpus) {
  303. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
  304. /*
  305. * Let's remember the last match to create "odd"
  306. * pairings on purpose in order for other code not
  307. * to assume any relation between physical and
  308. * logical CPU numbers.
  309. */
  310. if (cluster != cluster_0)
  311. match = j;
  312. }
  313. if (match != -1) {
  314. bL_switcher_cpu_pairing[i] = match;
  315. cpumask_clear_cpu(match, &available_cpus);
  316. pr_info("CPU%d paired with CPU%d\n", i, match);
  317. }
  318. }
  319. /*
  320. * Now we disable the unwanted CPUs i.e. everything that has no
  321. * pairing information (that includes the pairing counterparts).
  322. */
  323. cpumask_clear(&bL_switcher_removed_logical_cpus);
  324. for_each_online_cpu(i) {
  325. cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
  326. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  327. /* Let's take note of the GIC ID for this CPU */
  328. gic_id = gic_get_cpu_id(i);
  329. if (gic_id < 0) {
  330. pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
  331. bL_switcher_restore_cpus();
  332. return -EINVAL;
  333. }
  334. bL_gic_id[cpu][cluster] = gic_id;
  335. pr_info("GIC ID for CPU %u cluster %u is %u\n",
  336. cpu, cluster, gic_id);
  337. if (bL_switcher_cpu_pairing[i] != -1) {
  338. bL_switcher_cpu_original_cluster[i] = cluster;
  339. continue;
  340. }
  341. ret = cpu_down(i);
  342. if (ret) {
  343. bL_switcher_restore_cpus();
  344. return ret;
  345. }
  346. cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
  347. }
  348. return 0;
  349. }
  350. static int bL_switcher_enable(void)
  351. {
  352. int cpu, ret;
  353. cpu_hotplug_driver_lock();
  354. if (bL_switcher_active) {
  355. cpu_hotplug_driver_unlock();
  356. return 0;
  357. }
  358. pr_info("big.LITTLE switcher initializing\n");
  359. ret = bL_switcher_halve_cpus();
  360. if (ret) {
  361. cpu_hotplug_driver_unlock();
  362. return ret;
  363. }
  364. for_each_online_cpu(cpu) {
  365. struct bL_thread *t = &bL_threads[cpu];
  366. init_waitqueue_head(&t->wq);
  367. init_completion(&t->started);
  368. t->wanted_cluster = -1;
  369. t->task = bL_switcher_thread_create(cpu, t);
  370. }
  371. bL_switcher_active = 1;
  372. cpu_hotplug_driver_unlock();
  373. pr_info("big.LITTLE switcher initialized\n");
  374. return 0;
  375. }
  376. #ifdef CONFIG_SYSFS
  377. static void bL_switcher_disable(void)
  378. {
  379. unsigned int cpu, cluster;
  380. struct bL_thread *t;
  381. struct task_struct *task;
  382. cpu_hotplug_driver_lock();
  383. if (!bL_switcher_active) {
  384. cpu_hotplug_driver_unlock();
  385. return;
  386. }
  387. bL_switcher_active = 0;
  388. /*
  389. * To deactivate the switcher, we must shut down the switcher
  390. * threads to prevent any other requests from being accepted.
  391. * Then, if the final cluster for given logical CPU is not the
  392. * same as the original one, we'll recreate a switcher thread
  393. * just for the purpose of switching the CPU back without any
  394. * possibility for interference from external requests.
  395. */
  396. for_each_online_cpu(cpu) {
  397. t = &bL_threads[cpu];
  398. task = t->task;
  399. t->task = NULL;
  400. if (!task || IS_ERR(task))
  401. continue;
  402. kthread_stop(task);
  403. /* no more switch may happen on this CPU at this point */
  404. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
  405. if (cluster == bL_switcher_cpu_original_cluster[cpu])
  406. continue;
  407. init_completion(&t->started);
  408. t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
  409. task = bL_switcher_thread_create(cpu, t);
  410. if (!IS_ERR(task)) {
  411. wait_for_completion(&t->started);
  412. kthread_stop(task);
  413. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
  414. if (cluster == bL_switcher_cpu_original_cluster[cpu])
  415. continue;
  416. }
  417. /* If execution gets here, we're in trouble. */
  418. pr_crit("%s: unable to restore original cluster for CPU %d\n",
  419. __func__, cpu);
  420. pr_crit("%s: CPU %d can't be restored\n",
  421. __func__, bL_switcher_cpu_pairing[cpu]);
  422. cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
  423. &bL_switcher_removed_logical_cpus);
  424. }
  425. bL_switcher_restore_cpus();
  426. cpu_hotplug_driver_unlock();
  427. }
  428. static ssize_t bL_switcher_active_show(struct kobject *kobj,
  429. struct kobj_attribute *attr, char *buf)
  430. {
  431. return sprintf(buf, "%u\n", bL_switcher_active);
  432. }
  433. static ssize_t bL_switcher_active_store(struct kobject *kobj,
  434. struct kobj_attribute *attr, const char *buf, size_t count)
  435. {
  436. int ret;
  437. switch (buf[0]) {
  438. case '0':
  439. bL_switcher_disable();
  440. ret = 0;
  441. break;
  442. case '1':
  443. ret = bL_switcher_enable();
  444. break;
  445. default:
  446. ret = -EINVAL;
  447. }
  448. return (ret >= 0) ? count : ret;
  449. }
  450. static struct kobj_attribute bL_switcher_active_attr =
  451. __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
  452. static struct attribute *bL_switcher_attrs[] = {
  453. &bL_switcher_active_attr.attr,
  454. NULL,
  455. };
  456. static struct attribute_group bL_switcher_attr_group = {
  457. .attrs = bL_switcher_attrs,
  458. };
  459. static struct kobject *bL_switcher_kobj;
  460. static int __init bL_switcher_sysfs_init(void)
  461. {
  462. int ret;
  463. bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
  464. if (!bL_switcher_kobj)
  465. return -ENOMEM;
  466. ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
  467. if (ret)
  468. kobject_put(bL_switcher_kobj);
  469. return ret;
  470. }
  471. #endif /* CONFIG_SYSFS */
  472. /*
  473. * Veto any CPU hotplug operation on those CPUs we've removed
  474. * while the switcher is active.
  475. * We're just not ready to deal with that given the trickery involved.
  476. */
  477. static int bL_switcher_hotplug_callback(struct notifier_block *nfb,
  478. unsigned long action, void *hcpu)
  479. {
  480. if (bL_switcher_active) {
  481. int pairing = bL_switcher_cpu_pairing[(unsigned long)hcpu];
  482. switch (action & 0xf) {
  483. case CPU_UP_PREPARE:
  484. case CPU_DOWN_PREPARE:
  485. if (pairing == -1)
  486. return NOTIFY_BAD;
  487. }
  488. }
  489. return NOTIFY_DONE;
  490. }
  491. static bool no_bL_switcher;
  492. core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
  493. static int __init bL_switcher_init(void)
  494. {
  495. int ret;
  496. if (MAX_NR_CLUSTERS != 2) {
  497. pr_err("%s: only dual cluster systems are supported\n", __func__);
  498. return -EINVAL;
  499. }
  500. cpu_notifier(bL_switcher_hotplug_callback, 0);
  501. if (!no_bL_switcher) {
  502. ret = bL_switcher_enable();
  503. if (ret)
  504. return ret;
  505. }
  506. #ifdef CONFIG_SYSFS
  507. ret = bL_switcher_sysfs_init();
  508. if (ret)
  509. pr_err("%s: unable to create sysfs entry\n", __func__);
  510. #endif
  511. return 0;
  512. }
  513. late_initcall(bL_switcher_init);