mcpm-exynos.c 11 KB

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