Module Name:    src
Committed By:   jmcneill
Date:           Fri Aug  6 19:38:54 UTC 2021

Modified Files:
        src/sys/arch/aarch64/conf: files.aarch64
        src/sys/arch/arm/acpi: acpi_platform.c
        src/sys/arch/arm/arm: psci.c psci.h
        src/sys/arch/arm/conf: files.arm
        src/sys/arch/arm/fdt: psci_fdt.c
Added Files:
        src/sys/arch/arm/arm: smccc.c smccc.h

Log Message:
Arm: Add support for SMC Calling Convention

Arm DEN0028 defines a calling mechanism used with Secure Monitor Call (SMC)
and Hypervisor Call (HVC) instructions. To discover SMCCC, we must:

 1) Find the PSCI conduit (either via ACPI FADT, or Device Tree)
 2) Use PSCI_VERSION to determine whether PSCI_FEATURES is supported
 3) Call PSCI_FEATURES with SMCCC_VERSION to determine the implementation
    version.


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/sys/arch/aarch64/conf/files.aarch64
cvs rdiff -u -r1.26 -r1.27 src/sys/arch/arm/acpi/acpi_platform.c
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/arm/arm/psci.c
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/arm/arm/psci.h
cvs rdiff -u -r0 -r1.1 src/sys/arch/arm/arm/smccc.c \
    src/sys/arch/arm/arm/smccc.h
cvs rdiff -u -r1.164 -r1.165 src/sys/arch/arm/conf/files.arm
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/arm/fdt/psci_fdt.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/aarch64/conf/files.aarch64
diff -u src/sys/arch/aarch64/conf/files.aarch64:1.31 src/sys/arch/aarch64/conf/files.aarch64:1.32
--- src/sys/arch/aarch64/conf/files.aarch64:1.31	Sat Jul 24 23:38:41 2021
+++ src/sys/arch/aarch64/conf/files.aarch64	Fri Aug  6 19:38:53 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: files.aarch64,v 1.31 2021/07/24 23:38:41 jmcneill Exp $
+#	$NetBSD: files.aarch64,v 1.32 2021/08/06 19:38:53 jmcneill Exp $
 
 defflag opt_cpuoptions.h	AARCH64_ALIGNMENT_CHECK
 defflag opt_cpuoptions.h	AARCH64_EL0_STACK_ALIGNMENT_CHECK
@@ -33,11 +33,15 @@ defparam opt_console.h		CONSPEED
 defparam opt_console.h		CONMODE
 defparam opt_console.h		CONADDR
 
-# Power State Coordination Interface (PSCI)
 # FP support
 file	arch/aarch64/aarch64/fpu.c
 
-device	psci
+# SMC Calling Convention
+define	smccc
+file	arch/arm/arm/smccc.c			smccc
+
+# Power State Coordination Interface (PSCI)
+device	psci: smccc
 file	arch/arm/arm/psci.c			psci
 file	arch/arm/arm/psci_arm.S			psci
 

