Module Name:    src
Committed By:   jmcneill
Date:           Sun Oct 28 10:21:42 UTC 2018

Modified Files:
        src/sys/arch/aarch64/conf: files.aarch64
        src/sys/arch/arm/acpi: acpi_platform.c
        src/sys/arch/arm/conf: files.arm
        src/sys/arch/arm/fdt: acpi_fdt.c
        src/sys/arch/evbarm/conf: GENERIC64
        src/sys/arch/evbarm/fdt: fdt_machdep.c
Added Files:
        src/sys/arch/aarch64/aarch64: efi_machdep.c
        src/sys/arch/arm/arm: efi_runtime.c efi_runtime.h
        src/sys/arch/arm/include: efi.h

Log Message:
Add support for EFI runtime services on aarch64.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/aarch64/aarch64/efi_machdep.c
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/aarch64/conf/files.aarch64
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/arm/acpi/acpi_platform.c
cvs rdiff -u -r0 -r1.1 src/sys/arch/arm/arm/efi_runtime.c \
    src/sys/arch/arm/arm/efi_runtime.h
cvs rdiff -u -r1.145 -r1.146 src/sys/arch/arm/conf/files.arm
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/arm/fdt/acpi_fdt.c
cvs rdiff -u -r0 -r1.1 src/sys/arch/arm/include/efi.h
cvs rdiff -u -r1.60 -r1.61 src/sys/arch/evbarm/conf/GENERIC64
cvs rdiff -u -r1.45 -r1.46 src/sys/arch/evbarm/fdt/fdt_machdep.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.8 src/sys/arch/aarch64/conf/files.aarch64:1.9
--- src/sys/arch/aarch64/conf/files.aarch64:1.8	Thu Oct 18 09:01:51 2018
+++ src/sys/arch/aarch64/conf/files.aarch64	Sun Oct 28 10:21:42 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: files.aarch64,v 1.8 2018/10/18 09:01:51 skrll Exp $
+#	$NetBSD: files.aarch64,v 1.9 2018/10/28 10:21:42 jmcneill Exp $
 
 defflag opt_cpuoptions.h	AARCH64_ALIGNMENT_CHECK
 defflag opt_cpuoptions.h	AARCH64_EL0_STACK_ALIGNMENT_CHECK
@@ -45,6 +45,10 @@ device	psci
 file	arch/arm/arm/psci.c			psci
 file	arch/arm/arm/psci_arm.S			psci
 
+# EFI support
+defflag	opt_efi.h		EFI_RUNTIME
+file	arch/arm/arm/efi_runtime.c		efi_runtime
+
 # PMAP_DEBUG (heavily abused option)
 defflag				PMAP_DEBUG
 
@@ -110,6 +114,9 @@ file	arch/aarch64/aarch64/pmapboot.c
 file	arch/aarch64/aarch64/pmap_page.S
 #file	uvm/pmap/pmap_pvt.c
 
+# EFI runtime (machdep)
+file	arch/aarch64/aarch64/efi_machdep.c		efi_runtime
+
 # cyclecounter
 #file	arch/aarch64/aarch64/cctr_machdep.c
 #file	kern/kern_cctr.c

