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