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/memory.h>
9 #include <linux/module.h>
10 #include <linux/string.h>
11 #include <linux/uaccess.h>
12 #include <asm/alternative.h>
13 #include <asm/cacheflush.h>
14 #include <asm/cpufeature.h>
15 #include <asm/errata_list.h>
16 #include <asm/hwprobe.h>
17 #include <asm/patch.h>
18 #include <asm/vendorid_list.h>
20 static bool errata_probe_pbmt(unsigned int stage,
21 unsigned long arch_id, unsigned long impid)
23 if (!IS_ENABLED(CONFIG_ERRATA_THEAD_PBMT))
26 if (arch_id != 0 || impid != 0)
29 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT ||
30 stage == RISCV_ALTERNATIVES_MODULE)
36 static bool errata_probe_cmo(unsigned int stage,
37 unsigned long arch_id, unsigned long impid)
39 if (!IS_ENABLED(CONFIG_ERRATA_THEAD_CMO))
42 if (arch_id != 0 || impid != 0)
45 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
48 if (stage == RISCV_ALTERNATIVES_BOOT) {
49 riscv_cbom_block_size = L1_CACHE_BYTES;
50 riscv_noncoherent_supported();
56 static bool errata_probe_pmu(unsigned int stage,
57 unsigned long arch_id, unsigned long impid)
59 if (!IS_ENABLED(CONFIG_ERRATA_THEAD_PMU))
62 /* target-c9xx cores report arch_id and impid as 0 */
63 if (arch_id != 0 || impid != 0)
66 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
72 static u32 thead_errata_probe(unsigned int stage,
73 unsigned long archid, unsigned long impid)
75 u32 cpu_req_errata = 0;
77 if (errata_probe_pbmt(stage, archid, impid))
78 cpu_req_errata |= BIT(ERRATA_THEAD_PBMT);
80 if (errata_probe_cmo(stage, archid, impid))
81 cpu_req_errata |= BIT(ERRATA_THEAD_CMO);
83 if (errata_probe_pmu(stage, archid, impid))
84 cpu_req_errata |= BIT(ERRATA_THEAD_PMU);
86 return cpu_req_errata;
89 void thead_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
90 unsigned long archid, unsigned long impid,
93 struct alt_entry *alt;
94 u32 cpu_req_errata = thead_errata_probe(stage, archid, impid);
96 void *oldptr, *altptr;
98 for (alt = begin; alt < end; alt++) {
99 if (alt->vendor_id != THEAD_VENDOR_ID)
101 if (alt->patch_id >= ERRATA_THEAD_NUMBER)
104 tmp = (1U << alt->patch_id);
105 if (cpu_req_errata & tmp) {
106 oldptr = ALT_OLD_PTR(alt);
107 altptr = ALT_ALT_PTR(alt);
109 /* On vm-alternatives, the mmu isn't running yet */
110 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT) {
111 memcpy(oldptr, altptr, alt->alt_len);
113 mutex_lock(&text_mutex);
114 patch_text_nosync(oldptr, altptr, alt->alt_len);
115 mutex_unlock(&text_mutex);
120 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
121 local_flush_icache_all();