Index: src/sys/arch/arm/acpi/acpi_platform.c
diff -u src/sys/arch/arm/acpi/acpi_platform.c:1.4 src/sys/arch/arm/acpi/acpi_platform.c:1.5
--- src/sys/arch/arm/acpi/acpi_platform.c:1.4	Fri Oct 19 15:29:00 2018
+++ src/sys/arch/arm/acpi/acpi_platform.c	Sun Oct 28 10:21:42 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: acpi_platform.c,v 1.4 2018/10/19 15:29:00 jmcneill Exp $ */
+/* $NetBSD: acpi_platform.c,v 1.5 2018/10/28 10:21:42 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -31,9 +31,10 @@
 
 #include "com.h"
 #include "plcom.h"
+#include "opt_efi.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.4 2018/10/19 15:29:00 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.5 2018/10/28 10:21:42 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -63,9 +64,15 @@ __KERNEL_RCSID(0, "$NetBSD: acpi_platfor
 #include <dev/ic/comreg.h>
 #include <dev/ic/comvar.h>
 
+#ifdef EFI_RUNTIME
+#include <arm/arm/efi_runtime.h>
+#endif
+
 #include <dev/acpi/acpireg.h>
 #include <dev/acpi/acpivar.h>
-#include <arch/arm/acpi/acpi_table.h>
+#include <arm/acpi/acpi_table.h>
+
+#include <libfdt.h>
 
 #define	SPCR_INTERFACE_TYPE_PL011		0x0003
 #define	SPCR_INTERFACE_TYPE_SBSA_32BIT		0x000d
@@ -219,6 +226,10 @@ acpi_platform_device_register(device_t s
 static void
 acpi_platform_reset(void)
 {
+#ifdef EFI_RUNTIME
+	if (arm_efirt_reset(EFI_RESET_COLD) == 0)
+		return;
+#endif
 	if (psci_available())
 		psci_system_reset();
 }

Index: src/sys/arch/arm/conf/files.arm
diff -u src/sys/arch/arm/conf/files.arm:1.145 src/sys/arch/arm/conf/files.arm:1.146
--- src/sys/arch/arm/conf/files.arm:1.145	Thu Oct 18 09:01:52 2018
+++ src/sys/arch/arm/conf/files.arm	Sun Oct 28 10:21:42 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: files.arm,v 1.145 2018/10/18 09:01:52 skrll Exp $
+#	$NetBSD: files.arm,v 1.146 2018/10/28 10:21:42 jmcneill Exp $
 
 # temporary define to allow easy moving to ../arch/arm/arm32
 defflag				ARM32
@@ -108,6 +108,10 @@ device	psci
 file	arch/arm/arm/psci.c			psci
 file	arch/arm/arm/psci_arm.S			psci
 
+# EFI support
+defflag	opt_efi.h		EFI_RUNTIME
+file	arch/arm/arm/efi_runtime.c		efi_runtime
+
 # PMAP_DEBUG (heavily abused option)
 defflag				PMAP_DEBUG
 

Index: src/sys/arch/arm/fdt/acpi_fdt.c
diff -u src/sys/arch/arm/fdt/acpi_fdt.c:1.6 src/sys/arch/arm/fdt/acpi_fdt.c:1.7
--- src/sys/arch/arm/fdt/acpi_fdt.c:1.6	Tue Oct 23 10:13:34 2018
+++ src/sys/arch/arm/fdt/acpi_fdt.c	Sun Oct 28 10:21:42 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: acpi_fdt.c,v 1.6 2018/10/23 10:13:34 jmcneill Exp $ */
+/* $NetBSD: acpi_fdt.c,v 1.7 2018/10/28 10:21:42 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2015-2017 Jared McNeill <jmcne...@invisible.ca>
@@ -26,8 +26,10 @@
  * SUCH DAMAGE.
  */
 
+#include "opt_efi.h"
+
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_fdt.c,v 1.6 2018/10/23 10:13:34 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_fdt.c,v 1.7 2018/10/28 10:21:42 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -49,17 +51,32 @@ __KERNEL_RCSID(0, "$NetBSD: acpi_fdt.c,v
 
 #include <arm/acpi/acpi_pci_machdep.h>
 
+#ifdef EFI_RUNTIME
+#include <arm/arm/efi_runtime.h>
+#include <dev/clock_subr.h>
+#endif
+
 static int	acpi_fdt_match(device_t, cfdata_t, void *);
 static void	acpi_fdt_attach(device_t, device_t, void *);
 
 static void	acpi_fdt_poweroff(device_t);
 
+#ifdef EFI_RUNTIME
+static void	acpi_fdt_efi_init(device_t);
+static int	acpi_fdt_efi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
+static int	acpi_fdt_efi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
+#endif
+
 static void	acpi_fdt_sysctl_init(void);
 
 static struct acpi_pci_context acpi_fdt_pci_context;
 
 static uint64_t smbios_table = 0;
 
+#ifdef EFI_RUNTIME
+static struct todr_chip_handle efi_todr;
+#endif
+
 static const char * const compatible[] = {
 	"netbsd,acpi",
 	NULL
@@ -86,7 +103,11 @@ acpi_fdt_attach(device_t parent, device_
 	struct acpibus_attach_args aa;
 
 	aprint_naive("\n");
-	aprint_normal(": ACPI Platform support\n");
+	aprint_normal("\n");
+
+#ifdef EFI_RUNTIME
+	acpi_fdt_efi_init(self);
+#endif
 
 	fdtbus_register_power_controller(self, faa->faa_phandle,
 	    &acpi_fdt_power_funcs);
@@ -122,6 +143,10 @@ static void
 acpi_fdt_poweroff(device_t dev)
 {
 	delay(500000);
+#ifdef EFI_RUNTIME
+	if (arm_efirt_reset(EFI_RESET_SHUTDOWN) == 0)
+		return;
+#endif
 	if (psci_available())
 		psci_system_off();
 }
@@ -150,3 +175,71 @@ acpi_fdt_sysctl_init(void)
 		    CTL_CREATE, CTL_EOL);
 	}
 }
