1 1.1 jmcneill /* $NetBSD: gic_v2m_acpi.c,v 1.1 2019/10/14 11:00:13 jmcneill Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2018 The NetBSD Foundation, Inc. 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * This code is derived from software contributed to The NetBSD Foundation 8 1.1 jmcneill * by Jared McNeill <jmcneill (at) invisible.ca>. 9 1.1 jmcneill * 10 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 11 1.1 jmcneill * modification, are permitted provided that the following conditions 12 1.1 jmcneill * are met: 13 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 14 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 15 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 17 1.1 jmcneill * documentation and/or other materials provided with the distribution. 18 1.1 jmcneill * 19 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE. 30 1.1 jmcneill */ 31 1.1 jmcneill 32 1.1 jmcneill #include <sys/cdefs.h> 33 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: gic_v2m_acpi.c,v 1.1 2019/10/14 11:00:13 jmcneill Exp $"); 34 1.1 jmcneill 35 1.1 jmcneill #include <sys/param.h> 36 1.1 jmcneill #include <sys/bus.h> 37 1.1 jmcneill #include <sys/cpu.h> 38 1.1 jmcneill #include <sys/device.h> 39 1.1 jmcneill #include <sys/kmem.h> 40 1.1 jmcneill 41 1.1 jmcneill #include <dev/acpi/acpireg.h> 42 1.1 jmcneill #include <dev/acpi/acpivar.h> 43 1.1 jmcneill 44 1.1 jmcneill #include <arm/cortex/gic_v2m.h> 45 1.1 jmcneill 46 1.1 jmcneill #include <arm/acpi/gic_v2m_acpi.h> 47 1.1 jmcneill 48 1.1 jmcneill #define GICMSIFRAME_SIZE 0x1000 49 1.1 jmcneill 50 1.1 jmcneill extern struct bus_space arm_generic_bs_tag; 51 1.1 jmcneill extern struct pic_softc *pic_list[]; 52 1.1 jmcneill 53 1.1 jmcneill static const struct gic_v2m_acpi_quirk { 54 1.1 jmcneill const char q_oemid[ACPI_OEM_ID_SIZE+1]; 55 1.1 jmcneill const char q_oemtableid[ACPI_OEM_TABLE_ID_SIZE+1]; 56 1.1 jmcneill uint32_t q_flags; 57 1.1 jmcneill } gic_v2m_acpi_quirks[] = { 58 1.1 jmcneill { "AMAZON", "GRAVITON", GIC_V2M_FLAG_GRAVITON }, 59 1.1 jmcneill }; 60 1.1 jmcneill 61 1.1 jmcneill static uint32_t 62 1.1 jmcneill gic_v2m_acpi_flags(void) 63 1.1 jmcneill { 64 1.1 jmcneill ACPI_TABLE_MADT *madt; 65 1.1 jmcneill ACPI_STATUS rv; 66 1.1 jmcneill int n; 67 1.1 jmcneill 68 1.1 jmcneill rv = AcpiGetTable(ACPI_SIG_MADT, 0, (ACPI_TABLE_HEADER **)&madt); 69 1.1 jmcneill if (ACPI_FAILURE(rv)) 70 1.1 jmcneill return 0; 71 1.1 jmcneill 72 1.1 jmcneill for (n = 0; n < __arraycount(gic_v2m_acpi_quirks); n++) { 73 1.1 jmcneill const struct gic_v2m_acpi_quirk *q = &gic_v2m_acpi_quirks[n]; 74 1.1 jmcneill if (memcmp(q->q_oemid, madt->Header.OemId, ACPI_OEM_ID_SIZE) == 0 && 75 1.1 jmcneill memcmp(q->q_oemtableid, madt->Header.OemTableId, ACPI_OEM_TABLE_ID_SIZE) == 0) 76 1.1 jmcneill return q->q_flags; 77 1.1 jmcneill } 78 1.1 jmcneill 79 1.1 jmcneill return 0; 80 1.1 jmcneill } 81 1.1 jmcneill 82 1.1 jmcneill ACPI_STATUS 83 1.1 jmcneill gic_v2m_acpi_find_msi_frame(ACPI_SUBTABLE_HEADER *hdrp, void *aux) 84 1.1 jmcneill { 85 1.1 jmcneill ACPI_MADT_GENERIC_MSI_FRAME *msi_frame = (ACPI_MADT_GENERIC_MSI_FRAME *)hdrp; 86 1.1 jmcneill struct gic_v2m_frame *frame; 87 1.1 jmcneill struct pic_softc *pic = pic_list[0]; 88 1.1 jmcneill device_t dev = aux; 89 1.1 jmcneill 90 1.1 jmcneill if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_MSI_FRAME) 91 1.1 jmcneill return AE_OK; 92 1.1 jmcneill 93 1.1 jmcneill frame = kmem_zalloc(sizeof(*frame), KM_SLEEP); 94 1.1 jmcneill frame->frame_reg = msi_frame->BaseAddress; 95 1.1 jmcneill frame->frame_pic = pic; 96 1.1 jmcneill frame->frame_flags = gic_v2m_acpi_flags(); 97 1.1 jmcneill if (msi_frame->Flags & ACPI_MADT_OVERRIDE_SPI_VALUES) { 98 1.1 jmcneill frame->frame_base = msi_frame->SpiBase; 99 1.1 jmcneill frame->frame_count = msi_frame->SpiCount; 100 1.1 jmcneill } else { 101 1.1 jmcneill bus_space_tag_t bst = &arm_generic_bs_tag; 102 1.1 jmcneill bus_space_handle_t bsh; 103 1.1 jmcneill if (bus_space_map(bst, frame->frame_reg, GICMSIFRAME_SIZE, 0, &bsh) != 0) { 104 1.1 jmcneill printf("%s: failed to map frame\n", __func__); 105 1.1 jmcneill return AE_OK; 106 1.1 jmcneill } 107 1.1 jmcneill const uint32_t typer = bus_space_read_4(bst, bsh, GIC_MSI_TYPER); 108 1.1 jmcneill bus_space_unmap(bst, bsh, GICMSIFRAME_SIZE); 109 1.1 jmcneill 110 1.1 jmcneill frame->frame_base = __SHIFTOUT(typer, GIC_MSI_TYPER_BASE); 111 1.1 jmcneill frame->frame_count = __SHIFTOUT(typer, GIC_MSI_TYPER_NUMBER); 112 1.1 jmcneill } 113 1.1 jmcneill 114 1.1 jmcneill if (gic_v2m_init(frame, dev, msi_frame->MsiFrameId) != 0) 115 1.1 jmcneill aprint_error_dev(dev, "failed to initialize GICv2m\n"); 116 1.1 jmcneill else 117 1.1 jmcneill aprint_normal_dev(dev, "GICv2m @ %#" PRIx64 ", SPIs %u-%u\n", 118 1.1 jmcneill (uint64_t)frame->frame_reg, frame->frame_base, 119 1.1 jmcneill frame->frame_base + frame->frame_count); 120 1.1 jmcneill 121 1.1 jmcneill return AE_OK; 122 1.1 jmcneill } 123