xref: /freebsd/sys/dev/psci/smccc.c (revision 0600af1ff16041f15633b2263a8ad9525eecd2f1)
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 
50*0600af1fSAndrew Turner void
51*0600af1fSAndrew Turner smccc_init(void)
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 }
70*0600af1fSAndrew Turner 
71*0600af1fSAndrew Turner uint32_t
72*0600af1fSAndrew Turner smccc_get_version(void)
73*0600af1fSAndrew Turner {
74*0600af1fSAndrew Turner 	return (smccc_version);
75*0600af1fSAndrew Turner }
76f651b525SAndrew Turner 
77f651b525SAndrew Turner int32_t
78f651b525SAndrew Turner smccc_arch_features(uint32_t smccc_func_id)
79f651b525SAndrew Turner {
80f651b525SAndrew Turner 
81f651b525SAndrew Turner 	if (smccc_version == SMCCC_VERSION_1_0)
82f651b525SAndrew Turner 		return (PSCI_RETVAL_NOT_SUPPORTED);
83f651b525SAndrew Turner 
84f651b525SAndrew Turner 	return (psci_call(SMCCC_ARCH_FEATURES, smccc_func_id, 0, 0));
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 
95f651b525SAndrew Turner 	KASSERT(smccc_version != SMCCC_VERSION_1_0,
96f651b525SAndrew Turner 	    ("SMCCC arch workaround 1 called with an invalid SMCCC interface"));
97f651b525SAndrew Turner 	return (psci_call(SMCCC_ARCH_WORKAROUND_1, 0, 0, 0));
98f651b525SAndrew Turner }
990594061eSAndrew Turner 
1000594061eSAndrew Turner int
101100a6d19SAndrew Turner smccc_arch_workaround_2(int enable)
1020594061eSAndrew Turner {
1030594061eSAndrew Turner 
1040594061eSAndrew Turner 	KASSERT(smccc_version != SMCCC_VERSION_1_0,
1050594061eSAndrew Turner 	    ("SMCCC arch workaround 2 called with an invalid SMCCC interface"));
106100a6d19SAndrew Turner 	return (psci_call(SMCCC_ARCH_WORKAROUND_2, enable, 0, 0));
1070594061eSAndrew Turner }
108