xref: /freebsd/sys/arm64/spe/arm_spe_acpi.c (revision 68f185ccc9f8f9498d536f4737d888b37cf11882)
1*68f185ccSZachary Leaf /*-
2*68f185ccSZachary Leaf  * SPDX-License-Identifier: BSD-2-Clause
3*68f185ccSZachary Leaf  *
4*68f185ccSZachary Leaf  * Copyright (c) 2024 Arm Ltd
5*68f185ccSZachary Leaf  * Copyright (c) 2022 The FreeBSD Foundation
6*68f185ccSZachary Leaf  *
7*68f185ccSZachary Leaf  * Portions of this software were developed by Andrew Turner under sponsorship
8*68f185ccSZachary Leaf  * from the FreeBSD Foundation.
9*68f185ccSZachary Leaf  *
10*68f185ccSZachary Leaf  * Redistribution and use in source and binary forms, with or without
11*68f185ccSZachary Leaf  * modification, are permitted provided that the following conditions
12*68f185ccSZachary Leaf  * are met:
13*68f185ccSZachary Leaf  * 1. Redistributions of source code must retain the above copyright
14*68f185ccSZachary Leaf  *    notice, this list of conditions and the following disclaimer.
15*68f185ccSZachary Leaf  * 2. Redistributions in binary form must reproduce the above copyright
16*68f185ccSZachary Leaf  *    notice, this list of conditions and the following disclaimer in the
17*68f185ccSZachary Leaf  *    documentation and/or other materials provided with the distribution.
18*68f185ccSZachary Leaf  *
19*68f185ccSZachary Leaf  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20*68f185ccSZachary Leaf  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*68f185ccSZachary Leaf  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*68f185ccSZachary Leaf  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23*68f185ccSZachary Leaf  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*68f185ccSZachary Leaf  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*68f185ccSZachary Leaf  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*68f185ccSZachary Leaf  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*68f185ccSZachary Leaf  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*68f185ccSZachary Leaf  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*68f185ccSZachary Leaf  * SUCH DAMAGE.
30*68f185ccSZachary Leaf  */
31*68f185ccSZachary Leaf 
32*68f185ccSZachary Leaf #include <sys/param.h>
33*68f185ccSZachary Leaf #include <sys/bus.h>
34*68f185ccSZachary Leaf #include <sys/kernel.h>
35*68f185ccSZachary Leaf #include <sys/lock.h>
36*68f185ccSZachary Leaf #include <sys/module.h>
37*68f185ccSZachary Leaf #include <sys/mutex.h>
38*68f185ccSZachary Leaf 
39*68f185ccSZachary Leaf #include <contrib/dev/acpica/include/acpi.h>
40*68f185ccSZachary Leaf #include <dev/acpica/acpivar.h>
41*68f185ccSZachary Leaf 
42*68f185ccSZachary Leaf #include <arm64/spe/arm_spe_dev.h>
43*68f185ccSZachary Leaf 
44*68f185ccSZachary Leaf static device_identify_t arm_spe_acpi_identify;
45*68f185ccSZachary Leaf static device_probe_t arm_spe_acpi_probe;
46*68f185ccSZachary Leaf 
47*68f185ccSZachary Leaf static device_method_t arm_spe_acpi_methods[] = {
48*68f185ccSZachary Leaf 	/* Device interface */
49*68f185ccSZachary Leaf 	DEVMETHOD(device_identify,      arm_spe_acpi_identify),
50*68f185ccSZachary Leaf 	DEVMETHOD(device_probe,         arm_spe_acpi_probe),
51*68f185ccSZachary Leaf 
52*68f185ccSZachary Leaf 	DEVMETHOD_END,
53*68f185ccSZachary Leaf };
54*68f185ccSZachary Leaf 
55*68f185ccSZachary Leaf DEFINE_CLASS_1(spe, arm_spe_acpi_driver, arm_spe_acpi_methods,
56*68f185ccSZachary Leaf     sizeof(struct arm_spe_softc), arm_spe_driver);
57*68f185ccSZachary Leaf 
58*68f185ccSZachary Leaf DRIVER_MODULE(spe, acpi, arm_spe_acpi_driver, 0, 0);
59*68f185ccSZachary Leaf 
60*68f185ccSZachary Leaf struct madt_data {
61*68f185ccSZachary Leaf 	u_int irq;
62*68f185ccSZachary Leaf 	bool found;
63*68f185ccSZachary Leaf 	bool valid;
64*68f185ccSZachary Leaf };
65*68f185ccSZachary Leaf 
66*68f185ccSZachary Leaf static void
madt_handler(ACPI_SUBTABLE_HEADER * entry,void * arg)67*68f185ccSZachary Leaf madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
68*68f185ccSZachary Leaf {
69*68f185ccSZachary Leaf 	ACPI_MADT_GENERIC_INTERRUPT *intr;
70*68f185ccSZachary Leaf 	struct madt_data *madt_data;
71*68f185ccSZachary Leaf 	u_int irq;
72*68f185ccSZachary Leaf 
73*68f185ccSZachary Leaf 	madt_data = (struct madt_data *)arg;
74*68f185ccSZachary Leaf 
75*68f185ccSZachary Leaf 	/* Exit early if we are have decided to not attach */
76*68f185ccSZachary Leaf 	if (!madt_data->valid)
77*68f185ccSZachary Leaf 		return;
78*68f185ccSZachary Leaf 
79*68f185ccSZachary Leaf 	switch(entry->Type) {
80*68f185ccSZachary Leaf 	case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
81*68f185ccSZachary Leaf 		intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
82*68f185ccSZachary Leaf 		irq = intr->SpeInterrupt;
83*68f185ccSZachary Leaf 
84*68f185ccSZachary Leaf 		if (irq == 0) {
85*68f185ccSZachary Leaf 			madt_data->valid = false;
86*68f185ccSZachary Leaf 		} else if (!madt_data->found) {
87*68f185ccSZachary Leaf 			madt_data->found = true;
88*68f185ccSZachary Leaf 			madt_data->irq = irq;
89*68f185ccSZachary Leaf 		} else if (madt_data->irq != irq) {
90*68f185ccSZachary Leaf 			madt_data->valid = false;
91*68f185ccSZachary Leaf 		}
92*68f185ccSZachary Leaf 		break;
93*68f185ccSZachary Leaf 
94*68f185ccSZachary Leaf 	default:
95*68f185ccSZachary Leaf 		break;
96*68f185ccSZachary Leaf 	}
97*68f185ccSZachary Leaf }
98*68f185ccSZachary Leaf 
99*68f185ccSZachary Leaf static void
arm_spe_acpi_identify(driver_t * driver,device_t parent)100*68f185ccSZachary Leaf arm_spe_acpi_identify(driver_t *driver, device_t parent)
101*68f185ccSZachary Leaf {
102*68f185ccSZachary Leaf 	struct madt_data madt_data;
103*68f185ccSZachary Leaf 	ACPI_TABLE_MADT *madt;
104*68f185ccSZachary Leaf 	device_t dev;
105*68f185ccSZachary Leaf 	vm_paddr_t physaddr;
106*68f185ccSZachary Leaf 
107*68f185ccSZachary Leaf 	physaddr = acpi_find_table(ACPI_SIG_MADT);
108*68f185ccSZachary Leaf 	if (physaddr == 0)
109*68f185ccSZachary Leaf 		return;
110*68f185ccSZachary Leaf 
111*68f185ccSZachary Leaf 	madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
112*68f185ccSZachary Leaf 	if (madt == NULL) {
113*68f185ccSZachary Leaf 		device_printf(parent, "spe: Unable to map the MADT\n");
114*68f185ccSZachary Leaf 		return;
115*68f185ccSZachary Leaf 	}
116*68f185ccSZachary Leaf 
117*68f185ccSZachary Leaf 	madt_data.irq = 0;
118*68f185ccSZachary Leaf 	madt_data.found = false;
119*68f185ccSZachary Leaf 	madt_data.valid = true;
120*68f185ccSZachary Leaf 
121*68f185ccSZachary Leaf 	acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
122*68f185ccSZachary Leaf 	    madt_handler, &madt_data);
123*68f185ccSZachary Leaf 
124*68f185ccSZachary Leaf 	if (!madt_data.found || !madt_data.valid)
125*68f185ccSZachary Leaf 		goto out;
126*68f185ccSZachary Leaf 
127*68f185ccSZachary Leaf 	MPASS(madt_data.irq != 0);
128*68f185ccSZachary Leaf 
129*68f185ccSZachary Leaf 	dev = BUS_ADD_CHILD(parent, 0, "spe", -1);
130*68f185ccSZachary Leaf 	if (dev == NULL) {
131*68f185ccSZachary Leaf 		device_printf(parent, "add spe child failed\n");
132*68f185ccSZachary Leaf 		goto out;
133*68f185ccSZachary Leaf 	}
134*68f185ccSZachary Leaf 
135*68f185ccSZachary Leaf 	BUS_SET_RESOURCE(parent, dev, SYS_RES_IRQ, 0, madt_data.irq, 1);
136*68f185ccSZachary Leaf 
137*68f185ccSZachary Leaf out:
138*68f185ccSZachary Leaf 	acpi_unmap_table(madt);
139*68f185ccSZachary Leaf }
140*68f185ccSZachary Leaf 
141*68f185ccSZachary Leaf static int
arm_spe_acpi_probe(device_t dev)142*68f185ccSZachary Leaf arm_spe_acpi_probe(device_t dev)
143*68f185ccSZachary Leaf {
144*68f185ccSZachary Leaf 	device_set_desc(dev, "ARM Statistical Profiling Extension");
145*68f185ccSZachary Leaf 	return (BUS_PROBE_NOWILDCARD);
146*68f185ccSZachary Leaf }
147