xref: /freebsd/sys/dev/psci/smccc.c (revision b9cd72b06d795a8c7b39df1f520e866ad7f11aa8)
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/param.h>
36f651b525SAndrew Turner #include <sys/systm.h>
37f651b525SAndrew Turner #include <sys/kernel.h>
38f651b525SAndrew Turner 
39f651b525SAndrew Turner #include <dev/psci/psci.h>
40f651b525SAndrew Turner #include <dev/psci/smccc.h>
41f651b525SAndrew Turner 
42f651b525SAndrew Turner #define	SMCCC_VERSION_1_0	0x10000
43f651b525SAndrew Turner 
44f651b525SAndrew Turner /* Assume 1.0 until we detect a later version */
458a723e2bSAndrew Turner static uint32_t	smccc_version;
46f651b525SAndrew Turner 
470600af1fSAndrew Turner void
480600af1fSAndrew Turner smccc_init(void)
49f651b525SAndrew Turner {
50f651b525SAndrew Turner 	int32_t features;
51f651b525SAndrew Turner 	uint32_t ret;
52f651b525SAndrew Turner 
538a723e2bSAndrew Turner 	smccc_version = SMCCC_VERSION_1_0;
54f651b525SAndrew Turner 	features = psci_features(SMCCC_VERSION);
55f651b525SAndrew Turner 	if (features != PSCI_RETVAL_NOT_SUPPORTED) {
56f651b525SAndrew Turner 		ret = psci_call(SMCCC_VERSION, 0, 0, 0);
57f651b525SAndrew Turner 		/* This should always be the case as we checked it above */
58f651b525SAndrew Turner 		if (ret > 0)
59f651b525SAndrew Turner 			smccc_version = ret;
60f651b525SAndrew Turner 	}
61f651b525SAndrew Turner 
62f651b525SAndrew Turner 	if (bootverbose) {
63f651b525SAndrew Turner 		printf("Found SMCCC version %u.%u\n",
64f651b525SAndrew Turner 		    SMCCC_VERSION_MAJOR(smccc_version),
65f651b525SAndrew Turner 		    SMCCC_VERSION_MINOR(smccc_version));
66f651b525SAndrew Turner 	}
67f651b525SAndrew Turner }
680600af1fSAndrew Turner 
690600af1fSAndrew Turner uint32_t
700600af1fSAndrew Turner smccc_get_version(void)
710600af1fSAndrew Turner {
728a723e2bSAndrew Turner 	MPASS(smccc_version != 0);
730600af1fSAndrew Turner 	return (smccc_version);
740600af1fSAndrew Turner }
75f651b525SAndrew Turner 
76f651b525SAndrew Turner int32_t
77f651b525SAndrew Turner smccc_arch_features(uint32_t smccc_func_id)
78f651b525SAndrew Turner {
79f651b525SAndrew Turner 
808a723e2bSAndrew Turner 	MPASS(smccc_version != 0);
81f651b525SAndrew Turner 	if (smccc_version == SMCCC_VERSION_1_0)
82f651b525SAndrew Turner 		return (PSCI_RETVAL_NOT_SUPPORTED);
83f651b525SAndrew Turner 
84*b9cd72b0SAndrew Turner 	return (arm_smccc_invoke(SMCCC_ARCH_FEATURES, smccc_func_id, NULL));
85f651b525SAndrew Turner }
86f651b525SAndrew Turner 
87f651b525SAndrew Turner /*
88f651b525SAndrew Turner  * The SMCCC handler for Spectre variant 2: Branch target injection.
89f651b525SAndrew Turner  * (CVE-2017-5715)
90f651b525SAndrew Turner  */
91f651b525SAndrew Turner int
92f651b525SAndrew Turner smccc_arch_workaround_1(void)
93f651b525SAndrew Turner {
94f651b525SAndrew Turner 
958a723e2bSAndrew Turner 	MPASS(smccc_version != 0);
96f651b525SAndrew Turner 	KASSERT(smccc_version != SMCCC_VERSION_1_0,
97f651b525SAndrew Turner 	    ("SMCCC arch workaround 1 called with an invalid SMCCC interface"));
98*b9cd72b0SAndrew Turner 	return (arm_smccc_invoke(SMCCC_ARCH_WORKAROUND_1, NULL));
99f651b525SAndrew Turner }
1000594061eSAndrew Turner 
1010594061eSAndrew Turner int
102100a6d19SAndrew Turner smccc_arch_workaround_2(int enable)
1030594061eSAndrew Turner {
1040594061eSAndrew Turner 
1058a723e2bSAndrew Turner 	MPASS(smccc_version != 0);
1060594061eSAndrew Turner 	KASSERT(smccc_version != SMCCC_VERSION_1_0,
1070594061eSAndrew Turner 	    ("SMCCC arch workaround 2 called with an invalid SMCCC interface"));
108*b9cd72b0SAndrew Turner 	return (arm_smccc_invoke(SMCCC_ARCH_WORKAROUND_2, enable, NULL));
1090594061eSAndrew Turner }
110