tc2_pm.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <asm/mcpm.h>
  21. #include <asm/proc-fns.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/cputype.h>
  24. #include <asm/cp15.h>
  25. #include <linux/arm-cci.h>
  26. #include "spc.h"
  27. /* SCC conf registers */
  28. #define A15_CONF 0x400
  29. #define A7_CONF 0x500
  30. #define SYS_INFO 0x700
  31. #define SPC_BASE 0xb00
  32. /*
  33. * We can't use regular spinlocks. In the switcher case, it is possible
  34. * for an outbound CPU to call power_down() after its inbound counterpart
  35. * is already live using the same logical CPU number which trips lockdep
  36. * debugging.
  37. */
  38. static arch_spinlock_t tc2_pm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  39. #define TC2_CLUSTERS 2
  40. #define TC2_MAX_CPUS_PER_CLUSTER 3
  41. static unsigned int tc2_nr_cpus[TC2_CLUSTERS];
  42. /* Keep per-cpu usage count to cope with unordered up/down requests */
  43. static int tc2_pm_use_count[TC2_MAX_CPUS_PER_CLUSTER][TC2_CLUSTERS];
  44. #define tc2_cluster_unused(cluster) \
  45. (!tc2_pm_use_count[0][cluster] && \
  46. !tc2_pm_use_count[1][cluster] && \
  47. !tc2_pm_use_count[2][cluster])
  48. static int tc2_pm_power_up(unsigned int cpu, unsigned int cluster)
  49. {
  50. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  51. if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster])
  52. return -EINVAL;
  53. /*
  54. * Since this is called with IRQs enabled, and no arch_spin_lock_irq
  55. * variant exists, we need to disable IRQs manually here.
  56. */
  57. local_irq_disable();
  58. arch_spin_lock(&tc2_pm_lock);
  59. if (tc2_cluster_unused(cluster))
  60. ve_spc_powerdown(cluster, false);
  61. tc2_pm_use_count[cpu][cluster]++;
  62. if (tc2_pm_use_count[cpu][cluster] == 1) {
  63. ve_spc_set_resume_addr(cluster, cpu,
  64. virt_to_phys(mcpm_entry_point));
  65. ve_spc_cpu_wakeup_irq(cluster, cpu, true);
  66. } else if (tc2_pm_use_count[cpu][cluster] != 2) {
  67. /*
  68. * The only possible values are:
  69. * 0 = CPU down
  70. * 1 = CPU (still) up
  71. * 2 = CPU requested to be up before it had a chance
  72. * to actually make itself down.
  73. * Any other value is a bug.
  74. */
  75. BUG();
  76. }
  77. arch_spin_unlock(&tc2_pm_lock);
  78. local_irq_enable();
  79. return 0;
  80. }
  81. static void tc2_pm_down(u64 residency)
  82. {
  83. unsigned int mpidr, cpu, cluster;
  84. bool last_man = false, skip_wfi = false;
  85. mpidr = read_cpuid_mpidr();
  86. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  87. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  88. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  89. BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
  90. __mcpm_cpu_going_down(cpu, cluster);
  91. arch_spin_lock(&tc2_pm_lock);
  92. BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
  93. tc2_pm_use_count[cpu][cluster]--;
  94. if (tc2_pm_use_count[cpu][cluster] == 0) {
  95. ve_spc_cpu_wakeup_irq(cluster, cpu, true);
  96. if (tc2_cluster_unused(cluster)) {
  97. ve_spc_powerdown(cluster, true);
  98. ve_spc_global_wakeup_irq(true);
  99. last_man = true;
  100. }
  101. } else if (tc2_pm_use_count[cpu][cluster] == 1) {
  102. /*
  103. * A power_up request went ahead of us.
  104. * Even if we do not want to shut this CPU down,
  105. * the caller expects a certain state as if the WFI
  106. * was aborted. So let's continue with cache cleaning.
  107. */
  108. skip_wfi = true;
  109. } else
  110. BUG();
  111. if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
  112. arch_spin_unlock(&tc2_pm_lock);
  113. if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A15) {
  114. /*
  115. * On the Cortex-A15 we need to disable
  116. * L2 prefetching before flushing the cache.
  117. */
  118. asm volatile(
  119. "mcr p15, 1, %0, c15, c0, 3 \n\t"
  120. "isb \n\t"
  121. "dsb "
  122. : : "r" (0x400) );
  123. }
  124. /*
  125. * We need to disable and flush the whole (L1 and L2) cache.
  126. * Let's do it in the safest possible way i.e. with
  127. * no memory access within the following sequence
  128. * including the stack.
  129. *
  130. * Note: fp is preserved to the stack explicitly prior doing
  131. * this since adding it to the clobber list is incompatible
  132. * with having CONFIG_FRAME_POINTER=y.
  133. */
  134. asm volatile(
  135. "str fp, [sp, #-4]! \n\t"
  136. "mrc p15, 0, r0, c1, c0, 0 @ get CR \n\t"
  137. "bic r0, r0, #"__stringify(CR_C)" \n\t"
  138. "mcr p15, 0, r0, c1, c0, 0 @ set CR \n\t"
  139. "isb \n\t"
  140. "bl v7_flush_dcache_all \n\t"
  141. "clrex \n\t"
  142. "mrc p15, 0, r0, c1, c0, 1 @ get AUXCR \n\t"
  143. "bic r0, r0, #(1 << 6) @ disable local coherency \n\t"
  144. "mcr p15, 0, r0, c1, c0, 1 @ set AUXCR \n\t"
  145. "isb \n\t"
  146. "dsb \n\t"
  147. "ldr fp, [sp], #4"
  148. : : : "r0","r1","r2","r3","r4","r5","r6","r7",
  149. "r9","r10","lr","memory");
  150. cci_disable_port_by_cpu(mpidr);
  151. __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
  152. } else {
  153. /*
  154. * If last man then undo any setup done previously.
  155. */
  156. if (last_man) {
  157. ve_spc_powerdown(cluster, false);
  158. ve_spc_global_wakeup_irq(false);
  159. }
  160. arch_spin_unlock(&tc2_pm_lock);
  161. /*
  162. * We need to disable and flush only the L1 cache.
  163. * Let's do it in the safest possible way as above.
  164. */
  165. asm volatile(
  166. "str fp, [sp, #-4]! \n\t"
  167. "mrc p15, 0, r0, c1, c0, 0 @ get CR \n\t"
  168. "bic r0, r0, #"__stringify(CR_C)" \n\t"
  169. "mcr p15, 0, r0, c1, c0, 0 @ set CR \n\t"
  170. "isb \n\t"
  171. "bl v7_flush_dcache_louis \n\t"
  172. "clrex \n\t"
  173. "mrc p15, 0, r0, c1, c0, 1 @ get AUXCR \n\t"
  174. "bic r0, r0, #(1 << 6) @ disable local coherency \n\t"
  175. "mcr p15, 0, r0, c1, c0, 1 @ set AUXCR \n\t"
  176. "isb \n\t"
  177. "dsb \n\t"
  178. "ldr fp, [sp], #4"
  179. : : : "r0","r1","r2","r3","r4","r5","r6","r7",
  180. "r9","r10","lr","memory");
  181. }
  182. __mcpm_cpu_down(cpu, cluster);
  183. /* Now we are prepared for power-down, do it: */
  184. if (!skip_wfi)
  185. wfi();
  186. /* Not dead at this point? Let our caller cope. */
  187. }
  188. static void tc2_pm_power_down(void)
  189. {
  190. tc2_pm_down(0);
  191. }
  192. static void tc2_pm_suspend(u64 residency)
  193. {
  194. unsigned int mpidr, cpu, cluster;
  195. mpidr = read_cpuid_mpidr();
  196. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  197. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  198. ve_spc_set_resume_addr(cluster, cpu, virt_to_phys(mcpm_entry_point));
  199. tc2_pm_down(residency);
  200. }
  201. static void tc2_pm_powered_up(void)
  202. {
  203. unsigned int mpidr, cpu, cluster;
  204. unsigned long flags;
  205. mpidr = read_cpuid_mpidr();
  206. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  207. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  208. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  209. BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
  210. local_irq_save(flags);
  211. arch_spin_lock(&tc2_pm_lock);
  212. if (tc2_cluster_unused(cluster)) {
  213. ve_spc_powerdown(cluster, false);
  214. ve_spc_global_wakeup_irq(false);
  215. }
  216. if (!tc2_pm_use_count[cpu][cluster])
  217. tc2_pm_use_count[cpu][cluster] = 1;
  218. ve_spc_cpu_wakeup_irq(cluster, cpu, false);
  219. ve_spc_set_resume_addr(cluster, cpu, 0);
  220. arch_spin_unlock(&tc2_pm_lock);
  221. local_irq_restore(flags);
  222. }
  223. static const struct mcpm_platform_ops tc2_pm_power_ops = {
  224. .power_up = tc2_pm_power_up,
  225. .power_down = tc2_pm_power_down,
  226. .suspend = tc2_pm_suspend,
  227. .powered_up = tc2_pm_powered_up,
  228. };
  229. static bool __init tc2_pm_usage_count_init(void)
  230. {
  231. unsigned int mpidr, cpu, cluster;
  232. mpidr = read_cpuid_mpidr();
  233. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  234. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  235. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  236. if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster]) {
  237. pr_err("%s: boot CPU is out of bound!\n", __func__);
  238. return false;
  239. }
  240. tc2_pm_use_count[cpu][cluster] = 1;
  241. return true;
  242. }
  243. /*
  244. * Enable cluster-level coherency, in preparation for turning on the MMU.
  245. */
  246. static void __naked tc2_pm_power_up_setup(unsigned int affinity_level)
  247. {
  248. asm volatile (" \n"
  249. " cmp r0, #1 \n"
  250. " bxne lr \n"
  251. " b cci_enable_port_for_self ");
  252. }
  253. static int __init tc2_pm_init(void)
  254. {
  255. int ret;
  256. void __iomem *scc;
  257. u32 a15_cluster_id, a7_cluster_id, sys_info;
  258. struct device_node *np;
  259. /*
  260. * The power management-related features are hidden behind
  261. * SCC registers. We need to extract runtime information like
  262. * cluster ids and number of CPUs really available in clusters.
  263. */
  264. np = of_find_compatible_node(NULL, NULL,
  265. "arm,vexpress-scc,v2p-ca15_a7");
  266. scc = of_iomap(np, 0);
  267. if (!scc)
  268. return -ENODEV;
  269. a15_cluster_id = readl_relaxed(scc + A15_CONF) & 0xf;
  270. a7_cluster_id = readl_relaxed(scc + A7_CONF) & 0xf;
  271. if (a15_cluster_id >= TC2_CLUSTERS || a7_cluster_id >= TC2_CLUSTERS)
  272. return -EINVAL;
  273. sys_info = readl_relaxed(scc + SYS_INFO);
  274. tc2_nr_cpus[a15_cluster_id] = (sys_info >> 16) & 0xf;
  275. tc2_nr_cpus[a7_cluster_id] = (sys_info >> 20) & 0xf;
  276. /*
  277. * A subset of the SCC registers is also used to communicate
  278. * with the SPC (power controller). We need to be able to
  279. * drive it very early in the boot process to power up
  280. * processors, so we initialize the SPC driver here.
  281. */
  282. ret = ve_spc_init(scc + SPC_BASE, a15_cluster_id);
  283. if (ret)
  284. return ret;
  285. if (!cci_probed())
  286. return -ENODEV;
  287. if (!tc2_pm_usage_count_init())
  288. return -EINVAL;
  289. ret = mcpm_platform_register(&tc2_pm_power_ops);
  290. if (!ret) {
  291. mcpm_sync_init(tc2_pm_power_up_setup);
  292. pr_info("TC2 power management initialized\n");
  293. }
  294. return ret;
  295. }
  296. early_initcall(tc2_pm_init);