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