bL_switcher.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. cpu_up(i);
  369. }
  370. static int bL_switcher_halve_cpus(void)
  371. {
  372. int i, j, cluster_0, gic_id, ret;
  373. unsigned int cpu, cluster, mask;
  374. cpumask_t available_cpus;
  375. /* First pass to validate what we have */
  376. mask = 0;
  377. for_each_online_cpu(i) {
  378. cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
  379. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  380. if (cluster >= 2) {
  381. pr_err("%s: only dual cluster systems are supported\n", __func__);
  382. return -EINVAL;
  383. }
  384. if (WARN_ON(cpu >= MAX_CPUS_PER_CLUSTER))
  385. return -EINVAL;
  386. mask |= (1 << cluster);
  387. }
  388. if (mask != 3) {
  389. pr_err("%s: no CPU pairing possible\n", __func__);
  390. return -EINVAL;
  391. }
  392. /*
  393. * Now let's do the pairing. We match each CPU with another CPU
  394. * from a different cluster. To get a uniform scheduling behavior
  395. * without fiddling with CPU topology and compute capacity data,
  396. * we'll use logical CPUs initially belonging to the same cluster.
  397. */
  398. memset(bL_switcher_cpu_pairing, -1, sizeof(bL_switcher_cpu_pairing));
  399. cpumask_copy(&available_cpus, cpu_online_mask);
  400. cluster_0 = -1;
  401. for_each_cpu(i, &available_cpus) {
  402. int match = -1;
  403. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  404. if (cluster_0 == -1)
  405. cluster_0 = cluster;
  406. if (cluster != cluster_0)
  407. continue;
  408. cpumask_clear_cpu(i, &available_cpus);
  409. for_each_cpu(j, &available_cpus) {
  410. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(j), 1);
  411. /*
  412. * Let's remember the last match to create "odd"
  413. * pairings on purpose in order for other code not
  414. * to assume any relation between physical and
  415. * logical CPU numbers.
  416. */
  417. if (cluster != cluster_0)
  418. match = j;
  419. }
  420. if (match != -1) {
  421. bL_switcher_cpu_pairing[i] = match;
  422. cpumask_clear_cpu(match, &available_cpus);
  423. pr_info("CPU%d paired with CPU%d\n", i, match);
  424. }
  425. }
  426. /*
  427. * Now we disable the unwanted CPUs i.e. everything that has no
  428. * pairing information (that includes the pairing counterparts).
  429. */
  430. cpumask_clear(&bL_switcher_removed_logical_cpus);
  431. for_each_online_cpu(i) {
  432. cpu = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0);
  433. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 1);
  434. /* Let's take note of the GIC ID for this CPU */
  435. gic_id = gic_get_cpu_id(i);
  436. if (gic_id < 0) {
  437. pr_err("%s: bad GIC ID for CPU %d\n", __func__, i);
  438. bL_switcher_restore_cpus();
  439. return -EINVAL;
  440. }
  441. bL_gic_id[cpu][cluster] = gic_id;
  442. pr_info("GIC ID for CPU %u cluster %u is %u\n",
  443. cpu, cluster, gic_id);
  444. if (bL_switcher_cpu_pairing[i] != -1) {
  445. bL_switcher_cpu_original_cluster[i] = cluster;
  446. continue;
  447. }
  448. ret = cpu_down(i);
  449. if (ret) {
  450. bL_switcher_restore_cpus();
  451. return ret;
  452. }
  453. cpumask_set_cpu(i, &bL_switcher_removed_logical_cpus);
  454. }
  455. return 0;
  456. }
  457. /* Determine the logical CPU a given physical CPU is grouped on. */
  458. int bL_switcher_get_logical_index(u32 mpidr)
  459. {
  460. int cpu;
  461. if (!bL_switcher_active)
  462. return -EUNATCH;
  463. mpidr &= MPIDR_HWID_BITMASK;
  464. for_each_online_cpu(cpu) {
  465. int pairing = bL_switcher_cpu_pairing[cpu];
  466. if (pairing == -1)
  467. continue;
  468. if ((mpidr == cpu_logical_map(cpu)) ||
  469. (mpidr == cpu_logical_map(pairing)))
  470. return cpu;
  471. }
  472. return -EINVAL;
  473. }
  474. static void bL_switcher_trace_trigger_cpu(void *__always_unused info)
  475. {
  476. trace_cpu_migrate_current(get_ns(), read_mpidr());
  477. }
  478. int bL_switcher_trace_trigger(void)
  479. {
  480. int ret;
  481. preempt_disable();
  482. bL_switcher_trace_trigger_cpu(NULL);
  483. ret = smp_call_function(bL_switcher_trace_trigger_cpu, NULL, true);
  484. preempt_enable();
  485. return ret;
  486. }
  487. EXPORT_SYMBOL_GPL(bL_switcher_trace_trigger);
  488. static int bL_switcher_enable(void)
  489. {
  490. int cpu, ret;
  491. mutex_lock(&bL_switcher_activation_lock);
  492. lock_device_hotplug();
  493. if (bL_switcher_active) {
  494. unlock_device_hotplug();
  495. mutex_unlock(&bL_switcher_activation_lock);
  496. return 0;
  497. }
  498. pr_info("big.LITTLE switcher initializing\n");
  499. ret = bL_activation_notify(BL_NOTIFY_PRE_ENABLE);
  500. if (ret)
  501. goto error;
  502. ret = bL_switcher_halve_cpus();
  503. if (ret)
  504. goto error;
  505. bL_switcher_trace_trigger();
  506. for_each_online_cpu(cpu) {
  507. struct bL_thread *t = &bL_threads[cpu];
  508. spin_lock_init(&t->lock);
  509. init_waitqueue_head(&t->wq);
  510. init_completion(&t->started);
  511. t->wanted_cluster = -1;
  512. t->task = bL_switcher_thread_create(cpu, t);
  513. }
  514. bL_switcher_active = 1;
  515. bL_activation_notify(BL_NOTIFY_POST_ENABLE);
  516. pr_info("big.LITTLE switcher initialized\n");
  517. goto out;
  518. error:
  519. pr_warn("big.LITTLE switcher initialization failed\n");
  520. bL_activation_notify(BL_NOTIFY_POST_DISABLE);
  521. out:
  522. unlock_device_hotplug();
  523. mutex_unlock(&bL_switcher_activation_lock);
  524. return ret;
  525. }
  526. #ifdef CONFIG_SYSFS
  527. static void bL_switcher_disable(void)
  528. {
  529. unsigned int cpu, cluster;
  530. struct bL_thread *t;
  531. struct task_struct *task;
  532. mutex_lock(&bL_switcher_activation_lock);
  533. lock_device_hotplug();
  534. if (!bL_switcher_active)
  535. goto out;
  536. if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE) != 0) {
  537. bL_activation_notify(BL_NOTIFY_POST_ENABLE);
  538. goto out;
  539. }
  540. bL_switcher_active = 0;
  541. /*
  542. * To deactivate the switcher, we must shut down the switcher
  543. * threads to prevent any other requests from being accepted.
  544. * Then, if the final cluster for given logical CPU is not the
  545. * same as the original one, we'll recreate a switcher thread
  546. * just for the purpose of switching the CPU back without any
  547. * possibility for interference from external requests.
  548. */
  549. for_each_online_cpu(cpu) {
  550. t = &bL_threads[cpu];
  551. task = t->task;
  552. t->task = NULL;
  553. if (!task || IS_ERR(task))
  554. continue;
  555. kthread_stop(task);
  556. /* no more switch may happen on this CPU at this point */
  557. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
  558. if (cluster == bL_switcher_cpu_original_cluster[cpu])
  559. continue;
  560. init_completion(&t->started);
  561. t->wanted_cluster = bL_switcher_cpu_original_cluster[cpu];
  562. task = bL_switcher_thread_create(cpu, t);
  563. if (!IS_ERR(task)) {
  564. wait_for_completion(&t->started);
  565. kthread_stop(task);
  566. cluster = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
  567. if (cluster == bL_switcher_cpu_original_cluster[cpu])
  568. continue;
  569. }
  570. /* If execution gets here, we're in trouble. */
  571. pr_crit("%s: unable to restore original cluster for CPU %d\n",
  572. __func__, cpu);
  573. pr_crit("%s: CPU %d can't be restored\n",
  574. __func__, bL_switcher_cpu_pairing[cpu]);
  575. cpumask_clear_cpu(bL_switcher_cpu_pairing[cpu],
  576. &bL_switcher_removed_logical_cpus);
  577. }
  578. bL_switcher_restore_cpus();
  579. bL_switcher_trace_trigger();
  580. bL_activation_notify(BL_NOTIFY_POST_DISABLE);
  581. out:
  582. unlock_device_hotplug();
  583. mutex_unlock(&bL_switcher_activation_lock);
  584. }
  585. static ssize_t bL_switcher_active_show(struct kobject *kobj,
  586. struct kobj_attribute *attr, char *buf)
  587. {
  588. return sprintf(buf, "%u\n", bL_switcher_active);
  589. }
  590. static ssize_t bL_switcher_active_store(struct kobject *kobj,
  591. struct kobj_attribute *attr, const char *buf, size_t count)
  592. {
  593. int ret;
  594. switch (buf[0]) {
  595. case '0':
  596. bL_switcher_disable();
  597. ret = 0;
  598. break;
  599. case '1':
  600. ret = bL_switcher_enable();
  601. break;
  602. default:
  603. ret = -EINVAL;
  604. }
  605. return (ret >= 0) ? count : ret;
  606. }
  607. static ssize_t bL_switcher_trace_trigger_store(struct kobject *kobj,
  608. struct kobj_attribute *attr, const char *buf, size_t count)
  609. {
  610. int ret = bL_switcher_trace_trigger();
  611. return ret ? ret : count;
  612. }
  613. static struct kobj_attribute bL_switcher_active_attr =
  614. __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
  615. static struct kobj_attribute bL_switcher_trace_trigger_attr =
  616. __ATTR(trace_trigger, 0200, NULL, bL_switcher_trace_trigger_store);
  617. static struct attribute *bL_switcher_attrs[] = {
  618. &bL_switcher_active_attr.attr,
  619. &bL_switcher_trace_trigger_attr.attr,
  620. NULL,
  621. };
  622. static struct attribute_group bL_switcher_attr_group = {
  623. .attrs = bL_switcher_attrs,
  624. };
  625. static struct kobject *bL_switcher_kobj;
  626. static int __init bL_switcher_sysfs_init(void)
  627. {
  628. int ret;
  629. bL_switcher_kobj = kobject_create_and_add("bL_switcher", kernel_kobj);
  630. if (!bL_switcher_kobj)
  631. return -ENOMEM;
  632. ret = sysfs_create_group(bL_switcher_kobj, &bL_switcher_attr_group);
  633. if (ret)
  634. kobject_put(bL_switcher_kobj);
  635. return ret;
  636. }
  637. #endif /* CONFIG_SYSFS */
  638. bool bL_switcher_get_enabled(void)
  639. {
  640. mutex_lock(&bL_switcher_activation_lock);
  641. return bL_switcher_active;
  642. }
  643. EXPORT_SYMBOL_GPL(bL_switcher_get_enabled);
  644. void bL_switcher_put_enabled(void)
  645. {
  646. mutex_unlock(&bL_switcher_activation_lock);
  647. }
  648. EXPORT_SYMBOL_GPL(bL_switcher_put_enabled);
  649. /*
  650. * Veto any CPU hotplug operation on those CPUs we've removed
  651. * while the switcher is active.
  652. * We're just not ready to deal with that given the trickery involved.
  653. */
  654. static int bL_switcher_hotplug_callback(struct notifier_block *nfb,
  655. unsigned long action, void *hcpu)
  656. {
  657. if (bL_switcher_active) {
  658. int pairing = bL_switcher_cpu_pairing[(unsigned long)hcpu];
  659. switch (action & 0xf) {
  660. case CPU_UP_PREPARE:
  661. case CPU_DOWN_PREPARE:
  662. if (pairing == -1)
  663. return NOTIFY_BAD;
  664. }
  665. }
  666. return NOTIFY_DONE;
  667. }
  668. static bool no_bL_switcher;
  669. core_param(no_bL_switcher, no_bL_switcher, bool, 0644);
  670. static int __init bL_switcher_init(void)
  671. {
  672. int ret;
  673. if (MAX_NR_CLUSTERS != 2) {
  674. pr_err("%s: only dual cluster systems are supported\n", __func__);
  675. return -EINVAL;
  676. }
  677. cpu_notifier(bL_switcher_hotplug_callback, 0);
  678. if (!no_bL_switcher) {
  679. ret = bL_switcher_enable();
  680. if (ret)
  681. return ret;
  682. }
  683. #ifdef CONFIG_SYSFS
  684. ret = bL_switcher_sysfs_init();
  685. if (ret)
  686. pr_err("%s: unable to create sysfs entry\n", __func__);
  687. #endif
  688. return 0;
  689. }
  690. late_initcall(bL_switcher_init);