+
+#ifdef EFI_RUNTIME
+static void
+acpi_fdt_efi_init(device_t dev)
+{
+	uint64_t efi_system_table;
+	struct efi_tm tm;
+	int error;
+
+	const int chosen = OF_finddevice("/chosen");
+	if (chosen < 0)
+		return;
+
+	if (of_getprop_uint64(chosen, "netbsd,uefi-system-table", &efi_system_table) != 0)
+		return;
+
+	error = arm_efirt_init(efi_system_table);
+	if (error)
+		return;
+
+	aprint_debug_dev(dev, "EFI system table at %#" PRIx64 "\n", efi_system_table);
+
+	if (arm_efirt_gettime(&tm) == 0) {
+		aprint_normal_dev(dev, "using EFI runtime services for RTC\n");
+		efi_todr.cookie = NULL;
+		efi_todr.todr_gettime_ymdhms = acpi_fdt_efi_rtc_gettime;
+		efi_todr.todr_settime_ymdhms = acpi_fdt_efi_rtc_settime;
+		todr_attach(&efi_todr);
+	}
+}
+
+static int
+acpi_fdt_efi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
+{
+	struct efi_tm tm;
+	int error;
+
+	error = arm_efirt_gettime(&tm);
+	if (error)
+		return error;
+
+	dt->dt_year = tm.tm_year;
+	dt->dt_mon = tm.tm_mon;
+	dt->dt_day = tm.tm_mday;
+	dt->dt_wday = 0;
+	dt->dt_hour = tm.tm_hour;
+	dt->dt_min = tm.tm_min;
+	dt->dt_sec = tm.tm_sec;
+
+	return 0;
+}
+
+static int
+acpi_fdt_efi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
+{
+	struct efi_tm tm;
+
+	memset(&tm, 0, sizeof(tm));
+	tm.tm_year = dt->dt_year;
+	tm.tm_mon = dt->dt_mon;
+	tm.tm_mday = dt->dt_day;
+	tm.tm_hour = dt->dt_hour;
+	tm.tm_min = dt->dt_min;
+	tm.tm_sec = dt->dt_sec;
+
+	return arm_efirt_settime(&tm);
+}
+#endif

Index: src/sys/arch/evbarm/conf/GENERIC64
diff -u src/sys/arch/evbarm/conf/GENERIC64:1.60 src/sys/arch/evbarm/conf/GENERIC64:1.61
--- src/sys/arch/evbarm/conf/GENERIC64:1.60	Fri Oct 26 23:34:05 2018
+++ src/sys/arch/evbarm/conf/GENERIC64	Sun Oct 28 10:21:42 2018
@@ -1,5 +1,5 @@
 #
-#	$NetBSD: GENERIC64,v 1.60 2018/10/26 23:34:05 jmcneill Exp $
+#	$NetBSD: GENERIC64,v 1.61 2018/10/28 10:21:42 jmcneill Exp $
 #
 #	GENERIC ARM (aarch64) kernel
 #
@@ -93,6 +93,9 @@ config		netbsd		root on ? type ?
 options 	COMPAT_NETBSD32
 options 	EXEC_ELF32
 
+# EFI runtime support
+options 	EFI_RUNTIME
+
 # Device tree support
 armfdt0		at root
 simplebus*	at fdt? pass 0

