bL_switcher.c 21 KB

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