mcpm_entry.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch/arm/common/mcpm_entry.c -- entry point for multi-cluster PM
  4. *
  5. * Created by: Nicolas Pitre, March 2012
  6. * Copyright: (C) 2012-2013 Linaro Limited
  7. */
  8. #include <linux/export.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/irqflags.h>
  12. #include <linux/cpu_pm.h>
  13. #include <asm/mcpm.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/idmap.h>
  16. #include <asm/cputype.h>
  17. #include <asm/suspend.h>
  18. /*
  19. * The public API for this code is documented in arch/arm/include/asm/mcpm.h.
  20. * For a comprehensive description of the main algorithm used here, please
  21. * see Documentation/arm/cluster-pm-race-avoidance.rst.
  22. */
  23. struct sync_struct mcpm_sync;
  24. /*
  25. * __mcpm_cpu_going_down: Indicates that the cpu is being torn down.
  26. * This must be called at the point of committing to teardown of a CPU.
  27. * The CPU cache (SCTRL.C bit) is expected to still be active.
  28. */
  29. static void __mcpm_cpu_going_down(unsigned int cpu, unsigned int cluster)
  30. {
  31. mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_GOING_DOWN;
  32. sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
  33. }
  34. /*
  35. * __mcpm_cpu_down: Indicates that cpu teardown is complete and that the
  36. * cluster can be torn down without disrupting this CPU.
  37. * To avoid deadlocks, this must be called before a CPU is powered down.
  38. * The CPU cache (SCTRL.C bit) is expected to be off.
  39. * However L2 cache might or might not be active.
  40. */
  41. static void __mcpm_cpu_down(unsigned int cpu, unsigned int cluster)
  42. {
  43. dmb();
  44. mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_DOWN;
  45. sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
  46. sev();
  47. }
  48. /*
  49. * __mcpm_outbound_leave_critical: Leave the cluster teardown critical section.
  50. * @state: the final state of the cluster:
  51. * CLUSTER_UP: no destructive teardown was done and the cluster has been
  52. * restored to the previous state (CPU cache still active); or
  53. * CLUSTER_DOWN: the cluster has been torn-down, ready for power-off
  54. * (CPU cache disabled, L2 cache either enabled or disabled).
  55. */
  56. static void __mcpm_outbound_leave_critical(unsigned int cluster, int state)
  57. {
  58. dmb();
  59. mcpm_sync.clusters[cluster].cluster = state;
  60. sync_cache_w(&mcpm_sync.clusters[cluster].cluster);
  61. sev();
  62. }
  63. /*
  64. * __mcpm_outbound_enter_critical: Enter the cluster teardown critical section.
  65. * This function should be called by the last man, after local CPU teardown
  66. * is complete. CPU cache expected to be active.
  67. *
  68. * Returns:
  69. * false: the critical section was not entered because an inbound CPU was
  70. * observed, or the cluster is already being set up;
  71. * true: the critical section was entered: it is now safe to tear down the
  72. * cluster.
  73. */
  74. static bool __mcpm_outbound_enter_critical(unsigned int cpu, unsigned int cluster)
  75. {
  76. unsigned int i;
  77. struct mcpm_sync_struct *c = &mcpm_sync.clusters[cluster];
  78. /* Warn inbound CPUs that the cluster is being torn down: */
  79. c->cluster = CLUSTER_GOING_DOWN;
  80. sync_cache_w(&c->cluster);
  81. /* Back out if the inbound cluster is already in the critical region: */
  82. sync_cache_r(&c->inbound);
  83. if (c->inbound == INBOUND_COMING_UP)
  84. goto abort;
  85. /*
  86. * Wait for all CPUs to get out of the GOING_DOWN state, so that local
  87. * teardown is complete on each CPU before tearing down the cluster.
  88. *
  89. * If any CPU has been woken up again from the DOWN state, then we
  90. * shouldn't be taking the cluster down at all: abort in that case.
  91. */
  92. sync_cache_r(&c->cpus);
  93. for (i = 0; i < MAX_CPUS_PER_CLUSTER; i++) {
  94. int cpustate;
  95. if (i == cpu)
  96. continue;
  97. while (1) {
  98. cpustate = c->cpus[i].cpu;
  99. if (cpustate != CPU_GOING_DOWN)
  100. break;
  101. wfe();
  102. sync_cache_r(&c->cpus[i].cpu);
  103. }
  104. switch (cpustate) {
  105. case CPU_DOWN:
  106. continue;
  107. default:
  108. goto abort;
  109. }
  110. }
  111. return true;
  112. abort:
  113. __mcpm_outbound_leave_critical(cluster, CLUSTER_UP);
  114. return false;
  115. }
  116. static int __mcpm_cluster_state(unsigned int cluster)
  117. {
  118. sync_cache_r(&mcpm_sync.clusters[cluster].cluster);
  119. return mcpm_sync.clusters[cluster].cluster;
  120. }
  121. extern unsigned long mcpm_entry_vectors[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER];
  122. void mcpm_set_entry_vector(unsigned cpu, unsigned cluster, void *ptr)
  123. {
  124. unsigned long val = ptr ? __pa_symbol(ptr) : 0;
  125. mcpm_entry_vectors[cluster][cpu] = val;
  126. sync_cache_w(&mcpm_entry_vectors[cluster][cpu]);
  127. }
  128. extern unsigned long mcpm_entry_early_pokes[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER][2];
  129. void mcpm_set_early_poke(unsigned cpu, unsigned cluster,
  130. unsigned long poke_phys_addr, unsigned long poke_val)
  131. {
  132. unsigned long *poke = &mcpm_entry_early_pokes[cluster][cpu][0];
  133. poke[0] = poke_phys_addr;
  134. poke[1] = poke_val;
  135. __sync_cache_range_w(poke, 2 * sizeof(*poke));
  136. }
  137. static const struct mcpm_platform_ops *platform_ops;
  138. int __init mcpm_platform_register(const struct mcpm_platform_ops *ops)
  139. {
  140. if (platform_ops)
  141. return -EBUSY;
  142. platform_ops = ops;
  143. return 0;
  144. }
  145. bool mcpm_is_available(void)
  146. {
  147. return (platform_ops) ? true : false;
  148. }
  149. EXPORT_SYMBOL_GPL(mcpm_is_available);
  150. /*
  151. * We can't use regular spinlocks. In the switcher case, it is possible
  152. * for an outbound CPU to call power_down() after its inbound counterpart
  153. * is already live using the same logical CPU number which trips lockdep
  154. * debugging.
  155. */
  156. static arch_spinlock_t mcpm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  157. static int mcpm_cpu_use_count[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER];
  158. static inline bool mcpm_cluster_unused(unsigned int cluster)
  159. {
  160. int i, cnt;
  161. for (i = 0, cnt = 0; i < MAX_CPUS_PER_CLUSTER; i++)
  162. cnt |= mcpm_cpu_use_count[cluster][i];
  163. return !cnt;
  164. }
  165. int mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster)
  166. {
  167. bool cpu_is_down, cluster_is_down;
  168. int ret = 0;
  169. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  170. if (!platform_ops)
  171. return -EUNATCH; /* try not to shadow power_up errors */
  172. might_sleep();
  173. /*
  174. * Since this is called with IRQs enabled, and no arch_spin_lock_irq
  175. * variant exists, we need to disable IRQs manually here.
  176. */
  177. local_irq_disable();
  178. arch_spin_lock(&mcpm_lock);
  179. cpu_is_down = !mcpm_cpu_use_count[cluster][cpu];
  180. cluster_is_down = mcpm_cluster_unused(cluster);
  181. mcpm_cpu_use_count[cluster][cpu]++;
  182. /*
  183. * The only possible values are:
  184. * 0 = CPU down
  185. * 1 = CPU (still) up
  186. * 2 = CPU requested to be up before it had a chance
  187. * to actually make itself down.
  188. * Any other value is a bug.
  189. */
  190. BUG_ON(mcpm_cpu_use_count[cluster][cpu] != 1 &&
  191. mcpm_cpu_use_count[cluster][cpu] != 2);
  192. if (cluster_is_down)
  193. ret = platform_ops->cluster_powerup(cluster);
  194. if (cpu_is_down && !ret)
  195. ret = platform_ops->cpu_powerup(cpu, cluster);
  196. arch_spin_unlock(&mcpm_lock);
  197. local_irq_enable();
  198. return ret;
  199. }
  200. typedef typeof(cpu_reset) phys_reset_t;
  201. void mcpm_cpu_power_down(void)
  202. {
  203. unsigned int mpidr, cpu, cluster;
  204. bool cpu_going_down, last_man;
  205. phys_reset_t phys_reset;
  206. mpidr = read_cpuid_mpidr();
  207. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  208. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  209. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  210. if (WARN_ON_ONCE(!platform_ops))
  211. return;
  212. BUG_ON(!irqs_disabled());
  213. setup_mm_for_reboot();
  214. __mcpm_cpu_going_down(cpu, cluster);
  215. arch_spin_lock(&mcpm_lock);
  216. BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
  217. mcpm_cpu_use_count[cluster][cpu]--;
  218. BUG_ON(mcpm_cpu_use_count[cluster][cpu] != 0 &&
  219. mcpm_cpu_use_count[cluster][cpu] != 1);
  220. cpu_going_down = !mcpm_cpu_use_count[cluster][cpu];
  221. last_man = mcpm_cluster_unused(cluster);
  222. if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
  223. platform_ops->cpu_powerdown_prepare(cpu, cluster);
  224. platform_ops->cluster_powerdown_prepare(cluster);
  225. arch_spin_unlock(&mcpm_lock);
  226. platform_ops->cluster_cache_disable();
  227. __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
  228. } else {
  229. if (cpu_going_down)
  230. platform_ops->cpu_powerdown_prepare(cpu, cluster);
  231. arch_spin_unlock(&mcpm_lock);
  232. /*
  233. * If cpu_going_down is false here, that means a power_up
  234. * request raced ahead of us. Even if we do not want to
  235. * shut this CPU down, the caller still expects execution
  236. * to return through the system resume entry path, like
  237. * when the WFI is aborted due to a new IRQ or the like..
  238. * So let's continue with cache cleaning in all cases.
  239. */
  240. platform_ops->cpu_cache_disable();
  241. }
  242. __mcpm_cpu_down(cpu, cluster);
  243. /* Now we are prepared for power-down, do it: */
  244. if (cpu_going_down)
  245. wfi();
  246. /*
  247. * It is possible for a power_up request to happen concurrently
  248. * with a power_down request for the same CPU. In this case the
  249. * CPU might not be able to actually enter a powered down state
  250. * with the WFI instruction if the power_up request has removed
  251. * the required reset condition. We must perform a re-entry in
  252. * the kernel as if the power_up method just had deasserted reset
  253. * on the CPU.
  254. */
  255. phys_reset = (phys_reset_t)(unsigned long)__pa_symbol(cpu_reset);
  256. phys_reset(__pa_symbol(mcpm_entry_point), false);
  257. /* should never get here */
  258. BUG();
  259. }
  260. int mcpm_wait_for_cpu_powerdown(unsigned int cpu, unsigned int cluster)
  261. {
  262. int ret;
  263. if (WARN_ON_ONCE(!platform_ops || !platform_ops->wait_for_powerdown))
  264. return -EUNATCH;
  265. ret = platform_ops->wait_for_powerdown(cpu, cluster);
  266. if (ret)
  267. pr_warn("%s: cpu %u, cluster %u failed to power down (%d)\n",
  268. __func__, cpu, cluster, ret);
  269. return ret;
  270. }
  271. void mcpm_cpu_suspend(void)
  272. {
  273. if (WARN_ON_ONCE(!platform_ops))
  274. return;
  275. /* Some platforms might have to enable special resume modes, etc. */
  276. if (platform_ops->cpu_suspend_prepare) {
  277. unsigned int mpidr = read_cpuid_mpidr();
  278. unsigned int cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  279. unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  280. arch_spin_lock(&mcpm_lock);
  281. platform_ops->cpu_suspend_prepare(cpu, cluster);
  282. arch_spin_unlock(&mcpm_lock);
  283. }
  284. mcpm_cpu_power_down();
  285. }
  286. int mcpm_cpu_powered_up(void)
  287. {
  288. unsigned int mpidr, cpu, cluster;
  289. bool cpu_was_down, first_man;
  290. unsigned long flags;
  291. if (!platform_ops)
  292. return -EUNATCH;
  293. mpidr = read_cpuid_mpidr();
  294. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  295. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  296. local_irq_save(flags);
  297. arch_spin_lock(&mcpm_lock);
  298. cpu_was_down = !mcpm_cpu_use_count[cluster][cpu];
  299. first_man = mcpm_cluster_unused(cluster);
  300. if (first_man && platform_ops->cluster_is_up)
  301. platform_ops->cluster_is_up(cluster);
  302. if (cpu_was_down)
  303. mcpm_cpu_use_count[cluster][cpu] = 1;
  304. if (platform_ops->cpu_is_up)
  305. platform_ops->cpu_is_up(cpu, cluster);
  306. arch_spin_unlock(&mcpm_lock);
  307. local_irq_restore(flags);
  308. return 0;
  309. }
  310. #ifdef CONFIG_ARM_CPU_SUSPEND
  311. static int __init nocache_trampoline(unsigned long _arg)
  312. {
  313. void (*cache_disable)(void) = (void *)_arg;
  314. unsigned int mpidr = read_cpuid_mpidr();
  315. unsigned int cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  316. unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  317. phys_reset_t phys_reset;
  318. mcpm_set_entry_vector(cpu, cluster, cpu_resume_no_hyp);
  319. setup_mm_for_reboot();
  320. __mcpm_cpu_going_down(cpu, cluster);
  321. BUG_ON(!__mcpm_outbound_enter_critical(cpu, cluster));
  322. cache_disable();
  323. __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
  324. __mcpm_cpu_down(cpu, cluster);
  325. phys_reset = (phys_reset_t)(unsigned long)__pa_symbol(cpu_reset);
  326. phys_reset(__pa_symbol(mcpm_entry_point), false);
  327. BUG();
  328. }
  329. int __init mcpm_loopback(void (*cache_disable)(void))
  330. {
  331. int ret;
  332. /*
  333. * We're going to soft-restart the current CPU through the
  334. * low-level MCPM code by leveraging the suspend/resume
  335. * infrastructure. Let's play it safe by using cpu_pm_enter()
  336. * in case the CPU init code path resets the VFP or similar.
  337. */
  338. local_irq_disable();
  339. local_fiq_disable();
  340. ret = cpu_pm_enter();
  341. if (!ret) {
  342. ret = cpu_suspend((unsigned long)cache_disable, nocache_trampoline);
  343. cpu_pm_exit();
  344. }
  345. local_fiq_enable();
  346. local_irq_enable();
  347. if (ret)
  348. pr_err("%s returned %d\n", __func__, ret);
  349. return ret;
  350. }
  351. #endif
  352. extern unsigned long mcpm_power_up_setup_phys;
  353. int __init mcpm_sync_init(
  354. void (*power_up_setup)(unsigned int affinity_level))
  355. {
  356. unsigned int i, j, mpidr, this_cluster;
  357. BUILD_BUG_ON(MCPM_SYNC_CLUSTER_SIZE * MAX_NR_CLUSTERS != sizeof mcpm_sync);
  358. BUG_ON((unsigned long)&mcpm_sync & (__CACHE_WRITEBACK_GRANULE - 1));
  359. /*
  360. * Set initial CPU and cluster states.
  361. * Only one cluster is assumed to be active at this point.
  362. */
  363. for (i = 0; i < MAX_NR_CLUSTERS; i++) {
  364. mcpm_sync.clusters[i].cluster = CLUSTER_DOWN;
  365. mcpm_sync.clusters[i].inbound = INBOUND_NOT_COMING_UP;
  366. for (j = 0; j < MAX_CPUS_PER_CLUSTER; j++)
  367. mcpm_sync.clusters[i].cpus[j].cpu = CPU_DOWN;
  368. }
  369. mpidr = read_cpuid_mpidr();
  370. this_cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  371. for_each_online_cpu(i) {
  372. mcpm_cpu_use_count[this_cluster][i] = 1;
  373. mcpm_sync.clusters[this_cluster].cpus[i].cpu = CPU_UP;
  374. }
  375. mcpm_sync.clusters[this_cluster].cluster = CLUSTER_UP;
  376. sync_cache_w(&mcpm_sync);
  377. if (power_up_setup) {
  378. mcpm_power_up_setup_phys = __pa_symbol(power_up_setup);
  379. sync_cache_w(&mcpm_power_up_setup_phys);
  380. }
  381. return 0;
  382. }