xref: /netbsd-src/sys/dev/acpi/acpica/OsdSchedule.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: OsdSchedule.c,v 1.10 2009/03/31 17:17:47 drochner 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.3: Scheduling services
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: OsdSchedule.c,v 1.10 2009/03/31 17:17:47 drochner Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/malloc.h>
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/condvar.h>
53 #include <sys/mutex.h>
54 
55 #include <dev/acpi/acpica.h>
56 
57 #include <dev/acpi/acpi_osd.h>
58 
59 #include <dev/sysmon/sysmon_taskq.h>
60 
61 extern int acpi_suspended;
62 
63 #define	_COMPONENT	ACPI_OS_SERVICES
64 ACPI_MODULE_NAME("SCHEDULE")
65 
66 static kcondvar_t	acpi_osd_sleep_cv;
67 static kmutex_t		acpi_osd_sleep_mtx;
68 
69 /*
70  * acpi_osd_sched_init:
71  *
72  *	Initialize the APCICA Osd scheduler.  Called from AcpiOsInitialize().
73  */
74 void
75 acpi_osd_sched_init(void)
76 {
77 
78 	ACPI_FUNCTION_TRACE(__func__);
79 
80 	sysmon_task_queue_init();
81 	mutex_init(&acpi_osd_sleep_mtx, MUTEX_DEFAULT, IPL_NONE);
82 	cv_init(&acpi_osd_sleep_cv, "acpislp");
83 
84 	return_VOID;
85 }
86 
87 /*
88  * acpi_osd_sched_fini:
89  *
90  *	Clean up the ACPICA Osd scheduler.  Called from AcpiOsdTerminate().
91  */
92 void
93 acpi_osd_sched_fini(void)
94 {
95 
96 	ACPI_FUNCTION_TRACE(__func__);
97 
98 	sysmon_task_queue_fini();
99 
100 	return_VOID;
101 }
102 
103 /*
104  * AcpiOsGetThreadId:
105  *
106  *	Obtain the ID of the currently executing thread.
107  */
108 ACPI_THREAD_ID
109 AcpiOsGetThreadId(void)
110 {
111 	return (ACPI_THREAD_ID)curlwp;
112 }
113 
114 /*
115  * AcpiOsQueueForExecution:
116  *
117  *	Schedule a procedure for deferred execution.
118  */
119 ACPI_STATUS
120 AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
121     void *Context)
122 {
123 	int pri;
124 
125 	ACPI_FUNCTION_TRACE(__func__);
126 
127 	switch (Type) {
128 	case OSL_GPE_HANDLER:
129 		pri = 10;
130 		break;
131 	case OSL_GLOBAL_LOCK_HANDLER:
132 	case OSL_EC_POLL_HANDLER:
133 	case OSL_EC_BURST_HANDLER:
134 		pri = 5;
135 		break;
136 	case OSL_NOTIFY_HANDLER:
137 		pri = 3;
138 		break;
139 	case OSL_DEBUGGER_THREAD:
140 		pri = 0;
141 		break;
142 	default:
143 		return_ACPI_STATUS(AE_BAD_PARAMETER);
144 	}
145 
146 	switch (sysmon_task_queue_sched(pri, Function, Context)) {
147 	case 0:
148 		return_ACPI_STATUS(AE_OK);
149 
150 	case ENOMEM:
151 		return_ACPI_STATUS(AE_NO_MEMORY);
152 
153 	default:
154 		return_ACPI_STATUS(AE_BAD_PARAMETER);
155 	}
156 }
157 
158 /*
159  * AcpiOsSleep:
160  *
161  *	Suspend the running task (coarse granularity).
162  */
163 void
164 AcpiOsSleep(ACPI_INTEGER Milliseconds)
165 {
166 	ACPI_FUNCTION_TRACE(__func__);
167 
168 	if (cold || doing_shutdown || acpi_suspended)
169 		DELAY(Milliseconds * 1000);
170 	else {
171 		mutex_enter(&acpi_osd_sleep_mtx);
172 		cv_timedwait_sig(&acpi_osd_sleep_cv, &acpi_osd_sleep_mtx,
173 		    MAX(mstohz(Milliseconds), 1));
174 		mutex_exit(&acpi_osd_sleep_mtx);
175 	}
176 }
177 
178 /*
179  * AcpiOsStall:
180  *
181  *	Suspend the running task (fine granularity).
182  */
183 void
184 AcpiOsStall(UINT32 Microseconds)
185 {
186 
187 	ACPI_FUNCTION_TRACE(__func__);
188 
189 	/*
190 	 * sleep(9) isn't safe because AcpiOsStall may be called
191 	 * with interrupt-disabled. (eg. by AcpiEnterSleepState)
192 	 * we should watch out for long stall requests.
193 	 */
194 #ifdef ACPI_DEBUG
195 	if (Microseconds > 1000)
196 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "long stall: %uus\n",
197 		    Microseconds));
198 #endif
199 
200 	delay(Microseconds);
201 
202 	return_VOID;
203 
204 }
205 
206 /*
207  * AcpiOsGetTimer:
208  *
209  *	Get the current system time in 100 nanosecond units
210  */
211 UINT64
212 AcpiOsGetTimer(void)
213 {
214 	struct timeval tv;
215 	UINT64 t;
216 
217 	/* XXX During early boot there is no (decent) timer available yet. */
218 	if (cold)
219 		panic("acpi: timer op not yet supported during boot");
220 
221 	microtime(&tv);
222 	t = (UINT64)10 * tv.tv_usec;
223 	t += (UINT64)10000000 * tv.tv_sec;
224 
225 	return (t);
226 }
227