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