af0b6eaa70afded977dbf920eaced5ad44870751
[dragonfly.git] / sys / dev / acpica5 / Osd / OsdSchedule.c
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/acpica/Osd/OsdSchedule.c,v 1.28 2004/05/06 02:18:58 njl Exp $
28  * $DragonFly: src/sys/dev/acpica5/Osd/OsdSchedule.c,v 1.3 2004/06/27 08:52:42 dillon Exp $
29  */
30
31 /*
32  * 6.3 : Scheduling services
33  */
34
35 #include "opt_acpi.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/interrupt.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 #include <machine/clock.h>
46
47 #include "acpi.h"
48 #include <dev/acpica5/acpivar.h>
49
50 #define _COMPONENT      ACPI_OS_SERVICES
51 ACPI_MODULE_NAME("SCHEDULE")
52
53 /*
54  * This is a little complicated due to the fact that we need to build and then
55  * free a 'struct task' for each task we enqueue.
56  */
57
58 MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
59
60 static void     AcpiOsExecuteQueue(void *arg, int pending);
61
62 struct acpi_task {
63     struct task                 at_task;
64     OSD_EXECUTION_CALLBACK      at_function;
65     void                        *at_context;
66 };
67
68 struct acpi_task_queue {
69     STAILQ_ENTRY(acpi_task_queue) at_q;
70     struct acpi_task            *at;
71 };
72
73 #if __FreeBSD_version >= 500000
74 /*
75  * Private task queue definition for ACPI
76  */
77 TASKQUEUE_DECLARE(acpi);
78 static void     *taskqueue_acpi_ih;
79
80 static void
81 taskqueue_acpi_enqueue(void *context)
82 {  
83     swi_sched(taskqueue_acpi_ih, 0);
84 }
85
86 static void
87 taskqueue_acpi_run(void *dummy)
88 {
89     taskqueue_run(taskqueue_acpi);
90 }
91
92 TASKQUEUE_DEFINE(acpi, taskqueue_acpi_enqueue, 0,
93                  swi_add(NULL, "acpitaskq", taskqueue_acpi_run, NULL,
94                      SWI_TQ, 0, &taskqueue_acpi_ih));
95
96 #ifdef ACPI_USE_THREADS
97 static STAILQ_HEAD(, acpi_task_queue) acpi_task_queue;
98 static struct mtx       acpi_task_mtx;
99
100 static void
101 acpi_task_thread(void *arg)
102 {
103     struct acpi_task_queue      *atq;
104     OSD_EXECUTION_CALLBACK      Function;
105     void                        *Context;
106
107     for (;;) {
108         mtx_lock(&acpi_task_mtx);
109         if ((atq = STAILQ_FIRST(&acpi_task_queue)) == NULL) {
110             msleep(&acpi_task_queue, &acpi_task_mtx, PCATCH, "actask", 0);
111             mtx_unlock(&acpi_task_mtx);
112             continue;
113         }
114
115         STAILQ_REMOVE_HEAD(&acpi_task_queue, at_q);
116         mtx_unlock(&acpi_task_mtx);
117
118         Function = (OSD_EXECUTION_CALLBACK)atq->at->at_function;
119         Context = atq->at->at_context;
120
121         mtx_lock(&Giant);
122         Function(Context);
123
124         free(atq->at, M_ACPITASK);
125         free(atq, M_ACPITASK);
126         mtx_unlock(&Giant);
127     }
128
129     kthread_exit(0);
130 }
131
132 int
133 acpi_task_thread_init(void)
134 {
135     int         i, err;
136     struct proc *acpi_kthread_proc;
137
138     err = 0;
139     STAILQ_INIT(&acpi_task_queue);
140     mtx_init(&acpi_task_mtx, "ACPI task", NULL, MTX_DEF);
141
142     for (i = 0; i < ACPI_MAX_THREADS; i++) {
143         err = kthread_create(acpi_task_thread, NULL, &acpi_kthread_proc,
144                              0, 0, "acpi_task%d", i);
145         if (err != 0) {
146             printf("%s: kthread_create failed(%d)\n", __func__, err);
147             break;
148         }
149     }
150     return (err);
151 }
152 #endif /* ACPI_USE_THREADS */
153 #endif /* __FreeBSD_version >= 500000 */
154
155 /* This function is called in interrupt context. */
156 ACPI_STATUS
157 AcpiOsQueueForExecution(UINT32 Priority, OSD_EXECUTION_CALLBACK Function,
158     void *Context)
159 {
160     struct acpi_task    *at;
161     int pri;
162
163     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
164
165     if (Function == NULL)
166         return_ACPI_STATUS (AE_BAD_PARAMETER);
167
168     /* Note: Interrupt Context */
169     at = malloc(sizeof(*at), M_ACPITASK, M_INTWAIT | M_ZERO);
170     at->at_function = Function;
171     at->at_context = Context;
172     switch (Priority) {
173     case OSD_PRIORITY_GPE:
174         pri = 4;
175         break;
176     case OSD_PRIORITY_HIGH:
177         pri = 3;
178         break;
179     case OSD_PRIORITY_MED:
180         pri = 2;
181         break;
182     case OSD_PRIORITY_LO:
183         pri = 1;
184         break;
185     default:
186         free(at, M_ACPITASK);
187         return_ACPI_STATUS (AE_BAD_PARAMETER);
188     }
189     TASK_INIT(&at->at_task, pri, AcpiOsExecuteQueue, at);
190
191 #if __FreeBSD_version >= 500000
192     taskqueue_enqueue(taskqueue_acpi, (struct task *)at);
193 #else
194     taskqueue_enqueue(taskqueue_swi, (struct task *)at);
195 #endif
196     return_ACPI_STATUS (AE_OK);
197 }
198
199 static void
200 AcpiOsExecuteQueue(void *arg, int pending)
201 {
202     struct acpi_task            *at;
203     struct acpi_task_queue      *atq;
204     OSD_EXECUTION_CALLBACK      Function;
205     void                        *Context;
206
207     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
208
209     at = (struct acpi_task *)arg;
210     atq = NULL;
211     Function = NULL;
212     Context = NULL;
213
214 #ifdef ACPI_USE_THREADS
215     atq = malloc(sizeof(*atq), M_ACPITASK, M_INTWAIT);
216     atq->at = at;
217
218     mtx_lock(&acpi_task_mtx);
219     STAILQ_INSERT_TAIL(&acpi_task_queue, atq, at_q);
220     mtx_unlock(&acpi_task_mtx);
221     wakeup_one(&acpi_task_queue);
222 #else
223     Function = (OSD_EXECUTION_CALLBACK)at->at_function;
224     Context = at->at_context;
225
226     Function(Context);
227     free(at, M_ACPITASK);
228 #endif
229
230     return_VOID;
231 }
232
233 void
234 AcpiOsSleep(UINT32 Seconds, UINT32 Milliseconds)
235 {
236     int         timo;
237     static int  dummy;
238
239     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
240
241     timo = (Seconds * hz) + Milliseconds * hz / 1000;
242
243     /* 
244      * If requested sleep time is less than our hz resolution, use
245      * DELAY instead for better granularity.
246      */
247     if (timo > 0)
248         tsleep(&dummy, 0, "acpislp", timo);
249     else
250         DELAY(Milliseconds * 1000);
251
252     return_VOID;
253 }
254
255 void
256 AcpiOsStall(UINT32 Microseconds)
257 {
258     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
259
260     DELAY(Microseconds);
261     return_VOID;
262 }
263
264 UINT32
265 AcpiOsGetThreadId(void)
266 {
267     struct proc *p;
268
269     /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */
270
271     p = curproc;
272 #if __FreeBSD_version < 500000
273     if (p == NULL)
274         p = &proc0;
275 #endif
276     KASSERT(p != NULL, ("%s: curproc is NULL!", __func__));
277
278     /* Returning 0 is not allowed. */
279     return (p->p_pid + 1);
280 }