xref: /freebsd/sys/arm/arm/pmu_acpi.c (revision 3494f7c0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Val Packett <val@packett.cool>
5  * Copyright (c) 2021 Ruslan Bukin <br@bsdpad.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/rman.h>
34 
35 #include <contrib/dev/acpica/include/acpi.h>
36 #include <dev/acpica/acpivar.h>
37 
38 #include "acpi_bus_if.h"
39 #include "pmu.h"
40 
41 struct madt_ctx {
42 	struct pmu_softc *sc;
43 	int error;
44 	int i;
45 };
46 
47 static void
48 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
49 {
50 	ACPI_MADT_GENERIC_INTERRUPT *intr;
51 	struct intr_map_data_acpi *ad;
52 	struct intr_map_data *data;
53 	struct madt_ctx *ctx;
54 	struct pmu_softc *sc;
55 	struct pcpu *pcpu;
56 	int rid;
57 	int cpuid;
58 	int i;
59 
60 	ctx = arg;
61 	sc = ctx->sc;
62 	rid = ctx->i;
63 	cpuid = -1;
64 
65 	if (ctx->error)
66 		return;
67 
68 	if (entry->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
69 		return;
70 	intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
71 
72 	for (i = 0; i < MAXCPU; i++) {
73 		pcpu = pcpu_find(i);
74 		if (pcpu != NULL && PCPU_GET_MPIDR(pcpu) == intr->ArmMpidr) {
75 			cpuid = i;
76 			break;
77 		}
78 	}
79 
80 	if (cpuid == -1) {
81 		/* pcpu not found. */
82 		device_printf(sc->dev, "MADT: could not find pcpu, "
83 		    "ArmMpidr %lx\n", intr->ArmMpidr);
84 		return;
85 	}
86 
87 	if (bootverbose)
88 		device_printf(sc->dev, "MADT: cpu %d (mpidr %lu) irq %d "
89 		    "%s-triggered\n", cpuid, intr->ArmMpidr,
90 		    intr->PerformanceInterrupt,
91 		    (intr->Flags & ACPI_MADT_PERFORMANCE_IRQ_MODE) ?
92 		    "edge" : "level");
93 
94 	bus_set_resource(sc->dev, SYS_RES_IRQ, ctx->i,
95 	    intr->PerformanceInterrupt, 1);
96 
97 	sc->irq[ctx->i].res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
98 	    &rid, RF_ACTIVE | RF_SHAREABLE);
99 	if (sc->irq[ctx->i].res == NULL) {
100 		device_printf(sc->dev, "Failed to allocate IRQ %d\n", ctx->i);
101 		ctx->error = ENXIO;
102 		return;
103 	}
104 
105 	/*
106 	 * BUS_CONFIG_INTR does nothing on arm64, so we manually set trigger
107 	 * mode.
108 	 */
109 	data = rman_get_virtual(sc->irq[ctx->i].res);
110 	KASSERT(data->type == INTR_MAP_DATA_ACPI, ("Wrong data type"));
111 	ad = (struct intr_map_data_acpi *)data;
112 	ad->trig = (intr->Flags & ACPI_MADT_PERFORMANCE_IRQ_MODE) ?
113 		INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL;
114 	ad->pol = INTR_POLARITY_HIGH;
115 
116 	if (!intr_is_per_cpu(sc->irq[ctx->i].res))
117 		sc->irq[ctx->i].cpuid = cpuid;
118 
119 	ctx->i++;
120 }
121 
122 static void
123 pmu_acpi_identify(driver_t *driver, device_t parent)
124 {
125 	device_t dev;
126 
127 	if (acpi_find_table(ACPI_SIG_MADT) == 0)
128 		return;
129 
130 	dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST,
131 	    "pmu", -1);
132 
133 	if (dev == NULL)
134 		device_printf(parent, "pmu: Unable to add pmu child\n");
135 }
136 
137 static int
138 pmu_acpi_probe(device_t dev)
139 {
140 
141 	device_set_desc(dev, "Performance Monitoring Unit");
142 
143 	return (BUS_PROBE_NOWILDCARD);
144 }
145 
146 static int
147 pmu_acpi_attach(device_t dev)
148 {
149 	struct pmu_softc *sc;
150 	struct madt_ctx ctx;
151 	ACPI_TABLE_MADT *madt;
152 	int i;
153 
154 	sc = device_get_softc(dev);
155 	sc->dev = dev;
156 
157 	madt = acpi_map_table(acpi_find_table(ACPI_SIG_MADT), ACPI_SIG_MADT);
158 	if (madt == NULL) {
159 		device_printf(dev, "Unable to map the MADT table\n");
160 		return (ENXIO);
161 	}
162 
163 	/* We have to initialize cpuid to -1. */
164 	for (i = 0; i < MAX_RLEN; i++)
165 		sc->irq[i].cpuid = -1;
166 
167 	ctx.sc = sc;
168 	ctx.i = 0;
169 	ctx.error = 0;
170 	acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
171 	    madt_handler, &ctx);
172 
173 	acpi_unmap_table(madt);
174 
175 	if (ctx.error)
176 		return (ctx.error);
177 
178 	return (pmu_attach(dev));
179 }
180 
181 static device_method_t pmu_acpi_methods[] = {
182 	DEVMETHOD(device_identify,	pmu_acpi_identify),
183 	DEVMETHOD(device_probe,		pmu_acpi_probe),
184 	DEVMETHOD(device_attach,	pmu_acpi_attach),
185 	DEVMETHOD_END,
186 };
187 
188 DEFINE_CLASS_0(pmu, pmu_acpi_driver, pmu_acpi_methods,
189     sizeof(struct pmu_softc));
190 
191 DRIVER_MODULE(pmu, acpi, pmu_acpi_driver, 0, 0);
192