Home | History | Annotate | Line # | Download | only in acpi
gicv3_acpi.c revision 1.1
      1 /* $NetBSD: gicv3_acpi.c,v 1.1 2018/10/21 21:18:41 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 #define	_INTR_PRIVATE
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: gicv3_acpi.c,v 1.1 2018/10/21 21:18:41 jmcneill Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/bus.h>
     39 #include <sys/cpu.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <sys/kmem.h>
     43 
     44 #include <dev/acpi/acpireg.h>
     45 #include <dev/acpi/acpivar.h>
     46 
     47 #include <dev/fdt/fdtvar.h>
     48 
     49 #include <arm/cortex/gicv3.h>
     50 #include <arm/cortex/gic_reg.h>
     51 
     52 #define	GICD_SIZE	0x10000
     53 #define	GICR_SIZE	0x20000
     54 
     55 extern struct bus_space arm_generic_bs_tag;
     56 
     57 struct gicv3_acpi_softc {
     58 	struct gicv3_softc	sc_gic;
     59 
     60 	ACPI_MADT_GENERIC_DISTRIBUTOR *sc_madt_gicd;
     61 };
     62 
     63 static int	gicv3_acpi_match(device_t, cfdata_t, void *);
     64 static void	gicv3_acpi_attach(device_t, device_t, void *);
     65 
     66 static int	gicv3_acpi_map_dist(struct gicv3_acpi_softc *);
     67 static int	gicv3_acpi_map_redist(struct gicv3_acpi_softc *);
     68 
     69 CFATTACH_DECL_NEW(gicv3_acpi, sizeof(struct gicv3_acpi_softc), gicv3_acpi_match, gicv3_acpi_attach, NULL, NULL);
     70 
     71 static int
     72 gicv3_acpi_match(device_t parent, cfdata_t cf, void *aux)
     73 {
     74 	ACPI_SUBTABLE_HEADER *hdrp = aux;
     75 	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd;
     76 
     77 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR)
     78 		return 0;
     79 
     80 	gicd = (ACPI_MADT_GENERIC_DISTRIBUTOR *)hdrp;
     81 
     82 	switch (gicd->Version) {
     83 	case ACPI_MADT_GIC_VERSION_NONE:
     84 		return __SHIFTOUT(reg_id_aa64pfr0_el1_read(), ID_AA64PFR0_EL1_GIC) == 1;
     85 	case ACPI_MADT_GIC_VERSION_V3:
     86 	case ACPI_MADT_GIC_VERSION_V4:
     87 		return 1;
     88 	default:
     89 		return 0;
     90 	}
     91 }
     92 
     93 static void
     94 gicv3_acpi_attach(device_t parent, device_t self, void *aux)
     95 {
     96 	struct gicv3_acpi_softc * const sc = device_private(self);
     97 	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd = aux;
     98 	int error;
     99 
    100 	sc->sc_gic.sc_dev = self;
    101 	sc->sc_gic.sc_bst = &arm_generic_bs_tag;
    102 	sc->sc_madt_gicd = gicd;
    103 
    104 	aprint_naive("\n");
    105 	aprint_normal(": GICv3\n");
    106 
    107 	error = gicv3_acpi_map_dist(sc);
    108 	if (error) {
    109 		aprint_error_dev(self, "failed to map distributor: %d\n", error);
    110 		return;
    111 	}
    112 
    113 	error = gicv3_acpi_map_redist(sc);
    114 	if (error) {
    115 		aprint_error_dev(self, "failed to map redistributor: %d\n", error);
    116 		return;
    117 	}
    118 
    119 	error = gicv3_init(&sc->sc_gic);
    120 	if (error) {
    121 		aprint_error_dev(self, "failed to initialize GIC: %d\n", error);
    122 		return;
    123 	}
    124 
    125 	arm_fdt_irq_set_handler(gicv3_irq_handler);
    126 }
    127 
    128 static int
    129 gicv3_acpi_map_dist(struct gicv3_acpi_softc *sc)
    130 {
    131 	const bus_addr_t addr = sc->sc_madt_gicd->BaseAddress;
    132 	const bus_size_t size = GICD_SIZE;
    133 	int error;
    134 
    135 	error = bus_space_map(sc->sc_gic.sc_bst, addr, size, 0, &sc->sc_gic.sc_bsh_d);
    136 	if (error)
    137 		return error;
    138 
    139 	return 0;
    140 }
    141 
    142 static ACPI_STATUS
    143 gicv3_acpi_count_gicr(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
    144 {
    145 	ACPI_MADT_GENERIC_REDISTRIBUTOR *gicr;
    146 	int *count = aux;
    147 
    148 	if (hdrp->Type == ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR) {
    149 		gicr = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)hdrp;
    150 		*count += howmany(gicr->Length, GICR_SIZE);
    151 	}
    152 
    153 	return AE_OK;
    154 }
    155 
    156 static ACPI_STATUS
    157 gicv3_acpi_map_gicr(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
    158 {
    159 	struct gicv3_acpi_softc * const sc = aux;
    160 	ACPI_MADT_GENERIC_REDISTRIBUTOR *gicr;
    161 	bus_space_handle_t bsh;
    162 	bus_size_t off;
    163 
    164 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR)
    165 		return AE_OK;
    166 
    167 	gicr = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)hdrp;
    168 
    169 	if (bus_space_map(sc->sc_gic.sc_bst, gicr->BaseAddress, gicr->Length, 0, &bsh) != 0) {
    170 		aprint_error_dev(sc->sc_gic.sc_dev, "failed to map redistributor at 0x%" PRIx64 " len %#x\n",
    171 		    gicr->BaseAddress, gicr->Length);
    172 		return AE_OK;
    173 	}
    174 
    175 	for (off = 0; off < gicr->Length; off += GICR_SIZE) {
    176 		const int redist = sc->sc_gic.sc_bsh_r_count;
    177 		if (bus_space_subregion(sc->sc_gic.sc_bst, bsh, off, GICR_SIZE, &sc->sc_gic.sc_bsh_r[redist]) != 0) {
    178 			aprint_error_dev(sc->sc_gic.sc_dev, "couldn't subregion redistributor registers\n");
    179 			return AE_OK;
    180 		}
    181 
    182 		aprint_debug_dev(sc->sc_gic.sc_dev, "redist at 0x%" PRIx64 " [GICR]\n", gicr->BaseAddress + off);
    183 
    184 		sc->sc_gic.sc_bsh_r_count++;
    185 
    186 		/* If this is the last redist in this region, skip to the next one */
    187 		const uint32_t typer = bus_space_read_4(sc->sc_gic.sc_bst, sc->sc_gic.sc_bsh_r[redist], GICR_TYPER);
    188 		if (typer & GICR_TYPER_Last)
    189 			break;
    190 	}
    191 
    192 	return AE_OK;
    193 }
    194 
    195 static ACPI_STATUS
    196 gicv3_acpi_count_gicc(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
    197 {
    198 	ACPI_MADT_GENERIC_INTERRUPT *gicc;
    199 	int *count = aux;
    200 
    201 	if (hdrp->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
    202 		gicc = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
    203 		if ((gicc->Flags & ACPI_MADT_ENABLED) != 0)
    204 			(*count)++;
    205 	}
    206 
    207 	return AE_OK;
    208 }
    209 
    210 static ACPI_STATUS
    211 gicv3_acpi_map_gicc(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
    212 {
    213 	struct gicv3_acpi_softc * const sc = aux;
    214 	ACPI_MADT_GENERIC_INTERRUPT *gicc;
    215 
    216 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
    217 		return AE_OK;
    218 
    219 	gicc = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
    220 	if ((gicc->Flags & ACPI_MADT_ENABLED) == 0)
    221 		return AE_OK;
    222 
    223 	const int redist = sc->sc_gic.sc_bsh_r_count;
    224 	if (bus_space_map(sc->sc_gic.sc_bst, gicc->GicrBaseAddress, GICR_SIZE, 0, &sc->sc_gic.sc_bsh_r[redist]) != 0) {
    225 		aprint_error_dev(sc->sc_gic.sc_dev, "failed to map redistributor at 0x%" PRIx64 " len %#x\n",
    226 		    gicc->GicrBaseAddress, GICR_SIZE);
    227 		return AE_OK;
    228 	}
    229 
    230 	aprint_debug_dev(sc->sc_gic.sc_dev, "redist at 0x%" PRIx64 " [GICC]\n", gicc->GicrBaseAddress);
    231 
    232 	sc->sc_gic.sc_bsh_r_count++;
    233 
    234 	return AE_OK;
    235 }
    236 
    237 static int
    238 gicv3_acpi_map_redist(struct gicv3_acpi_softc *sc)
    239 {
    240 	bool use_gicr = false;
    241 	int max_redist = 0;
    242 
    243 	/*
    244 	 * Try to use GICR structures to describe redistributors. If no GICR
    245 	 * subtables are found, use the GICR address from the GICC subtables.
    246 	 */
    247 	acpi_madt_walk(gicv3_acpi_count_gicr, &max_redist);
    248 	if (max_redist != 0)
    249 		use_gicr = true;
    250 	else
    251 		acpi_madt_walk(gicv3_acpi_count_gicc, &max_redist);
    252 
    253 	if (max_redist == 0)
    254 		return ENODEV;
    255 
    256 	sc->sc_gic.sc_bsh_r = kmem_alloc(sizeof(bus_space_handle_t) * max_redist, KM_SLEEP);
    257 	if (use_gicr)
    258 		acpi_madt_walk(gicv3_acpi_map_gicr, sc);
    259 	else
    260 		acpi_madt_walk(gicv3_acpi_map_gicc, sc);
    261 
    262 	if (sc->sc_gic.sc_bsh_r_count == 0)
    263 		return ENXIO;
    264 
    265 	return 0;
    266 }
    267