1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021 Heiko Stuebner <heiko@sntech.de>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/string.h>
10 #include <linux/uaccess.h>
11 #include <asm/alternative.h>
12 #include <asm/cacheflush.h>
13 #include <asm/errata_list.h>
14 #include <asm/patch.h>
15 #include <asm/vendorid_list.h>
18 char name[ERRATA_STRING_LENGTH_MAX];
19 bool (*check_func)(unsigned long arch_id, unsigned long impid);
23 static bool errata_mt_check_func(unsigned long arch_id, unsigned long impid)
25 if (arch_id != 0 || impid != 0)
30 static const struct errata_info errata_list[ERRATA_THEAD_NUMBER] = {
32 .name = "memory-types",
33 .stage = RISCV_ALTERNATIVES_EARLY_BOOT,
34 .check_func = errata_mt_check_func
38 static u32 thead_errata_probe(unsigned int stage, unsigned long archid, unsigned long impid)
40 const struct errata_info *info;
41 u32 cpu_req_errata = 0;
44 for (idx = 0; idx < ERRATA_THEAD_NUMBER; idx++) {
45 info = &errata_list[idx];
47 if ((stage == RISCV_ALTERNATIVES_MODULE ||
48 info->stage == stage) && info->check_func(archid, impid))
49 cpu_req_errata |= (1U << idx);
52 return cpu_req_errata;
55 void __init_or_module thead_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
56 unsigned long archid, unsigned long impid,
59 struct alt_entry *alt;
60 u32 cpu_req_errata = thead_errata_probe(stage, archid, impid);
63 for (alt = begin; alt < end; alt++) {
64 if (alt->vendor_id != THEAD_VENDOR_ID)
66 if (alt->errata_id >= ERRATA_THEAD_NUMBER)
69 tmp = (1U << alt->errata_id);
70 if (cpu_req_errata & tmp) {
71 /* On vm-alternatives, the mmu isn't running yet */
72 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
73 memcpy((void *)__pa_symbol(alt->old_ptr),
74 (void *)__pa_symbol(alt->alt_ptr), alt->alt_len);
76 patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
80 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
81 local_flush_icache_all();