bL_switcher.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. struct tick_device *tdev;
  135. enum clock_event_mode tdev_mode;
  136. long volatile *handshake_ptr;
  137. int ipi_nr, ret;
  138. this_cpu = smp_processor_id();
  139. ob_mpidr = read_mpidr();
  140. ob_cpu = MPIDR_AFFINITY_LEVEL(ob_mpidr, 0);
  141. ob_cluster = MPIDR_AFFINITY_LEVEL(ob_mpidr, 1);
  142. BUG_ON(cpu_logical_map(this_cpu) != ob_mpidr);
  143. if (new_cluster_id == ob_cluster)
  144. return 0;
  145. that_cpu = bL_switcher_cpu_pairing[this_cpu];
  146. ib_mpidr = cpu_logical_map(that_cpu);
  147. ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
  148. ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
  149. pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
  150. this_cpu, ob_mpidr, ib_mpidr);
  151. this_cpu = smp_processor_id();
  152. /* Close the gate for our entry vectors */
  153. mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
  154. mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
  155. /* Install our "inbound alive" notifier. */
  156. init_completion(&inbound_alive);
  157. ipi_nr = register_ipi_completion(&inbound_alive, this_cpu);
  158. ipi_nr |= ((1 << 16) << bL_gic_id[ob_cpu][ob_cluster]);
  159. mcpm_set_early_poke(ib_cpu, ib_cluster, gic_get_sgir_physaddr(), ipi_nr);
  160. /*
  161. * Let's wake up the inbound CPU now in case it requires some delay
  162. * to come online, but leave it gated in our entry vector code.
  163. */
  164. ret = mcpm_cpu_power_up(ib_cpu, ib_cluster);
  165. if (ret) {
  166. pr_err("%s: mcpm_cpu_power_up() returned %d\n", __func__, ret);
  167. return ret;
  168. }
  169. /*
  170. * Raise a SGI on the inbound CPU to make sure it doesn't stall
  171. * in a possible WFI, such as in bL_power_down().
  172. */
  173. gic_send_sgi(bL_gic_id[ib_cpu][ib_cluster], 0);
  174. /*
  175. * Wait for the inbound to come up. This allows for other
  176. * tasks to be scheduled in the mean time.
  177. */
  178. wait_for_completion(&inbound_alive);
  179. mcpm_set_early_poke(ib_cpu, ib_cluster, 0, 0);
  180. /*
  181. * From this point we are entering the switch critical zone
  182. * and can't take any interrupts anymore.
  183. */
  184. local_irq_disable();
  185. local_fiq_disable();
  186. trace_cpu_migrate_begin(ktime_get_real_ns(), ob_mpidr);
  187. /* redirect GIC's SGIs to our counterpart */
  188. gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
  189. tdev = tick_get_device(this_cpu);
  190. if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
  191. tdev = NULL;
  192. if (tdev) {
  193. tdev_mode = tdev->evtdev->mode;
  194. clockevents_set_mode(tdev->evtdev, CLOCK_EVT_MODE_SHUTDOWN);
  195. }
  196. ret = cpu_pm_enter();
  197. /* we can not tolerate errors at this point */
  198. if (ret)
  199. panic("%s: cpu_pm_enter() returned %d\n", __func__, ret);
  200. /* Swap the physical CPUs in the logical map for this logical CPU. */
  201. cpu_logical_map(this_cpu) = ib_mpidr;
  202. cpu_logical_map(that_cpu) = ob_mpidr;
  203. /* Let's do the actual CPU switch. */
  204. ret = cpu_suspend((unsigned long)&handshake_ptr, bL_switchpoint);
  205. if (ret > 0)
  206. panic("%s: cpu_suspend() returned %d\n", __func__, ret);
  207. /* We are executing on the inbound CPU at this point */
  208. mpidr = read_mpidr();
  209. pr_debug("after switch: CPU %d MPIDR %#x\n", this_cpu, mpidr);
  210. BUG_ON(mpidr != ib_mpidr);
  211. mcpm_cpu_powered_up();
  212. ret = cpu_pm_exit();
  213. if (tdev) {
  214. clockevents_set_mode(tdev->evtdev, tdev_mode);
  215. clockevents_program_event(tdev->evtdev,
  216. tdev->evtdev->next_event, 1);
  217. }
  218. trace_cpu_migrate_finish(ktime_get_real_ns(), ib_mpidr);
  219. local_fiq_enable();
  220. local_irq_enable();
  221. *handshake_ptr = 1;
  222. dsb_sev();
  223. if (ret)
  224. pr_err("%s exiting with error %d\n", __func__, ret);
  225. return ret;
  226. }
  227. struct bL_thread {
  228. spinlock_t lock;
  229. struct task_struct *task;
  230. wait_queue_head_t wq;
  231. int wanted_cluster;
  232. struct completion started;
  233. bL_switch_completion_handler completer;
  234. void *completer_cookie;
  235. };
  236. static struct bL_thread bL_threads[NR_CPUS];
  237. static int bL_switcher_thread(void *arg)
  238. {
  239. struct bL_thread *t = arg;
  240. struct sched_param param = { .sched_priority = 1 };
  241. int cluster;
  242. bL_switch_completion_handler completer;
  243. void *completer_cookie;
  244. sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
  245. complete(&t->started);
  246. do {
  247. if (signal_pending(current))
  248. flush_signals(current);
  249. wait_event_interruptible(t->wq,
  250. t->wanted_cluster != -1 ||
  251. kthread_should_stop());
  252. spin_lock(&t->lock);
  253. cluster = t->wanted_cluster;
  254. completer = t->completer;
  255. completer_cookie = t->completer_cookie;
  256. t->wanted_cluster = -1;
  257. t->completer = NULL;
  258. spin_unlock(&t->lock);
  259. if (cluster != -1) {
  260. bL_switch_to(cluster);
  261. if (completer)
  262. completer(completer_cookie);
  263. }
  264. } while (!kthread_should_stop());
  265. return 0;
  266. }
  267. static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
  268. {
  269. struct task_struct *task;
  270. task = kthread_create_on_node(bL_switcher_thread, arg,
  271. cpu_to_node(cpu), "kswitcher_%d", cpu);
  272. if (!IS_ERR(task)) {
  273. kthread_bind(task, cpu);
  274. wake_up_process(task);
  275. } else
  276. pr_err("%s failed for CPU %d\n", __func__, cpu);
  277. return task;
  278. }
  279. /*
  280. * bL_switch_request_cb - Switch to a specific cluster for the given CPU,
  281. * with completion notification via a callback
  282. *
  283. * @cpu: the CPU to switch
  284. * @new_cluster_id: the ID of the cluster to switch to.
  285. * @completer: switch completion callback. if non-NULL,
  286. * @completer(@completer_cookie) will be called on completion of
  287. * the switch, in non-atomic context.
  288. * @completer_cookie: opaque context argument for @completer.
  289. *
  290. * This function causes a cluster switch on the given CPU by waking up
  291. * the appropriate switcher thread. This function may or may not return
  292. * before the switch has occurred.
  293. *
  294. * If a @completer callback function is supplied, it will be called when
  295. * the switch is complete. This can be used to determine asynchronously
  296. * when the switch is complete, regardless of when bL_switch_request()
  297. * returns. When @completer is supplied, no new switch request is permitted
  298. * for the affected CPU until after the switch is complete, and @completer
  299. * has returned.
  300. */
  301. int bL_switch_request_cb(unsigned int cpu, unsigned int new_cluster_id,
  302. bL_switch_completion_handler completer,
  303. void *completer_cookie)
  304. {
  305. struct bL_thread *t;
  306. if (cpu >= ARRAY_SIZE(bL_threads)) {
  307. pr_err("%s: cpu %d out of bounds\n", __func__, cpu);
  308. return -EINVAL;
  309. }
  310. t = &bL_threads[cpu];
  311. if (IS_ERR(t->task))
  312. return PTR_ERR(t->task);
  313. if (!t->task)
  314. return -ESRCH;
  315. spin_lock(&t->lock);
  316. if (t->completer) {
  317. spin_unlock(&t->lock);
  318. return -EBUSY;
  319. }
  320. t->completer = completer;
  321. t->completer_cookie = completer_cookie;
  322. t->wanted_cluster = new_cluster_id;
  323. spin_unlock(&t->lock);
  324. wake_up(&t->wq);
  325. return 0;
  326. }
  327. EXPORT_SYMBOL_GPL(bL_switch_request_cb);
  328. /*
  329. * Activation and configuration code.
  330. */
  331. static DEFINE_MUTEX(bL_switcher_activation_lock);
  332. static BLOCKING_NOTIFIER_HEAD(bL_activation_notifier);
  333. static unsigned int bL_switcher_active;
  334. static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
  335. static cpumask_t bL_switcher_removed_logical_cpus;
  336. int bL_switcher_register_notifier(struct notifier_block *nb)
  337. {
  338. return blocking_notifier_chain_register(&bL_activation_notifier, nb);
  339. }
  340. EXPORT_SYMBOL_GPL(bL_switcher_register_notifier);
  341. int bL_switcher_unregister_notifier(struct notifier_block *nb)
  342. {
  343. return blocking_notifier_chain_unregister(&bL_activation_notifier, nb);
  344. }
  345. EXPORT_SYMBOL_GPL(bL_switcher_unregister_notifier);
  346. static int bL_activation_notify(unsigned long val)
  347. {
  348. int ret;
  349. ret = blocking_notifier_call_chain(&bL_activation_notifier, val, NULL);
  350. if (ret & NOTIFY_STOP_MASK)
  351. pr_err("%s: notifier chain failed with status 0x%x\n",
  352. __func__, ret);
  353. return notifier_to_errno(ret);
  354. }
  355. static void bL_switcher_restore_cpus(void)
  356. {
  357. int i;
  358. for_each_cpu(i, &bL_switcher_removed_logical_cpus) {
  359. struct device *cpu_dev = get_cpu_device(i);
  360. int ret = device_online(cpu_dev);
  361. if (ret)
  362. dev_err(cpu_dev, "switcher: unable to restore CPU\n");
  363. }
  364. }
  365. static int bL_switcher_halve_cpus(void)
  366. {
  367. int i, j, cluster_0, gic_id, ret;
  368. unsigned int cpu, cluster, mask;
  369. cpumask_t available_cpus;
  370. /* First pass to validate what we have */
  371. mask = 0;
  372. for_each_online_cpu(i) {
  373. cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
  374. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  375. if (cluster >= 2) {
  376. pr_err("%s: only dual cluster systems are supported\n", __func__);
  377. return -EINVAL;
  378. }
  379. if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
  380. return -EINVAL;
  381. mask |= (1 << cluster);
  382. }
  383. if (mask != 3) {
  384. pr_err("%s: no CPU pairing possible\n", __func__);
  385. return -EINVAL;
  386. }
  387. /*
  388. * Now let's do the pairing. We match each CPU with another CPU
  389. * from a different cluster. To get a uniform scheduling behavior
  390. * without fiddling with CPU topology and compute capacity data,
  391. * we'll use logical CPUs initially belonging to the same cluster.
  392. */
  393. memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
  394. cpumask_copy(&available_cpus, cpu_online_mask);
  395. cluster_0 = -1;
  396. for_each_cpu(i, &available_cpus) {
  397. int match = -1;
  398. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  399. if (cluster_0 == -1)
  400. cluster_0 = cluster;
  401. if (cluster != cluster_0)
  402. continue;
  403. cpumask_clear_cpu(i, &available_cpus);
  404. for_each_cpu(j, &available_cpus) {
  405. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
  406. /*
  407. * Let's remember the last match to create "odd"
  408. * pairings on purpose in order for other code not
  409. * to assume any relation between physical and
  410. * logical CPU numbers.
  411. */
  412. if (cluster != cluster_0)
  413. match = j;
  414. }
  415. if (match != -1) {
  416. bL_switcher_cpu_pairing[i] = match;
  417. cpumask_clear_cpu(match, &available_cpus);
  418. pr_info("CPU%d paired with CPU%d\n", i, match);
  419. }
  420. }
  421. /*
  422. * Now we disable the unwanted CPUs i.e. everything that has no
  423. * pairing information (that includes the pairing counterparts).
  424. */
  425. cpumask_clear(&bL_switcher_removed_logical_cpus);
  426. for_each_online_cpu(i) {
  427. cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
  428. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  429. /* Let's take note of the GIC ID for this CPU */
  430. gic_id = gic_get_cpu_id(i);
  431. if (gic_id < 0) {
  432. pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
  433. bL_switcher_restore_cpus();
  434. return -EINVAL;
  435. }
  436. bL_gic_id[cpu][cluster] = gic_id;
  437. pr_info("GIC ID for CPU %u cluster %u is %u\n",
  438. cpu, cluster, gic_id);
  439. if (bL_switcher_cpu_pairing[i] != -1) {
  440. bL_switcher_cpu_original_cluster[i] = cluster;
  441. continue;
  442. }
  443. ret = device_offline(get_cpu_device(i));
  444. if (ret) {
  445. bL_switcher_restore_cpus();
  446. return ret;
  447. }
  448. cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
  449. }
  450. return 0;
  451. }
  452. /* Determine the logical CPU a given physical CPU is grouped on. */
  453. int bL_switcher_get_logical_index(u32 mpidr)
  454. {
  455. int cpu;
  456. if (!bL_switcher_active)
  457. return -EUNATCH;
  458. mpidr &= MPIDR_HWID_BITMASK;
  459. for_each_online_cpu(cpu) {
  460. int pairing = bL_switcher_cpu_pairing[cpu];
  461. if (pairing == -1)
  462. continue;
  463. if ((mpidr == cpu_logical_map(cpu)) ||
  464. (mpidr == cpu_logical_map(pairing)))
  465. return cpu;
  466. }
  467. return -EINVAL;
  468. }
  469. static void bL_switcher_trace_trigger_cpu(void *__always_unused info)
  470. {
  471. trace_cpu_migrate_current(ktime_get_real_ns(), read_mpidr());
  472. }
  473. int bL_switcher_trace_trigger(void)
  474. {
  475. int ret;
  476. preempt_disable();
  477. bL_switcher_trace_trigger_cpu(NULL);
  478. ret = smp_call_function(bL_switcher_trace_trigger_cpu, NULL, true);
  479. preempt_enable();
  480. return ret;
  481. }
  482. EXPORT_SYMBOL_GPL(bL_switcher_trace_trigger);
  483. static int bL_switcher_enable(void)
  484. {
  485. int cpu, ret;
  486. mutex_lock(&bL_switcher_activation_lock);
  487. lock_device_hotplug();
  488. if (bL_switcher_active) {
  489. unlock_device_hotplug();
  490. mutex_unlock(&bL_switcher_activation_lock);
  491. return 0;
  492. }
  493. pr_info("big.LITTLE switcher initializing\n");
  494. ret = bL_activation_notify(BL_NOTIFY_PRE_ENABLE);
  495. if (ret)
  496. goto error;
  497. ret = bL_switcher_halve_cpus();
  498. if (ret)
  499. goto error;
  500. bL_switcher_trace_trigger();
  501. for_each_online_cpu(cpu) {
  502. struct bL_thread *t = &bL_threads[cpu];
  503. spin_lock_init(&t->lock);
  504. init_waitqueue_head(&t->wq);
  505. init_completion(&t->started);
  506. t->wanted_cluster = -1;
  507. t->task = bL_switcher_thread_create(cpu, t);
  508. }
  509. bL_switcher_active = 1;
  510. bL_activation_notify(BL_NOTIFY_POST_ENABLE);
  511. pr_info("big.LITTLE switcher initialized\n");
  512. goto out;
  513. error:
  514. pr_warn("big.LITTLE switcher initialization failed\n");
  515. bL_activation_notify(BL_NOTIFY_POST_DISABLE);
  516. out:
  517. unlock_device_hotplug();
  518. mutex_unlock(&bL_switcher_activation_lock);
  519. return ret;
  520. }
  521. #ifdef CONFIG_SYSFS
  522. static void bL_switcher_disable(void)
  523. {
  524. unsigned int cpu, cluster;
  525. struct bL_thread *t;
  526. struct task_struct *task;
  527. mutex_lock(&bL_switcher_activation_lock);
  528. lock_device_hotplug();
  529. if (!bL_switcher_active)
  530. goto out;
  531. if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE) != 0) {
  532. bL_activation_notify(BL_NOTIFY_POST_ENABLE);
  533. goto out;
  534. }
  535. bL_switcher_active = 0;
  536. /*
  537. * To deactivate the switcher, we must shut down the switcher
  538. * threads to prevent any other requests from being accepted.
  539. * Then, if the final cluster for given logical CPU is not the
  540. * same as the original one, we'll recreate a switcher thread
  541. * just for the purpose of switching the CPU back without any
  542. * possibility for interference from external requests.
  543. */
  544. for_each_online_cpu(cpu) {
  545. t = &bL_threads[cpu];
  546. task = t->task;
  547. t->task = NULL;
  548. if (!task || IS_ERR(task))
  549. continue;
  550. kthread_stop(task);
  551. /* no more switch may happen on this CPU at this point */
  552. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
  553. if (cluster == bL_switcher_cpu_original_cluster[cpu])
  554. continue;
  555. init_completion(&t->started);
  556. t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
  557. task = bL_switcher_thread_create(cpu, t);
  558. if (!IS_ERR(task)) {
  559. wait_for_completion(&t->started);
  560. kthread_stop(task);
  561. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
  562. if (cluster == bL_switcher_cpu_original_cluster[cpu])
  563. continue;
  564. }
  565. /* If execution gets here, we're in trouble. */
  566. pr_crit("%s: unable to restore original cluster for CPU %d\n",
  567. __func__, cpu);
  568. pr_crit("%s: CPU %d can't be restored\n",
  569. __func__, bL_switcher_cpu_pairing[cpu]);
  570. cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
  571. &bL_switcher_removed_logical_cpus);
  572. }
  573. bL_switcher_restore_cpus();
  574. bL_switcher_trace_trigger();
  575. bL_activation_notify(BL_NOTIFY_POST_DISABLE);
  576. out:
  577. unlock_device_hotplug();
  578. mutex_unlock(&bL_switcher_activation_lock);
  579. }
  580. static ssize_t bL_switcher_active_show(struct kobject *kobj,
  581. struct kobj_attribute *attr, char *buf)
  582. {
  583. return sprintf(buf, "%u\n", bL_switcher_active);
  584. }
  585. static ssize_t bL_switcher_active_store(struct kobject *kobj,
  586. struct kobj_attribute *attr, const char *buf, size_t count)
  587. {
  588. int ret;
  589. switch (buf[0]) {
  590. case '0':
  591. bL_switcher_disable();
  592. ret = 0;
  593. break;
  594. case '1':
  595. ret = bL_switcher_enable();
  596. break;
  597. default:
  598. ret = -EINVAL;
  599. }
  600. return (ret >= 0) ? count : ret;
  601. }
  602. static ssize_t bL_switcher_trace_trigger_store(struct kobject *kobj,
  603. struct kobj_attribute *attr, const char *buf, size_t count)
  604. {
  605. int ret = bL_switcher_trace_trigger();
  606. return ret ? ret : count;
  607. }
  608. static struct kobj_attribute bL_switcher_active_attr =
  609. __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
  610. static struct kobj_attribute bL_switcher_trace_trigger_attr =
  611. __ATTR(trace_trigger, 0200, NULL, bL_switcher_trace_trigger_store);
  612. static struct attribute *bL_switcher_attrs[] = {
  613. &bL_switcher_active_attr.attr,
  614. &bL_switcher_trace_trigger_attr.attr,
  615. NULL,
  616. };
  617. static struct attribute_group bL_switcher_attr_group = {
  618. .attrs = bL_switcher_attrs,
  619. };
  620. static struct kobject *bL_switcher_kobj;
  621. static int __init bL_switcher_sysfs_init(void)
  622. {
  623. int ret;
  624. bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
  625. if (!bL_switcher_kobj)
  626. return -ENOMEM;
  627. ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
  628. if (ret)
  629. kobject_put(bL_switcher_kobj);
  630. return ret;
  631. }
  632. #endif /* CONFIG_SYSFS */
  633. bool bL_switcher_get_enabled(void)
  634. {
  635. mutex_lock(&bL_switcher_activation_lock);
  636. return bL_switcher_active;
  637. }
  638. EXPORT_SYMBOL_GPL(bL_switcher_get_enabled);
  639. void bL_switcher_put_enabled(void)
  640. {
  641. mutex_unlock(&bL_switcher_activation_lock);
  642. }
  643. EXPORT_SYMBOL_GPL(bL_switcher_put_enabled);
  644. /*
  645. * Veto any CPU hotplug operation on those CPUs we've removed
  646. * while the switcher is active.
  647. * We're just not ready to deal with that given the trickery involved.
  648. */
  649. static int bL_switcher_hotplug_callback(struct notifier_block *nfb,
  650. unsigned long action, void *hcpu)
  651. {
  652. if (bL_switcher_active) {
  653. int pairing = bL_switcher_cpu_pairing[(unsigned long)hcpu];
  654. switch (action & 0xf) {
  655. case CPU_UP_PREPARE:
  656. case CPU_DOWN_PREPARE:
  657. if (pairing == -1)
  658. return NOTIFY_BAD;
  659. }
  660. }
  661. return NOTIFY_DONE;
  662. }
  663. static bool no_bL_switcher;
  664. core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
  665. static int __init bL_switcher_init(void)
  666. {
  667. int ret;
  668. if (!mcpm_is_available())
  669. return -ENODEV;
  670. cpu_notifier(bL_switcher_hotplug_callback, 0);
  671. if (!no_bL_switcher) {
  672. ret = bL_switcher_enable();
  673. if (ret)
  674. return ret;
  675. }
  676. #ifdef CONFIG_SYSFS
  677. ret = bL_switcher_sysfs_init();
  678. if (ret)
  679. pr_err("%s: unable to create sysfs entry\n", __func__);
  680. #endif
  681. return 0;
  682. }
  683. late_initcall(bL_switcher_init);