xref: /linux/arch/riscv/include/asm/errata_list.h (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2021 Sifive.
4  */
5 #ifndef ASM_ERRATA_LIST_H
6 #define ASM_ERRATA_LIST_H
7 
8 #include <asm/csr.h>
9 #include <asm/insn-def.h>
10 #include <asm/hwcap.h>
11 #include <asm/vendorid_list.h>
12 #include <asm/errata_list_vendors.h>
13 #include <asm/vendor_extensions/mips.h>
14 
15 #ifdef __ASSEMBLER__
16 
17 #define ALT_INSN_FAULT(x)						\
18 ALTERNATIVE(__stringify(RISCV_PTR do_trap_insn_fault),			\
19 	    __stringify(RISCV_PTR sifive_cip_453_insn_fault_trp),	\
20 	    SIFIVE_VENDOR_ID, ERRATA_SIFIVE_CIP_453,			\
21 	    CONFIG_ERRATA_SIFIVE_CIP_453)
22 
23 #define ALT_PAGE_FAULT(x)						\
24 ALTERNATIVE(__stringify(RISCV_PTR do_page_fault),			\
25 	    __stringify(RISCV_PTR sifive_cip_453_page_fault_trp),	\
26 	    SIFIVE_VENDOR_ID, ERRATA_SIFIVE_CIP_453,			\
27 	    CONFIG_ERRATA_SIFIVE_CIP_453)
28 
29 #ifdef CONFIG_ERRATA_MIPS_P8700_WFI
30 #define ALT_WFI								\
31 ALTERNATIVE("wfi; .rept 7; nop; .endr;",				\
32 	".rept 8; .4byte 0x00501013; .endr;", MIPS_VENDOR_ID,		\
33 	ERRATA_MIPS_P8700_WFI, CONFIG_ERRATA_MIPS_P8700_WFI)
34 #else
35 #define ALT_WFI	wfi
36 #endif
37 
38 #else /* !__ASSEMBLER__ */
39 
40 #define ALT_SFENCE_VMA_ASID(asid)					\
41 asm(ALTERNATIVE("sfence.vma x0, %0", "sfence.vma", SIFIVE_VENDOR_ID,	\
42 		ERRATA_SIFIVE_CIP_1200, CONFIG_ERRATA_SIFIVE_CIP_1200)	\
43 		: : "r" (asid) : "memory")
44 
45 #define ALT_SFENCE_VMA_ADDR(addr)					\
46 asm(ALTERNATIVE("sfence.vma %0", "sfence.vma", SIFIVE_VENDOR_ID,	\
47 		ERRATA_SIFIVE_CIP_1200, CONFIG_ERRATA_SIFIVE_CIP_1200)	\
48 		: : "r" (addr) : "memory")
49 
50 #define ALT_SFENCE_VMA_ADDR_ASID(addr, asid)				\
51 asm(ALTERNATIVE("sfence.vma %0, %1", "sfence.vma", SIFIVE_VENDOR_ID,	\
52 		ERRATA_SIFIVE_CIP_1200, CONFIG_ERRATA_SIFIVE_CIP_1200)	\
53 		: : "r" (addr), "r" (asid) : "memory")
54 
55 #define ALT_RISCV_PAUSE()					\
56 asm(ALTERNATIVE(	\
57 		RISCV_PAUSE, /* Original RISC‑V pause insn */	\
58 		MIPS_PAUSE, /* Replacement for MIPS P8700 */	\
59 		MIPS_VENDOR_ID, /* Vendor ID to match */	\
60 		ERRATA_MIPS_P8700_PAUSE_OPCODE, /* patch_id */	\
61 		CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE)	\
62 	: /* no outputs */	\
63 	: /* no inputs */	\
64 	: "memory")
65 
66 #ifdef CONFIG_ERRATA_MIPS_P8700_WFI
67 /*
68  * A WFI instruction after a mispredicted branch can result in erroneous address translation
69  * on the MIPS P8700.
70  * The number of MIPS_PAUSE instructions required was determined by measuring
71  * Coremark performance on a one-core, two-hart configuration. Eight PAUSE instructions matched
72  * the latency of a single WFI instruction.
73  * Usually, the maximum number of cycles per PAUSE instruction is 63. Exceptionally, if the hart's
74  * LR reservation bit is set, the maximum number of cycles is extended to 255
75  * (and the hart is woken up once it is cleared). This case should not affect the patched sites.
76  */
77 #define ALT_RISCV_WFI()										\
78 asm volatile(ALTERNATIVE(									\
79 		"wfi\n" /* Original RISC-V wfi insn */						\
80 		__nops(7),									\
81 		".rept 8;" MIPS_PAUSE ".endr;\n", /* Replacement: mips.pause for P8700 */	\
82 		MIPS_VENDOR_ID, /* Vendor ID to match */					\
83 		ERRATA_MIPS_P8700_WFI, /* patch_id */						\
84 		CONFIG_ERRATA_MIPS_P8700_WFI)							\
85 		: /* no outputs */								\
86 		: /* no inputs */								\
87 		: "memory")
88 #else
89 #define ALT_RISCV_WFI()		\
90 	__asm__ __volatile__ ("wfi")
91 #endif
92 
93 /*
94  * _val is marked as "will be overwritten", so need to set it to 0
95  * in the default case.
96  */
97 #define ALT_SVPBMT_SHIFT 61
98 #define ALT_THEAD_MAE_SHIFT 59
99 #define ALT_SVPBMT(_val, prot)						\
100 asm(ALTERNATIVE_2("li %0, 0\t\nnop",					\
101 		  "li %0, %1\t\nslli %0,%0,%3", 0,			\
102 			RISCV_ISA_EXT_SVPBMT, CONFIG_RISCV_ISA_SVPBMT,	\
103 		  "li %0, %2\t\nslli %0,%0,%4", THEAD_VENDOR_ID,	\
104 			ERRATA_THEAD_MAE, CONFIG_ERRATA_THEAD_MAE)	\
105 		: "=r"(_val)						\
106 		: "I"(prot##_SVPBMT >> ALT_SVPBMT_SHIFT),		\
107 		  "I"(prot##_THEAD >> ALT_THEAD_MAE_SHIFT),		\
108 		  "I"(ALT_SVPBMT_SHIFT),				\
109 		  "I"(ALT_THEAD_MAE_SHIFT))
110 
111 #ifdef CONFIG_ERRATA_THEAD_MAE
112 /*
113  * IO/NOCACHE memory types are handled together with svpbmt,
114  * so on T-Head chips, check if no other memory type is set,
115  * and set the non-0 PMA type if applicable.
116  */
117 #define ALT_THEAD_PMA(_val)						\
118 asm volatile(ALTERNATIVE(						\
119 	__nops(7),							\
120 	"li      t3, %1\n\t"						\
121 	"slli    t3, t3, %3\n\t"					\
122 	"and     t3, %0, t3\n\t"					\
123 	"bne     t3, zero, 2f\n\t"					\
124 	"li      t3, %2\n\t"						\
125 	"slli    t3, t3, %3\n\t"					\
126 	"or      %0, %0, t3\n\t"					\
127 	"2:",  THEAD_VENDOR_ID,						\
128 		ERRATA_THEAD_MAE, CONFIG_ERRATA_THEAD_MAE)		\
129 	: "+r"(_val)							\
130 	: "I"(_PAGE_MTMASK_THEAD >> ALT_THEAD_MAE_SHIFT),		\
131 	  "I"(_PAGE_PMA_THEAD >> ALT_THEAD_MAE_SHIFT),			\
132 	  "I"(ALT_THEAD_MAE_SHIFT)					\
133 	: "t3")
134 #else
135 #define ALT_THEAD_PMA(_val)
136 #endif
137 
138 #define ALT_CMO_OP(_op, _start, _size, _cachesize)			\
139 asm volatile(ALTERNATIVE(						\
140 	__nops(5),							\
141 	"mv a0, %1\n\t"							\
142 	"j 2f\n\t"							\
143 	"3:\n\t"							\
144 	CBO_##_op(a0)							\
145 	"add a0, a0, %0\n\t"						\
146 	"2:\n\t"							\
147 	"bltu a0, %2, 3b\n\t",						\
148 	0, RISCV_ISA_EXT_ZICBOM, CONFIG_RISCV_ISA_ZICBOM)		\
149 	: : "r"(_cachesize),						\
150 	    "r"((unsigned long)(_start) & ~((_cachesize) - 1UL)),	\
151 	    "r"((unsigned long)(_start) + (_size))			\
152 	: "a0")
153 
154 #define THEAD_C9XX_RV_IRQ_PMU			17
155 #define THEAD_C9XX_CSR_SCOUNTEROF		0x5c5
156 
157 #endif /* __ASSEMBLER__ */
158 
159 #endif
160