blob: 87a73eec9b36a9e930ee3443e679d219ab80beb4 [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
38static inline void cpu_leave_lowpower(void)
39{
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{
Arnd Bergmann944483d2014-07-26 17:54:21 +020095 pmu_raw_writel(0, EXYNOS_ARM_CORE_CONFIGURATION(cpu));
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +090096}
97
98/**
99 * exynos_cpu_power_up : power up the specified cpu
100 * @cpu : the cpu to power up
101 *
102 * Power up the specified cpu
103 */
104void exynos_cpu_power_up(int cpu)
105{
Arnd Bergmann944483d2014-07-26 17:54:21 +0200106 pmu_raw_writel(S5P_CORE_LOCAL_PWR_EN,
107 EXYNOS_ARM_CORE_CONFIGURATION(cpu));
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +0900108}
109
110/**
111 * exynos_cpu_power_state : returns the power state of the cpu
112 * @cpu : the cpu to retrieve the power state from
113 *
114 */
115int exynos_cpu_power_state(int cpu)
116{
Arnd Bergmann944483d2014-07-26 17:54:21 +0200117 return (pmu_raw_readl(EXYNOS_ARM_CORE_STATUS(cpu)) &
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +0900118 S5P_CORE_LOCAL_PWR_EN);
119}
120
121/**
122 * exynos_cluster_power_down : power down the specified cluster
123 * @cluster : the cluster to power down
124 */
125void exynos_cluster_power_down(int cluster)
126{
Arnd Bergmann944483d2014-07-26 17:54:21 +0200127 pmu_raw_writel(0, EXYNOS_COMMON_CONFIGURATION(cluster));
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +0900128}
129
130/**
131 * exynos_cluster_power_up : power up the specified cluster
132 * @cluster : the cluster to power up
133 */
134void exynos_cluster_power_up(int cluster)
135{
Arnd Bergmann944483d2014-07-26 17:54:21 +0200136 pmu_raw_writel(S5P_CORE_LOCAL_PWR_EN,
137 EXYNOS_COMMON_CONFIGURATION(cluster));
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +0900138}
139
140/**
141 * exynos_cluster_power_state : returns the power state of the cluster
142 * @cluster : the cluster to retrieve the power state from
143 *
144 */
145int exynos_cluster_power_state(int cluster)
146{
Arnd Bergmann944483d2014-07-26 17:54:21 +0200147 return (pmu_raw_readl(EXYNOS_COMMON_STATUS(cluster)) &
148 S5P_CORE_LOCAL_PWR_EN);
Krzysztof Kozlowski7310d992014-07-19 04:45:02 +0900149}
150
Tomasz Figa1f054f52012-11-24 11:13:48 +0900151static inline void __iomem *cpu_boot_reg_base(void)
152{
153 if (soc_is_exynos4210() && samsung_rev() == EXYNOS4210_REV_1_1)
Pankaj Dubey2e94ac42014-07-19 03:43:22 +0900154 return pmu_base_addr + S5P_INFORM5;
Sachin Kamatb3205de2014-05-13 07:13:44 +0900155 return sysram_base_addr;
Tomasz Figa1f054f52012-11-24 11:13:48 +0900156}
157
158static inline void __iomem *cpu_boot_reg(int cpu)
159{
160 void __iomem *boot_reg;
161
162 boot_reg = cpu_boot_reg_base();
Sachin Kamatb3205de2014-05-13 07:13:44 +0900163 if (!boot_reg)
164 return ERR_PTR(-ENODEV);
Tomasz Figa1f054f52012-11-24 11:13:48 +0900165 if (soc_is_exynos4412())
166 boot_reg += 4*cpu;
Arun Kumar K86c6f142014-05-26 04:16:11 +0900167 else if (soc_is_exynos5420() || soc_is_exynos5800())
Chander Kashyap1580be32013-06-19 00:29:35 +0900168 boot_reg += 4;
Tomasz Figa1f054f52012-11-24 11:13:48 +0900169 return boot_reg;
170}
JungHi Min911c29b2011-07-16 13:39:09 +0900171
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900172/*
Russell King3705ff62010-12-18 10:53:12 +0000173 * Write pen_release in a way that is guaranteed to be visible to all
174 * observers, irrespective of whether they're taking part in coherency
175 * or not. This is necessary for the hotplug code to work reliably.
176 */
177static void write_pen_release(int val)
178{
179 pen_release = val;
180 smp_wmb();
Nicolas Pitref45913f2013-12-05 14:26:16 -0500181 sync_cache_w(&pen_release);
Russell King3705ff62010-12-18 10:53:12 +0000182}
183
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900184static void __iomem *scu_base_addr(void)
185{
186 return (void __iomem *)(S5P_VA_SCU);
187}
188
189static DEFINE_SPINLOCK(boot_lock);
190
Paul Gortmaker8bd26e32013-06-17 15:43:14 -0400191static void exynos_secondary_init(unsigned int cpu)
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900192{
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900193 /*
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900194 * let the primary processor know we're out of the
195 * pen, then head off into the C entry point
196 */
Russell King3705ff62010-12-18 10:53:12 +0000197 write_pen_release(-1);
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900198
199 /*
200 * Synchronise with the boot thread.
201 */
202 spin_lock(&boot_lock);
203 spin_unlock(&boot_lock);
204}
205
Paul Gortmaker8bd26e32013-06-17 15:43:14 -0400206static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900207{
208 unsigned long timeout;
Tomasz Figa9637f302014-07-16 02:59:18 +0900209 u32 mpidr = cpu_logical_map(cpu);
210 u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
Sachin Kamatb3205de2014-05-13 07:13:44 +0900211 int ret = -ENOSYS;
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900212
213 /*
214 * Set synchronisation state between this boot processor
215 * and the secondary one
216 */
217 spin_lock(&boot_lock);
218
219 /*
220 * The secondary processor is waiting to be released from
221 * the holding pen - release it, then wait for it to flag
222 * that it has been released by resetting pen_release.
223 *
Tomasz Figa9637f302014-07-16 02:59:18 +0900224 * Note that "pen_release" is the hardware CPU core ID, whereas
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900225 * "cpu" is Linux's internal ID.
226 */
Tomasz Figa9637f302014-07-16 02:59:18 +0900227 write_pen_release(core_id);
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900228
Tomasz Figa9637f302014-07-16 02:59:18 +0900229 if (!exynos_cpu_power_state(core_id)) {
230 exynos_cpu_power_up(core_id);
JungHi Min911c29b2011-07-16 13:39:09 +0900231 timeout = 10;
232
233 /* wait max 10 ms until cpu1 is on */
Tomasz Figa9637f302014-07-16 02:59:18 +0900234 while (exynos_cpu_power_state(core_id)
235 != S5P_CORE_LOCAL_PWR_EN) {
JungHi Min911c29b2011-07-16 13:39:09 +0900236 if (timeout-- == 0)
237 break;
238
239 mdelay(1);
240 }
241
242 if (timeout == 0) {
243 printk(KERN_ERR "cpu1 power enable failed");
244 spin_unlock(&boot_lock);
245 return -ETIMEDOUT;
246 }
247 }
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900248 /*
249 * Send the secondary CPU a soft interrupt, thereby causing
250 * the boot monitor to read the system wide flags register,
251 * and branch to the address found there.
252 */
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900253
254 timeout = jiffies + (1 * HZ);
255 while (time_before(jiffies, timeout)) {
Tomasz Figabeddf632012-12-11 13:58:43 +0900256 unsigned long boot_addr;
257
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900258 smp_rmb();
JungHi Min911c29b2011-07-16 13:39:09 +0900259
Tomasz Figabeddf632012-12-11 13:58:43 +0900260 boot_addr = virt_to_phys(exynos4_secondary_startup);
261
262 /*
263 * Try to set boot address using firmware first
264 * and fall back to boot register if it fails.
265 */
Tomasz Figa9637f302014-07-16 02:59:18 +0900266 ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr);
Sachin Kamatb3205de2014-05-13 07:13:44 +0900267 if (ret && ret != -ENOSYS)
268 goto fail;
269 if (ret == -ENOSYS) {
Tomasz Figa9637f302014-07-16 02:59:18 +0900270 void __iomem *boot_reg = cpu_boot_reg(core_id);
Sachin Kamatb3205de2014-05-13 07:13:44 +0900271
272 if (IS_ERR(boot_reg)) {
273 ret = PTR_ERR(boot_reg);
274 goto fail;
275 }
Krzysztof Kozlowski68ba9472014-09-14 02:31:19 +0900276 __raw_writel(boot_addr, boot_reg);
Sachin Kamatb3205de2014-05-13 07:13:44 +0900277 }
Tomasz Figabeddf632012-12-11 13:58:43 +0900278
Tomasz Figa9637f302014-07-16 02:59:18 +0900279 call_firmware_op(cpu_boot, core_id);
Tomasz Figabeddf632012-12-11 13:58:43 +0900280
Rob Herringb1cffeb2012-11-26 15:05:48 -0600281 arch_send_wakeup_ipi_mask(cpumask_of(cpu));
JungHi Min911c29b2011-07-16 13:39:09 +0900282
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900283 if (pen_release == -1)
284 break;
285
286 udelay(10);
287 }
288
289 /*
290 * now the secondary core is starting up let it run its
291 * calibrations, then wait for it to finish
292 */
Sachin Kamatb3205de2014-05-13 07:13:44 +0900293fail:
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900294 spin_unlock(&boot_lock);
295
Sachin Kamatb3205de2014-05-13 07:13:44 +0900296 return pen_release != -1 ? ret : 0;
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900297}
298
299/*
300 * Initialise the CPU possible map early - this describes the CPUs
301 * which may be present or become present in the system.
302 */
303
Marc Zyngier06853ae2011-09-08 13:15:22 +0100304static void __init exynos_smp_init_cpus(void)
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900305{
306 void __iomem *scu_base = scu_base_addr();
307 unsigned int i, ncores;
308
Russell Kingaf040ff2014-06-24 19:43:15 +0100309 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
Kukjin Kime9bba612012-01-25 15:35:57 +0900310 ncores = scu_base ? scu_get_core_count(scu_base) : 1;
Chander Kashyap1897d2f2013-06-19 00:29:34 +0900311 else
312 /*
313 * CPU Nodes are passed thru DT and set_cpu_possible
314 * is set by "arm_dt_init_cpu_maps".
315 */
316 return;
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900317
318 /* sanity check */
Russell Kinga06f9162011-10-20 22:04:18 +0100319 if (ncores > nr_cpu_ids) {
320 pr_warn("SMP: %u cores greater than maximum (%u), clipping\n",
321 ncores, nr_cpu_ids);
322 ncores = nr_cpu_ids;
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900323 }
324
325 for (i = 0; i < ncores; i++)
326 set_cpu_possible(i, true);
327}
328
Marc Zyngier06853ae2011-09-08 13:15:22 +0100329static void __init exynos_smp_prepare_cpus(unsigned int max_cpus)
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900330{
Tomasz Figa1f054f52012-11-24 11:13:48 +0900331 int i;
332
Olof Johansson1754c422014-06-02 21:47:46 -0700333 exynos_sysram_init();
334
Russell Kingaf040ff2014-06-24 19:43:15 +0100335 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
Kukjin Kime9bba612012-01-25 15:35:57 +0900336 scu_enable(scu_base_addr());
Russell King05c74a62010-12-03 11:09:48 +0000337
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900338 /*
Russell King05c74a62010-12-03 11:09:48 +0000339 * Write the address of secondary startup into the
340 * system-wide flags register. The boot monitor waits
341 * until it receives a soft interrupt, and then the
342 * secondary CPU branches to this address.
Tomasz Figabeddf632012-12-11 13:58:43 +0900343 *
344 * Try using firmware operation first and fall back to
345 * boot register if it fails.
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900346 */
Tomasz Figabeddf632012-12-11 13:58:43 +0900347 for (i = 1; i < max_cpus; ++i) {
Tomasz Figabeddf632012-12-11 13:58:43 +0900348 unsigned long boot_addr;
Tomasz Figa9637f302014-07-16 02:59:18 +0900349 u32 mpidr;
350 u32 core_id;
Sachin Kamatb3205de2014-05-13 07:13:44 +0900351 int ret;
Tomasz Figabeddf632012-12-11 13:58:43 +0900352
Tomasz Figa9637f302014-07-16 02:59:18 +0900353 mpidr = cpu_logical_map(i);
354 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
Tomasz Figabeddf632012-12-11 13:58:43 +0900355 boot_addr = virt_to_phys(exynos4_secondary_startup);
356
Tomasz Figa9637f302014-07-16 02:59:18 +0900357 ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr);
Sachin Kamatb3205de2014-05-13 07:13:44 +0900358 if (ret && ret != -ENOSYS)
359 break;
360 if (ret == -ENOSYS) {
Tomasz Figa9637f302014-07-16 02:59:18 +0900361 void __iomem *boot_reg = cpu_boot_reg(core_id);
Sachin Kamatb3205de2014-05-13 07:13:44 +0900362
363 if (IS_ERR(boot_reg))
364 break;
Krzysztof Kozlowski68ba9472014-09-14 02:31:19 +0900365 __raw_writel(boot_addr, boot_reg);
Sachin Kamatb3205de2014-05-13 07:13:44 +0900366 }
Tomasz Figabeddf632012-12-11 13:58:43 +0900367 }
Changhwan Youn2b12b5c2010-07-26 21:08:52 +0900368}
Marc Zyngier06853ae2011-09-08 13:15:22 +0100369
Krzysztof Kozlowski6f0b7c02014-09-14 02:49:31 +0900370#ifdef CONFIG_HOTPLUG_CPU
371/*
372 * platform-specific code to shutdown a CPU
373 *
374 * Called with IRQs disabled
375 */
376static void __ref exynos_cpu_die(unsigned int cpu)
377{
378 int spurious = 0;
379
380 v7_exit_coherency_flush(louis);
381
382 platform_do_lowpower(cpu, &spurious);
383
384 /*
385 * bring this CPU back into the world of cache
386 * coherency, and then restore interrupts
387 */
388 cpu_leave_lowpower();
389
390 if (spurious)
391 pr_warn("CPU%u: %u spurious wakeup calls\n", cpu, spurious);
392}
393#endif /* CONFIG_HOTPLUG_CPU */
394
Marc Zyngier06853ae2011-09-08 13:15:22 +0100395struct smp_operations exynos_smp_ops __initdata = {
396 .smp_init_cpus = exynos_smp_init_cpus,
397 .smp_prepare_cpus = exynos_smp_prepare_cpus,
398 .smp_secondary_init = exynos_secondary_init,
399 .smp_boot_secondary = exynos_boot_secondary,
400#ifdef CONFIG_HOTPLUG_CPU
401 .cpu_die = exynos_cpu_die,
402#endif
403};