xref: /freebsd/sys/dev/acpica/Osd/OsdSchedule.c (revision 190cef3d)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * Copyright (c) 2007-2012 Jung-uk Kim <jkim@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * 6.3 : Scheduling services
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_acpi.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/taskqueue.h>
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 
49 #include <dev/acpica/acpivar.h>
50 
51 #define _COMPONENT	ACPI_OS_SERVICES
52 ACPI_MODULE_NAME("SCHEDULE")
53 
54 /*
55  * Allow the user to tune the maximum number of tasks we may enqueue.
56  */
57 static int acpi_max_tasks = ACPI_MAX_TASKS;
58 SYSCTL_INT(_debug_acpi, OID_AUTO, max_tasks, CTLFLAG_RDTUN, &acpi_max_tasks,
59     0, "Maximum acpi tasks");
60 
61 /*
62  * Track and report the system's demand for task slots.
63  */
64 static int acpi_tasks_hiwater;
65 SYSCTL_INT(_debug_acpi, OID_AUTO, tasks_hiwater, CTLFLAG_RD,
66     &acpi_tasks_hiwater, 1, "Peak demand for ACPI event task slots.");
67 
68 /*
69  * Allow the user to tune the number of task threads we start.  It seems
70  * some systems have problems with increased parallelism.
71  */
72 static int acpi_max_threads = ACPI_MAX_THREADS;
73 SYSCTL_INT(_debug_acpi, OID_AUTO, max_threads, CTLFLAG_RDTUN, &acpi_max_threads,
74     0, "Maximum acpi threads");
75 
76 static MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
77 
78 struct acpi_task_ctx {
79     struct task			at_task;
80     ACPI_OSD_EXEC_CALLBACK	at_function;
81     void 			*at_context;
82     int				at_flag;
83 #define	ACPI_TASK_FREE		0
84 #define	ACPI_TASK_USED		1
85 #define	ACPI_TASK_ENQUEUED	2
86 };
87 
88 struct taskqueue		*acpi_taskq;
89 static struct acpi_task_ctx	*acpi_tasks;
90 static int			acpi_task_count;
91 static int			acpi_taskq_started;
92 
93 /*
94  * Preallocate some memory for tasks early enough.
95  * malloc(9) cannot be used with spin lock held.
96  */
97 static void
98 acpi_task_init(void *arg)
99 {
100 
101     acpi_tasks = malloc(sizeof(*acpi_tasks) * acpi_max_tasks, M_ACPITASK,
102 	M_WAITOK | M_ZERO);
103 }
104 
105 SYSINIT(acpi_tasks, SI_SUB_DRIVERS, SI_ORDER_FIRST, acpi_task_init, NULL);
106 
107 /*
108  * Initialize ACPI task queue.
109  */
110 static void
111 acpi_taskq_init(void *arg)
112 {
113     int i;
114 
115     acpi_taskq = taskqueue_create_fast("acpi_task", M_NOWAIT,
116 	&taskqueue_thread_enqueue, &acpi_taskq);
117     taskqueue_start_threads(&acpi_taskq, acpi_max_threads, PWAIT, "acpi_task");
118     if (acpi_task_count > 0) {
119 	if (bootverbose)
120 	    printf("AcpiOsExecute: enqueue %d pending tasks\n",
121 		acpi_task_count);
122 	for (i = 0; i < acpi_max_tasks; i++)
123 	    if (atomic_cmpset_int(&acpi_tasks[i].at_flag, ACPI_TASK_USED,
124 		ACPI_TASK_USED | ACPI_TASK_ENQUEUED))
125 		taskqueue_enqueue(acpi_taskq, &acpi_tasks[i].at_task);
126     }
127     acpi_taskq_started = 1;
128 }
129 
130 SYSINIT(acpi_taskq, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, acpi_taskq_init, NULL);
131 
132 /*
133  * Bounce through this wrapper function since ACPI-CA doesn't understand
134  * the pending argument for its callbacks.
135  */
136 static void
137 acpi_task_execute(void *context, int pending)
138 {
139     struct acpi_task_ctx *at;
140 
141     at = (struct acpi_task_ctx *)context;
142     at->at_function(at->at_context);
143     atomic_clear_int(&at->at_flag, ACPI_TASK_USED | ACPI_TASK_ENQUEUED);
144     acpi_task_count--;
145 }
146 
147 static ACPI_STATUS
148 acpi_task_enqueue(int priority, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
149 {
150     struct acpi_task_ctx *at;
151     int i;
152 
153     for (at = NULL, i = 0; i < acpi_max_tasks; i++)
154 	if (atomic_cmpset_int(&acpi_tasks[i].at_flag, ACPI_TASK_FREE,
155 	    ACPI_TASK_USED)) {
156 	    at = &acpi_tasks[i];
157 	    acpi_task_count++;
158 	    break;
159 	}
160 
161     if (i > acpi_tasks_hiwater)
162 	atomic_cmpset_int(&acpi_tasks_hiwater, acpi_tasks_hiwater, i);
163 
164     if (at == NULL) {
165 	printf("AcpiOsExecute: failed to enqueue task, consider increasing "
166 	    "the debug.acpi.max_tasks tunable\n");
167 	return (AE_NO_MEMORY);
168     }
169 
170     TASK_INIT(&at->at_task, priority, acpi_task_execute, at);
171     at->at_function = Function;
172     at->at_context = Context;
173 
174     /*
175      * If the task queue is ready, enqueue it now.
176      */
177     if (acpi_taskq_started) {
178 	atomic_set_int(&at->at_flag, ACPI_TASK_ENQUEUED);
179 	taskqueue_enqueue(acpi_taskq, &at->at_task);
180 	return (AE_OK);
181     }
182     if (bootverbose)
183 	printf("AcpiOsExecute: task queue not started\n");
184 
185     return (AE_OK);
186 }
187 
188 /*
189  * This function may be called in interrupt context, i.e. when a GPE fires.
190  * We allocate and queue a task for one of our taskqueue threads to process.
191  */
192 ACPI_STATUS
193 AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
194     void *Context)
195 {
196     ACPI_STATUS status;
197     int pri;
198 
199     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
200 
201     if (Function == NULL)
202 	return_ACPI_STATUS(AE_BAD_PARAMETER);
203 
204     switch (Type) {
205     case OSL_GPE_HANDLER:
206     case OSL_NOTIFY_HANDLER:
207 	/*
208 	 * Run GPEs and Notifies at the same priority.  This allows
209 	 * Notifies that are generated by running a GPE's method (e.g., _L00)
210 	 * to not be pre-empted by a later GPE that arrives during the
211 	 * Notify handler execution.
212 	 */
213 	pri = 10;
214 	break;
215     case OSL_GLOBAL_LOCK_HANDLER:
216     case OSL_EC_POLL_HANDLER:
217     case OSL_EC_BURST_HANDLER:
218 	pri = 5;
219 	break;
220     case OSL_DEBUGGER_MAIN_THREAD:
221     case OSL_DEBUGGER_EXEC_THREAD:
222 	pri = 0;
223 	break;
224     default:
225 	return_ACPI_STATUS(AE_BAD_PARAMETER);
226     }
227 
228     status = acpi_task_enqueue(pri, Function, Context);
229     return_ACPI_STATUS(status);
230 }
231 
232 void
233 AcpiOsWaitEventsComplete(void)
234 {
235 	int i;
236 
237 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
238 
239 	for (i = 0; i < acpi_max_tasks; i++)
240 		if ((atomic_load_acq_int(&acpi_tasks[i].at_flag) &
241 		    ACPI_TASK_ENQUEUED) != 0)
242 			taskqueue_drain(acpi_taskq, &acpi_tasks[i].at_task);
243 	return_VOID;
244 }
245 
246 void
247 AcpiOsSleep(UINT64 Milliseconds)
248 {
249     int		timo;
250 
251     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
252 
253     timo = Milliseconds * hz / 1000;
254 
255     /*
256      * If requested sleep time is less than our hz resolution, use
257      * DELAY instead for better granularity.
258      */
259     if (timo > 0)
260 	pause("acpislp", timo);
261     else
262 	DELAY(Milliseconds * 1000);
263 
264     return_VOID;
265 }
266 
267 /*
268  * Return the current time in 100 nanosecond units
269  */
270 UINT64
271 AcpiOsGetTimer(void)
272 {
273     struct bintime bt;
274     UINT64 t;
275 
276     binuptime(&bt);
277     t = (uint64_t)bt.sec * 10000000;
278     t += ((uint64_t)10000000 * (uint32_t)(bt.frac >> 32)) >> 32;
279 
280     return (t);
281 }
282 
283 void
284 AcpiOsStall(UINT32 Microseconds)
285 {
286     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
287 
288     DELAY(Microseconds);
289     return_VOID;
290 }
291 
292 ACPI_THREAD_ID
293 AcpiOsGetThreadId(void)
294 {
295 
296     /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */
297 
298     /* Returning 0 is not allowed. */
299     return (curthread->td_tid);
300 }
301