mcpm-exynos.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * arch/arm/mach-exynos/mcpm-exynos.c
  6. *
  7. * Based on arch/arm/mach-vexpress/dcscb.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/arm-cci.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/of_address.h>
  17. #include <asm/cputype.h>
  18. #include <asm/cp15.h>
  19. #include <asm/mcpm.h>
  20. #include "regs-pmu.h"
  21. #include "common.h"
  22. #define EXYNOS5420_CPUS_PER_CLUSTER 4
  23. #define EXYNOS5420_NR_CLUSTERS 2
  24. /*
  25. * The common v7_exit_coherency_flush API could not be used because of the
  26. * Erratum 799270 workaround. This macro is the same as the common one (in
  27. * arch/arm/include/asm/cacheflush.h) except for the erratum handling.
  28. */
  29. #define exynos_v7_exit_coherency_flush(level) \
  30. asm volatile( \
  31. "stmfd sp!, {fp, ip}\n\t"\
  32. "mrc p15, 0, r0, c1, c0, 0 @ get SCTLR\n\t" \
  33. "bic r0, r0, #"__stringify(CR_C)"\n\t" \
  34. "mcr p15, 0, r0, c1, c0, 0 @ set SCTLR\n\t" \
  35. "isb\n\t"\
  36. "bl v7_flush_dcache_"__stringify(level)"\n\t" \
  37. "clrex\n\t"\
  38. "mrc p15, 0, r0, c1, c0, 1 @ get ACTLR\n\t" \
  39. "bic r0, r0, #(1 << 6) @ disable local coherency\n\t" \
  40. /* Dummy Load of a device register to avoid Erratum 799270 */ \
  41. "ldr r4, [%0]\n\t" \
  42. "and r4, r4, #0\n\t" \
  43. "orr r0, r0, r4\n\t" \
  44. "mcr p15, 0, r0, c1, c0, 1 @ set ACTLR\n\t" \
  45. "isb\n\t" \
  46. "dsb\n\t" \
  47. "ldmfd sp!, {fp, ip}" \
  48. : \
  49. : "Ir" (S5P_INFORM0) \
  50. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", \
  51. "r9", "r10", "lr", "memory")
  52. /*
  53. * We can't use regular spinlocks. In the switcher case, it is possible
  54. * for an outbound CPU to call power_down() after its inbound counterpart
  55. * is already live using the same logical CPU number which trips lockdep
  56. * debugging.
  57. */
  58. static arch_spinlock_t exynos_mcpm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  59. static int
  60. cpu_use_count[EXYNOS5420_CPUS_PER_CLUSTER][EXYNOS5420_NR_CLUSTERS];
  61. #define exynos_cluster_usecnt(cluster) \
  62. (cpu_use_count[0][cluster] + \
  63. cpu_use_count[1][cluster] + \
  64. cpu_use_count[2][cluster] + \
  65. cpu_use_count[3][cluster])
  66. #define exynos_cluster_unused(cluster) !exynos_cluster_usecnt(cluster)
  67. static int exynos_cluster_power_control(unsigned int cluster, int enable)
  68. {
  69. unsigned int tries = 100;
  70. unsigned int val;
  71. if (enable) {
  72. exynos_cluster_power_up(cluster);
  73. val = S5P_CORE_LOCAL_PWR_EN;
  74. } else {
  75. exynos_cluster_power_down(cluster);
  76. val = 0;
  77. }
  78. /* Wait until cluster power control is applied */
  79. while (tries--) {
  80. if (exynos_cluster_power_state(cluster) == val)
  81. return 0;
  82. cpu_relax();
  83. }
  84. pr_debug("timed out waiting for cluster %u to power %s\n", cluster,
  85. enable ? "on" : "off");
  86. return -ETIMEDOUT;
  87. }
  88. static int exynos_power_up(unsigned int cpu, unsigned int cluster)
  89. {
  90. unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  91. int err = 0;
  92. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  93. if (cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  94. cluster >= EXYNOS5420_NR_CLUSTERS)
  95. return -EINVAL;
  96. /*
  97. * Since this is called with IRQs enabled, and no arch_spin_lock_irq
  98. * variant exists, we need to disable IRQs manually here.
  99. */
  100. local_irq_disable();
  101. arch_spin_lock(&exynos_mcpm_lock);
  102. cpu_use_count[cpu][cluster]++;
  103. if (cpu_use_count[cpu][cluster] == 1) {
  104. bool was_cluster_down =
  105. (exynos_cluster_usecnt(cluster) == 1);
  106. /*
  107. * Turn on the cluster (L2/COMMON) and then power on the
  108. * cores.
  109. */
  110. if (was_cluster_down)
  111. err = exynos_cluster_power_control(cluster, 1);
  112. if (!err)
  113. exynos_cpu_power_up(cpunr);
  114. else
  115. exynos_cluster_power_control(cluster, 0);
  116. } else if (cpu_use_count[cpu][cluster] != 2) {
  117. /*
  118. * The only possible values are:
  119. * 0 = CPU down
  120. * 1 = CPU (still) up
  121. * 2 = CPU requested to be up before it had a chance
  122. * to actually make itself down.
  123. * Any other value is a bug.
  124. */
  125. BUG();
  126. }
  127. arch_spin_unlock(&exynos_mcpm_lock);
  128. local_irq_enable();
  129. return err;
  130. }
  131. /*
  132. * NOTE: This function requires the stack data to be visible through power down
  133. * and can only be executed on processors like A15 and A7 that hit the cache
  134. * with the C bit clear in the SCTLR register.
  135. */
  136. static void exynos_power_down(void)
  137. {
  138. unsigned int mpidr, cpu, cluster;
  139. bool last_man = false, skip_wfi = false;
  140. unsigned int cpunr;
  141. mpidr = read_cpuid_mpidr();
  142. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  143. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  144. cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  145. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  146. BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  147. cluster >= EXYNOS5420_NR_CLUSTERS);
  148. __mcpm_cpu_going_down(cpu, cluster);
  149. arch_spin_lock(&exynos_mcpm_lock);
  150. BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
  151. cpu_use_count[cpu][cluster]--;
  152. if (cpu_use_count[cpu][cluster] == 0) {
  153. exynos_cpu_power_down(cpunr);
  154. if (exynos_cluster_unused(cluster))
  155. /* TODO: Turn off the cluster here to save power. */
  156. last_man = true;
  157. } else if (cpu_use_count[cpu][cluster] == 1) {
  158. /*
  159. * A power_up request went ahead of us.
  160. * Even if we do not want to shut this CPU down,
  161. * the caller expects a certain state as if the WFI
  162. * was aborted. So let's continue with cache cleaning.
  163. */
  164. skip_wfi = true;
  165. } else {
  166. BUG();
  167. }
  168. if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
  169. arch_spin_unlock(&exynos_mcpm_lock);
  170. if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A15) {
  171. /*
  172. * On the Cortex-A15 we need to disable
  173. * L2 prefetching before flushing the cache.
  174. */
  175. asm volatile(
  176. "mcr p15, 1, %0, c15, c0, 3\n\t"
  177. "isb\n\t"
  178. "dsb"
  179. : : "r" (0x400));
  180. }
  181. /* Flush all cache levels for this cluster. */
  182. exynos_v7_exit_coherency_flush(all);
  183. /*
  184. * Disable cluster-level coherency by masking
  185. * incoming snoops and DVM messages:
  186. */
  187. cci_disable_port_by_cpu(mpidr);
  188. __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
  189. } else {
  190. arch_spin_unlock(&exynos_mcpm_lock);
  191. /* Disable and flush the local CPU cache. */
  192. exynos_v7_exit_coherency_flush(louis);
  193. }
  194. __mcpm_cpu_down(cpu, cluster);
  195. /* Now we are prepared for power-down, do it: */
  196. if (!skip_wfi)
  197. wfi();
  198. /* Not dead at this point? Let our caller cope. */
  199. }
  200. static int exynos_wait_for_powerdown(unsigned int cpu, unsigned int cluster)
  201. {
  202. unsigned int tries = 100;
  203. unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  204. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  205. BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  206. cluster >= EXYNOS5420_NR_CLUSTERS);
  207. /* Wait for the core state to be OFF */
  208. while (tries--) {
  209. if (ACCESS_ONCE(cpu_use_count[cpu][cluster]) == 0) {
  210. if ((exynos_cpu_power_state(cpunr) == 0))
  211. return 0; /* success: the CPU is halted */
  212. }
  213. /* Otherwise, wait and retry: */
  214. msleep(1);
  215. }
  216. return -ETIMEDOUT; /* timeout */
  217. }
  218. static const struct mcpm_platform_ops exynos_power_ops = {
  219. .power_up = exynos_power_up,
  220. .power_down = exynos_power_down,
  221. .wait_for_powerdown = exynos_wait_for_powerdown,
  222. };
  223. static void __init exynos_mcpm_usage_count_init(void)
  224. {
  225. unsigned int mpidr, cpu, cluster;
  226. mpidr = read_cpuid_mpidr();
  227. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  228. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  229. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  230. BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  231. cluster >= EXYNOS5420_NR_CLUSTERS);
  232. cpu_use_count[cpu][cluster] = 1;
  233. }
  234. /*
  235. * Enable cluster-level coherency, in preparation for turning on the MMU.
  236. */
  237. static void __naked exynos_pm_power_up_setup(unsigned int affinity_level)
  238. {
  239. asm volatile ("\n"
  240. "cmp r0, #1\n"
  241. "bxne lr\n"
  242. "b cci_enable_port_for_self");
  243. }
  244. static const struct of_device_id exynos_dt_mcpm_match[] = {
  245. { .compatible = "samsung,exynos5420" },
  246. { .compatible = "samsung,exynos5800" },
  247. {},
  248. };
  249. static int __init exynos_mcpm_init(void)
  250. {
  251. struct device_node *node;
  252. void __iomem *ns_sram_base_addr;
  253. int ret;
  254. node = of_find_matching_node(NULL, exynos_dt_mcpm_match);
  255. if (!node)
  256. return -ENODEV;
  257. of_node_put(node);
  258. if (!cci_probed())
  259. return -ENODEV;
  260. node = of_find_compatible_node(NULL, NULL,
  261. "samsung,exynos4210-sysram-ns");
  262. if (!node)
  263. return -ENODEV;
  264. ns_sram_base_addr = of_iomap(node, 0);
  265. of_node_put(node);
  266. if (!ns_sram_base_addr) {
  267. pr_err("failed to map non-secure iRAM base address\n");
  268. return -ENOMEM;
  269. }
  270. /*
  271. * To increase the stability of KFC reset we need to program
  272. * the PMU SPARE3 register
  273. */
  274. __raw_writel(EXYNOS5420_SWRESET_KFC_SEL, S5P_PMU_SPARE3);
  275. exynos_mcpm_usage_count_init();
  276. ret = mcpm_platform_register(&exynos_power_ops);
  277. if (!ret)
  278. ret = mcpm_sync_init(exynos_pm_power_up_setup);
  279. if (ret) {
  280. iounmap(ns_sram_base_addr);
  281. return ret;
  282. }
  283. mcpm_smp_set_ops();
  284. pr_info("Exynos MCPM support installed\n");
  285. /*
  286. * U-Boot SPL is hardcoded to jump to the start of ns_sram_base_addr
  287. * as part of secondary_cpu_start(). Let's redirect it to the
  288. * mcpm_entry_point().
  289. */
  290. __raw_writel(0xe59f0000, ns_sram_base_addr); /* ldr r0, [pc, #0] */
  291. __raw_writel(0xe12fff10, ns_sram_base_addr + 4); /* bx r0 */
  292. __raw_writel(virt_to_phys(mcpm_entry_point), ns_sram_base_addr + 8);
  293. iounmap(ns_sram_base_addr);
  294. return ret;
  295. }
  296. early_initcall(exynos_mcpm_init);