summaryrefslogtreecommitdiffstats
path: root/sys/dev/acpica/Osd/OsdInterrupt.c
blob: 74db170373b71abc24e72616f4f8aea6e79dd62f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*-
 * Copyright (c) 2000 Michael Smith
 * Copyright (c) 2000 BSDi
 * Copyright (c) 2011 Jung-uk Kim <jkim@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * 6.5 : Interrupt handling
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>

#include <contrib/dev/acpica/include/acpi.h>
#include <contrib/dev/acpica/include/accommon.h>

#include <dev/acpica/acpivar.h>

#define _COMPONENT	ACPI_OS_SERVICES
ACPI_MODULE_NAME("INTERRUPT")

MALLOC_DEFINE(M_ACPIINTR, "acpiintr", "ACPI interrupt");

struct acpi_intr {
	SLIST_ENTRY(acpi_intr)	ai_link;
	struct resource		*ai_irq;
	int			ai_rid;
	void			*ai_handle;
	int			ai_number;
	ACPI_OSD_HANDLER	ai_handler;
	void			*ai_context;
};
static SLIST_HEAD(, acpi_intr) acpi_intr_list =
    SLIST_HEAD_INITIALIZER(acpi_intr_list);
static struct mtx	acpi_intr_lock;

static UINT32		InterruptOverride;

static void
acpi_intr_init(struct mtx *lock)
{

	mtx_init(lock, "ACPI interrupt lock", NULL, MTX_DEF);
}

SYSINIT(acpi_intr, SI_SUB_DRIVERS, SI_ORDER_FIRST, acpi_intr_init,
    &acpi_intr_lock);

static int
acpi_intr_handler(void *arg)
{
	struct acpi_intr *ai;

	ai = arg;
	KASSERT(ai != NULL && ai->ai_handler != NULL,
	    ("invalid ACPI interrupt handler"));
	if (ai->ai_handler(ai->ai_context) == ACPI_INTERRUPT_HANDLED)
		return (FILTER_HANDLED);
	return (FILTER_STRAY);
}

ACPI_STATUS
AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,
    ACPI_OSD_HANDLER ServiceRoutine, void *Context)
{
	struct acpi_softc *sc;
	struct acpi_intr *ai, *ap;

	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);

	sc = devclass_get_softc(devclass_find("acpi"), 0);
	KASSERT(sc != NULL && sc->acpi_dev != NULL,
	    ("can't find ACPI device to register interrupt"));

	if (InterruptNumber < 0 || InterruptNumber > 255 ||
	    ServiceRoutine == NULL)
		return_ACPI_STATUS (AE_BAD_PARAMETER);

	ai = malloc(sizeof(*ai), M_ACPIINTR, M_WAITOK | M_ZERO);
	mtx_lock(&acpi_intr_lock);
	SLIST_FOREACH(ap, &acpi_intr_list, ai_link) {
		if (InterruptNumber == ap->ai_number ||
		    (InterruptNumber == InterruptOverride &&
		    InterruptNumber != AcpiGbl_FADT.SciInterrupt)) {
			mtx_unlock(&acpi_intr_lock);
			return_ACPI_STATUS (AE_ALREADY_EXISTS);
		}
		if (ai->ai_rid <= ap->ai_rid)
			ai->ai_rid = ap->ai_rid + 1;
	}
	ai->ai_number = InterruptNumber;
	ai->ai_handler = ServiceRoutine;
	ai->ai_context = Context;

	/*
	 * If the MADT contained an interrupt override directive for the SCI,
	 * we use that value instead of the one from the FADT.
	 */
	if (InterruptOverride != 0 &&
	    InterruptNumber == AcpiGbl_FADT.SciInterrupt) {
		device_printf(sc->acpi_dev,
		    "Overriding SCI from IRQ %u to IRQ %u\n",
		    InterruptNumber, InterruptOverride);
		InterruptNumber = InterruptOverride;
	}

	/* Set up the interrupt resource. */
	bus_set_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid,
	    InterruptNumber, 1);
	ai->ai_irq = bus_alloc_resource_any(sc->acpi_dev, SYS_RES_IRQ,
	    &ai->ai_rid, RF_SHAREABLE | RF_ACTIVE);
	if (ai->ai_irq == NULL) {
		device_printf(sc->acpi_dev, "could not allocate interrupt\n");
		goto error;
	}
	if (bus_setup_intr(sc->acpi_dev, ai->ai_irq,
	    INTR_TYPE_MISC | INTR_MPSAFE, acpi_intr_handler, NULL, ai,
	    &ai->ai_handle) != 0) {
		device_printf(sc->acpi_dev, "could not set up interrupt\n");
		goto error;
	}
	SLIST_INSERT_HEAD(&acpi_intr_list, ai, ai_link);
	mtx_unlock(&acpi_intr_lock);
	return_ACPI_STATUS (AE_OK);

error:
	mtx_unlock(&acpi_intr_lock);
	if (ai->ai_handle != NULL)
		bus_teardown_intr(sc->acpi_dev, ai->ai_irq, ai->ai_handle);
	if (ai->ai_irq != NULL)
		bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid,
		    ai->ai_irq);
	bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid);
	free(ai, M_ACPIINTR);
	return_ACPI_STATUS (AE_ALREADY_EXISTS);
}

ACPI_STATUS
AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber,
    ACPI_OSD_HANDLER ServiceRoutine)
{
	struct acpi_softc *sc;
	struct acpi_intr *ai;

	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);

	sc = devclass_get_softc(devclass_find("acpi"), 0);
	KASSERT(sc != NULL && sc->acpi_dev != NULL,
	    ("can't find ACPI device to deregister interrupt"));

	if (InterruptNumber < 0 || InterruptNumber > 255 ||
	    ServiceRoutine == NULL)
		return_ACPI_STATUS (AE_BAD_PARAMETER);
	mtx_lock(&acpi_intr_lock);
	SLIST_FOREACH(ai, &acpi_intr_list, ai_link)
		if (InterruptNumber == ai->ai_number) {
			if (ServiceRoutine != ai->ai_handler) {
				mtx_unlock(&acpi_intr_lock);
				return_ACPI_STATUS (AE_BAD_PARAMETER);
			}
			SLIST_REMOVE(&acpi_intr_list, ai, acpi_intr, ai_link);
			break;
		}
	mtx_unlock(&acpi_intr_lock);
	if (ai == NULL)
		return_ACPI_STATUS (AE_NOT_EXIST);
	bus_teardown_intr(sc->acpi_dev, ai->ai_irq, ai->ai_handle);
	bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid, ai->ai_irq);
	bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid);
	free(ai, M_ACPIINTR);
	return_ACPI_STATUS (AE_OK);
}

ACPI_STATUS
acpi_OverrideInterruptLevel(UINT32 InterruptNumber)
{

	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);

	if (InterruptOverride != 0)
		return_ACPI_STATUS (AE_ALREADY_EXISTS);
	InterruptOverride = InterruptNumber;
	return_ACPI_STATUS (AE_OK);
}
OpenPOWER on IntegriCloud