mcpm-exynos.c 11 KB

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