1745373e3SChen-Yu Tsai // SPDX-License-Identifier: GPL-2.0
2745373e3SChen-Yu Tsai /*
3745373e3SChen-Yu Tsai * Copyright (c) 2018 Chen-Yu Tsai
4745373e3SChen-Yu Tsai *
5745373e3SChen-Yu Tsai * Chen-Yu Tsai <wens@csie.org>
6745373e3SChen-Yu Tsai *
7745373e3SChen-Yu Tsai * arch/arm/mach-sunxi/mc_smp.c
8745373e3SChen-Yu Tsai *
9745373e3SChen-Yu Tsai * Based on Allwinner code, arch/arm/mach-exynos/mcpm-exynos.c, and
10745373e3SChen-Yu Tsai * arch/arm/mach-hisi/platmcpm.c
11745373e3SChen-Yu Tsai * Cluster cache enable trampoline code adapted from MCPM framework
12745373e3SChen-Yu Tsai */
13745373e3SChen-Yu Tsai
14745373e3SChen-Yu Tsai #include <linux/arm-cci.h>
15745373e3SChen-Yu Tsai #include <linux/cpu_pm.h>
16745373e3SChen-Yu Tsai #include <linux/delay.h>
17745373e3SChen-Yu Tsai #include <linux/io.h>
187cbea632SChen-Yu Tsai #include <linux/iopoll.h>
197cbea632SChen-Yu Tsai #include <linux/irqchip/arm-gic.h>
20745373e3SChen-Yu Tsai #include <linux/of.h>
21745373e3SChen-Yu Tsai #include <linux/of_address.h>
22745373e3SChen-Yu Tsai #include <linux/smp.h>
23745373e3SChen-Yu Tsai
24745373e3SChen-Yu Tsai #include <asm/cacheflush.h>
25745373e3SChen-Yu Tsai #include <asm/cp15.h>
26745373e3SChen-Yu Tsai #include <asm/cputype.h>
27745373e3SChen-Yu Tsai #include <asm/idmap.h>
28745373e3SChen-Yu Tsai #include <asm/smp_plat.h>
29745373e3SChen-Yu Tsai #include <asm/suspend.h>
30745373e3SChen-Yu Tsai
31745373e3SChen-Yu Tsai #define SUNXI_CPUS_PER_CLUSTER 4
32745373e3SChen-Yu Tsai #define SUNXI_NR_CLUSTERS 2
33745373e3SChen-Yu Tsai
347cbea632SChen-Yu Tsai #define POLL_USEC 100
357cbea632SChen-Yu Tsai #define TIMEOUT_USEC 100000
367cbea632SChen-Yu Tsai
37745373e3SChen-Yu Tsai #define CPUCFG_CX_CTRL_REG0(c) (0x10 * (c))
38745373e3SChen-Yu Tsai #define CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE(n) BIT(n)
39745373e3SChen-Yu Tsai #define CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE_ALL 0xf
40745373e3SChen-Yu Tsai #define CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A7 BIT(4)
41745373e3SChen-Yu Tsai #define CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A15 BIT(0)
42745373e3SChen-Yu Tsai #define CPUCFG_CX_CTRL_REG1(c) (0x10 * (c) + 0x4)
43745373e3SChen-Yu Tsai #define CPUCFG_CX_CTRL_REG1_ACINACTM BIT(0)
447cbea632SChen-Yu Tsai #define CPUCFG_CX_STATUS(c) (0x30 + 0x4 * (c))
457cbea632SChen-Yu Tsai #define CPUCFG_CX_STATUS_STANDBYWFI(n) BIT(16 + (n))
467cbea632SChen-Yu Tsai #define CPUCFG_CX_STATUS_STANDBYWFIL2 BIT(0)
47745373e3SChen-Yu Tsai #define CPUCFG_CX_RST_CTRL(c) (0x80 + 0x4 * (c))
48745373e3SChen-Yu Tsai #define CPUCFG_CX_RST_CTRL_DBG_SOC_RST BIT(24)
49745373e3SChen-Yu Tsai #define CPUCFG_CX_RST_CTRL_ETM_RST(n) BIT(20 + (n))
50745373e3SChen-Yu Tsai #define CPUCFG_CX_RST_CTRL_ETM_RST_ALL (0xf << 20)
51745373e3SChen-Yu Tsai #define CPUCFG_CX_RST_CTRL_DBG_RST(n) BIT(16 + (n))
52745373e3SChen-Yu Tsai #define CPUCFG_CX_RST_CTRL_DBG_RST_ALL (0xf << 16)
53745373e3SChen-Yu Tsai #define CPUCFG_CX_RST_CTRL_H_RST BIT(12)
54745373e3SChen-Yu Tsai #define CPUCFG_CX_RST_CTRL_L2_RST BIT(8)
55745373e3SChen-Yu Tsai #define CPUCFG_CX_RST_CTRL_CX_RST(n) BIT(4 + (n))
56745373e3SChen-Yu Tsai #define CPUCFG_CX_RST_CTRL_CORE_RST(n) BIT(n)
576961275eSMylène Josserand #define CPUCFG_CX_RST_CTRL_CORE_RST_ALL (0xf << 0)
58745373e3SChen-Yu Tsai
59745373e3SChen-Yu Tsai #define PRCM_CPU_PO_RST_CTRL(c) (0x4 + 0x4 * (c))
60745373e3SChen-Yu Tsai #define PRCM_CPU_PO_RST_CTRL_CORE(n) BIT(n)
61745373e3SChen-Yu Tsai #define PRCM_CPU_PO_RST_CTRL_CORE_ALL 0xf
62745373e3SChen-Yu Tsai #define PRCM_PWROFF_GATING_REG(c) (0x100 + 0x4 * (c))
636961275eSMylène Josserand /* The power off register for clusters are different from a80 and a83t */
646961275eSMylène Josserand #define PRCM_PWROFF_GATING_REG_CLUSTER_SUN8I BIT(0)
65c64738e5SMylène Josserand #define PRCM_PWROFF_GATING_REG_CLUSTER_SUN9I BIT(4)
66745373e3SChen-Yu Tsai #define PRCM_PWROFF_GATING_REG_CORE(n) BIT(n)
67745373e3SChen-Yu Tsai #define PRCM_PWR_SWITCH_REG(c, cpu) (0x140 + 0x10 * (c) + 0x4 * (cpu))
68745373e3SChen-Yu Tsai #define PRCM_CPU_SOFT_ENTRY_REG 0x164
69745373e3SChen-Yu Tsai
706961275eSMylène Josserand /* R_CPUCFG registers, specific to sun8i-a83t */
716961275eSMylène Josserand #define R_CPUCFG_CLUSTER_PO_RST_CTRL(c) (0x30 + (c) * 0x4)
726961275eSMylène Josserand #define R_CPUCFG_CLUSTER_PO_RST_CTRL_CORE(n) BIT(n)
736961275eSMylène Josserand #define R_CPUCFG_CPU_SOFT_ENTRY_REG 0x01a4
746961275eSMylène Josserand
758eaa0648SChen-Yu Tsai #define CPU0_SUPPORT_HOTPLUG_MAGIC0 0xFA50392F
768eaa0648SChen-Yu Tsai #define CPU0_SUPPORT_HOTPLUG_MAGIC1 0x790DCA3A
778eaa0648SChen-Yu Tsai
78745373e3SChen-Yu Tsai static void __iomem *cpucfg_base;
79745373e3SChen-Yu Tsai static void __iomem *prcm_base;
808eaa0648SChen-Yu Tsai static void __iomem *sram_b_smp_base;
816961275eSMylène Josserand static void __iomem *r_cpucfg_base;
82745373e3SChen-Yu Tsai
83dff052ccSMylène Josserand extern void sunxi_mc_smp_secondary_startup(void);
84dff052ccSMylène Josserand extern void sunxi_mc_smp_resume(void);
851631090eSMylène Josserand static bool is_a83t;
86dff052ccSMylène Josserand
sunxi_core_is_cortex_a15(unsigned int core,unsigned int cluster)87745373e3SChen-Yu Tsai static bool sunxi_core_is_cortex_a15(unsigned int core, unsigned int cluster)
88745373e3SChen-Yu Tsai {
89745373e3SChen-Yu Tsai struct device_node *node;
90745373e3SChen-Yu Tsai int cpu = cluster * SUNXI_CPUS_PER_CLUSTER + core;
91995c770bSWen Yang bool is_compatible;
92745373e3SChen-Yu Tsai
93745373e3SChen-Yu Tsai node = of_cpu_device_node_get(cpu);
94745373e3SChen-Yu Tsai
95745373e3SChen-Yu Tsai /* In case of_cpu_device_node_get fails */
96745373e3SChen-Yu Tsai if (!node)
97745373e3SChen-Yu Tsai node = of_get_cpu_node(cpu, NULL);
98745373e3SChen-Yu Tsai
99745373e3SChen-Yu Tsai if (!node) {
100745373e3SChen-Yu Tsai /*
101745373e3SChen-Yu Tsai * There's no point in returning an error, since we
102745373e3SChen-Yu Tsai * would be mid way in a core or cluster power sequence.
103745373e3SChen-Yu Tsai */
104745373e3SChen-Yu Tsai pr_err("%s: Couldn't get CPU cluster %u core %u device node\n",
105745373e3SChen-Yu Tsai __func__, cluster, core);
106745373e3SChen-Yu Tsai
107745373e3SChen-Yu Tsai return false;
108745373e3SChen-Yu Tsai }
109745373e3SChen-Yu Tsai
110995c770bSWen Yang is_compatible = of_device_is_compatible(node, "arm,cortex-a15");
111995c770bSWen Yang of_node_put(node);
112995c770bSWen Yang return is_compatible;
113745373e3SChen-Yu Tsai }
114745373e3SChen-Yu Tsai
sunxi_cpu_power_switch_set(unsigned int cpu,unsigned int cluster,bool enable)115745373e3SChen-Yu Tsai static int sunxi_cpu_power_switch_set(unsigned int cpu, unsigned int cluster,
116745373e3SChen-Yu Tsai bool enable)
117745373e3SChen-Yu Tsai {
118745373e3SChen-Yu Tsai u32 reg;
119745373e3SChen-Yu Tsai
120745373e3SChen-Yu Tsai /* control sequence from Allwinner A80 user manual v1.2 PRCM section */
121745373e3SChen-Yu Tsai reg = readl(prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
122745373e3SChen-Yu Tsai if (enable) {
123745373e3SChen-Yu Tsai if (reg == 0x00) {
124745373e3SChen-Yu Tsai pr_debug("power clamp for cluster %u cpu %u already open\n",
125745373e3SChen-Yu Tsai cluster, cpu);
126745373e3SChen-Yu Tsai return 0;
127745373e3SChen-Yu Tsai }
128745373e3SChen-Yu Tsai
129745373e3SChen-Yu Tsai writel(0xff, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
130745373e3SChen-Yu Tsai udelay(10);
131745373e3SChen-Yu Tsai writel(0xfe, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
132745373e3SChen-Yu Tsai udelay(10);
133745373e3SChen-Yu Tsai writel(0xf8, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
134745373e3SChen-Yu Tsai udelay(10);
135745373e3SChen-Yu Tsai writel(0xf0, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
136745373e3SChen-Yu Tsai udelay(10);
137745373e3SChen-Yu Tsai writel(0x00, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
138745373e3SChen-Yu Tsai udelay(10);
139745373e3SChen-Yu Tsai } else {
140745373e3SChen-Yu Tsai writel(0xff, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
141745373e3SChen-Yu Tsai udelay(10);
142745373e3SChen-Yu Tsai }
143745373e3SChen-Yu Tsai
144745373e3SChen-Yu Tsai return 0;
145745373e3SChen-Yu Tsai }
146745373e3SChen-Yu Tsai
sunxi_cpu0_hotplug_support_set(bool enable)1478eaa0648SChen-Yu Tsai static void sunxi_cpu0_hotplug_support_set(bool enable)
1488eaa0648SChen-Yu Tsai {
1498eaa0648SChen-Yu Tsai if (enable) {
1508eaa0648SChen-Yu Tsai writel(CPU0_SUPPORT_HOTPLUG_MAGIC0, sram_b_smp_base);
1518eaa0648SChen-Yu Tsai writel(CPU0_SUPPORT_HOTPLUG_MAGIC1, sram_b_smp_base + 0x4);
1528eaa0648SChen-Yu Tsai } else {
1538eaa0648SChen-Yu Tsai writel(0x0, sram_b_smp_base);
1548eaa0648SChen-Yu Tsai writel(0x0, sram_b_smp_base + 0x4);
1558eaa0648SChen-Yu Tsai }
1568eaa0648SChen-Yu Tsai }
1578eaa0648SChen-Yu Tsai
sunxi_cpu_powerup(unsigned int cpu,unsigned int cluster)158745373e3SChen-Yu Tsai static int sunxi_cpu_powerup(unsigned int cpu, unsigned int cluster)
159745373e3SChen-Yu Tsai {
160745373e3SChen-Yu Tsai u32 reg;
161745373e3SChen-Yu Tsai
1627cbea632SChen-Yu Tsai pr_debug("%s: cluster %u cpu %u\n", __func__, cluster, cpu);
163745373e3SChen-Yu Tsai if (cpu >= SUNXI_CPUS_PER_CLUSTER || cluster >= SUNXI_NR_CLUSTERS)
164745373e3SChen-Yu Tsai return -EINVAL;
165745373e3SChen-Yu Tsai
1668eaa0648SChen-Yu Tsai /* Set hotplug support magic flags for cpu0 */
1678eaa0648SChen-Yu Tsai if (cluster == 0 && cpu == 0)
1688eaa0648SChen-Yu Tsai sunxi_cpu0_hotplug_support_set(true);
1698eaa0648SChen-Yu Tsai
170745373e3SChen-Yu Tsai /* assert processor power-on reset */
171745373e3SChen-Yu Tsai reg = readl(prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
172745373e3SChen-Yu Tsai reg &= ~PRCM_CPU_PO_RST_CTRL_CORE(cpu);
173745373e3SChen-Yu Tsai writel(reg, prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
174745373e3SChen-Yu Tsai
1756961275eSMylène Josserand if (is_a83t) {
1766961275eSMylène Josserand /* assert cpu power-on reset */
1776961275eSMylène Josserand reg = readl(r_cpucfg_base +
1786961275eSMylène Josserand R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
1796961275eSMylène Josserand reg &= ~(R_CPUCFG_CLUSTER_PO_RST_CTRL_CORE(cpu));
1806961275eSMylène Josserand writel(reg, r_cpucfg_base +
1816961275eSMylène Josserand R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
1826961275eSMylène Josserand udelay(10);
1836961275eSMylène Josserand }
1846961275eSMylène Josserand
185745373e3SChen-Yu Tsai /* Cortex-A7: hold L1 reset disable signal low */
186745373e3SChen-Yu Tsai if (!sunxi_core_is_cortex_a15(cpu, cluster)) {
187745373e3SChen-Yu Tsai reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
188745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE(cpu);
189745373e3SChen-Yu Tsai writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
190745373e3SChen-Yu Tsai }
191745373e3SChen-Yu Tsai
192745373e3SChen-Yu Tsai /* assert processor related resets */
193745373e3SChen-Yu Tsai reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
194745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_RST_CTRL_DBG_RST(cpu);
195745373e3SChen-Yu Tsai
196745373e3SChen-Yu Tsai /*
197745373e3SChen-Yu Tsai * Allwinner code also asserts resets for NEON on A15. According
198745373e3SChen-Yu Tsai * to ARM manuals, asserting power-on reset is sufficient.
199745373e3SChen-Yu Tsai */
200745373e3SChen-Yu Tsai if (!sunxi_core_is_cortex_a15(cpu, cluster))
201745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_RST_CTRL_ETM_RST(cpu);
202745373e3SChen-Yu Tsai
203745373e3SChen-Yu Tsai writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
204745373e3SChen-Yu Tsai
205745373e3SChen-Yu Tsai /* open power switch */
206745373e3SChen-Yu Tsai sunxi_cpu_power_switch_set(cpu, cluster, true);
207745373e3SChen-Yu Tsai
2086961275eSMylène Josserand /* Handle A83T bit swap */
2096961275eSMylène Josserand if (is_a83t) {
2106961275eSMylène Josserand if (cpu == 0)
2116961275eSMylène Josserand cpu = 4;
2126961275eSMylène Josserand }
2136961275eSMylène Josserand
214745373e3SChen-Yu Tsai /* clear processor power gate */
215745373e3SChen-Yu Tsai reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
216745373e3SChen-Yu Tsai reg &= ~PRCM_PWROFF_GATING_REG_CORE(cpu);
217745373e3SChen-Yu Tsai writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
218745373e3SChen-Yu Tsai udelay(20);
219745373e3SChen-Yu Tsai
2206961275eSMylène Josserand /* Handle A83T bit swap */
2216961275eSMylène Josserand if (is_a83t) {
2226961275eSMylène Josserand if (cpu == 4)
2236961275eSMylène Josserand cpu = 0;
2246961275eSMylène Josserand }
2256961275eSMylène Josserand
226745373e3SChen-Yu Tsai /* de-assert processor power-on reset */
227745373e3SChen-Yu Tsai reg = readl(prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
228745373e3SChen-Yu Tsai reg |= PRCM_CPU_PO_RST_CTRL_CORE(cpu);
229745373e3SChen-Yu Tsai writel(reg, prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
230745373e3SChen-Yu Tsai
2316961275eSMylène Josserand if (is_a83t) {
2326961275eSMylène Josserand reg = readl(r_cpucfg_base +
2336961275eSMylène Josserand R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
2346961275eSMylène Josserand reg |= R_CPUCFG_CLUSTER_PO_RST_CTRL_CORE(cpu);
2356961275eSMylène Josserand writel(reg, r_cpucfg_base +
2366961275eSMylène Josserand R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
2376961275eSMylène Josserand udelay(10);
2386961275eSMylène Josserand }
2396961275eSMylène Josserand
240745373e3SChen-Yu Tsai /* de-assert all processor resets */
241745373e3SChen-Yu Tsai reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
242745373e3SChen-Yu Tsai reg |= CPUCFG_CX_RST_CTRL_DBG_RST(cpu);
243745373e3SChen-Yu Tsai reg |= CPUCFG_CX_RST_CTRL_CORE_RST(cpu);
244745373e3SChen-Yu Tsai if (!sunxi_core_is_cortex_a15(cpu, cluster))
245745373e3SChen-Yu Tsai reg |= CPUCFG_CX_RST_CTRL_ETM_RST(cpu);
246745373e3SChen-Yu Tsai else
247745373e3SChen-Yu Tsai reg |= CPUCFG_CX_RST_CTRL_CX_RST(cpu); /* NEON */
248745373e3SChen-Yu Tsai writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
249745373e3SChen-Yu Tsai
250745373e3SChen-Yu Tsai return 0;
251745373e3SChen-Yu Tsai }
252745373e3SChen-Yu Tsai
sunxi_cluster_powerup(unsigned int cluster)253745373e3SChen-Yu Tsai static int sunxi_cluster_powerup(unsigned int cluster)
254745373e3SChen-Yu Tsai {
255745373e3SChen-Yu Tsai u32 reg;
256745373e3SChen-Yu Tsai
257745373e3SChen-Yu Tsai pr_debug("%s: cluster %u\n", __func__, cluster);
258745373e3SChen-Yu Tsai if (cluster >= SUNXI_NR_CLUSTERS)
259745373e3SChen-Yu Tsai return -EINVAL;
260745373e3SChen-Yu Tsai
2616961275eSMylène Josserand /* For A83T, assert cluster cores resets */
2626961275eSMylène Josserand if (is_a83t) {
2636961275eSMylène Josserand reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
2646961275eSMylène Josserand reg &= ~CPUCFG_CX_RST_CTRL_CORE_RST_ALL; /* Core Reset */
2656961275eSMylène Josserand writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
2666961275eSMylène Josserand udelay(10);
2676961275eSMylène Josserand }
2686961275eSMylène Josserand
269745373e3SChen-Yu Tsai /* assert ACINACTM */
270745373e3SChen-Yu Tsai reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
271745373e3SChen-Yu Tsai reg |= CPUCFG_CX_CTRL_REG1_ACINACTM;
272745373e3SChen-Yu Tsai writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
273745373e3SChen-Yu Tsai
274745373e3SChen-Yu Tsai /* assert cluster processor power-on resets */
275745373e3SChen-Yu Tsai reg = readl(prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
276745373e3SChen-Yu Tsai reg &= ~PRCM_CPU_PO_RST_CTRL_CORE_ALL;
277745373e3SChen-Yu Tsai writel(reg, prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
278745373e3SChen-Yu Tsai
2796961275eSMylène Josserand /* assert cluster cores resets */
2806961275eSMylène Josserand if (is_a83t) {
2816961275eSMylène Josserand reg = readl(r_cpucfg_base +
2826961275eSMylène Josserand R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
2836961275eSMylène Josserand reg &= ~CPUCFG_CX_RST_CTRL_CORE_RST_ALL;
2846961275eSMylène Josserand writel(reg, r_cpucfg_base +
2856961275eSMylène Josserand R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
2866961275eSMylène Josserand udelay(10);
2876961275eSMylène Josserand }
2886961275eSMylène Josserand
289745373e3SChen-Yu Tsai /* assert cluster resets */
290745373e3SChen-Yu Tsai reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
291745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_RST_CTRL_DBG_SOC_RST;
292745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_RST_CTRL_DBG_RST_ALL;
293745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_RST_CTRL_H_RST;
294745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_RST_CTRL_L2_RST;
295745373e3SChen-Yu Tsai
296745373e3SChen-Yu Tsai /*
297745373e3SChen-Yu Tsai * Allwinner code also asserts resets for NEON on A15. According
298745373e3SChen-Yu Tsai * to ARM manuals, asserting power-on reset is sufficient.
299745373e3SChen-Yu Tsai */
300745373e3SChen-Yu Tsai if (!sunxi_core_is_cortex_a15(0, cluster))
301745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_RST_CTRL_ETM_RST_ALL;
302745373e3SChen-Yu Tsai
303745373e3SChen-Yu Tsai writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
304745373e3SChen-Yu Tsai
305745373e3SChen-Yu Tsai /* hold L1/L2 reset disable signals low */
306745373e3SChen-Yu Tsai reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
307745373e3SChen-Yu Tsai if (sunxi_core_is_cortex_a15(0, cluster)) {
308745373e3SChen-Yu Tsai /* Cortex-A15: hold L2RSTDISABLE low */
309745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A15;
310745373e3SChen-Yu Tsai } else {
311745373e3SChen-Yu Tsai /* Cortex-A7: hold L1RSTDISABLE and L2RSTDISABLE low */
312745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE_ALL;
313745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A7;
314745373e3SChen-Yu Tsai }
315745373e3SChen-Yu Tsai writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
316745373e3SChen-Yu Tsai
317745373e3SChen-Yu Tsai /* clear cluster power gate */
318745373e3SChen-Yu Tsai reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
3196961275eSMylène Josserand if (is_a83t)
3206961275eSMylène Josserand reg &= ~PRCM_PWROFF_GATING_REG_CLUSTER_SUN8I;
3216961275eSMylène Josserand else
322c64738e5SMylène Josserand reg &= ~PRCM_PWROFF_GATING_REG_CLUSTER_SUN9I;
323745373e3SChen-Yu Tsai writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
324745373e3SChen-Yu Tsai udelay(20);
325745373e3SChen-Yu Tsai
326745373e3SChen-Yu Tsai /* de-assert cluster resets */
327745373e3SChen-Yu Tsai reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
328745373e3SChen-Yu Tsai reg |= CPUCFG_CX_RST_CTRL_DBG_SOC_RST;
329745373e3SChen-Yu Tsai reg |= CPUCFG_CX_RST_CTRL_H_RST;
330745373e3SChen-Yu Tsai reg |= CPUCFG_CX_RST_CTRL_L2_RST;
331745373e3SChen-Yu Tsai writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
332745373e3SChen-Yu Tsai
333745373e3SChen-Yu Tsai /* de-assert ACINACTM */
334745373e3SChen-Yu Tsai reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
335745373e3SChen-Yu Tsai reg &= ~CPUCFG_CX_CTRL_REG1_ACINACTM;
336745373e3SChen-Yu Tsai writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
337745373e3SChen-Yu Tsai
338745373e3SChen-Yu Tsai return 0;
339745373e3SChen-Yu Tsai }
340745373e3SChen-Yu Tsai
341745373e3SChen-Yu Tsai /*
342745373e3SChen-Yu Tsai * This bit is shared between the initial nocache_trampoline call to
343745373e3SChen-Yu Tsai * enable CCI-400 and proper cluster cache disable before power down.
344745373e3SChen-Yu Tsai */
sunxi_cluster_cache_disable_without_axi(void)345745373e3SChen-Yu Tsai static void sunxi_cluster_cache_disable_without_axi(void)
346745373e3SChen-Yu Tsai {
347745373e3SChen-Yu Tsai if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A15) {
348745373e3SChen-Yu Tsai /*
349745373e3SChen-Yu Tsai * On the Cortex-A15 we need to disable
350745373e3SChen-Yu Tsai * L2 prefetching before flushing the cache.
351745373e3SChen-Yu Tsai */
352745373e3SChen-Yu Tsai asm volatile(
353745373e3SChen-Yu Tsai "mcr p15, 1, %0, c15, c0, 3\n"
354745373e3SChen-Yu Tsai "isb\n"
355745373e3SChen-Yu Tsai "dsb"
356745373e3SChen-Yu Tsai : : "r" (0x400));
357745373e3SChen-Yu Tsai }
358745373e3SChen-Yu Tsai
359745373e3SChen-Yu Tsai /* Flush all cache levels for this cluster. */
360745373e3SChen-Yu Tsai v7_exit_coherency_flush(all);
361745373e3SChen-Yu Tsai
362745373e3SChen-Yu Tsai /*
363745373e3SChen-Yu Tsai * Disable cluster-level coherency by masking
364745373e3SChen-Yu Tsai * incoming snoops and DVM messages:
365745373e3SChen-Yu Tsai */
366745373e3SChen-Yu Tsai cci_disable_port_by_cpu(read_cpuid_mpidr());
367745373e3SChen-Yu Tsai }
368745373e3SChen-Yu Tsai
369745373e3SChen-Yu Tsai static int sunxi_mc_smp_cpu_table[SUNXI_NR_CLUSTERS][SUNXI_CPUS_PER_CLUSTER];
370dff052ccSMylène Josserand int sunxi_mc_smp_first_comer;
371745373e3SChen-Yu Tsai
372745373e3SChen-Yu Tsai static DEFINE_SPINLOCK(boot_lock);
373745373e3SChen-Yu Tsai
sunxi_mc_smp_cluster_is_down(unsigned int cluster)374745373e3SChen-Yu Tsai static bool sunxi_mc_smp_cluster_is_down(unsigned int cluster)
375745373e3SChen-Yu Tsai {
376745373e3SChen-Yu Tsai int i;
377745373e3SChen-Yu Tsai
378745373e3SChen-Yu Tsai for (i = 0; i < SUNXI_CPUS_PER_CLUSTER; i++)
379745373e3SChen-Yu Tsai if (sunxi_mc_smp_cpu_table[cluster][i])
380745373e3SChen-Yu Tsai return false;
381745373e3SChen-Yu Tsai return true;
382745373e3SChen-Yu Tsai }
383745373e3SChen-Yu Tsai
sunxi_mc_smp_secondary_init(unsigned int cpu)3848eaa0648SChen-Yu Tsai static void sunxi_mc_smp_secondary_init(unsigned int cpu)
3858eaa0648SChen-Yu Tsai {
3868eaa0648SChen-Yu Tsai /* Clear hotplug support magic flags for cpu0 */
3878eaa0648SChen-Yu Tsai if (cpu == 0)
3888eaa0648SChen-Yu Tsai sunxi_cpu0_hotplug_support_set(false);
3898eaa0648SChen-Yu Tsai }
3908eaa0648SChen-Yu Tsai
sunxi_mc_smp_boot_secondary(unsigned int l_cpu,struct task_struct * idle)391745373e3SChen-Yu Tsai static int sunxi_mc_smp_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
392745373e3SChen-Yu Tsai {
393745373e3SChen-Yu Tsai unsigned int mpidr, cpu, cluster;
394745373e3SChen-Yu Tsai
395745373e3SChen-Yu Tsai mpidr = cpu_logical_map(l_cpu);
396745373e3SChen-Yu Tsai cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
397745373e3SChen-Yu Tsai cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
398745373e3SChen-Yu Tsai
399745373e3SChen-Yu Tsai if (!cpucfg_base)
400745373e3SChen-Yu Tsai return -ENODEV;
401745373e3SChen-Yu Tsai if (cluster >= SUNXI_NR_CLUSTERS || cpu >= SUNXI_CPUS_PER_CLUSTER)
402745373e3SChen-Yu Tsai return -EINVAL;
403745373e3SChen-Yu Tsai
404745373e3SChen-Yu Tsai spin_lock_irq(&boot_lock);
405745373e3SChen-Yu Tsai
406745373e3SChen-Yu Tsai if (sunxi_mc_smp_cpu_table[cluster][cpu])
407745373e3SChen-Yu Tsai goto out;
408745373e3SChen-Yu Tsai
409745373e3SChen-Yu Tsai if (sunxi_mc_smp_cluster_is_down(cluster)) {
410745373e3SChen-Yu Tsai sunxi_mc_smp_first_comer = true;
411745373e3SChen-Yu Tsai sunxi_cluster_powerup(cluster);
412745373e3SChen-Yu Tsai } else {
413745373e3SChen-Yu Tsai sunxi_mc_smp_first_comer = false;
414745373e3SChen-Yu Tsai }
415745373e3SChen-Yu Tsai
416745373e3SChen-Yu Tsai /* This is read by incoming CPUs with their cache and MMU disabled */
417745373e3SChen-Yu Tsai sync_cache_w(&sunxi_mc_smp_first_comer);
418745373e3SChen-Yu Tsai sunxi_cpu_powerup(cpu, cluster);
419745373e3SChen-Yu Tsai
420745373e3SChen-Yu Tsai out:
421745373e3SChen-Yu Tsai sunxi_mc_smp_cpu_table[cluster][cpu]++;
422745373e3SChen-Yu Tsai spin_unlock_irq(&boot_lock);
423745373e3SChen-Yu Tsai
424745373e3SChen-Yu Tsai return 0;
425745373e3SChen-Yu Tsai }
426745373e3SChen-Yu Tsai
4277cbea632SChen-Yu Tsai #ifdef CONFIG_HOTPLUG_CPU
sunxi_cluster_cache_disable(void)4287cbea632SChen-Yu Tsai static void sunxi_cluster_cache_disable(void)
4297cbea632SChen-Yu Tsai {
4307cbea632SChen-Yu Tsai unsigned int cluster = MPIDR_AFFINITY_LEVEL(read_cpuid_mpidr(), 1);
4317cbea632SChen-Yu Tsai u32 reg;
4327cbea632SChen-Yu Tsai
4337cbea632SChen-Yu Tsai pr_debug("%s: cluster %u\n", __func__, cluster);
4347cbea632SChen-Yu Tsai
4357cbea632SChen-Yu Tsai sunxi_cluster_cache_disable_without_axi();
4367cbea632SChen-Yu Tsai
4377cbea632SChen-Yu Tsai /* last man standing, assert ACINACTM */
4387cbea632SChen-Yu Tsai reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
4397cbea632SChen-Yu Tsai reg |= CPUCFG_CX_CTRL_REG1_ACINACTM;
4407cbea632SChen-Yu Tsai writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
4417cbea632SChen-Yu Tsai }
4427cbea632SChen-Yu Tsai
sunxi_mc_smp_cpu_die(unsigned int l_cpu)4437cbea632SChen-Yu Tsai static void sunxi_mc_smp_cpu_die(unsigned int l_cpu)
4447cbea632SChen-Yu Tsai {
4457cbea632SChen-Yu Tsai unsigned int mpidr, cpu, cluster;
4467cbea632SChen-Yu Tsai bool last_man;
4477cbea632SChen-Yu Tsai
4487cbea632SChen-Yu Tsai mpidr = cpu_logical_map(l_cpu);
4497cbea632SChen-Yu Tsai cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
4507cbea632SChen-Yu Tsai cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
4517cbea632SChen-Yu Tsai pr_debug("%s: cluster %u cpu %u\n", __func__, cluster, cpu);
4527cbea632SChen-Yu Tsai
4537cbea632SChen-Yu Tsai spin_lock(&boot_lock);
4547cbea632SChen-Yu Tsai sunxi_mc_smp_cpu_table[cluster][cpu]--;
4557cbea632SChen-Yu Tsai if (sunxi_mc_smp_cpu_table[cluster][cpu] == 1) {
4567cbea632SChen-Yu Tsai /* A power_up request went ahead of us. */
4577cbea632SChen-Yu Tsai pr_debug("%s: aborting due to a power up request\n",
4587cbea632SChen-Yu Tsai __func__);
4597cbea632SChen-Yu Tsai spin_unlock(&boot_lock);
4607cbea632SChen-Yu Tsai return;
4617cbea632SChen-Yu Tsai } else if (sunxi_mc_smp_cpu_table[cluster][cpu] > 1) {
4627cbea632SChen-Yu Tsai pr_err("Cluster %d CPU%d boots multiple times\n",
4637cbea632SChen-Yu Tsai cluster, cpu);
4647cbea632SChen-Yu Tsai BUG();
4657cbea632SChen-Yu Tsai }
4667cbea632SChen-Yu Tsai
4677cbea632SChen-Yu Tsai last_man = sunxi_mc_smp_cluster_is_down(cluster);
4687cbea632SChen-Yu Tsai spin_unlock(&boot_lock);
4697cbea632SChen-Yu Tsai
4707cbea632SChen-Yu Tsai gic_cpu_if_down(0);
4717cbea632SChen-Yu Tsai if (last_man)
4727cbea632SChen-Yu Tsai sunxi_cluster_cache_disable();
4737cbea632SChen-Yu Tsai else
4747cbea632SChen-Yu Tsai v7_exit_coherency_flush(louis);
4757cbea632SChen-Yu Tsai
4767cbea632SChen-Yu Tsai for (;;)
4777cbea632SChen-Yu Tsai wfi();
4787cbea632SChen-Yu Tsai }
4797cbea632SChen-Yu Tsai
sunxi_cpu_powerdown(unsigned int cpu,unsigned int cluster)4807cbea632SChen-Yu Tsai static int sunxi_cpu_powerdown(unsigned int cpu, unsigned int cluster)
4817cbea632SChen-Yu Tsai {
4827cbea632SChen-Yu Tsai u32 reg;
483e690053eSOndrej Jirman int gating_bit = cpu;
4847cbea632SChen-Yu Tsai
4857cbea632SChen-Yu Tsai pr_debug("%s: cluster %u cpu %u\n", __func__, cluster, cpu);
4867cbea632SChen-Yu Tsai if (cpu >= SUNXI_CPUS_PER_CLUSTER || cluster >= SUNXI_NR_CLUSTERS)
4877cbea632SChen-Yu Tsai return -EINVAL;
4887cbea632SChen-Yu Tsai
489e690053eSOndrej Jirman if (is_a83t && cpu == 0)
490e690053eSOndrej Jirman gating_bit = 4;
491e690053eSOndrej Jirman
4927cbea632SChen-Yu Tsai /* gate processor power */
4937cbea632SChen-Yu Tsai reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
494e690053eSOndrej Jirman reg |= PRCM_PWROFF_GATING_REG_CORE(gating_bit);
4957cbea632SChen-Yu Tsai writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
4967cbea632SChen-Yu Tsai udelay(20);
4977cbea632SChen-Yu Tsai
4987cbea632SChen-Yu Tsai /* close power switch */
4997cbea632SChen-Yu Tsai sunxi_cpu_power_switch_set(cpu, cluster, false);
5007cbea632SChen-Yu Tsai
5017cbea632SChen-Yu Tsai return 0;
5027cbea632SChen-Yu Tsai }
5037cbea632SChen-Yu Tsai
sunxi_cluster_powerdown(unsigned int cluster)5047cbea632SChen-Yu Tsai static int sunxi_cluster_powerdown(unsigned int cluster)
5057cbea632SChen-Yu Tsai {
5067cbea632SChen-Yu Tsai u32 reg;
5077cbea632SChen-Yu Tsai
5087cbea632SChen-Yu Tsai pr_debug("%s: cluster %u\n", __func__, cluster);
5097cbea632SChen-Yu Tsai if (cluster >= SUNXI_NR_CLUSTERS)
5107cbea632SChen-Yu Tsai return -EINVAL;
5117cbea632SChen-Yu Tsai
5127cbea632SChen-Yu Tsai /* assert cluster resets or system will hang */
5137cbea632SChen-Yu Tsai pr_debug("%s: assert cluster reset\n", __func__);
5147cbea632SChen-Yu Tsai reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
5157cbea632SChen-Yu Tsai reg &= ~CPUCFG_CX_RST_CTRL_DBG_SOC_RST;
5167cbea632SChen-Yu Tsai reg &= ~CPUCFG_CX_RST_CTRL_H_RST;
5177cbea632SChen-Yu Tsai reg &= ~CPUCFG_CX_RST_CTRL_L2_RST;
5187cbea632SChen-Yu Tsai writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
5197cbea632SChen-Yu Tsai
5207cbea632SChen-Yu Tsai /* gate cluster power */
5217cbea632SChen-Yu Tsai pr_debug("%s: gate cluster power\n", __func__);
5227cbea632SChen-Yu Tsai reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
5236961275eSMylène Josserand if (is_a83t)
5246961275eSMylène Josserand reg |= PRCM_PWROFF_GATING_REG_CLUSTER_SUN8I;
5256961275eSMylène Josserand else
526c64738e5SMylène Josserand reg |= PRCM_PWROFF_GATING_REG_CLUSTER_SUN9I;
5277cbea632SChen-Yu Tsai writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
5287cbea632SChen-Yu Tsai udelay(20);
5297cbea632SChen-Yu Tsai
5307cbea632SChen-Yu Tsai return 0;
5317cbea632SChen-Yu Tsai }
5327cbea632SChen-Yu Tsai
sunxi_mc_smp_cpu_kill(unsigned int l_cpu)5337cbea632SChen-Yu Tsai static int sunxi_mc_smp_cpu_kill(unsigned int l_cpu)
5347cbea632SChen-Yu Tsai {
5357cbea632SChen-Yu Tsai unsigned int mpidr, cpu, cluster;
5367cbea632SChen-Yu Tsai unsigned int tries, count;
5377cbea632SChen-Yu Tsai int ret = 0;
5387cbea632SChen-Yu Tsai u32 reg;
5397cbea632SChen-Yu Tsai
5407cbea632SChen-Yu Tsai mpidr = cpu_logical_map(l_cpu);
5417cbea632SChen-Yu Tsai cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
5427cbea632SChen-Yu Tsai cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
5437cbea632SChen-Yu Tsai
5447cbea632SChen-Yu Tsai /* This should never happen */
5457cbea632SChen-Yu Tsai if (WARN_ON(cluster >= SUNXI_NR_CLUSTERS ||
5467cbea632SChen-Yu Tsai cpu >= SUNXI_CPUS_PER_CLUSTER))
5477cbea632SChen-Yu Tsai return 0;
5487cbea632SChen-Yu Tsai
5497cbea632SChen-Yu Tsai /* wait for CPU core to die and enter WFI */
5507cbea632SChen-Yu Tsai count = TIMEOUT_USEC / POLL_USEC;
5517cbea632SChen-Yu Tsai spin_lock_irq(&boot_lock);
5527cbea632SChen-Yu Tsai for (tries = 0; tries < count; tries++) {
5537cbea632SChen-Yu Tsai spin_unlock_irq(&boot_lock);
5547cbea632SChen-Yu Tsai usleep_range(POLL_USEC / 2, POLL_USEC);
5557cbea632SChen-Yu Tsai spin_lock_irq(&boot_lock);
5567cbea632SChen-Yu Tsai
5577cbea632SChen-Yu Tsai /*
5587cbea632SChen-Yu Tsai * If the user turns off a bunch of cores at the same
5597cbea632SChen-Yu Tsai * time, the kernel might call cpu_kill before some of
5607cbea632SChen-Yu Tsai * them are ready. This is because boot_lock serializes
5617cbea632SChen-Yu Tsai * both cpu_die and cpu_kill callbacks. Either one could
5627cbea632SChen-Yu Tsai * run first. We should wait for cpu_die to complete.
5637cbea632SChen-Yu Tsai */
5647cbea632SChen-Yu Tsai if (sunxi_mc_smp_cpu_table[cluster][cpu])
5657cbea632SChen-Yu Tsai continue;
5667cbea632SChen-Yu Tsai
5677cbea632SChen-Yu Tsai reg = readl(cpucfg_base + CPUCFG_CX_STATUS(cluster));
5687cbea632SChen-Yu Tsai if (reg & CPUCFG_CX_STATUS_STANDBYWFI(cpu))
5697cbea632SChen-Yu Tsai break;
5707cbea632SChen-Yu Tsai }
5717cbea632SChen-Yu Tsai
5727cbea632SChen-Yu Tsai if (tries >= count) {
5737cbea632SChen-Yu Tsai ret = ETIMEDOUT;
5747cbea632SChen-Yu Tsai goto out;
5757cbea632SChen-Yu Tsai }
5767cbea632SChen-Yu Tsai
5777cbea632SChen-Yu Tsai /* power down CPU core */
5787cbea632SChen-Yu Tsai sunxi_cpu_powerdown(cpu, cluster);
5797cbea632SChen-Yu Tsai
5807cbea632SChen-Yu Tsai if (!sunxi_mc_smp_cluster_is_down(cluster))
5817cbea632SChen-Yu Tsai goto out;
5827cbea632SChen-Yu Tsai
5837cbea632SChen-Yu Tsai /* wait for cluster L2 WFI */
5847cbea632SChen-Yu Tsai ret = readl_poll_timeout(cpucfg_base + CPUCFG_CX_STATUS(cluster), reg,
5857cbea632SChen-Yu Tsai reg & CPUCFG_CX_STATUS_STANDBYWFIL2,
5867cbea632SChen-Yu Tsai POLL_USEC, TIMEOUT_USEC);
5877cbea632SChen-Yu Tsai if (ret) {
5887cbea632SChen-Yu Tsai /*
5897cbea632SChen-Yu Tsai * Ignore timeout on the cluster. Leaving the cluster on
5907cbea632SChen-Yu Tsai * will not affect system execution, just use a bit more
5917cbea632SChen-Yu Tsai * power. But returning an error here will only confuse
5927cbea632SChen-Yu Tsai * the user as the CPU has already been shutdown.
5937cbea632SChen-Yu Tsai */
5947cbea632SChen-Yu Tsai ret = 0;
5957cbea632SChen-Yu Tsai goto out;
5967cbea632SChen-Yu Tsai }
5977cbea632SChen-Yu Tsai
5987cbea632SChen-Yu Tsai /* Power down cluster */
5997cbea632SChen-Yu Tsai sunxi_cluster_powerdown(cluster);
6007cbea632SChen-Yu Tsai
6017cbea632SChen-Yu Tsai out:
6027cbea632SChen-Yu Tsai spin_unlock_irq(&boot_lock);
6037cbea632SChen-Yu Tsai pr_debug("%s: cluster %u cpu %u powerdown: %d\n",
6047cbea632SChen-Yu Tsai __func__, cluster, cpu, ret);
6057cbea632SChen-Yu Tsai return !ret;
6067cbea632SChen-Yu Tsai }
6077cbea632SChen-Yu Tsai
sunxi_mc_smp_cpu_can_disable(unsigned int cpu)6086961275eSMylène Josserand static bool sunxi_mc_smp_cpu_can_disable(unsigned int cpu)
6098eaa0648SChen-Yu Tsai {
6106961275eSMylène Josserand /* CPU0 hotplug not handled for sun8i-a83t */
6116961275eSMylène Josserand if (is_a83t)
6126961275eSMylène Josserand if (cpu == 0)
6136961275eSMylène Josserand return false;
6148eaa0648SChen-Yu Tsai return true;
6158eaa0648SChen-Yu Tsai }
6167cbea632SChen-Yu Tsai #endif
6177cbea632SChen-Yu Tsai
618745373e3SChen-Yu Tsai static const struct smp_operations sunxi_mc_smp_smp_ops __initconst = {
6198eaa0648SChen-Yu Tsai .smp_secondary_init = sunxi_mc_smp_secondary_init,
620745373e3SChen-Yu Tsai .smp_boot_secondary = sunxi_mc_smp_boot_secondary,
6217cbea632SChen-Yu Tsai #ifdef CONFIG_HOTPLUG_CPU
6227cbea632SChen-Yu Tsai .cpu_die = sunxi_mc_smp_cpu_die,
6237cbea632SChen-Yu Tsai .cpu_kill = sunxi_mc_smp_cpu_kill,
6248eaa0648SChen-Yu Tsai .cpu_can_disable = sunxi_mc_smp_cpu_can_disable,
6257cbea632SChen-Yu Tsai #endif
626745373e3SChen-Yu Tsai };
627745373e3SChen-Yu Tsai
sunxi_mc_smp_cpu_table_init(void)628745373e3SChen-Yu Tsai static bool __init sunxi_mc_smp_cpu_table_init(void)
629745373e3SChen-Yu Tsai {
630745373e3SChen-Yu Tsai unsigned int mpidr, cpu, cluster;
631745373e3SChen-Yu Tsai
632745373e3SChen-Yu Tsai mpidr = read_cpuid_mpidr();
633745373e3SChen-Yu Tsai cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
634745373e3SChen-Yu Tsai cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
635745373e3SChen-Yu Tsai
636745373e3SChen-Yu Tsai if (cluster >= SUNXI_NR_CLUSTERS || cpu >= SUNXI_CPUS_PER_CLUSTER) {
637745373e3SChen-Yu Tsai pr_err("%s: boot CPU is out of bounds!\n", __func__);
638745373e3SChen-Yu Tsai return false;
639745373e3SChen-Yu Tsai }
640745373e3SChen-Yu Tsai sunxi_mc_smp_cpu_table[cluster][cpu] = 1;
641745373e3SChen-Yu Tsai return true;
642745373e3SChen-Yu Tsai }
643745373e3SChen-Yu Tsai
644745373e3SChen-Yu Tsai /*
645745373e3SChen-Yu Tsai * Adapted from arch/arm/common/mc_smp_entry.c
646745373e3SChen-Yu Tsai *
647745373e3SChen-Yu Tsai * We need the trampoline code to enable CCI-400 on the first cluster
648745373e3SChen-Yu Tsai */
649745373e3SChen-Yu Tsai typedef typeof(cpu_reset) phys_reset_t;
650745373e3SChen-Yu Tsai
nocache_trampoline(unsigned long __unused)651745373e3SChen-Yu Tsai static int __init nocache_trampoline(unsigned long __unused)
652745373e3SChen-Yu Tsai {
653745373e3SChen-Yu Tsai phys_reset_t phys_reset;
654745373e3SChen-Yu Tsai
655745373e3SChen-Yu Tsai setup_mm_for_reboot();
656745373e3SChen-Yu Tsai sunxi_cluster_cache_disable_without_axi();
657745373e3SChen-Yu Tsai
658745373e3SChen-Yu Tsai phys_reset = (phys_reset_t)(unsigned long)__pa_symbol(cpu_reset);
659745373e3SChen-Yu Tsai phys_reset(__pa_symbol(sunxi_mc_smp_resume), false);
660745373e3SChen-Yu Tsai BUG();
661745373e3SChen-Yu Tsai }
662745373e3SChen-Yu Tsai
sunxi_mc_smp_loopback(void)663c161daeaSChen-Yu Tsai static int __init sunxi_mc_smp_loopback(void)
664745373e3SChen-Yu Tsai {
665745373e3SChen-Yu Tsai int ret;
666745373e3SChen-Yu Tsai
667745373e3SChen-Yu Tsai /*
668745373e3SChen-Yu Tsai * We're going to soft-restart the current CPU through the
669745373e3SChen-Yu Tsai * low-level MCPM code by leveraging the suspend/resume
670745373e3SChen-Yu Tsai * infrastructure. Let's play it safe by using cpu_pm_enter()
671745373e3SChen-Yu Tsai * in case the CPU init code path resets the VFP or similar.
672745373e3SChen-Yu Tsai */
673745373e3SChen-Yu Tsai sunxi_mc_smp_first_comer = true;
674745373e3SChen-Yu Tsai local_irq_disable();
675745373e3SChen-Yu Tsai local_fiq_disable();
676745373e3SChen-Yu Tsai ret = cpu_pm_enter();
677745373e3SChen-Yu Tsai if (!ret) {
678745373e3SChen-Yu Tsai ret = cpu_suspend(0, nocache_trampoline);
679745373e3SChen-Yu Tsai cpu_pm_exit();
680745373e3SChen-Yu Tsai }
681745373e3SChen-Yu Tsai local_fiq_enable();
682745373e3SChen-Yu Tsai local_irq_enable();
683745373e3SChen-Yu Tsai sunxi_mc_smp_first_comer = false;
684745373e3SChen-Yu Tsai
685745373e3SChen-Yu Tsai return ret;
686745373e3SChen-Yu Tsai }
687745373e3SChen-Yu Tsai
68899e3a1e6SChen-Yu Tsai /*
68999e3a1e6SChen-Yu Tsai * This holds any device nodes that we requested resources for,
69099e3a1e6SChen-Yu Tsai * so that we may easily release resources in the error path.
69199e3a1e6SChen-Yu Tsai */
69299e3a1e6SChen-Yu Tsai struct sunxi_mc_smp_nodes {
69399e3a1e6SChen-Yu Tsai struct device_node *prcm_node;
69499e3a1e6SChen-Yu Tsai struct device_node *cpucfg_node;
69599e3a1e6SChen-Yu Tsai struct device_node *sram_node;
6966961275eSMylène Josserand struct device_node *r_cpucfg_node;
69799e3a1e6SChen-Yu Tsai };
69899e3a1e6SChen-Yu Tsai
69999e3a1e6SChen-Yu Tsai /* This structure holds SoC-specific bits tied to an enable-method string. */
70099e3a1e6SChen-Yu Tsai struct sunxi_mc_smp_data {
70199e3a1e6SChen-Yu Tsai const char *enable_method;
70299e3a1e6SChen-Yu Tsai int (*get_smp_nodes)(struct sunxi_mc_smp_nodes *nodes);
7031631090eSMylène Josserand bool is_a83t;
70499e3a1e6SChen-Yu Tsai };
70599e3a1e6SChen-Yu Tsai
sunxi_mc_smp_put_nodes(struct sunxi_mc_smp_nodes * nodes)70699e3a1e6SChen-Yu Tsai static void __init sunxi_mc_smp_put_nodes(struct sunxi_mc_smp_nodes *nodes)
70799e3a1e6SChen-Yu Tsai {
70899e3a1e6SChen-Yu Tsai of_node_put(nodes->prcm_node);
70999e3a1e6SChen-Yu Tsai of_node_put(nodes->cpucfg_node);
71099e3a1e6SChen-Yu Tsai of_node_put(nodes->sram_node);
7116961275eSMylène Josserand of_node_put(nodes->r_cpucfg_node);
71299e3a1e6SChen-Yu Tsai memset(nodes, 0, sizeof(*nodes));
71399e3a1e6SChen-Yu Tsai }
71499e3a1e6SChen-Yu Tsai
sun9i_a80_get_smp_nodes(struct sunxi_mc_smp_nodes * nodes)71599e3a1e6SChen-Yu Tsai static int __init sun9i_a80_get_smp_nodes(struct sunxi_mc_smp_nodes *nodes)
71699e3a1e6SChen-Yu Tsai {
71799e3a1e6SChen-Yu Tsai nodes->prcm_node = of_find_compatible_node(NULL, NULL,
71899e3a1e6SChen-Yu Tsai "allwinner,sun9i-a80-prcm");
71999e3a1e6SChen-Yu Tsai if (!nodes->prcm_node) {
72099e3a1e6SChen-Yu Tsai pr_err("%s: PRCM not available\n", __func__);
72199e3a1e6SChen-Yu Tsai return -ENODEV;
72299e3a1e6SChen-Yu Tsai }
72399e3a1e6SChen-Yu Tsai
72499e3a1e6SChen-Yu Tsai nodes->cpucfg_node = of_find_compatible_node(NULL, NULL,
72599e3a1e6SChen-Yu Tsai "allwinner,sun9i-a80-cpucfg");
72699e3a1e6SChen-Yu Tsai if (!nodes->cpucfg_node) {
72799e3a1e6SChen-Yu Tsai pr_err("%s: CPUCFG not available\n", __func__);
72899e3a1e6SChen-Yu Tsai return -ENODEV;
72999e3a1e6SChen-Yu Tsai }
73099e3a1e6SChen-Yu Tsai
73199e3a1e6SChen-Yu Tsai nodes->sram_node = of_find_compatible_node(NULL, NULL,
73299e3a1e6SChen-Yu Tsai "allwinner,sun9i-a80-smp-sram");
73399e3a1e6SChen-Yu Tsai if (!nodes->sram_node) {
73499e3a1e6SChen-Yu Tsai pr_err("%s: Secure SRAM not available\n", __func__);
73599e3a1e6SChen-Yu Tsai return -ENODEV;
73699e3a1e6SChen-Yu Tsai }
73799e3a1e6SChen-Yu Tsai
73899e3a1e6SChen-Yu Tsai return 0;
73999e3a1e6SChen-Yu Tsai }
74099e3a1e6SChen-Yu Tsai
sun8i_a83t_get_smp_nodes(struct sunxi_mc_smp_nodes * nodes)7416961275eSMylène Josserand static int __init sun8i_a83t_get_smp_nodes(struct sunxi_mc_smp_nodes *nodes)
7426961275eSMylène Josserand {
7436961275eSMylène Josserand nodes->prcm_node = of_find_compatible_node(NULL, NULL,
7446961275eSMylène Josserand "allwinner,sun8i-a83t-r-ccu");
7456961275eSMylène Josserand if (!nodes->prcm_node) {
7466961275eSMylène Josserand pr_err("%s: PRCM not available\n", __func__);
7476961275eSMylène Josserand return -ENODEV;
7486961275eSMylène Josserand }
7496961275eSMylène Josserand
7506961275eSMylène Josserand nodes->cpucfg_node = of_find_compatible_node(NULL, NULL,
7516961275eSMylène Josserand "allwinner,sun8i-a83t-cpucfg");
7526961275eSMylène Josserand if (!nodes->cpucfg_node) {
7536961275eSMylène Josserand pr_err("%s: CPUCFG not available\n", __func__);
7546961275eSMylène Josserand return -ENODEV;
7556961275eSMylène Josserand }
7566961275eSMylène Josserand
7576961275eSMylène Josserand nodes->r_cpucfg_node = of_find_compatible_node(NULL, NULL,
7586961275eSMylène Josserand "allwinner,sun8i-a83t-r-cpucfg");
7596961275eSMylène Josserand if (!nodes->r_cpucfg_node) {
7606961275eSMylène Josserand pr_err("%s: RCPUCFG not available\n", __func__);
7616961275eSMylène Josserand return -ENODEV;
7626961275eSMylène Josserand }
7636961275eSMylène Josserand
7646961275eSMylène Josserand return 0;
7656961275eSMylène Josserand }
7666961275eSMylène Josserand
76799e3a1e6SChen-Yu Tsai static const struct sunxi_mc_smp_data sunxi_mc_smp_data[] __initconst = {
76899e3a1e6SChen-Yu Tsai {
76999e3a1e6SChen-Yu Tsai .enable_method = "allwinner,sun9i-a80-smp",
77099e3a1e6SChen-Yu Tsai .get_smp_nodes = sun9i_a80_get_smp_nodes,
77199e3a1e6SChen-Yu Tsai },
7726961275eSMylène Josserand {
7736961275eSMylène Josserand .enable_method = "allwinner,sun8i-a83t-smp",
7746961275eSMylène Josserand .get_smp_nodes = sun8i_a83t_get_smp_nodes,
7756961275eSMylène Josserand .is_a83t = true,
7766961275eSMylène Josserand },
77799e3a1e6SChen-Yu Tsai };
77899e3a1e6SChen-Yu Tsai
sunxi_mc_smp_init(void)779745373e3SChen-Yu Tsai static int __init sunxi_mc_smp_init(void)
780745373e3SChen-Yu Tsai {
78199e3a1e6SChen-Yu Tsai struct sunxi_mc_smp_nodes nodes = { 0 };
78299e3a1e6SChen-Yu Tsai struct device_node *node;
783745373e3SChen-Yu Tsai struct resource res;
7846961275eSMylène Josserand void __iomem *addr;
78599e3a1e6SChen-Yu Tsai int i, ret;
786745373e3SChen-Yu Tsai
7878591743fSChen-Yu Tsai /*
7888591743fSChen-Yu Tsai * Don't bother checking the "cpus" node, as an enable-method
7898591743fSChen-Yu Tsai * property in that node is undocumented.
7908591743fSChen-Yu Tsai */
7918591743fSChen-Yu Tsai node = of_cpu_device_node_get(0);
7928591743fSChen-Yu Tsai if (!node)
7938591743fSChen-Yu Tsai return -ENODEV;
7948591743fSChen-Yu Tsai
7958591743fSChen-Yu Tsai /*
7968591743fSChen-Yu Tsai * We can't actually use the enable-method magic in the kernel.
7978591743fSChen-Yu Tsai * Our loopback / trampoline code uses the CPU suspend framework,
7988591743fSChen-Yu Tsai * which requires the identity mapping be available. It would not
7998591743fSChen-Yu Tsai * yet be available if we used the .init_cpus or .prepare_cpus
8008591743fSChen-Yu Tsai * callbacks in smp_operations, which we would use if we were to
8018591743fSChen-Yu Tsai * use CPU_METHOD_OF_DECLARE
8028591743fSChen-Yu Tsai */
80399e3a1e6SChen-Yu Tsai for (i = 0; i < ARRAY_SIZE(sunxi_mc_smp_data); i++) {
8048591743fSChen-Yu Tsai ret = of_property_match_string(node, "enable-method",
80599e3a1e6SChen-Yu Tsai sunxi_mc_smp_data[i].enable_method);
806*643fe70eSStefan Wahren if (ret >= 0)
80799e3a1e6SChen-Yu Tsai break;
80899e3a1e6SChen-Yu Tsai }
80999e3a1e6SChen-Yu Tsai
8108591743fSChen-Yu Tsai of_node_put(node);
811*643fe70eSStefan Wahren if (ret < 0)
812745373e3SChen-Yu Tsai return -ENODEV;
813745373e3SChen-Yu Tsai
81472ad3b77SStefan Wahren is_a83t = sunxi_mc_smp_data[i].is_a83t;
81572ad3b77SStefan Wahren
816745373e3SChen-Yu Tsai if (!sunxi_mc_smp_cpu_table_init())
817745373e3SChen-Yu Tsai return -EINVAL;
818745373e3SChen-Yu Tsai
819745373e3SChen-Yu Tsai if (!cci_probed()) {
820745373e3SChen-Yu Tsai pr_err("%s: CCI-400 not available\n", __func__);
821745373e3SChen-Yu Tsai return -ENODEV;
822745373e3SChen-Yu Tsai }
823745373e3SChen-Yu Tsai
82499e3a1e6SChen-Yu Tsai /* Get needed device tree nodes */
82599e3a1e6SChen-Yu Tsai ret = sunxi_mc_smp_data[i].get_smp_nodes(&nodes);
82699e3a1e6SChen-Yu Tsai if (ret)
82799e3a1e6SChen-Yu Tsai goto err_put_nodes;
828745373e3SChen-Yu Tsai
829745373e3SChen-Yu Tsai /*
830745373e3SChen-Yu Tsai * Unfortunately we can not request the I/O region for the PRCM.
831745373e3SChen-Yu Tsai * It is shared with the PRCM clock.
832745373e3SChen-Yu Tsai */
83399e3a1e6SChen-Yu Tsai prcm_base = of_iomap(nodes.prcm_node, 0);
834745373e3SChen-Yu Tsai if (!prcm_base) {
835745373e3SChen-Yu Tsai pr_err("%s: failed to map PRCM registers\n", __func__);
83699e3a1e6SChen-Yu Tsai ret = -ENOMEM;
83799e3a1e6SChen-Yu Tsai goto err_put_nodes;
838745373e3SChen-Yu Tsai }
839745373e3SChen-Yu Tsai
84099e3a1e6SChen-Yu Tsai cpucfg_base = of_io_request_and_map(nodes.cpucfg_node, 0,
84199e3a1e6SChen-Yu Tsai "sunxi-mc-smp");
842745373e3SChen-Yu Tsai if (IS_ERR(cpucfg_base)) {
843745373e3SChen-Yu Tsai ret = PTR_ERR(cpucfg_base);
844745373e3SChen-Yu Tsai pr_err("%s: failed to map CPUCFG registers: %d\n",
845745373e3SChen-Yu Tsai __func__, ret);
84699e3a1e6SChen-Yu Tsai goto err_unmap_prcm;
847745373e3SChen-Yu Tsai }
848745373e3SChen-Yu Tsai
8496961275eSMylène Josserand if (is_a83t) {
8506961275eSMylène Josserand r_cpucfg_base = of_io_request_and_map(nodes.r_cpucfg_node,
8516961275eSMylène Josserand 0, "sunxi-mc-smp");
8526961275eSMylène Josserand if (IS_ERR(r_cpucfg_base)) {
8536961275eSMylène Josserand ret = PTR_ERR(r_cpucfg_base);
8546961275eSMylène Josserand pr_err("%s: failed to map R-CPUCFG registers\n",
8556961275eSMylène Josserand __func__);
8566961275eSMylène Josserand goto err_unmap_release_cpucfg;
8576961275eSMylène Josserand }
8586961275eSMylène Josserand } else {
85999e3a1e6SChen-Yu Tsai sram_b_smp_base = of_io_request_and_map(nodes.sram_node, 0,
86099e3a1e6SChen-Yu Tsai "sunxi-mc-smp");
8618eaa0648SChen-Yu Tsai if (IS_ERR(sram_b_smp_base)) {
8628eaa0648SChen-Yu Tsai ret = PTR_ERR(sram_b_smp_base);
8638eaa0648SChen-Yu Tsai pr_err("%s: failed to map secure SRAM\n", __func__);
86499e3a1e6SChen-Yu Tsai goto err_unmap_release_cpucfg;
8658eaa0648SChen-Yu Tsai }
8666961275eSMylène Josserand }
8678eaa0648SChen-Yu Tsai
868745373e3SChen-Yu Tsai /* Configure CCI-400 for boot cluster */
869c161daeaSChen-Yu Tsai ret = sunxi_mc_smp_loopback();
870745373e3SChen-Yu Tsai if (ret) {
871745373e3SChen-Yu Tsai pr_err("%s: failed to configure boot cluster: %d\n",
872745373e3SChen-Yu Tsai __func__, ret);
8736961275eSMylène Josserand goto err_unmap_release_sram_rcpucfg;
874745373e3SChen-Yu Tsai }
875745373e3SChen-Yu Tsai
87699e3a1e6SChen-Yu Tsai /* We don't need the device nodes anymore */
87799e3a1e6SChen-Yu Tsai sunxi_mc_smp_put_nodes(&nodes);
878745373e3SChen-Yu Tsai
879745373e3SChen-Yu Tsai /* Set the hardware entry point address */
8806961275eSMylène Josserand if (is_a83t)
8816961275eSMylène Josserand addr = r_cpucfg_base + R_CPUCFG_CPU_SOFT_ENTRY_REG;
8826961275eSMylène Josserand else
8836961275eSMylène Josserand addr = prcm_base + PRCM_CPU_SOFT_ENTRY_REG;
8846961275eSMylène Josserand writel(__pa_symbol(sunxi_mc_smp_secondary_startup), addr);
885745373e3SChen-Yu Tsai
886745373e3SChen-Yu Tsai /* Actually enable multi cluster SMP */
887745373e3SChen-Yu Tsai smp_set_ops(&sunxi_mc_smp_smp_ops);
888745373e3SChen-Yu Tsai
889745373e3SChen-Yu Tsai pr_info("sunxi multi cluster SMP support installed\n");
890745373e3SChen-Yu Tsai
891745373e3SChen-Yu Tsai return 0;
892745373e3SChen-Yu Tsai
8936961275eSMylène Josserand err_unmap_release_sram_rcpucfg:
8946961275eSMylène Josserand if (is_a83t) {
8956961275eSMylène Josserand iounmap(r_cpucfg_base);
8966961275eSMylène Josserand of_address_to_resource(nodes.r_cpucfg_node, 0, &res);
8976961275eSMylène Josserand } else {
8988eaa0648SChen-Yu Tsai iounmap(sram_b_smp_base);
89999e3a1e6SChen-Yu Tsai of_address_to_resource(nodes.sram_node, 0, &res);
9006961275eSMylène Josserand }
9018eaa0648SChen-Yu Tsai release_mem_region(res.start, resource_size(&res));
902745373e3SChen-Yu Tsai err_unmap_release_cpucfg:
903745373e3SChen-Yu Tsai iounmap(cpucfg_base);
90499e3a1e6SChen-Yu Tsai of_address_to_resource(nodes.cpucfg_node, 0, &res);
905745373e3SChen-Yu Tsai release_mem_region(res.start, resource_size(&res));
906745373e3SChen-Yu Tsai err_unmap_prcm:
907745373e3SChen-Yu Tsai iounmap(prcm_base);
90899e3a1e6SChen-Yu Tsai err_put_nodes:
90999e3a1e6SChen-Yu Tsai sunxi_mc_smp_put_nodes(&nodes);
910745373e3SChen-Yu Tsai return ret;
911745373e3SChen-Yu Tsai }
912745373e3SChen-Yu Tsai
913745373e3SChen-Yu Tsai early_initcall(sunxi_mc_smp_init);
914