tc2_pm.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/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 RESET_CTRL 0x018
  31. #define RESET_A15_NCORERESET(cpu) (1 << (2 + (cpu)))
  32. #define RESET_A7_NCORERESET(cpu) (1 << (16 + (cpu)))
  33. #define A15_CONF 0x400
  34. #define A7_CONF 0x500
  35. #define SYS_INFO 0x700
  36. #define SPC_BASE 0xb00
  37. static void __iomem *scc;
  38. #define TC2_CLUSTERS 2
  39. #define TC2_MAX_CPUS_PER_CLUSTER 3
  40. static unsigned int tc2_nr_cpus[TC2_CLUSTERS];
  41. static int tc2_pm_cpu_powerup(unsigned int cpu, unsigned int cluster)
  42. {
  43. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  44. if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster])
  45. return -EINVAL;
  46. ve_spc_set_resume_addr(cluster, cpu,
  47. virt_to_phys(mcpm_entry_point));
  48. ve_spc_cpu_wakeup_irq(cluster, cpu, true);
  49. return 0;
  50. }
  51. static int tc2_pm_cluster_powerup(unsigned int cluster)
  52. {
  53. pr_debug("%s: cluster %u\n", __func__, cluster);
  54. if (cluster >= TC2_CLUSTERS)
  55. return -EINVAL;
  56. ve_spc_powerdown(cluster, false);
  57. return 0;
  58. }
  59. static void tc2_pm_cpu_powerdown_prepare(unsigned int cpu, unsigned int cluster)
  60. {
  61. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  62. BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
  63. ve_spc_cpu_wakeup_irq(cluster, cpu, true);
  64. /*
  65. * If the CPU is committed to power down, make sure
  66. * the power controller will be in charge of waking it
  67. * up upon IRQ, ie IRQ lines are cut from GIC CPU IF
  68. * to the CPU by disabling the GIC CPU IF to prevent wfi
  69. * from completing execution behind power controller back
  70. */
  71. gic_cpu_if_down(0);
  72. }
  73. static void tc2_pm_cluster_powerdown_prepare(unsigned int cluster)
  74. {
  75. pr_debug("%s: cluster %u\n", __func__, cluster);
  76. BUG_ON(cluster >= TC2_CLUSTERS);
  77. ve_spc_powerdown(cluster, true);
  78. ve_spc_global_wakeup_irq(true);
  79. }
  80. static void tc2_pm_cpu_cache_disable(void)
  81. {
  82. v7_exit_coherency_flush(louis);
  83. }
  84. static void tc2_pm_cluster_cache_disable(void)
  85. {
  86. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A15) {
  87. /*
  88. * On the Cortex-A15 we need to disable
  89. * L2 prefetching before flushing the cache.
  90. */
  91. asm volatile(
  92. "mcr p15, 1, %0, c15, c0, 3 \n\t"
  93. "isb \n\t"
  94. "dsb "
  95. : : "r" (0x400) );
  96. }
  97. v7_exit_coherency_flush(all);
  98. cci_disable_port_by_cpu(read_cpuid_mpidr());
  99. }
  100. static int tc2_core_in_reset(unsigned int cpu, unsigned int cluster)
  101. {
  102. u32 mask = cluster ?
  103. RESET_A7_NCORERESET(cpu)
  104. : RESET_A15_NCORERESET(cpu);
  105. return !(readl_relaxed(scc + RESET_CTRL) & mask);
  106. }
  107. #define POLL_MSEC 10
  108. #define TIMEOUT_MSEC 1000
  109. static int tc2_pm_wait_for_powerdown(unsigned int cpu, unsigned int cluster)
  110. {
  111. unsigned tries;
  112. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  113. BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
  114. for (tries = 0; tries < TIMEOUT_MSEC / POLL_MSEC; ++tries) {
  115. pr_debug("%s(cpu=%u, cluster=%u): RESET_CTRL = 0x%08X\n",
  116. __func__, cpu, cluster,
  117. readl_relaxed(scc + RESET_CTRL));
  118. /*
  119. * We need the CPU to reach WFI, but the power
  120. * controller may put the cluster in reset and
  121. * power it off as soon as that happens, before
  122. * we have a chance to see STANDBYWFI.
  123. *
  124. * So we need to check for both conditions:
  125. */
  126. if (tc2_core_in_reset(cpu, cluster) ||
  127. ve_spc_cpu_in_wfi(cpu, cluster))
  128. return 0; /* success: the CPU is halted */
  129. /* Otherwise, wait and retry: */
  130. msleep(POLL_MSEC);
  131. }
  132. return -ETIMEDOUT; /* timeout */
  133. }
  134. static void tc2_pm_cpu_suspend_prepare(unsigned int cpu, unsigned int cluster)
  135. {
  136. ve_spc_set_resume_addr(cluster, cpu, virt_to_phys(mcpm_entry_point));
  137. }
  138. static void tc2_pm_cpu_is_up(unsigned int cpu, unsigned int cluster)
  139. {
  140. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  141. BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
  142. ve_spc_cpu_wakeup_irq(cluster, cpu, false);
  143. ve_spc_set_resume_addr(cluster, cpu, 0);
  144. }
  145. static void tc2_pm_cluster_is_up(unsigned int cluster)
  146. {
  147. pr_debug("%s: cluster %u\n", __func__, cluster);
  148. BUG_ON(cluster >= TC2_CLUSTERS);
  149. ve_spc_powerdown(cluster, false);
  150. ve_spc_global_wakeup_irq(false);
  151. }
  152. static const struct mcpm_platform_ops tc2_pm_power_ops = {
  153. .cpu_powerup = tc2_pm_cpu_powerup,
  154. .cluster_powerup = tc2_pm_cluster_powerup,
  155. .cpu_suspend_prepare = tc2_pm_cpu_suspend_prepare,
  156. .cpu_powerdown_prepare = tc2_pm_cpu_powerdown_prepare,
  157. .cluster_powerdown_prepare = tc2_pm_cluster_powerdown_prepare,
  158. .cpu_cache_disable = tc2_pm_cpu_cache_disable,
  159. .cluster_cache_disable = tc2_pm_cluster_cache_disable,
  160. .wait_for_powerdown = tc2_pm_wait_for_powerdown,
  161. .cpu_is_up = tc2_pm_cpu_is_up,
  162. .cluster_is_up = tc2_pm_cluster_is_up,
  163. };
  164. /*
  165. * Enable cluster-level coherency, in preparation for turning on the MMU.
  166. */
  167. static void __naked tc2_pm_power_up_setup(unsigned int affinity_level)
  168. {
  169. asm volatile (" \n"
  170. " cmp r0, #1 \n"
  171. " bxne lr \n"
  172. " b cci_enable_port_for_self ");
  173. }
  174. static int __init tc2_pm_init(void)
  175. {
  176. unsigned int mpidr, cpu, cluster;
  177. int ret, irq;
  178. u32 a15_cluster_id, a7_cluster_id, sys_info;
  179. struct device_node *np;
  180. /*
  181. * The power management-related features are hidden behind
  182. * SCC registers. We need to extract runtime information like
  183. * cluster ids and number of CPUs really available in clusters.
  184. */
  185. np = of_find_compatible_node(NULL, NULL,
  186. "arm,vexpress-scc,v2p-ca15_a7");
  187. scc = of_iomap(np, 0);
  188. if (!scc)
  189. return -ENODEV;
  190. a15_cluster_id = readl_relaxed(scc + A15_CONF) & 0xf;
  191. a7_cluster_id = readl_relaxed(scc + A7_CONF) & 0xf;
  192. if (a15_cluster_id >= TC2_CLUSTERS || a7_cluster_id >= TC2_CLUSTERS)
  193. return -EINVAL;
  194. sys_info = readl_relaxed(scc + SYS_INFO);
  195. tc2_nr_cpus[a15_cluster_id] = (sys_info >> 16) & 0xf;
  196. tc2_nr_cpus[a7_cluster_id] = (sys_info >> 20) & 0xf;
  197. irq = irq_of_parse_and_map(np, 0);
  198. /*
  199. * A subset of the SCC registers is also used to communicate
  200. * with the SPC (power controller). We need to be able to
  201. * drive it very early in the boot process to power up
  202. * processors, so we initialize the SPC driver here.
  203. */
  204. ret = ve_spc_init(scc + SPC_BASE, a15_cluster_id, irq);
  205. if (ret)
  206. return ret;
  207. if (!cci_probed())
  208. return -ENODEV;
  209. mpidr = read_cpuid_mpidr();
  210. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  211. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  212. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  213. if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster]) {
  214. pr_err("%s: boot CPU is out of bound!\n", __func__);
  215. return -EINVAL;
  216. }
  217. ret = mcpm_platform_register(&tc2_pm_power_ops);
  218. if (!ret) {
  219. mcpm_sync_init(tc2_pm_power_up_setup);
  220. /* test if we can (re)enable the CCI on our own */
  221. BUG_ON(mcpm_loopback(tc2_pm_cluster_cache_disable) != 0);
  222. pr_info("TC2 power management initialized\n");
  223. }
  224. return ret;
  225. }
  226. early_initcall(tc2_pm_init);