Index: src/sys/arch/evbarm/fdt/fdt_machdep.c
diff -u src/sys/arch/evbarm/fdt/fdt_machdep.c:1.45 src/sys/arch/evbarm/fdt/fdt_machdep.c:1.46
--- src/sys/arch/evbarm/fdt/fdt_machdep.c:1.45	Thu Oct 18 17:34:10 2018
+++ src/sys/arch/evbarm/fdt/fdt_machdep.c	Sun Oct 28 10:21:42 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: fdt_machdep.c,v 1.45 2018/10/18 17:34:10 skrll Exp $ */
+/* $NetBSD: fdt_machdep.c,v 1.46 2018/10/28 10:21:42 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2015-2017 Jared McNeill <jmcne...@invisible.ca>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.45 2018/10/18 17:34:10 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.46 2018/10/28 10:21:42 jmcneill Exp $");
 
 #include "opt_machdep.h"
 #include "opt_bootconfig.h"
@@ -36,6 +36,7 @@ __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.
 #include "opt_arm_debug.h"
 #include "opt_multiprocessor.h"
 #include "opt_cpuoptions.h"
+#include "opt_efi.h"
 
 #include "ukbd.h"
 #include "wsdisplay.h"
@@ -82,6 +83,10 @@ __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.
 
 #include <arm/fdt/arm_fdtvar.h>
 
+#ifdef EFI_RUNTIME
+#include <arm/arm/efi_runtime.h>
+#endif
+
 #if NUKBD > 0
 #include <dev/usb/ukbdvar.h>
 #endif
@@ -369,6 +374,31 @@ fdt_setup_initrd(void)
 #endif
 }
 
+#ifdef EFI_RUNTIME
+static void
+fdt_map_efi_runtime(const char *prop, bool exec)
+{
+	int len;
+
+	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
+	if (chosen_off < 0)
+		return;
+
+	const uint64_t *map = fdt_getprop(fdt_data, chosen_off, prop, &len);
+	if (map == NULL)
+		return;
+
+	while (len >= 24) {
+		const paddr_t pa = be64toh(map[0]);
+		const vaddr_t va = be64toh(map[1]);
+		const uint64_t sz = be64toh(map[2]);
+		arm_efirt_md_map_range(va, pa, sz, exec);
+		map += 3;
+		len -= 24;
+	}
+}
+#endif
+
 u_int initarm(void *arg);
 
 u_int
@@ -480,6 +510,11 @@ initarm(void *arg)
 	VPRINTF("%s: fdt_build_bootconfig\n", __func__);
 	fdt_build_bootconfig(memory_start, memory_end);
 
+#ifdef EFI_RUNTIME
+	fdt_map_efi_runtime("netbsd,uefi-runtime-code", true);
+	fdt_map_efi_runtime("netbsd,uefi-runtime-data", false);
+#endif
+
 	/* Perform PT build and VM init */
 	cpu_kernel_vm_init(memory_start, memory_size);
 

Added files:

