Home | History | Annotate | Line # | Download | only in acpi
gic_acpi.c revision 1.3
      1 /* $NetBSD: gic_acpi.c,v 1.3 2018/11/12 12:56:05 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 "pci.h"
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: gic_acpi.c,v 1.3 2018/11/12 12:56:05 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 <arm/cortex/gic_intr.h>
     47 #include <arm/cortex/gic_reg.h>
     48 #include <arm/cortex/gic_v2m.h>
     49 #include <arm/cortex/mpcore_var.h>
     50 
     51 #include <dev/fdt/fdtvar.h>
     52 
     53 #define	GICD_SIZE		0x1000
     54 #define	GICC_SIZE		0x1000
     55 #define	GICMSIFRAME_SIZE	0x1000
     56 
     57 extern struct bus_space arm_generic_bs_tag;
     58 extern struct pic_softc *pic_list[];
     59 
     60 static int	gic_acpi_match(device_t, cfdata_t, void *);
     61 static void	gic_acpi_attach(device_t, device_t, void *);
     62 
     63 static ACPI_STATUS gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER *, void *);
     64 #if NPCI > 0
     65 static ACPI_STATUS gic_acpi_find_msi_frame(ACPI_SUBTABLE_HEADER *, void *);
     66 #endif
     67 
     68 CFATTACH_DECL_NEW(gic_acpi, 0, gic_acpi_match, gic_acpi_attach, NULL, NULL);
     69 
     70 static int
     71 gic_acpi_match(device_t parent, cfdata_t cf, void *aux)
     72 {
     73 	ACPI_SUBTABLE_HEADER *hdrp = aux;
     74 	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd;
     75 
     76 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR)
     77 		return 0;
     78 
     79 	gicd = (ACPI_MADT_GENERIC_DISTRIBUTOR *)hdrp;
     80 
     81 	switch (gicd->Version) {
     82 	case ACPI_MADT_GIC_VERSION_NONE:
     83 		return __SHIFTOUT(reg_id_aa64pfr0_el1_read(), ID_AA64PFR0_EL1_GIC) == 0;
     84 	case ACPI_MADT_GIC_VERSION_V1:
     85 	case ACPI_MADT_GIC_VERSION_V2:
     86 		return 1;
     87 	default:
     88 		return 0;
     89 	}
     90 }
     91 
     92 static void
     93 gic_acpi_attach(device_t parent, device_t self, void *aux)
     94 {
     95 	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd = aux;
     96 	ACPI_MADT_GENERIC_INTERRUPT *gicc = NULL;
     97 	bus_space_handle_t bsh;
     98 	device_t armgic;
     99 	int error;
    100 
    101 	acpi_madt_walk(gic_acpi_find_gicc, &gicc);
    102 	if (gicc == NULL) {
    103 		aprint_error(": couldn't find GICC registers\n");
    104 		return;
    105 	}
    106 
    107 	const bus_addr_t addr = uimin(gicd->BaseAddress, gicc->BaseAddress);
    108 	const bus_size_t end = uimax(gicd->BaseAddress + GICD_SIZE, gicc->BaseAddress + GICC_SIZE);
    109 	const bus_size_t size = end - addr;
    110 
    111 	error = bus_space_map(&arm_generic_bs_tag, addr, size, 0, &bsh);
    112 	if (error) {
    113 		aprint_error(": couldn't map registers: %d\n", error);
    114 		return;
    115 	}
    116 
    117 	aprint_naive("\n");
    118 	aprint_normal(": GIC\n");
    119 
    120 	struct mpcore_attach_args mpcaa = {
    121 		.mpcaa_name = "armgic",
    122 		.mpcaa_memt = &arm_generic_bs_tag,
    123 		.mpcaa_memh = bsh,
    124 		.mpcaa_off1 = gicd->BaseAddress - addr,
    125 		.mpcaa_off2 = gicc->BaseAddress - addr,
    126 	};
    127 
    128 	armgic = config_found(self, &mpcaa, NULL);
    129 	if (armgic != NULL)
    130 		arm_fdt_irq_set_handler(armgic_irq_handler);
    131 
    132 #if NPCI > 0
    133 	acpi_madt_walk(gic_acpi_find_msi_frame, armgic);
    134 #endif
    135 }
    136 
    137 static ACPI_STATUS
    138 gic_acpi_find_gicc(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
    139 {
    140 	ACPI_MADT_GENERIC_INTERRUPT **giccp = aux;
    141 
    142 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
    143 		return AE_OK;
    144 
    145 	*giccp = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
    146 
    147 	return AE_LIMIT;
    148 }
    149 
    150 #if NPCI > 0
    151 static ACPI_STATUS
    152 gic_acpi_find_msi_frame(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
    153 {
    154 	ACPI_MADT_GENERIC_MSI_FRAME *msi_frame = (ACPI_MADT_GENERIC_MSI_FRAME *)hdrp;
    155 	struct gic_v2m_frame *frame;
    156 	struct pic_softc *pic = pic_list[0];
    157 	device_t armgic = aux;
    158 
    159 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_MSI_FRAME)
    160 		return AE_OK;
    161 
    162 	frame = kmem_zalloc(sizeof(*frame), KM_SLEEP);
    163 	frame->frame_reg = msi_frame->BaseAddress;
    164 	frame->frame_pic = pic;
    165 	if (msi_frame->Flags & ACPI_MADT_OVERRIDE_SPI_VALUES) {
    166 		frame->frame_base = msi_frame->SpiBase;
    167 		frame->frame_count = msi_frame->SpiCount;
    168 	} else {
    169 		bus_space_tag_t bst = &arm_generic_bs_tag;
    170 		bus_space_handle_t bsh;
    171 		if (bus_space_map(bst, frame->frame_reg, GICMSIFRAME_SIZE, 0, &bsh) != 0) {
    172 			printf("%s: failed to map frame\n", __func__);
    173 			return AE_OK;
    174 		}
    175 		const uint32_t typer = bus_space_read_4(bst, bsh, GIC_MSI_TYPER);
    176 		bus_space_unmap(bst, bsh, GICMSIFRAME_SIZE);
    177 
    178 		frame->frame_base = __SHIFTOUT(typer, GIC_MSI_TYPER_BASE);
    179 		frame->frame_count = __SHIFTOUT(typer, GIC_MSI_TYPER_NUMBER);
    180 	}
    181 
    182 	if (gic_v2m_init(frame, armgic, msi_frame->MsiFrameId) != 0)
    183 		aprint_error_dev(armgic, "failed to initialize GICv2m\n");
    184 	else
    185 		aprint_normal_dev(armgic, "GICv2m @ %#" PRIx64 ", SPIs %u-%u\n",
    186 		    (uint64_t)frame->frame_reg, frame->frame_base,
    187 		    frame->frame_base + frame->frame_count);
    188 
    189 	return AE_OK;
    190 }
    191 #endif
    192