bL_switcher.c 20 KB

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