xref: /openbmc/linux/arch/riscv/errata/sifive/errata.c (revision caa80275)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2021 Sifive.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/bug.h>
9 #include <asm/patch.h>
10 #include <asm/alternative.h>
11 #include <asm/vendorid_list.h>
12 #include <asm/errata_list.h>
13 
14 struct errata_info_t {
15 	char name[ERRATA_STRING_LENGTH_MAX];
16 	bool (*check_func)(unsigned long  arch_id, unsigned long impid);
17 };
18 
19 static bool errata_cip_453_check_func(unsigned long  arch_id, unsigned long impid)
20 {
21 	/*
22 	 * Affected cores:
23 	 * Architecture ID: 0x8000000000000007
24 	 * Implement ID: 0x20181004 <= impid <= 0x20191105
25 	 */
26 	if (arch_id != 0x8000000000000007 ||
27 	    (impid < 0x20181004 || impid > 0x20191105))
28 		return false;
29 	return true;
30 }
31 
32 static bool errata_cip_1200_check_func(unsigned long  arch_id, unsigned long impid)
33 {
34 	/*
35 	 * Affected cores:
36 	 * Architecture ID: 0x8000000000000007 or 0x1
37 	 * Implement ID: mimpid[23:0] <= 0x200630 and mimpid != 0x01200626
38 	 */
39 	if (arch_id != 0x8000000000000007 && arch_id != 0x1)
40 		return false;
41 	if ((impid & 0xffffff) > 0x200630 || impid == 0x1200626)
42 		return false;
43 	return true;
44 }
45 
46 static struct errata_info_t errata_list[ERRATA_SIFIVE_NUMBER] = {
47 	{
48 		.name = "cip-453",
49 		.check_func = errata_cip_453_check_func
50 	},
51 	{
52 		.name = "cip-1200",
53 		.check_func = errata_cip_1200_check_func
54 	},
55 };
56 
57 static u32 __init sifive_errata_probe(unsigned long archid, unsigned long impid)
58 {
59 	int idx;
60 	u32 cpu_req_errata = 0;
61 
62 	for (idx = 0; idx < ERRATA_SIFIVE_NUMBER; idx++)
63 		if (errata_list[idx].check_func(archid, impid))
64 			cpu_req_errata |= (1U << idx);
65 
66 	return cpu_req_errata;
67 }
68 
69 static void __init warn_miss_errata(u32 miss_errata)
70 {
71 	int i;
72 
73 	pr_warn("----------------------------------------------------------------\n");
74 	pr_warn("WARNING: Missing the following errata may cause potential issues\n");
75 	for (i = 0; i < ERRATA_SIFIVE_NUMBER; i++)
76 		if (miss_errata & 0x1 << i)
77 			pr_warn("\tSiFive Errata[%d]:%s\n", i, errata_list[i].name);
78 	pr_warn("Please enable the corresponding Kconfig to apply them\n");
79 	pr_warn("----------------------------------------------------------------\n");
80 }
81 
82 void __init sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
83 				     unsigned long archid, unsigned long impid)
84 {
85 	struct alt_entry *alt;
86 	u32 cpu_req_errata = sifive_errata_probe(archid, impid);
87 	u32 cpu_apply_errata = 0;
88 	u32 tmp;
89 
90 	for (alt = begin; alt < end; alt++) {
91 		if (alt->vendor_id != SIFIVE_VENDOR_ID)
92 			continue;
93 		if (alt->errata_id >= ERRATA_SIFIVE_NUMBER) {
94 			WARN(1, "This errata id:%d is not in kernel errata list", alt->errata_id);
95 			continue;
96 		}
97 
98 		tmp = (1U << alt->errata_id);
99 		if (cpu_req_errata & tmp) {
100 			patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
101 			cpu_apply_errata |= tmp;
102 		}
103 	}
104 	if (cpu_apply_errata != cpu_req_errata)
105 		warn_miss_errata(cpu_req_errata - cpu_apply_errata);
106 }
107