1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2021 Heiko Stuebner <heiko@sntech.de> 4 */ 5 6 #include <linux/bug.h> 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> 16 17 static bool errata_probe_pbmt(unsigned int stage, 18 unsigned long arch_id, unsigned long impid) 19 { 20 if (!IS_ENABLED(CONFIG_ERRATA_THEAD_PBMT)) 21 return false; 22 23 if (arch_id != 0 || impid != 0) 24 return false; 25 26 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT || 27 stage == RISCV_ALTERNATIVES_MODULE) 28 return true; 29 30 return false; 31 } 32 33 static bool errata_probe_cmo(unsigned int stage, 34 unsigned long arch_id, unsigned long impid) 35 { 36 if (!IS_ENABLED(CONFIG_ERRATA_THEAD_CMO)) 37 return false; 38 39 if (arch_id != 0 || impid != 0) 40 return false; 41 42 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT) 43 return false; 44 45 riscv_cbom_block_size = L1_CACHE_BYTES; 46 riscv_noncoherent_supported(); 47 return true; 48 } 49 50 static bool errata_probe_pmu(unsigned int stage, 51 unsigned long arch_id, unsigned long impid) 52 { 53 if (!IS_ENABLED(CONFIG_ERRATA_THEAD_PMU)) 54 return false; 55 56 /* target-c9xx cores report arch_id and impid as 0 */ 57 if (arch_id != 0 || impid != 0) 58 return false; 59 60 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT) 61 return false; 62 63 return true; 64 } 65 66 static u32 thead_errata_probe(unsigned int stage, 67 unsigned long archid, unsigned long impid) 68 { 69 u32 cpu_req_errata = 0; 70 71 if (errata_probe_pbmt(stage, archid, impid)) 72 cpu_req_errata |= BIT(ERRATA_THEAD_PBMT); 73 74 if (errata_probe_cmo(stage, archid, impid)) 75 cpu_req_errata |= BIT(ERRATA_THEAD_CMO); 76 77 if (errata_probe_pmu(stage, archid, impid)) 78 cpu_req_errata |= BIT(ERRATA_THEAD_PMU); 79 80 return cpu_req_errata; 81 } 82 83 void __init_or_module thead_errata_patch_func(struct alt_entry *begin, struct alt_entry *end, 84 unsigned long archid, unsigned long impid, 85 unsigned int stage) 86 { 87 struct alt_entry *alt; 88 u32 cpu_req_errata = thead_errata_probe(stage, archid, impid); 89 u32 tmp; 90 91 for (alt = begin; alt < end; alt++) { 92 if (alt->vendor_id != THEAD_VENDOR_ID) 93 continue; 94 if (alt->errata_id >= ERRATA_THEAD_NUMBER) 95 continue; 96 97 tmp = (1U << alt->errata_id); 98 if (cpu_req_errata & tmp) { 99 /* On vm-alternatives, the mmu isn't running yet */ 100 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT) 101 memcpy((void *)__pa_symbol(alt->old_ptr), 102 (void *)__pa_symbol(alt->alt_ptr), alt->alt_len); 103 else 104 patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len); 105 } 106 } 107 108 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT) 109 local_flush_icache_all(); 110 } 111