xref: /linux/arch/riscv/errata/sifive/errata.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2021 Sifive.
4  */
5 
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>
15 
16 struct errata_info_t {
17 	char name[32];
18 	bool (*check_func)(unsigned long  arch_id, unsigned long impid);
19 };
20 
21 static bool errata_cip_453_check_func(unsigned long  arch_id, unsigned long impid)
22 {
23 	/*
24 	 * Affected cores:
25 	 * Architecture ID: 0x8000000000000007
26 	 * Implement ID: 0x20181004 <= impid <= 0x20191105
27 	 */
28 	if (arch_id != 0x8000000000000007 ||
29 	    (impid < 0x20181004 || impid > 0x20191105))
30 		return false;
31 	return true;
32 }
33 
34 static bool errata_cip_1200_check_func(unsigned long  arch_id, unsigned long impid)
35 {
36 	/*
37 	 * Affected cores:
38 	 * Architecture ID: 0x8000000000000007 or 0x1
39 	 * Implement ID: mimpid[23:0] <= 0x200630 and mimpid != 0x01200626
40 	 */
41 	if (arch_id != 0x8000000000000007 && arch_id != 0x1)
42 		return false;
43 	if ((impid & 0xffffff) > 0x200630 || impid == 0x1200626)
44 		return false;
45 	return true;
46 }
47 
48 static struct errata_info_t errata_list[ERRATA_SIFIVE_NUMBER] = {
49 	{
50 		.name = "cip-453",
51 		.check_func = errata_cip_453_check_func
52 	},
53 	{
54 		.name = "cip-1200",
55 		.check_func = errata_cip_1200_check_func
56 	},
57 };
58 
59 static u32 __init_or_module sifive_errata_probe(unsigned long archid,
60 						unsigned long impid)
61 {
62 	int idx;
63 	u32 cpu_req_errata = 0;
64 
65 	for (idx = 0; idx < ERRATA_SIFIVE_NUMBER; idx++)
66 		if (errata_list[idx].check_func(archid, impid))
67 			cpu_req_errata |= (1U << idx);
68 
69 	return cpu_req_errata;
70 }
71 
72 static void __init_or_module warn_miss_errata(u32 miss_errata)
73 {
74 	int i;
75 
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");
83 }
84 
85 void sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
86 			      unsigned long archid, unsigned long impid,
87 			      unsigned int stage)
88 {
89 	struct alt_entry *alt;
90 	u32 cpu_req_errata;
91 	u32 cpu_apply_errata = 0;
92 	u32 tmp;
93 
94 	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
95 		return;
96 
97 	cpu_req_errata = sifive_errata_probe(archid, impid);
98 
99 	for (alt = begin; alt < end; alt++) {
100 		if (alt->vendor_id != SIFIVE_VENDOR_ID)
101 			continue;
102 		if (alt->patch_id >= ERRATA_SIFIVE_NUMBER) {
103 			WARN(1, "This errata id:%d is not in kernel errata list", alt->patch_id);
104 			continue;
105 		}
106 
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),
111 					  alt->alt_len);
112 			mutex_unlock(&text_mutex);
113 			cpu_apply_errata |= tmp;
114 		}
115 	}
116 	if (stage != RISCV_ALTERNATIVES_MODULE &&
117 	    cpu_apply_errata != cpu_req_errata)
118 		warn_miss_errata(cpu_req_errata - cpu_apply_errata);
119 }
120