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 struct cpu_feat;
77
78 typedef cpu_feat_en (cpu_feat_check)(const struct cpu_feat *, u_int);
79 typedef bool (cpu_feat_has_errata)(const struct cpu_feat *, u_int,
80 u_int **, u_int *);
81 typedef bool (cpu_feat_enable)(const struct cpu_feat *, cpu_feat_errata,
82 u_int *, u_int);
83
84 struct cpu_feat {
85 const char *feat_name;
86 cpu_feat_check *feat_check;
87 cpu_feat_has_errata *feat_has_errata;
88 cpu_feat_enable *feat_enable;
89 uint32_t feat_flags;
90 bool feat_enabled;
91 };
92 SET_DECLARE(cpu_feat_set, struct cpu_feat);
93
94 SYSCTL_DECL(_hw_feat);
95
96 #define CPU_FEAT(name, descr, check, has_errata, enable, flags) \
97 static struct cpu_feat name = { \
98 .feat_name = #name, \
99 .feat_check = check, \
100 .feat_has_errata = has_errata, \
101 .feat_enable = enable, \
102 .feat_flags = flags, \
103 .feat_enabled = false, \
104 }; \
105 DATA_SET(cpu_feat_set, name); \
106 SYSCTL_BOOL(_hw_feat, OID_AUTO, name, CTLFLAG_RD, &name.feat_enabled, \
107 0, descr)
108
109 /*
110 * Allow drivers to mark an erratum as worked around, e.g. the Errata
111 * Management ABI may know the workaround isn't needed on a given system.
112 */
113 typedef cpu_feat_errata (*cpu_feat_errata_check_fn)(const struct cpu_feat *,
114 u_int);
115 void cpu_feat_register_errata_check(cpu_feat_errata_check_fn);
116
117 void enable_cpu_feat(uint32_t);
118
119 /* Check if an erratum is in the list of errata */
120 static inline bool
cpu_feat_has_erratum(u_int * errata_list,u_int errata_count,u_int erratum)121 cpu_feat_has_erratum(u_int *errata_list, u_int errata_count, u_int erratum)
122 {
123 for (u_int i = 0; i < errata_count; i++)
124 if (errata_list[0] == erratum)
125 return (true);
126
127 return (false);
128 }
129
130 #endif /* _MACHINE_CPU_FEAT_H_ */
131