1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a7ed099fSArnd Bergmann /*
3a7ed099fSArnd Bergmann * arch/arm/mach-spear13xx/platsmp.c
4a7ed099fSArnd Bergmann *
5a7ed099fSArnd Bergmann * based upon linux/arch/arm/mach-realview/platsmp.c
6a7ed099fSArnd Bergmann *
7a7ed099fSArnd Bergmann * Copyright (C) 2012 ST Microelectronics Ltd.
89cc23682SViresh Kumar * Shiraz Hashim <shiraz.linux.kernel@gmail.com>
9a7ed099fSArnd Bergmann */
10a7ed099fSArnd Bergmann
11a7ed099fSArnd Bergmann #include <linux/delay.h>
12a7ed099fSArnd Bergmann #include <linux/jiffies.h>
13a7ed099fSArnd Bergmann #include <linux/io.h>
14a7ed099fSArnd Bergmann #include <linux/smp.h>
15a7ed099fSArnd Bergmann #include <asm/cacheflush.h>
16a7ed099fSArnd Bergmann #include <asm/smp_scu.h>
17*c164620aSArnd Bergmann #include "spear.h"
182b9c613cSArnd Bergmann #include "generic.h"
19a7ed099fSArnd Bergmann
206213f70eSRussell King /* XXX spear_pen_release is cargo culted code - DO NOT COPY XXX */
216213f70eSRussell King volatile int spear_pen_release = -1;
226213f70eSRussell King
2303a166e2SRussell King /*
246213f70eSRussell King * XXX CARGO CULTED CODE - DO NOT COPY XXX
256213f70eSRussell King *
266213f70eSRussell King * Write spear_pen_release in a way that is guaranteed to be visible to
276213f70eSRussell King * all observers, irrespective of whether they're taking part in coherency
2803a166e2SRussell King * or not. This is necessary for the hotplug code to work reliably.
2903a166e2SRussell King */
spear_write_pen_release(int val)306213f70eSRussell King static void spear_write_pen_release(int val)
3103a166e2SRussell King {
326213f70eSRussell King spear_pen_release = val;
3303a166e2SRussell King smp_wmb();
346213f70eSRussell King sync_cache_w(&spear_pen_release);
3503a166e2SRussell King }
3603a166e2SRussell King
37a7ed099fSArnd Bergmann static DEFINE_SPINLOCK(boot_lock);
38a7ed099fSArnd Bergmann
39a7ed099fSArnd Bergmann static void __iomem *scu_base = IOMEM(VA_SCU_BASE);
40a7ed099fSArnd Bergmann
spear13xx_secondary_init(unsigned int cpu)418bd26e3aSPaul Gortmaker static void spear13xx_secondary_init(unsigned int cpu)
42a7ed099fSArnd Bergmann {
43a7ed099fSArnd Bergmann /*
44a7ed099fSArnd Bergmann * let the primary processor know we're out of the
45a7ed099fSArnd Bergmann * pen, then head off into the C entry point
46a7ed099fSArnd Bergmann */
476213f70eSRussell King spear_write_pen_release(-1);
48a7ed099fSArnd Bergmann
49a7ed099fSArnd Bergmann /*
50a7ed099fSArnd Bergmann * Synchronise with the boot thread.
51a7ed099fSArnd Bergmann */
52a7ed099fSArnd Bergmann spin_lock(&boot_lock);
53a7ed099fSArnd Bergmann spin_unlock(&boot_lock);
54a7ed099fSArnd Bergmann }
55a7ed099fSArnd Bergmann
spear13xx_boot_secondary(unsigned int cpu,struct task_struct * idle)568bd26e3aSPaul Gortmaker static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
57a7ed099fSArnd Bergmann {
58a7ed099fSArnd Bergmann unsigned long timeout;
59a7ed099fSArnd Bergmann
60a7ed099fSArnd Bergmann /*
61a7ed099fSArnd Bergmann * set synchronisation state between this boot processor
62a7ed099fSArnd Bergmann * and the secondary one
63a7ed099fSArnd Bergmann */
64a7ed099fSArnd Bergmann spin_lock(&boot_lock);
65a7ed099fSArnd Bergmann
66a7ed099fSArnd Bergmann /*
67a7ed099fSArnd Bergmann * The secondary processor is waiting to be released from
68a7ed099fSArnd Bergmann * the holding pen - release it, then wait for it to flag
696213f70eSRussell King * that it has been released by resetting spear_pen_release.
70a7ed099fSArnd Bergmann *
716213f70eSRussell King * Note that "spear_pen_release" is the hardware CPU ID, whereas
72a7ed099fSArnd Bergmann * "cpu" is Linux's internal ID.
73a7ed099fSArnd Bergmann */
746213f70eSRussell King spear_write_pen_release(cpu);
75a7ed099fSArnd Bergmann
76a7ed099fSArnd Bergmann timeout = jiffies + (1 * HZ);
77a7ed099fSArnd Bergmann while (time_before(jiffies, timeout)) {
78a7ed099fSArnd Bergmann smp_rmb();
796213f70eSRussell King if (spear_pen_release == -1)
80a7ed099fSArnd Bergmann break;
81a7ed099fSArnd Bergmann
82a7ed099fSArnd Bergmann udelay(10);
83a7ed099fSArnd Bergmann }
84a7ed099fSArnd Bergmann
85a7ed099fSArnd Bergmann /*
86a7ed099fSArnd Bergmann * now the secondary core is starting up let it run its
87a7ed099fSArnd Bergmann * calibrations, then wait for it to finish
88a7ed099fSArnd Bergmann */
89a7ed099fSArnd Bergmann spin_unlock(&boot_lock);
90a7ed099fSArnd Bergmann
916213f70eSRussell King return spear_pen_release != -1 ? -ENOSYS : 0;
92a7ed099fSArnd Bergmann }
93a7ed099fSArnd Bergmann
94a7ed099fSArnd Bergmann /*
95a7ed099fSArnd Bergmann * Initialise the CPU possible map early - this describes the CPUs
96a7ed099fSArnd Bergmann * which may be present or become present in the system.
97a7ed099fSArnd Bergmann */
spear13xx_smp_init_cpus(void)98a7ed099fSArnd Bergmann static void __init spear13xx_smp_init_cpus(void)
99a7ed099fSArnd Bergmann {
100a7ed099fSArnd Bergmann unsigned int i, ncores = scu_get_core_count(scu_base);
101a7ed099fSArnd Bergmann
102a7ed099fSArnd Bergmann if (ncores > nr_cpu_ids) {
103a7ed099fSArnd Bergmann pr_warn("SMP: %u cores greater than maximum (%u), clipping\n",
104a7ed099fSArnd Bergmann ncores, nr_cpu_ids);
105a7ed099fSArnd Bergmann ncores = nr_cpu_ids;
106a7ed099fSArnd Bergmann }
107a7ed099fSArnd Bergmann
108a7ed099fSArnd Bergmann for (i = 0; i < ncores; i++)
109a7ed099fSArnd Bergmann set_cpu_possible(i, true);
110a7ed099fSArnd Bergmann }
111a7ed099fSArnd Bergmann
spear13xx_smp_prepare_cpus(unsigned int max_cpus)112a7ed099fSArnd Bergmann static void __init spear13xx_smp_prepare_cpus(unsigned int max_cpus)
113a7ed099fSArnd Bergmann {
114a7ed099fSArnd Bergmann
115a7ed099fSArnd Bergmann scu_enable(scu_base);
116a7ed099fSArnd Bergmann
117a7ed099fSArnd Bergmann /*
118a7ed099fSArnd Bergmann * Write the address of secondary startup into the system-wide location
119a7ed099fSArnd Bergmann * (presently it is in SRAM). The BootMonitor waits until it receives a
120a7ed099fSArnd Bergmann * soft interrupt, and then the secondary CPU branches to this address.
121a7ed099fSArnd Bergmann */
12264fc2a94SFlorian Fainelli __raw_writel(__pa_symbol(spear13xx_secondary_startup), SYS_LOCATION);
123a7ed099fSArnd Bergmann }
124a7ed099fSArnd Bergmann
12575305275SMasahiro Yamada const struct smp_operations spear13xx_smp_ops __initconst = {
126a7ed099fSArnd Bergmann .smp_init_cpus = spear13xx_smp_init_cpus,
127a7ed099fSArnd Bergmann .smp_prepare_cpus = spear13xx_smp_prepare_cpus,
128a7ed099fSArnd Bergmann .smp_secondary_init = spear13xx_secondary_init,
129a7ed099fSArnd Bergmann .smp_boot_secondary = spear13xx_boot_secondary,
130a7ed099fSArnd Bergmann #ifdef CONFIG_HOTPLUG_CPU
131a7ed099fSArnd Bergmann .cpu_die = spear13xx_cpu_die,
132a7ed099fSArnd Bergmann #endif
133a7ed099fSArnd Bergmann };
134