Index: src/sys/arch/aarch64/aarch64/efi_machdep.c
diff -u /dev/null src/sys/arch/aarch64/aarch64/efi_machdep.c:1.1
--- /dev/null	Sun Oct 28 10:21:42 2018
+++ src/sys/arch/aarch64/aarch64/efi_machdep.c	Sun Oct 28 10:21:42 2018
@@ -0,0 +1,58 @@
+/* $NetBSD: efi_machdep.c,v 1.1 2018/10/28 10:21:42 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jared McNeill <jmcne...@invisible.ca>.
+ *
+ * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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: efi_machdep.c,v 1.1 2018/10/28 10:21:42 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <uvm/uvm_extern.h>
+#include <machine/cpufunc.h>
+
+#include <arm/arm/efi_runtime.h>
+
+void
+arm_efirt_md_map_range(vaddr_t va, paddr_t pa, size_t sz, bool exec)
+{       
+	pt_entry_t attr;
+        
+	attr = LX_BLKPAG_OS_READ | LX_BLKPAG_AF | LX_BLKPAG_UXN | LX_BLKPAG_ATTR_NORMAL_WB;
+	if (exec)
+		attr |= LX_BLKPAG_AP_RO;
+	else    
+		attr |= LX_BLKPAG_AP_RW | LX_BLKPAG_OS_WRITE | LX_BLKPAG_PXN;
+        
+	pmapboot_enter(va, pa, sz, L3_SIZE, attr, 0, bootpage_alloc, NULL);
+	while (sz >= PAGE_SIZE) {
+		aarch64_tlbi_by_va(va);
+		va += PAGE_SIZE;
+		sz -= PAGE_SIZE;
+	}
+}

Index: src/sys/arch/arm/arm/efi_runtime.c
diff -u /dev/null src/sys/arch/arm/arm/efi_runtime.c:1.1
--- /dev/null	Sun Oct 28 10:21:43 2018
+++ src/sys/arch/arm/arm/efi_runtime.c	Sun Oct 28 10:21:42 2018
@@ -0,0 +1,128 @@
+/* $NetBSD: efi_runtime.c,v 1.1 2018/10/28 10:21:42 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jared McNeill <jmcne...@invisible.ca>.
+ *
+ * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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: efi_runtime.c,v 1.1 2018/10/28 10:21:42 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/mutex.h>
+
+#include <uvm/uvm_extern.h>
+
+#include <arm/arm/efi_runtime.h>
+
+static kmutex_t efi_lock;
+
+static struct efi_systbl *ST = NULL;
+static struct efi_rt *RT = NULL;
+
+int
+arm_efirt_init(paddr_t efi_system_table)
+{
+	const size_t sz = PAGE_SIZE * 2;
+	vaddr_t va, cva;
+	paddr_t cpa;
+
+	va = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_VAONLY);
+	if (va == 0) {
+		aprint_error("%s: can't allocate VA\n", __func__);
+		return ENOMEM;
+	}
+	for (cva = va, cpa = trunc_page(efi_system_table);
+	     cva < va + sz;
+	     cva += PAGE_SIZE, cpa += PAGE_SIZE) {
+		pmap_kenter_pa(cva, cpa, VM_PROT_READ, 0);
+	}
+	pmap_update(pmap_kernel());
+
+	ST = (void *)(va + (efi_system_table - trunc_page(efi_system_table)));
+	if (ST->st_hdr.th_sig != EFI_SYSTBL_SIG) {
+		aprint_error("EFI: signature mismatch (%#lx != %#lx)\n",
+		    ST->st_hdr.th_sig, EFI_SYSTBL_SIG);
+		return EINVAL;
+	}
+
+	RT = ST->st_rt;
+	mutex_init(&efi_lock, MUTEX_DEFAULT, IPL_HIGH);
+
+	return 0;
+}
+
+int
+arm_efirt_gettime(struct efi_tm *tm)
+{
+	efi_status status;
+
+	if (RT == NULL || RT->rt_gettime == NULL)
+		return ENXIO;
+
+	mutex_enter(&efi_lock);
+	status = RT->rt_gettime(tm, NULL);
+	mutex_exit(&efi_lock);
+	if (status)
+		return EIO;
+
+	return 0;
+}
+
+int
+arm_efirt_settime(struct efi_tm *tm)
+{
+	efi_status status;
+
+	if (RT == NULL || RT->rt_settime == NULL)
+		return ENXIO;
+
+	mutex_enter(&efi_lock);
+	status = RT->rt_settime(tm);
+	mutex_exit(&efi_lock);
+	if (status)
+		return EIO;
+
+	return 0;
+}
+
+int
+arm_efirt_reset(enum efi_reset type)
+{
+	efi_status status;
+
+	if (RT == NULL || RT->rt_reset == NULL)
+		return ENXIO;
+
+	mutex_enter(&efi_lock);
+	status = RT->rt_reset(type, 0, 0, NULL);
+	mutex_exit(&efi_lock);
+	if (status)
+		return EIO;
+
+	return 0;
+}
Index: src/sys/arch/arm/arm/efi_runtime.h
diff -u /dev/null src/sys/arch/arm/arm/efi_runtime.h:1.1
--- /dev/null	Sun Oct 28 10:21:43 2018
+++ src/sys/arch/arm/arm/efi_runtime.h	Sun Oct 28 10:21:42 2018
@@ -0,0 +1,44 @@
+/* $NetBSD: efi_runtime.h,v 1.1 2018/10/28 10:21:42 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jared McNeill <jmcne...@invisible.ca>.
+ *
+ * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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_EFI_RUNTIME_H
+#define _ARM_EFI_RUNTIME_H
+
+#include <arm/efi.h>
+
+int		arm_efirt_init(paddr_t);
+int		arm_efirt_gettime(struct efi_tm *);
+int		arm_efirt_settime(struct efi_tm *);
+int		arm_efirt_reset(enum efi_reset);
+
+void		arm_efirt_md_map_range(vaddr_t, paddr_t, size_t, bool);
+
+#endif /* !_ARM_EFI_RUNTIME_H */

