gic_acpi.c revision 1.1 1 /* $NetBSD: gic_acpi.c,v 1.1 2018/10/12 22:20:04 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 (at) 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 <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: gic_acpi.c,v 1.1 2018/10/12 22:20:04 jmcneill Exp $");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/cpu.h>
38 #include <sys/device.h>
39
40 #include <dev/acpi/acpireg.h>
41 #include <dev/acpi/acpivar.h>
42
43 #include <arm/cortex/gic_intr.h>
44 #include <arm/cortex/mpcore_var.h>
45
46 #include <dev/fdt/fdtvar.h>
47
48 #define GICD_SIZE 0x1000
49 #define GICC_SIZE 0x1000
50
51 extern struct bus_space arm_generic_bs_tag;
52
53 static int gic_acpi_match(device_t, cfdata_t, void *);
54 static void gic_acpi_attach(device_t, device_t, void *);
55
56 static ACPI_STATUS gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER *, void *);
57
58 CFATTACH_DECL_NEW(gic_acpi, 0, gic_acpi_match, gic_acpi_attach, NULL, NULL);
59
60 static int
61 gic_acpi_match(device_t parent, cfdata_t cf, void *aux)
62 {
63 ACPI_SUBTABLE_HEADER *hdrp = aux;
64 ACPI_MADT_GENERIC_DISTRIBUTOR *gicd;
65
66 if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR)
67 return 0;
68
69 gicd = (ACPI_MADT_GENERIC_DISTRIBUTOR *)hdrp;
70
71 switch (gicd->Version) {
72 case ACPI_MADT_GIC_VERSION_NONE:
73 return __SHIFTOUT(reg_id_aa64pfr0_el1_read(), ID_AA64PFR0_EL1_GIC) == 0;
74 case ACPI_MADT_GIC_VERSION_V1:
75 case ACPI_MADT_GIC_VERSION_V2:
76 return 1;
77 default:
78 return 0;
79 }
80 }
81
82 static void
83 gic_acpi_attach(device_t parent, device_t self, void *aux)
84 {
85 ACPI_MADT_GENERIC_DISTRIBUTOR *gicd = aux;
86 ACPI_MADT_GENERIC_INTERRUPT *gicc = NULL;
87 bus_space_handle_t bsh;
88 int error;
89
90 acpi_madt_walk(gic_acpi_find_gicc, &gicc);
91 if (gicc == NULL) {
92 aprint_error(": couldn't find GICC registers\n");
93 return;
94 }
95
96 const bus_addr_t addr = uimin(gicd->BaseAddress, gicc->BaseAddress);
97 const bus_size_t end = uimax(gicd->BaseAddress + GICD_SIZE, gicc->BaseAddress + GICC_SIZE);
98 const bus_size_t size = end - addr;
99
100 error = bus_space_map(&arm_generic_bs_tag, addr, size, 0, &bsh);
101 if (error) {
102 aprint_error(": couldn't map registers: %d\n", error);
103 return;
104 }
105
106 aprint_naive("\n");
107 aprint_normal(": GIC\n");
108
109 struct mpcore_attach_args mpcaa = {
110 .mpcaa_name = "armgic",
111 .mpcaa_memt = &arm_generic_bs_tag,
112 .mpcaa_memh = bsh,
113 .mpcaa_off1 = gicd->BaseAddress - addr,
114 .mpcaa_off2 = gicc->BaseAddress - addr,
115 };
116
117 config_found(self, &mpcaa, NULL);
118
119 arm_fdt_irq_set_handler(armgic_irq_handler);
120 }
121
122 static ACPI_STATUS
123 gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
124 {
125 ACPI_MADT_GENERIC_INTERRUPT **giccp = aux;
126
127 if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
128 return AE_OK;
129
130 *giccp = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
131
132 return AE_LIMIT;
133 }
134