mcpm-exynos.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2014 Samsung Electronics Co., Ltd.
  3. // http://www.samsung.com
  4. //
  5. // Based on arch/arm/mach-vexpress/dcscb.c
  6. #include <linux/arm-cci.h>
  7. #include <linux/delay.h>
  8. #include <linux/io.h>
  9. #include <linux/of_address.h>
  10. #include <linux/syscore_ops.h>
  11. #include <linux/soc/samsung/exynos-regs-pmu.h>
  12. #include <asm/cputype.h>
  13. #include <asm/cp15.h>
  14. #include <asm/mcpm.h>
  15. #include <asm/smp_plat.h>
  16. #include "common.h"
  17. #define EXYNOS5420_CPUS_PER_CLUSTER 4
  18. #define EXYNOS5420_NR_CLUSTERS 2
  19. #define EXYNOS5420_ENABLE_AUTOMATIC_CORE_DOWN BIT(9)
  20. #define EXYNOS5420_USE_ARM_CORE_DOWN_STATE BIT(29)
  21. #define EXYNOS5420_USE_L2_COMMON_UP_STATE BIT(30)
  22. static void __iomem *ns_sram_base_addr __ro_after_init;
  23. /*
  24. * The common v7_exit_coherency_flush API could not be used because of the
  25. * Erratum 799270 workaround. This macro is the same as the common one (in
  26. * arch/arm/include/asm/cacheflush.h) except for the erratum handling.
  27. */
  28. #define exynos_v7_exit_coherency_flush(level) \
  29. asm volatile( \
  30. "stmfd sp!, {fp, ip}\n\t"\
  31. "mrc p15, 0, r0, c1, c0, 0 @ get SCTLR\n\t" \
  32. "bic r0, r0, #"__stringify(CR_C)"\n\t" \
  33. "mcr p15, 0, r0, c1, c0, 0 @ set SCTLR\n\t" \
  34. "isb\n\t"\
  35. "bl v7_flush_dcache_"__stringify(level)"\n\t" \
  36. "mrc p15, 0, r0, c1, c0, 1 @ get ACTLR\n\t" \
  37. "bic r0, r0, #(1 << 6) @ disable local coherency\n\t" \
  38. /* Dummy Load of a device register to avoid Erratum 799270 */ \
  39. "ldr r4, [%0]\n\t" \
  40. "and r4, r4, #0\n\t" \
  41. "orr r0, r0, r4\n\t" \
  42. "mcr p15, 0, r0, c1, c0, 1 @ set ACTLR\n\t" \
  43. "isb\n\t" \
  44. "dsb\n\t" \
  45. "ldmfd sp!, {fp, ip}" \
  46. : \
  47. : "Ir" (pmu_base_addr + S5P_INFORM0) \
  48. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", \
  49. "r9", "r10", "lr", "memory")
  50. static int exynos_cpu_powerup(unsigned int cpu, unsigned int cluster)
  51. {
  52. unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  53. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  54. if (cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  55. cluster >= EXYNOS5420_NR_CLUSTERS)
  56. return -EINVAL;
  57. if (!exynos_cpu_power_state(cpunr)) {
  58. exynos_cpu_power_up(cpunr);
  59. /*
  60. * This assumes the cluster number of the big cores(Cortex A15)
  61. * is 0 and the Little cores(Cortex A7) is 1.
  62. * When the system was booted from the Little core,
  63. * they should be reset during power up cpu.
  64. */
  65. if (cluster &&
  66. cluster == MPIDR_AFFINITY_LEVEL(cpu_logical_map(0), 1)) {
  67. /*
  68. * Before we reset the Little cores, we should wait
  69. * the SPARE2 register is set to 1 because the init
  70. * codes of the iROM will set the register after
  71. * initialization.
  72. */
  73. while (!pmu_raw_readl(S5P_PMU_SPARE2))
  74. udelay(10);
  75. pmu_raw_writel(EXYNOS5420_KFC_CORE_RESET(cpu),
  76. EXYNOS_SWRESET);
  77. }
  78. }
  79. return 0;
  80. }
  81. static int exynos_cluster_powerup(unsigned int cluster)
  82. {
  83. pr_debug("%s: cluster %u\n", __func__, cluster);
  84. if (cluster >= EXYNOS5420_NR_CLUSTERS)
  85. return -EINVAL;
  86. exynos_cluster_power_up(cluster);
  87. return 0;
  88. }
  89. static void exynos_cpu_powerdown_prepare(unsigned int cpu, unsigned int cluster)
  90. {
  91. unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  92. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  93. BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  94. cluster >= EXYNOS5420_NR_CLUSTERS);
  95. exynos_cpu_power_down(cpunr);
  96. }
  97. static void exynos_cluster_powerdown_prepare(unsigned int cluster)
  98. {
  99. pr_debug("%s: cluster %u\n", __func__, cluster);
  100. BUG_ON(cluster >= EXYNOS5420_NR_CLUSTERS);
  101. exynos_cluster_power_down(cluster);
  102. }
  103. static void exynos_cpu_cache_disable(void)
  104. {
  105. /* Disable and flush the local CPU cache. */
  106. exynos_v7_exit_coherency_flush(louis);
  107. }
  108. static void exynos_cluster_cache_disable(void)
  109. {
  110. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A15) {
  111. /*
  112. * On the Cortex-A15 we need to disable
  113. * L2 prefetching before flushing the cache.
  114. */
  115. asm volatile(
  116. "mcr p15, 1, %0, c15, c0, 3\n\t"
  117. "isb\n\t"
  118. "dsb"
  119. : : "r" (0x400));
  120. }
  121. /* Flush all cache levels for this cluster. */
  122. exynos_v7_exit_coherency_flush(all);
  123. /*
  124. * Disable cluster-level coherency by masking
  125. * incoming snoops and DVM messages:
  126. */
  127. cci_disable_port_by_cpu(read_cpuid_mpidr());
  128. }
  129. static int exynos_wait_for_powerdown(unsigned int cpu, unsigned int cluster)
  130. {
  131. unsigned int tries = 100;
  132. unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  133. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  134. BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  135. cluster >= EXYNOS5420_NR_CLUSTERS);
  136. /* Wait for the core state to be OFF */
  137. while (tries--) {
  138. if ((exynos_cpu_power_state(cpunr) == 0))
  139. return 0; /* success: the CPU is halted */
  140. /* Otherwise, wait and retry: */
  141. msleep(1);
  142. }
  143. return -ETIMEDOUT; /* timeout */
  144. }
  145. static void exynos_cpu_is_up(unsigned int cpu, unsigned int cluster)
  146. {
  147. /* especially when resuming: make sure power control is set */
  148. exynos_cpu_powerup(cpu, cluster);
  149. }
  150. static const struct mcpm_platform_ops exynos_power_ops = {
  151. .cpu_powerup = exynos_cpu_powerup,
  152. .cluster_powerup = exynos_cluster_powerup,
  153. .cpu_powerdown_prepare = exynos_cpu_powerdown_prepare,
  154. .cluster_powerdown_prepare = exynos_cluster_powerdown_prepare,
  155. .cpu_cache_disable = exynos_cpu_cache_disable,
  156. .cluster_cache_disable = exynos_cluster_cache_disable,
  157. .wait_for_powerdown = exynos_wait_for_powerdown,
  158. .cpu_is_up = exynos_cpu_is_up,
  159. };
  160. /*
  161. * Enable cluster-level coherency, in preparation for turning on the MMU.
  162. */
  163. static void __naked exynos_pm_power_up_setup(unsigned int affinity_level)
  164. {
  165. asm volatile ("\n"
  166. "cmp r0, #1\n"
  167. "bxne lr\n"
  168. "b cci_enable_port_for_self");
  169. }
  170. static const struct of_device_id exynos_dt_mcpm_match[] = {
  171. { .compatible = "samsung,exynos5420" },
  172. { .compatible = "samsung,exynos5800" },
  173. {},
  174. };
  175. static void exynos_mcpm_setup_entry_point(void)
  176. {
  177. /*
  178. * U-Boot SPL is hardcoded to jump to the start of ns_sram_base_addr
  179. * as part of secondary_cpu_start(). Let's redirect it to the
  180. * mcpm_entry_point(). This is done during both secondary boot-up as
  181. * well as system resume.
  182. */
  183. __raw_writel(0xe59f0000, ns_sram_base_addr); /* ldr r0, [pc, #0] */
  184. __raw_writel(0xe12fff10, ns_sram_base_addr + 4); /* bx r0 */
  185. __raw_writel(__pa_symbol(mcpm_entry_point), ns_sram_base_addr + 8);
  186. }
  187. static struct syscore_ops exynos_mcpm_syscore_ops = {
  188. .resume = exynos_mcpm_setup_entry_point,
  189. };
  190. static int __init exynos_mcpm_init(void)
  191. {
  192. struct device_node *node;
  193. unsigned int value, i;
  194. int ret;
  195. node = of_find_matching_node(NULL, exynos_dt_mcpm_match);
  196. if (!node)
  197. return -ENODEV;
  198. of_node_put(node);
  199. if (!cci_probed())
  200. return -ENODEV;
  201. node = of_find_compatible_node(NULL, NULL,
  202. "samsung,exynos4210-sysram-ns");
  203. if (!node)
  204. return -ENODEV;
  205. ns_sram_base_addr = of_iomap(node, 0);
  206. of_node_put(node);
  207. if (!ns_sram_base_addr) {
  208. pr_err("failed to map non-secure iRAM base address\n");
  209. return -ENOMEM;
  210. }
  211. /*
  212. * To increase the stability of KFC reset we need to program
  213. * the PMU SPARE3 register
  214. */
  215. pmu_raw_writel(EXYNOS5420_SWRESET_KFC_SEL, S5P_PMU_SPARE3);
  216. ret = mcpm_platform_register(&exynos_power_ops);
  217. if (!ret)
  218. ret = mcpm_sync_init(exynos_pm_power_up_setup);
  219. if (!ret)
  220. ret = mcpm_loopback(exynos_cluster_cache_disable); /* turn on the CCI */
  221. if (ret) {
  222. iounmap(ns_sram_base_addr);
  223. return ret;
  224. }
  225. mcpm_smp_set_ops();
  226. pr_info("Exynos MCPM support installed\n");
  227. /*
  228. * On Exynos5420/5800 for the A15 and A7 clusters:
  229. *
  230. * EXYNOS5420_ENABLE_AUTOMATIC_CORE_DOWN ensures that all the cores
  231. * in a cluster are turned off before turning off the cluster L2.
  232. *
  233. * EXYNOS5420_USE_ARM_CORE_DOWN_STATE ensures that a cores is powered
  234. * off before waking it up.
  235. *
  236. * EXYNOS5420_USE_L2_COMMON_UP_STATE ensures that cluster L2 will be
  237. * turned on before the first man is powered up.
  238. */
  239. for (i = 0; i < EXYNOS5420_NR_CLUSTERS; i++) {
  240. value = pmu_raw_readl(EXYNOS_COMMON_OPTION(i));
  241. value |= EXYNOS5420_ENABLE_AUTOMATIC_CORE_DOWN |
  242. EXYNOS5420_USE_ARM_CORE_DOWN_STATE |
  243. EXYNOS5420_USE_L2_COMMON_UP_STATE;
  244. pmu_raw_writel(value, EXYNOS_COMMON_OPTION(i));
  245. }
  246. exynos_mcpm_setup_entry_point();
  247. register_syscore_ops(&exynos_mcpm_syscore_ops);
  248. return ret;
  249. }
  250. early_initcall(exynos_mcpm_init);