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
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.
34 #define exynos_v7_exit_coherency_flush(level) \
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" \
41 "bl v7_flush_dcache_"__stringify(level)"\n\t" \
43 "mrc p15, 0, r0, c1, c0, 1 @ get ACTLR\n\t" \
44 "bic r0, r0, #(1 << 6) @ disable local coherency\n\t" \
45 /* Dummy Load of a device register to avoid Erratum 799270 */ \
47 "and r4, r4, #0\n\t" \
48 "orr r0, r0, r4\n\t" \
49 "mcr p15, 0, r0, c1, c0, 1 @ set ACTLR\n\t" \
52 "ldmfd sp!, {fp, ip}" \
54 : "Ir" (S5P_INFORM0) \
55 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", \
56 "r9", "r10", "lr", "memory")
59 * We can't use regular spinlocks. In the switcher case, it is possible
60 * for an outbound CPU to call power_down() after its inbound counterpart
61 * is already live using the same logical CPU number which trips lockdep
64 static arch_spinlock_t exynos_mcpm_lock = __ARCH_SPIN_LOCK_UNLOCKED;
66 cpu_use_count[EXYNOS5420_CPUS_PER_CLUSTER][EXYNOS5420_NR_CLUSTERS];
68 #define exynos_cluster_usecnt(cluster) \
69 (cpu_use_count[0][cluster] + \
70 cpu_use_count[1][cluster] + \
71 cpu_use_count[2][cluster] + \
72 cpu_use_count[3][cluster])
74 #define exynos_cluster_unused(cluster) !exynos_cluster_usecnt(cluster)
76 static int exynos_cluster_power_control(unsigned int cluster, int enable)
78 unsigned int tries = 100;
82 exynos_cluster_power_up(cluster);
83 val = S5P_CORE_LOCAL_PWR_EN;
85 exynos_cluster_power_down(cluster);
89 /* Wait until cluster power control is applied */
91 if (exynos_cluster_power_state(cluster) == val)
96 pr_debug("timed out waiting for cluster %u to power %s\n", cluster,
97 enable ? "on" : "off");
102 static int exynos_power_up(unsigned int cpu, unsigned int cluster)
104 unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
107 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
108 if (cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
109 cluster >= EXYNOS5420_NR_CLUSTERS)
113 * Since this is called with IRQs enabled, and no arch_spin_lock_irq
114 * variant exists, we need to disable IRQs manually here.
117 arch_spin_lock(&exynos_mcpm_lock);
119 cpu_use_count[cpu][cluster]++;
120 if (cpu_use_count[cpu][cluster] == 1) {
121 bool was_cluster_down =
122 (exynos_cluster_usecnt(cluster) == 1);
125 * Turn on the cluster (L2/COMMON) and then power on the
128 if (was_cluster_down)
129 err = exynos_cluster_power_control(cluster, 1);
132 exynos_cpu_power_up(cpunr);
134 exynos_cluster_power_control(cluster, 0);
135 } else if (cpu_use_count[cpu][cluster] != 2) {
137 * The only possible values are:
140 * 2 = CPU requested to be up before it had a chance
141 * to actually make itself down.
142 * Any other value is a bug.
147 arch_spin_unlock(&exynos_mcpm_lock);
154 * NOTE: This function requires the stack data to be visible through power down
155 * and can only be executed on processors like A15 and A7 that hit the cache
156 * with the C bit clear in the SCTLR register.
158 static void exynos_power_down(void)
160 unsigned int mpidr, cpu, cluster;
161 bool last_man = false, skip_wfi = false;
164 mpidr = read_cpuid_mpidr();
165 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
166 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
167 cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
169 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
170 BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
171 cluster >= EXYNOS5420_NR_CLUSTERS);
173 __mcpm_cpu_going_down(cpu, cluster);
175 arch_spin_lock(&exynos_mcpm_lock);
176 BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
177 cpu_use_count[cpu][cluster]--;
178 if (cpu_use_count[cpu][cluster] == 0) {
179 exynos_cpu_power_down(cpunr);
181 if (exynos_cluster_unused(cluster))
182 /* TODO: Turn off the cluster here to save power. */
184 } else if (cpu_use_count[cpu][cluster] == 1) {
186 * A power_up request went ahead of us.
187 * Even if we do not want to shut this CPU down,
188 * the caller expects a certain state as if the WFI
189 * was aborted. So let's continue with cache cleaning.
196 if (last_man && __mcpm_outbound_enter_critical(cpu, cluster)) {
197 arch_spin_unlock(&exynos_mcpm_lock);
199 if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A15) {
201 * On the Cortex-A15 we need to disable
202 * L2 prefetching before flushing the cache.
205 "mcr p15, 1, %0, c15, c0, 3\n\t"
211 /* Flush all cache levels for this cluster. */
212 exynos_v7_exit_coherency_flush(all);
215 * Disable cluster-level coherency by masking
216 * incoming snoops and DVM messages:
218 cci_disable_port_by_cpu(mpidr);
220 __mcpm_outbound_leave_critical(cluster, CLUSTER_DOWN);
222 arch_spin_unlock(&exynos_mcpm_lock);
224 /* Disable and flush the local CPU cache. */
225 exynos_v7_exit_coherency_flush(louis);
228 __mcpm_cpu_down(cpu, cluster);
230 /* Now we are prepared for power-down, do it: */
234 /* Not dead at this point? Let our caller cope. */
237 static int exynos_wait_for_powerdown(unsigned int cpu, unsigned int cluster)
239 unsigned int tries = 100;
240 unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
242 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
243 BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
244 cluster >= EXYNOS5420_NR_CLUSTERS);
246 /* Wait for the core state to be OFF */
248 if (ACCESS_ONCE(cpu_use_count[cpu][cluster]) == 0) {
249 if ((exynos_cpu_power_state(cpunr) == 0))
250 return 0; /* success: the CPU is halted */
253 /* Otherwise, wait and retry: */
257 return -ETIMEDOUT; /* timeout */
260 static void exynos_powered_up(void)
262 unsigned int mpidr, cpu, cluster;
264 mpidr = read_cpuid_mpidr();
265 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
266 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
268 arch_spin_lock(&exynos_mcpm_lock);
269 if (cpu_use_count[cpu][cluster] == 0)
270 cpu_use_count[cpu][cluster] = 1;
271 arch_spin_unlock(&exynos_mcpm_lock);
274 static void exynos_suspend(u64 residency)
276 unsigned int mpidr, cpunr;
281 * Execution reaches here only if cpu did not power down.
282 * Hence roll back the changes done in exynos_power_down function.
284 * CAUTION: "This function requires the stack data to be visible through
285 * power down and can only be executed on processors like A15 and A7
286 * that hit the cache with the C bit clear in the SCTLR register."
288 mpidr = read_cpuid_mpidr();
289 cpunr = exynos_pmu_cpunr(mpidr);
291 exynos_cpu_power_up(cpunr);
294 static const struct mcpm_platform_ops exynos_power_ops = {
295 .power_up = exynos_power_up,
296 .power_down = exynos_power_down,
297 .wait_for_powerdown = exynos_wait_for_powerdown,
298 .suspend = exynos_suspend,
299 .powered_up = exynos_powered_up,
302 static void __init exynos_mcpm_usage_count_init(void)
304 unsigned int mpidr, cpu, cluster;
306 mpidr = read_cpuid_mpidr();
307 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
308 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
310 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
311 BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
312 cluster >= EXYNOS5420_NR_CLUSTERS);
314 cpu_use_count[cpu][cluster] = 1;
318 * Enable cluster-level coherency, in preparation for turning on the MMU.
320 static void __naked exynos_pm_power_up_setup(unsigned int affinity_level)
325 "b cci_enable_port_for_self");
328 static const struct of_device_id exynos_dt_mcpm_match[] = {
329 { .compatible = "samsung,exynos5420" },
330 { .compatible = "samsung,exynos5800" },
334 static int __init exynos_mcpm_init(void)
336 struct device_node *node;
337 void __iomem *ns_sram_base_addr;
340 node = of_find_matching_node(NULL, exynos_dt_mcpm_match);
348 node = of_find_compatible_node(NULL, NULL,
349 "samsung,exynos4210-sysram-ns");
353 ns_sram_base_addr = of_iomap(node, 0);
355 if (!ns_sram_base_addr) {
356 pr_err("failed to map non-secure iRAM base address\n");
361 * To increase the stability of KFC reset we need to program
362 * the PMU SPARE3 register
364 __raw_writel(EXYNOS5420_SWRESET_KFC_SEL, S5P_PMU_SPARE3);
366 exynos_mcpm_usage_count_init();
368 ret = mcpm_platform_register(&exynos_power_ops);
370 ret = mcpm_sync_init(exynos_pm_power_up_setup);
372 iounmap(ns_sram_base_addr);
378 pr_info("Exynos MCPM support installed\n");
381 * U-Boot SPL is hardcoded to jump to the start of ns_sram_base_addr
382 * as part of secondary_cpu_start(). Let's redirect it to the
383 * mcpm_entry_point().
385 __raw_writel(0xe59f0000, ns_sram_base_addr); /* ldr r0, [pc, #0] */
386 __raw_writel(0xe12fff10, ns_sram_base_addr + 4); /* bx r0 */
387 __raw_writel(virt_to_phys(mcpm_entry_point), ns_sram_base_addr + 8);
389 iounmap(ns_sram_base_addr);
394 early_initcall(exynos_mcpm_init);