xref: /freebsd/sys/arm64/include/cpu_feat.h (revision a259b98fa211ed87bfee58c575de4e2de94ee0fa)
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 	/*
66 	 * Always enabled. Used when there is no way for the kernel to
67 	 * work without the feature, e.g. configuring the interrupt
68 	 * controller.
69 	 */
70 	FEAT_ALWAYS_ENABLE,
71 } cpu_feat_en;
72 
73 #define	CPU_FEAT_STAGE_MASK	0x00000001
74 #define	CPU_FEAT_EARLY_BOOT	0x00000000
75 #define	CPU_FEAT_AFTER_DEV	0x00000001
76 
77 #define	CPU_FEAT_SCOPE_MASK	0x00000010
78 #define	CPU_FEAT_PER_CPU	0x00000000
79 #define	CPU_FEAT_SYSTEM		0x00000010
80 
81 #define	CPU_FEAT_USER_ENABLED	0x40000000
82 #define	CPU_FEAT_USER_DISABLED	0x80000000
83 
84 struct cpu_feat;
85 
86 typedef cpu_feat_en (cpu_feat_check)(const struct cpu_feat *, u_int);
87 typedef bool (cpu_feat_has_errata)(const struct cpu_feat *, u_int,
88     u_int **, u_int *);
89 typedef bool (cpu_feat_enable)(const struct cpu_feat *, cpu_feat_errata,
90     u_int *, u_int);
91 typedef void (cpu_feat_disabled)(const struct cpu_feat *);
92 
93 struct cpu_feat {
94 	const char		*feat_name;
95 	cpu_feat_check		*feat_check;
96 	cpu_feat_has_errata	*feat_has_errata;
97 	cpu_feat_enable		*feat_enable;
98 	cpu_feat_disabled	*feat_disabled;
99 	uint32_t		 feat_flags;
100 	bool			 feat_enabled;
101 };
102 SET_DECLARE(cpu_feat_set, struct cpu_feat);
103 
104 SYSCTL_DECL(_hw_feat);
105 
106 #define	CPU_FEAT(name, descr, check, has_errata, enable, disabled, flags) \
107 static struct cpu_feat name = {						\
108 	.feat_name		= #name,				\
109 	.feat_check		= check,				\
110 	.feat_has_errata	= has_errata,				\
111 	.feat_enable		= enable,				\
112 	.feat_disabled		= disabled,				\
113 	.feat_flags		= flags,				\
114 	.feat_enabled		= false,				\
115 };									\
116 DATA_SET(cpu_feat_set, name);						\
117 SYSCTL_BOOL(_hw_feat, OID_AUTO, name, CTLFLAG_RD, &name.feat_enabled,	\
118     0, descr)
119 
120 /*
121  * Allow drivers to mark an erratum as worked around, e.g. the Errata
122  * Management ABI may know the workaround isn't needed on a given system.
123  */
124 typedef cpu_feat_errata (*cpu_feat_errata_check_fn)(const struct cpu_feat *,
125     u_int);
126 void cpu_feat_register_errata_check(cpu_feat_errata_check_fn);
127 
128 void	enable_cpu_feat(uint32_t);
129 
130 /* Check if an erratum is in the list of errata */
131 static inline bool
132 cpu_feat_has_erratum(u_int *errata_list, u_int errata_count, u_int erratum)
133 {
134 	for (u_int i = 0; i < errata_count; i++)
135 		if (errata_list[0] == erratum)
136 			return (true);
137 
138 	return (false);
139 }
140 
141 #endif /* _MACHINE_CPU_FEAT_H_ */
142