1*1a0e5dbdSVincent Chen // SPDX-License-Identifier: GPL-2.0-only 2*1a0e5dbdSVincent Chen /* 3*1a0e5dbdSVincent Chen * Copyright (C) 2021 Sifive. 4*1a0e5dbdSVincent Chen */ 5*1a0e5dbdSVincent Chen 6*1a0e5dbdSVincent Chen #include <linux/kernel.h> 7*1a0e5dbdSVincent Chen #include <linux/string.h> 8*1a0e5dbdSVincent Chen #include <linux/bug.h> 9*1a0e5dbdSVincent Chen #include <asm/patch.h> 10*1a0e5dbdSVincent Chen #include <asm/alternative.h> 11*1a0e5dbdSVincent Chen #include <asm/vendorid_list.h> 12*1a0e5dbdSVincent Chen #include <asm/errata_list.h> 13*1a0e5dbdSVincent Chen 14*1a0e5dbdSVincent Chen struct errata_info_t { 15*1a0e5dbdSVincent Chen char name[ERRATA_STRING_LENGTH_MAX]; 16*1a0e5dbdSVincent Chen bool (*check_func)(unsigned long arch_id, unsigned long impid); 17*1a0e5dbdSVincent Chen }; 18*1a0e5dbdSVincent Chen 19*1a0e5dbdSVincent Chen static u32 __init sifive_errata_probe(unsigned long archid, unsigned long impid) 20*1a0e5dbdSVincent Chen { 21*1a0e5dbdSVincent Chen int idx; 22*1a0e5dbdSVincent Chen u32 cpu_req_errata = 0; 23*1a0e5dbdSVincent Chen 24*1a0e5dbdSVincent Chen for (idx = 0; idx < ERRATA_SIFIVE_NUMBER; idx++) 25*1a0e5dbdSVincent Chen if (errata_list[idx].check_func(archid, impid)) 26*1a0e5dbdSVincent Chen cpu_req_errata |= (1U << idx); 27*1a0e5dbdSVincent Chen 28*1a0e5dbdSVincent Chen return cpu_req_errata; 29*1a0e5dbdSVincent Chen } 30*1a0e5dbdSVincent Chen 31*1a0e5dbdSVincent Chen static void __init warn_miss_errata(u32 miss_errata) 32*1a0e5dbdSVincent Chen { 33*1a0e5dbdSVincent Chen int i; 34*1a0e5dbdSVincent Chen 35*1a0e5dbdSVincent Chen pr_warn("----------------------------------------------------------------\n"); 36*1a0e5dbdSVincent Chen pr_warn("WARNING: Missing the following errata may cause potential issues\n"); 37*1a0e5dbdSVincent Chen for (i = 0; i < ERRATA_SIFIVE_NUMBER; i++) 38*1a0e5dbdSVincent Chen if (miss_errata & 0x1 << i) 39*1a0e5dbdSVincent Chen pr_warn("\tSiFive Errata[%d]:%s\n", i, errata_list[i].name); 40*1a0e5dbdSVincent Chen pr_warn("Please enable the corresponding Kconfig to apply them\n"); 41*1a0e5dbdSVincent Chen pr_warn("----------------------------------------------------------------\n"); 42*1a0e5dbdSVincent Chen } 43*1a0e5dbdSVincent Chen 44*1a0e5dbdSVincent Chen void __init sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end, 45*1a0e5dbdSVincent Chen unsigned long archid, unsigned long impid) 46*1a0e5dbdSVincent Chen { 47*1a0e5dbdSVincent Chen struct alt_entry *alt; 48*1a0e5dbdSVincent Chen u32 cpu_req_errata = sifive_errata_probe(archid, impid); 49*1a0e5dbdSVincent Chen u32 cpu_apply_errata = 0; 50*1a0e5dbdSVincent Chen u32 tmp; 51*1a0e5dbdSVincent Chen 52*1a0e5dbdSVincent Chen for (alt = begin; alt < end; alt++) { 53*1a0e5dbdSVincent Chen if (alt->vendor_id != SIFIVE_VENDOR_ID) 54*1a0e5dbdSVincent Chen continue; 55*1a0e5dbdSVincent Chen if (alt->errata_id >= ERRATA_SIFIVE_NUMBER) { 56*1a0e5dbdSVincent Chen WARN(1, "This errata id:%d is not in kernel errata list", alt->errata_id); 57*1a0e5dbdSVincent Chen continue; 58*1a0e5dbdSVincent Chen } 59*1a0e5dbdSVincent Chen 60*1a0e5dbdSVincent Chen tmp = (1U << alt->errata_id); 61*1a0e5dbdSVincent Chen if (cpu_req_errata & tmp) { 62*1a0e5dbdSVincent Chen patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len); 63*1a0e5dbdSVincent Chen cpu_apply_errata |= tmp; 64*1a0e5dbdSVincent Chen } 65*1a0e5dbdSVincent Chen } 66*1a0e5dbdSVincent Chen if (cpu_apply_errata != cpu_req_errata) 67*1a0e5dbdSVincent Chen warn_miss_errata(cpu_req_errata - cpu_apply_errata); 68*1a0e5dbdSVincent Chen } 69