11a0e5dbdSVincent Chen // SPDX-License-Identifier: GPL-2.0-only
21a0e5dbdSVincent Chen /*
31a0e5dbdSVincent Chen * Copyright (C) 2021 Sifive.
41a0e5dbdSVincent Chen */
51a0e5dbdSVincent Chen
61a0e5dbdSVincent Chen #include <linux/kernel.h>
79493e6f3SConor Dooley #include <linux/memory.h>
8a8e91016SHeiko Stuebner #include <linux/module.h>
91a0e5dbdSVincent Chen #include <linux/string.h>
101a0e5dbdSVincent Chen #include <linux/bug.h>
111a0e5dbdSVincent Chen #include <asm/patch.h>
121a0e5dbdSVincent Chen #include <asm/alternative.h>
131a0e5dbdSVincent Chen #include <asm/vendorid_list.h>
141a0e5dbdSVincent Chen #include <asm/errata_list.h>
15*23c996fcSCharlie Jenkins #include <asm/vendor_extensions.h>
161a0e5dbdSVincent Chen
171a0e5dbdSVincent Chen struct errata_info_t {
18ce06b42aSAndrew Jones char name[32];
191a0e5dbdSVincent Chen bool (*check_func)(unsigned long arch_id, unsigned long impid);
201a0e5dbdSVincent Chen };
211a0e5dbdSVincent Chen
errata_cip_453_check_func(unsigned long arch_id,unsigned long impid)22800149a7SVincent Chen static bool errata_cip_453_check_func(unsigned long arch_id, unsigned long impid)
23800149a7SVincent Chen {
24800149a7SVincent Chen /*
25800149a7SVincent Chen * Affected cores:
26800149a7SVincent Chen * Architecture ID: 0x8000000000000007
27800149a7SVincent Chen * Implement ID: 0x20181004 <= impid <= 0x20191105
28800149a7SVincent Chen */
29800149a7SVincent Chen if (arch_id != 0x8000000000000007 ||
30800149a7SVincent Chen (impid < 0x20181004 || impid > 0x20191105))
31800149a7SVincent Chen return false;
32800149a7SVincent Chen return true;
33800149a7SVincent Chen }
34800149a7SVincent Chen
errata_cip_1200_check_func(unsigned long arch_id,unsigned long impid)35bff3ff52SVincent Chen static bool errata_cip_1200_check_func(unsigned long arch_id, unsigned long impid)
36bff3ff52SVincent Chen {
37bff3ff52SVincent Chen /*
38bff3ff52SVincent Chen * Affected cores:
39bff3ff52SVincent Chen * Architecture ID: 0x8000000000000007 or 0x1
40bff3ff52SVincent Chen * Implement ID: mimpid[23:0] <= 0x200630 and mimpid != 0x01200626
41bff3ff52SVincent Chen */
42bff3ff52SVincent Chen if (arch_id != 0x8000000000000007 && arch_id != 0x1)
43bff3ff52SVincent Chen return false;
44bff3ff52SVincent Chen if ((impid & 0xffffff) > 0x200630 || impid == 0x1200626)
45bff3ff52SVincent Chen return false;
46d6dcdabaSSamuel Holland
47d6dcdabaSSamuel Holland #ifdef CONFIG_MMU
48d6dcdabaSSamuel Holland tlb_flush_all_threshold = 0;
49d6dcdabaSSamuel Holland #endif
50d6dcdabaSSamuel Holland
51bff3ff52SVincent Chen return true;
52bff3ff52SVincent Chen }
53bff3ff52SVincent Chen
54800149a7SVincent Chen static struct errata_info_t errata_list[ERRATA_SIFIVE_NUMBER] = {
55800149a7SVincent Chen {
56800149a7SVincent Chen .name = "cip-453",
57800149a7SVincent Chen .check_func = errata_cip_453_check_func
58800149a7SVincent Chen },
59bff3ff52SVincent Chen {
60bff3ff52SVincent Chen .name = "cip-1200",
61bff3ff52SVincent Chen .check_func = errata_cip_1200_check_func
62bff3ff52SVincent Chen },
63800149a7SVincent Chen };
64800149a7SVincent Chen
sifive_errata_probe(unsigned long archid,unsigned long impid)65a8e91016SHeiko Stuebner static u32 __init_or_module sifive_errata_probe(unsigned long archid,
66a8e91016SHeiko Stuebner unsigned long impid)
671a0e5dbdSVincent Chen {
681a0e5dbdSVincent Chen int idx;
691a0e5dbdSVincent Chen u32 cpu_req_errata = 0;
701a0e5dbdSVincent Chen
711a0e5dbdSVincent Chen for (idx = 0; idx < ERRATA_SIFIVE_NUMBER; idx++)
721a0e5dbdSVincent Chen if (errata_list[idx].check_func(archid, impid))
731a0e5dbdSVincent Chen cpu_req_errata |= (1U << idx);
741a0e5dbdSVincent Chen
751a0e5dbdSVincent Chen return cpu_req_errata;
761a0e5dbdSVincent Chen }
771a0e5dbdSVincent Chen
warn_miss_errata(u32 miss_errata)78a8e91016SHeiko Stuebner static void __init_or_module warn_miss_errata(u32 miss_errata)
791a0e5dbdSVincent Chen {
801a0e5dbdSVincent Chen int i;
811a0e5dbdSVincent Chen
821a0e5dbdSVincent Chen pr_warn("----------------------------------------------------------------\n");
831a0e5dbdSVincent Chen pr_warn("WARNING: Missing the following errata may cause potential issues\n");
841a0e5dbdSVincent Chen for (i = 0; i < ERRATA_SIFIVE_NUMBER; i++)
851a0e5dbdSVincent Chen if (miss_errata & 0x1 << i)
861a0e5dbdSVincent Chen pr_warn("\tSiFive Errata[%d]:%s\n", i, errata_list[i].name);
871a0e5dbdSVincent Chen pr_warn("Please enable the corresponding Kconfig to apply them\n");
881a0e5dbdSVincent Chen pr_warn("----------------------------------------------------------------\n");
891a0e5dbdSVincent Chen }
901a0e5dbdSVincent Chen
sifive_errata_patch_func(struct alt_entry * begin,struct alt_entry * end,unsigned long archid,unsigned long impid,unsigned int stage)91a2a58b5cSRandy Dunlap void sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
92a2a58b5cSRandy Dunlap unsigned long archid, unsigned long impid,
93d14ca1f8SHeiko Stuebner unsigned int stage)
941a0e5dbdSVincent Chen {
951a0e5dbdSVincent Chen struct alt_entry *alt;
96a35707c3SHeiko Stuebner u32 cpu_req_errata;
971a0e5dbdSVincent Chen u32 cpu_apply_errata = 0;
981a0e5dbdSVincent Chen u32 tmp;
991a0e5dbdSVincent Chen
100*23c996fcSCharlie Jenkins BUILD_BUG_ON(ERRATA_SIFIVE_NUMBER >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE);
101*23c996fcSCharlie Jenkins
102a35707c3SHeiko Stuebner if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
103a35707c3SHeiko Stuebner return;
104a35707c3SHeiko Stuebner
105a35707c3SHeiko Stuebner cpu_req_errata = sifive_errata_probe(archid, impid);
106a35707c3SHeiko Stuebner
1071a0e5dbdSVincent Chen for (alt = begin; alt < end; alt++) {
1081a0e5dbdSVincent Chen if (alt->vendor_id != SIFIVE_VENDOR_ID)
1091a0e5dbdSVincent Chen continue;
110ff19a8deSAndrew Jones if (alt->patch_id >= ERRATA_SIFIVE_NUMBER) {
111ff19a8deSAndrew Jones WARN(1, "This errata id:%d is not in kernel errata list", alt->patch_id);
1121a0e5dbdSVincent Chen continue;
1131a0e5dbdSVincent Chen }
1141a0e5dbdSVincent Chen
115ff19a8deSAndrew Jones tmp = (1U << alt->patch_id);
1161a0e5dbdSVincent Chen if (cpu_req_errata & tmp) {
1179493e6f3SConor Dooley mutex_lock(&text_mutex);
1188d23e94aSJisheng Zhang patch_text_nosync(ALT_OLD_PTR(alt), ALT_ALT_PTR(alt),
1198d23e94aSJisheng Zhang alt->alt_len);
120bf89b7eeSConor Dooley mutex_unlock(&text_mutex);
1211a0e5dbdSVincent Chen cpu_apply_errata |= tmp;
1221a0e5dbdSVincent Chen }
1231a0e5dbdSVincent Chen }
124dc5cb7a8SHeiko Stuebner if (stage != RISCV_ALTERNATIVES_MODULE &&
125dc5cb7a8SHeiko Stuebner cpu_apply_errata != cpu_req_errata)
1261a0e5dbdSVincent Chen warn_miss_errata(cpu_req_errata - cpu_apply_errata);
1271a0e5dbdSVincent Chen }
128