xref: /freebsd/sys/dev/psci/smccc.c (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
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/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 
40 #include <dev/psci/psci.h>
41 #include <dev/psci/smccc.h>
42 
43 #define	SMCCC_VERSION_1_0	0x10000
44 
45 /* Assume 1.0 until we detect a later version */
46 static uint32_t	smccc_version;
47 
48 void
49 smccc_init(void)
50 {
51 	int32_t features;
52 	uint32_t ret;
53 
54 	smccc_version = SMCCC_VERSION_1_0;
55 	features = psci_features(SMCCC_VERSION);
56 	if (features != PSCI_RETVAL_NOT_SUPPORTED) {
57 		ret = psci_call(SMCCC_VERSION, 0, 0, 0);
58 		/* This should always be the case as we checked it above */
59 		if (ret > 0)
60 			smccc_version = ret;
61 	}
62 
63 	if (bootverbose) {
64 		printf("Found SMCCC version %u.%u\n",
65 		    SMCCC_VERSION_MAJOR(smccc_version),
66 		    SMCCC_VERSION_MINOR(smccc_version));
67 	}
68 }
69 
70 uint32_t
71 smccc_get_version(void)
72 {
73 	MPASS(smccc_version != 0);
74 	return (smccc_version);
75 }
76 
77 int32_t
78 smccc_arch_features(uint32_t smccc_func_id)
79 {
80 
81 	MPASS(smccc_version != 0);
82 	if (smccc_version == SMCCC_VERSION_1_0)
83 		return (PSCI_RETVAL_NOT_SUPPORTED);
84 
85 	return (psci_call(SMCCC_ARCH_FEATURES, smccc_func_id, 0, 0));
86 }
87 
88 /*
89  * The SMCCC handler for Spectre variant 2: Branch target injection.
90  * (CVE-2017-5715)
91  */
92 int
93 smccc_arch_workaround_1(void)
94 {
95 
96 	MPASS(smccc_version != 0);
97 	KASSERT(smccc_version != SMCCC_VERSION_1_0,
98 	    ("SMCCC arch workaround 1 called with an invalid SMCCC interface"));
99 	return (psci_call(SMCCC_ARCH_WORKAROUND_1, 0, 0, 0));
100 }
101 
102 int
103 smccc_arch_workaround_2(int enable)
104 {
105 
106 	MPASS(smccc_version != 0);
107 	KASSERT(smccc_version != SMCCC_VERSION_1_0,
108 	    ("SMCCC arch workaround 2 called with an invalid SMCCC interface"));
109 	return (psci_call(SMCCC_ARCH_WORKAROUND_2, enable, 0, 0));
110 }
111