dcscb.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 = dcscb_allcpus_mask[cluster];
  48. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  49. if (cpu >= 4 || cluster >= 2)
  50. return -EINVAL;
  51. /*
  52. * Since this is called with IRQs enabled, and no arch_spin_lock_irq
  53. * variant exists, we need to disable IRQs manually here.
  54. */
  55. local_irq_disable();
  56. arch_spin_lock(&dcscb_lock);
  57. dcscb_use_count[cpu][cluster]++;
  58. if (dcscb_use_count[cpu][cluster] == 1) {
  59. rst_hold = readl_relaxed(dcscb_base + RST_HOLD0 + cluster * 4);
  60. if (rst_hold & (1 << 8)) {
  61. /* remove cluster reset and add individual CPU's reset */
  62. rst_hold &= ~(1 << 8);
  63. rst_hold |= all_mask;
  64. }
  65. rst_hold &= ~(cpumask | (cpumask << 4));
  66. writel_relaxed(rst_hold, dcscb_base + RST_HOLD0 + cluster * 4);
  67. } else if (dcscb_use_count[cpu][cluster] != 2) {
  68. /*
  69. * The only possible values are:
  70. * 0 = CPU down
  71. * 1 = CPU (still) up
  72. * 2 = CPU requested to be up before it had a chance
  73. * to actually make itself down.
  74. * Any other value is a bug.
  75. */
  76. BUG();
  77. }
  78. arch_spin_unlock(&dcscb_lock);
  79. local_irq_enable();
  80. return 0;
  81. }
  82. static void dcscb_power_down(void)
  83. {
  84. unsigned int mpidr, cpu, cluster, rst_hold, cpumask, all_mask;
  85. bool last_man = false, skip_wfi = false;
  86. mpidr = read_cpuid_mpidr();
  87. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  88. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  89. cpumask = (1 << cpu);
  90. all_mask = dcscb_allcpus_mask[cluster];
  91. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  92. BUG_ON(cpu >= 4 || cluster >= 2);
  93. __mcpm_cpu_going_down(cpu, cluster);
  94. arch_spin_lock(&dcscb_lock);
  95. BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
  96. dcscb_use_count[cpu][cluster]--;
  97. if (dcscb_use_count[cpu][cluster] == 0) {
  98. rst_hold = readl_relaxed(dcscb_base + RST_HOLD0 + cluster * 4);
  99. rst_hold |= cpumask;
  100. if (((rst_hold | (rst_hold >> 4)) & all_mask) == all_mask) {
  101. rst_hold |= (1 << 8);
  102. last_man = true;
  103. }
  104. writel_relaxed(rst_hold, dcscb_base + RST_HOLD0 + cluster * 4);
  105. } else if (dcscb_use_count[cpu][cluster] == 1) {
  106. /*
  107. * A power_up request went ahead of us.
  108. * Even if we do not want to shut this CPU down,
  109. * the caller expects a certain state as if the WFI
  110. * was aborted. So let's continue with cache cleaning.
  111. */
  112. skip_wfi = true;
  113. } else
  114. BUG();
  115. if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
  116. arch_spin_unlock(&dcscb_lock);
  117. /*
  118. * Flush all cache levels for this cluster.
  119. *
  120. * To do so we do:
  121. * - Clear the SCTLR.C bit to prevent further cache allocations
  122. * - Flush the whole cache
  123. * - Clear the ACTLR "SMP" bit to disable local coherency
  124. *
  125. * Let's do it in the safest possible way i.e. with
  126. * no memory access within the following sequence
  127. * including to the stack.
  128. *
  129. * Note: fp is preserved to the stack explicitly prior doing
  130. * this since adding it to the clobber list is incompatible
  131. * with having CONFIG_FRAME_POINTER=y.
  132. */
  133. asm volatile(
  134. "str fp, [sp, #-4]! \n\t"
  135. "mrc p15, 0, r0, c1, c0, 0 @ get CR \n\t"
  136. "bic r0, r0, #"__stringify(CR_C)" \n\t"
  137. "mcr p15, 0, r0, c1, c0, 0 @ set CR \n\t"
  138. "isb \n\t"
  139. "bl v7_flush_dcache_all \n\t"
  140. "clrex \n\t"
  141. "mrc p15, 0, r0, c1, c0, 1 @ get AUXCR \n\t"
  142. "bic r0, r0, #(1 << 6) @ disable local coherency \n\t"
  143. "mcr p15, 0, r0, c1, c0, 1 @ set AUXCR \n\t"
  144. "isb \n\t"
  145. "dsb \n\t"
  146. "ldr fp, [sp], #4"
  147. : : : "r0","r1","r2","r3","r4","r5","r6","r7",
  148. "r9","r10","lr","memory");
  149. /*
  150. * This is a harmless no-op. On platforms with a real
  151. * outer cache this might either be needed or not,
  152. * depending on where the outer cache sits.
  153. */
  154. outer_flush_all();
  155. /*
  156. * Disable cluster-level coherency by masking
  157. * incoming snoops and DVM messages:
  158. */
  159. cci_disable_port_by_cpu(mpidr);
  160. __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
  161. } else {
  162. arch_spin_unlock(&dcscb_lock);
  163. /*
  164. * Flush the local CPU cache.
  165. * Let's do it in the safest possible way as above.
  166. */
  167. asm volatile(
  168. "str fp, [sp, #-4]! \n\t"
  169. "mrc p15, 0, r0, c1, c0, 0 @ get CR \n\t"
  170. "bic r0, r0, #"__stringify(CR_C)" \n\t"
  171. "mcr p15, 0, r0, c1, c0, 0 @ set CR \n\t"
  172. "isb \n\t"
  173. "bl v7_flush_dcache_louis \n\t"
  174. "clrex \n\t"
  175. "mrc p15, 0, r0, c1, c0, 1 @ get AUXCR \n\t"
  176. "bic r0, r0, #(1 << 6) @ disable local coherency \n\t"
  177. "mcr p15, 0, r0, c1, c0, 1 @ set AUXCR \n\t"
  178. "isb \n\t"
  179. "dsb \n\t"
  180. "ldr fp, [sp], #4"
  181. : : : "r0","r1","r2","r3","r4","r5","r6","r7",
  182. "r9","r10","lr","memory");
  183. }
  184. __mcpm_cpu_down(cpu, cluster);
  185. /* Now we are prepared for power-down, do it: */
  186. dsb();
  187. if (!skip_wfi)
  188. wfi();
  189. /* Not dead at this point? Let our caller cope. */
  190. }
  191. static const struct mcpm_platform_ops dcscb_power_ops = {
  192. .power_up = dcscb_power_up,
  193. .power_down = dcscb_power_down,
  194. };
  195. static void __init dcscb_usage_count_init(void)
  196. {
  197. unsigned int mpidr, cpu, cluster;
  198. mpidr = read_cpuid_mpidr();
  199. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  200. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  201. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  202. BUG_ON(cpu >= 4 || cluster >= 2);
  203. dcscb_use_count[cpu][cluster] = 1;
  204. }
  205. extern void dcscb_power_up_setup(unsigned int affinity_level);
  206. static int __init dcscb_init(void)
  207. {
  208. struct device_node *node;
  209. unsigned int cfg;
  210. int ret;
  211. if (!cci_probed())
  212. return -ENODEV;
  213. node = of_find_compatible_node(NULL, NULL, "arm,rtsm,dcscb");
  214. if (!node)
  215. return -ENODEV;
  216. dcscb_base = of_iomap(node, 0);
  217. if (!dcscb_base)
  218. return -EADDRNOTAVAIL;
  219. cfg = readl_relaxed(dcscb_base + DCS_CFG_R);
  220. dcscb_allcpus_mask[0] = (1 << (((cfg >> 16) >> (0 << 2)) & 0xf)) - 1;
  221. dcscb_allcpus_mask[1] = (1 << (((cfg >> 16) >> (1 << 2)) & 0xf)) - 1;
  222. dcscb_usage_count_init();
  223. ret = mcpm_platform_register(&dcscb_power_ops);
  224. if (!ret)
  225. ret = mcpm_sync_init(dcscb_power_up_setup);
  226. if (ret) {
  227. iounmap(dcscb_base);
  228. return ret;
  229. }
  230. pr_info("VExpress DCSCB support installed\n");
  231. /*
  232. * Future entries into the kernel can now go
  233. * through the cluster entry vectors.
  234. */
  235. vexpress_flags_set(virt_to_phys(mcpm_entry_point));
  236. return 0;
  237. }
  238. early_initcall(dcscb_init);