mcpm-exynos.c 11 KB

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