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>
17 static bool errata_probe_pbmt(unsigned int stage,
18 unsigned long arch_id, unsigned long impid)
20 if (!IS_ENABLED(CONFIG_ERRATA_THEAD_PBMT))
23 if (arch_id != 0 || impid != 0)
26 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT ||
27 stage == RISCV_ALTERNATIVES_MODULE)
33 static bool errata_probe_cmo(unsigned int stage,
34 unsigned long arch_id, unsigned long impid)
36 if (!IS_ENABLED(CONFIG_ERRATA_THEAD_CMO))
39 if (arch_id != 0 || impid != 0)
42 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
45 riscv_cbom_block_size = L1_CACHE_BYTES;
46 riscv_noncoherent_supported();
50 static u32 thead_errata_probe(unsigned int stage,
51 unsigned long archid, unsigned long impid)
53 u32 cpu_req_errata = 0;
55 if (errata_probe_pbmt(stage, archid, impid))
56 cpu_req_errata |= BIT(ERRATA_THEAD_PBMT);
58 if (errata_probe_cmo(stage, archid, impid))
59 cpu_req_errata |= BIT(ERRATA_THEAD_CMO);
61 return cpu_req_errata;
64 void __init_or_module thead_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
65 unsigned long archid, unsigned long impid,
68 struct alt_entry *alt;
69 u32 cpu_req_errata = thead_errata_probe(stage, archid, impid);
72 for (alt = begin; alt < end; alt++) {
73 if (alt->vendor_id != THEAD_VENDOR_ID)
75 if (alt->errata_id >= ERRATA_THEAD_NUMBER)
78 tmp = (1U << alt->errata_id);
79 if (cpu_req_errata & tmp) {
80 /* On vm-alternatives, the mmu isn't running yet */
81 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
82 memcpy((void *)__pa_symbol(alt->old_ptr),
83 (void *)__pa_symbol(alt->alt_ptr), alt->alt_len);
85 patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len);
89 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
90 local_flush_icache_all();