mcpm_entry.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. int mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster)
  49. {
  50. if (!platform_ops)
  51. return -EUNATCH; /* try not to shadow power_up errors */
  52. might_sleep();
  53. return platform_ops->power_up(cpu, cluster);
  54. }
  55. typedef void (*phys_reset_t)(unsigned long);
  56. void mcpm_cpu_power_down(void)
  57. {
  58. phys_reset_t phys_reset;
  59. if (WARN_ON_ONCE(!platform_ops || !platform_ops->power_down))
  60. return;
  61. BUG_ON(!irqs_disabled());
  62. /*
  63. * Do this before calling into the power_down method,
  64. * as it might not always be safe to do afterwards.
  65. */
  66. setup_mm_for_reboot();
  67. platform_ops->power_down();
  68. /*
  69. * It is possible for a power_up request to happen concurrently
  70. * with a power_down request for the same CPU. In this case the
  71. * power_down method might not be able to actually enter a
  72. * powered down state with the WFI instruction if the power_up
  73. * method has removed the required reset condition. The
  74. * power_down method is then allowed to return. We must perform
  75. * a re-entry in the kernel as if the power_up method just had
  76. * deasserted reset on the CPU.
  77. *
  78. * To simplify race issues, the platform specific implementation
  79. * must accommodate for the possibility of unordered calls to
  80. * power_down and power_up with a usage count. Therefore, if a
  81. * call to power_up is issued for a CPU that is not down, then
  82. * the next call to power_down must not attempt a full shutdown
  83. * but only do the minimum (normally disabling L1 cache and CPU
  84. * coherency) and return just as if a concurrent power_up request
  85. * had happened as described above.
  86. */
  87. phys_reset = (phys_reset_t)(unsigned long)virt_to_phys(cpu_reset);
  88. phys_reset(virt_to_phys(mcpm_entry_point));
  89. /* should never get here */
  90. BUG();
  91. }
  92. int mcpm_wait_for_cpu_powerdown(unsigned int cpu, unsigned int cluster)
  93. {
  94. int ret;
  95. if (WARN_ON_ONCE(!platform_ops || !platform_ops->wait_for_powerdown))
  96. return -EUNATCH;
  97. ret = platform_ops->wait_for_powerdown(cpu, cluster);
  98. if (ret)
  99. pr_warn("%s: cpu %u, cluster %u failed to power down (%d)\n",
  100. __func__, cpu, cluster, ret);
  101. return ret;
  102. }
  103. void mcpm_cpu_suspend(u64 expected_residency)
  104. {
  105. phys_reset_t phys_reset;
  106. if (WARN_ON_ONCE(!platform_ops || !platform_ops->suspend))
  107. return;
  108. BUG_ON(!irqs_disabled());
  109. /* Very similar to mcpm_cpu_power_down() */
  110. setup_mm_for_reboot();
  111. platform_ops->suspend(expected_residency);
  112. phys_reset = (phys_reset_t)(unsigned long)virt_to_phys(cpu_reset);
  113. phys_reset(virt_to_phys(mcpm_entry_point));
  114. BUG();
  115. }
  116. int mcpm_cpu_powered_up(void)
  117. {
  118. if (!platform_ops)
  119. return -EUNATCH;
  120. if (platform_ops->powered_up)
  121. platform_ops->powered_up();
  122. return 0;
  123. }
  124. #ifdef CONFIG_ARM_CPU_SUSPEND
  125. static int __init nocache_trampoline(unsigned long _arg)
  126. {
  127. void (*cache_disable)(void) = (void *)_arg;
  128. unsigned int mpidr = read_cpuid_mpidr();
  129. unsigned int cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  130. unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  131. phys_reset_t phys_reset;
  132. mcpm_set_entry_vector(cpu, cluster, cpu_resume);
  133. setup_mm_for_reboot();
  134. __mcpm_cpu_going_down(cpu, cluster);
  135. BUG_ON(!__mcpm_outbound_enter_critical(cpu, cluster));
  136. cache_disable();
  137. __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
  138. __mcpm_cpu_down(cpu, cluster);
  139. phys_reset = (phys_reset_t)(unsigned long)virt_to_phys(cpu_reset);
  140. phys_reset(virt_to_phys(mcpm_entry_point));
  141. BUG();
  142. }
  143. int __init mcpm_loopback(void (*cache_disable)(void))
  144. {
  145. int ret;
  146. /*
  147. * We're going to soft-restart the current CPU through the
  148. * low-level MCPM code by leveraging the suspend/resume
  149. * infrastructure. Let's play it safe by using cpu_pm_enter()
  150. * in case the CPU init code path resets the VFP or similar.
  151. */
  152. local_irq_disable();
  153. local_fiq_disable();
  154. ret = cpu_pm_enter();
  155. if (!ret) {
  156. ret = cpu_suspend((unsigned long)cache_disable, nocache_trampoline);
  157. cpu_pm_exit();
  158. }
  159. local_fiq_enable();
  160. local_irq_enable();
  161. if (ret)
  162. pr_err("%s returned %d\n", __func__, ret);
  163. return ret;
  164. }
  165. #endif
  166. struct sync_struct mcpm_sync;
  167. /*
  168. * __mcpm_cpu_going_down: Indicates that the cpu is being torn down.
  169. * This must be called at the point of committing to teardown of a CPU.
  170. * The CPU cache (SCTRL.C bit) is expected to still be active.
  171. */
  172. void __mcpm_cpu_going_down(unsigned int cpu, unsigned int cluster)
  173. {
  174. mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_GOING_DOWN;
  175. sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
  176. }
  177. /*
  178. * __mcpm_cpu_down: Indicates that cpu teardown is complete and that the
  179. * cluster can be torn down without disrupting this CPU.
  180. * To avoid deadlocks, this must be called before a CPU is powered down.
  181. * The CPU cache (SCTRL.C bit) is expected to be off.
  182. * However L2 cache might or might not be active.
  183. */
  184. void __mcpm_cpu_down(unsigned int cpu, unsigned int cluster)
  185. {
  186. dmb();
  187. mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_DOWN;
  188. sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
  189. sev();
  190. }
  191. /*
  192. * __mcpm_outbound_leave_critical: Leave the cluster teardown critical section.
  193. * @state: the final state of the cluster:
  194. * CLUSTER_UP: no destructive teardown was done and the cluster has been
  195. * restored to the previous state (CPU cache still active); or
  196. * CLUSTER_DOWN: the cluster has been torn-down, ready for power-off
  197. * (CPU cache disabled, L2 cache either enabled or disabled).
  198. */
  199. void __mcpm_outbound_leave_critical(unsigned int cluster, int state)
  200. {
  201. dmb();
  202. mcpm_sync.clusters[cluster].cluster = state;
  203. sync_cache_w(&mcpm_sync.clusters[cluster].cluster);
  204. sev();
  205. }
  206. /*
  207. * __mcpm_outbound_enter_critical: Enter the cluster teardown critical section.
  208. * This function should be called by the last man, after local CPU teardown
  209. * is complete. CPU cache expected to be active.
  210. *
  211. * Returns:
  212. * false: the critical section was not entered because an inbound CPU was
  213. * observed, or the cluster is already being set up;
  214. * true: the critical section was entered: it is now safe to tear down the
  215. * cluster.
  216. */
  217. bool __mcpm_outbound_enter_critical(unsigned int cpu, unsigned int cluster)
  218. {
  219. unsigned int i;
  220. struct mcpm_sync_struct *c = &mcpm_sync.clusters[cluster];
  221. /* Warn inbound CPUs that the cluster is being torn down: */
  222. c->cluster = CLUSTER_GOING_DOWN;
  223. sync_cache_w(&c->cluster);
  224. /* Back out if the inbound cluster is already in the critical region: */
  225. sync_cache_r(&c->inbound);
  226. if (c->inbound == INBOUND_COMING_UP)
  227. goto abort;
  228. /*
  229. * Wait for all CPUs to get out of the GOING_DOWN state, so that local
  230. * teardown is complete on each CPU before tearing down the cluster.
  231. *
  232. * If any CPU has been woken up again from the DOWN state, then we
  233. * shouldn't be taking the cluster down at all: abort in that case.
  234. */
  235. sync_cache_r(&c->cpus);
  236. for (i = 0; i < MAX_CPUS_PER_CLUSTER; i++) {
  237. int cpustate;
  238. if (i == cpu)
  239. continue;
  240. while (1) {
  241. cpustate = c->cpus[i].cpu;
  242. if (cpustate != CPU_GOING_DOWN)
  243. break;
  244. wfe();
  245. sync_cache_r(&c->cpus[i].cpu);
  246. }
  247. switch (cpustate) {
  248. case CPU_DOWN:
  249. continue;
  250. default:
  251. goto abort;
  252. }
  253. }
  254. return true;
  255. abort:
  256. __mcpm_outbound_leave_critical(cluster, CLUSTER_UP);
  257. return false;
  258. }
  259. int __mcpm_cluster_state(unsigned int cluster)
  260. {
  261. sync_cache_r(&mcpm_sync.clusters[cluster].cluster);
  262. return mcpm_sync.clusters[cluster].cluster;
  263. }
  264. extern unsigned long mcpm_power_up_setup_phys;
  265. int __init mcpm_sync_init(
  266. void (*power_up_setup)(unsigned int affinity_level))
  267. {
  268. unsigned int i, j, mpidr, this_cluster;
  269. BUILD_BUG_ON(MCPM_SYNC_CLUSTER_SIZE * MAX_NR_CLUSTERS != sizeof mcpm_sync);
  270. BUG_ON((unsigned long)&mcpm_sync & (__CACHE_WRITEBACK_GRANULE - 1));
  271. /*
  272. * Set initial CPU and cluster states.
  273. * Only one cluster is assumed to be active at this point.
  274. */
  275. for (i = 0; i < MAX_NR_CLUSTERS; i++) {
  276. mcpm_sync.clusters[i].cluster = CLUSTER_DOWN;
  277. mcpm_sync.clusters[i].inbound = INBOUND_NOT_COMING_UP;
  278. for (j = 0; j < MAX_CPUS_PER_CLUSTER; j++)
  279. mcpm_sync.clusters[i].cpus[j].cpu = CPU_DOWN;
  280. }
  281. mpidr = read_cpuid_mpidr();
  282. this_cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  283. for_each_online_cpu(i)
  284. mcpm_sync.clusters[this_cluster].cpus[i].cpu = CPU_UP;
  285. mcpm_sync.clusters[this_cluster].cluster = CLUSTER_UP;
  286. sync_cache_w(&mcpm_sync);
  287. if (power_up_setup) {
  288. mcpm_power_up_setup_phys = virt_to_phys(power_up_setup);
  289. sync_cache_w(&mcpm_power_up_setup_phys);
  290. }
  291. return 0;
  292. }