2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
5 * arch/arm/mach-exynos/mcpm-exynos.c
7 * Based on arch/arm/mach-vexpress/dcscb.c
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.
14 #include <linux/arm-cci.h>
15 #include <linux/delay.h>
17 #include <linux/of_address.h>
19 #include <asm/cputype.h>
26 #define EXYNOS5420_CPUS_PER_CLUSTER 4
27 #define EXYNOS5420_NR_CLUSTERS 2
29 #define EXYNOS5420_ENABLE_AUTOMATIC_CORE_DOWN BIT(9)
30 #define EXYNOS5420_USE_ARM_CORE_DOWN_STATE BIT(29)
31 #define EXYNOS5420_USE_L2_COMMON_UP_STATE BIT(30)
34 * The common v7_exit_coherency_flush API could not be used because of the
35 * Erratum 799270 workaround. This macro is the same as the common one (in
36 * arch/arm/include/asm/cacheflush.h) except for the erratum handling.
38 #define exynos_v7_exit_coherency_flush(level) \
40 "stmfd sp!, {fp, ip}\n\t"\
41 "mrc p15, 0, r0, c1, c0, 0 @ get SCTLR\n\t" \
42 "bic r0, r0, #"__stringify(CR_C)"\n\t" \
43 "mcr p15, 0, r0, c1, c0, 0 @ set SCTLR\n\t" \
45 "bl v7_flush_dcache_"__stringify(level)"\n\t" \
46 "mrc p15, 0, r0, c1, c0, 1 @ get ACTLR\n\t" \
47 "bic r0, r0, #(1 << 6) @ disable local coherency\n\t" \
48 /* Dummy Load of a device register to avoid Erratum 799270 */ \
50 "and r4, r4, #0\n\t" \
51 "orr r0, r0, r4\n\t" \
52 "mcr p15, 0, r0, c1, c0, 1 @ set ACTLR\n\t" \
55 "ldmfd sp!, {fp, ip}" \
57 : "Ir" (pmu_base_addr + S5P_INFORM0) \
58 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", \
59 "r9", "r10", "lr", "memory")
62 * We can't use regular spinlocks. In the switcher case, it is possible
63 * for an outbound CPU to call power_down() after its inbound counterpart
64 * is already live using the same logical CPU number which trips lockdep
67 static arch_spinlock_t exynos_mcpm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
69 cpu_use_count[EXYNOS5420_CPUS_PER_CLUSTER][EXYNOS5420_NR_CLUSTERS];
71 #define exynos_cluster_usecnt(cluster) \
72 (cpu_use_count[0][cluster] + \
73 cpu_use_count[1][cluster] + \
74 cpu_use_count[2][cluster] + \
75 cpu_use_count[3][cluster])
77 #define exynos_cluster_unused(cluster) !exynos_cluster_usecnt(cluster)
79 static int exynos_power_up(unsigned int cpu, unsigned int cluster)
81 unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
83 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
84 if (cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
85 cluster >= EXYNOS5420_NR_CLUSTERS)
89 * Since this is called with IRQs enabled, and no arch_spin_lock_irq
90 * variant exists, we need to disable IRQs manually here.
93 arch_spin_lock(&exynos_mcpm_lock);
95 cpu_use_count[cpu][cluster]++;
96 if (cpu_use_count[cpu][cluster] == 1) {
97 bool was_cluster_down =
98 (exynos_cluster_usecnt(cluster) == 1);
101 * Turn on the cluster (L2/COMMON) and then power on the
104 if (was_cluster_down)
105 exynos_cluster_power_up(cluster);
107 exynos_cpu_power_up(cpunr);
108 } else if (cpu_use_count[cpu][cluster] != 2) {
110 * The only possible values are:
113 * 2 = CPU requested to be up before it had a chance
114 * to actually make itself down.
115 * Any other value is a bug.
120 arch_spin_unlock(&exynos_mcpm_lock);
127 * NOTE: This function requires the stack data to be visible through power down
128 * and can only be executed on processors like A15 and A7 that hit the cache
129 * with the C bit clear in the SCTLR register.
131 static void exynos_power_down(void)
133 unsigned int mpidr, cpu, cluster;
134 bool last_man = false, skip_wfi = false;
137 mpidr = read_cpuid_mpidr();
138 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
139 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
140 cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
142 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
143 BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
144 cluster >= EXYNOS5420_NR_CLUSTERS);
146 __mcpm_cpu_going_down(cpu, cluster);
148 arch_spin_lock(&exynos_mcpm_lock);
149 BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
150 cpu_use_count[cpu][cluster]--;
151 if (cpu_use_count[cpu][cluster] == 0) {
152 exynos_cpu_power_down(cpunr);
154 if (exynos_cluster_unused(cluster)) {
155 exynos_cluster_power_down(cluster);
158 } else if (cpu_use_count[cpu][cluster] == 1) {
160 * A power_up request went ahead of us.
161 * Even if we do not want to shut this CPU down,
162 * the caller expects a certain state as if the WFI
163 * was aborted. So let's continue with cache cleaning.
170 if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
171 arch_spin_unlock(&exynos_mcpm_lock);
173 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A15) {
175 * On the Cortex-A15 we need to disable
176 * L2 prefetching before flushing the cache.
179 "mcr p15, 1, %0, c15, c0, 3\n\t"
185 /* Flush all cache levels for this cluster. */
186 exynos_v7_exit_coherency_flush(all);
189 * Disable cluster-level coherency by masking
190 * incoming snoops and DVM messages:
192 cci_disable_port_by_cpu(mpidr);
194 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
196 arch_spin_unlock(&exynos_mcpm_lock);
198 /* Disable and flush the local CPU cache. */
199 exynos_v7_exit_coherency_flush(louis);
202 __mcpm_cpu_down(cpu, cluster);
204 /* Now we are prepared for power-down, do it: */
208 /* Not dead at this point? Let our caller cope. */
211 static int exynos_wait_for_powerdown(unsigned int cpu, unsigned int cluster)
213 unsigned int tries = 100;
214 unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
216 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
217 BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
218 cluster >= EXYNOS5420_NR_CLUSTERS);
220 /* Wait for the core state to be OFF */
222 if (ACCESS_ONCE(cpu_use_count[cpu][cluster]) == 0) {
223 if ((exynos_cpu_power_state(cpunr) == 0))
224 return 0; /* success: the CPU is halted */
227 /* Otherwise, wait and retry: */
231 return -ETIMEDOUT; /* timeout */
234 static void exynos_powered_up(void)
236 unsigned int mpidr, cpu, cluster;
238 mpidr = read_cpuid_mpidr();
239 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
240 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
242 arch_spin_lock(&exynos_mcpm_lock);
243 if (cpu_use_count[cpu][cluster] == 0)
244 cpu_use_count[cpu][cluster] = 1;
245 arch_spin_unlock(&exynos_mcpm_lock);
248 static void exynos_suspend(u64 residency)
250 unsigned int mpidr, cpunr;
255 * Execution reaches here only if cpu did not power down.
256 * Hence roll back the changes done in exynos_power_down function.
258 * CAUTION: "This function requires the stack data to be visible through
259 * power down and can only be executed on processors like A15 and A7
260 * that hit the cache with the C bit clear in the SCTLR register."
262 mpidr = read_cpuid_mpidr();
263 cpunr = exynos_pmu_cpunr(mpidr);
265 exynos_cpu_power_up(cpunr);
268 static const struct mcpm_platform_ops exynos_power_ops = {
269 .power_up = exynos_power_up,
270 .power_down = exynos_power_down,
271 .wait_for_powerdown = exynos_wait_for_powerdown,
272 .suspend = exynos_suspend,
273 .powered_up = exynos_powered_up,
276 static void __init exynos_mcpm_usage_count_init(void)
278 unsigned int mpidr, cpu, cluster;
280 mpidr = read_cpuid_mpidr();
281 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
282 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
284 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
285 BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
286 cluster >= EXYNOS5420_NR_CLUSTERS);
288 cpu_use_count[cpu][cluster] = 1;
292 * Enable cluster-level coherency, in preparation for turning on the MMU.
294 static void __naked exynos_pm_power_up_setup(unsigned int affinity_level)
299 "b cci_enable_port_for_self");
302 static void __init exynos_cache_off(void)
304 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A15) {
305 /* disable L2 prefetching on the Cortex-A15 */
307 "mcr p15, 1, %0, c15, c0, 3\n\t"
312 exynos_v7_exit_coherency_flush(all);
315 static const struct of_device_id exynos_dt_mcpm_match[] = {
316 { .compatible = "samsung,exynos5420" },
317 { .compatible = "samsung,exynos5800" },
321 static int __init exynos_mcpm_init(void)
323 struct device_node *node;
324 void __iomem *ns_sram_base_addr;
325 unsigned int value, i;
328 node = of_find_matching_node(NULL, exynos_dt_mcpm_match);
336 node = of_find_compatible_node(NULL, NULL,
337 "samsung,exynos4210-sysram-ns");
341 ns_sram_base_addr = of_iomap(node, 0);
343 if (!ns_sram_base_addr) {
344 pr_err("failed to map non-secure iRAM base address\n");
349 * To increase the stability of KFC reset we need to program
350 * the PMU SPARE3 register
352 pmu_raw_writel(EXYNOS5420_SWRESET_KFC_SEL, S5P_PMU_SPARE3);
354 exynos_mcpm_usage_count_init();
356 ret = mcpm_platform_register(&exynos_power_ops);
358 ret = mcpm_sync_init(exynos_pm_power_up_setup);
360 ret = mcpm_loopback(exynos_cache_off); /* turn on the CCI */
362 iounmap(ns_sram_base_addr);
368 pr_info("Exynos MCPM support installed\n");
371 * On Exynos5420/5800 for the A15 and A7 clusters:
373 * EXYNOS5420_ENABLE_AUTOMATIC_CORE_DOWN ensures that all the cores
374 * in a cluster are turned off before turning off the cluster L2.
376 * EXYNOS5420_USE_ARM_CORE_DOWN_STATE ensures that a cores is powered
377 * off before waking it up.
379 * EXYNOS5420_USE_L2_COMMON_UP_STATE ensures that cluster L2 will be
380 * turned on before the first man is powered up.
382 for (i = 0; i < EXYNOS5420_NR_CLUSTERS; i++) {
383 value = pmu_raw_readl(EXYNOS_COMMON_OPTION(i));
384 value |= EXYNOS5420_ENABLE_AUTOMATIC_CORE_DOWN |
385 EXYNOS5420_USE_ARM_CORE_DOWN_STATE |
386 EXYNOS5420_USE_L2_COMMON_UP_STATE;
387 pmu_raw_writel(value, EXYNOS_COMMON_OPTION(i));
391 * U-Boot SPL is hardcoded to jump to the start of ns_sram_base_addr
392 * as part of secondary_cpu_start(). Let's redirect it to the
393 * mcpm_entry_point().
395 __raw_writel(0xe59f0000, ns_sram_base_addr); /* ldr r0, [pc, #0] */
396 __raw_writel(0xe12fff10, ns_sram_base_addr + 4); /* bx r0 */
397 __raw_writel(virt_to_phys(mcpm_entry_point), ns_sram_base_addr + 8);
399 iounmap(ns_sram_base_addr);
404 early_initcall(exynos_mcpm_init);