xref: /freebsd/sys/dev/psci/smccc.c (revision 8a723e2bd8683b0e046ed9d03178082f84c8cf26)
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 #include <sys/param.h>
37f651b525SAndrew Turner #include <sys/systm.h>
38f651b525SAndrew Turner #include <sys/kernel.h>
39f651b525SAndrew Turner 
40f651b525SAndrew Turner #include <dev/psci/psci.h>
41f651b525SAndrew Turner #include <dev/psci/smccc.h>
42f651b525SAndrew Turner 
43f651b525SAndrew Turner #define	SMCCC_VERSION_1_0	0x10000
44f651b525SAndrew Turner 
45f651b525SAndrew Turner /* Assume 1.0 until we detect a later version */
46*8a723e2bSAndrew Turner static uint32_t	smccc_version;
47f651b525SAndrew Turner 
480600af1fSAndrew Turner void
490600af1fSAndrew Turner smccc_init(void)
50f651b525SAndrew Turner {
51f651b525SAndrew Turner 	int32_t features;
52f651b525SAndrew Turner 	uint32_t ret;
53f651b525SAndrew Turner 
54*8a723e2bSAndrew Turner 	smccc_version = SMCCC_VERSION_1_0;
55f651b525SAndrew Turner 	features = psci_features(SMCCC_VERSION);
56f651b525SAndrew Turner 	if (features != PSCI_RETVAL_NOT_SUPPORTED) {
57f651b525SAndrew Turner 		ret = psci_call(SMCCC_VERSION, 0, 0, 0);
58f651b525SAndrew Turner 		/* This should always be the case as we checked it above */
59f651b525SAndrew Turner 		if (ret > 0)
60f651b525SAndrew Turner 			smccc_version = ret;
61f651b525SAndrew Turner 	}
62f651b525SAndrew Turner 
63f651b525SAndrew Turner 	if (bootverbose) {
64f651b525SAndrew Turner 		printf("Found SMCCC version %u.%u\n",
65f651b525SAndrew Turner 		    SMCCC_VERSION_MAJOR(smccc_version),
66f651b525SAndrew Turner 		    SMCCC_VERSION_MINOR(smccc_version));
67f651b525SAndrew Turner 	}
68f651b525SAndrew Turner }
690600af1fSAndrew Turner 
700600af1fSAndrew Turner uint32_t
710600af1fSAndrew Turner smccc_get_version(void)
720600af1fSAndrew Turner {
73*8a723e2bSAndrew Turner 	MPASS(smccc_version != 0);
740600af1fSAndrew Turner 	return (smccc_version);
750600af1fSAndrew Turner }
76f651b525SAndrew Turner 
77f651b525SAndrew Turner int32_t
78f651b525SAndrew Turner smccc_arch_features(uint32_t smccc_func_id)
79f651b525SAndrew Turner {
80f651b525SAndrew Turner 
81*8a723e2bSAndrew Turner 	MPASS(smccc_version != 0);
82f651b525SAndrew Turner 	if (smccc_version == SMCCC_VERSION_1_0)
83f651b525SAndrew Turner 		return (PSCI_RETVAL_NOT_SUPPORTED);
84f651b525SAndrew Turner 
85f651b525SAndrew Turner 	return (psci_call(SMCCC_ARCH_FEATURES, smccc_func_id, 0, 0));
86f651b525SAndrew Turner }
87f651b525SAndrew Turner 
88f651b525SAndrew Turner /*
89f651b525SAndrew Turner  * The SMCCC handler for Spectre variant 2: Branch target injection.
90f651b525SAndrew Turner  * (CVE-2017-5715)
91f651b525SAndrew Turner  */
92f651b525SAndrew Turner int
93f651b525SAndrew Turner smccc_arch_workaround_1(void)
94f651b525SAndrew Turner {
95f651b525SAndrew Turner 
96*8a723e2bSAndrew Turner 	MPASS(smccc_version != 0);
97f651b525SAndrew Turner 	KASSERT(smccc_version != SMCCC_VERSION_1_0,
98f651b525SAndrew Turner 	    ("SMCCC arch workaround 1 called with an invalid SMCCC interface"));
99f651b525SAndrew Turner 	return (psci_call(SMCCC_ARCH_WORKAROUND_1, 0, 0, 0));
100f651b525SAndrew Turner }
1010594061eSAndrew Turner 
1020594061eSAndrew Turner int
103100a6d19SAndrew Turner smccc_arch_workaround_2(int enable)
1040594061eSAndrew Turner {
1050594061eSAndrew Turner 
106*8a723e2bSAndrew Turner 	MPASS(smccc_version != 0);
1070594061eSAndrew Turner 	KASSERT(smccc_version != SMCCC_VERSION_1_0,
1080594061eSAndrew Turner 	    ("SMCCC arch workaround 2 called with an invalid SMCCC interface"));
109100a6d19SAndrew Turner 	return (psci_call(SMCCC_ARCH_WORKAROUND_2, enable, 0, 0));
1100594061eSAndrew Turner }
111