xref: /linux/arch/riscv/errata/mips/errata.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2025 MIPS.
4  */
5 
6 #include <linux/memory.h>
7 #include <linux/module.h>
8 #include <asm/text-patching.h>
9 #include <asm/alternative.h>
10 #include <asm/cacheflush.h>
11 #include <asm/errata_list.h>
12 #include <asm/vendorid_list.h>
13 #include <asm/vendor_extensions.h>
14 #include <asm/vendor_extensions/mips.h>
15 
16 static inline bool errata_probe_pause(unsigned int stage)
17 {
18 	if (!IS_ENABLED(CONFIG_ERRATA_MIPS_P8700_PAUSE_OPCODE))
19 		return false;
20 
21 	if (!riscv_isa_vendor_extension_available(MIPS_VENDOR_ID, XMIPSEXECTL))
22 		return false;
23 
24 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
25 		return false;
26 
27 	return true;
28 }
29 
30 static inline bool errata_probe_wfi(unsigned int stage)
31 {
32 	if (!IS_ENABLED(CONFIG_ERRATA_MIPS_P8700_WFI))
33 		return false;
34 
35 	if (!riscv_isa_vendor_extension_available(MIPS_VENDOR_ID, XMIPSEXECTL))
36 		return false;
37 
38 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
39 		return false;
40 
41 	return true;
42 }
43 
44 static u32 mips_errata_probe(unsigned int stage)
45 {
46 	u32 cpu_req_errata = 0;
47 
48 	if (errata_probe_pause(stage))
49 		cpu_req_errata |= BIT(ERRATA_MIPS_P8700_PAUSE_OPCODE);
50 
51 	if (errata_probe_wfi(stage))
52 		cpu_req_errata |= BIT(ERRATA_MIPS_P8700_WFI);
53 
54 	return cpu_req_errata;
55 }
56 
57 void mips_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
58 			    unsigned long archid, unsigned long impid,
59 			    unsigned int stage)
60 {
61 	struct alt_entry *alt;
62 	u32 cpu_req_errata = mips_errata_probe(stage);
63 	u32 tmp;
64 	void *oldptr, *altptr;
65 
66 	BUILD_BUG_ON(ERRATA_MIPS_NUMBER >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE);
67 
68 	for (alt = begin; alt < end; alt++) {
69 		if (alt->vendor_id != MIPS_VENDOR_ID)
70 			continue;
71 
72 		if (alt->patch_id >= ERRATA_MIPS_NUMBER)
73 			continue;
74 
75 		tmp = (1U << alt->patch_id);
76 		if (cpu_req_errata & tmp) {
77 			oldptr = ALT_OLD_PTR(alt);
78 			altptr = ALT_ALT_PTR(alt);
79 
80 			if (stage == RISCV_ALTERNATIVES_EARLY_BOOT) {
81 				memcpy(oldptr, altptr, alt->alt_len);
82 			} else {
83 				mutex_lock(&text_mutex);
84 				patch_text_nosync(oldptr, altptr, alt->alt_len);
85 				mutex_unlock(&text_mutex);
86 			}
87 		}
88 	}
89 
90 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
91 		local_flush_icache_all();
92 }
93