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