xref: /freebsd/sys/dev/psci/smccc_errata.c (revision b5daf675efc746611c7cfcd1fa474b8905064c4b)
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 /*
29  * A driver for the Arm Errata Management Firmware Interface (Errata ABI).
30  * This queries into the SMCCC firmware for the status of errata using the
31  * interface documented in den0100 [1].
32  *
33  * [1] https://developer.arm.com/documentation/den0100/latest
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/random.h>
44 
45 #include <dev/psci/psci.h>
46 #include <dev/psci/smccc.h>
47 
48 #include <machine/cpu_feat.h>
49 
50 #define	ERRATA_HIGHER_EL_MITIGATION	3
51 #define	ERRATA_NOT_AFFECTED		2
52 #define	ERRATA_AFFECTED			1
53 
54 #define	EM_VERSION		SMCCC_FUNC_ID(SMCCC_FAST_CALL,		\
55     SMCCC_32BIT_CALL, SMCCC_STD_SECURE_SERVICE_CALLS, 0xf0u)
56 #define	 EM_VERSION_MIN	0x10000L
57 #define	EM_FEATURES		SMCCC_FUNC_ID(SMCCC_FAST_CALL,		\
58     SMCCC_32BIT_CALL, SMCCC_STD_SECURE_SERVICE_CALLS, 0xf1u)
59 #define	EM_CPU_ERRATUM_FEATURES	SMCCC_FUNC_ID(SMCCC_FAST_CALL,		\
60     SMCCC_32BIT_CALL, SMCCC_STD_SECURE_SERVICE_CALLS, 0xf2u)
61 
62 static device_identify_t errata_identify;
63 static device_probe_t errata_probe;
64 static device_attach_t errata_attach;
65 static cpu_feat_errata errata_cpu_feat_errata_check(const struct cpu_feat *,
66     u_int);
67 
68 static void
69 errata_identify(driver_t *driver, device_t parent)
70 {
71 	int32_t version;
72 
73 	/* Check if Errata ABI is supported */
74 	if (smccc_arch_features(EM_VERSION) != SMCCC_RET_SUCCESS)
75 		return;
76 
77 	/* Check we have Errata 1.0 or later */
78 	version = psci_call(EM_VERSION, 0, 0, 0);
79 	if (version < EM_VERSION_MIN)
80 		return;
81 
82 	if (BUS_ADD_CHILD(parent, 0, "errata", DEVICE_UNIT_ANY) == NULL)
83 		device_printf(parent, "add errata child failed\n");
84 }
85 
86 static int
87 errata_probe(device_t dev)
88 {
89 	device_set_desc(dev, "Arm SMCCC Errata Management");
90 	return (BUS_PROBE_NOWILDCARD);
91 }
92 
93 static int
94 errata_attach(device_t dev)
95 {
96 	/* Check for EM_CPU_ERRATUM_FEATURES. It's mandatory, so should exist */
97 	if (arm_smccc_invoke(EM_FEATURES, EM_CPU_ERRATUM_FEATURES, NULL) < 0) {
98 		device_printf(dev,
99 		    "EM_CPU_ERRATUM_FEATURES is not implemented\n");
100 		return (ENXIO);
101 	}
102 
103 	cpu_feat_register_errata_check(errata_cpu_feat_errata_check);
104 
105 	return (0);
106 }
107 
108 static cpu_feat_errata
109 errata_cpu_feat_errata_check(const struct cpu_feat *feat __unused, u_int errata_id)
110 {
111 	struct arm_smccc_res res;
112 
113 	switch(arm_smccc_invoke(EM_CPU_ERRATUM_FEATURES, errata_id, 0, &res)) {
114 	default:
115 		return (ERRATA_UNKNOWN);
116 	case ERRATA_NOT_AFFECTED:
117 		return (ERRATA_NONE);
118 	case ERRATA_AFFECTED:
119 		return (ERRATA_AFFECTED);
120 	case ERRATA_HIGHER_EL_MITIGATION:
121 		return (ERRATA_FW_MITIGAION);
122 	}
123 }
124 
125 static device_method_t errata_methods[] = {
126 	DEVMETHOD(device_identify,	errata_identify),
127 	DEVMETHOD(device_probe,		errata_probe),
128 	DEVMETHOD(device_attach,	errata_attach),
129 
130 	DEVMETHOD_END
131 };
132 
133 static driver_t errata_driver = {
134 	"errata",
135 	errata_methods,
136 	0
137 };
138 
139 DRIVER_MODULE(errata, smccc, errata_driver, 0, 0);
140