OsdInterrupt.c revision 217240
1/*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * Copyright (c) 2011 Jung-uk Kim <jkim@FreeBSD.org>
5 * All rights reserved.
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/*
30 * 6.5 : Interrupt handling
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/acpica/Osd/OsdInterrupt.c 217240 2011-01-10 21:01:41Z jkim $");
35
36#include <sys/param.h>
37#include <sys/kernel.h>
38#include <sys/bus.h>
39#include <sys/lock.h>
40#include <sys/malloc.h>
41#include <sys/mutex.h>
42#include <machine/bus.h>
43#include <machine/resource.h>
44#include <sys/rman.h>
45
46#include <contrib/dev/acpica/include/acpi.h>
47#include <contrib/dev/acpica/include/accommon.h>
48
49#include <dev/acpica/acpivar.h>
50
51#define _COMPONENT	ACPI_OS_SERVICES
52ACPI_MODULE_NAME("INTERRUPT")
53
54MALLOC_DEFINE(M_ACPIINTR, "acpiintr", "ACPI interrupt");
55
56struct acpi_intr {
57	SLIST_ENTRY(acpi_intr)	ai_link;
58	struct resource		*ai_irq;
59	int			ai_rid;
60	void			*ai_handle;
61	int			ai_number;
62	ACPI_OSD_HANDLER	ai_handler;
63	void			*ai_context;
64};
65static SLIST_HEAD(, acpi_intr) acpi_intr_list =
66    SLIST_HEAD_INITIALIZER(acpi_intr_list);
67static struct mtx	acpi_intr_lock;
68
69static UINT32		InterruptOverride;
70
71static void
72acpi_intr_init(struct mtx *lock)
73{
74
75	mtx_init(lock, "ACPI interrupt lock", NULL, MTX_DEF);
76}
77
78SYSINIT(acpi_intr, SI_SUB_DRIVERS, SI_ORDER_FIRST, acpi_intr_init,
79    &acpi_intr_lock);
80
81static int
82acpi_intr_handler(void *arg)
83{
84	struct acpi_intr *ai;
85
86	ai = 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);
92}
93
94ACPI_STATUS
95AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,
96    ACPI_OSD_HANDLER ServiceRoutine, void *Context)
97{
98	struct acpi_softc *sc;
99	struct acpi_intr *ai, *ap;
100
101	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
102
103	sc = devclass_get_softc(devclass_find("acpi"), 0);
104	KASSERT(sc != NULL && sc->acpi_dev != NULL,
105	    ("can't find ACPI device to register interrupt"));
106
107	if (InterruptNumber < 0 || InterruptNumber > 255 ||
108	    ServiceRoutine == NULL)
109		return_ACPI_STATUS (AE_BAD_PARAMETER);
110
111	ai = malloc(sizeof(*ai), M_ACPIINTR, M_WAITOK | M_ZERO);
112	mtx_lock(&acpi_intr_lock);
113	SLIST_FOREACH(ap, &acpi_intr_list, ai_link) {
114		if (InterruptNumber == ap->ai_number ||
115		    (InterruptNumber == InterruptOverride &&
116		    InterruptNumber != AcpiGbl_FADT.SciInterrupt)) {
117			mtx_unlock(&acpi_intr_lock);
118			free(ai, M_ACPIINTR);
119			return_ACPI_STATUS (AE_ALREADY_EXISTS);
120		}
121		if (ai->ai_rid <= ap->ai_rid)
122			ai->ai_rid = ap->ai_rid + 1;
123	}
124	ai->ai_number = InterruptNumber;
125	ai->ai_handler = ServiceRoutine;
126	ai->ai_context = Context;
127
128	/*
129	 * If the MADT contained an interrupt override directive for the SCI,
130	 * we use that value instead of the one from the FADT.
131	 */
132	if (InterruptOverride != 0 &&
133	    InterruptNumber == AcpiGbl_FADT.SciInterrupt) {
134		device_printf(sc->acpi_dev,
135		    "Overriding SCI from IRQ %u to IRQ %u\n",
136		    InterruptNumber, InterruptOverride);
137		InterruptNumber = InterruptOverride;
138	}
139
140	/* Set up the interrupt resource. */
141	bus_set_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid,
142	    InterruptNumber, 1);
143	ai->ai_irq = bus_alloc_resource_any(sc->acpi_dev, SYS_RES_IRQ,
144	    &ai->ai_rid, RF_SHAREABLE | RF_ACTIVE);
145	if (ai->ai_irq == NULL) {
146		device_printf(sc->acpi_dev, "could not allocate interrupt\n");
147		goto error;
148	}
149	if (bus_setup_intr(sc->acpi_dev, ai->ai_irq,
150	    INTR_TYPE_MISC | INTR_MPSAFE, acpi_intr_handler, NULL, ai,
151	    &ai->ai_handle) != 0) {
152		device_printf(sc->acpi_dev, "could not set up interrupt\n");
153		goto error;
154	}
155	SLIST_INSERT_HEAD(&acpi_intr_list, ai, ai_link);
156	mtx_unlock(&acpi_intr_lock);
157	return_ACPI_STATUS (AE_OK);
158
159error:
160	mtx_unlock(&acpi_intr_lock);
161	if (ai->ai_handle != NULL)
162		bus_teardown_intr(sc->acpi_dev, ai->ai_irq, ai->ai_handle);
163	if (ai->ai_irq != NULL)
164		bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid,
165		    ai->ai_irq);
166	bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid);
167	free(ai, M_ACPIINTR);
168	return_ACPI_STATUS (AE_ALREADY_EXISTS);
169}
170
171ACPI_STATUS
172AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber,
173    ACPI_OSD_HANDLER ServiceRoutine)
174{
175	struct acpi_softc *sc;
176	struct acpi_intr *ai;
177
178	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
179
180	sc = devclass_get_softc(devclass_find("acpi"), 0);
181	KASSERT(sc != NULL && sc->acpi_dev != NULL,
182	    ("can't find ACPI device to deregister interrupt"));
183
184	if (InterruptNumber < 0 || InterruptNumber > 255 ||
185	    ServiceRoutine == NULL)
186		return_ACPI_STATUS (AE_BAD_PARAMETER);
187	mtx_lock(&acpi_intr_lock);
188	SLIST_FOREACH(ai, &acpi_intr_list, ai_link)
189		if (InterruptNumber == ai->ai_number) {
190			if (ServiceRoutine != ai->ai_handler) {
191				mtx_unlock(&acpi_intr_lock);
192				return_ACPI_STATUS (AE_BAD_PARAMETER);
193			}
194			SLIST_REMOVE(&acpi_intr_list, ai, acpi_intr, ai_link);
195			break;
196		}
197	mtx_unlock(&acpi_intr_lock);
198	if (ai == NULL)
199		return_ACPI_STATUS (AE_NOT_EXIST);
200	bus_teardown_intr(sc->acpi_dev, ai->ai_irq, ai->ai_handle);
201	bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid, ai->ai_irq);
202	bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid);
203	free(ai, M_ACPIINTR);
204	return_ACPI_STATUS (AE_OK);
205}
206
207ACPI_STATUS
208acpi_OverrideInterruptLevel(UINT32 InterruptNumber)
209{
210
211	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
212
213	if (InterruptOverride != 0)
214		return_ACPI_STATUS (AE_ALREADY_EXISTS);
215	InterruptOverride = InterruptNumber;
216	return_ACPI_STATUS (AE_OK);
217}
218