]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/acpica/Osd/OsdSchedule.c
MFV r325013,r325034: 640 number_to_scaled_string is duplicated in several commands
[FreeBSD/FreeBSD.git] / sys / dev / acpica / Osd / OsdSchedule.c
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/interrupt.h>
41 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/malloc.h>
44 #include <sys/proc.h>
45 #include <sys/taskqueue.h>
46
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49
50 #include <dev/acpica/acpivar.h>
51
52 #define _COMPONENT      ACPI_OS_SERVICES
53 ACPI_MODULE_NAME("SCHEDULE")
54
55 /*
56  * Allow the user to tune the maximum number of tasks we may enqueue.
57  */
58 static int acpi_max_tasks = ACPI_MAX_TASKS;
59 SYSCTL_INT(_debug_acpi, OID_AUTO, max_tasks, CTLFLAG_RDTUN, &acpi_max_tasks,
60     0, "Maximum acpi tasks");
61
62 /*
63  * Track and report the system's demand for task slots.
64  */
65 static int acpi_tasks_hiwater;
66 SYSCTL_INT(_debug_acpi, OID_AUTO, tasks_hiwater, CTLFLAG_RD,
67     &acpi_tasks_hiwater, 1, "Peak demand for ACPI event task slots.");
68
69 /*
70  * Allow the user to tune the number of task threads we start.  It seems
71  * some systems have problems with increased parallelism.
72  */
73 static int acpi_max_threads = ACPI_MAX_THREADS;
74 SYSCTL_INT(_debug_acpi, OID_AUTO, max_threads, CTLFLAG_RDTUN, &acpi_max_threads,
75     0, "Maximum acpi threads");
76
77 static MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
78
79 struct acpi_task_ctx {
80     struct task                 at_task;
81     ACPI_OSD_EXEC_CALLBACK      at_function;
82     void                        *at_context;
83     int                         at_flag;
84 #define ACPI_TASK_FREE          0
85 #define ACPI_TASK_USED          1
86 #define ACPI_TASK_ENQUEUED      2
87 };
88
89 struct taskqueue                *acpi_taskq;
90 static struct acpi_task_ctx     *acpi_tasks;
91 static int                      acpi_task_count;
92 static int                      acpi_taskq_started;
93
94 /*
95  * Preallocate some memory for tasks early enough.
96  * malloc(9) cannot be used with spin lock held.
97  */
98 static void
99 acpi_task_init(void *arg)
100 {
101
102     acpi_tasks = malloc(sizeof(*acpi_tasks) * acpi_max_tasks, M_ACPITASK,
103         M_WAITOK | M_ZERO);
104 }
105
106 SYSINIT(acpi_tasks, SI_SUB_DRIVERS, SI_ORDER_FIRST, acpi_task_init, NULL);
107
108 /*
109  * Initialize ACPI task queue.
110  */
111 static void
112 acpi_taskq_init(void *arg)
113 {
114     int i;
115
116     acpi_taskq = taskqueue_create_fast("acpi_task", M_NOWAIT,
117         &taskqueue_thread_enqueue, &acpi_taskq);
118     taskqueue_start_threads(&acpi_taskq, acpi_max_threads, PWAIT, "acpi_task");
119     if (acpi_task_count > 0) {
120         if (bootverbose)
121             printf("AcpiOsExecute: enqueue %d pending tasks\n",
122                 acpi_task_count);
123         for (i = 0; i < acpi_max_tasks; i++)
124             if (atomic_cmpset_int(&acpi_tasks[i].at_flag, ACPI_TASK_USED,
125                 ACPI_TASK_USED | ACPI_TASK_ENQUEUED))
126                 taskqueue_enqueue(acpi_taskq, &acpi_tasks[i].at_task);
127     }
128     acpi_taskq_started = 1;
129 }
130
131 SYSINIT(acpi_taskq, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, acpi_taskq_init, NULL);
132
133 /*
134  * Bounce through this wrapper function since ACPI-CA doesn't understand
135  * the pending argument for its callbacks.
136  */
137 static void
138 acpi_task_execute(void *context, int pending)
139 {
140     struct acpi_task_ctx *at;
141
142     at = (struct acpi_task_ctx *)context;
143     at->at_function(at->at_context);
144     atomic_clear_int(&at->at_flag, ACPI_TASK_USED | ACPI_TASK_ENQUEUED);
145     acpi_task_count--;
146 }
147
148 static ACPI_STATUS
149 acpi_task_enqueue(int priority, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
150 {
151     struct acpi_task_ctx *at;
152     int i;
153
154     for (at = NULL, i = 0; i < acpi_max_tasks; i++)
155         if (atomic_cmpset_int(&acpi_tasks[i].at_flag, ACPI_TASK_FREE,
156             ACPI_TASK_USED)) {
157             at = &acpi_tasks[i];
158             acpi_task_count++;
159             break;
160         }
161
162     if (i > acpi_tasks_hiwater)
163         atomic_cmpset_int(&acpi_tasks_hiwater, acpi_tasks_hiwater, i);
164
165     if (at == NULL) {
166         printf("AcpiOsExecute: failed to enqueue task, consider increasing "
167             "the debug.acpi.max_tasks tunable\n");
168         return (AE_NO_MEMORY);
169     }
170
171     TASK_INIT(&at->at_task, priority, acpi_task_execute, at);
172     at->at_function = Function;
173     at->at_context = Context;
174
175     /*
176      * If the task queue is ready, enqueue it now.
177      */
178     if (acpi_taskq_started) {
179         atomic_set_int(&at->at_flag, ACPI_TASK_ENQUEUED);
180         taskqueue_enqueue(acpi_taskq, &at->at_task);
181         return (AE_OK);
182     }
183     if (bootverbose)
184         printf("AcpiOsExecute: task queue not started\n");
185
186     return (AE_OK);
187 }
188
189 /*
190  * This function may be called in interrupt context, i.e. when a GPE fires.
191  * We allocate and queue a task for one of our taskqueue threads to process.
192  */
193 ACPI_STATUS
194 AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
195     void *Context)
196 {
197     ACPI_STATUS status;
198     int pri;
199
200     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
201
202     if (Function == NULL)
203         return_ACPI_STATUS(AE_BAD_PARAMETER);
204
205     switch (Type) {
206     case OSL_GPE_HANDLER:
207     case OSL_NOTIFY_HANDLER:
208         /*
209          * Run GPEs and Notifies at the same priority.  This allows
210          * Notifies that are generated by running a GPE's method (e.g., _L00)
211          * to not be pre-empted by a later GPE that arrives during the
212          * Notify handler execution.
213          */
214         pri = 10;
215         break;
216     case OSL_GLOBAL_LOCK_HANDLER:
217     case OSL_EC_POLL_HANDLER:
218     case OSL_EC_BURST_HANDLER:
219         pri = 5;
220         break;
221     case OSL_DEBUGGER_MAIN_THREAD:
222     case OSL_DEBUGGER_EXEC_THREAD:
223         pri = 0;
224         break;
225     default:
226         return_ACPI_STATUS(AE_BAD_PARAMETER);
227     }
228
229     status = acpi_task_enqueue(pri, Function, Context);
230     return_ACPI_STATUS(status);
231 }
232
233 void
234 AcpiOsWaitEventsComplete(void)
235 {
236         int i;
237
238         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
239
240         for (i = 0; i < acpi_max_tasks; i++)
241                 if ((atomic_load_acq_int(&acpi_tasks[i].at_flag) &
242                     ACPI_TASK_ENQUEUED) != 0)
243                         taskqueue_drain(acpi_taskq, &acpi_tasks[i].at_task);
244         return_VOID;
245 }
246
247 void
248 AcpiOsSleep(UINT64 Milliseconds)
249 {
250     int         timo;
251
252     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
253
254     timo = Milliseconds * hz / 1000;
255
256     /*
257      * If requested sleep time is less than our hz resolution, use
258      * DELAY instead for better granularity.
259      */
260     if (timo > 0)
261         pause("acpislp", timo);
262     else
263         DELAY(Milliseconds * 1000);
264
265     return_VOID;
266 }
267
268 /*
269  * Return the current time in 100 nanosecond units
270  */
271 UINT64
272 AcpiOsGetTimer(void)
273 {
274     struct bintime bt;
275     UINT64 t;
276
277     binuptime(&bt);
278     t = (uint64_t)bt.sec * 10000000;
279     t += ((uint64_t)10000000 * (uint32_t)(bt.frac >> 32)) >> 32;
280
281     return (t);
282 }
283
284 void
285 AcpiOsStall(UINT32 Microseconds)
286 {
287     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
288
289     DELAY(Microseconds);
290     return_VOID;
291 }
292
293 ACPI_THREAD_ID
294 AcpiOsGetThreadId(void)
295 {
296
297     /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */
298
299     /* Returning 0 is not allowed. */
300     return (curthread->td_tid);
301 }