xref: /netbsd-src/sys/arch/arm/acpi/gic_acpi.c (revision 1695255c36ff99ede21447438896dc7b333bccb3)
1 /* $NetBSD: gic_acpi.c,v 1.8 2023/11/25 20:17:52 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jared McNeill <jmcneill@invisible.ca>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "pci.h"
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: gic_acpi.c,v 1.8 2023/11/25 20:17:52 jmcneill Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/cpu.h>
40 #include <sys/device.h>
41 #include <sys/kmem.h>
42 
43 #include <dev/acpi/acpireg.h>
44 #include <dev/acpi/acpivar.h>
45 
46 #include <dev/fdt/fdtvar.h>
47 
48 #include <arm/cortex/gic_intr.h>
49 #include <arm/cortex/gic_reg.h>
50 #include <arm/cortex/gic_v2m.h>
51 #include <arm/cortex/mpcore_var.h>
52 
53 #include <arm/acpi/gic_v2m_acpi.h>
54 
55 #define	GICD_SIZE		0x1000
56 #define	GICC_SIZE		0x1000
57 
58 extern struct bus_space arm_generic_bs_tag;
59 extern struct pic_softc *pic_list[];
60 
61 static int	gic_acpi_match(device_t, cfdata_t, void *);
62 static void	gic_acpi_attach(device_t, device_t, void *);
63 
64 static ACPI_STATUS gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER *, void *);
65 
66 CFATTACH_DECL_NEW(gic_acpi, 0, gic_acpi_match, gic_acpi_attach, NULL, NULL);
67 
68 static int
gic_acpi_match(device_t parent,cfdata_t cf,void * aux)69 gic_acpi_match(device_t parent, cfdata_t cf, void *aux)
70 {
71 	ACPI_SUBTABLE_HEADER *hdrp = aux;
72 	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd;
73 
74 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR)
75 		return 0;
76 
77 	gicd = (ACPI_MADT_GENERIC_DISTRIBUTOR *)hdrp;
78 
79 	switch (gicd->Version) {
80 	case ACPI_MADT_GIC_VERSION_NONE:
81 		return __SHIFTOUT(reg_id_aa64pfr0_el1_read(), ID_AA64PFR0_EL1_GIC) == 0;
82 	case ACPI_MADT_GIC_VERSION_V1:
83 	case ACPI_MADT_GIC_VERSION_V2:
84 		return 1;
85 	default:
86 		return 0;
87 	}
88 }
89 
90 static void
gic_acpi_attach(device_t parent,device_t self,void * aux)91 gic_acpi_attach(device_t parent, device_t self, void *aux)
92 {
93 	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd = aux;
94 	ACPI_MADT_GENERIC_INTERRUPT *gicc = NULL;
95 	bus_space_handle_t bsh;
96 	device_t armgic;
97 	int error;
98 
99 	acpi_madt_walk(gic_acpi_find_gicc, &gicc);
100 	if (gicc == NULL) {
101 		aprint_error(": couldn't find GICC registers\n");
102 		return;
103 	}
104 
105 	const bus_addr_t addr = ulmin(gicd->BaseAddress, gicc->BaseAddress);
106 	const bus_size_t end = ulmax(gicd->BaseAddress + GICD_SIZE, gicc->BaseAddress + GICC_SIZE);
107 	const bus_size_t size = end - addr;
108 
109 	error = bus_space_map(&arm_generic_bs_tag, addr, size, 0, &bsh);
110 	if (error) {
111 		aprint_error(": couldn't map registers: %d\n", error);
112 		return;
113 	}
114 
115 	aprint_naive("\n");
116 	aprint_normal(": GIC\n");
117 
118 	struct mpcore_attach_args mpcaa = {
119 		.mpcaa_name = "armgic",
120 		.mpcaa_memt = &arm_generic_bs_tag,
121 		.mpcaa_memh = bsh,
122 		.mpcaa_off1 = gicd->BaseAddress - addr,
123 		.mpcaa_off2 = gicc->BaseAddress - addr,
124 	};
125 
126 	armgic = config_found(self, &mpcaa, NULL, CFARGS_NONE);
127 	if (armgic != NULL) {
128 		arm_fdt_irq_set_handler(armgic_irq_handler);
129 
130 #if NPCI > 0
131 		acpi_madt_walk(gic_v2m_acpi_find_msi_frame, armgic);
132 #endif
133 	}
134 }
135 
136 static ACPI_STATUS
gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER * hdrp,void * aux)137 gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
138 {
139 	ACPI_MADT_GENERIC_INTERRUPT **giccp = aux;
140 
141 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
142 		return AE_OK;
143 
144 	*giccp = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
145 
146 	return AE_LIMIT;
147 }
148