xref: /freebsd/sys/dev/psci/smccc_errata.c (revision 0c333f8c6e6abee331b12a7dbadfe077dbaf79c8)
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
errata_identify(driver_t * driver,device_t parent)69 errata_identify(driver_t *driver, device_t parent)
70 {
71 	int32_t version;
72 
73 	if (smccc_get_version() < SMCCC_MAKE_VERSION(1, 1))
74 		return;
75 
76 	/* Check we have Errata 1.0 or later */
77 	version = psci_call(EM_VERSION, 0, 0, 0);
78 	if (version < EM_VERSION_MIN)
79 		return;
80 
81 	if (BUS_ADD_CHILD(parent, 0, "errata", -1) == NULL)
82 		device_printf(parent, "add errata child failed\n");
83 }
84 
85 static int
errata_probe(device_t dev)86 errata_probe(device_t dev)
87 {
88 	device_set_desc(dev, "Arm SMCCC Errata Management");
89 	return (BUS_PROBE_NOWILDCARD);
90 }
91 
92 static int
errata_attach(device_t dev)93 errata_attach(device_t dev)
94 {
95 	/* Check for EM_CPU_ERRATUM_FEATURES. It's mandatory, so should exist */
96 	if (arm_smccc_invoke(EM_FEATURES, EM_CPU_ERRATUM_FEATURES, NULL) < 0) {
97 		device_printf(dev,
98 		    "EM_CPU_ERRATUM_FEATURES is not implemented\n");
99 		return (ENXIO);
100 	}
101 
102 	cpu_feat_register_errata_check(errata_cpu_feat_errata_check);
103 
104 	return (0);
105 }
106 
107 static cpu_feat_errata
errata_cpu_feat_errata_check(const struct cpu_feat * feat __unused,u_int errata_id)108 errata_cpu_feat_errata_check(const struct cpu_feat *feat __unused, u_int errata_id)
109 {
110 	struct arm_smccc_res res;
111 
112 	switch(arm_smccc_invoke(EM_CPU_ERRATUM_FEATURES, errata_id, 0, &res)) {
113 	default:
114 		return (ERRATA_UNKNOWN);
115 	case ERRATA_NOT_AFFECTED:
116 		return (ERRATA_NONE);
117 	case ERRATA_AFFECTED:
118 		return (ERRATA_AFFECTED);
119 	case ERRATA_HIGHER_EL_MITIGATION:
120 		return (ERRATA_FW_MITIGAION);
121 	}
122 }
123 
124 static device_method_t errata_methods[] = {
125 	DEVMETHOD(device_identify,	errata_identify),
126 	DEVMETHOD(device_probe,		errata_probe),
127 	DEVMETHOD(device_attach,	errata_attach),
128 
129 	DEVMETHOD_END
130 };
131 
132 static driver_t errata_driver = {
133 	"errata",
134 	errata_methods,
135 	0
136 };
137 
138 DRIVER_MODULE(errata, smccc, errata_driver, 0, 0);
139