bL_switcher.c 21 KB

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