tc2_pm.c 9.7 KB

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