mcpm_entry.c 13 KB

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