11a0e5dbdSVincent Chen // SPDX-License-Identifier: GPL-2.0-only 21a0e5dbdSVincent Chen /* 31a0e5dbdSVincent Chen * Copyright (C) 2021 Sifive. 41a0e5dbdSVincent Chen */ 51a0e5dbdSVincent Chen 61a0e5dbdSVincent Chen #include <linux/kernel.h> 71a0e5dbdSVincent Chen #include <linux/string.h> 81a0e5dbdSVincent Chen #include <linux/bug.h> 91a0e5dbdSVincent Chen #include <asm/patch.h> 101a0e5dbdSVincent Chen #include <asm/alternative.h> 111a0e5dbdSVincent Chen #include <asm/vendorid_list.h> 121a0e5dbdSVincent Chen #include <asm/errata_list.h> 131a0e5dbdSVincent Chen 141a0e5dbdSVincent Chen struct errata_info_t { 151a0e5dbdSVincent Chen char name[ERRATA_STRING_LENGTH_MAX]; 161a0e5dbdSVincent Chen bool (*check_func)(unsigned long arch_id, unsigned long impid); 171a0e5dbdSVincent Chen }; 181a0e5dbdSVincent Chen 19800149a7SVincent Chen static bool errata_cip_453_check_func(unsigned long arch_id, unsigned long impid) 20800149a7SVincent Chen { 21800149a7SVincent Chen /* 22800149a7SVincent Chen * Affected cores: 23800149a7SVincent Chen * Architecture ID: 0x8000000000000007 24800149a7SVincent Chen * Implement ID: 0x20181004 <= impid <= 0x20191105 25800149a7SVincent Chen */ 26800149a7SVincent Chen if (arch_id != 0x8000000000000007 || 27800149a7SVincent Chen (impid < 0x20181004 || impid > 0x20191105)) 28800149a7SVincent Chen return false; 29800149a7SVincent Chen return true; 30800149a7SVincent Chen } 31800149a7SVincent Chen 32*bff3ff52SVincent Chen static bool errata_cip_1200_check_func(unsigned long arch_id, unsigned long impid) 33*bff3ff52SVincent Chen { 34*bff3ff52SVincent Chen /* 35*bff3ff52SVincent Chen * Affected cores: 36*bff3ff52SVincent Chen * Architecture ID: 0x8000000000000007 or 0x1 37*bff3ff52SVincent Chen * Implement ID: mimpid[23:0] <= 0x200630 and mimpid != 0x01200626 38*bff3ff52SVincent Chen */ 39*bff3ff52SVincent Chen if (arch_id != 0x8000000000000007 && arch_id != 0x1) 40*bff3ff52SVincent Chen return false; 41*bff3ff52SVincent Chen if ((impid & 0xffffff) > 0x200630 || impid == 0x1200626) 42*bff3ff52SVincent Chen return false; 43*bff3ff52SVincent Chen return true; 44*bff3ff52SVincent Chen } 45*bff3ff52SVincent Chen 46800149a7SVincent Chen static struct errata_info_t errata_list[ERRATA_SIFIVE_NUMBER] = { 47800149a7SVincent Chen { 48800149a7SVincent Chen .name = "cip-453", 49800149a7SVincent Chen .check_func = errata_cip_453_check_func 50800149a7SVincent Chen }, 51*bff3ff52SVincent Chen { 52*bff3ff52SVincent Chen .name = "cip-1200", 53*bff3ff52SVincent Chen .check_func = errata_cip_1200_check_func 54*bff3ff52SVincent Chen }, 55800149a7SVincent Chen }; 56800149a7SVincent Chen 571a0e5dbdSVincent Chen static u32 __init sifive_errata_probe(unsigned long archid, unsigned long impid) 581a0e5dbdSVincent Chen { 591a0e5dbdSVincent Chen int idx; 601a0e5dbdSVincent Chen u32 cpu_req_errata = 0; 611a0e5dbdSVincent Chen 621a0e5dbdSVincent Chen for (idx = 0; idx < ERRATA_SIFIVE_NUMBER; idx++) 631a0e5dbdSVincent Chen if (errata_list[idx].check_func(archid, impid)) 641a0e5dbdSVincent Chen cpu_req_errata |= (1U << idx); 651a0e5dbdSVincent Chen 661a0e5dbdSVincent Chen return cpu_req_errata; 671a0e5dbdSVincent Chen } 681a0e5dbdSVincent Chen 691a0e5dbdSVincent Chen static void __init warn_miss_errata(u32 miss_errata) 701a0e5dbdSVincent Chen { 711a0e5dbdSVincent Chen int i; 721a0e5dbdSVincent Chen 731a0e5dbdSVincent Chen pr_warn("----------------------------------------------------------------\n"); 741a0e5dbdSVincent Chen pr_warn("WARNING: Missing the following errata may cause potential issues\n"); 751a0e5dbdSVincent Chen for (i = 0; i < ERRATA_SIFIVE_NUMBER; i++) 761a0e5dbdSVincent Chen if (miss_errata & 0x1 << i) 771a0e5dbdSVincent Chen pr_warn("\tSiFive Errata[%d]:%s\n", i, errata_list[i].name); 781a0e5dbdSVincent Chen pr_warn("Please enable the corresponding Kconfig to apply them\n"); 791a0e5dbdSVincent Chen pr_warn("----------------------------------------------------------------\n"); 801a0e5dbdSVincent Chen } 811a0e5dbdSVincent Chen 821a0e5dbdSVincent Chen void __init sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end, 831a0e5dbdSVincent Chen unsigned long archid, unsigned long impid) 841a0e5dbdSVincent Chen { 851a0e5dbdSVincent Chen struct alt_entry *alt; 861a0e5dbdSVincent Chen u32 cpu_req_errata = sifive_errata_probe(archid, impid); 871a0e5dbdSVincent Chen u32 cpu_apply_errata = 0; 881a0e5dbdSVincent Chen u32 tmp; 891a0e5dbdSVincent Chen 901a0e5dbdSVincent Chen for (alt = begin; alt < end; alt++) { 911a0e5dbdSVincent Chen if (alt->vendor_id != SIFIVE_VENDOR_ID) 921a0e5dbdSVincent Chen continue; 931a0e5dbdSVincent Chen if (alt->errata_id >= ERRATA_SIFIVE_NUMBER) { 941a0e5dbdSVincent Chen WARN(1, "This errata id:%d is not in kernel errata list", alt->errata_id); 951a0e5dbdSVincent Chen continue; 961a0e5dbdSVincent Chen } 971a0e5dbdSVincent Chen 981a0e5dbdSVincent Chen tmp = (1U << alt->errata_id); 991a0e5dbdSVincent Chen if (cpu_req_errata & tmp) { 1001a0e5dbdSVincent Chen patch_text_nosync(alt->old_ptr, alt->alt_ptr, alt->alt_len); 1011a0e5dbdSVincent Chen cpu_apply_errata |= tmp; 1021a0e5dbdSVincent Chen } 1031a0e5dbdSVincent Chen } 1041a0e5dbdSVincent Chen if (cpu_apply_errata != cpu_req_errata) 1051a0e5dbdSVincent Chen warn_miss_errata(cpu_req_errata - cpu_apply_errata); 1061a0e5dbdSVincent Chen } 107