xref: /netbsd-src/sys/dev/acpi/acpica/OsdSchedule.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: OsdSchedule.c,v 1.19 2017/11/12 02:59:55 christos 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.19 2017/11/12 02:59:55 christos 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 	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  * AcpiOsGetThreadId:
84  *
85  *	Obtain the ID of the currently executing thread.
86  */
87 ACPI_THREAD_ID
88 AcpiOsGetThreadId(void)
89 {
90 	return (ACPI_THREAD_ID)(uintptr_t)curlwp;
91 }
92 
93 /*
94  * AcpiOsQueueForExecution:
95  *
96  *	Schedule a procedure for deferred execution.
97  */
98 ACPI_STATUS
99 AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
100     void *Context)
101 {
102 	int pri;
103 
104 	switch (Type) {
105 	case OSL_GPE_HANDLER:
106 		pri = 10;
107 		break;
108 	case OSL_GLOBAL_LOCK_HANDLER:
109 	case OSL_EC_POLL_HANDLER:
110 	case OSL_EC_BURST_HANDLER:
111 		pri = 5;
112 		break;
113 	case OSL_NOTIFY_HANDLER:
114 		pri = 3;
115 		break;
116 	default:
117 		return AE_BAD_PARAMETER;
118 	}
119 
120 	switch (sysmon_task_queue_sched(pri, Function, Context)) {
121 	case 0:
122 		return AE_OK;
123 
124 	case ENOMEM:
125 		return AE_NO_MEMORY;
126 
127 	default:
128 		return AE_BAD_PARAMETER;
129 	}
130 }
131 
132 /*
133  * AcpiOsSleep:
134  *
135  *	Suspend the running task (coarse granularity).
136  */
137 void
138 AcpiOsSleep(ACPI_INTEGER Milliseconds)
139 {
140 
141 	if (cold || doing_shutdown || acpi_suspended)
142 		DELAY(Milliseconds * 1000);
143 	else {
144 		mutex_enter(&acpi_osd_sleep_mtx);
145 		cv_timedwait_sig(&acpi_osd_sleep_cv, &acpi_osd_sleep_mtx,
146 		    MAX(mstohz(Milliseconds), 1));
147 		mutex_exit(&acpi_osd_sleep_mtx);
148 	}
149 }
150 
151 /*
152  * AcpiOsStall:
153  *
154  *	Suspend the running task (fine granularity).
155  */
156 void
157 AcpiOsStall(UINT32 Microseconds)
158 {
159 
160 	delay(Microseconds);
161 }
162 
163 /*
164  * AcpiOsGetTimer:
165  *
166  *	Get the current system time in 100 nanosecond units
167  */
168 UINT64
169 AcpiOsGetTimer(void)
170 {
171 	static UINT64 xt;
172 	struct timeval tv;
173 	UINT64 t;
174 
175 	/* XXX During early boot there is no (decent) timer available yet. */
176 	if (cold) {
177 		return xt += 100;
178 	}
179 
180 	microtime(&tv);
181 	t = (UINT64)10 * tv.tv_usec;
182 	t += (UINT64)10000000 * tv.tv_sec;
183 
184 	return t;
185 }
186 
187 /*
188  *
189  * AcpiOsWaitEventsComplete:
190  *
191  * 	Wait for all asynchronous events to complete. This implementation
192  *	does nothing.
193  */
194 void
195 AcpiOsWaitEventsComplete(void)
196 {
197 	return;
198 }
199