mcpm_entry.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 <asm/mcpm.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/idmap.h>
  17. #include <asm/cputype.h>
  18. extern unsigned long mcpm_entry_vectors[MAX_NR_CLUSTERS][MAX_CPUS_PER_CLUSTER];
  19. void mcpm_set_entry_vector(unsigned cpu, unsigned cluster, void *ptr)
  20. {
  21. unsigned long val = ptr ? virt_to_phys(ptr) : 0;
  22. mcpm_entry_vectors[cluster][cpu] = val;
  23. sync_cache_w(&mcpm_entry_vectors[cluster][cpu]);
  24. }
  25. static const struct mcpm_platform_ops *platform_ops;
  26. int __init mcpm_platform_register(const struct mcpm_platform_ops *ops)
  27. {
  28. if (platform_ops)
  29. return -EBUSY;
  30. platform_ops = ops;
  31. return 0;
  32. }
  33. int mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster)
  34. {
  35. if (!platform_ops)
  36. return -EUNATCH; /* try not to shadow power_up errors */
  37. might_sleep();
  38. return platform_ops->power_up(cpu, cluster);
  39. }
  40. typedef void (*phys_reset_t)(unsigned long);
  41. void mcpm_cpu_power_down(void)
  42. {
  43. phys_reset_t phys_reset;
  44. BUG_ON(!platform_ops);
  45. BUG_ON(!irqs_disabled());
  46. /*
  47. * Do this before calling into the power_down method,
  48. * as it might not always be safe to do afterwards.
  49. */
  50. setup_mm_for_reboot();
  51. platform_ops->power_down();
  52. /*
  53. * It is possible for a power_up request to happen concurrently
  54. * with a power_down request for the same CPU. In this case the
  55. * power_down method might not be able to actually enter a
  56. * powered down state with the WFI instruction if the power_up
  57. * method has removed the required reset condition. The
  58. * power_down method is then allowed to return. We must perform
  59. * a re-entry in the kernel as if the power_up method just had
  60. * deasserted reset on the CPU.
  61. *
  62. * To simplify race issues, the platform specific implementation
  63. * must accommodate for the possibility of unordered calls to
  64. * power_down and power_up with a usage count. Therefore, if a
  65. * call to power_up is issued for a CPU that is not down, then
  66. * the next call to power_down must not attempt a full shutdown
  67. * but only do the minimum (normally disabling L1 cache and CPU
  68. * coherency) and return just as if a concurrent power_up request
  69. * had happened as described above.
  70. */
  71. phys_reset = (phys_reset_t)(unsigned long)virt_to_phys(cpu_reset);
  72. phys_reset(virt_to_phys(mcpm_entry_point));
  73. /* should never get here */
  74. BUG();
  75. }
  76. void mcpm_cpu_suspend(u64 expected_residency)
  77. {
  78. phys_reset_t phys_reset;
  79. BUG_ON(!platform_ops);
  80. BUG_ON(!irqs_disabled());
  81. /* Very similar to mcpm_cpu_power_down() */
  82. setup_mm_for_reboot();
  83. platform_ops->suspend(expected_residency);
  84. phys_reset = (phys_reset_t)(unsigned long)virt_to_phys(cpu_reset);
  85. phys_reset(virt_to_phys(mcpm_entry_point));
  86. BUG();
  87. }
  88. int mcpm_cpu_powered_up(void)
  89. {
  90. if (!platform_ops)
  91. return -EUNATCH;
  92. if (platform_ops->powered_up)
  93. platform_ops->powered_up();
  94. return 0;
  95. }
  96. struct sync_struct mcpm_sync;
  97. /*
  98. * __mcpm_cpu_going_down: Indicates that the cpu is being torn down.
  99. * This must be called at the point of committing to teardown of a CPU.
  100. * The CPU cache (SCTRL.C bit) is expected to still be active.
  101. */
  102. void __mcpm_cpu_going_down(unsigned int cpu, unsigned int cluster)
  103. {
  104. mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_GOING_DOWN;
  105. sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
  106. }
  107. /*
  108. * __mcpm_cpu_down: Indicates that cpu teardown is complete and that the
  109. * cluster can be torn down without disrupting this CPU.
  110. * To avoid deadlocks, this must be called before a CPU is powered down.
  111. * The CPU cache (SCTRL.C bit) is expected to be off.
  112. * However L2 cache might or might not be active.
  113. */
  114. void __mcpm_cpu_down(unsigned int cpu, unsigned int cluster)
  115. {
  116. dmb();
  117. mcpm_sync.clusters[cluster].cpus[cpu].cpu = CPU_DOWN;
  118. sync_cache_w(&mcpm_sync.clusters[cluster].cpus[cpu].cpu);
  119. dsb_sev();
  120. }
  121. /*
  122. * __mcpm_outbound_leave_critical: Leave the cluster teardown critical section.
  123. * @state: the final state of the cluster:
  124. * CLUSTER_UP: no destructive teardown was done and the cluster has been
  125. * restored to the previous state (CPU cache still active); or
  126. * CLUSTER_DOWN: the cluster has been torn-down, ready for power-off
  127. * (CPU cache disabled, L2 cache either enabled or disabled).
  128. */
  129. void __mcpm_outbound_leave_critical(unsigned int cluster, int state)
  130. {
  131. dmb();
  132. mcpm_sync.clusters[cluster].cluster = state;
  133. sync_cache_w(&mcpm_sync.clusters[cluster].cluster);
  134. dsb_sev();
  135. }
  136. /*
  137. * __mcpm_outbound_enter_critical: Enter the cluster teardown critical section.
  138. * This function should be called by the last man, after local CPU teardown
  139. * is complete. CPU cache expected to be active.
  140. *
  141. * Returns:
  142. * false: the critical section was not entered because an inbound CPU was
  143. * observed, or the cluster is already being set up;
  144. * true: the critical section was entered: it is now safe to tear down the
  145. * cluster.
  146. */
  147. bool __mcpm_outbound_enter_critical(unsigned int cpu, unsigned int cluster)
  148. {
  149. unsigned int i;
  150. struct mcpm_sync_struct *c = &mcpm_sync.clusters[cluster];
  151. /* Warn inbound CPUs that the cluster is being torn down: */
  152. c->cluster = CLUSTER_GOING_DOWN;
  153. sync_cache_w(&c->cluster);
  154. /* Back out if the inbound cluster is already in the critical region: */
  155. sync_cache_r(&c->inbound);
  156. if (c->inbound == INBOUND_COMING_UP)
  157. goto abort;
  158. /*
  159. * Wait for all CPUs to get out of the GOING_DOWN state, so that local
  160. * teardown is complete on each CPU before tearing down the cluster.
  161. *
  162. * If any CPU has been woken up again from the DOWN state, then we
  163. * shouldn't be taking the cluster down at all: abort in that case.
  164. */
  165. sync_cache_r(&c->cpus);
  166. for (i = 0; i < MAX_CPUS_PER_CLUSTER; i++) {
  167. int cpustate;
  168. if (i == cpu)
  169. continue;
  170. while (1) {
  171. cpustate = c->cpus[i].cpu;
  172. if (cpustate != CPU_GOING_DOWN)
  173. break;
  174. wfe();
  175. sync_cache_r(&c->cpus[i].cpu);
  176. }
  177. switch (cpustate) {
  178. case CPU_DOWN:
  179. continue;
  180. default:
  181. goto abort;
  182. }
  183. }
  184. return true;
  185. abort:
  186. __mcpm_outbound_leave_critical(cluster, CLUSTER_UP);
  187. return false;
  188. }
  189. int __mcpm_cluster_state(unsigned int cluster)
  190. {
  191. sync_cache_r(&mcpm_sync.clusters[cluster].cluster);
  192. return mcpm_sync.clusters[cluster].cluster;
  193. }
  194. extern unsigned long mcpm_power_up_setup_phys;
  195. int __init mcpm_sync_init(
  196. void (*power_up_setup)(unsigned int affinity_level))
  197. {
  198. unsigned int i, j, mpidr, this_cluster;
  199. BUILD_BUG_ON(MCPM_SYNC_CLUSTER_SIZE * MAX_NR_CLUSTERS != sizeof mcpm_sync);
  200. BUG_ON((unsigned long)&mcpm_sync & (__CACHE_WRITEBACK_GRANULE - 1));
  201. /*
  202. * Set initial CPU and cluster states.
  203. * Only one cluster is assumed to be active at this point.
  204. */
  205. for (i = 0; i < MAX_NR_CLUSTERS; i++) {
  206. mcpm_sync.clusters[i].cluster = CLUSTER_DOWN;
  207. mcpm_sync.clusters[i].inbound = INBOUND_NOT_COMING_UP;
  208. for (j = 0; j < MAX_CPUS_PER_CLUSTER; j++)
  209. mcpm_sync.clusters[i].cpus[j].cpu = CPU_DOWN;
  210. }
  211. mpidr = read_cpuid_mpidr();
  212. this_cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  213. for_each_online_cpu(i)
  214. mcpm_sync.clusters[this_cluster].cpus[i].cpu = CPU_UP;
  215. mcpm_sync.clusters[this_cluster].cluster = CLUSTER_UP;
  216. sync_cache_w(&mcpm_sync);
  217. if (power_up_setup) {
  218. mcpm_power_up_setup_phys = virt_to_phys(power_up_setup);
  219. sync_cache_w(&mcpm_power_up_setup_phys);
  220. }
  221. return 0;
  222. }