2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * Copyright (c) 2011 Jung-uk Kim <jkim@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 * 6.5 : Interrupt handling
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
49 #include <dev/acpica/acpivar.h>
51 #define _COMPONENT ACPI_OS_SERVICES
52 ACPI_MODULE_NAME("INTERRUPT")
54 static MALLOC_DEFINE(M_ACPIINTR, "acpiintr", "ACPI interrupt");
57 SLIST_ENTRY(acpi_intr) ai_link;
58 struct resource *ai_irq;
62 ACPI_OSD_HANDLER ai_handler;
65 static SLIST_HEAD(, acpi_intr) acpi_intr_list =
66 SLIST_HEAD_INITIALIZER(acpi_intr_list);
67 static struct mtx acpi_intr_lock;
69 static UINT32 InterruptOverride;
72 acpi_intr_init(struct mtx *lock)
75 mtx_init(lock, "ACPI interrupt lock", NULL, MTX_DEF);
78 SYSINIT(acpi_intr, SI_SUB_DRIVERS, SI_ORDER_FIRST, acpi_intr_init,
82 acpi_intr_handler(void *arg)
87 KASSERT(ai != NULL && ai->ai_handler != NULL,
88 ("invalid ACPI interrupt handler"));
89 if (ai->ai_handler(ai->ai_context) == ACPI_INTERRUPT_HANDLED)
90 return (FILTER_HANDLED);
91 return (FILTER_STRAY);
95 acpi_intr_destroy(device_t dev, struct acpi_intr *ai)
98 if (ai->ai_handle != NULL)
99 bus_teardown_intr(dev, ai->ai_irq, ai->ai_handle);
100 if (ai->ai_irq != NULL)
101 bus_release_resource(dev, SYS_RES_IRQ, ai->ai_rid, ai->ai_irq);
102 bus_delete_resource(dev, SYS_RES_IRQ, ai->ai_rid);
103 free(ai, M_ACPIINTR);
107 AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,
108 ACPI_OSD_HANDLER ServiceRoutine, void *Context)
110 struct acpi_softc *sc;
111 struct acpi_intr *ai, *ap;
113 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
115 sc = devclass_get_softc(devclass_find("acpi"), 0);
116 KASSERT(sc != NULL && sc->acpi_dev != NULL,
117 ("can't find ACPI device to register interrupt"));
119 if (InterruptNumber > 255 || ServiceRoutine == NULL)
120 return_ACPI_STATUS (AE_BAD_PARAMETER);
122 ai = malloc(sizeof(*ai), M_ACPIINTR, M_WAITOK | M_ZERO);
123 mtx_lock(&acpi_intr_lock);
124 SLIST_FOREACH(ap, &acpi_intr_list, ai_link) {
125 if (InterruptNumber == ap->ai_number ||
126 (InterruptNumber == InterruptOverride &&
127 InterruptNumber != AcpiGbl_FADT.SciInterrupt)) {
128 mtx_unlock(&acpi_intr_lock);
129 free(ai, M_ACPIINTR);
130 return_ACPI_STATUS (AE_ALREADY_EXISTS);
132 if (ai->ai_rid <= ap->ai_rid)
133 ai->ai_rid = ap->ai_rid + 1;
135 ai->ai_number = InterruptNumber;
136 ai->ai_handler = ServiceRoutine;
137 ai->ai_context = Context;
138 SLIST_INSERT_HEAD(&acpi_intr_list, ai, ai_link);
139 mtx_unlock(&acpi_intr_lock);
142 * If the MADT contained an interrupt override directive for the SCI,
143 * we use that value instead of the one from the FADT.
145 if (InterruptOverride != 0 &&
146 InterruptNumber == AcpiGbl_FADT.SciInterrupt) {
147 device_printf(sc->acpi_dev,
148 "Overriding SCI from IRQ %u to IRQ %u\n",
149 InterruptNumber, InterruptOverride);
150 InterruptNumber = InterruptOverride;
153 /* Set up the interrupt resource. */
154 bus_set_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid,
156 ai->ai_irq = bus_alloc_resource_any(sc->acpi_dev, SYS_RES_IRQ,
157 &ai->ai_rid, RF_SHAREABLE | RF_ACTIVE);
158 if (ai->ai_irq == NULL) {
159 device_printf(sc->acpi_dev, "could not allocate interrupt\n");
162 if (bus_setup_intr(sc->acpi_dev, ai->ai_irq,
163 INTR_TYPE_MISC | INTR_MPSAFE, acpi_intr_handler, NULL, ai,
164 &ai->ai_handle) != 0) {
165 device_printf(sc->acpi_dev, "could not set up interrupt\n");
168 return_ACPI_STATUS (AE_OK);
171 mtx_lock(&acpi_intr_lock);
172 SLIST_REMOVE(&acpi_intr_list, ai, acpi_intr, ai_link);
173 mtx_unlock(&acpi_intr_lock);
174 acpi_intr_destroy(sc->acpi_dev, ai);
175 return_ACPI_STATUS (AE_ALREADY_EXISTS);
179 AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber,
180 ACPI_OSD_HANDLER ServiceRoutine)
182 struct acpi_softc *sc;
183 struct acpi_intr *ai;
185 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
187 sc = devclass_get_softc(devclass_find("acpi"), 0);
188 KASSERT(sc != NULL && sc->acpi_dev != NULL,
189 ("can't find ACPI device to deregister interrupt"));
191 if (InterruptNumber > 255 || ServiceRoutine == NULL)
192 return_ACPI_STATUS (AE_BAD_PARAMETER);
193 mtx_lock(&acpi_intr_lock);
194 SLIST_FOREACH(ai, &acpi_intr_list, ai_link)
195 if (InterruptNumber == ai->ai_number) {
196 if (ServiceRoutine != ai->ai_handler) {
197 mtx_unlock(&acpi_intr_lock);
198 return_ACPI_STATUS (AE_BAD_PARAMETER);
200 SLIST_REMOVE(&acpi_intr_list, ai, acpi_intr, ai_link);
203 mtx_unlock(&acpi_intr_lock);
205 return_ACPI_STATUS (AE_NOT_EXIST);
206 acpi_intr_destroy(sc->acpi_dev, ai);
207 return_ACPI_STATUS (AE_OK);
211 acpi_OverrideInterruptLevel(UINT32 InterruptNumber)
214 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
216 if (InterruptOverride != 0)
217 return_ACPI_STATUS (AE_ALREADY_EXISTS);
218 InterruptOverride = InterruptNumber;
219 return_ACPI_STATUS (AE_OK);