mcpm-exynos.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. static bool secure_firmware __ro_after_init;
  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. "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. : \
  46. : "Ir" (pmu_base_addr + S5P_INFORM0) \
  47. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", \
  48. "r9", "r10", "ip", "lr", "memory")
  49. static int exynos_cpu_powerup(unsigned int cpu, unsigned int cluster)
  50. {
  51. unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  52. bool state;
  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. state = exynos_cpu_power_state(cpunr);
  58. exynos_cpu_power_up(cpunr);
  59. if (!state && secure_firmware) {
  60. /*
  61. * This assumes the cluster number of the big cores(Cortex A15)
  62. * is 0 and the Little cores(Cortex A7) is 1.
  63. * When the system was booted from the Little core,
  64. * they should be reset during power up cpu.
  65. */
  66. if (cluster &&
  67. cluster == MPIDR_AFFINITY_LEVEL(cpu_logical_map(0), 1)) {
  68. unsigned int timeout = 16;
  69. /*
  70. * Before we reset the Little cores, we should wait
  71. * the SPARE2 register is set to 1 because the init
  72. * codes of the iROM will set the register after
  73. * initialization.
  74. */
  75. while (timeout && !pmu_raw_readl(S5P_PMU_SPARE2)) {
  76. timeout--;
  77. udelay(10);
  78. }
  79. if (timeout == 0) {
  80. pr_err("cpu %u cluster %u powerup failed\n",
  81. cpu, cluster);
  82. exynos_cpu_power_down(cpunr);
  83. return -ETIMEDOUT;
  84. }
  85. pmu_raw_writel(EXYNOS5420_KFC_CORE_RESET(cpu),
  86. EXYNOS_SWRESET);
  87. }
  88. }
  89. return 0;
  90. }
  91. static int exynos_cluster_powerup(unsigned int cluster)
  92. {
  93. pr_debug("%s: cluster %u\n", __func__, cluster);
  94. if (cluster >= EXYNOS5420_NR_CLUSTERS)
  95. return -EINVAL;
  96. exynos_cluster_power_up(cluster);
  97. return 0;
  98. }
  99. static void exynos_cpu_powerdown_prepare(unsigned int cpu, unsigned int cluster)
  100. {
  101. unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  102. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  103. BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  104. cluster >= EXYNOS5420_NR_CLUSTERS);
  105. exynos_cpu_power_down(cpunr);
  106. }
  107. static void exynos_cluster_powerdown_prepare(unsigned int cluster)
  108. {
  109. pr_debug("%s: cluster %u\n", __func__, cluster);
  110. BUG_ON(cluster >= EXYNOS5420_NR_CLUSTERS);
  111. exynos_cluster_power_down(cluster);
  112. }
  113. static void exynos_cpu_cache_disable(void)
  114. {
  115. /* Disable and flush the local CPU cache. */
  116. exynos_v7_exit_coherency_flush(louis);
  117. }
  118. static void exynos_cluster_cache_disable(void)
  119. {
  120. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A15) {
  121. /*
  122. * On the Cortex-A15 we need to disable
  123. * L2 prefetching before flushing the cache.
  124. */
  125. asm volatile(
  126. "mcr p15, 1, %0, c15, c0, 3\n\t"
  127. "isb\n\t"
  128. "dsb"
  129. : : "r" (0x400));
  130. }
  131. /* Flush all cache levels for this cluster. */
  132. exynos_v7_exit_coherency_flush(all);
  133. /*
  134. * Disable cluster-level coherency by masking
  135. * incoming snoops and DVM messages:
  136. */
  137. cci_disable_port_by_cpu(read_cpuid_mpidr());
  138. }
  139. static int exynos_wait_for_powerdown(unsigned int cpu, unsigned int cluster)
  140. {
  141. unsigned int tries = 100;
  142. unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  143. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  144. BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  145. cluster >= EXYNOS5420_NR_CLUSTERS);
  146. /* Wait for the core state to be OFF */
  147. while (tries--) {
  148. if ((exynos_cpu_power_state(cpunr) == 0))
  149. return 0; /* success: the CPU is halted */
  150. /* Otherwise, wait and retry: */
  151. msleep(1);
  152. }
  153. return -ETIMEDOUT; /* timeout */
  154. }
  155. static void exynos_cpu_is_up(unsigned int cpu, unsigned int cluster)
  156. {
  157. /* especially when resuming: make sure power control is set */
  158. exynos_cpu_powerup(cpu, cluster);
  159. }
  160. static const struct mcpm_platform_ops exynos_power_ops = {
  161. .cpu_powerup = exynos_cpu_powerup,
  162. .cluster_powerup = exynos_cluster_powerup,
  163. .cpu_powerdown_prepare = exynos_cpu_powerdown_prepare,
  164. .cluster_powerdown_prepare = exynos_cluster_powerdown_prepare,
  165. .cpu_cache_disable = exynos_cpu_cache_disable,
  166. .cluster_cache_disable = exynos_cluster_cache_disable,
  167. .wait_for_powerdown = exynos_wait_for_powerdown,
  168. .cpu_is_up = exynos_cpu_is_up,
  169. };
  170. /*
  171. * Enable cluster-level coherency, in preparation for turning on the MMU.
  172. */
  173. static void __naked exynos_pm_power_up_setup(unsigned int affinity_level)
  174. {
  175. asm volatile ("\n"
  176. "cmp r0, #1\n"
  177. "bxne lr\n"
  178. "b cci_enable_port_for_self");
  179. }
  180. static const struct of_device_id exynos_dt_mcpm_match[] = {
  181. { .compatible = "samsung,exynos5420" },
  182. { .compatible = "samsung,exynos5800" },
  183. {},
  184. };
  185. static void exynos_mcpm_setup_entry_point(void)
  186. {
  187. /*
  188. * U-Boot SPL is hardcoded to jump to the start of ns_sram_base_addr
  189. * as part of secondary_cpu_start(). Let's redirect it to the
  190. * mcpm_entry_point(). This is done during both secondary boot-up as
  191. * well as system resume.
  192. */
  193. __raw_writel(0xe59f0000, ns_sram_base_addr); /* ldr r0, [pc, #0] */
  194. __raw_writel(0xe12fff10, ns_sram_base_addr + 4); /* bx r0 */
  195. __raw_writel(__pa_symbol(mcpm_entry_point), ns_sram_base_addr + 8);
  196. }
  197. static struct syscore_ops exynos_mcpm_syscore_ops = {
  198. .resume = exynos_mcpm_setup_entry_point,
  199. };
  200. static int __init exynos_mcpm_init(void)
  201. {
  202. struct device_node *node;
  203. unsigned int value, i;
  204. int ret;
  205. node = of_find_matching_node(NULL, exynos_dt_mcpm_match);
  206. if (!node)
  207. return -ENODEV;
  208. of_node_put(node);
  209. if (!cci_probed())
  210. return -ENODEV;
  211. node = of_find_compatible_node(NULL, NULL,
  212. "samsung,exynos4210-sysram-ns");
  213. if (!node)
  214. return -ENODEV;
  215. ns_sram_base_addr = of_iomap(node, 0);
  216. of_node_put(node);
  217. if (!ns_sram_base_addr) {
  218. pr_err("failed to map non-secure iRAM base address\n");
  219. return -ENOMEM;
  220. }
  221. secure_firmware = exynos_secure_firmware_available();
  222. /*
  223. * To increase the stability of KFC reset we need to program
  224. * the PMU SPARE3 register
  225. */
  226. pmu_raw_writel(EXYNOS5420_SWRESET_KFC_SEL, S5P_PMU_SPARE3);
  227. ret = mcpm_platform_register(&exynos_power_ops);
  228. if (!ret)
  229. ret = mcpm_sync_init(exynos_pm_power_up_setup);
  230. if (!ret)
  231. ret = mcpm_loopback(exynos_cluster_cache_disable); /* turn on the CCI */
  232. if (ret) {
  233. iounmap(ns_sram_base_addr);
  234. return ret;
  235. }
  236. mcpm_smp_set_ops();
  237. pr_info("Exynos MCPM support installed\n");
  238. /*
  239. * On Exynos5420/5800 for the A15 and A7 clusters:
  240. *
  241. * EXYNOS5420_ENABLE_AUTOMATIC_CORE_DOWN ensures that all the cores
  242. * in a cluster are turned off before turning off the cluster L2.
  243. *
  244. * EXYNOS5420_USE_ARM_CORE_DOWN_STATE ensures that a cores is powered
  245. * off before waking it up.
  246. *
  247. * EXYNOS5420_USE_L2_COMMON_UP_STATE ensures that cluster L2 will be
  248. * turned on before the first man is powered up.
  249. */
  250. for (i = 0; i < EXYNOS5420_NR_CLUSTERS; i++) {
  251. value = pmu_raw_readl(EXYNOS_COMMON_OPTION(i));
  252. value |= EXYNOS5420_ENABLE_AUTOMATIC_CORE_DOWN |
  253. EXYNOS5420_USE_ARM_CORE_DOWN_STATE |
  254. EXYNOS5420_USE_L2_COMMON_UP_STATE;
  255. pmu_raw_writel(value, EXYNOS_COMMON_OPTION(i));
  256. }
  257. exynos_mcpm_setup_entry_point();
  258. register_syscore_ops(&exynos_mcpm_syscore_ops);
  259. return ret;
  260. }
  261. early_initcall(exynos_mcpm_init);