Index: src/sys/arch/arm/acpi/acpi_platform.c
diff -u src/sys/arch/arm/acpi/acpi_platform.c:1.26 src/sys/arch/arm/acpi/acpi_platform.c:1.27
--- src/sys/arch/arm/acpi/acpi_platform.c:1.26	Wed May 12 23:22:32 2021
+++ src/sys/arch/arm/acpi/acpi_platform.c	Fri Aug  6 19:38:53 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: acpi_platform.c,v 1.26 2021/05/12 23:22:32 thorpej Exp $ */
+/* $NetBSD: acpi_platform.c,v 1.27 2021/08/06 19:38:53 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 #include "opt_multiprocessor.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.26 2021/05/12 23:22:32 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.27 2021/08/06 19:38:53 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -55,6 +55,7 @@ __KERNEL_RCSID(0, "$NetBSD: acpi_platfor
 
 #include <arm/cortex/gtmr_var.h>
 
+#include <arm/arm/smccc.h>
 #include <arm/arm/psci.h>
 #include <arm/fdt/psci_fdtvar.h>
 

Index: src/sys/arch/arm/arm/psci.c
diff -u src/sys/arch/arm/arm/psci.c:1.5 src/sys/arch/arm/arm/psci.c:1.6
--- src/sys/arch/arm/arm/psci.c:1.5	Sat Jan 16 23:51:50 2021
+++ src/sys/arch/arm/arm/psci.c	Fri Aug  6 19:38:53 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: psci.c,v 1.5 2021/01/16 23:51:50 chs Exp $ */
+/* $NetBSD: psci.c,v 1.6 2021/08/06 19:38:53 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2017 Jared McNeill <jmcne...@invisible.ca>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: psci.c,v 1.5 2021/01/16 23:51:50 chs Exp $");
+__KERNEL_RCSID(0, "$NetBSD: psci.c,v 1.6 2021/08/06 19:38:53 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -45,6 +45,7 @@ __KERNEL_RCSID(0, "$NetBSD: psci.c,v 1.5
 #else
 #define	PSCI_CPU_ON		0x84000003
 #endif
+#define	PSCI_FEATURES		0x8400000a
 
 static psci_fn psci_call_fn;
 
@@ -53,9 +54,10 @@ static uint32_t psci_functions[PSCI_FUNC
         [PSCI_FUNC_SYSTEM_OFF] = PSCI_SYSTEM_OFF,
 	[PSCI_FUNC_SYSTEM_RESET] = PSCI_SYSTEM_RESET,
 	[PSCI_FUNC_CPU_ON] = PSCI_CPU_ON,
+	[PSCI_FUNC_FEATURES] = PSCI_FEATURES,
 };
 
-static int
+int
 psci_call(register_t fid, register_t arg1, register_t arg2, register_t arg3)
 {
 	KASSERT(psci_call_fn != NULL);
@@ -98,6 +100,15 @@ psci_system_reset(void)
 	psci_call(psci_functions[PSCI_FUNC_SYSTEM_RESET], 0, 0, 0);
 }
 
+int
+psci_features(uint32_t fid)
+{
+	if (psci_functions[PSCI_FUNC_FEATURES] == 0) {
+		return PSCI_NOT_SUPPORTED;
+	}
+	return psci_call(psci_functions[PSCI_FUNC_FEATURES], fid, 0, 0);
+}
+
 void
 psci_init(psci_fn fn)
 {

Index: src/sys/arch/arm/arm/psci.h
diff -u src/sys/arch/arm/arm/psci.h:1.2 src/sys/arch/arm/arm/psci.h:1.3
--- src/sys/arch/arm/arm/psci.h:1.2	Sat Oct 13 00:07:55 2018
+++ src/sys/arch/arm/arm/psci.h	Fri Aug  6 19:38:53 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: psci.h,v 1.2 2018/10/13 00:07:55 jmcneill Exp $ */
+/* $NetBSD: psci.h,v 1.3 2021/08/06 19:38:53 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2017 Jared McNeill <jmcne...@invisible.ca>
@@ -37,6 +37,7 @@ enum psci_function {
 	PSCI_FUNC_CPU_ON,
 	PSCI_FUNC_SYSTEM_OFF,
 	PSCI_FUNC_SYSTEM_RESET,
+	PSCI_FUNC_FEATURES,
 	PSCI_FUNC_MAX
 };
 
@@ -108,4 +109,13 @@ void	psci_system_off(void);
  */
 void	psci_system_reset(void);
 
+/*
+ * Discover supported features.
+ */
+int	psci_features(uint32_t);
+
+/*
+ * Generic PSCI call.
+ */
+int	psci_call(register_t, register_t, register_t, register_t);
 #endif /* _ARM_PSCI_H */

Index: src/sys/arch/arm/conf/files.arm
diff -u src/sys/arch/arm/conf/files.arm:1.164 src/sys/arch/arm/conf/files.arm:1.165
--- src/sys/arch/arm/conf/files.arm:1.164	Wed Oct 21 13:31:51 2020
+++ src/sys/arch/arm/conf/files.arm	Fri Aug  6 19:38:53 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: files.arm,v 1.164 2020/10/21 13:31:51 christos Exp $
+#	$NetBSD: files.arm,v 1.165 2021/08/06 19:38:53 jmcneill Exp $
 
 # temporary define to allow easy moving to ../arch/arm/arm32
 defflag				ARM32
@@ -99,8 +99,12 @@ obsolete defflag		ARMFPE
 file	arch/arm/vfp/vfp_init.c			arm32
 #file	arch/arm/vfp/pmap_vfp.S			arm32 & fpu_vfp
 
+# SMC Calling Convention
+define	smccc
+file	arch/arm/arm/smccc.c			smccc
+
 # Power State Coordination Interface (PSCI)
-device	psci
+device	psci: smccc
 file	arch/arm/arm/psci.c			psci
 file	arch/arm/arm/psci_arm.S			psci
 

