xref: /dragonfly/sys/dev/acpica/Osd/OsdInterrupt.c (revision 16dd80e4)
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  *
27  * $FreeBSD: src/sys/dev/acpica/Osd/OsdInterrupt.c,v 1.17 2004/04/14 03:41:06 njl Exp $
28  */
29 
30 /*
31  * Interrupt Handling
32  */
33 
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/bus.h>
37 #include <sys/rman.h>
38 #include <sys/machintr.h>
39 
40 #include "acpi.h"
41 #include "accommon.h"
42 #include <dev/acpica/acpivar.h>
43 #include <dev/acpica/acpi_sci_var.h>
44 
45 #define _COMPONENT	ACPI_OS_SERVICES
46 ACPI_MODULE_NAME("INTERRUPT")
47 
48 static void		InterruptWrapper(void *arg);
49 
50 static ACPI_OSD_HANDLER	InterruptHandler;
51 
52 ACPI_STATUS
53 AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,
54     ACPI_OSD_HANDLER ServiceRoutine, void *Context)
55 {
56     struct acpi_softc	*sc;
57     u_int flags;
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 
66     /*
67      * Configure SCI mode
68      */
69     acpi_sci_config();
70 
71     /*
72      * This could happen:
73      * - SCI is disabled by user
74      * - No suitable interrupt mode for SCI
75      */
76     if (!acpi_sci_enabled())
77 	return_ACPI_STATUS (AE_OK);
78 
79     if (InterruptNumber < 0 || InterruptNumber > 255)
80 	return_ACPI_STATUS (AE_BAD_PARAMETER);
81     if (ServiceRoutine == NULL)
82 	return_ACPI_STATUS (AE_BAD_PARAMETER);
83     if (InterruptHandler != NULL) {
84 	device_printf(sc->acpi_dev, "interrupt handler already installed\n");
85 	return_ACPI_STATUS (AE_ALREADY_EXISTS);
86     }
87     InterruptHandler = ServiceRoutine;
88 
89     flags = RF_ACTIVE;
90     if (acpi_sci_pci_shareable())
91 	flags |= RF_SHAREABLE;
92 
93     /* Set up the interrupt resource. */
94     sc->acpi_irq_rid = 0;
95     bus_set_resource(sc->acpi_dev, SYS_RES_IRQ, 0, InterruptNumber, 1,
96 	machintr_legacy_intr_cpuid(InterruptNumber));
97     sc->acpi_irq = bus_alloc_resource_any(sc->acpi_dev, SYS_RES_IRQ,
98 	&sc->acpi_irq_rid, flags);
99     if (sc->acpi_irq == NULL) {
100 	device_printf(sc->acpi_dev, "could not allocate interrupt\n");
101 	goto error;
102     }
103     if (bus_setup_intr(sc->acpi_dev, sc->acpi_irq, INTR_MPSAFE,
104 		    InterruptWrapper, Context, &sc->acpi_irq_handle, NULL)) {
105 	device_printf(sc->acpi_dev, "could not set up interrupt\n");
106 	goto error;
107     }
108 
109     return_ACPI_STATUS (AE_OK);
110 
111 error:
112     if (sc->acpi_irq_handle)
113 	bus_teardown_intr(sc->acpi_dev, sc->acpi_irq, sc->acpi_irq_handle);
114     sc->acpi_irq_handle = NULL;
115     if (sc->acpi_irq)
116 	bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, 0, sc->acpi_irq);
117     sc->acpi_irq = NULL;
118     bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, 0);
119     InterruptHandler = NULL;
120 
121     return_ACPI_STATUS (AE_ALREADY_EXISTS);
122 }
123 
124 ACPI_STATUS
125 AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber, ACPI_OSD_HANDLER ServiceRoutine)
126 {
127     struct acpi_softc	*sc;
128 
129     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
130 
131     if (!acpi_sci_enabled())
132 	return_ACPI_STATUS (AE_OK);
133 
134     if (InterruptNumber > 255 || ServiceRoutine == NULL)
135 	return_ACPI_STATUS (AE_BAD_PARAMETER);
136 
137     if ((sc = devclass_get_softc(devclass_find("acpi"), 0)) == NULL)
138 	panic("can't find ACPI device to deregister interrupt");
139 
140     if (sc->acpi_irq == NULL)
141 	return_ACPI_STATUS (AE_NOT_EXIST);
142 
143     bus_teardown_intr(sc->acpi_dev, sc->acpi_irq, sc->acpi_irq_handle);
144     bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, 0, sc->acpi_irq);
145     bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, 0);
146 
147     sc->acpi_irq = NULL;
148     InterruptHandler = NULL;
149 
150     return_ACPI_STATUS (AE_OK);
151 }
152 
153 static void
154 InterruptWrapper(void *arg)
155 {
156     lwkt_gettoken(&acpi_token);
157     InterruptHandler(arg);
158     lwkt_reltoken(&acpi_token);
159 }
160