bL_switcher.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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/atomic.h>
  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/cpu.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/kthread.h>
  21. #include <linux/wait.h>
  22. #include <linux/time.h>
  23. #include <linux/clockchips.h>
  24. #include <linux/hrtimer.h>
  25. #include <linux/tick.h>
  26. #include <linux/notifier.h>
  27. #include <linux/mm.h>
  28. #include <linux/mutex.h>
  29. #include <linux/smp.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/string.h>
  32. #include <linux/sysfs.h>
  33. #include <linux/irqchip/arm-gic.h>
  34. #include <linux/moduleparam.h>
  35. #include <asm/smp_plat.h>
  36. #include <asm/cputype.h>
  37. #include <asm/suspend.h>
  38. #include <asm/mcpm.h>
  39. #include <asm/bL_switcher.h>
  40. #define CREATE_TRACE_POINTS
  41. #include <trace/events/power_cpu_migrate.h>
  42. /*
  43. * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
  44. * __attribute_const__ and we don't want the compiler to assume any
  45. * constness here as the value _does_ change along some code paths.
  46. */
  47. static int read_mpidr(void)
  48. {
  49. unsigned int id;
  50. asm volatile ("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));
  51. return id & MPIDR_HWID_BITMASK;
  52. }
  53. /*
  54. * bL switcher core code.
  55. */
  56. static void bL_do_switch(void *_arg)
  57. {
  58. unsigned ib_mpidr, ib_cpu, ib_cluster;
  59. long volatile handshake, **handshake_ptr = _arg;
  60. pr_debug("%s\n", __func__);
  61. ib_mpidr = cpu_logical_map(smp_processor_id());
  62. ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
  63. ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
  64. /* Advertise our handshake location */
  65. if (handshake_ptr) {
  66. handshake = 0;
  67. *handshake_ptr = &handshake;
  68. } else
  69. handshake = -1;
  70. /*
  71. * Our state has been saved at this point. Let's release our
  72. * inbound CPU.
  73. */
  74. mcpm_set_entry_vector(ib_cpu, ib_cluster, cpu_resume);
  75. sev();
  76. /*
  77. * From this point, we must assume that our counterpart CPU might
  78. * have taken over in its parallel world already, as if execution
  79. * just returned from cpu_suspend(). It is therefore important to
  80. * be very careful not to make any change the other guy is not
  81. * expecting. This is why we need stack isolation.
  82. *
  83. * Fancy under cover tasks could be performed here. For now
  84. * we have none.
  85. */
  86. /*
  87. * Let's wait until our inbound is alive.
  88. */
  89. while (!handshake) {
  90. wfe();
  91. smp_mb();
  92. }
  93. /* Let's put ourself down. */
  94. mcpm_cpu_power_down();
  95. /* should never get here */
  96. BUG();
  97. }
  98. /*
  99. * Stack isolation. To ensure 'current' remains valid, we just use another
  100. * piece of our thread's stack space which should be fairly lightly used.
  101. * The selected area starts just above the thread_info structure located
  102. * at the very bottom of the stack, aligned to a cache line, and indexed
  103. * with the cluster number.
  104. */
  105. #define STACK_SIZE 512
  106. extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
  107. static int bL_switchpoint(unsigned long _arg)
  108. {
  109. unsigned int mpidr = read_mpidr();
  110. unsigned int clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  111. void *stack = current_thread_info() + 1;
  112. stack = PTR_ALIGN(stack, L1_CACHE_BYTES);
  113. stack += clusterid * STACK_SIZE + STACK_SIZE;
  114. call_with_stack(bL_do_switch, (void *)_arg, stack);
  115. BUG();
  116. }
  117. /*
  118. * Generic switcher interface
  119. */
  120. static unsigned int bL_gic_id[MAX_CPUS_PER_CLUSTER][MAX_NR_CLUSTERS];
  121. static int bL_switcher_cpu_pairing[NR_CPUS];
  122. /*
  123. * bL_switch_to - Switch to a specific cluster for the current CPU
  124. * @new_cluster_id: the ID of the cluster to switch to.
  125. *
  126. * This function must be called on the CPU to be switched.
  127. * Returns 0 on success, else a negative status code.
  128. */
  129. static int bL_switch_to(unsigned int new_cluster_id)
  130. {
  131. unsigned int mpidr, this_cpu, that_cpu;
  132. unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster;
  133. struct completion inbound_alive;
  134. long volatile *handshake_ptr;
  135. int ipi_nr, ret;
  136. this_cpu = smp_processor_id();
  137. ob_mpidr = read_mpidr();
  138. ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0);
  139. ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1);
  140. BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr);
  141. if (new_cluster_id == ob_cluster)
  142. return 0;
  143. that_cpu = bL_switcher_cpu_pairing[this_cpu];
  144. ib_mpidr = cpu_logical_map(that_cpu);
  145. ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
  146. ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
  147. pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
  148. this_cpu, ob_mpidr, ib_mpidr);
  149. this_cpu = smp_processor_id();
  150. /* Close the gate for our entry vectors */
  151. mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
  152. mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
  153. /* Install our "inbound alive" notifier. */
  154. init_completion(&inbound_alive);
  155. ipi_nr = register_ipi_completion(&inbound_alive, this_cpu);
  156. ipi_nr |= ((1 << 16) << bL_gic_id[ob_cpu][ob_cluster]);
  157. mcpm_set_early_poke(ib_cpu, ib_cluster, gic_get_sgir_physaddr(), ipi_nr);
  158. /*
  159. * Let's wake up the inbound CPU now in case it requires some delay
  160. * to come online, but leave it gated in our entry vector code.
  161. */
  162. ret = mcpm_cpu_power_up(ib_cpu, ib_cluster);
  163. if (ret) {
  164. pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
  165. return ret;
  166. }
  167. /*
  168. * Raise a SGI on the inbound CPU to make sure it doesn't stall
  169. * in a possible WFI, such as in bL_power_down().
  170. */
  171. gic_send_sgi(bL_gic_id[ib_cpu][ib_cluster], 0);
  172. /*
  173. * Wait for the inbound to come up. This allows for other
  174. * tasks to be scheduled in the mean time.
  175. */
  176. wait_for_completion(&inbound_alive);
  177. mcpm_set_early_poke(ib_cpu, ib_cluster, 0, 0);
  178. /*
  179. * From this point we are entering the switch critical zone
  180. * and can't take any interrupts anymore.
  181. */
  182. local_irq_disable();
  183. local_fiq_disable();
  184. trace_cpu_migrate_begin(ktime_get_real_ns(), ob_mpidr);
  185. /* redirect GIC's SGIs to our counterpart */
  186. gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
  187. tick_suspend_local();
  188. ret = cpu_pm_enter();
  189. /* we can not tolerate errors at this point */
  190. if (ret)
  191. panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
  192. /* Swap the physical CPUs in the logical map for this logical CPU. */
  193. cpu_logical_map(this_cpu) = ib_mpidr;
  194. cpu_logical_map(that_cpu) = ob_mpidr;
  195. /* Let's do the actual CPU switch. */
  196. ret = cpu_suspend((unsigned long)&handshake_ptr, bL_switchpoint);
  197. if (ret > 0)
  198. panic("%s: cpu_suspend() returned %d\n", __func__, ret);
  199. /* We are executing on the inbound CPU at this point */
  200. mpidr = read_mpidr();
  201. pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr);
  202. BUG_ON(mpidr != ib_mpidr);
  203. mcpm_cpu_powered_up();
  204. ret = cpu_pm_exit();
  205. tick_resume_local();
  206. trace_cpu_migrate_finish(ktime_get_real_ns(), ib_mpidr);
  207. local_fiq_enable();
  208. local_irq_enable();
  209. *handshake_ptr = 1;
  210. dsb_sev();
  211. if (ret)
  212. pr_err("%s exiting with error %d\n", __func__, ret);
  213. return ret;
  214. }
  215. struct bL_thread {
  216. spinlock_t lock;
  217. struct task_struct *task;
  218. wait_queue_head_t wq;
  219. int wanted_cluster;
  220. struct completion started;
  221. bL_switch_completion_handler completer;
  222. void *completer_cookie;
  223. };
  224. static struct bL_thread bL_threads[NR_CPUS];
  225. static int bL_switcher_thread(void *arg)
  226. {
  227. struct bL_thread *t = arg;
  228. struct sched_param param = { .sched_priority = 1 };
  229. int cluster;
  230. bL_switch_completion_handler completer;
  231. void *completer_cookie;
  232. sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
  233. complete(&t->started);
  234. do {
  235. if (signal_pending(current))
  236. flush_signals(current);
  237. wait_event_interruptible(t->wq,
  238. t->wanted_cluster != -1 ||
  239. kthread_should_stop());
  240. spin_lock(&t->lock);
  241. cluster = t->wanted_cluster;
  242. completer = t->completer;
  243. completer_cookie = t->completer_cookie;
  244. t->wanted_cluster = -1;
  245. t->completer = NULL;
  246. spin_unlock(&t->lock);
  247. if (cluster != -1) {
  248. bL_switch_to(cluster);
  249. if (completer)
  250. completer(completer_cookie);
  251. }
  252. } while (!kthread_should_stop());
  253. return 0;
  254. }
  255. static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
  256. {
  257. struct task_struct *task;
  258. task = kthread_create_on_node(bL_switcher_thread, arg,
  259. cpu_to_node(cpu), "kswitcher_%d", cpu);
  260. if (!IS_ERR(task)) {
  261. kthread_bind(task, cpu);
  262. wake_up_process(task);
  263. } else
  264. pr_err("%s failed for CPU %d\n", __func__, cpu);
  265. return task;
  266. }
  267. /*
  268. * bL_switch_request_cb - Switch to a specific cluster for the given CPU,
  269. * with completion notification via a callback
  270. *
  271. * @cpu: the CPU to switch
  272. * @new_cluster_id: the ID of the cluster to switch to.
  273. * @completer: switch completion callback. if non-NULL,
  274. * @completer(@completer_cookie) will be called on completion of
  275. * the switch, in non-atomic context.
  276. * @completer_cookie: opaque context argument for @completer.
  277. *
  278. * This function causes a cluster switch on the given CPU by waking up
  279. * the appropriate switcher thread. This function may or may not return
  280. * before the switch has occurred.
  281. *
  282. * If a @completer callback function is supplied, it will be called when
  283. * the switch is complete. This can be used to determine asynchronously
  284. * when the switch is complete, regardless of when bL_switch_request()
  285. * returns. When @completer is supplied, no new switch request is permitted
  286. * for the affected CPU until after the switch is complete, and @completer
  287. * has returned.
  288. */
  289. int bL_switch_request_cb(unsigned int cpu, unsigned int new_cluster_id,
  290. bL_switch_completion_handler completer,
  291. void *completer_cookie)
  292. {
  293. struct bL_thread *t;
  294. if (cpu >= ARRAY_SIZE(bL_threads)) {
  295. pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
  296. return -EINVAL;
  297. }
  298. t = &bL_threads[cpu];
  299. if (IS_ERR(t->task))
  300. return PTR_ERR(t->task);
  301. if (!t->task)
  302. return -ESRCH;
  303. spin_lock(&t->lock);
  304. if (t->completer) {
  305. spin_unlock(&t->lock);
  306. return -EBUSY;
  307. }
  308. t->completer = completer;
  309. t->completer_cookie = completer_cookie;
  310. t->wanted_cluster = new_cluster_id;
  311. spin_unlock(&t->lock);
  312. wake_up(&t->wq);
  313. return 0;
  314. }
  315. EXPORT_SYMBOL_GPL(bL_switch_request_cb);
  316. /*
  317. * Activation and configuration code.
  318. */
  319. static DEFINE_MUTEX(bL_switcher_activation_lock);
  320. static BLOCKING_NOTIFIER_HEAD(bL_activation_notifier);
  321. static unsigned int bL_switcher_active;
  322. static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
  323. static cpumask_t bL_switcher_removed_logical_cpus;
  324. int bL_switcher_register_notifier(struct notifier_block *nb)
  325. {
  326. return blocking_notifier_chain_register(&bL_activation_notifier, nb);
  327. }
  328. EXPORT_SYMBOL_GPL(bL_switcher_register_notifier);
  329. int bL_switcher_unregister_notifier(struct notifier_block *nb)
  330. {
  331. return blocking_notifier_chain_unregister(&bL_activation_notifier, nb);
  332. }
  333. EXPORT_SYMBOL_GPL(bL_switcher_unregister_notifier);
  334. static int bL_activation_notify(unsigned long val)
  335. {
  336. int ret;
  337. ret = blocking_notifier_call_chain(&bL_activation_notifier, val, NULL);
  338. if (ret & NOTIFY_STOP_MASK)
  339. pr_err("%s: notifier chain failed with status 0x%x\n",
  340. __func__, ret);
  341. return notifier_to_errno(ret);
  342. }
  343. static void bL_switcher_restore_cpus(void)
  344. {
  345. int i;
  346. for_each_cpu(i, &bL_switcher_removed_logical_cpus) {
  347. struct device *cpu_dev = get_cpu_device(i);
  348. int ret = device_online(cpu_dev);
  349. if (ret)
  350. dev_err(cpu_dev, "switcher: unable to restore CPU\n");
  351. }
  352. }
  353. static int bL_switcher_halve_cpus(void)
  354. {
  355. int i, j, cluster_0, gic_id, ret;
  356. unsigned int cpu, cluster, mask;
  357. cpumask_t available_cpus;
  358. /* First pass to validate what we have */
  359. mask = 0;
  360. for_each_online_cpu(i) {
  361. cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
  362. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  363. if (cluster >= 2) {
  364. pr_err("%s: only dual cluster systems are supported\n", __func__);
  365. return -EINVAL;
  366. }
  367. if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
  368. return -EINVAL;
  369. mask |= (1 << cluster);
  370. }
  371. if (mask != 3) {
  372. pr_err("%s: no CPU pairing possible\n", __func__);
  373. return -EINVAL;
  374. }
  375. /*
  376. * Now let's do the pairing. We match each CPU with another CPU
  377. * from a different cluster. To get a uniform scheduling behavior
  378. * without fiddling with CPU topology and compute capacity data,
  379. * we'll use logical CPUs initially belonging to the same cluster.
  380. */
  381. memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
  382. cpumask_copy(&available_cpus, cpu_online_mask);
  383. cluster_0 = -1;
  384. for_each_cpu(i, &available_cpus) {
  385. int match = -1;
  386. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  387. if (cluster_0 == -1)
  388. cluster_0 = cluster;
  389. if (cluster != cluster_0)
  390. continue;
  391. cpumask_clear_cpu(i, &available_cpus);
  392. for_each_cpu(j, &available_cpus) {
  393. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
  394. /*
  395. * Let's remember the last match to create "odd"
  396. * pairings on purpose in order for other code not
  397. * to assume any relation between physical and
  398. * logical CPU numbers.
  399. */
  400. if (cluster != cluster_0)
  401. match = j;
  402. }
  403. if (match != -1) {
  404. bL_switcher_cpu_pairing[i] = match;
  405. cpumask_clear_cpu(match, &available_cpus);
  406. pr_info("CPU%d paired with CPU%d\n", i, match);
  407. }
  408. }
  409. /*
  410. * Now we disable the unwanted CPUs i.e. everything that has no
  411. * pairing information (that includes the pairing counterparts).
  412. */
  413. cpumask_clear(&bL_switcher_removed_logical_cpus);
  414. for_each_online_cpu(i) {
  415. cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
  416. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  417. /* Let's take note of the GIC ID for this CPU */
  418. gic_id = gic_get_cpu_id(i);
  419. if (gic_id < 0) {
  420. pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
  421. bL_switcher_restore_cpus();
  422. return -EINVAL;
  423. }
  424. bL_gic_id[cpu][cluster] = gic_id;
  425. pr_info("GIC ID for CPU %u cluster %u is %u\n",
  426. cpu, cluster, gic_id);
  427. if (bL_switcher_cpu_pairing[i] != -1) {
  428. bL_switcher_cpu_original_cluster[i] = cluster;
  429. continue;
  430. }
  431. ret = device_offline(get_cpu_device(i));
  432. if (ret) {
  433. bL_switcher_restore_cpus();
  434. return ret;
  435. }
  436. cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
  437. }
  438. return 0;
  439. }
  440. /* Determine the logical CPU a given physical CPU is grouped on. */
  441. int bL_switcher_get_logical_index(u32 mpidr)
  442. {
  443. int cpu;
  444. if (!bL_switcher_active)
  445. return -EUNATCH;
  446. mpidr &= MPIDR_HWID_BITMASK;
  447. for_each_online_cpu(cpu) {
  448. int pairing = bL_switcher_cpu_pairing[cpu];
  449. if (pairing == -1)
  450. continue;
  451. if ((mpidr == cpu_logical_map(cpu)) ||
  452. (mpidr == cpu_logical_map(pairing)))
  453. return cpu;
  454. }
  455. return -EINVAL;
  456. }
  457. static void bL_switcher_trace_trigger_cpu(void *__always_unused info)
  458. {
  459. trace_cpu_migrate_current(ktime_get_real_ns(), read_mpidr());
  460. }
  461. int bL_switcher_trace_trigger(void)
  462. {
  463. int ret;
  464. preempt_disable();
  465. bL_switcher_trace_trigger_cpu(NULL);
  466. ret = smp_call_function(bL_switcher_trace_trigger_cpu, NULL, true);
  467. preempt_enable();
  468. return ret;
  469. }
  470. EXPORT_SYMBOL_GPL(bL_switcher_trace_trigger);
  471. static int bL_switcher_enable(void)
  472. {
  473. int cpu, ret;
  474. mutex_lock(&bL_switcher_activation_lock);
  475. lock_device_hotplug();
  476. if (bL_switcher_active) {
  477. unlock_device_hotplug();
  478. mutex_unlock(&bL_switcher_activation_lock);
  479. return 0;
  480. }
  481. pr_info("big.LITTLE switcher initializing\n");
  482. ret = bL_activation_notify(BL_NOTIFY_PRE_ENABLE);
  483. if (ret)
  484. goto error;
  485. ret = bL_switcher_halve_cpus();
  486. if (ret)
  487. goto error;
  488. bL_switcher_trace_trigger();
  489. for_each_online_cpu(cpu) {
  490. struct bL_thread *t = &bL_threads[cpu];
  491. spin_lock_init(&t->lock);
  492. init_waitqueue_head(&t->wq);
  493. init_completion(&t->started);
  494. t->wanted_cluster = -1;
  495. t->task = bL_switcher_thread_create(cpu, t);
  496. }
  497. bL_switcher_active = 1;
  498. bL_activation_notify(BL_NOTIFY_POST_ENABLE);
  499. pr_info("big.LITTLE switcher initialized\n");
  500. goto out;
  501. error:
  502. pr_warn("big.LITTLE switcher initialization failed\n");
  503. bL_activation_notify(BL_NOTIFY_POST_DISABLE);
  504. out:
  505. unlock_device_hotplug();
  506. mutex_unlock(&bL_switcher_activation_lock);
  507. return ret;
  508. }
  509. #ifdef CONFIG_SYSFS
  510. static void bL_switcher_disable(void)
  511. {
  512. unsigned int cpu, cluster;
  513. struct bL_thread *t;
  514. struct task_struct *task;
  515. mutex_lock(&bL_switcher_activation_lock);
  516. lock_device_hotplug();
  517. if (!bL_switcher_active)
  518. goto out;
  519. if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE) != 0) {
  520. bL_activation_notify(BL_NOTIFY_POST_ENABLE);
  521. goto out;
  522. }
  523. bL_switcher_active = 0;
  524. /*
  525. * To deactivate the switcher, we must shut down the switcher
  526. * threads to prevent any other requests from being accepted.
  527. * Then, if the final cluster for given logical CPU is not the
  528. * same as the original one, we'll recreate a switcher thread
  529. * just for the purpose of switching the CPU back without any
  530. * possibility for interference from external requests.
  531. */
  532. for_each_online_cpu(cpu) {
  533. t = &bL_threads[cpu];
  534. task = t->task;
  535. t->task = NULL;
  536. if (!task || IS_ERR(task))
  537. continue;
  538. kthread_stop(task);
  539. /* no more switch may happen on this CPU at this point */
  540. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
  541. if (cluster == bL_switcher_cpu_original_cluster[cpu])
  542. continue;
  543. init_completion(&t->started);
  544. t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
  545. task = bL_switcher_thread_create(cpu, t);
  546. if (!IS_ERR(task)) {
  547. wait_for_completion(&t->started);
  548. kthread_stop(task);
  549. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
  550. if (cluster == bL_switcher_cpu_original_cluster[cpu])
  551. continue;
  552. }
  553. /* If execution gets here, we're in trouble. */
  554. pr_crit("%s: unable to restore original cluster for CPU %d\n",
  555. __func__, cpu);
  556. pr_crit("%s: CPU %d can't be restored\n",
  557. __func__, bL_switcher_cpu_pairing[cpu]);
  558. cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
  559. &bL_switcher_removed_logical_cpus);
  560. }
  561. bL_switcher_restore_cpus();
  562. bL_switcher_trace_trigger();
  563. bL_activation_notify(BL_NOTIFY_POST_DISABLE);
  564. out:
  565. unlock_device_hotplug();
  566. mutex_unlock(&bL_switcher_activation_lock);
  567. }
  568. static ssize_t bL_switcher_active_show(struct kobject *kobj,
  569. struct kobj_attribute *attr, char *buf)
  570. {
  571. return sprintf(buf, "%u\n", bL_switcher_active);
  572. }
  573. static ssize_t bL_switcher_active_store(struct kobject *kobj,
  574. struct kobj_attribute *attr, const char *buf, size_t count)
  575. {
  576. int ret;
  577. switch (buf[0]) {
  578. case '0':
  579. bL_switcher_disable();
  580. ret = 0;
  581. break;
  582. case '1':
  583. ret = bL_switcher_enable();
  584. break;
  585. default:
  586. ret = -EINVAL;
  587. }
  588. return (ret >= 0) ? count : ret;
  589. }
  590. static ssize_t bL_switcher_trace_trigger_store(struct kobject *kobj,
  591. struct kobj_attribute *attr, const char *buf, size_t count)
  592. {
  593. int ret = bL_switcher_trace_trigger();
  594. return ret ? ret : count;
  595. }
  596. static struct kobj_attribute bL_switcher_active_attr =
  597. __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
  598. static struct kobj_attribute bL_switcher_trace_trigger_attr =
  599. __ATTR(trace_trigger, 0200, NULL, bL_switcher_trace_trigger_store);
  600. static struct attribute *bL_switcher_attrs[] = {
  601. &bL_switcher_active_attr.attr,
  602. &bL_switcher_trace_trigger_attr.attr,
  603. NULL,
  604. };
  605. static struct attribute_group bL_switcher_attr_group = {
  606. .attrs = bL_switcher_attrs,
  607. };
  608. static struct kobject *bL_switcher_kobj;
  609. static int __init bL_switcher_sysfs_init(void)
  610. {
  611. int ret;
  612. bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
  613. if (!bL_switcher_kobj)
  614. return -ENOMEM;
  615. ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
  616. if (ret)
  617. kobject_put(bL_switcher_kobj);
  618. return ret;
  619. }
  620. #endif /* CONFIG_SYSFS */
  621. bool bL_switcher_get_enabled(void)
  622. {
  623. mutex_lock(&bL_switcher_activation_lock);
  624. return bL_switcher_active;
  625. }
  626. EXPORT_SYMBOL_GPL(bL_switcher_get_enabled);
  627. void bL_switcher_put_enabled(void)
  628. {
  629. mutex_unlock(&bL_switcher_activation_lock);
  630. }
  631. EXPORT_SYMBOL_GPL(bL_switcher_put_enabled);
  632. /*
  633. * Veto any CPU hotplug operation on those CPUs we've removed
  634. * while the switcher is active.
  635. * We're just not ready to deal with that given the trickery involved.
  636. */
  637. static int bL_switcher_hotplug_callback(struct notifier_block *nfb,
  638. unsigned long action, void *hcpu)
  639. {
  640. if (bL_switcher_active) {
  641. int pairing = bL_switcher_cpu_pairing[(unsigned long)hcpu];
  642. switch (action & 0xf) {
  643. case CPU_UP_PREPARE:
  644. case CPU_DOWN_PREPARE:
  645. if (pairing == -1)
  646. return NOTIFY_BAD;
  647. }
  648. }
  649. return NOTIFY_DONE;
  650. }
  651. static bool no_bL_switcher;
  652. core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
  653. static int __init bL_switcher_init(void)
  654. {
  655. int ret;
  656. if (!mcpm_is_available())
  657. return -ENODEV;
  658. cpu_notifier(bL_switcher_hotplug_callback, 0);
  659. if (!no_bL_switcher) {
  660. ret = bL_switcher_enable();
  661. if (ret)
  662. return ret;
  663. }
  664. #ifdef CONFIG_SYSFS
  665. ret = bL_switcher_sysfs_init();
  666. if (ret)
  667. pr_err("%s: unable to create sysfs entry\n", __func__);
  668. #endif
  669. return 0;
  670. }
  671. late_initcall(bL_switcher_init);