dcscb.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * arch/arm/mach-vexpress/dcscb.c - Dual Cluster System Configuration Block
  3. *
  4. * Created by: Nicolas Pitre, May 2012
  5. * Copyright: (C) 2012-2013 Linaro Limited
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/io.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/errno.h>
  16. #include <linux/of_address.h>
  17. #include <linux/vexpress.h>
  18. #include <linux/arm-cci.h>
  19. #include <asm/mcpm.h>
  20. #include <asm/proc-fns.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/cputype.h>
  23. #include <asm/cp15.h>
  24. #define RST_HOLD0 0x0
  25. #define RST_HOLD1 0x4
  26. #define SYS_SWRESET 0x8
  27. #define RST_STAT0 0xc
  28. #define RST_STAT1 0x10
  29. #define EAG_CFG_R 0x20
  30. #define EAG_CFG_W 0x24
  31. #define KFC_CFG_R 0x28
  32. #define KFC_CFG_W 0x2c
  33. #define DCS_CFG_R 0x30
  34. /*
  35. * We can't use regular spinlocks. In the switcher case, it is possible
  36. * for an outbound CPU to call power_down() while its inbound counterpart
  37. * is already live using the same logical CPU number which trips lockdep
  38. * debugging.
  39. */
  40. static arch_spinlock_t dcscb_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  41. static void __iomem *dcscb_base;
  42. static int dcscb_use_count[4][2];
  43. static int dcscb_allcpus_mask[2];
  44. static int dcscb_power_up(unsigned int cpu, unsigned int cluster)
  45. {
  46. unsigned int rst_hold, cpumask = (1 << cpu);
  47. unsigned int all_mask;
  48. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  49. if (cpu >= 4 || cluster >= 2)
  50. return -EINVAL;
  51. all_mask = dcscb_allcpus_mask[cluster];
  52. /*
  53. * Since this is called with IRQs enabled, and no arch_spin_lock_irq
  54. * variant exists, we need to disable IRQs manually here.
  55. */
  56. local_irq_disable();
  57. arch_spin_lock(&dcscb_lock);
  58. dcscb_use_count[cpu][cluster]++;
  59. if (dcscb_use_count[cpu][cluster] == 1) {
  60. rst_hold = readl_relaxed(dcscb_base + RST_HOLD0 + cluster * 4);
  61. if (rst_hold & (1 << 8)) {
  62. /* remove cluster reset and add individual CPU's reset */
  63. rst_hold &= ~(1 << 8);
  64. rst_hold |= all_mask;
  65. }
  66. rst_hold &= ~(cpumask | (cpumask << 4));
  67. writel_relaxed(rst_hold, dcscb_base + RST_HOLD0 + cluster * 4);
  68. } else if (dcscb_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(&dcscb_lock);
  80. local_irq_enable();
  81. return 0;
  82. }
  83. static void dcscb_power_down(void)
  84. {
  85. unsigned int mpidr, cpu, cluster, rst_hold, cpumask, all_mask;
  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. cpumask = (1 << cpu);
  91. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  92. BUG_ON(cpu >= 4 || cluster >= 2);
  93. all_mask = dcscb_allcpus_mask[cluster];
  94. __mcpm_cpu_going_down(cpu, cluster);
  95. arch_spin_lock(&dcscb_lock);
  96. BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
  97. dcscb_use_count[cpu][cluster]--;
  98. if (dcscb_use_count[cpu][cluster] == 0) {
  99. rst_hold = readl_relaxed(dcscb_base + RST_HOLD0 + cluster * 4);
  100. rst_hold |= cpumask;
  101. if (((rst_hold | (rst_hold >> 4)) & all_mask) == all_mask) {
  102. rst_hold |= (1 << 8);
  103. last_man = true;
  104. }
  105. writel_relaxed(rst_hold, dcscb_base + RST_HOLD0 + cluster * 4);
  106. } else if (dcscb_use_count[cpu][cluster] == 1) {
  107. /*
  108. * A power_up request went ahead of us.
  109. * Even if we do not want to shut this CPU down,
  110. * the caller expects a certain state as if the WFI
  111. * was aborted. So let's continue with cache cleaning.
  112. */
  113. skip_wfi = true;
  114. } else
  115. BUG();
  116. if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
  117. arch_spin_unlock(&dcscb_lock);
  118. /* Flush all cache levels for this cluster. */
  119. v7_exit_coherency_flush(all);
  120. /*
  121. * A full outer cache flush could be needed at this point
  122. * on platforms with such a cache, depending on where the
  123. * outer cache sits. In some cases the notion of a "last
  124. * cluster standing" would need to be implemented if the
  125. * outer cache is shared across clusters. In any case, when
  126. * the outer cache needs flushing, there is no concurrent
  127. * access to the cache controller to worry about and no
  128. * special locking besides what is already provided by the
  129. * MCPM state machinery is needed.
  130. */
  131. /*
  132. * Disable cluster-level coherency by masking
  133. * incoming snoops and DVM messages:
  134. */
  135. cci_disable_port_by_cpu(mpidr);
  136. __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
  137. } else {
  138. arch_spin_unlock(&dcscb_lock);
  139. /* Disable and flush the local CPU cache. */
  140. v7_exit_coherency_flush(louis);
  141. }
  142. __mcpm_cpu_down(cpu, cluster);
  143. /* Now we are prepared for power-down, do it: */
  144. dsb();
  145. if (!skip_wfi)
  146. wfi();
  147. /* Not dead at this point? Let our caller cope. */
  148. }
  149. static const struct mcpm_platform_ops dcscb_power_ops = {
  150. .power_up = dcscb_power_up,
  151. .power_down = dcscb_power_down,
  152. };
  153. static void __init dcscb_usage_count_init(void)
  154. {
  155. unsigned int mpidr, cpu, cluster;
  156. mpidr = read_cpuid_mpidr();
  157. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  158. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  159. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  160. BUG_ON(cpu >= 4 || cluster >= 2);
  161. dcscb_use_count[cpu][cluster] = 1;
  162. }
  163. extern void dcscb_power_up_setup(unsigned int affinity_level);
  164. static int __init dcscb_init(void)
  165. {
  166. struct device_node *node;
  167. unsigned int cfg;
  168. int ret;
  169. if (!cci_probed())
  170. return -ENODEV;
  171. node = of_find_compatible_node(NULL, NULL, "arm,rtsm,dcscb");
  172. if (!node)
  173. return -ENODEV;
  174. dcscb_base = of_iomap(node, 0);
  175. if (!dcscb_base)
  176. return -EADDRNOTAVAIL;
  177. cfg = readl_relaxed(dcscb_base + DCS_CFG_R);
  178. dcscb_allcpus_mask[0] = (1 << (((cfg >> 16) >> (0 << 2)) & 0xf)) - 1;
  179. dcscb_allcpus_mask[1] = (1 << (((cfg >> 16) >> (1 << 2)) & 0xf)) - 1;
  180. dcscb_usage_count_init();
  181. ret = mcpm_platform_register(&dcscb_power_ops);
  182. if (!ret)
  183. ret = mcpm_sync_init(dcscb_power_up_setup);
  184. if (ret) {
  185. iounmap(dcscb_base);
  186. return ret;
  187. }
  188. pr_info("VExpress DCSCB support installed\n");
  189. /*
  190. * Future entries into the kernel can now go
  191. * through the cluster entry vectors.
  192. */
  193. vexpress_flags_set(virt_to_phys(mcpm_entry_point));
  194. return 0;
  195. }
  196. early_initcall(dcscb_init);