xref: /openbmc/linux/arch/mips/kernel/mips-mt.c (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
241c594abSRalf Baechle /*
3b633648cSRalf Baechle  * General MIPS MT support routines, usable in AP/SP and SMVP.
441c594abSRalf Baechle  * Copyright (C) 2005 Mips Technologies, Inc
541c594abSRalf Baechle  */
641c594abSRalf Baechle 
727a3bbafSRalf Baechle #include <linux/device.h>
841c594abSRalf Baechle #include <linux/kernel.h>
941c594abSRalf Baechle #include <linux/sched.h>
1073bc256dSPaul Gortmaker #include <linux/export.h>
1141c594abSRalf Baechle #include <linux/interrupt.h>
12f72af3cfSYoichi Yuasa #include <linux/security.h>
1341c594abSRalf Baechle 
1441c594abSRalf Baechle #include <asm/cpu.h>
1541c594abSRalf Baechle #include <asm/processor.h>
1660063497SArun Sharma #include <linux/atomic.h>
1741c594abSRalf Baechle #include <asm/hardirq.h>
1841c594abSRalf Baechle #include <asm/mmu_context.h>
1941c594abSRalf Baechle #include <asm/mipsmtregs.h>
2041c594abSRalf Baechle #include <asm/r4kcache.h>
2141c594abSRalf Baechle #include <asm/cacheflush.h>
2241c594abSRalf Baechle 
2307cc0c9eSRalf Baechle int vpelimit;
2407cc0c9eSRalf Baechle 
2507cc0c9eSRalf Baechle static int __init maxvpes(char *str)
2607cc0c9eSRalf Baechle {
2707cc0c9eSRalf Baechle 	get_option(&str, &vpelimit);
2807cc0c9eSRalf Baechle 
2907cc0c9eSRalf Baechle 	return 1;
3007cc0c9eSRalf Baechle }
3107cc0c9eSRalf Baechle 
3207cc0c9eSRalf Baechle __setup("maxvpes=", maxvpes);
3307cc0c9eSRalf Baechle 
3407cc0c9eSRalf Baechle int tclimit;
3507cc0c9eSRalf Baechle 
3607cc0c9eSRalf Baechle static int __init maxtcs(char *str)
3707cc0c9eSRalf Baechle {
3807cc0c9eSRalf Baechle 	get_option(&str, &tclimit);
3907cc0c9eSRalf Baechle 
4007cc0c9eSRalf Baechle 	return 1;
4107cc0c9eSRalf Baechle }
4207cc0c9eSRalf Baechle 
4307cc0c9eSRalf Baechle __setup("maxtcs=", maxtcs);
4407cc0c9eSRalf Baechle 
4541c594abSRalf Baechle /*
4641c594abSRalf Baechle  * Dump new MIPS MT state for the core. Does not leave TCs halted.
4741c594abSRalf Baechle  * Takes an argument which taken to be a pre-call MVPControl value.
4841c594abSRalf Baechle  */
4941c594abSRalf Baechle 
5041c594abSRalf Baechle void mips_mt_regdump(unsigned long mvpctl)
5141c594abSRalf Baechle {
5241c594abSRalf Baechle 	unsigned long flags;
5341c594abSRalf Baechle 	unsigned long vpflags;
5441c594abSRalf Baechle 	unsigned long mvpconf0;
5541c594abSRalf Baechle 	int nvpe;
5641c594abSRalf Baechle 	int ntc;
5741c594abSRalf Baechle 	int i;
5841c594abSRalf Baechle 	int tc;
5941c594abSRalf Baechle 	unsigned long haltval;
6041c594abSRalf Baechle 	unsigned long tcstatval;
6141c594abSRalf Baechle 
6241c594abSRalf Baechle 	local_irq_save(flags);
6341c594abSRalf Baechle 	vpflags = dvpe();
6441c594abSRalf Baechle 	printk("=== MIPS MT State Dump ===\n");
6541c594abSRalf Baechle 	printk("-- Global State --\n");
6641c594abSRalf Baechle 	printk("   MVPControl Passed: %08lx\n", mvpctl);
6741c594abSRalf Baechle 	printk("   MVPControl Read: %08lx\n", vpflags);
6841c594abSRalf Baechle 	printk("   MVPConf0 : %08lx\n", (mvpconf0 = read_c0_mvpconf0()));
6941c594abSRalf Baechle 	nvpe = ((mvpconf0 & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
7041c594abSRalf Baechle 	ntc = ((mvpconf0 & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
7141c594abSRalf Baechle 	printk("-- per-VPE State --\n");
7241c594abSRalf Baechle 	for (i = 0; i < nvpe; i++) {
7341c594abSRalf Baechle 		for (tc = 0; tc < ntc; tc++) {
7441c594abSRalf Baechle 			settc(tc);
7541c594abSRalf Baechle 			if ((read_tc_c0_tcbind() & TCBIND_CURVPE) == i) {
7641c594abSRalf Baechle 				printk("  VPE %d\n", i);
77d223a861SRalf Baechle 				printk("   VPEControl : %08lx\n",
78d223a861SRalf Baechle 				       read_vpe_c0_vpecontrol());
79d223a861SRalf Baechle 				printk("   VPEConf0 : %08lx\n",
80d223a861SRalf Baechle 				       read_vpe_c0_vpeconf0());
8141c594abSRalf Baechle 				printk("   VPE%d.Status : %08lx\n",
8241c594abSRalf Baechle 				       i, read_vpe_c0_status());
83b012cffeSRalf Baechle 				printk("   VPE%d.EPC : %08lx %pS\n",
84b012cffeSRalf Baechle 				       i, read_vpe_c0_epc(),
85b012cffeSRalf Baechle 				       (void *) read_vpe_c0_epc());
86d223a861SRalf Baechle 				printk("   VPE%d.Cause : %08lx\n",
87d223a861SRalf Baechle 				       i, read_vpe_c0_cause());
8841c594abSRalf Baechle 				printk("   VPE%d.Config7 : %08lx\n",
8941c594abSRalf Baechle 				       i, read_vpe_c0_config7());
9041c594abSRalf Baechle 				break; /* Next VPE */
9141c594abSRalf Baechle 			}
9241c594abSRalf Baechle 		}
9341c594abSRalf Baechle 	}
9441c594abSRalf Baechle 	printk("-- per-TC State --\n");
9541c594abSRalf Baechle 	for (tc = 0; tc < ntc; tc++) {
9641c594abSRalf Baechle 		settc(tc);
9741c594abSRalf Baechle 		if (read_tc_c0_tcbind() == read_c0_tcbind()) {
9841c594abSRalf Baechle 			/* Are we dumping ourself?  */
9941c594abSRalf Baechle 			haltval = 0; /* Then we're not halted, and mustn't be */
10041c594abSRalf Baechle 			tcstatval = flags; /* And pre-dump TCStatus is flags */
10141c594abSRalf Baechle 			printk("  TC %d (current TC with VPE EPC above)\n", tc);
10241c594abSRalf Baechle 		} else {
10341c594abSRalf Baechle 			haltval = read_tc_c0_tchalt();
10441c594abSRalf Baechle 			write_tc_c0_tchalt(1);
10541c594abSRalf Baechle 			tcstatval = read_tc_c0_tcstatus();
10641c594abSRalf Baechle 			printk("  TC %d\n", tc);
10741c594abSRalf Baechle 		}
10841c594abSRalf Baechle 		printk("   TCStatus : %08lx\n", tcstatval);
10941c594abSRalf Baechle 		printk("   TCBind : %08lx\n", read_tc_c0_tcbind());
110b012cffeSRalf Baechle 		printk("   TCRestart : %08lx %pS\n",
111b012cffeSRalf Baechle 		       read_tc_c0_tcrestart(), (void *) read_tc_c0_tcrestart());
11241c594abSRalf Baechle 		printk("   TCHalt : %08lx\n", haltval);
11341c594abSRalf Baechle 		printk("   TCContext : %08lx\n", read_tc_c0_tccontext());
11441c594abSRalf Baechle 		if (!haltval)
11541c594abSRalf Baechle 			write_tc_c0_tchalt(0);
11641c594abSRalf Baechle 	}
11741c594abSRalf Baechle 	printk("===========================\n");
11841c594abSRalf Baechle 	evpe(vpflags);
11941c594abSRalf Baechle 	local_irq_restore(flags);
12041c594abSRalf Baechle }
12141c594abSRalf Baechle 
122982f6ffeSRalf Baechle static int mt_opt_norps;
12341c594abSRalf Baechle static int mt_opt_rpsctl = -1;
12441c594abSRalf Baechle static int mt_opt_nblsu = -1;
125982f6ffeSRalf Baechle static int mt_opt_forceconfig7;
12641c594abSRalf Baechle static int mt_opt_config7 = -1;
12741c594abSRalf Baechle 
12841c594abSRalf Baechle static int __init rps_disable(char *s)
12941c594abSRalf Baechle {
13041c594abSRalf Baechle 	mt_opt_norps = 1;
13141c594abSRalf Baechle 	return 1;
13241c594abSRalf Baechle }
13341c594abSRalf Baechle __setup("norps", rps_disable);
13441c594abSRalf Baechle 
13541c594abSRalf Baechle static int __init rpsctl_set(char *str)
13641c594abSRalf Baechle {
13741c594abSRalf Baechle 	get_option(&str, &mt_opt_rpsctl);
13841c594abSRalf Baechle 	return 1;
13941c594abSRalf Baechle }
14041c594abSRalf Baechle __setup("rpsctl=", rpsctl_set);
14141c594abSRalf Baechle 
14241c594abSRalf Baechle static int __init nblsu_set(char *str)
14341c594abSRalf Baechle {
14441c594abSRalf Baechle 	get_option(&str, &mt_opt_nblsu);
14541c594abSRalf Baechle 	return 1;
14641c594abSRalf Baechle }
14741c594abSRalf Baechle __setup("nblsu=", nblsu_set);
14841c594abSRalf Baechle 
14941c594abSRalf Baechle static int __init config7_set(char *str)
15041c594abSRalf Baechle {
15141c594abSRalf Baechle 	get_option(&str, &mt_opt_config7);
15241c594abSRalf Baechle 	mt_opt_forceconfig7 = 1;
15341c594abSRalf Baechle 	return 1;
15441c594abSRalf Baechle }
15541c594abSRalf Baechle __setup("config7=", config7_set);
15641c594abSRalf Baechle 
15741c594abSRalf Baechle /* Experimental cache flush control parameters that should go away some day */
158982f6ffeSRalf Baechle int mt_protiflush;
159982f6ffeSRalf Baechle int mt_protdflush;
16041c594abSRalf Baechle int mt_n_iflushes = 1;
16141c594abSRalf Baechle int mt_n_dflushes = 1;
16241c594abSRalf Baechle 
16341c594abSRalf Baechle static int __init set_protiflush(char *s)
16441c594abSRalf Baechle {
16541c594abSRalf Baechle 	mt_protiflush = 1;
16641c594abSRalf Baechle 	return 1;
16741c594abSRalf Baechle }
16841c594abSRalf Baechle __setup("protiflush", set_protiflush);
16941c594abSRalf Baechle 
17041c594abSRalf Baechle static int __init set_protdflush(char *s)
17141c594abSRalf Baechle {
17241c594abSRalf Baechle 	mt_protdflush = 1;
17341c594abSRalf Baechle 	return 1;
17441c594abSRalf Baechle }
17541c594abSRalf Baechle __setup("protdflush", set_protdflush);
17641c594abSRalf Baechle 
17741c594abSRalf Baechle static int __init niflush(char *s)
17841c594abSRalf Baechle {
17941c594abSRalf Baechle 	get_option(&s, &mt_n_iflushes);
18041c594abSRalf Baechle 	return 1;
18141c594abSRalf Baechle }
18241c594abSRalf Baechle __setup("niflush=", niflush);
18341c594abSRalf Baechle 
18441c594abSRalf Baechle static int __init ndflush(char *s)
18541c594abSRalf Baechle {
18641c594abSRalf Baechle 	get_option(&s, &mt_n_dflushes);
18741c594abSRalf Baechle 	return 1;
18841c594abSRalf Baechle }
18941c594abSRalf Baechle __setup("ndflush=", ndflush);
19041c594abSRalf Baechle 
191982f6ffeSRalf Baechle static unsigned int itc_base;
19241c594abSRalf Baechle 
19341c594abSRalf Baechle static int __init set_itc_base(char *str)
19441c594abSRalf Baechle {
19541c594abSRalf Baechle 	get_option(&str, &itc_base);
19641c594abSRalf Baechle 	return 1;
19741c594abSRalf Baechle }
19841c594abSRalf Baechle 
19941c594abSRalf Baechle __setup("itcbase=", set_itc_base);
20041c594abSRalf Baechle 
20141c594abSRalf Baechle void mips_mt_set_cpuoptions(void)
20241c594abSRalf Baechle {
20341c594abSRalf Baechle 	unsigned int oconfig7 = read_c0_config7();
20441c594abSRalf Baechle 	unsigned int nconfig7 = oconfig7;
20541c594abSRalf Baechle 
20641c594abSRalf Baechle 	if (mt_opt_norps) {
2076997991aSMasanari Iida 		printk("\"norps\" option deprecated: use \"rpsctl=\"\n");
20841c594abSRalf Baechle 	}
20941c594abSRalf Baechle 	if (mt_opt_rpsctl >= 0) {
21041c594abSRalf Baechle 		printk("34K return prediction stack override set to %d.\n",
21141c594abSRalf Baechle 			mt_opt_rpsctl);
21241c594abSRalf Baechle 		if (mt_opt_rpsctl)
21341c594abSRalf Baechle 			nconfig7 |= (1 << 2);
21441c594abSRalf Baechle 		else
21541c594abSRalf Baechle 			nconfig7 &= ~(1 << 2);
21641c594abSRalf Baechle 	}
21741c594abSRalf Baechle 	if (mt_opt_nblsu >= 0) {
21841c594abSRalf Baechle 		printk("34K ALU/LSU sync override set to %d.\n", mt_opt_nblsu);
21941c594abSRalf Baechle 		if (mt_opt_nblsu)
22041c594abSRalf Baechle 			nconfig7 |= (1 << 5);
22141c594abSRalf Baechle 		else
22241c594abSRalf Baechle 			nconfig7 &= ~(1 << 5);
22341c594abSRalf Baechle 	}
22441c594abSRalf Baechle 	if (mt_opt_forceconfig7) {
22541c594abSRalf Baechle 		printk("CP0.Config7 forced to 0x%08x.\n", mt_opt_config7);
22641c594abSRalf Baechle 		nconfig7 = mt_opt_config7;
22741c594abSRalf Baechle 	}
22841c594abSRalf Baechle 	if (oconfig7 != nconfig7) {
22941c594abSRalf Baechle 		__asm__ __volatile("sync");
23041c594abSRalf Baechle 		write_c0_config7(nconfig7);
23141c594abSRalf Baechle 		ehb();
23241c594abSRalf Baechle 		printk("Config7: 0x%08x\n", read_c0_config7());
23341c594abSRalf Baechle 	}
23441c594abSRalf Baechle 
23541c594abSRalf Baechle 	/* Report Cache management debug options */
23641c594abSRalf Baechle 	if (mt_protiflush)
23741c594abSRalf Baechle 		printk("I-cache flushes single-threaded\n");
23841c594abSRalf Baechle 	if (mt_protdflush)
23941c594abSRalf Baechle 		printk("D-cache flushes single-threaded\n");
24041c594abSRalf Baechle 	if (mt_n_iflushes != 1)
24141c594abSRalf Baechle 		printk("I-Cache Flushes Repeated %d times\n", mt_n_iflushes);
24241c594abSRalf Baechle 	if (mt_n_dflushes != 1)
24341c594abSRalf Baechle 		printk("D-Cache Flushes Repeated %d times\n", mt_n_dflushes);
24441c594abSRalf Baechle 
24541c594abSRalf Baechle 	if (itc_base != 0) {
24641c594abSRalf Baechle 		/*
24741c594abSRalf Baechle 		 * Configure ITC mapping.  This code is very
24841c594abSRalf Baechle 		 * specific to the 34K core family, which uses
24941c594abSRalf Baechle 		 * a special mode bit ("ITC") in the ErrCtl
25041c594abSRalf Baechle 		 * register to enable access to ITC control
25141c594abSRalf Baechle 		 * registers via cache "tag" operations.
25241c594abSRalf Baechle 		 */
25341c594abSRalf Baechle 		unsigned long ectlval;
25441c594abSRalf Baechle 		unsigned long itcblkgrn;
25541c594abSRalf Baechle 
25641c594abSRalf Baechle 		/* ErrCtl register is known as "ecc" to Linux */
25741c594abSRalf Baechle 		ectlval = read_c0_ecc();
25841c594abSRalf Baechle 		write_c0_ecc(ectlval | (0x1 << 26));
25941c594abSRalf Baechle 		ehb();
26041c594abSRalf Baechle #define INDEX_0 (0x80000000)
26141c594abSRalf Baechle #define INDEX_8 (0x80000008)
26241c594abSRalf Baechle 		/* Read "cache tag" for Dcache pseudo-index 8 */
26341c594abSRalf Baechle 		cache_op(Index_Load_Tag_D, INDEX_8);
26441c594abSRalf Baechle 		ehb();
26541c594abSRalf Baechle 		itcblkgrn = read_c0_dtaglo();
26641c594abSRalf Baechle 		itcblkgrn &= 0xfffe0000;
26741c594abSRalf Baechle 		/* Set for 128 byte pitch of ITC cells */
26841c594abSRalf Baechle 		itcblkgrn |= 0x00000c00;
26941c594abSRalf Baechle 		/* Stage in Tag register */
27041c594abSRalf Baechle 		write_c0_dtaglo(itcblkgrn);
27141c594abSRalf Baechle 		ehb();
27241c594abSRalf Baechle 		/* Write out to ITU with CACHE op */
27341c594abSRalf Baechle 		cache_op(Index_Store_Tag_D, INDEX_8);
27441c594abSRalf Baechle 		/* Now set base address, and turn ITC on with 0x1 bit */
27541c594abSRalf Baechle 		write_c0_dtaglo((itc_base & 0xfffffc00) | 0x1 );
27641c594abSRalf Baechle 		ehb();
27741c594abSRalf Baechle 		/* Write out to ITU with CACHE op */
27841c594abSRalf Baechle 		cache_op(Index_Store_Tag_D, INDEX_0);
27941c594abSRalf Baechle 		write_c0_ecc(ectlval);
28041c594abSRalf Baechle 		ehb();
28141c594abSRalf Baechle 		printk("Mapped %ld ITC cells starting at 0x%08x\n",
28241c594abSRalf Baechle 			((itcblkgrn & 0x7fe00000) >> 20), itc_base);
28341c594abSRalf Baechle 	}
28441c594abSRalf Baechle }
28541c594abSRalf Baechle 
28641c594abSRalf Baechle /*
28741c594abSRalf Baechle  * Function to protect cache flushes from concurrent execution
28841c594abSRalf Baechle  * depends on MP software model chosen.
28941c594abSRalf Baechle  */
29041c594abSRalf Baechle 
29141c594abSRalf Baechle void mt_cflush_lockdown(void)
29241c594abSRalf Baechle {
29341c594abSRalf Baechle 	/* FILL IN VSMP and AP/SP VERSIONS HERE */
29441c594abSRalf Baechle }
29541c594abSRalf Baechle 
29641c594abSRalf Baechle void mt_cflush_release(void)
29741c594abSRalf Baechle {
29841c594abSRalf Baechle 	/* FILL IN VSMP and AP/SP VERSIONS HERE */
29941c594abSRalf Baechle }
30027a3bbafSRalf Baechle 
30127a3bbafSRalf Baechle struct class *mt_class;
30227a3bbafSRalf Baechle 
30327a3bbafSRalf Baechle static int __init mt_init(void)
30427a3bbafSRalf Baechle {
30527a3bbafSRalf Baechle 	struct class *mtc;
30627a3bbafSRalf Baechle 
30727a3bbafSRalf Baechle 	mtc = class_create(THIS_MODULE, "mt");
30827a3bbafSRalf Baechle 	if (IS_ERR(mtc))
30927a3bbafSRalf Baechle 		return PTR_ERR(mtc);
31027a3bbafSRalf Baechle 
31127a3bbafSRalf Baechle 	mt_class = mtc;
31227a3bbafSRalf Baechle 
31327a3bbafSRalf Baechle 	return 0;
31427a3bbafSRalf Baechle }
31527a3bbafSRalf Baechle 
31627a3bbafSRalf Baechle subsys_initcall(mt_init);
317