Index: src/sys/arch/arm/include/efi.h
diff -u /dev/null src/sys/arch/arm/include/efi.h:1.1
--- /dev/null	Sun Oct 28 10:21:43 2018
+++ src/sys/arch/arm/include/efi.h	Sun Oct 28 10:21:42 2018
@@ -0,0 +1,174 @@
+/*     $NetBSD: efi.h,v 1.1 2018/10/28 10:21:42 jmcneill Exp $   */
+
+/*-
+ * Copyright (c) 2004 Marcel Moolenaar
+ * 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.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * This file is mainly x86/include/efi.h, which in turn is mainly
+ * ia64/include/efi.h with little modifications.
+ */
+
+#ifndef _ARM_EFI_H_
+#define _ARM_EFI_H_
+
+#include <sys/uuid.h>
+
+#define        EFI_PAGE_SHIFT          12
+#define        EFI_PAGE_SIZE           (1 << EFI_PAGE_SHIFT)
+#define        EFI_PAGE_MASK           (EFI_PAGE_SIZE - 1)
+
+#define	EFI_TABLE_ACPI20						\
+	{0x8868e871,0xe4f1,0x11d3,0xbc,0x22,{0x00,0x80,0xc7,0x3c,0x88,0x81}}
+#define	EFI_TABLE_ACPI10						\
+	{0xeb9d2d30,0x2d88,0x11d3,0x9a,0x16,{0x00,0x90,0x27,0x3f,0xc1,0x4d}}
+#define	EFI_TABLE_SMBIOS						\
+	{0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,{0x00,0x90,0x27,0x3f,0xc1,0x4d}}
+#define	EFI_TABLE_SMBIOS3						\
+	{0xf2fd1544,0x9794,0x4a2c,0x99,0x2e,{0xe5,0xbb,0xcf,0x20,0xe3,0x94}}
+
+extern const struct uuid EFI_UUID_ACPI20;
+extern const struct uuid EFI_UUID_ACPI10;
+extern const struct uuid EFI_UUID_SMBIOS;
+extern const struct uuid EFI_UUID_SMBIOS3;
+
+extern bool bootmethod_efi;
+
+enum efi_reset {
+	EFI_RESET_COLD,
+	EFI_RESET_WARM,
+	EFI_RESET_SHUTDOWN,
+	EFI_RESET_PLATFORM_SPECIFIC,
+};
+
+typedef uint16_t       efi_char;
+typedef unsigned long efi_status;
+
+struct efi_cfgtbl {
+       struct uuid     ct_uuid;
+       void           *ct_data;
+};
+
+struct efi_md {
+       uint32_t        md_type;
+#define        EFI_MD_TYPE_NULL        0
+#define        EFI_MD_TYPE_CODE        1       /* Loader text. */
+#define        EFI_MD_TYPE_DATA        2       /* Loader data. */
+#define        EFI_MD_TYPE_BS_CODE     3       /* Boot services text. */
+#define        EFI_MD_TYPE_BS_DATA     4       /* Boot services data. */
+#define        EFI_MD_TYPE_RT_CODE     5       /* Runtime services text. */
+#define        EFI_MD_TYPE_RT_DATA     6       /* Runtime services data. */
+#define        EFI_MD_TYPE_FREE        7       /* Unused/free memory. */
+#define        EFI_MD_TYPE_BAD         8       /* Bad memory */
+#define        EFI_MD_TYPE_RECLAIM     9       /* ACPI reclaimable memory. */
+#define        EFI_MD_TYPE_FIRMWARE    10      /* ACPI NV memory */
+#define        EFI_MD_TYPE_IOMEM       11      /* Memory-mapped I/O. */
+#define        EFI_MD_TYPE_IOPORT      12      /* I/O port space. */
+#define        EFI_MD_TYPE_PALCODE     13      /* PAL */
+#define        EFI_MD_TYPE_PMEM        14      /* Persistent memory. */
+       uint32_t        __pad;
+       uint64_t        md_phys;
+       uint64_t        md_virt;
+       uint64_t        md_pages;
+       uint64_t        md_attr;
+#define        EFI_MD_ATTR_UC          0x0000000000000001UL
+#define        EFI_MD_ATTR_WC          0x0000000000000002UL
+#define        EFI_MD_ATTR_WT          0x0000000000000004UL
+#define        EFI_MD_ATTR_WB          0x0000000000000008UL
+#define        EFI_MD_ATTR_UCE         0x0000000000000010UL
+#define        EFI_MD_ATTR_WP          0x0000000000001000UL
+#define        EFI_MD_ATTR_RP          0x0000000000002000UL
+#define        EFI_MD_ATTR_XP          0x0000000000004000UL
+#define        EFI_MD_ATTR_RT          0x8000000000000000UL
+};
+
+struct efi_tm {
+       uint16_t        tm_year;                /* 1998 - 20XX */
+       uint8_t         tm_mon;                 /* 1 - 12 */
+       uint8_t         tm_mday;                /* 1 - 31 */
+       uint8_t         tm_hour;                /* 0 - 23 */
+       uint8_t         tm_min;                 /* 0 - 59 */
+       uint8_t         tm_sec;                 /* 0 - 59 */
+       uint8_t         __pad1;
+       uint32_t        tm_nsec;                /* 0 - 999,999,999 */
+       int16_t         tm_tz;                  /* -1440 to 1440 or 2047 */
+       uint8_t         tm_dst;
+       uint8_t         __pad2;
+};
+
+struct efi_tmcap {
+       uint32_t        tc_res;         /* 1e-6 parts per million */
+       uint32_t        tc_prec;        /* hertz */
+       uint8_t         tc_stz;         /* Set clears sub-second time */
+};
+
+struct efi_tblhdr {
+       uint64_t        th_sig;
+       uint32_t        th_rev;
+       uint32_t        th_hdrsz;
+       uint32_t        th_crc32;
+       uint32_t        __res;
+};
+
+struct efi_rt {
+       struct efi_tblhdr rt_hdr;
+       efi_status      (*rt_gettime)(struct efi_tm *, struct efi_tmcap *);
+       efi_status      (*rt_settime)(struct efi_tm *);
+       efi_status      (*rt_getwaketime)(uint8_t *, uint8_t *,
+           struct efi_tm *);
+       efi_status      (*rt_setwaketime)(uint8_t, struct efi_tm *);
+       efi_status      (*rt_setvirtual)(u_long, u_long, uint32_t,
+           struct efi_md *);
+       efi_status      (*rt_cvtptr)(u_long, void **);
+       efi_status      (*rt_getvar)(efi_char *, struct uuid *, uint32_t *,
+           u_long *, void *);
+       efi_status      (*rt_scanvar)(u_long *, efi_char *, struct uuid *);
+       efi_status      (*rt_setvar)(efi_char *, struct uuid *, uint32_t,
+           u_long, void *);
+       efi_status      (*rt_gethicnt)(uint32_t *);
+       efi_status      (*rt_reset)(enum efi_reset, efi_status, u_long,
+           efi_char *);
+};
+
+struct efi_systbl {
+       struct efi_tblhdr st_hdr;
+#define        EFI_SYSTBL_SIG  0x5453595320494249UL
+       efi_char        *st_fwvendor;
+       uint32_t        st_fwrev;
+       void            *st_cin;
+       void            *st_cinif;
+       void            *st_cout;
+       void            *st_coutif;
+       void            *st_cerr;
+       void            *st_cerrif;
+       struct efi_rt   *st_rt;
+       void            *st_bs;
+       u_long          st_entries;
+       struct efi_cfgtbl *st_cfgtbl;
+};
+
+#endif /* _ARM_EFI_H_ */

Reply via email to