Index: src/sys/arch/arm/fdt/psci_fdt.c
diff -u src/sys/arch/arm/fdt/psci_fdt.c:1.20 src/sys/arch/arm/fdt/psci_fdt.c:1.21
--- src/sys/arch/arm/fdt/psci_fdt.c:1.20	Wed Jan 27 03:10:19 2021
+++ src/sys/arch/arm/fdt/psci_fdt.c	Fri Aug  6 19:38:53 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: psci_fdt.c,v 1.20 2021/01/27 03:10:19 thorpej Exp $ */
+/* $NetBSD: psci_fdt.c,v 1.21 2021/08/06 19:38:53 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2017 Jared McNeill <jmcne...@invisible.ca>
@@ -29,7 +29,7 @@
 #include "opt_multiprocessor.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: psci_fdt.c,v 1.20 2021/01/27 03:10:19 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: psci_fdt.c,v 1.21 2021/08/06 19:38:53 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -40,6 +40,7 @@ __KERNEL_RCSID(0, "$NetBSD: psci_fdt.c,v
 
 #include <dev/fdt/fdtvar.h>
 
+#include <arm/arm/smccc.h>
 #include <arm/arm/psci.h>
 #include <arm/fdt/psci_fdtvar.h>
 
@@ -135,6 +136,8 @@ psci_fdt_init(const int phandle)
 			psci_setfunc(PSCI_FUNC_CPU_ON, val);
 	}
 
+	smccc_probe();
+
 	return 0;
 }
 

Added files:

Index: src/sys/arch/arm/arm/smccc.c
diff -u /dev/null src/sys/arch/arm/arm/smccc.c:1.1
--- /dev/null	Fri Aug  6 19:38:54 2021
+++ src/sys/arch/arm/arm/smccc.c	Fri Aug  6 19:38:53 2021
@@ -0,0 +1,89 @@
+/* $NetBSD: smccc.c,v 1.1 2021/08/06 19:38:53 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2021 Jared McNeill <jmcne...@invisible.ca>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: smccc.c,v 1.1 2021/08/06 19:38:53 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/kernel.h>
+
+#include <arm/arm/psci.h>
+#include <arm/arm/smccc.h>
+
+/* Minimum supported PSCI version for SMCCC discovery */
+#define	PSCI_VERSION_1_0	0x10000
+
+/* Retrieve the implemented version of the SMC Calling Convention. */
+#define	SMCCC_VERSION		0x80000000
+
+/* True if SMCCC is detected. */
+static bool	smccc_present;
+
+/*
+ * smccc_probe --
+ *
+ *	Returns true if SMCCC is supported by platform firmware.
+ */
+bool
+smccc_probe(void)
+{
+	if (cold && !smccc_present) {
+		if (!psci_available() || psci_version() < PSCI_VERSION_1_0) {
+			return false;
+		}
+
+		smccc_present = psci_features(SMCCC_VERSION) == PSCI_SUCCESS;
+	}
+	return smccc_present;
+}
+
+/*
+ * smccc_version --
+ *
+ *	Return the implemented SMCCC version, or a negative error code on failure.
+ */
+int
+smccc_version(void)
+{
+	return smccc_call(SMCCC_VERSION, 0, 0, 0);
+}
+
+/*
+ * smccc_call --
+ *
+ *	Generic call interface for SMC/HVC calls.
+ */
+int
+smccc_call(register_t fid, register_t arg1, register_t arg2, register_t arg3)
+{
+	if (!smccc_present) {
+		return SMCCC_NOT_SUPPORTED;
+	}
+
+	return psci_call(fid, arg1, arg2, arg3);
+}
Index: src/sys/arch/arm/arm/smccc.h
diff -u /dev/null src/sys/arch/arm/arm/smccc.h:1.1
--- /dev/null	Fri Aug  6 19:38:54 2021
+++ src/sys/arch/arm/arm/smccc.h	Fri Aug  6 19:38:53 2021
@@ -0,0 +1,55 @@
+/* $NetBSD: smccc.h,v 1.1 2021/08/06 19:38:53 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2021 Jared McNeill <jmcne...@invisible.ca>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _ARM_SMCCC_H
+#define _ARM_SMCCC_H
+
+/*
+ * SMCCC return codes
+ */
+#define	SMCCC_SUCCESS			0
+#define	SMCCC_NOT_SUPPORTED		-1
+#define	SMCCC_NOT_REQUIRED		-2
+#define	SMCCC_INVALID_PARAMETER		-3
+
+/*
+ * Returns true if SMCCC is supported.
+ */
+bool	smccc_probe(void);
+
+/*
+ * Get SMCCC version.
+ */
+int	smccc_version(void);
+
+/*
+ * Call an SMC/HVC service.
+ */
+int	smccc_call(register_t, register_t, register_t, register_t);
+
+#endif /* _ARM_SMCCC_H */

Reply via email to