xref: /netbsd-src/sys/dev/acpi/acpica/OsdInterrupt.c (revision 684eede062ef4c8b326c466ae5798a25bdf4e91d)
1 /*	$NetBSD: OsdInterrupt.c,v 1.9 2018/03/20 12:14:52 bouyer Exp $	*/
2 
3 /*
4  * Copyright 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * OS Services Layer
40  *
41  * 6.5: Interrupt Handling
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: OsdInterrupt.c,v 1.9 2018/03/20 12:14:52 bouyer Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/queue.h>
51 
52 #include <dev/acpi/acpica.h>
53 
54 #include <machine/acpi_machdep.h>
55 
56 #define	_COMPONENT	ACPI_OS_SERVICES
57 ACPI_MODULE_NAME("INTERRUPT")
58 
59 MALLOC_DEFINE(M_ACPI, "acpi", "Advanced Configuration and Power Interface");
60 
61 /*
62  * We're lucky -- ACPI uses the same convention for interrupt service
63  * routine return values at NetBSD does -- so we can just ask MD code
64  * to hook it up directly (ACPI interrupts are always level triggered
65  * and shareable, and return 0 for "not handled" and 1 for "handled").
66  */
67 
68 struct acpi_interrupt_handler {
69 	LIST_ENTRY(acpi_interrupt_handler) aih_list;
70 	UINT32 aih_intrnum;
71 	ACPI_OSD_HANDLER aih_func;
72 	void *aih_ih;
73 };
74 
75 static LIST_HEAD(, acpi_interrupt_handler) acpi_interrupt_list =
76     LIST_HEAD_INITIALIZER(&acpi_interrupt_list);
77 
78 kmutex_t acpi_interrupt_list_mtx;
79 
80 /*
81  * AcpiOsInstallInterruptHandler:
82  *
83  *	Install a handler for a hardware interrupt level.
84  */
85 ACPI_STATUS
AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,ACPI_OSD_HANDLER ServiceRoutine,void * Context)86 AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,
87     ACPI_OSD_HANDLER ServiceRoutine, void *Context)
88 {
89 	return AcpiOsInstallInterruptHandler_xname(InterruptNumber,
90 	    ServiceRoutine, Context, "acpi SCI");
91 }
92 
93 ACPI_STATUS
AcpiOsInstallInterruptHandler_xname(UINT32 InterruptNumber,ACPI_OSD_HANDLER ServiceRoutine,void * Context,const char * xname)94 AcpiOsInstallInterruptHandler_xname(UINT32 InterruptNumber,
95     ACPI_OSD_HANDLER ServiceRoutine, void *Context, const char *xname)
96 {
97 	struct acpi_interrupt_handler *aih;
98 	ACPI_STATUS rv;
99 
100 	if (InterruptNumber > 255)
101 		return AE_BAD_PARAMETER;
102 	if (ServiceRoutine == NULL)
103 		return AE_BAD_PARAMETER;
104 
105 	aih = malloc(sizeof(*aih), M_ACPI, M_NOWAIT);
106 	if (aih == NULL)
107 		return AE_NO_MEMORY;
108 
109 	aih->aih_intrnum = InterruptNumber;
110 	aih->aih_func = ServiceRoutine;
111 
112 	rv = acpi_md_OsInstallInterruptHandler(InterruptNumber,
113 	    ServiceRoutine, Context, &aih->aih_ih, xname);
114 	if (rv == AE_OK) {
115 		mutex_enter(&acpi_interrupt_list_mtx);
116 		LIST_INSERT_HEAD(&acpi_interrupt_list, aih, aih_list);
117 		mutex_exit(&acpi_interrupt_list_mtx);
118 	} else
119 		free(aih, M_ACPI);
120 
121 	return rv;
122 }
123 
124 /*
125  * AcpiOsRemoveInterruptHandler:
126  *
127  *	Remove an interrupt handler.
128  */
129 ACPI_STATUS
AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber,ACPI_OSD_HANDLER ServiceRoutine)130 AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber,
131     ACPI_OSD_HANDLER ServiceRoutine)
132 {
133 	struct acpi_interrupt_handler *aih;
134 
135 	if (InterruptNumber > 255)
136 		return AE_BAD_PARAMETER;
137 	if (ServiceRoutine == NULL)
138 		return AE_BAD_PARAMETER;
139 
140 	mutex_enter(&acpi_interrupt_list_mtx);
141 	LIST_FOREACH(aih, &acpi_interrupt_list, aih_list) {
142 		if (aih->aih_intrnum == InterruptNumber &&
143 		    aih->aih_func == ServiceRoutine) {
144 			LIST_REMOVE(aih, aih_list);
145 			mutex_exit(&acpi_interrupt_list_mtx);
146 			acpi_md_OsRemoveInterruptHandler(aih->aih_ih);
147 			free(aih, M_ACPI);
148 			return AE_OK;
149 		}
150 	}
151 	mutex_exit(&acpi_interrupt_list_mtx);
152 	return AE_NOT_EXIST;
153 }
154