1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021 Sifive.
6 #include <linux/kernel.h>
7 #include <linux/memory.h>
8 #include <linux/module.h>
9 #include <linux/string.h>
10 #include <linux/bug.h>
11 #include <asm/patch.h>
12 #include <asm/alternative.h>
13 #include <asm/vendorid_list.h>
14 #include <asm/errata_list.h>
16 struct errata_info_t {
18 bool (*check_func)(unsigned long arch_id, unsigned long impid);
21 static bool errata_cip_453_check_func(unsigned long arch_id, unsigned long impid)
25 * Architecture ID: 0x8000000000000007
26 * Implement ID: 0x20181004 <= impid <= 0x20191105
28 if (arch_id != 0x8000000000000007 ||
29 (impid < 0x20181004 || impid > 0x20191105))
34 static bool errata_cip_1200_check_func(unsigned long arch_id, unsigned long impid)
38 * Architecture ID: 0x8000000000000007 or 0x1
39 * Implement ID: mimpid[23:0] <= 0x200630 and mimpid != 0x01200626
41 if (arch_id != 0x8000000000000007 && arch_id != 0x1)
43 if ((impid & 0xffffff) > 0x200630 || impid == 0x1200626)
48 static struct errata_info_t errata_list[ERRATA_SIFIVE_NUMBER] = {
51 .check_func = errata_cip_453_check_func
55 .check_func = errata_cip_1200_check_func
59 static u32 __init_or_module sifive_errata_probe(unsigned long archid,
63 u32 cpu_req_errata = 0;
65 for (idx = 0; idx < ERRATA_SIFIVE_NUMBER; idx++)
66 if (errata_list[idx].check_func(archid, impid))
67 cpu_req_errata |= (1U << idx);
69 return cpu_req_errata;
72 static void __init_or_module warn_miss_errata(u32 miss_errata)
76 pr_warn("----------------------------------------------------------------\n");
77 pr_warn("WARNING: Missing the following errata may cause potential issues\n");
78 for (i = 0; i < ERRATA_SIFIVE_NUMBER; i++)
79 if (miss_errata & 0x1 << i)
80 pr_warn("\tSiFive Errata[%d]:%s\n", i, errata_list[i].name);
81 pr_warn("Please enable the corresponding Kconfig to apply them\n");
82 pr_warn("----------------------------------------------------------------\n");
85 void sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
86 unsigned long archid, unsigned long impid,
89 struct alt_entry *alt;
91 u32 cpu_apply_errata = 0;
94 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
97 cpu_req_errata = sifive_errata_probe(archid, impid);
99 for (alt = begin; alt < end; alt++) {
100 if (alt->vendor_id != SIFIVE_VENDOR_ID)
102 if (alt->patch_id >= ERRATA_SIFIVE_NUMBER) {
103 WARN(1, "This errata id:%d is not in kernel errata list", alt->patch_id);
107 tmp = (1U << alt->patch_id);
108 if (cpu_req_errata & tmp) {
109 mutex_lock(&text_mutex);
110 patch_text_nosync(ALT_OLD_PTR(alt), ALT_ALT_PTR(alt),
112 mutex_unlock(&text_mutex);
113 cpu_apply_errata |= tmp;
116 if (stage != RISCV_ALTERNATIVES_MODULE &&
117 cpu_apply_errata != cpu_req_errata)
118 warn_miss_errata(cpu_req_errata - cpu_apply_errata);