Bring in YONETANI Tomokazu's acpi-update-2.patch (27-May-2004), a major
[dragonfly.git] / sys / dev / acpica5 / Osd / OsdInterrupt.c
CommitLineData
5ed44076
MD
1/*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
f9d8cd12
MD
27 * $FreeBSD: src/sys/dev/acpica/Osd/OsdInterrupt.c,v 1.17 2004/04/14 03:41:06 njl Exp $
28 * $DragonFly: src/sys/dev/acpica5/Osd/OsdInterrupt.c,v 1.2 2004/06/27 08:52:42 dillon Exp $
5ed44076
MD
29 */
30
31/*
32 * 6.5 : Interrupt handling
33 */
34
f9d8cd12
MD
35#include <sys/param.h>
36#include <sys/kernel.h>
5ed44076 37#include <sys/bus.h>
5ed44076 38#include <machine/bus.h>
f9d8cd12 39#include <machine/resource.h>
5ed44076
MD
40#include <sys/rman.h>
41
f9d8cd12 42#include "acpi.h"
5ed44076
MD
43#include <dev/acpica5/acpivar.h>
44
45#define _COMPONENT ACPI_OS_SERVICES
46ACPI_MODULE_NAME("INTERRUPT")
47
48static void InterruptWrapper(void *arg);
5ed44076 49
f9d8cd12
MD
50static OSD_HANDLER InterruptHandler;
51static UINT32 InterruptOverride = 0;
5ed44076 52
5ed44076 53ACPI_STATUS
f9d8cd12
MD
54AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,
55 OSD_HANDLER ServiceRoutine, void *Context)
5ed44076
MD
56{
57 struct acpi_softc *sc;
58
59 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
60
61 if ((sc = devclass_get_softc(devclass_find("acpi"), 0)) == NULL)
62 panic("can't find ACPI device to register interrupt");
63 if (sc->acpi_dev == NULL)
64 panic("acpi softc has invalid device");
65
f9d8cd12
MD
66 if (InterruptNumber < 0 || InterruptNumber > 255)
67 return_ACPI_STATUS (AE_BAD_PARAMETER);
5ed44076 68 if (ServiceRoutine == NULL)
f9d8cd12
MD
69 return_ACPI_STATUS (AE_BAD_PARAMETER);
70 if (InterruptHandler != NULL) {
71 device_printf(sc->acpi_dev, "interrupt handler already installed\n");
72 return_ACPI_STATUS (AE_ALREADY_EXISTS);
5ed44076
MD
73 }
74 InterruptHandler = ServiceRoutine;
75
76 /*
f9d8cd12
MD
77 * If the MADT contained an interrupt override directive for the SCI,
78 * we use that value instead of the one from the FADT.
5ed44076 79 */
5ed44076
MD
80 if (InterruptOverride != 0) {
81 device_printf(sc->acpi_dev,
82 "Overriding SCI Interrupt from IRQ %u to IRQ %u\n",
83 InterruptNumber, InterruptOverride);
84 InterruptNumber = InterruptOverride;
85 }
f9d8cd12
MD
86
87 /* Set up the interrupt resource. */
88 sc->acpi_irq_rid = 0;
5ed44076 89 bus_set_resource(sc->acpi_dev, SYS_RES_IRQ, 0, InterruptNumber, 1);
f9d8cd12
MD
90 sc->acpi_irq = bus_alloc_resource_any(sc->acpi_dev, SYS_RES_IRQ,
91 &sc->acpi_irq_rid, RF_SHAREABLE | RF_ACTIVE);
92 if (sc->acpi_irq == NULL) {
93 device_printf(sc->acpi_dev, "could not allocate interrupt\n");
94 goto error;
5ed44076 95 }
f9d8cd12
MD
96 if (bus_setup_intr(sc->acpi_dev, sc->acpi_irq, INTR_TYPE_MISC,
97 InterruptWrapper, Context, &sc->acpi_irq_handle)) {
98 device_printf(sc->acpi_dev, "could not set up interrupt\n");
99 goto error;
5ed44076 100 }
f9d8cd12
MD
101
102 return_ACPI_STATUS (AE_OK);
103
104error:
105 if (sc->acpi_irq_handle)
106 bus_teardown_intr(sc->acpi_dev, sc->acpi_irq, sc->acpi_irq_handle);
107 sc->acpi_irq_handle = NULL;
108 if (sc->acpi_irq)
109 bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, 0, sc->acpi_irq);
110 sc->acpi_irq = NULL;
111 bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, 0);
112 InterruptHandler = NULL;
113
114 return_ACPI_STATUS (AE_ALREADY_EXISTS);
5ed44076
MD
115}
116
117ACPI_STATUS
f9d8cd12 118AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber, OSD_HANDLER ServiceRoutine)
5ed44076
MD
119{
120 struct acpi_softc *sc;
121
122 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
123
f9d8cd12
MD
124 if (InterruptNumber < 0 || InterruptNumber > 255)
125 return_ACPI_STATUS (AE_BAD_PARAMETER);
5ed44076 126 if (ServiceRoutine == NULL)
f9d8cd12 127 return_ACPI_STATUS (AE_BAD_PARAMETER);
5ed44076
MD
128
129 if ((sc = devclass_get_softc(devclass_find("acpi"), 0)) == NULL)
130 panic("can't find ACPI device to deregister interrupt");
131
132 if (sc->acpi_irq == NULL)
f9d8cd12 133 return_ACPI_STATUS (AE_NOT_EXIST);
5ed44076
MD
134
135 bus_teardown_intr(sc->acpi_dev, sc->acpi_irq, sc->acpi_irq_handle);
136 bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, 0, sc->acpi_irq);
137 bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, 0);
138
139 sc->acpi_irq = NULL;
f9d8cd12 140 InterruptHandler = NULL;
5ed44076 141
f9d8cd12 142 return_ACPI_STATUS (AE_OK);
5ed44076
MD
143}
144
145ACPI_STATUS
146acpi_OverrideInterruptLevel(UINT32 InterruptNumber)
147{
148
149 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
150
151 if (InterruptOverride != 0)
f9d8cd12 152 return_ACPI_STATUS (AE_ALREADY_EXISTS);
5ed44076 153 InterruptOverride = InterruptNumber;
f9d8cd12 154 return_ACPI_STATUS (AE_OK);
5ed44076
MD
155}
156
5ed44076
MD
157static void
158InterruptWrapper(void *arg)
159{
160 ACPI_LOCK_DECL;
161
162 ACPI_LOCK;
163 InterruptHandler(arg);
164 ACPI_UNLOCK;
165}