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