1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2024 Arm Ltd 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #ifndef _MACHINE_CPU_FEAT_H_ 29 #define _MACHINE_CPU_FEAT_H_ 30 31 #include <sys/linker_set.h> 32 #include <sys/sysctl.h> 33 34 typedef enum { 35 ERRATA_UNKNOWN, /* Unknown erratum */ 36 ERRATA_NONE, /* No errata for this feature on this system. */ 37 ERRATA_AFFECTED, /* There is errata on this system. */ 38 ERRATA_FW_MITIGAION, /* There is errata, and a firmware */ 39 /* mitigation. The mitigation may need a */ 40 /* kernel component. */ 41 } cpu_feat_errata; 42 43 typedef enum { 44 /* 45 * Don't implement the feature or erratum wrokarount, 46 * e.g. the feature is not implemented or erratum is 47 * for another CPU. 48 */ 49 FEAT_ALWAYS_DISABLE, 50 51 /* 52 * Disable by default, but allow the user to enable, 53 * e.g. For a rare erratum with a workaround, Arm 54 * Category B (rare) or similar. 55 */ 56 FEAT_DEFAULT_DISABLE, 57 58 /* 59 * Enabled by default, bit allow the user to disable, 60 * e.g. For a common erratum with a workaround, Arm 61 * Category A or B or similar. 62 */ 63 FEAT_DEFAULT_ENABLE, 64 65 /* We could add FEAT_ALWAYS_ENABLE if a need was found. */ 66 } cpu_feat_en; 67 68 #define CPU_FEAT_STAGE_MASK 0x00000001 69 #define CPU_FEAT_EARLY_BOOT 0x00000000 70 #define CPU_FEAT_AFTER_DEV 0x00000001 71 72 #define CPU_FEAT_SCOPE_MASK 0x00000010 73 #define CPU_FEAT_PER_CPU 0x00000000 74 #define CPU_FEAT_SYSTEM 0x00000010 75 76 #define CPU_FEAT_USER_ENABLED 0x40000000 77 #define CPU_FEAT_USER_DISABLED 0x80000000 78 79 struct cpu_feat; 80 81 typedef cpu_feat_en (cpu_feat_check)(const struct cpu_feat *, u_int); 82 typedef bool (cpu_feat_has_errata)(const struct cpu_feat *, u_int, 83 u_int **, u_int *); 84 typedef bool (cpu_feat_enable)(const struct cpu_feat *, cpu_feat_errata, 85 u_int *, u_int); 86 typedef void (cpu_feat_disabled)(const struct cpu_feat *); 87 88 struct cpu_feat { 89 const char *feat_name; 90 cpu_feat_check *feat_check; 91 cpu_feat_has_errata *feat_has_errata; 92 cpu_feat_enable *feat_enable; 93 cpu_feat_disabled *feat_disabled; 94 uint32_t feat_flags; 95 bool feat_enabled; 96 }; 97 SET_DECLARE(cpu_feat_set, struct cpu_feat); 98 99 SYSCTL_DECL(_hw_feat); 100 101 #define CPU_FEAT(name, descr, check, has_errata, enable, disabled, flags) \ 102 static struct cpu_feat name = { \ 103 .feat_name = #name, \ 104 .feat_check = check, \ 105 .feat_has_errata = has_errata, \ 106 .feat_enable = enable, \ 107 .feat_disabled = disabled, \ 108 .feat_flags = flags, \ 109 .feat_enabled = false, \ 110 }; \ 111 DATA_SET(cpu_feat_set, name); \ 112 SYSCTL_BOOL(_hw_feat, OID_AUTO, name, CTLFLAG_RD, &name.feat_enabled, \ 113 0, descr) 114 115 /* 116 * Allow drivers to mark an erratum as worked around, e.g. the Errata 117 * Management ABI may know the workaround isn't needed on a given system. 118 */ 119 typedef cpu_feat_errata (*cpu_feat_errata_check_fn)(const struct cpu_feat *, 120 u_int); 121 void cpu_feat_register_errata_check(cpu_feat_errata_check_fn); 122 123 void enable_cpu_feat(uint32_t); 124 125 /* Check if an erratum is in the list of errata */ 126 static inline bool 127 cpu_feat_has_erratum(u_int *errata_list, u_int errata_count, u_int erratum) 128 { 129 for (u_int i = 0; i < errata_count; i++) 130 if (errata_list[0] == erratum) 131 return (true); 132 133 return (false); 134 } 135 136 #endif /* _MACHINE_CPU_FEAT_H_ */ 137