1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2013 Imagination Technologies
4*4882a593Smuzhiyun * Author: Paul Burton <paul.burton@mips.com>
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/cpu.h>
8*4882a593Smuzhiyun #include <linux/delay.h>
9*4882a593Smuzhiyun #include <linux/io.h>
10*4882a593Smuzhiyun #include <linux/sched/task_stack.h>
11*4882a593Smuzhiyun #include <linux/sched/hotplug.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/smp.h>
14*4882a593Smuzhiyun #include <linux/types.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <asm/bcache.h>
17*4882a593Smuzhiyun #include <asm/mips-cps.h>
18*4882a593Smuzhiyun #include <asm/mips_mt.h>
19*4882a593Smuzhiyun #include <asm/mipsregs.h>
20*4882a593Smuzhiyun #include <asm/pm-cps.h>
21*4882a593Smuzhiyun #include <asm/r4kcache.h>
22*4882a593Smuzhiyun #include <asm/smp-cps.h>
23*4882a593Smuzhiyun #include <asm/time.h>
24*4882a593Smuzhiyun #include <asm/uasm.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun static bool threads_disabled;
27*4882a593Smuzhiyun static DECLARE_BITMAP(core_power, NR_CPUS);
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun struct core_boot_config *mips_cps_core_bootcfg;
30*4882a593Smuzhiyun
setup_nothreads(char * s)31*4882a593Smuzhiyun static int __init setup_nothreads(char *s)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun threads_disabled = true;
34*4882a593Smuzhiyun return 0;
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun early_param("nothreads", setup_nothreads);
37*4882a593Smuzhiyun
core_vpe_count(unsigned int cluster,unsigned core)38*4882a593Smuzhiyun static unsigned core_vpe_count(unsigned int cluster, unsigned core)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun if (threads_disabled)
41*4882a593Smuzhiyun return 1;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun return mips_cps_numvps(cluster, core);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
cps_smp_setup(void)46*4882a593Smuzhiyun static void __init cps_smp_setup(void)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun unsigned int nclusters, ncores, nvpes, core_vpes;
49*4882a593Smuzhiyun unsigned long core_entry;
50*4882a593Smuzhiyun int cl, c, v;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /* Detect & record VPE topology */
53*4882a593Smuzhiyun nvpes = 0;
54*4882a593Smuzhiyun nclusters = mips_cps_numclusters();
55*4882a593Smuzhiyun pr_info("%s topology ", cpu_has_mips_r6 ? "VP" : "VPE");
56*4882a593Smuzhiyun for (cl = 0; cl < nclusters; cl++) {
57*4882a593Smuzhiyun if (cl > 0)
58*4882a593Smuzhiyun pr_cont(",");
59*4882a593Smuzhiyun pr_cont("{");
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun ncores = mips_cps_numcores(cl);
62*4882a593Smuzhiyun for (c = 0; c < ncores; c++) {
63*4882a593Smuzhiyun core_vpes = core_vpe_count(cl, c);
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun if (c > 0)
66*4882a593Smuzhiyun pr_cont(",");
67*4882a593Smuzhiyun pr_cont("%u", core_vpes);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* Use the number of VPEs in cluster 0 core 0 for smp_num_siblings */
70*4882a593Smuzhiyun if (!cl && !c)
71*4882a593Smuzhiyun smp_num_siblings = core_vpes;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun for (v = 0; v < min_t(int, core_vpes, NR_CPUS - nvpes); v++) {
74*4882a593Smuzhiyun cpu_set_cluster(&cpu_data[nvpes + v], cl);
75*4882a593Smuzhiyun cpu_set_core(&cpu_data[nvpes + v], c);
76*4882a593Smuzhiyun cpu_set_vpe_id(&cpu_data[nvpes + v], v);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun nvpes += core_vpes;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun pr_cont("}");
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun pr_cont(" total %u\n", nvpes);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /* Indicate present CPUs (CPU being synonymous with VPE) */
87*4882a593Smuzhiyun for (v = 0; v < min_t(unsigned, nvpes, NR_CPUS); v++) {
88*4882a593Smuzhiyun set_cpu_possible(v, cpu_cluster(&cpu_data[v]) == 0);
89*4882a593Smuzhiyun set_cpu_present(v, cpu_cluster(&cpu_data[v]) == 0);
90*4882a593Smuzhiyun __cpu_number_map[v] = v;
91*4882a593Smuzhiyun __cpu_logical_map[v] = v;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* Set a coherent default CCA (CWB) */
95*4882a593Smuzhiyun change_c0_config(CONF_CM_CMASK, 0x5);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* Core 0 is powered up (we're running on it) */
98*4882a593Smuzhiyun bitmap_set(core_power, 0, 1);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /* Initialise core 0 */
101*4882a593Smuzhiyun mips_cps_core_init();
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /* Make core 0 coherent with everything */
104*4882a593Smuzhiyun write_gcr_cl_coherence(0xff);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun if (mips_cm_revision() >= CM_REV_CM3) {
107*4882a593Smuzhiyun core_entry = CKSEG1ADDR((unsigned long)mips_cps_core_entry);
108*4882a593Smuzhiyun write_gcr_bev_base(core_entry);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun #ifdef CONFIG_MIPS_MT_FPAFF
112*4882a593Smuzhiyun /* If we have an FPU, enroll ourselves in the FPU-full mask */
113*4882a593Smuzhiyun if (cpu_has_fpu)
114*4882a593Smuzhiyun cpumask_set_cpu(0, &mt_fpu_cpumask);
115*4882a593Smuzhiyun #endif /* CONFIG_MIPS_MT_FPAFF */
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
cps_prepare_cpus(unsigned int max_cpus)118*4882a593Smuzhiyun static void __init cps_prepare_cpus(unsigned int max_cpus)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun unsigned ncores, core_vpes, c, cca;
121*4882a593Smuzhiyun bool cca_unsuitable, cores_limited;
122*4882a593Smuzhiyun u32 *entry_code;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun mips_mt_set_cpuoptions();
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /* Detect whether the CCA is unsuited to multi-core SMP */
127*4882a593Smuzhiyun cca = read_c0_config() & CONF_CM_CMASK;
128*4882a593Smuzhiyun switch (cca) {
129*4882a593Smuzhiyun case 0x4: /* CWBE */
130*4882a593Smuzhiyun case 0x5: /* CWB */
131*4882a593Smuzhiyun /* The CCA is coherent, multi-core is fine */
132*4882a593Smuzhiyun cca_unsuitable = false;
133*4882a593Smuzhiyun break;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun default:
136*4882a593Smuzhiyun /* CCA is not coherent, multi-core is not usable */
137*4882a593Smuzhiyun cca_unsuitable = true;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /* Warn the user if the CCA prevents multi-core */
141*4882a593Smuzhiyun cores_limited = false;
142*4882a593Smuzhiyun if (cca_unsuitable || cpu_has_dc_aliases) {
143*4882a593Smuzhiyun for_each_present_cpu(c) {
144*4882a593Smuzhiyun if (cpus_are_siblings(smp_processor_id(), c))
145*4882a593Smuzhiyun continue;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun set_cpu_present(c, false);
148*4882a593Smuzhiyun cores_limited = true;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun if (cores_limited)
152*4882a593Smuzhiyun pr_warn("Using only one core due to %s%s%s\n",
153*4882a593Smuzhiyun cca_unsuitable ? "unsuitable CCA" : "",
154*4882a593Smuzhiyun (cca_unsuitable && cpu_has_dc_aliases) ? " & " : "",
155*4882a593Smuzhiyun cpu_has_dc_aliases ? "dcache aliasing" : "");
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /*
158*4882a593Smuzhiyun * Patch the start of mips_cps_core_entry to provide:
159*4882a593Smuzhiyun *
160*4882a593Smuzhiyun * s0 = kseg0 CCA
161*4882a593Smuzhiyun */
162*4882a593Smuzhiyun entry_code = (u32 *)&mips_cps_core_entry;
163*4882a593Smuzhiyun uasm_i_addiu(&entry_code, 16, 0, cca);
164*4882a593Smuzhiyun blast_dcache_range((unsigned long)&mips_cps_core_entry,
165*4882a593Smuzhiyun (unsigned long)entry_code);
166*4882a593Smuzhiyun bc_wback_inv((unsigned long)&mips_cps_core_entry,
167*4882a593Smuzhiyun (void *)entry_code - (void *)&mips_cps_core_entry);
168*4882a593Smuzhiyun __sync();
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /* Allocate core boot configuration structs */
171*4882a593Smuzhiyun ncores = mips_cps_numcores(0);
172*4882a593Smuzhiyun mips_cps_core_bootcfg = kcalloc(ncores, sizeof(*mips_cps_core_bootcfg),
173*4882a593Smuzhiyun GFP_KERNEL);
174*4882a593Smuzhiyun if (!mips_cps_core_bootcfg) {
175*4882a593Smuzhiyun pr_err("Failed to allocate boot config for %u cores\n", ncores);
176*4882a593Smuzhiyun goto err_out;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /* Allocate VPE boot configuration structs */
180*4882a593Smuzhiyun for (c = 0; c < ncores; c++) {
181*4882a593Smuzhiyun core_vpes = core_vpe_count(0, c);
182*4882a593Smuzhiyun mips_cps_core_bootcfg[c].vpe_config = kcalloc(core_vpes,
183*4882a593Smuzhiyun sizeof(*mips_cps_core_bootcfg[c].vpe_config),
184*4882a593Smuzhiyun GFP_KERNEL);
185*4882a593Smuzhiyun if (!mips_cps_core_bootcfg[c].vpe_config) {
186*4882a593Smuzhiyun pr_err("Failed to allocate %u VPE boot configs\n",
187*4882a593Smuzhiyun core_vpes);
188*4882a593Smuzhiyun goto err_out;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /* Mark this CPU as booted */
193*4882a593Smuzhiyun atomic_set(&mips_cps_core_bootcfg[cpu_core(¤t_cpu_data)].vpe_mask,
194*4882a593Smuzhiyun 1 << cpu_vpe_id(¤t_cpu_data));
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun return;
197*4882a593Smuzhiyun err_out:
198*4882a593Smuzhiyun /* Clean up allocations */
199*4882a593Smuzhiyun if (mips_cps_core_bootcfg) {
200*4882a593Smuzhiyun for (c = 0; c < ncores; c++)
201*4882a593Smuzhiyun kfree(mips_cps_core_bootcfg[c].vpe_config);
202*4882a593Smuzhiyun kfree(mips_cps_core_bootcfg);
203*4882a593Smuzhiyun mips_cps_core_bootcfg = NULL;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* Effectively disable SMP by declaring CPUs not present */
207*4882a593Smuzhiyun for_each_possible_cpu(c) {
208*4882a593Smuzhiyun if (c == 0)
209*4882a593Smuzhiyun continue;
210*4882a593Smuzhiyun set_cpu_present(c, false);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
boot_core(unsigned int core,unsigned int vpe_id)214*4882a593Smuzhiyun static void boot_core(unsigned int core, unsigned int vpe_id)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun u32 stat, seq_state;
217*4882a593Smuzhiyun unsigned timeout;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /* Select the appropriate core */
220*4882a593Smuzhiyun mips_cm_lock_other(0, core, 0, CM_GCR_Cx_OTHER_BLOCK_LOCAL);
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* Set its reset vector */
223*4882a593Smuzhiyun write_gcr_co_reset_base(CKSEG1ADDR((unsigned long)mips_cps_core_entry));
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /* Ensure its coherency is disabled */
226*4882a593Smuzhiyun write_gcr_co_coherence(0);
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /* Start it with the legacy memory map and exception base */
229*4882a593Smuzhiyun write_gcr_co_reset_ext_base(CM_GCR_Cx_RESET_EXT_BASE_UEB);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /* Ensure the core can access the GCRs */
232*4882a593Smuzhiyun set_gcr_access(1 << core);
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun if (mips_cpc_present()) {
235*4882a593Smuzhiyun /* Reset the core */
236*4882a593Smuzhiyun mips_cpc_lock_other(core);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun if (mips_cm_revision() >= CM_REV_CM3) {
239*4882a593Smuzhiyun /* Run only the requested VP following the reset */
240*4882a593Smuzhiyun write_cpc_co_vp_stop(0xf);
241*4882a593Smuzhiyun write_cpc_co_vp_run(1 << vpe_id);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /*
244*4882a593Smuzhiyun * Ensure that the VP_RUN register is written before the
245*4882a593Smuzhiyun * core leaves reset.
246*4882a593Smuzhiyun */
247*4882a593Smuzhiyun wmb();
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun write_cpc_co_cmd(CPC_Cx_CMD_RESET);
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun timeout = 100;
253*4882a593Smuzhiyun while (true) {
254*4882a593Smuzhiyun stat = read_cpc_co_stat_conf();
255*4882a593Smuzhiyun seq_state = stat & CPC_Cx_STAT_CONF_SEQSTATE;
256*4882a593Smuzhiyun seq_state >>= __ffs(CPC_Cx_STAT_CONF_SEQSTATE);
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun /* U6 == coherent execution, ie. the core is up */
259*4882a593Smuzhiyun if (seq_state == CPC_Cx_STAT_CONF_SEQSTATE_U6)
260*4882a593Smuzhiyun break;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun /* Delay a little while before we start warning */
263*4882a593Smuzhiyun if (timeout) {
264*4882a593Smuzhiyun timeout--;
265*4882a593Smuzhiyun mdelay(10);
266*4882a593Smuzhiyun continue;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun pr_warn("Waiting for core %u to start... STAT_CONF=0x%x\n",
270*4882a593Smuzhiyun core, stat);
271*4882a593Smuzhiyun mdelay(1000);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun mips_cpc_unlock_other();
275*4882a593Smuzhiyun } else {
276*4882a593Smuzhiyun /* Take the core out of reset */
277*4882a593Smuzhiyun write_gcr_co_reset_release(0);
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun mips_cm_unlock_other();
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /* The core is now powered up */
283*4882a593Smuzhiyun bitmap_set(core_power, core, 1);
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
remote_vpe_boot(void * dummy)286*4882a593Smuzhiyun static void remote_vpe_boot(void *dummy)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun unsigned core = cpu_core(¤t_cpu_data);
289*4882a593Smuzhiyun struct core_boot_config *core_cfg = &mips_cps_core_bootcfg[core];
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun mips_cps_boot_vpes(core_cfg, cpu_vpe_id(¤t_cpu_data));
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
cps_boot_secondary(int cpu,struct task_struct * idle)294*4882a593Smuzhiyun static int cps_boot_secondary(int cpu, struct task_struct *idle)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun unsigned core = cpu_core(&cpu_data[cpu]);
297*4882a593Smuzhiyun unsigned vpe_id = cpu_vpe_id(&cpu_data[cpu]);
298*4882a593Smuzhiyun struct core_boot_config *core_cfg = &mips_cps_core_bootcfg[core];
299*4882a593Smuzhiyun struct vpe_boot_config *vpe_cfg = &core_cfg->vpe_config[vpe_id];
300*4882a593Smuzhiyun unsigned long core_entry;
301*4882a593Smuzhiyun unsigned int remote;
302*4882a593Smuzhiyun int err;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun /* We don't yet support booting CPUs in other clusters */
305*4882a593Smuzhiyun if (cpu_cluster(&cpu_data[cpu]) != cpu_cluster(&raw_current_cpu_data))
306*4882a593Smuzhiyun return -ENOSYS;
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun vpe_cfg->pc = (unsigned long)&smp_bootstrap;
309*4882a593Smuzhiyun vpe_cfg->sp = __KSTK_TOS(idle);
310*4882a593Smuzhiyun vpe_cfg->gp = (unsigned long)task_thread_info(idle);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun atomic_or(1 << cpu_vpe_id(&cpu_data[cpu]), &core_cfg->vpe_mask);
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun preempt_disable();
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun if (!test_bit(core, core_power)) {
317*4882a593Smuzhiyun /* Boot a VPE on a powered down core */
318*4882a593Smuzhiyun boot_core(core, vpe_id);
319*4882a593Smuzhiyun goto out;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun if (cpu_has_vp) {
323*4882a593Smuzhiyun mips_cm_lock_other(0, core, vpe_id, CM_GCR_Cx_OTHER_BLOCK_LOCAL);
324*4882a593Smuzhiyun core_entry = CKSEG1ADDR((unsigned long)mips_cps_core_entry);
325*4882a593Smuzhiyun write_gcr_co_reset_base(core_entry);
326*4882a593Smuzhiyun mips_cm_unlock_other();
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun if (!cpus_are_siblings(cpu, smp_processor_id())) {
330*4882a593Smuzhiyun /* Boot a VPE on another powered up core */
331*4882a593Smuzhiyun for (remote = 0; remote < NR_CPUS; remote++) {
332*4882a593Smuzhiyun if (!cpus_are_siblings(cpu, remote))
333*4882a593Smuzhiyun continue;
334*4882a593Smuzhiyun if (cpu_online(remote))
335*4882a593Smuzhiyun break;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun if (remote >= NR_CPUS) {
338*4882a593Smuzhiyun pr_crit("No online CPU in core %u to start CPU%d\n",
339*4882a593Smuzhiyun core, cpu);
340*4882a593Smuzhiyun goto out;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun err = smp_call_function_single(remote, remote_vpe_boot,
344*4882a593Smuzhiyun NULL, 1);
345*4882a593Smuzhiyun if (err)
346*4882a593Smuzhiyun panic("Failed to call remote CPU\n");
347*4882a593Smuzhiyun goto out;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun BUG_ON(!cpu_has_mipsmt && !cpu_has_vp);
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun /* Boot a VPE on this core */
353*4882a593Smuzhiyun mips_cps_boot_vpes(core_cfg, vpe_id);
354*4882a593Smuzhiyun out:
355*4882a593Smuzhiyun preempt_enable();
356*4882a593Smuzhiyun return 0;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
cps_init_secondary(void)359*4882a593Smuzhiyun static void cps_init_secondary(void)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun /* Disable MT - we only want to run 1 TC per VPE */
362*4882a593Smuzhiyun if (cpu_has_mipsmt)
363*4882a593Smuzhiyun dmt();
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun if (mips_cm_revision() >= CM_REV_CM3) {
366*4882a593Smuzhiyun unsigned int ident = read_gic_vl_ident();
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun /*
369*4882a593Smuzhiyun * Ensure that our calculation of the VP ID matches up with
370*4882a593Smuzhiyun * what the GIC reports, otherwise we'll have configured
371*4882a593Smuzhiyun * interrupts incorrectly.
372*4882a593Smuzhiyun */
373*4882a593Smuzhiyun BUG_ON(ident != mips_cm_vp_id(smp_processor_id()));
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun if (cpu_has_veic)
377*4882a593Smuzhiyun clear_c0_status(ST0_IM);
378*4882a593Smuzhiyun else
379*4882a593Smuzhiyun change_c0_status(ST0_IM, STATUSF_IP2 | STATUSF_IP3 |
380*4882a593Smuzhiyun STATUSF_IP4 | STATUSF_IP5 |
381*4882a593Smuzhiyun STATUSF_IP6 | STATUSF_IP7);
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun
cps_smp_finish(void)384*4882a593Smuzhiyun static void cps_smp_finish(void)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun write_c0_compare(read_c0_count() + (8 * mips_hpt_frequency / HZ));
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun #ifdef CONFIG_MIPS_MT_FPAFF
389*4882a593Smuzhiyun /* If we have an FPU, enroll ourselves in the FPU-full mask */
390*4882a593Smuzhiyun if (cpu_has_fpu)
391*4882a593Smuzhiyun cpumask_set_cpu(smp_processor_id(), &mt_fpu_cpumask);
392*4882a593Smuzhiyun #endif /* CONFIG_MIPS_MT_FPAFF */
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun local_irq_enable();
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_KEXEC)
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun enum cpu_death {
400*4882a593Smuzhiyun CPU_DEATH_HALT,
401*4882a593Smuzhiyun CPU_DEATH_POWER,
402*4882a593Smuzhiyun };
403*4882a593Smuzhiyun
cps_shutdown_this_cpu(enum cpu_death death)404*4882a593Smuzhiyun static void cps_shutdown_this_cpu(enum cpu_death death)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun unsigned int cpu, core, vpe_id;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun cpu = smp_processor_id();
409*4882a593Smuzhiyun core = cpu_core(&cpu_data[cpu]);
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun if (death == CPU_DEATH_HALT) {
412*4882a593Smuzhiyun vpe_id = cpu_vpe_id(&cpu_data[cpu]);
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun pr_debug("Halting core %d VP%d\n", core, vpe_id);
415*4882a593Smuzhiyun if (cpu_has_mipsmt) {
416*4882a593Smuzhiyun /* Halt this TC */
417*4882a593Smuzhiyun write_c0_tchalt(TCHALT_H);
418*4882a593Smuzhiyun instruction_hazard();
419*4882a593Smuzhiyun } else if (cpu_has_vp) {
420*4882a593Smuzhiyun write_cpc_cl_vp_stop(1 << vpe_id);
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun /* Ensure that the VP_STOP register is written */
423*4882a593Smuzhiyun wmb();
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun } else {
426*4882a593Smuzhiyun pr_debug("Gating power to core %d\n", core);
427*4882a593Smuzhiyun /* Power down the core */
428*4882a593Smuzhiyun cps_pm_enter_state(CPS_PM_POWER_GATED);
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun #ifdef CONFIG_KEXEC
433*4882a593Smuzhiyun
cps_kexec_nonboot_cpu(void)434*4882a593Smuzhiyun static void cps_kexec_nonboot_cpu(void)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun if (cpu_has_mipsmt || cpu_has_vp)
437*4882a593Smuzhiyun cps_shutdown_this_cpu(CPU_DEATH_HALT);
438*4882a593Smuzhiyun else
439*4882a593Smuzhiyun cps_shutdown_this_cpu(CPU_DEATH_POWER);
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun #endif /* CONFIG_KEXEC */
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun #endif /* CONFIG_HOTPLUG_CPU || CONFIG_KEXEC */
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun #ifdef CONFIG_HOTPLUG_CPU
447*4882a593Smuzhiyun
cps_cpu_disable(void)448*4882a593Smuzhiyun static int cps_cpu_disable(void)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun unsigned cpu = smp_processor_id();
451*4882a593Smuzhiyun struct core_boot_config *core_cfg;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun if (!cpu)
454*4882a593Smuzhiyun return -EBUSY;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun if (!cps_pm_support_state(CPS_PM_POWER_GATED))
457*4882a593Smuzhiyun return -EINVAL;
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun core_cfg = &mips_cps_core_bootcfg[cpu_core(¤t_cpu_data)];
460*4882a593Smuzhiyun atomic_sub(1 << cpu_vpe_id(¤t_cpu_data), &core_cfg->vpe_mask);
461*4882a593Smuzhiyun smp_mb__after_atomic();
462*4882a593Smuzhiyun set_cpu_online(cpu, false);
463*4882a593Smuzhiyun calculate_cpu_foreign_map();
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun return 0;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun static unsigned cpu_death_sibling;
469*4882a593Smuzhiyun static enum cpu_death cpu_death;
470*4882a593Smuzhiyun
play_dead(void)471*4882a593Smuzhiyun void play_dead(void)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun unsigned int cpu;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun local_irq_disable();
476*4882a593Smuzhiyun idle_task_exit();
477*4882a593Smuzhiyun cpu = smp_processor_id();
478*4882a593Smuzhiyun cpu_death = CPU_DEATH_POWER;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun pr_debug("CPU%d going offline\n", cpu);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun if (cpu_has_mipsmt || cpu_has_vp) {
483*4882a593Smuzhiyun /* Look for another online VPE within the core */
484*4882a593Smuzhiyun for_each_online_cpu(cpu_death_sibling) {
485*4882a593Smuzhiyun if (!cpus_are_siblings(cpu, cpu_death_sibling))
486*4882a593Smuzhiyun continue;
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun /*
489*4882a593Smuzhiyun * There is an online VPE within the core. Just halt
490*4882a593Smuzhiyun * this TC and leave the core alone.
491*4882a593Smuzhiyun */
492*4882a593Smuzhiyun cpu_death = CPU_DEATH_HALT;
493*4882a593Smuzhiyun break;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun /* This CPU has chosen its way out */
498*4882a593Smuzhiyun (void)cpu_report_death();
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun cps_shutdown_this_cpu(cpu_death);
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun /* This should never be reached */
503*4882a593Smuzhiyun panic("Failed to offline CPU %u", cpu);
504*4882a593Smuzhiyun }
505*4882a593Smuzhiyun
wait_for_sibling_halt(void * ptr_cpu)506*4882a593Smuzhiyun static void wait_for_sibling_halt(void *ptr_cpu)
507*4882a593Smuzhiyun {
508*4882a593Smuzhiyun unsigned cpu = (unsigned long)ptr_cpu;
509*4882a593Smuzhiyun unsigned vpe_id = cpu_vpe_id(&cpu_data[cpu]);
510*4882a593Smuzhiyun unsigned halted;
511*4882a593Smuzhiyun unsigned long flags;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun do {
514*4882a593Smuzhiyun local_irq_save(flags);
515*4882a593Smuzhiyun settc(vpe_id);
516*4882a593Smuzhiyun halted = read_tc_c0_tchalt();
517*4882a593Smuzhiyun local_irq_restore(flags);
518*4882a593Smuzhiyun } while (!(halted & TCHALT_H));
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun
cps_cpu_die(unsigned int cpu)521*4882a593Smuzhiyun static void cps_cpu_die(unsigned int cpu)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun unsigned core = cpu_core(&cpu_data[cpu]);
524*4882a593Smuzhiyun unsigned int vpe_id = cpu_vpe_id(&cpu_data[cpu]);
525*4882a593Smuzhiyun ktime_t fail_time;
526*4882a593Smuzhiyun unsigned stat;
527*4882a593Smuzhiyun int err;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun /* Wait for the cpu to choose its way out */
530*4882a593Smuzhiyun if (!cpu_wait_death(cpu, 5)) {
531*4882a593Smuzhiyun pr_err("CPU%u: didn't offline\n", cpu);
532*4882a593Smuzhiyun return;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun /*
536*4882a593Smuzhiyun * Now wait for the CPU to actually offline. Without doing this that
537*4882a593Smuzhiyun * offlining may race with one or more of:
538*4882a593Smuzhiyun *
539*4882a593Smuzhiyun * - Onlining the CPU again.
540*4882a593Smuzhiyun * - Powering down the core if another VPE within it is offlined.
541*4882a593Smuzhiyun * - A sibling VPE entering a non-coherent state.
542*4882a593Smuzhiyun *
543*4882a593Smuzhiyun * In the non-MT halt case (ie. infinite loop) the CPU is doing nothing
544*4882a593Smuzhiyun * with which we could race, so do nothing.
545*4882a593Smuzhiyun */
546*4882a593Smuzhiyun if (cpu_death == CPU_DEATH_POWER) {
547*4882a593Smuzhiyun /*
548*4882a593Smuzhiyun * Wait for the core to enter a powered down or clock gated
549*4882a593Smuzhiyun * state, the latter happening when a JTAG probe is connected
550*4882a593Smuzhiyun * in which case the CPC will refuse to power down the core.
551*4882a593Smuzhiyun */
552*4882a593Smuzhiyun fail_time = ktime_add_ms(ktime_get(), 2000);
553*4882a593Smuzhiyun do {
554*4882a593Smuzhiyun mips_cm_lock_other(0, core, 0, CM_GCR_Cx_OTHER_BLOCK_LOCAL);
555*4882a593Smuzhiyun mips_cpc_lock_other(core);
556*4882a593Smuzhiyun stat = read_cpc_co_stat_conf();
557*4882a593Smuzhiyun stat &= CPC_Cx_STAT_CONF_SEQSTATE;
558*4882a593Smuzhiyun stat >>= __ffs(CPC_Cx_STAT_CONF_SEQSTATE);
559*4882a593Smuzhiyun mips_cpc_unlock_other();
560*4882a593Smuzhiyun mips_cm_unlock_other();
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun if (stat == CPC_Cx_STAT_CONF_SEQSTATE_D0 ||
563*4882a593Smuzhiyun stat == CPC_Cx_STAT_CONF_SEQSTATE_D2 ||
564*4882a593Smuzhiyun stat == CPC_Cx_STAT_CONF_SEQSTATE_U2)
565*4882a593Smuzhiyun break;
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun /*
568*4882a593Smuzhiyun * The core ought to have powered down, but didn't &
569*4882a593Smuzhiyun * now we don't really know what state it's in. It's
570*4882a593Smuzhiyun * likely that its _pwr_up pin has been wired to logic
571*4882a593Smuzhiyun * 1 & it powered back up as soon as we powered it
572*4882a593Smuzhiyun * down...
573*4882a593Smuzhiyun *
574*4882a593Smuzhiyun * The best we can do is warn the user & continue in
575*4882a593Smuzhiyun * the hope that the core is doing nothing harmful &
576*4882a593Smuzhiyun * might behave properly if we online it later.
577*4882a593Smuzhiyun */
578*4882a593Smuzhiyun if (WARN(ktime_after(ktime_get(), fail_time),
579*4882a593Smuzhiyun "CPU%u hasn't powered down, seq. state %u\n",
580*4882a593Smuzhiyun cpu, stat))
581*4882a593Smuzhiyun break;
582*4882a593Smuzhiyun } while (1);
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun /* Indicate the core is powered off */
585*4882a593Smuzhiyun bitmap_clear(core_power, core, 1);
586*4882a593Smuzhiyun } else if (cpu_has_mipsmt) {
587*4882a593Smuzhiyun /*
588*4882a593Smuzhiyun * Have a CPU with access to the offlined CPUs registers wait
589*4882a593Smuzhiyun * for its TC to halt.
590*4882a593Smuzhiyun */
591*4882a593Smuzhiyun err = smp_call_function_single(cpu_death_sibling,
592*4882a593Smuzhiyun wait_for_sibling_halt,
593*4882a593Smuzhiyun (void *)(unsigned long)cpu, 1);
594*4882a593Smuzhiyun if (err)
595*4882a593Smuzhiyun panic("Failed to call remote sibling CPU\n");
596*4882a593Smuzhiyun } else if (cpu_has_vp) {
597*4882a593Smuzhiyun do {
598*4882a593Smuzhiyun mips_cm_lock_other(0, core, vpe_id, CM_GCR_Cx_OTHER_BLOCK_LOCAL);
599*4882a593Smuzhiyun stat = read_cpc_co_vp_running();
600*4882a593Smuzhiyun mips_cm_unlock_other();
601*4882a593Smuzhiyun } while (stat & (1 << vpe_id));
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun #endif /* CONFIG_HOTPLUG_CPU */
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun static const struct plat_smp_ops cps_smp_ops = {
608*4882a593Smuzhiyun .smp_setup = cps_smp_setup,
609*4882a593Smuzhiyun .prepare_cpus = cps_prepare_cpus,
610*4882a593Smuzhiyun .boot_secondary = cps_boot_secondary,
611*4882a593Smuzhiyun .init_secondary = cps_init_secondary,
612*4882a593Smuzhiyun .smp_finish = cps_smp_finish,
613*4882a593Smuzhiyun .send_ipi_single = mips_smp_send_ipi_single,
614*4882a593Smuzhiyun .send_ipi_mask = mips_smp_send_ipi_mask,
615*4882a593Smuzhiyun #ifdef CONFIG_HOTPLUG_CPU
616*4882a593Smuzhiyun .cpu_disable = cps_cpu_disable,
617*4882a593Smuzhiyun .cpu_die = cps_cpu_die,
618*4882a593Smuzhiyun #endif
619*4882a593Smuzhiyun #ifdef CONFIG_KEXEC
620*4882a593Smuzhiyun .kexec_nonboot_cpu = cps_kexec_nonboot_cpu,
621*4882a593Smuzhiyun #endif
622*4882a593Smuzhiyun };
623*4882a593Smuzhiyun
mips_cps_smp_in_use(void)624*4882a593Smuzhiyun bool mips_cps_smp_in_use(void)
625*4882a593Smuzhiyun {
626*4882a593Smuzhiyun extern const struct plat_smp_ops *mp_ops;
627*4882a593Smuzhiyun return mp_ops == &cps_smp_ops;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun
register_cps_smp_ops(void)630*4882a593Smuzhiyun int register_cps_smp_ops(void)
631*4882a593Smuzhiyun {
632*4882a593Smuzhiyun if (!mips_cm_present()) {
633*4882a593Smuzhiyun pr_warn("MIPS CPS SMP unable to proceed without a CM\n");
634*4882a593Smuzhiyun return -ENODEV;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun /* check we have a GIC - we need one for IPIs */
638*4882a593Smuzhiyun if (!(read_gcr_gic_status() & CM_GCR_GIC_STATUS_EX)) {
639*4882a593Smuzhiyun pr_warn("MIPS CPS SMP unable to proceed without a GIC\n");
640*4882a593Smuzhiyun return -ENODEV;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun register_smp_ops(&cps_smp_ops);
644*4882a593Smuzhiyun return 0;
645*4882a593Smuzhiyun }
646