xref: /freebsd/sys/dev/psci/smccc.c (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2018 Andrew Turner
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
8  * ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "opt_acpi.h"
33 #include "opt_platform.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 
41 #include <dev/psci/psci.h>
42 #include <dev/psci/smccc.h>
43 
44 #define	SMCCC_VERSION_1_0	0x10000
45 
46 /* Assume 1.0 until we detect a later version */
47 static uint32_t	smccc_version;
48 
49 void
50 smccc_init(void)
51 {
52 	int32_t features;
53 	uint32_t ret;
54 
55 	smccc_version = SMCCC_VERSION_1_0;
56 	features = psci_features(SMCCC_VERSION);
57 	if (features != PSCI_RETVAL_NOT_SUPPORTED) {
58 		ret = psci_call(SMCCC_VERSION, 0, 0, 0);
59 		/* This should always be the case as we checked it above */
60 		if (ret > 0)
61 			smccc_version = ret;
62 	}
63 
64 	if (bootverbose) {
65 		printf("Found SMCCC version %u.%u\n",
66 		    SMCCC_VERSION_MAJOR(smccc_version),
67 		    SMCCC_VERSION_MINOR(smccc_version));
68 	}
69 }
70 
71 static int
72 smccc_probe(device_t dev)
73 {
74 	int32_t version;
75 
76 	/*
77 	 * If the version is not implemented then we treat it as SMCCC 1.0
78 	 */
79 	if (psci_features(SMCCC_VERSION) == PSCI_RETVAL_NOT_SUPPORTED ||
80 	    (version = arm_smccc_invoke(SMCCC_VERSION, NULL)) <= 0) {
81 		device_set_desc(dev, "ARM SMCCC v1.0");
82 		return (0);
83 	}
84 
85 	device_set_descf(dev, "ARM SMCCC v%d.%d", SMCCC_VERSION_MAJOR(version),
86 	    SMCCC_VERSION_MINOR(version));
87 
88 	return (0);
89 }
90 
91 static int
92 smccc_attach(device_t dev)
93 {
94 	return (bus_generic_attach(dev));
95 }
96 
97 uint32_t
98 smccc_get_version(void)
99 {
100 	MPASS(smccc_version != 0);
101 	return (smccc_version);
102 }
103 
104 int32_t
105 smccc_arch_features(uint32_t smccc_func_id)
106 {
107 
108 	MPASS(smccc_version != 0);
109 	if (smccc_version == SMCCC_VERSION_1_0)
110 		return (PSCI_RETVAL_NOT_SUPPORTED);
111 
112 	return (arm_smccc_invoke(SMCCC_ARCH_FEATURES, smccc_func_id, NULL));
113 }
114 
115 /*
116  * The SMCCC handler for Spectre variant 2: Branch target injection.
117  * (CVE-2017-5715)
118  */
119 int
120 smccc_arch_workaround_1(void)
121 {
122 
123 	MPASS(smccc_version != 0);
124 	KASSERT(smccc_version != SMCCC_VERSION_1_0,
125 	    ("SMCCC arch workaround 1 called with an invalid SMCCC interface"));
126 	return (arm_smccc_invoke(SMCCC_ARCH_WORKAROUND_1, NULL));
127 }
128 
129 int
130 smccc_arch_workaround_2(int enable)
131 {
132 
133 	MPASS(smccc_version != 0);
134 	KASSERT(smccc_version != SMCCC_VERSION_1_0,
135 	    ("SMCCC arch workaround 2 called with an invalid SMCCC interface"));
136 	return (arm_smccc_invoke(SMCCC_ARCH_WORKAROUND_2, enable, NULL));
137 }
138 
139 static device_method_t smccc_methods[] = {
140 	DEVMETHOD(device_probe,		smccc_probe),
141 	DEVMETHOD(device_attach,	smccc_attach),
142 
143 	DEVMETHOD(bus_add_child,	bus_generic_add_child),
144 
145 	DEVMETHOD_END
146 };
147 
148 static driver_t smccc_driver = {
149 	"smccc",
150 	smccc_methods,
151 	0,
152 };
153 
154 EARLY_DRIVER_MODULE(smccc, psci, smccc_driver, 0, 0,
155     BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);
156