1 /* $NetBSD: gic_v2m_acpi.c,v 1.1 2019/10/14 11:00:13 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@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_v2m_acpi.c,v 1.1 2019/10/14 11:00:13 jmcneill Exp $");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/cpu.h>
38 #include <sys/device.h>
39 #include <sys/kmem.h>
40
41 #include <dev/acpi/acpireg.h>
42 #include <dev/acpi/acpivar.h>
43
44 #include <arm/cortex/gic_v2m.h>
45
46 #include <arm/acpi/gic_v2m_acpi.h>
47
48 #define GICMSIFRAME_SIZE 0x1000
49
50 extern struct bus_space arm_generic_bs_tag;
51 extern struct pic_softc *pic_list[];
52
53 static const struct gic_v2m_acpi_quirk {
54 const char q_oemid[ACPI_OEM_ID_SIZE+1];
55 const char q_oemtableid[ACPI_OEM_TABLE_ID_SIZE+1];
56 uint32_t q_flags;
57 } gic_v2m_acpi_quirks[] = {
58 { "AMAZON", "GRAVITON", GIC_V2M_FLAG_GRAVITON },
59 };
60
61 static uint32_t
gic_v2m_acpi_flags(void)62 gic_v2m_acpi_flags(void)
63 {
64 ACPI_TABLE_MADT *madt;
65 ACPI_STATUS rv;
66 int n;
67
68 rv = AcpiGetTable(ACPI_SIG_MADT, 0, (ACPI_TABLE_HEADER **)&madt);
69 if (ACPI_FAILURE(rv))
70 return 0;
71
72 for (n = 0; n < __arraycount(gic_v2m_acpi_quirks); n++) {
73 const struct gic_v2m_acpi_quirk *q = &gic_v2m_acpi_quirks[n];
74 if (memcmp(q->q_oemid, madt->Header.OemId, ACPI_OEM_ID_SIZE) == 0 &&
75 memcmp(q->q_oemtableid, madt->Header.OemTableId, ACPI_OEM_TABLE_ID_SIZE) == 0)
76 return q->q_flags;
77 }
78
79 return 0;
80 }
81
82 ACPI_STATUS
gic_v2m_acpi_find_msi_frame(ACPI_SUBTABLE_HEADER * hdrp,void * aux)83 gic_v2m_acpi_find_msi_frame(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
84 {
85 ACPI_MADT_GENERIC_MSI_FRAME *msi_frame = (ACPI_MADT_GENERIC_MSI_FRAME *)hdrp;
86 struct gic_v2m_frame *frame;
87 struct pic_softc *pic = pic_list[0];
88 device_t dev = aux;
89
90 if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_MSI_FRAME)
91 return AE_OK;
92
93 frame = kmem_zalloc(sizeof(*frame), KM_SLEEP);
94 frame->frame_reg = msi_frame->BaseAddress;
95 frame->frame_pic = pic;
96 frame->frame_flags = gic_v2m_acpi_flags();
97 if (msi_frame->Flags & ACPI_MADT_OVERRIDE_SPI_VALUES) {
98 frame->frame_base = msi_frame->SpiBase;
99 frame->frame_count = msi_frame->SpiCount;
100 } else {
101 bus_space_tag_t bst = &arm_generic_bs_tag;
102 bus_space_handle_t bsh;
103 if (bus_space_map(bst, frame->frame_reg, GICMSIFRAME_SIZE, 0, &bsh) != 0) {
104 printf("%s: failed to map frame\n", __func__);
105 return AE_OK;
106 }
107 const uint32_t typer = bus_space_read_4(bst, bsh, GIC_MSI_TYPER);
108 bus_space_unmap(bst, bsh, GICMSIFRAME_SIZE);
109
110 frame->frame_base = __SHIFTOUT(typer, GIC_MSI_TYPER_BASE);
111 frame->frame_count = __SHIFTOUT(typer, GIC_MSI_TYPER_NUMBER);
112 }
113
114 if (gic_v2m_init(frame, dev, msi_frame->MsiFrameId) != 0)
115 aprint_error_dev(dev, "failed to initialize GICv2m\n");
116 else
117 aprint_normal_dev(dev, "GICv2m @ %#" PRIx64 ", SPIs %u-%u\n",
118 (uint64_t)frame->frame_reg, frame->frame_base,
119 frame->frame_base + frame->frame_count);
120
121 return AE_OK;
122 }
123