Module Name:    src
Committed By:   jmcneill
Date:           Fri Oct 12 22:20:04 UTC 2018

Added Files:
        src/sys/arch/arm/acpi: acpi_platform.c cpu_acpi.c files.acpi gic_acpi.c
            gtmr_acpi.c plcom_acpi.c

Log Message:
Add ACPI platform glue and basic device drivers (CPU, GIC, Generic Timer,
SBSA UART).


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/arm/acpi/acpi_platform.c \
    src/sys/arch/arm/acpi/cpu_acpi.c src/sys/arch/arm/acpi/files.acpi \
    src/sys/arch/arm/acpi/gic_acpi.c src/sys/arch/arm/acpi/gtmr_acpi.c \
    src/sys/arch/arm/acpi/plcom_acpi.c

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

Added files:

Index: src/sys/arch/arm/acpi/acpi_platform.c
diff -u /dev/null src/sys/arch/arm/acpi/acpi_platform.c:1.1
--- /dev/null	Fri Oct 12 22:20:04 2018
+++ src/sys/arch/arm/acpi/acpi_platform.c	Fri Oct 12 22:20:04 2018
@@ -0,0 +1,183 @@
+/* $NetBSD: acpi_platform.c,v 1.1 2018/10/12 22:20:04 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: acpi_platform.c,v 1.1 2018/10/12 22:20:04 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/cpu.h>
+#include <sys/device.h>
+#include <sys/termios.h>
+
+#include <dev/fdt/fdtvar.h>
+#include <arm/fdt/arm_fdtvar.h>
+
+#include <uvm/uvm_extern.h>
+
+#include <machine/bootconfig.h>
+#include <arm/cpufunc.h>
+#include <arm/locore.h>
+
+#include <arm/cortex/gtmr_var.h>
+
+#include <arm/arm/psci.h>
+#include <arm/fdt/psci_fdtvar.h>
+
+#include <evbarm/fdt/platform.h>
+
+#include <evbarm/dev/plcomreg.h>
+#include <evbarm/dev/plcomvar.h>
+#include <dev/ic/ns16550reg.h>
+#include <dev/ic/comreg.h>
+
+#include <dev/acpi/acpireg.h>
+#include <dev/acpi/acpivar.h>
+#include <arch/arm/acpi/acpi_table.h>
+
+#define	SPCR_INTERFACE_TYPE_PL011	0x0003
+
+extern struct bus_space arm_generic_bs_tag;
+
+static struct plcom_instance plcom_console;
+
+static const struct pmap_devmap *
+acpi_platform_devmap(void)
+{
+	static const struct pmap_devmap devmap[] = {
+		DEVMAP_ENTRY_END
+	};
+
+	return devmap;
+}
+
+static void
+acpi_platform_bootstrap(void)
+{
+}
+
+static void
+acpi_platform_startup(void)
+{
+	ACPI_TABLE_SPCR *spcr;
+	ACPI_TABLE_FADT *fadt;
+	ACPI_TABLE_MADT *madt;
+
+	/*
+	 * Setup serial console device
+	 */
+	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
+		if (spcr->InterfaceType == SPCR_INTERFACE_TYPE_PL011 &&
+		    spcr->SerialPort.SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
+		    spcr->SerialPort.Address != 0) {
+
+			plcom_console.pi_type = PLCOM_TYPE_PL011;
+			plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
+			plcom_console.pi_iot = &arm_generic_bs_tag;
+			plcom_console.pi_iobase = spcr->SerialPort.Address;
+			plcom_console.pi_size = PL011COM_UART_SIZE;
+
+			plcomcnattach(&plcom_console, 115200 /* XXX */, 0, TTYDEF_CFLAG, -1);
+		}
+		acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
+	}
+
+	/*
+	 * Initialize PSCI 0.2+ if implemented
+	 */
+	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
+		if (fadt->ArmBootFlags & ACPI_FADT_PSCI_COMPLIANT) {
+			if (fadt->ArmBootFlags & ACPI_FADT_PSCI_USE_HVC) {
+				psci_init(psci_call_hvc);
+			} else {
+				psci_init(psci_call_smc);
+			}
+		}
+		acpi_table_unmap((ACPI_TABLE_HEADER *)fadt);
+	}
+
+	/*
+	 * Count CPUs
+	 */
+	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_MADT, (void **)&madt))) {
+		char *end = (char *)madt + madt->Header.Length;
+		char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
+		while (where < end) {
+			ACPI_SUBTABLE_HEADER *subtable = (ACPI_SUBTABLE_HEADER *)where;
+			if (subtable->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
+				arm_cpu_max++;
+			where += subtable->Length;
+		}
+		acpi_table_unmap((ACPI_TABLE_HEADER *)madt);
+	}
+}
+
+static void
+acpi_platform_init_attach_args(struct fdt_attach_args *faa)
+{
+	extern struct arm32_bus_dma_tag arm_generic_dma_tag;
+	extern struct bus_space arm_generic_bs_tag;
+	extern struct bus_space arm_generic_a4x_bs_tag;
+
+	faa->faa_bst = &arm_generic_bs_tag;
+	faa->faa_a4x_bst = &arm_generic_a4x_bs_tag;
+	faa->faa_dmat = &arm_generic_dma_tag;
+}
+
+static void
+acpi_platform_early_putchar(char c)
+{
+}
+
+static void
+acpi_platform_device_register(device_t self, void *aux)
+{
+}
+
+static u_int
+acpi_platform_uart_freq(void)
+{
+	return 0;
+}
+
+static const struct arm_platform acpi_platform = {
+	.ap_devmap = acpi_platform_devmap,
+	.ap_bootstrap = acpi_platform_bootstrap,
+	.ap_startup = acpi_platform_startup,
+	.ap_init_attach_args = acpi_platform_init_attach_args,
+	.ap_early_putchar = acpi_platform_early_putchar,
+	.ap_device_register = acpi_platform_device_register,
+	.ap_reset = psci_fdt_reset,
+	.ap_delay = gtmr_delay,
+	.ap_uart_freq = acpi_platform_uart_freq,
+};
+
+ARM_PLATFORM(virt, "netbsd,generic-acpi", &acpi_platform);
Index: src/sys/arch/arm/acpi/cpu_acpi.c
diff -u /dev/null src/sys/arch/arm/acpi/cpu_acpi.c:1.1
--- /dev/null	Fri Oct 12 22:20:04 2018
+++ src/sys/arch/arm/acpi/cpu_acpi.c	Fri Oct 12 22:20:04 2018
@@ -0,0 +1,101 @@
+/* $NetBSD: cpu_acpi.c,v 1.1 2018/10/12 22:20:04 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: cpu_acpi.c,v 1.1 2018/10/12 22:20:04 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/cpu.h>
+#include <sys/device.h>
+
+#include <dev/acpi/acpireg.h>
+#include <dev/acpi/acpivar.h>
+
+#include <arm/armreg.h>
+#include <arm/cpu.h>
+#include <arm/cpufunc.h>
+#include <arm/locore.h>
+
+#include <arm/arm/psci.h>
+
+static int	cpu_acpi_match(device_t, cfdata_t, void *);
+static void	cpu_acpi_attach(device_t, device_t, void *);
+
+CFATTACH_DECL_NEW(cpu_acpi, 0, cpu_acpi_match, cpu_acpi_attach, NULL, NULL);
+
+static register_t
+cpu_acpi_mpstart_pa(void)
+{
+	extern void aarch64_mpstart(void);
+	return (register_t)aarch64_kern_vtophys((vaddr_t)aarch64_mpstart);
+}
+
+static int
+cpu_acpi_match(device_t parent, cfdata_t cf, void *aux)
+{
+	ACPI_SUBTABLE_HEADER *hdrp = aux;
+
+	return hdrp->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT;
+}
+
+static void
+cpu_acpi_attach(device_t parent, device_t self, void *aux)
+{
+	ACPI_MADT_GENERIC_INTERRUPT *gicc = aux;
+	const uint64_t mpidr = gicc->ArmMpidr;
+	int error;
+
+	if (cpu_mpidr_aff_read() != mpidr) {
+		const u_int cpuindex = device_unit(self);
+
+		cpu_mpidr[cpuindex] = mpidr;
+		cpu_dcache_wb_range((vaddr_t)&cpu_mpidr[cpuindex], sizeof(cpu_mpidr[cpuindex]));
+
+		/* XXX support spin table */
+		error = psci_cpu_on(mpidr, cpu_acpi_mpstart_pa(), 0);
+		if (error != PSCI_SUCCESS) {
+			aprint_error_dev(self, "failed to start CPU\n");
+			return;
+		}
+
+		__asm __volatile("sev" ::: "memory");
+
+		for (u_int i = 0x10000000; i > 0; i--) {
+			membar_consumer();
+			if (arm_cpu_hatched & __BIT(cpuindex))
+				break;
+		}
+	}
+
+	/* Attach the CPU */
+	cpu_attach(self, mpidr);
+}
Index: src/sys/arch/arm/acpi/files.acpi
diff -u /dev/null src/sys/arch/arm/acpi/files.acpi:1.1
--- /dev/null	Fri Oct 12 22:20:04 2018
+++ src/sys/arch/arm/acpi/files.acpi	Fri Oct 12 22:20:04 2018
@@ -0,0 +1,27 @@
+#	$NetBSD: files.acpi,v 1.1 2018/10/12 22:20:04 jmcneill Exp $
+#
+# Configuration info for ACPI compliant ARM boards.
+#
+#
+
+device	fdc { drive = -1 }	# XXX
+include "dev/apm/files.apm"	# XXX
+defflag	opt_pcifixup.h		ACPI_PCI_FIXUP
+
+include "dev/acpi/files.acpi"
+
+file	arch/arm/acpi/acpi_machdep.c		acpi
+file	arch/arm/acpi/acpi_platform.c		acpi
+file	arch/arm/acpi/acpi_table.c		acpi
+
+attach	cpu at acpimadtbus with cpu_acpi
+file	arch/arm/acpi/cpu_acpi.c		cpu_acpi
+
+attach	gic at acpimadtbus with gic_acpi
+file	arch/arm/acpi/gic_acpi.c		gic_acpi
+
+attach	gtmr at acpisdtbus with gtmr_acpi
+file	arch/arm/acpi/gtmr_acpi.c		gtmr_acpi
+
+attach	plcom at acpinodebus with plcom_acpi
+file	arch/arm/acpi/plcom_acpi.c		plcom_acpi
Index: src/sys/arch/arm/acpi/gic_acpi.c
diff -u /dev/null src/sys/arch/arm/acpi/gic_acpi.c:1.1
--- /dev/null	Fri Oct 12 22:20:04 2018
+++ src/sys/arch/arm/acpi/gic_acpi.c	Fri Oct 12 22:20:04 2018
@@ -0,0 +1,133 @@
+/* $NetBSD: gic_acpi.c,v 1.1 2018/10/12 22:20:04 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: gic_acpi.c,v 1.1 2018/10/12 22:20:04 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/cpu.h>
+#include <sys/device.h>
+
+#include <dev/acpi/acpireg.h>
+#include <dev/acpi/acpivar.h>
+
+#include <arm/cortex/gic_intr.h>
+#include <arm/cortex/mpcore_var.h>
+
+#include <dev/fdt/fdtvar.h>
+
+#define	GICD_SIZE	0x1000
+#define	GICC_SIZE	0x1000
+
+extern struct bus_space arm_generic_bs_tag;
+
+static int	gic_acpi_match(device_t, cfdata_t, void *);
+static void	gic_acpi_attach(device_t, device_t, void *);
+
+static ACPI_STATUS gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER *, void *);
+
+CFATTACH_DECL_NEW(gic_acpi, 0, gic_acpi_match, gic_acpi_attach, NULL, NULL);
+
+static int
+gic_acpi_match(device_t parent, cfdata_t cf, void *aux)
+{
+	ACPI_SUBTABLE_HEADER *hdrp = aux;
+	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd;
+
+	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR)
+		return 0;
+
+	gicd = (ACPI_MADT_GENERIC_DISTRIBUTOR *)hdrp;
+
+	switch (gicd->Version) {
+	case ACPI_MADT_GIC_VERSION_NONE:
+		return __SHIFTOUT(reg_id_aa64pfr0_el1_read(), ID_AA64PFR0_EL1_GIC) == 0;
+	case ACPI_MADT_GIC_VERSION_V1:
+	case ACPI_MADT_GIC_VERSION_V2:
+		return 1;
+	default:
+		return 0;
+	}
+}
+
+static void
+gic_acpi_attach(device_t parent, device_t self, void *aux)
+{
+	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd = aux;
+	ACPI_MADT_GENERIC_INTERRUPT *gicc = NULL;
+	bus_space_handle_t bsh;
+	int error;
+
+	acpi_madt_walk(gic_acpi_find_gicc, &gicc);
+	if (gicc == NULL) {
+		aprint_error(": couldn't find GICC registers\n");
+		return;
+	}
+
+	const bus_addr_t addr = uimin(gicd->BaseAddress, gicc->BaseAddress);
+	const bus_size_t end = uimax(gicd->BaseAddress + GICD_SIZE, gicc->BaseAddress + GICC_SIZE);
+	const bus_size_t size = end - addr;
+
+	error = bus_space_map(&arm_generic_bs_tag, addr, size, 0, &bsh);
+	if (error) {
+		aprint_error(": couldn't map registers: %d\n", error);
+		return;
+	}
+
+	aprint_naive("\n");
+	aprint_normal(": GIC\n");
+
+	struct mpcore_attach_args mpcaa = {
+		.mpcaa_name = "armgic",
+		.mpcaa_memt = &arm_generic_bs_tag,
+		.mpcaa_memh = bsh,
+		.mpcaa_off1 = gicd->BaseAddress - addr,
+		.mpcaa_off2 = gicc->BaseAddress - addr,
+	};
+
+	config_found(self, &mpcaa, NULL);
+
+	arm_fdt_irq_set_handler(armgic_irq_handler);
+}
+
+static ACPI_STATUS
+gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
+{
+	ACPI_MADT_GENERIC_INTERRUPT **giccp = aux;
+
+	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
+		return AE_OK;
+
+	*giccp = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
+
+	return AE_LIMIT;
+}
Index: src/sys/arch/arm/acpi/gtmr_acpi.c
diff -u /dev/null src/sys/arch/arm/acpi/gtmr_acpi.c:1.1
--- /dev/null	Fri Oct 12 22:20:04 2018
+++ src/sys/arch/arm/acpi/gtmr_acpi.c	Fri Oct 12 22:20:04 2018
@@ -0,0 +1,101 @@
+/* $NetBSD: gtmr_acpi.c,v 1.1 2018/10/12 22:20:04 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: gtmr_acpi.c,v 1.1 2018/10/12 22:20:04 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/cpu.h>
+#include <sys/device.h>
+
+#include <dev/acpi/acpireg.h>
+#include <dev/acpi/acpivar.h>
+
+#include <arm/cortex/gtmr_intr.h>
+#include <arm/cortex/mpcore_var.h>
+#include <arm/cortex/gtmr_var.h>
+
+#include <dev/fdt/fdtvar.h>
+#include <arm/fdt/arm_fdtvar.h>
+
+extern struct bus_space arm_generic_bs_tag;
+
+static int	gtmr_acpi_match(device_t, cfdata_t, void *);
+static void	gtmr_acpi_attach(device_t, device_t, void *);
+
+static void	gtmr_acpi_cpu_hatch(void *, struct cpu_info *);
+
+CFATTACH_DECL_NEW(gtmr_acpi, 0, gtmr_acpi_match, gtmr_acpi_attach, NULL, NULL);
+
+static int
+gtmr_acpi_match(device_t parent, cfdata_t cf, void *aux)
+{
+	ACPI_TABLE_HEADER *hdrp = aux;
+
+	return memcmp(hdrp->Signature, ACPI_SIG_GTDT, ACPI_NAME_SIZE) == 0;
+}
+
+static void
+gtmr_acpi_attach(device_t parent, device_t self, void *aux)
+{
+	ACPI_TABLE_GTDT *gtdt = aux;
+	struct mpcore_attach_args mpcaa;
+	void *ih;
+
+	const int irq = gtdt->VirtualTimerInterrupt;
+	const int ipl = IPL_CLOCK;
+	const int type = (gtdt->VirtualTimerFlags & ACPI_GTDT_INTERRUPT_MODE) ?
+	    IST_EDGE : IST_LEVEL;
+
+	aprint_naive("\n");
+	aprint_normal(": irq %d\n", irq);
+
+	ih = intr_establish(irq, ipl, type | IST_MPSAFE, gtmr_intr, NULL);
+	if (ih == NULL) {
+		aprint_error_dev(self, "couldn't install interrupt handler\n");
+		return;
+	}
+
+	memset(&mpcaa, 0, sizeof(mpcaa));
+	mpcaa.mpcaa_name = "armgtmr";
+	mpcaa.mpcaa_irq = -1;
+	config_found(self, &mpcaa, NULL);
+
+	arm_fdt_cpu_hatch_register(self, gtmr_acpi_cpu_hatch);
+	arm_fdt_timer_register(gtmr_cpu_initclocks);
+}
+
+static void
+gtmr_acpi_cpu_hatch(void *priv, struct cpu_info *ci)
+{
+	gtmr_init_cpu_clock(ci);
+}
Index: src/sys/arch/arm/acpi/plcom_acpi.c
diff -u /dev/null src/sys/arch/arm/acpi/plcom_acpi.c:1.1
--- /dev/null	Fri Oct 12 22:20:04 2018
+++ src/sys/arch/arm/acpi/plcom_acpi.c	Fri Oct 12 22:20:04 2018
@@ -0,0 +1,121 @@
+/* $NetBSD: plcom_acpi.c,v 1.1 2018/10/12 22:20:04 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: plcom_acpi.c,v 1.1 2018/10/12 22:20:04 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/cpu.h>
+#include <sys/device.h>
+#include <sys/termios.h>
+
+#include <dev/acpi/acpireg.h>
+#include <dev/acpi/acpivar.h>
+
+#include <evbarm/dev/plcomreg.h>
+#include <evbarm/dev/plcomvar.h>
+
+static int	plcom_acpi_match(device_t, cfdata_t, void *);
+static void	plcom_acpi_attach(device_t, device_t, void *);
+
+CFATTACH_DECL_NEW(plcom_acpi, sizeof(struct plcom_softc), plcom_acpi_match, plcom_acpi_attach, NULL, NULL);
+
+static const char * const compatible[] = {
+	"ARMH0011",
+	NULL
+};
+
+static int
+plcom_acpi_match(device_t parent, cfdata_t cf, void *aux)
+{
+	struct acpi_attach_args *aa = aux;
+
+	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
+		return 0;
+
+	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
+}
+
+static void
+plcom_acpi_attach(device_t parent, device_t self, void *aux)
+{
+	struct plcom_softc * const sc = device_private(self);
+	struct acpi_attach_args *aa = aux;
+	struct acpi_resources res;
+	struct acpi_mem *mem;
+	struct acpi_irq *irq;
+	ACPI_STATUS rv;
+	void *ih;
+
+	sc->sc_dev = self;
+
+	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
+	    &res, &acpi_resource_parse_ops_default);
+	if (ACPI_FAILURE(rv))
+		return;
+
+	mem = acpi_res_mem(&res, 0);
+	if (mem == NULL) {
+		aprint_error(": couldn't find mem resource\n");
+		goto done;
+	}
+
+	irq = acpi_res_irq(&res, 0);
+	if (irq == NULL) {
+		aprint_error(": couldn't find irq resource\n");
+		goto done;
+	}
+
+	sc->sc_hwflags = PLCOM_HW_TXFIFO_DISABLE;
+	sc->sc_swflags = 0;
+
+	sc->sc_pi.pi_type = PLCOM_TYPE_PL011;
+	sc->sc_pi.pi_flags = PLC_FLAG_32BIT_ACCESS;
+	sc->sc_pi.pi_iot = aa->aa_memt;
+	sc->sc_pi.pi_iobase = mem->ar_base;
+	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0, &sc->sc_pi.pi_ioh) != 0) {
+		aprint_error(": couldn't map registers\n");
+		goto done;
+	}
+
+	plcom_attach_subr(sc);
+
+	const int type = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
+	ih = intr_establish(irq->ar_irq, IPL_SERIAL, type | IST_MPSAFE, plcomintr, sc);
+	if (ih == NULL) {
+		aprint_error_dev(self, "couldn't install interrupt handler\n");
+		return;
+	}
+
+done:
+	acpi_resource_cleanup(&res);
+}

Reply via email to