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 (arch_id != 0 || impid != 0) 21 return false; 22 23 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT || 24 stage == RISCV_ALTERNATIVES_MODULE) 25 return true; 26 27 return false; 28 } 29 30 static bool errata_probe_cmo(unsigned int stage, 31 unsigned long arch_id, unsigned long impid) 32 { 33 if (!IS_ENABLED(CONFIG_ERRATA_THEAD_CMO)) 34 return false; 35 36 if (arch_id != 0 || impid != 0) 37 return false; 38 39 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT) 40 return false; 41 42 riscv_noncoherent_supported(); 43 return true; 44 } 45 46 static u32 thead_errata_probe(unsigned int stage, 47 unsigned long archid, unsigned long impid) 48 { 49 u32 cpu_req_errata = 0; 50 51 if (errata_probe_pbmt(stage, archid, impid)) 52 cpu_req_errata |= (1U << ERRATA_THEAD_PBMT); 53 54 if (errata_probe_cmo(stage, archid, impid)) 55 cpu_req_errata |= (1U << ERRATA_THEAD_CMO); 56 57 return cpu_req_errata; 58 } 59 60 void __init_or_module thead_errata_patch_func(struct alt_entry *begin, struct alt_entry *end, 61 unsigned long archid, unsigned long impid, 62 unsigned int stage) 63 { 64 struct alt_entry *alt; 65 u32 cpu_req_errata = thead_errata_probe(stage, archid, impid); 66 u32 tmp; 67 68 for (alt = begin; alt < end; alt++) { 69 if (alt->vendor_id != THEAD_VENDOR_ID) 70 continue; 71 if (alt->errata_id >= ERRATA_THEAD_NUMBER) 72 continue; 73 74 tmp = (1U << alt->errata_id); 75 if (cpu_req_errata & tmp) { 76 /* On vm-alternatives, the mmu isn't running yet */ 77 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT) 78 memcpy((void *)__pa_symbol(alt->old_ptr), 79 (void *)__pa_symbol(alt->alt_ptr), alt->alt_len); 80 else 81 patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len); 82 } 83 } 84 85 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT) 86 local_flush_icache_all(); 87 } 88