xref: /netbsd-src/sys/arch/arm/acpi/gic_acpi.c (revision 1695255c36ff99ede21447438896dc7b333bccb3)
1*1695255cSjmcneill /* $NetBSD: gic_acpi.c,v 1.8 2023/11/25 20:17:52 jmcneill Exp $ */
297d57f80Sjmcneill 
397d57f80Sjmcneill /*-
497d57f80Sjmcneill  * Copyright (c) 2018 The NetBSD Foundation, Inc.
597d57f80Sjmcneill  * All rights reserved.
697d57f80Sjmcneill  *
797d57f80Sjmcneill  * This code is derived from software contributed to The NetBSD Foundation
897d57f80Sjmcneill  * by Jared McNeill <jmcneill@invisible.ca>.
997d57f80Sjmcneill  *
1097d57f80Sjmcneill  * Redistribution and use in source and binary forms, with or without
1197d57f80Sjmcneill  * modification, are permitted provided that the following conditions
1297d57f80Sjmcneill  * are met:
1397d57f80Sjmcneill  * 1. Redistributions of source code must retain the above copyright
1497d57f80Sjmcneill  *    notice, this list of conditions and the following disclaimer.
1597d57f80Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
1697d57f80Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
1797d57f80Sjmcneill  *    documentation and/or other materials provided with the distribution.
1897d57f80Sjmcneill  *
1997d57f80Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2097d57f80Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2197d57f80Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2297d57f80Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2397d57f80Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2497d57f80Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2597d57f80Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2697d57f80Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2797d57f80Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2897d57f80Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2997d57f80Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
3097d57f80Sjmcneill  */
3197d57f80Sjmcneill 
32193d42ddSjmcneill #include "pci.h"
33193d42ddSjmcneill 
3497d57f80Sjmcneill #include <sys/cdefs.h>
35*1695255cSjmcneill __KERNEL_RCSID(0, "$NetBSD: gic_acpi.c,v 1.8 2023/11/25 20:17:52 jmcneill Exp $");
3697d57f80Sjmcneill 
3797d57f80Sjmcneill #include <sys/param.h>
3897d57f80Sjmcneill #include <sys/bus.h>
3997d57f80Sjmcneill #include <sys/cpu.h>
4097d57f80Sjmcneill #include <sys/device.h>
41187fa5a9Sjmcneill #include <sys/kmem.h>
4297d57f80Sjmcneill 
4397d57f80Sjmcneill #include <dev/acpi/acpireg.h>
4497d57f80Sjmcneill #include <dev/acpi/acpivar.h>
4597d57f80Sjmcneill 
4697755cb2Sjmcneill #include <dev/fdt/fdtvar.h>
4797755cb2Sjmcneill 
4897d57f80Sjmcneill #include <arm/cortex/gic_intr.h>
49187fa5a9Sjmcneill #include <arm/cortex/gic_reg.h>
50187fa5a9Sjmcneill #include <arm/cortex/gic_v2m.h>
5197d57f80Sjmcneill #include <arm/cortex/mpcore_var.h>
5297d57f80Sjmcneill 
5397755cb2Sjmcneill #include <arm/acpi/gic_v2m_acpi.h>
5497d57f80Sjmcneill 
5597d57f80Sjmcneill #define	GICD_SIZE		0x1000
5697d57f80Sjmcneill #define	GICC_SIZE		0x1000
5797d57f80Sjmcneill 
5897d57f80Sjmcneill extern struct bus_space arm_generic_bs_tag;
59187fa5a9Sjmcneill extern struct pic_softc *pic_list[];
6097d57f80Sjmcneill 
6197d57f80Sjmcneill static int	gic_acpi_match(device_t, cfdata_t, void *);
6297d57f80Sjmcneill static void	gic_acpi_attach(device_t, device_t, void *);
6397d57f80Sjmcneill 
6497d57f80Sjmcneill static ACPI_STATUS gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER *, void *);
6597d57f80Sjmcneill 
6697d57f80Sjmcneill CFATTACH_DECL_NEW(gic_acpi, 0, gic_acpi_match, gic_acpi_attach, NULL, NULL);
6797d57f80Sjmcneill 
6897d57f80Sjmcneill static int
gic_acpi_match(device_t parent,cfdata_t cf,void * aux)6997d57f80Sjmcneill gic_acpi_match(device_t parent, cfdata_t cf, void *aux)
7097d57f80Sjmcneill {
7197d57f80Sjmcneill 	ACPI_SUBTABLE_HEADER *hdrp = aux;
7297d57f80Sjmcneill 	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd;
7397d57f80Sjmcneill 
7497d57f80Sjmcneill 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR)
7597d57f80Sjmcneill 		return 0;
7697d57f80Sjmcneill 
7797d57f80Sjmcneill 	gicd = (ACPI_MADT_GENERIC_DISTRIBUTOR *)hdrp;
7897d57f80Sjmcneill 
7997d57f80Sjmcneill 	switch (gicd->Version) {
8097d57f80Sjmcneill 	case ACPI_MADT_GIC_VERSION_NONE:
8197d57f80Sjmcneill 		return __SHIFTOUT(reg_id_aa64pfr0_el1_read(), ID_AA64PFR0_EL1_GIC) == 0;
8297d57f80Sjmcneill 	case ACPI_MADT_GIC_VERSION_V1:
8397d57f80Sjmcneill 	case ACPI_MADT_GIC_VERSION_V2:
8497d57f80Sjmcneill 		return 1;
8597d57f80Sjmcneill 	default:
8697d57f80Sjmcneill 		return 0;
8797d57f80Sjmcneill 	}
8897d57f80Sjmcneill }
8997d57f80Sjmcneill 
9097d57f80Sjmcneill static void
gic_acpi_attach(device_t parent,device_t self,void * aux)9197d57f80Sjmcneill gic_acpi_attach(device_t parent, device_t self, void *aux)
9297d57f80Sjmcneill {
9397d57f80Sjmcneill 	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd = aux;
9497d57f80Sjmcneill 	ACPI_MADT_GENERIC_INTERRUPT *gicc = NULL;
9597d57f80Sjmcneill 	bus_space_handle_t bsh;
96187fa5a9Sjmcneill 	device_t armgic;
9797d57f80Sjmcneill 	int error;
9897d57f80Sjmcneill 
9997d57f80Sjmcneill 	acpi_madt_walk(gic_acpi_find_gicc, &gicc);
10097d57f80Sjmcneill 	if (gicc == NULL) {
10197d57f80Sjmcneill 		aprint_error(": couldn't find GICC registers\n");
10297d57f80Sjmcneill 		return;
10397d57f80Sjmcneill 	}
10497d57f80Sjmcneill 
105*1695255cSjmcneill 	const bus_addr_t addr = ulmin(gicd->BaseAddress, gicc->BaseAddress);
106*1695255cSjmcneill 	const bus_size_t end = ulmax(gicd->BaseAddress + GICD_SIZE, gicc->BaseAddress + GICC_SIZE);
10797d57f80Sjmcneill 	const bus_size_t size = end - addr;
10897d57f80Sjmcneill 
10997d57f80Sjmcneill 	error = bus_space_map(&arm_generic_bs_tag, addr, size, 0, &bsh);
11097d57f80Sjmcneill 	if (error) {
11197d57f80Sjmcneill 		aprint_error(": couldn't map registers: %d\n", error);
11297d57f80Sjmcneill 		return;
11397d57f80Sjmcneill 	}
11497d57f80Sjmcneill 
11597d57f80Sjmcneill 	aprint_naive("\n");
11697d57f80Sjmcneill 	aprint_normal(": GIC\n");
11797d57f80Sjmcneill 
11897d57f80Sjmcneill 	struct mpcore_attach_args mpcaa = {
11997d57f80Sjmcneill 		.mpcaa_name = "armgic",
12097d57f80Sjmcneill 		.mpcaa_memt = &arm_generic_bs_tag,
12197d57f80Sjmcneill 		.mpcaa_memh = bsh,
12297d57f80Sjmcneill 		.mpcaa_off1 = gicd->BaseAddress - addr,
12397d57f80Sjmcneill 		.mpcaa_off2 = gicc->BaseAddress - addr,
12497d57f80Sjmcneill 	};
12597d57f80Sjmcneill 
126c7fb772bSthorpej 	armgic = config_found(self, &mpcaa, NULL, CFARGS_NONE);
1279e611ae9Sjmcneill 	if (armgic != NULL) {
12897d57f80Sjmcneill 		arm_fdt_irq_set_handler(armgic_irq_handler);
129187fa5a9Sjmcneill 
130193d42ddSjmcneill #if NPCI > 0
13197755cb2Sjmcneill 		acpi_madt_walk(gic_v2m_acpi_find_msi_frame, armgic);
132193d42ddSjmcneill #endif
13397d57f80Sjmcneill 	}
1349e611ae9Sjmcneill }
13597d57f80Sjmcneill 
13697d57f80Sjmcneill static ACPI_STATUS
gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER * hdrp,void * aux)13797d57f80Sjmcneill gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
13897d57f80Sjmcneill {
13997d57f80Sjmcneill 	ACPI_MADT_GENERIC_INTERRUPT **giccp = aux;
14097d57f80Sjmcneill 
14197d57f80Sjmcneill 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
14297d57f80Sjmcneill 		return AE_OK;
14397d57f80Sjmcneill 
14497d57f80Sjmcneill 	*giccp = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
14597d57f80Sjmcneill 
14697d57f80Sjmcneill 	return AE_LIMIT;
14797d57f80Sjmcneill }
148