xref: /freebsd/sys/dev/psci/smccc.c (revision f651b5252702fbcaf6d2f69b51e2c6b6f9bdd4a0)
1*f651b525SAndrew Turner /*-
2*f651b525SAndrew Turner  * SPDX-License-Identifier: BSD-2-Clause
3*f651b525SAndrew Turner  *
4*f651b525SAndrew Turner  * Copyright (c) 2018 Andrew Turner
5*f651b525SAndrew Turner  *
6*f651b525SAndrew Turner  * This software was developed by SRI International and the University of
7*f651b525SAndrew Turner  * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
8*f651b525SAndrew Turner  * ("CTSRD"), as part of the DARPA CRASH research programme.
9*f651b525SAndrew Turner  *
10*f651b525SAndrew Turner  * Redistribution and use in source and binary forms, with or without
11*f651b525SAndrew Turner  * modification, are permitted provided that the following conditions
12*f651b525SAndrew Turner  * are met:
13*f651b525SAndrew Turner  * 1. Redistributions of source code must retain the above copyright
14*f651b525SAndrew Turner  *    notice, this list of conditions and the following disclaimer.
15*f651b525SAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright
16*f651b525SAndrew Turner  *    notice, this list of conditions and the following disclaimer in the
17*f651b525SAndrew Turner  *    documentation and/or other materials provided with the distribution.
18*f651b525SAndrew Turner  *
19*f651b525SAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20*f651b525SAndrew Turner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*f651b525SAndrew Turner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*f651b525SAndrew Turner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23*f651b525SAndrew Turner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*f651b525SAndrew Turner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*f651b525SAndrew Turner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*f651b525SAndrew Turner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*f651b525SAndrew Turner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*f651b525SAndrew Turner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*f651b525SAndrew Turner  * SUCH DAMAGE.
30*f651b525SAndrew Turner  */
31*f651b525SAndrew Turner 
32*f651b525SAndrew Turner #include "opt_acpi.h"
33*f651b525SAndrew Turner #include "opt_platform.h"
34*f651b525SAndrew Turner 
35*f651b525SAndrew Turner #include <sys/cdefs.h>
36*f651b525SAndrew Turner __FBSDID("$FreeBSD$");
37*f651b525SAndrew Turner 
38*f651b525SAndrew Turner #include <sys/param.h>
39*f651b525SAndrew Turner #include <sys/systm.h>
40*f651b525SAndrew Turner #include <sys/kernel.h>
41*f651b525SAndrew Turner 
42*f651b525SAndrew Turner #include <dev/psci/psci.h>
43*f651b525SAndrew Turner #include <dev/psci/smccc.h>
44*f651b525SAndrew Turner 
45*f651b525SAndrew Turner #define	SMCCC_VERSION_1_0	0x10000
46*f651b525SAndrew Turner 
47*f651b525SAndrew Turner /* Assume 1.0 until we detect a later version */
48*f651b525SAndrew Turner static uint32_t	smccc_version = SMCCC_VERSION_1_0;
49*f651b525SAndrew Turner 
50*f651b525SAndrew Turner static void
51*f651b525SAndrew Turner smccc_init(void *dummy)
52*f651b525SAndrew Turner {
53*f651b525SAndrew Turner 	int32_t features;
54*f651b525SAndrew Turner 	uint32_t ret;
55*f651b525SAndrew Turner 
56*f651b525SAndrew Turner 	features = psci_features(SMCCC_VERSION);
57*f651b525SAndrew Turner 	if (features != PSCI_RETVAL_NOT_SUPPORTED) {
58*f651b525SAndrew Turner 		ret = psci_call(SMCCC_VERSION, 0, 0, 0);
59*f651b525SAndrew Turner 		/* This should always be the case as we checked it above */
60*f651b525SAndrew Turner 		if (ret > 0)
61*f651b525SAndrew Turner 			smccc_version = ret;
62*f651b525SAndrew Turner 	}
63*f651b525SAndrew Turner 
64*f651b525SAndrew Turner 	if (bootverbose) {
65*f651b525SAndrew Turner 		printf("Found SMCCC version %u.%u\n",
66*f651b525SAndrew Turner 		    SMCCC_VERSION_MAJOR(smccc_version),
67*f651b525SAndrew Turner 		    SMCCC_VERSION_MINOR(smccc_version));
68*f651b525SAndrew Turner 	}
69*f651b525SAndrew Turner }
70*f651b525SAndrew Turner SYSINIT(smccc_start, SI_SUB_DRIVERS, SI_ORDER_ANY, smccc_init, NULL);
71*f651b525SAndrew Turner 
72*f651b525SAndrew Turner int32_t
73*f651b525SAndrew Turner smccc_arch_features(uint32_t smccc_func_id)
74*f651b525SAndrew Turner {
75*f651b525SAndrew Turner 
76*f651b525SAndrew Turner 	if (smccc_version == SMCCC_VERSION_1_0)
77*f651b525SAndrew Turner 		return (PSCI_RETVAL_NOT_SUPPORTED);
78*f651b525SAndrew Turner 
79*f651b525SAndrew Turner 	return (psci_call(SMCCC_ARCH_FEATURES, smccc_func_id, 0, 0));
80*f651b525SAndrew Turner }
81*f651b525SAndrew Turner 
82*f651b525SAndrew Turner /*
83*f651b525SAndrew Turner  * The SMCCC handler for Spectre variant 2: Branch target injection.
84*f651b525SAndrew Turner  * (CVE-2017-5715)
85*f651b525SAndrew Turner  */
86*f651b525SAndrew Turner int
87*f651b525SAndrew Turner smccc_arch_workaround_1(void)
88*f651b525SAndrew Turner {
89*f651b525SAndrew Turner 
90*f651b525SAndrew Turner 	KASSERT(smccc_version != SMCCC_VERSION_1_0,
91*f651b525SAndrew Turner 	    ("SMCCC arch workaround 1 called with an invalid SMCCC interface"));
92*f651b525SAndrew Turner 	return (psci_call(SMCCC_ARCH_WORKAROUND_1, 0, 0, 0));
93*f651b525SAndrew Turner }
94