blob: b7cfcdcc0984a35505d5b8cbf903a08dbb51c18d [file] [log] [blame]
Pankaj Dubey45523862014-07-08 07:54:13 +09001 /*
Kukjin Kim7d30e8b2011-02-14 16:33:10 +09002 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
Changhwan Youn2b12b5c2010-07-26 21:08:52 +09004 *
5 * Cloned from linux/arch/arm/mach-vexpress/platsmp.c
6 *
7 * Copyright (C) 2002 ARM Ltd.
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13*/
14
15#include <linux/init.h>
16#include <linux/errno.h>
17#include <linux/delay.h>
18#include <linux/device.h>
19#include <linux/jiffies.h>
20#include <linux/smp.h>
21#include <linux/io.h>
Sachin Kamatb3205de2014-05-13 07:13:44 +090022#include <linux/of_address.h>
Changhwan Youn2b12b5c2010-07-26 21:08:52 +090023
24#include <asm/cacheflush.h>
Krzysztof Kozlowski6f0b7c02014-09-14 02:49:31 +090025#include <asm/cp15.h>
Will Deaconeb504392012-01-20 12:01:12 +010026#include <asm/smp_plat.h>
Changhwan Youn2b12b5c2010-07-26 21:08:52 +090027#include <asm/smp_scu.h>
Tomasz Figabeddf632012-12-11 13:58:43 +090028#include <asm/firmware.h>
Changhwan Youn2b12b5c2010-07-26 21:08:52 +090029
Pankaj Dubey2e94ac42014-07-19 03:43:22 +090030#include <mach/map.h>
31
Marc Zyngier06853ae2011-09-08 13:15:22 +010032#include "common.h"
Kukjin Kim65c9a852013-12-19 04:06:56 +090033#include "regs-pmu.h"
Marc Zyngier06853ae2011-09-08 13:15:22 +010034
Kukjin Kim7d30e8b2011-02-14 16:33:10 +090035extern void exynos4_secondary_startup(void);
Changhwan Youn2b12b5c2010-07-26 21:08:52 +090036
Krzysztof Kozlowski6f0b7c02014-09-14 02:49:31 +090037#ifdef CONFIG_HOTPLUG_CPU
Krzysztof Kozlowski13cfa6c2014-09-14 02:49:32 +090038static inline void cpu_leave_lowpower(u32 core_id)
Krzysztof Kozlowski6f0b7c02014-09-14 02:49:31 +090039{
40 unsigned int v;
41
42 asm volatile(
43 "mrc p15, 0, %0, c1, c0, 0\n"
44 " orr %0, %0, %1\n"
45 " mcr p15, 0, %0, c1, c0, 0\n"
46 " mrc p15, 0, %0, c1, c0, 1\n"
47 " orr %0, %0, %2\n"
48 " mcr p15, 0, %0, c1, c0, 1\n"
49 : "=&r" (v)
50 : "Ir" (CR_C), "Ir" (0x40)
51 : "cc");
52}
53
54static inline void platform_do_lowpower(unsigned int cpu, int *spurious)
55{
56 u32 mpidr = cpu_logical_map(cpu);
57 u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
58
59 for (;;) {
60
61 /* Turn the CPU off on next WFI instruction. */
62 exynos_cpu_power_down(core_id);
63
64 wfi();
65
66 if (pen_release == core_id) {
67 /*
68 * OK, proper wakeup, we're done
69 */
70 break;
71 }
72
73 /*
74 * Getting here, means that we have come out of WFI without
75 * having been woken up - this shouldn't happen
76 *
77 * Just note it happening - when we're woken, we can report
78 * its occurrence.
79 */
80 (*spurious)++;
81 }
82}
83#endif /* CONFIG_HOTPLUG_CPU */
84
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +090085/**
86 * exynos_core_power_down : power down the specified cpu
87 * @cpu : the cpu to power down
88 *
89 * Power down the specified cpu. The sequence must be finished by a
90 * call to cpu_do_idle()
91 *
92 */
93void exynos_cpu_power_down(int cpu)
94{
Bartlomiej Zolnierkiewicz497ab3b2015-03-27 02:32:56 +090095 u32 core_conf;
96
Krzysztof Kozlowskica489c52015-02-27 05:50:41 +090097 if (cpu == 0 && (soc_is_exynos5420() || soc_is_exynos5800())) {
Abhilash Kesavanadc548d2014-11-07 09:20:16 +090098 /*
99 * Bypass power down for CPU0 during suspend. Check for
100 * the SYS_PWR_REG value to decide if we are suspending
101 * the system.
102 */
103 int val = pmu_raw_readl(EXYNOS5_ARM_CORE0_SYS_PWR_REG);
104
105 if (!(val & S5P_CORE_LOCAL_PWR_EN))
106 return;
107 }
Bartlomiej Zolnierkiewicz497ab3b2015-03-27 02:32:56 +0900108
109 core_conf = pmu_raw_readl(EXYNOS_ARM_CORE_CONFIGURATION(cpu));
110 core_conf &= ~S5P_CORE_LOCAL_PWR_EN;
111 pmu_raw_writel(core_conf, EXYNOS_ARM_CORE_CONFIGURATION(cpu));
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +0900112}
113
114/**
115 * exynos_cpu_power_up : power up the specified cpu
116 * @cpu : the cpu to power up
117 *
118 * Power up the specified cpu
119 */
120void exynos_cpu_power_up(int cpu)
121{
Bartlomiej Zolnierkiewicz497ab3b2015-03-27 02:32:56 +0900122 u32 core_conf = S5P_CORE_LOCAL_PWR_EN;
123
124 if (soc_is_exynos3250())
125 core_conf |= S5P_CORE_AUTOWAKEUP_EN;
126
127 pmu_raw_writel(core_conf,
Arnd Bergmann944483d2014-07-26 17:54:21 +0200128 EXYNOS_ARM_CORE_CONFIGURATION(cpu));
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +0900129}
130
131/**
132 * exynos_cpu_power_state : returns the power state of the cpu
133 * @cpu : the cpu to retrieve the power state from
134 *
135 */
136int exynos_cpu_power_state(int cpu)
137{
Arnd Bergmann944483d2014-07-26 17:54:21 +0200138 return (pmu_raw_readl(EXYNOS_ARM_CORE_STATUS(cpu)) &
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +0900139 S5P_CORE_LOCAL_PWR_EN);
140}
141
142/**
143 * exynos_cluster_power_down : power down the specified cluster
144 * @cluster : the cluster to power down
145 */
146void exynos_cluster_power_down(int cluster)
147{
Arnd Bergmann944483d2014-07-26 17:54:21 +0200148 pmu_raw_writel(0, EXYNOS_COMMON_CONFIGURATION(cluster));
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +0900149}
150
151/**
152 * exynos_cluster_power_up : power up the specified cluster
153 * @cluster : the cluster to power up
154 */
155void exynos_cluster_power_up(int cluster)
156{
Arnd Bergmann944483d2014-07-26 17:54:21 +0200157 pmu_raw_writel(S5P_CORE_LOCAL_PWR_EN,
158 EXYNOS_COMMON_CONFIGURATION(cluster));
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +0900159}
160
161/**
162 * exynos_cluster_power_state : returns the power state of the cluster
163 * @cluster : the cluster to retrieve the power state from
164 *
165 */
166int exynos_cluster_power_state(int cluster)
167{
Arnd Bergmann944483d2014-07-26 17:54:21 +0200168 return (pmu_raw_readl(EXYNOS_COMMON_STATUS(cluster)) &
169 S5P_CORE_LOCAL_PWR_EN);
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +0900170}
171
Bartlomiej Zolnierkiewicz712eddf72015-01-24 14:05:50 +0900172void __iomem *cpu_boot_reg_base(void)
Tomasz Figa1f054f52012-11-24 11:13:48 +0900173{
174 if (soc_is_exynos4210() && samsung_rev() == EXYNOS4210_REV_1_1)
Pankaj Dubey2e94ac42014-07-19 03:43:22 +0900175 return pmu_base_addr + S5P_INFORM5;
Sachin Kamatb3205de2014-05-13 07:13:44 +0900176 return sysram_base_addr;
Tomasz Figa1f054f52012-11-24 11:13:48 +0900177}
178
179static inline void __iomem *cpu_boot_reg(int cpu)
180{
181 void __iomem *boot_reg;
182
183 boot_reg = cpu_boot_reg_base();
Sachin Kamatb3205de2014-05-13 07:13:44 +0900184 if (!boot_reg)
185 return ERR_PTR(-ENODEV);
Tomasz Figa1f054f52012-11-24 11:13:48 +0900186 if (soc_is_exynos4412())
187 boot_reg += 4*cpu;
Arun Kumar K86c6f142014-05-26 04:16:11 +0900188 else if (soc_is_exynos5420() || soc_is_exynos5800())
Chander Kashyap1580be32013-06-19 00:29:35 +0900189 boot_reg += 4;
Tomasz Figa1f054f52012-11-24 11:13:48 +0900190 return boot_reg;
191}
JungHi Min911c29b2011-07-16 13:39:09 +0900192
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900193/*
Krzysztof Kozlowskib588aae2014-09-25 18:15:13 +0900194 * Set wake up by local power mode and execute software reset for given core.
195 *
196 * Currently this is needed only when booting secondary CPU on Exynos3250.
197 */
198static void exynos_core_restart(u32 core_id)
199{
200 u32 val;
201
202 if (!of_machine_is_compatible("samsung,exynos3250"))
203 return;
204
Bartlomiej Zolnierkiewicz497ab3b2015-03-27 02:32:56 +0900205 while (!pmu_raw_readl(S5P_PMU_SPARE2))
206 udelay(10);
207 udelay(10);
208
Krzysztof Kozlowskib588aae2014-09-25 18:15:13 +0900209 val = pmu_raw_readl(EXYNOS_ARM_CORE_STATUS(core_id));
210 val |= S5P_CORE_WAKEUP_FROM_LOCAL_CFG;
211 pmu_raw_writel(val, EXYNOS_ARM_CORE_STATUS(core_id));
212
Krzysztof Kozlowskib588aae2014-09-25 18:15:13 +0900213 pmu_raw_writel(EXYNOS_CORE_PO_RESET(core_id), EXYNOS_SWRESET);
214}
215
216/*
Russell King3705ff62010-12-18 10:53:12 +0000217 * Write pen_release in a way that is guaranteed to be visible to all
218 * observers, irrespective of whether they're taking part in coherency
219 * or not. This is necessary for the hotplug code to work reliably.
220 */
221static void write_pen_release(int val)
222{
223 pen_release = val;
224 smp_wmb();
Nicolas Pitref45913f2013-12-05 14:26:16 -0500225 sync_cache_w(&pen_release);
Russell King3705ff62010-12-18 10:53:12 +0000226}
227
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900228static void __iomem *scu_base_addr(void)
229{
230 return (void __iomem *)(S5P_VA_SCU);
231}
232
233static DEFINE_SPINLOCK(boot_lock);
234
Paul Gortmaker8bd26e32013-06-17 15:43:14 -0400235static void exynos_secondary_init(unsigned int cpu)
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900236{
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900237 /*
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900238 * let the primary processor know we're out of the
239 * pen, then head off into the C entry point
240 */
Russell King3705ff62010-12-18 10:53:12 +0000241 write_pen_release(-1);
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900242
243 /*
244 * Synchronise with the boot thread.
245 */
246 spin_lock(&boot_lock);
247 spin_unlock(&boot_lock);
248}
249
Bartlomiej Zolnierkiewicz955d4cf2015-03-18 14:09:55 +0100250static int exynos_set_boot_addr(u32 core_id, unsigned long boot_addr)
251{
252 int ret;
253
254 /*
255 * Try to set boot address using firmware first
256 * and fall back to boot register if it fails.
257 */
258 ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr);
259 if (ret && ret != -ENOSYS)
260 goto fail;
261 if (ret == -ENOSYS) {
262 void __iomem *boot_reg = cpu_boot_reg(core_id);
263
264 if (IS_ERR(boot_reg)) {
265 ret = PTR_ERR(boot_reg);
266 goto fail;
267 }
268 __raw_writel(boot_addr, boot_reg);
269 ret = 0;
270 }
271fail:
272 return ret;
273}
274
Paul Gortmaker8bd26e32013-06-17 15:43:14 -0400275static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900276{
277 unsigned long timeout;
Tomasz Figa9637f302014-07-16 02:59:18 +0900278 u32 mpidr = cpu_logical_map(cpu);
279 u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
Sachin Kamatb3205de2014-05-13 07:13:44 +0900280 int ret = -ENOSYS;
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900281
282 /*
283 * Set synchronisation state between this boot processor
284 * and the secondary one
285 */
286 spin_lock(&boot_lock);
287
288 /*
289 * The secondary processor is waiting to be released from
290 * the holding pen - release it, then wait for it to flag
291 * that it has been released by resetting pen_release.
292 *
Tomasz Figa9637f302014-07-16 02:59:18 +0900293 * Note that "pen_release" is the hardware CPU core ID, whereas
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900294 * "cpu" is Linux's internal ID.
295 */
Tomasz Figa9637f302014-07-16 02:59:18 +0900296 write_pen_release(core_id);
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900297
Tomasz Figa9637f302014-07-16 02:59:18 +0900298 if (!exynos_cpu_power_state(core_id)) {
299 exynos_cpu_power_up(core_id);
JungHi Min911c29b2011-07-16 13:39:09 +0900300 timeout = 10;
301
302 /* wait max 10 ms until cpu1 is on */
Tomasz Figa9637f302014-07-16 02:59:18 +0900303 while (exynos_cpu_power_state(core_id)
304 != S5P_CORE_LOCAL_PWR_EN) {
JungHi Min911c29b2011-07-16 13:39:09 +0900305 if (timeout-- == 0)
306 break;
307
308 mdelay(1);
309 }
310
311 if (timeout == 0) {
312 printk(KERN_ERR "cpu1 power enable failed");
313 spin_unlock(&boot_lock);
314 return -ETIMEDOUT;
315 }
316 }
Krzysztof Kozlowskib588aae2014-09-25 18:15:13 +0900317
318 exynos_core_restart(core_id);
319
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900320 /*
321 * Send the secondary CPU a soft interrupt, thereby causing
322 * the boot monitor to read the system wide flags register,
323 * and branch to the address found there.
324 */
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900325
326 timeout = jiffies + (1 * HZ);
327 while (time_before(jiffies, timeout)) {
Tomasz Figabeddf632012-12-11 13:58:43 +0900328 unsigned long boot_addr;
329
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900330 smp_rmb();
JungHi Min911c29b2011-07-16 13:39:09 +0900331
Tomasz Figabeddf632012-12-11 13:58:43 +0900332 boot_addr = virt_to_phys(exynos4_secondary_startup);
333
Bartlomiej Zolnierkiewicz955d4cf2015-03-18 14:09:55 +0100334 ret = exynos_set_boot_addr(core_id, boot_addr);
335 if (ret)
Sachin Kamatb3205de2014-05-13 07:13:44 +0900336 goto fail;
Tomasz Figabeddf632012-12-11 13:58:43 +0900337
Tomasz Figa9637f302014-07-16 02:59:18 +0900338 call_firmware_op(cpu_boot, core_id);
Tomasz Figabeddf632012-12-11 13:58:43 +0900339
Bartlomiej Zolnierkiewicz497ab3b2015-03-27 02:32:56 +0900340 if (soc_is_exynos3250())
341 dsb_sev();
342 else
343 arch_send_wakeup_ipi_mask(cpumask_of(cpu));
JungHi Min911c29b2011-07-16 13:39:09 +0900344
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900345 if (pen_release == -1)
346 break;
347
348 udelay(10);
349 }
350
Bartlomiej Zolnierkiewicz9f294c12015-03-18 14:09:53 +0100351 if (pen_release != -1)
352 ret = -ETIMEDOUT;
353
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900354 /*
355 * now the secondary core is starting up let it run its
356 * calibrations, then wait for it to finish
357 */
Sachin Kamatb3205de2014-05-13 07:13:44 +0900358fail:
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900359 spin_unlock(&boot_lock);
360
Sachin Kamatb3205de2014-05-13 07:13:44 +0900361 return pen_release != -1 ? ret : 0;
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900362}
363
364/*
365 * Initialise the CPU possible map early - this describes the CPUs
366 * which may be present or become present in the system.
367 */
368
Marc Zyngier06853ae2011-09-08 13:15:22 +0100369static void __init exynos_smp_init_cpus(void)
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900370{
371 void __iomem *scu_base = scu_base_addr();
372 unsigned int i, ncores;
373
Russell Kingaf040ff2014-06-24 19:43:15 +0100374 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
Kukjin Kime9bba612012-01-25 15:35:57 +0900375 ncores = scu_base ? scu_get_core_count(scu_base) : 1;
Chander Kashyap1897d2f2013-06-19 00:29:34 +0900376 else
377 /*
378 * CPU Nodes are passed thru DT and set_cpu_possible
379 * is set by "arm_dt_init_cpu_maps".
380 */
381 return;
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900382
383 /* sanity check */
Russell Kinga06f9162011-10-20 22:04:18 +0100384 if (ncores > nr_cpu_ids) {
385 pr_warn("SMP: %u cores greater than maximum (%u), clipping\n",
386 ncores, nr_cpu_ids);
387 ncores = nr_cpu_ids;
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900388 }
389
390 for (i = 0; i < ncores; i++)
391 set_cpu_possible(i, true);
392}
393
Marc Zyngier06853ae2011-09-08 13:15:22 +0100394static void __init exynos_smp_prepare_cpus(unsigned int max_cpus)
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900395{
Tomasz Figa1f054f52012-11-24 11:13:48 +0900396 int i;
397
Olof Johansson1754c422014-06-02 21:47:46 -0700398 exynos_sysram_init();
399
Krzysztof Kozlowski6f024972015-03-11 11:13:57 +0100400 exynos_set_delayed_reset_assertion(true);
401
Russell Kingaf040ff2014-06-24 19:43:15 +0100402 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
Kukjin Kime9bba612012-01-25 15:35:57 +0900403 scu_enable(scu_base_addr());
Russell King05c74a62010-12-03 11:09:48 +0000404
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900405 /*
Russell King05c74a62010-12-03 11:09:48 +0000406 * Write the address of secondary startup into the
407 * system-wide flags register. The boot monitor waits
408 * until it receives a soft interrupt, and then the
409 * secondary CPU branches to this address.
Tomasz Figabeddf632012-12-11 13:58:43 +0900410 *
411 * Try using firmware operation first and fall back to
412 * boot register if it fails.
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900413 */
Tomasz Figabeddf632012-12-11 13:58:43 +0900414 for (i = 1; i < max_cpus; ++i) {
Tomasz Figabeddf632012-12-11 13:58:43 +0900415 unsigned long boot_addr;
Tomasz Figa9637f302014-07-16 02:59:18 +0900416 u32 mpidr;
417 u32 core_id;
Sachin Kamatb3205de2014-05-13 07:13:44 +0900418 int ret;
Tomasz Figabeddf632012-12-11 13:58:43 +0900419
Tomasz Figa9637f302014-07-16 02:59:18 +0900420 mpidr = cpu_logical_map(i);
421 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
Tomasz Figabeddf632012-12-11 13:58:43 +0900422 boot_addr = virt_to_phys(exynos4_secondary_startup);
423
Bartlomiej Zolnierkiewicz955d4cf2015-03-18 14:09:55 +0100424 ret = exynos_set_boot_addr(core_id, boot_addr);
425 if (ret)
Sachin Kamatb3205de2014-05-13 07:13:44 +0900426 break;
Tomasz Figabeddf632012-12-11 13:58:43 +0900427 }
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900428}
Marc Zyngier06853ae2011-09-08 13:15:22 +0100429
Krzysztof Kozlowski6f0b7c02014-09-14 02:49:31 +0900430#ifdef CONFIG_HOTPLUG_CPU
431/*
432 * platform-specific code to shutdown a CPU
433 *
434 * Called with IRQs disabled
435 */
Krzysztof Kozlowski27b9ee82014-09-14 02:49:32 +0900436static void exynos_cpu_die(unsigned int cpu)
Krzysztof Kozlowski6f0b7c02014-09-14 02:49:31 +0900437{
438 int spurious = 0;
Krzysztof Kozlowski13cfa6c2014-09-14 02:49:32 +0900439 u32 mpidr = cpu_logical_map(cpu);
440 u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
Krzysztof Kozlowski6f0b7c02014-09-14 02:49:31 +0900441
442 v7_exit_coherency_flush(louis);
443
444 platform_do_lowpower(cpu, &spurious);
445
446 /*
447 * bring this CPU back into the world of cache
448 * coherency, and then restore interrupts
449 */
Krzysztof Kozlowski13cfa6c2014-09-14 02:49:32 +0900450 cpu_leave_lowpower(core_id);
Krzysztof Kozlowski6f0b7c02014-09-14 02:49:31 +0900451
452 if (spurious)
453 pr_warn("CPU%u: %u spurious wakeup calls\n", cpu, spurious);
454}
455#endif /* CONFIG_HOTPLUG_CPU */
456
Marc Zyngier06853ae2011-09-08 13:15:22 +0100457struct smp_operations exynos_smp_ops __initdata = {
458 .smp_init_cpus = exynos_smp_init_cpus,
459 .smp_prepare_cpus = exynos_smp_prepare_cpus,
460 .smp_secondary_init = exynos_secondary_init,
461 .smp_boot_secondary = exynos_boot_secondary,
462#ifdef CONFIG_HOTPLUG_CPU
463 .cpu_die = exynos_cpu_die,
464#endif
465};