tc2_pm.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * arch/arm/mach-vexpress/tc2_pm.c - TC2 power management support
  3. *
  4. * Created by: Nicolas Pitre, October 2012
  5. * Copyright: (C) 2012-2013 Linaro Limited
  6. *
  7. * Some portions of this file were originally written by Achin Gupta
  8. * Copyright: (C) 2012 ARM Limited
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/of_address.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/errno.h>
  20. #include <linux/irqchip/arm-gic.h>
  21. #include <asm/mcpm.h>
  22. #include <asm/proc-fns.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/cputype.h>
  25. #include <asm/cp15.h>
  26. #include <linux/arm-cci.h>
  27. #include "spc.h"
  28. /* SCC conf registers */
  29. #define A15_CONF 0x400
  30. #define A7_CONF 0x500
  31. #define SYS_INFO 0x700
  32. #define SPC_BASE 0xb00
  33. /*
  34. * We can't use regular spinlocks. In the switcher case, it is possible
  35. * for an outbound CPU to call power_down() after its inbound counterpart
  36. * is already live using the same logical CPU number which trips lockdep
  37. * debugging.
  38. */
  39. static arch_spinlock_t tc2_pm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  40. #define TC2_CLUSTERS 2
  41. #define TC2_MAX_CPUS_PER_CLUSTER 3
  42. static unsigned int tc2_nr_cpus[TC2_CLUSTERS];
  43. /* Keep per-cpu usage count to cope with unordered up/down requests */
  44. static int tc2_pm_use_count[TC2_MAX_CPUS_PER_CLUSTER][TC2_CLUSTERS];
  45. #define tc2_cluster_unused(cluster) \
  46. (!tc2_pm_use_count[0][cluster] && \
  47. !tc2_pm_use_count[1][cluster] && \
  48. !tc2_pm_use_count[2][cluster])
  49. static int tc2_pm_power_up(unsigned int cpu, unsigned int cluster)
  50. {
  51. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  52. if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster])
  53. return -EINVAL;
  54. /*
  55. * Since this is called with IRQs enabled, and no arch_spin_lock_irq
  56. * variant exists, we need to disable IRQs manually here.
  57. */
  58. local_irq_disable();
  59. arch_spin_lock(&tc2_pm_lock);
  60. if (tc2_cluster_unused(cluster))
  61. ve_spc_powerdown(cluster, false);
  62. tc2_pm_use_count[cpu][cluster]++;
  63. if (tc2_pm_use_count[cpu][cluster] == 1) {
  64. ve_spc_set_resume_addr(cluster, cpu,
  65. virt_to_phys(mcpm_entry_point));
  66. ve_spc_cpu_wakeup_irq(cluster, cpu, true);
  67. } else if (tc2_pm_use_count[cpu][cluster] != 2) {
  68. /*
  69. * The only possible values are:
  70. * 0 = CPU down
  71. * 1 = CPU (still) up
  72. * 2 = CPU requested to be up before it had a chance
  73. * to actually make itself down.
  74. * Any other value is a bug.
  75. */
  76. BUG();
  77. }
  78. arch_spin_unlock(&tc2_pm_lock);
  79. local_irq_enable();
  80. return 0;
  81. }
  82. static void tc2_pm_down(u64 residency)
  83. {
  84. unsigned int mpidr, cpu, cluster;
  85. bool last_man = false, skip_wfi = false;
  86. mpidr = read_cpuid_mpidr();
  87. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  88. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  89. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  90. BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
  91. __mcpm_cpu_going_down(cpu, cluster);
  92. arch_spin_lock(&tc2_pm_lock);
  93. BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
  94. tc2_pm_use_count[cpu][cluster]--;
  95. if (tc2_pm_use_count[cpu][cluster] == 0) {
  96. ve_spc_cpu_wakeup_irq(cluster, cpu, true);
  97. if (tc2_cluster_unused(cluster)) {
  98. ve_spc_powerdown(cluster, true);
  99. ve_spc_global_wakeup_irq(true);
  100. last_man = true;
  101. }
  102. } else if (tc2_pm_use_count[cpu][cluster] == 1) {
  103. /*
  104. * A power_up request went ahead of us.
  105. * Even if we do not want to shut this CPU down,
  106. * the caller expects a certain state as if the WFI
  107. * was aborted. So let's continue with cache cleaning.
  108. */
  109. skip_wfi = true;
  110. } else
  111. BUG();
  112. /*
  113. * If the CPU is committed to power down, make sure
  114. * the power controller will be in charge of waking it
  115. * up upon IRQ, ie IRQ lines are cut from GIC CPU IF
  116. * to the CPU by disabling the GIC CPU IF to prevent wfi
  117. * from completing execution behind power controller back
  118. */
  119. if (!skip_wfi)
  120. gic_cpu_if_down();
  121. if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
  122. arch_spin_unlock(&tc2_pm_lock);
  123. if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A15) {
  124. /*
  125. * On the Cortex-A15 we need to disable
  126. * L2 prefetching before flushing the cache.
  127. */
  128. asm volatile(
  129. "mcr p15, 1, %0, c15, c0, 3 \n\t"
  130. "isb \n\t"
  131. "dsb "
  132. : : "r" (0x400) );
  133. }
  134. v7_exit_coherency_flush(all);
  135. cci_disable_port_by_cpu(mpidr);
  136. __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
  137. } else {
  138. /*
  139. * If last man then undo any setup done previously.
  140. */
  141. if (last_man) {
  142. ve_spc_powerdown(cluster, false);
  143. ve_spc_global_wakeup_irq(false);
  144. }
  145. arch_spin_unlock(&tc2_pm_lock);
  146. v7_exit_coherency_flush(louis);
  147. }
  148. __mcpm_cpu_down(cpu, cluster);
  149. /* Now we are prepared for power-down, do it: */
  150. if (!skip_wfi)
  151. wfi();
  152. /* Not dead at this point? Let our caller cope. */
  153. }
  154. static void tc2_pm_power_down(void)
  155. {
  156. tc2_pm_down(0);
  157. }
  158. static void tc2_pm_suspend(u64 residency)
  159. {
  160. unsigned int mpidr, cpu, cluster;
  161. mpidr = read_cpuid_mpidr();
  162. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  163. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  164. ve_spc_set_resume_addr(cluster, cpu, virt_to_phys(mcpm_entry_point));
  165. tc2_pm_down(residency);
  166. }
  167. static void tc2_pm_powered_up(void)
  168. {
  169. unsigned int mpidr, cpu, cluster;
  170. unsigned long flags;
  171. mpidr = read_cpuid_mpidr();
  172. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  173. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  174. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  175. BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
  176. local_irq_save(flags);
  177. arch_spin_lock(&tc2_pm_lock);
  178. if (tc2_cluster_unused(cluster)) {
  179. ve_spc_powerdown(cluster, false);
  180. ve_spc_global_wakeup_irq(false);
  181. }
  182. if (!tc2_pm_use_count[cpu][cluster])
  183. tc2_pm_use_count[cpu][cluster] = 1;
  184. ve_spc_cpu_wakeup_irq(cluster, cpu, false);
  185. ve_spc_set_resume_addr(cluster, cpu, 0);
  186. arch_spin_unlock(&tc2_pm_lock);
  187. local_irq_restore(flags);
  188. }
  189. static const struct mcpm_platform_ops tc2_pm_power_ops = {
  190. .power_up = tc2_pm_power_up,
  191. .power_down = tc2_pm_power_down,
  192. .suspend = tc2_pm_suspend,
  193. .powered_up = tc2_pm_powered_up,
  194. };
  195. static bool __init tc2_pm_usage_count_init(void)
  196. {
  197. unsigned int mpidr, cpu, cluster;
  198. mpidr = read_cpuid_mpidr();
  199. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  200. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  201. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  202. if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster]) {
  203. pr_err("%s: boot CPU is out of bound!\n", __func__);
  204. return false;
  205. }
  206. tc2_pm_use_count[cpu][cluster] = 1;
  207. return true;
  208. }
  209. /*
  210. * Enable cluster-level coherency, in preparation for turning on the MMU.
  211. */
  212. static void __naked tc2_pm_power_up_setup(unsigned int affinity_level)
  213. {
  214. asm volatile (" \n"
  215. " cmp r0, #1 \n"
  216. " bxne lr \n"
  217. " b cci_enable_port_for_self ");
  218. }
  219. static int __init tc2_pm_init(void)
  220. {
  221. int ret;
  222. void __iomem *scc;
  223. u32 a15_cluster_id, a7_cluster_id, sys_info;
  224. struct device_node *np;
  225. /*
  226. * The power management-related features are hidden behind
  227. * SCC registers. We need to extract runtime information like
  228. * cluster ids and number of CPUs really available in clusters.
  229. */
  230. np = of_find_compatible_node(NULL, NULL,
  231. "arm,vexpress-scc,v2p-ca15_a7");
  232. scc = of_iomap(np, 0);
  233. if (!scc)
  234. return -ENODEV;
  235. a15_cluster_id = readl_relaxed(scc + A15_CONF) & 0xf;
  236. a7_cluster_id = readl_relaxed(scc + A7_CONF) & 0xf;
  237. if (a15_cluster_id >= TC2_CLUSTERS || a7_cluster_id >= TC2_CLUSTERS)
  238. return -EINVAL;
  239. sys_info = readl_relaxed(scc + SYS_INFO);
  240. tc2_nr_cpus[a15_cluster_id] = (sys_info >> 16) & 0xf;
  241. tc2_nr_cpus[a7_cluster_id] = (sys_info >> 20) & 0xf;
  242. /*
  243. * A subset of the SCC registers is also used to communicate
  244. * with the SPC (power controller). We need to be able to
  245. * drive it very early in the boot process to power up
  246. * processors, so we initialize the SPC driver here.
  247. */
  248. ret = ve_spc_init(scc + SPC_BASE, a15_cluster_id);
  249. if (ret)
  250. return ret;
  251. if (!cci_probed())
  252. return -ENODEV;
  253. if (!tc2_pm_usage_count_init())
  254. return -EINVAL;
  255. ret = mcpm_platform_register(&tc2_pm_power_ops);
  256. if (!ret) {
  257. mcpm_sync_init(tc2_pm_power_up_setup);
  258. pr_info("TC2 power management initialized\n");
  259. }
  260. return ret;
  261. }
  262. early_initcall(tc2_pm_init);