xref: /openbmc/linux/arch/mips/kernel/smp-mt.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
141173abcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
241c594abSRalf Baechle /*
341c594abSRalf Baechle  *
441c594abSRalf Baechle  * Copyright (C) 2004, 05, 06 MIPS Technologies, Inc.
541c594abSRalf Baechle  *    Elizabeth Clarke (beth@mips.com)
641c594abSRalf Baechle  *    Ralf Baechle (ralf@linux-mips.org)
741c594abSRalf Baechle  * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
841c594abSRalf Baechle  */
941c594abSRalf Baechle #include <linux/kernel.h>
1041c594abSRalf Baechle #include <linux/sched.h>
1141c594abSRalf Baechle #include <linux/cpumask.h>
1241c594abSRalf Baechle #include <linux/interrupt.h>
1341c594abSRalf Baechle #include <linux/compiler.h>
14fc69910fSArnd Bergmann #include <linux/sched/task_stack.h>
150ab7aefcSRalf Baechle #include <linux/smp.h>
1641c594abSRalf Baechle 
1760063497SArun Sharma #include <linux/atomic.h>
1841c594abSRalf Baechle #include <asm/cacheflush.h>
1941c594abSRalf Baechle #include <asm/cpu.h>
2041c594abSRalf Baechle #include <asm/processor.h>
2141c594abSRalf Baechle #include <asm/hardirq.h>
2241c594abSRalf Baechle #include <asm/mmu_context.h>
2341c594abSRalf Baechle #include <asm/time.h>
2441c594abSRalf Baechle #include <asm/mipsregs.h>
2541c594abSRalf Baechle #include <asm/mipsmtregs.h>
2641c594abSRalf Baechle #include <asm/mips_mt.h>
2772eb2995SPaul Burton #include <asm/mips-cps.h>
2841c594abSRalf Baechle 
smvp_copy_vpe_config(void)2939b8d525SRalf Baechle static void __init smvp_copy_vpe_config(void)
30781b0f8dSRalf Baechle {
31781b0f8dSRalf Baechle 	write_vpe_c0_status(
32781b0f8dSRalf Baechle 		(read_c0_status() & ~(ST0_IM | ST0_IE | ST0_KSU)) | ST0_CU0);
33781b0f8dSRalf Baechle 
34781b0f8dSRalf Baechle 	/* set config to be the same as vpe0, particularly kseg0 coherency alg */
35781b0f8dSRalf Baechle 	write_vpe_c0_config( read_c0_config());
36781b0f8dSRalf Baechle 
37781b0f8dSRalf Baechle 	/* make sure there are no software interrupts pending */
38781b0f8dSRalf Baechle 	write_vpe_c0_cause(0);
39781b0f8dSRalf Baechle 
40781b0f8dSRalf Baechle 	/* Propagate Config7 */
41781b0f8dSRalf Baechle 	write_vpe_c0_config7(read_c0_config7());
4270e46f48SRalf Baechle 
4370e46f48SRalf Baechle 	write_vpe_c0_count(read_c0_count());
44781b0f8dSRalf Baechle }
45781b0f8dSRalf Baechle 
smvp_vpe_init(unsigned int tc,unsigned int mvpconf0,unsigned int ncpu)4639b8d525SRalf Baechle static unsigned int __init smvp_vpe_init(unsigned int tc, unsigned int mvpconf0,
47781b0f8dSRalf Baechle 	unsigned int ncpu)
48781b0f8dSRalf Baechle {
49*96cb8ae2SJiaxun Yang 	if (tc >= smp_max_threads ||
50*96cb8ae2SJiaxun Yang 		(tc > ((mvpconf0 & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT)))
51781b0f8dSRalf Baechle 		return ncpu;
52781b0f8dSRalf Baechle 
53781b0f8dSRalf Baechle 	/* Deactivate all but VPE 0 */
54781b0f8dSRalf Baechle 	if (tc != 0) {
55781b0f8dSRalf Baechle 		unsigned long tmp = read_vpe_c0_vpeconf0();
56781b0f8dSRalf Baechle 
57781b0f8dSRalf Baechle 		tmp &= ~VPECONF0_VPA;
58781b0f8dSRalf Baechle 
59781b0f8dSRalf Baechle 		/* master VPE */
60781b0f8dSRalf Baechle 		tmp |= VPECONF0_MVP;
61781b0f8dSRalf Baechle 		write_vpe_c0_vpeconf0(tmp);
62781b0f8dSRalf Baechle 
63781b0f8dSRalf Baechle 		/* Record this as available CPU */
644037ac6eSRusty Russell 		set_cpu_possible(tc, true);
65c2c2a644SMarkos Chandras 		set_cpu_present(tc, true);
66781b0f8dSRalf Baechle 		__cpu_number_map[tc]	= ++ncpu;
67781b0f8dSRalf Baechle 		__cpu_logical_map[ncpu] = tc;
68781b0f8dSRalf Baechle 	}
69781b0f8dSRalf Baechle 
70781b0f8dSRalf Baechle 	/* Disable multi-threading with TC's */
71781b0f8dSRalf Baechle 	write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() & ~VPECONTROL_TE);
72781b0f8dSRalf Baechle 
73781b0f8dSRalf Baechle 	if (tc != 0)
7439b8d525SRalf Baechle 		smvp_copy_vpe_config();
75781b0f8dSRalf Baechle 
76f875a832SPaul Burton 	cpu_set_vpe_id(&cpu_data[ncpu], tc);
771eed4004SPaul Burton 
78781b0f8dSRalf Baechle 	return ncpu;
79781b0f8dSRalf Baechle }
80781b0f8dSRalf Baechle 
smvp_tc_init(unsigned int tc,unsigned int mvpconf0)8139b8d525SRalf Baechle static void __init smvp_tc_init(unsigned int tc, unsigned int mvpconf0)
82781b0f8dSRalf Baechle {
83781b0f8dSRalf Baechle 	unsigned long tmp;
84781b0f8dSRalf Baechle 
85781b0f8dSRalf Baechle 	if (!tc)
86781b0f8dSRalf Baechle 		return;
87781b0f8dSRalf Baechle 
88781b0f8dSRalf Baechle 	/* bind a TC to each VPE, May as well put all excess TC's
89781b0f8dSRalf Baechle 	   on the last VPE */
90781b0f8dSRalf Baechle 	if (tc >= (((mvpconf0 & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT)+1))
91781b0f8dSRalf Baechle 		write_tc_c0_tcbind(read_tc_c0_tcbind() | ((mvpconf0 & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT));
92781b0f8dSRalf Baechle 	else {
93781b0f8dSRalf Baechle 		write_tc_c0_tcbind(read_tc_c0_tcbind() | tc);
94781b0f8dSRalf Baechle 
95781b0f8dSRalf Baechle 		/* and set XTC */
96781b0f8dSRalf Baechle 		write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | (tc << VPECONF0_XTC_SHIFT));
97781b0f8dSRalf Baechle 	}
98781b0f8dSRalf Baechle 
99781b0f8dSRalf Baechle 	tmp = read_tc_c0_tcstatus();
100781b0f8dSRalf Baechle 
101781b0f8dSRalf Baechle 	/* mark not allocated and not dynamically allocatable */
102781b0f8dSRalf Baechle 	tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
103781b0f8dSRalf Baechle 	tmp |= TCSTATUS_IXMT;		/* interrupt exempt */
104781b0f8dSRalf Baechle 	write_tc_c0_tcstatus(tmp);
105781b0f8dSRalf Baechle 
106781b0f8dSRalf Baechle 	write_tc_c0_tchalt(TCHALT_H);
107781b0f8dSRalf Baechle }
108781b0f8dSRalf Baechle 
vsmp_init_secondary(void)109078a55fcSPaul Gortmaker static void vsmp_init_secondary(void)
11087353d8aSRalf Baechle {
111d002aaadSRalf Baechle 	/* This is Malta specific: IPI,performance and timer interrupts */
11272eb2995SPaul Burton 	if (mips_gic_present())
113c3f134fbSJames Hogan 		change_c0_status(ST0_IM, STATUSF_IP2 | STATUSF_IP3 |
114c3f134fbSJames Hogan 					 STATUSF_IP4 | STATUSF_IP5 |
11539b8d525SRalf Baechle 					 STATUSF_IP6 | STATUSF_IP7);
11639b8d525SRalf Baechle 	else
11739b8d525SRalf Baechle 		change_c0_status(ST0_IM, STATUSF_IP0 | STATUSF_IP1 |
11839b8d525SRalf Baechle 					 STATUSF_IP6 | STATUSF_IP7);
11987353d8aSRalf Baechle }
12087353d8aSRalf Baechle 
vsmp_smp_finish(void)121078a55fcSPaul Gortmaker static void vsmp_smp_finish(void)
12287353d8aSRalf Baechle {
12339b8d525SRalf Baechle 	/* CDFIXME: remove this? */
12487353d8aSRalf Baechle 	write_c0_compare(read_c0_count() + (8* mips_hpt_frequency/HZ));
12587353d8aSRalf Baechle 
12687353d8aSRalf Baechle #ifdef CONFIG_MIPS_MT_FPAFF
12787353d8aSRalf Baechle 	/* If we have an FPU, enroll ourselves in the FPU-full mask */
12887353d8aSRalf Baechle 	if (cpu_has_fpu)
1298dd92891SRusty Russell 		cpumask_set_cpu(smp_processor_id(), &mt_fpu_cpumask);
13087353d8aSRalf Baechle #endif /* CONFIG_MIPS_MT_FPAFF */
13187353d8aSRalf Baechle 
13287353d8aSRalf Baechle 	local_irq_enable();
13387353d8aSRalf Baechle }
13487353d8aSRalf Baechle 
13587353d8aSRalf Baechle /*
13687353d8aSRalf Baechle  * Setup the PC, SP, and GP of a secondary processor and start it
13787353d8aSRalf Baechle  * running!
13887353d8aSRalf Baechle  * smp_bootstrap is the place to resume from
13987353d8aSRalf Baechle  * __KSTK_TOS(idle) is apparently the stack pointer
14087353d8aSRalf Baechle  * (unsigned long)idle->thread_info the gp
14187353d8aSRalf Baechle  * assumes a 1:1 mapping of TC => VPE
14287353d8aSRalf Baechle  */
vsmp_boot_secondary(int cpu,struct task_struct * idle)143d595d423SPaul Burton static int vsmp_boot_secondary(int cpu, struct task_struct *idle)
14487353d8aSRalf Baechle {
14587353d8aSRalf Baechle 	struct thread_info *gp = task_thread_info(idle);
14687353d8aSRalf Baechle 	dvpe();
14787353d8aSRalf Baechle 	set_c0_mvpcontrol(MVPCONTROL_VPC);
14887353d8aSRalf Baechle 
14987353d8aSRalf Baechle 	settc(cpu);
15087353d8aSRalf Baechle 
15187353d8aSRalf Baechle 	/* restart */
15287353d8aSRalf Baechle 	write_tc_c0_tcrestart((unsigned long)&smp_bootstrap);
15387353d8aSRalf Baechle 
15487353d8aSRalf Baechle 	/* enable the tc this vpe/cpu will be running */
15587353d8aSRalf Baechle 	write_tc_c0_tcstatus((read_tc_c0_tcstatus() & ~TCSTATUS_IXMT) | TCSTATUS_A);
15687353d8aSRalf Baechle 
15787353d8aSRalf Baechle 	write_tc_c0_tchalt(0);
15887353d8aSRalf Baechle 
15987353d8aSRalf Baechle 	/* enable the VPE */
16087353d8aSRalf Baechle 	write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
16187353d8aSRalf Baechle 
16287353d8aSRalf Baechle 	/* stack pointer */
16387353d8aSRalf Baechle 	write_tc_gpr_sp( __KSTK_TOS(idle));
16487353d8aSRalf Baechle 
16587353d8aSRalf Baechle 	/* global pointer */
16687353d8aSRalf Baechle 	write_tc_gpr_gp((unsigned long)gp);
16787353d8aSRalf Baechle 
16887353d8aSRalf Baechle 	flush_icache_range((unsigned long)gp,
16987353d8aSRalf Baechle 			   (unsigned long)(gp + sizeof(struct thread_info)));
17087353d8aSRalf Baechle 
17187353d8aSRalf Baechle 	/* finally out of configuration and into chaos */
17287353d8aSRalf Baechle 	clear_c0_mvpcontrol(MVPCONTROL_VPC);
17387353d8aSRalf Baechle 
17487353d8aSRalf Baechle 	evpe(EVPE_ENABLE);
175d595d423SPaul Burton 
176d595d423SPaul Burton 	return 0;
17787353d8aSRalf Baechle }
17887353d8aSRalf Baechle 
17941c594abSRalf Baechle /*
18041c594abSRalf Baechle  * Common setup before any secondaries are started
18141c594abSRalf Baechle  * Make sure all CPU's are in a sensible state before we boot any of the
18239b8d525SRalf Baechle  * secondaries
18341c594abSRalf Baechle  */
vsmp_smp_setup(void)18487353d8aSRalf Baechle static void __init vsmp_smp_setup(void)
18541c594abSRalf Baechle {
186781b0f8dSRalf Baechle 	unsigned int mvpconf0, ntc, tc, ncpu = 0;
1870ab7aefcSRalf Baechle 	unsigned int nvpe;
18841c594abSRalf Baechle 
189f088fc84SRalf Baechle #ifdef CONFIG_MIPS_MT_FPAFF
190f088fc84SRalf Baechle 	/* If we have an FPU, enroll ourselves in the FPU-full mask */
191f088fc84SRalf Baechle 	if (cpu_has_fpu)
1928dd92891SRusty Russell 		cpumask_set_cpu(0, &mt_fpu_cpumask);
193f088fc84SRalf Baechle #endif /* CONFIG_MIPS_MT_FPAFF */
19441c594abSRalf Baechle 	if (!cpu_has_mipsmt)
19541c594abSRalf Baechle 		return;
19641c594abSRalf Baechle 
19741c594abSRalf Baechle 	/* disable MT so we can configure */
19841c594abSRalf Baechle 	dvpe();
19941c594abSRalf Baechle 	dmt();
20041c594abSRalf Baechle 
20141c594abSRalf Baechle 	/* Put MVPE's into 'configuration state' */
20241c594abSRalf Baechle 	set_c0_mvpcontrol(MVPCONTROL_VPC);
20341c594abSRalf Baechle 
204781b0f8dSRalf Baechle 	mvpconf0 = read_c0_mvpconf0();
205781b0f8dSRalf Baechle 	ntc = (mvpconf0 & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT;
20641c594abSRalf Baechle 
2070ab7aefcSRalf Baechle 	nvpe = ((mvpconf0 & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
2080ab7aefcSRalf Baechle 	smp_num_siblings = nvpe;
2090ab7aefcSRalf Baechle 
21041c594abSRalf Baechle 	/* we'll always have more TC's than VPE's, so loop setting everything
21141c594abSRalf Baechle 	   to a sensible state */
212781b0f8dSRalf Baechle 	for (tc = 0; tc <= ntc; tc++) {
213781b0f8dSRalf Baechle 		settc(tc);
21441c594abSRalf Baechle 
21539b8d525SRalf Baechle 		smvp_tc_init(tc, mvpconf0);
21639b8d525SRalf Baechle 		ncpu = smvp_vpe_init(tc, mvpconf0, ncpu);
21741c594abSRalf Baechle 	}
21841c594abSRalf Baechle 
21941c594abSRalf Baechle 	/* Release config state */
22041c594abSRalf Baechle 	clear_c0_mvpcontrol(MVPCONTROL_VPC);
22141c594abSRalf Baechle 
22241c594abSRalf Baechle 	/* We'll wait until starting the secondaries before starting MVPE */
22341c594abSRalf Baechle 
224781b0f8dSRalf Baechle 	printk(KERN_INFO "Detected %i available secondary CPU(s)\n", ncpu);
22541c594abSRalf Baechle }
22641c594abSRalf Baechle 
vsmp_prepare_cpus(unsigned int max_cpus)22787353d8aSRalf Baechle static void __init vsmp_prepare_cpus(unsigned int max_cpus)
22841c594abSRalf Baechle {
2298c976e34SRalf Baechle 	mips_mt_set_cpuoptions();
23041c594abSRalf Baechle }
23141c594abSRalf Baechle 
232ff2c8252SMatt Redfearn const struct plat_smp_ops vsmp_smp_ops = {
2331eed4004SPaul Burton 	.send_ipi_single	= mips_smp_send_ipi_single,
2341eed4004SPaul Burton 	.send_ipi_mask		= mips_smp_send_ipi_mask,
23587353d8aSRalf Baechle 	.init_secondary		= vsmp_init_secondary,
23687353d8aSRalf Baechle 	.smp_finish		= vsmp_smp_finish,
23787353d8aSRalf Baechle 	.boot_secondary		= vsmp_boot_secondary,
23887353d8aSRalf Baechle 	.smp_setup		= vsmp_smp_setup,
23987353d8aSRalf Baechle 	.prepare_cpus		= vsmp_prepare_cpus,
24087353d8aSRalf Baechle };
241d6d3c9afSRalf Baechle 
242