Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch Arm: Add support for SMC Calling Convention



details:   https://anonhg.NetBSD.org/src/rev/14c10096a20f
branches:  trunk
changeset: 985058:14c10096a20f
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Fri Aug 06 19:38:53 2021 +0000

description:
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.

diffstat:

 sys/arch/aarch64/conf/files.aarch64 |  10 ++-
 sys/arch/arm/acpi/acpi_platform.c   |   5 +-
 sys/arch/arm/arm/psci.c             |  17 +++++-
 sys/arch/arm/arm/psci.h             |  12 ++++-
 sys/arch/arm/arm/smccc.c            |  89 +++++++++++++++++++++++++++++++++++++
 sys/arch/arm/arm/smccc.h            |  55 ++++++++++++++++++++++
 sys/arch/arm/conf/files.arm         |   8 ++-
 sys/arch/arm/fdt/psci_fdt.c         |   7 ++-
 8 files changed, 190 insertions(+), 13 deletions(-)

diffs (truncated from 348 to 300 lines):

diff -r 0b748ae27918 -r 14c10096a20f sys/arch/aarch64/conf/files.aarch64
--- a/sys/arch/aarch64/conf/files.aarch64       Fri Aug 06 18:15:48 2021 +0000
+++ b/sys/arch/aarch64/conf/files.aarch64       Fri Aug 06 19:38:53 2021 +0000
@@ -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         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
 
diff -r 0b748ae27918 -r 14c10096a20f sys/arch/arm/acpi/acpi_platform.c
--- a/sys/arch/arm/acpi/acpi_platform.c Fri Aug 06 18:15:48 2021 +0000
+++ b/sys/arch/arm/acpi/acpi_platform.c Fri Aug 06 19:38:53 2021 +0000
@@ -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 @@
 
 #include <arm/cortex/gtmr_var.h>
 
+#include <arm/arm/smccc.h>
 #include <arm/arm/psci.h>
 #include <arm/fdt/psci_fdtvar.h>
 
diff -r 0b748ae27918 -r 14c10096a20f sys/arch/arm/arm/psci.c
--- a/sys/arch/arm/arm/psci.c   Fri Aug 06 18:15:48 2021 +0000
+++ b/sys/arch/arm/arm/psci.c   Fri Aug 06 19:38:53 2021 +0000
@@ -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 <jmcneill%invisible.ca@localhost>
@@ -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 @@
 #else
 #define        PSCI_CPU_ON             0x84000003
 #endif
+#define        PSCI_FEATURES           0x8400000a
 
 static psci_fn psci_call_fn;
 
@@ -53,9 +54,10 @@
         [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_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)
 {
diff -r 0b748ae27918 -r 14c10096a20f sys/arch/arm/arm/psci.h
--- a/sys/arch/arm/arm/psci.h   Fri Aug 06 18:15:48 2021 +0000
+++ b/sys/arch/arm/arm/psci.h   Fri Aug 06 19:38:53 2021 +0000
@@ -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 <jmcneill%invisible.ca@localhost>
@@ -37,6 +37,7 @@
        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_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 */
diff -r 0b748ae27918 -r 14c10096a20f sys/arch/arm/arm/smccc.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/arm/arm/smccc.c  Fri Aug 06 19:38:53 2021 +0000
@@ -0,0 +1,89 @@
+/* $NetBSD: smccc.c,v 1.1 2021/08/06 19:38:53 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2021 Jared McNeill <jmcneill%invisible.ca@localhost>
+ * 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);
+}
diff -r 0b748ae27918 -r 14c10096a20f sys/arch/arm/arm/smccc.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/arm/arm/smccc.h  Fri Aug 06 19:38:53 2021 +0000
@@ -0,0 +1,55 @@
+/* $NetBSD: smccc.h,v 1.1 2021/08/06 19:38:53 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2021 Jared McNeill <jmcneill%invisible.ca@localhost>
+ * 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 */
diff -r 0b748ae27918 -r 14c10096a20f sys/arch/arm/conf/files.arm
--- a/sys/arch/arm/conf/files.arm       Fri Aug 06 18:15:48 2021 +0000
+++ b/sys/arch/arm/conf/files.arm       Fri Aug 06 19:38:53 2021 +0000
@@ -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 @@



Home | Main Index | Thread Index | Old Index