tc2_pm.c 9.7 KB

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