1fd660059SMike Smith /*-
2fd660059SMike Smith * Copyright (c) 2000 Michael Smith
3fd660059SMike Smith * Copyright (c) 2000 BSDi
482bf5557SJung-uk Kim * Copyright (c) 2011 Jung-uk Kim <jkim@FreeBSD.org>
5fd660059SMike Smith * All rights reserved.
6fd660059SMike Smith *
7fd660059SMike Smith * Redistribution and use in source and binary forms, with or without
8fd660059SMike Smith * modification, are permitted provided that the following conditions
9fd660059SMike Smith * are met:
10fd660059SMike Smith * 1. Redistributions of source code must retain the above copyright
11fd660059SMike Smith * notice, this list of conditions and the following disclaimer.
12fd660059SMike Smith * 2. Redistributions in binary form must reproduce the above copyright
13fd660059SMike Smith * notice, this list of conditions and the following disclaimer in the
14fd660059SMike Smith * documentation and/or other materials provided with the distribution.
15fd660059SMike Smith *
16fd660059SMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17fd660059SMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18fd660059SMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19fd660059SMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20fd660059SMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21fd660059SMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22fd660059SMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23fd660059SMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24fd660059SMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25fd660059SMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26fd660059SMike Smith * SUCH DAMAGE.
27fd660059SMike Smith */
28fd660059SMike Smith
29fd660059SMike Smith /*
30fd660059SMike Smith * 6.5 : Interrupt handling
31fd660059SMike Smith */
32fd660059SMike Smith
33e6f06f99SNate Lawson #include <sys/param.h>
34e6f06f99SNate Lawson #include <sys/kernel.h>
35fd660059SMike Smith #include <sys/bus.h>
3682bf5557SJung-uk Kim #include <sys/lock.h>
3782bf5557SJung-uk Kim #include <sys/malloc.h>
3882bf5557SJung-uk Kim #include <sys/mutex.h>
39fd660059SMike Smith #include <machine/bus.h>
40e6f06f99SNate Lawson #include <machine/resource.h>
41fd660059SMike Smith #include <sys/rman.h>
42fd660059SMike Smith
43129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
44129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
45129d3046SJung-uk Kim
46fd660059SMike Smith #include <dev/acpica/acpivar.h>
47fd660059SMike Smith
482a4ac806SMike Smith #define _COMPONENT ACPI_OS_SERVICES
4972e5754cSMike Smith ACPI_MODULE_NAME("INTERRUPT")
50b2e6de72SMike Smith
51*d745c852SEd Schouten static MALLOC_DEFINE(M_ACPIINTR, "acpiintr", "ACPI interrupt");
5282bf5557SJung-uk Kim
5382bf5557SJung-uk Kim struct acpi_intr {
5482bf5557SJung-uk Kim SLIST_ENTRY(acpi_intr) ai_link;
5582bf5557SJung-uk Kim struct resource *ai_irq;
5682bf5557SJung-uk Kim int ai_rid;
5782bf5557SJung-uk Kim void *ai_handle;
5882bf5557SJung-uk Kim int ai_number;
5982bf5557SJung-uk Kim ACPI_OSD_HANDLER ai_handler;
6082bf5557SJung-uk Kim void *ai_context;
6182bf5557SJung-uk Kim };
6282bf5557SJung-uk Kim static SLIST_HEAD(, acpi_intr) acpi_intr_list =
6382bf5557SJung-uk Kim SLIST_HEAD_INITIALIZER(acpi_intr_list);
6482bf5557SJung-uk Kim static struct mtx acpi_intr_lock;
6582bf5557SJung-uk Kim
6682bf5557SJung-uk Kim static UINT32 InterruptOverride;
6782bf5557SJung-uk Kim
6882bf5557SJung-uk Kim static void
acpi_intr_init(struct mtx * lock)6982bf5557SJung-uk Kim acpi_intr_init(struct mtx *lock)
7082bf5557SJung-uk Kim {
7182bf5557SJung-uk Kim
7282bf5557SJung-uk Kim mtx_init(lock, "ACPI interrupt lock", NULL, MTX_DEF);
7382bf5557SJung-uk Kim }
7482bf5557SJung-uk Kim
7582bf5557SJung-uk Kim SYSINIT(acpi_intr, SI_SUB_DRIVERS, SI_ORDER_FIRST, acpi_intr_init,
7682bf5557SJung-uk Kim &acpi_intr_lock);
7782bf5557SJung-uk Kim
7882bf5557SJung-uk Kim static int
acpi_intr_handler(void * arg)7982bf5557SJung-uk Kim acpi_intr_handler(void *arg)
8082bf5557SJung-uk Kim {
8182bf5557SJung-uk Kim struct acpi_intr *ai;
8282bf5557SJung-uk Kim
8382bf5557SJung-uk Kim ai = arg;
8482bf5557SJung-uk Kim KASSERT(ai != NULL && ai->ai_handler != NULL,
8582bf5557SJung-uk Kim ("invalid ACPI interrupt handler"));
8682bf5557SJung-uk Kim if (ai->ai_handler(ai->ai_context) == ACPI_INTERRUPT_HANDLED)
8782bf5557SJung-uk Kim return (FILTER_HANDLED);
8882bf5557SJung-uk Kim return (FILTER_STRAY);
8982bf5557SJung-uk Kim }
90801cc576SJohn Baldwin
915947a0a3SJung-uk Kim static void
acpi_intr_destroy(device_t dev,struct acpi_intr * ai)925947a0a3SJung-uk Kim acpi_intr_destroy(device_t dev, struct acpi_intr *ai)
935947a0a3SJung-uk Kim {
945947a0a3SJung-uk Kim
955947a0a3SJung-uk Kim if (ai->ai_handle != NULL)
965947a0a3SJung-uk Kim bus_teardown_intr(dev, ai->ai_irq, ai->ai_handle);
975947a0a3SJung-uk Kim if (ai->ai_irq != NULL)
985947a0a3SJung-uk Kim bus_release_resource(dev, SYS_RES_IRQ, ai->ai_rid, ai->ai_irq);
995947a0a3SJung-uk Kim bus_delete_resource(dev, SYS_RES_IRQ, ai->ai_rid);
1005947a0a3SJung-uk Kim free(ai, M_ACPIINTR);
1015947a0a3SJung-uk Kim }
1025947a0a3SJung-uk Kim
103fd660059SMike Smith ACPI_STATUS
AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,ACPI_OSD_HANDLER ServiceRoutine,void * Context)104e6f06f99SNate Lawson AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,
105f8c60df7SMark Santcroos ACPI_OSD_HANDLER ServiceRoutine, void *Context)
106fd660059SMike Smith {
107fd660059SMike Smith struct acpi_softc *sc;
10882bf5557SJung-uk Kim struct acpi_intr *ai, *ap;
109fd660059SMike Smith
110b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
111b2e6de72SMike Smith
11282bf5557SJung-uk Kim sc = devclass_get_softc(devclass_find("acpi"), 0);
11382bf5557SJung-uk Kim KASSERT(sc != NULL && sc->acpi_dev != NULL,
11482bf5557SJung-uk Kim ("can't find ACPI device to register interrupt"));
115db302f99SMike Smith
116ea5bef49SJung-uk Kim if (InterruptNumber > 255 || ServiceRoutine == NULL)
117b2e6de72SMike Smith return_ACPI_STATUS (AE_BAD_PARAMETER);
11882bf5557SJung-uk Kim
11982bf5557SJung-uk Kim ai = malloc(sizeof(*ai), M_ACPIINTR, M_WAITOK | M_ZERO);
12082bf5557SJung-uk Kim mtx_lock(&acpi_intr_lock);
12182bf5557SJung-uk Kim SLIST_FOREACH(ap, &acpi_intr_list, ai_link) {
12282bf5557SJung-uk Kim if (InterruptNumber == ap->ai_number ||
12382bf5557SJung-uk Kim (InterruptNumber == InterruptOverride &&
12482bf5557SJung-uk Kim InterruptNumber != AcpiGbl_FADT.SciInterrupt)) {
12582bf5557SJung-uk Kim mtx_unlock(&acpi_intr_lock);
126bd532602SJung-uk Kim free(ai, M_ACPIINTR);
12782bf5557SJung-uk Kim return_ACPI_STATUS (AE_ALREADY_EXISTS);
12882bf5557SJung-uk Kim }
12982bf5557SJung-uk Kim if (ai->ai_rid <= ap->ai_rid)
13082bf5557SJung-uk Kim ai->ai_rid = ap->ai_rid + 1;
13182bf5557SJung-uk Kim }
13282bf5557SJung-uk Kim ai->ai_number = InterruptNumber;
13382bf5557SJung-uk Kim ai->ai_handler = ServiceRoutine;
13482bf5557SJung-uk Kim ai->ai_context = Context;
1355947a0a3SJung-uk Kim SLIST_INSERT_HEAD(&acpi_intr_list, ai, ai_link);
1365947a0a3SJung-uk Kim mtx_unlock(&acpi_intr_lock);
137fd660059SMike Smith
138fd660059SMike Smith /*
139e6f06f99SNate Lawson * If the MADT contained an interrupt override directive for the SCI,
140e6f06f99SNate Lawson * we use that value instead of the one from the FADT.
141fd660059SMike Smith */
14282bf5557SJung-uk Kim if (InterruptOverride != 0 &&
14382bf5557SJung-uk Kim InterruptNumber == AcpiGbl_FADT.SciInterrupt) {
144801cc576SJohn Baldwin device_printf(sc->acpi_dev,
14582bf5557SJung-uk Kim "Overriding SCI from IRQ %u to IRQ %u\n",
146801cc576SJohn Baldwin InterruptNumber, InterruptOverride);
147801cc576SJohn Baldwin InterruptNumber = InterruptOverride;
148801cc576SJohn Baldwin }
149e6f06f99SNate Lawson
150e6f06f99SNate Lawson /* Set up the interrupt resource. */
15182bf5557SJung-uk Kim bus_set_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid,
15282bf5557SJung-uk Kim InterruptNumber, 1);
15382bf5557SJung-uk Kim ai->ai_irq = bus_alloc_resource_any(sc->acpi_dev, SYS_RES_IRQ,
15482bf5557SJung-uk Kim &ai->ai_rid, RF_SHAREABLE | RF_ACTIVE);
15582bf5557SJung-uk Kim if (ai->ai_irq == NULL) {
156e6f06f99SNate Lawson device_printf(sc->acpi_dev, "could not allocate interrupt\n");
157e6f06f99SNate Lawson goto error;
158fd660059SMike Smith }
15982bf5557SJung-uk Kim if (bus_setup_intr(sc->acpi_dev, ai->ai_irq,
16082bf5557SJung-uk Kim INTR_TYPE_MISC | INTR_MPSAFE, acpi_intr_handler, NULL, ai,
16182bf5557SJung-uk Kim &ai->ai_handle) != 0) {
162e6f06f99SNate Lawson device_printf(sc->acpi_dev, "could not set up interrupt\n");
163e6f06f99SNate Lawson goto error;
164fd660059SMike Smith }
165b2e6de72SMike Smith return_ACPI_STATUS (AE_OK);
166e6f06f99SNate Lawson
167e6f06f99SNate Lawson error:
1685947a0a3SJung-uk Kim mtx_lock(&acpi_intr_lock);
1695947a0a3SJung-uk Kim SLIST_REMOVE(&acpi_intr_list, ai, acpi_intr, ai_link);
17082bf5557SJung-uk Kim mtx_unlock(&acpi_intr_lock);
1715947a0a3SJung-uk Kim acpi_intr_destroy(sc->acpi_dev, ai);
172e6f06f99SNate Lawson return_ACPI_STATUS (AE_ALREADY_EXISTS);
173fd660059SMike Smith }
174fd660059SMike Smith
175fd660059SMike Smith ACPI_STATUS
AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber,ACPI_OSD_HANDLER ServiceRoutine)17682bf5557SJung-uk Kim AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber,
17782bf5557SJung-uk Kim ACPI_OSD_HANDLER ServiceRoutine)
178fd660059SMike Smith {
179fd660059SMike Smith struct acpi_softc *sc;
18082bf5557SJung-uk Kim struct acpi_intr *ai;
181fd660059SMike Smith
182b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
183b2e6de72SMike Smith
18482bf5557SJung-uk Kim sc = devclass_get_softc(devclass_find("acpi"), 0);
18582bf5557SJung-uk Kim KASSERT(sc != NULL && sc->acpi_dev != NULL,
18682bf5557SJung-uk Kim ("can't find ACPI device to deregister interrupt"));
187fd660059SMike Smith
188ea5bef49SJung-uk Kim if (InterruptNumber > 255 || ServiceRoutine == NULL)
18982bf5557SJung-uk Kim return_ACPI_STATUS (AE_BAD_PARAMETER);
19082bf5557SJung-uk Kim mtx_lock(&acpi_intr_lock);
19182bf5557SJung-uk Kim SLIST_FOREACH(ai, &acpi_intr_list, ai_link)
19282bf5557SJung-uk Kim if (InterruptNumber == ai->ai_number) {
19382bf5557SJung-uk Kim if (ServiceRoutine != ai->ai_handler) {
19482bf5557SJung-uk Kim mtx_unlock(&acpi_intr_lock);
19582bf5557SJung-uk Kim return_ACPI_STATUS (AE_BAD_PARAMETER);
19682bf5557SJung-uk Kim }
19782bf5557SJung-uk Kim SLIST_REMOVE(&acpi_intr_list, ai, acpi_intr, ai_link);
19882bf5557SJung-uk Kim break;
19982bf5557SJung-uk Kim }
20082bf5557SJung-uk Kim mtx_unlock(&acpi_intr_lock);
20182bf5557SJung-uk Kim if (ai == NULL)
202b2e6de72SMike Smith return_ACPI_STATUS (AE_NOT_EXIST);
2035947a0a3SJung-uk Kim acpi_intr_destroy(sc->acpi_dev, ai);
204b2e6de72SMike Smith return_ACPI_STATUS (AE_OK);
205fd660059SMike Smith }
206fd660059SMike Smith
207801cc576SJohn Baldwin ACPI_STATUS
acpi_OverrideInterruptLevel(UINT32 InterruptNumber)208801cc576SJohn Baldwin acpi_OverrideInterruptLevel(UINT32 InterruptNumber)
209801cc576SJohn Baldwin {
210801cc576SJohn Baldwin
21107f0d09eSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
21207f0d09eSJohn Baldwin
213801cc576SJohn Baldwin if (InterruptOverride != 0)
214801cc576SJohn Baldwin return_ACPI_STATUS (AE_ALREADY_EXISTS);
215801cc576SJohn Baldwin InterruptOverride = InterruptNumber;
216801cc576SJohn Baldwin return_ACPI_STATUS (AE_OK);
217801cc576SJohn Baldwin }
218