Add __DragonFly__
[dragonfly.git] / sys / dev / acpica / 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.23.6.1 2003/08/22 20:49:21 jhb Exp $
28  *      $DragonFly: src/sys/dev/acpica/Osd/Attic/OsdSchedule.c,v 1.2 2004/02/13 00:25:17 joerg Exp $ 
29  */
30
31 /*
32  * 6.3 : Scheduling services
33  */
34
35 #include "acpi.h"
36
37 #include "opt_acpi.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/interrupt.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 #include <sys/taskqueue.h>
47 #include <machine/clock.h>
48
49 #include <sys/bus.h>
50
51 #include <dev/acpica/acpivar.h>
52
53 #define _COMPONENT      ACPI_OS_SERVICES
54 ACPI_MODULE_NAME("SCHEDULE")
55
56 /*
57  * This is a little complicated due to the fact that we need to build and then
58  * free a 'struct task' for each task we enqueue.
59  */
60
61 MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
62
63 static void     AcpiOsExecuteQueue(void *arg, int pending);
64
65 struct acpi_task {
66     struct task                 at_task;
67     OSD_EXECUTION_CALLBACK      at_function;
68     void                        *at_context;
69 };
70
71 struct acpi_task_queue {
72     STAILQ_ENTRY(acpi_task_queue) at_q;
73     struct acpi_task            *at;
74 };
75
76 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
77 /*
78  * Private task queue definition for ACPI
79  */
80 TASKQUEUE_DECLARE(acpi);
81 static void     *taskqueue_acpi_ih;
82
83 static void
84 taskqueue_acpi_enqueue(void *context)
85 {  
86     swi_sched(taskqueue_acpi_ih, 0);
87 }
88
89 static void
90 taskqueue_acpi_run(void *dummy)
91 {
92     taskqueue_run(taskqueue_acpi);
93 }
94
95 TASKQUEUE_DEFINE(acpi, taskqueue_acpi_enqueue, 0,
96                  swi_add(NULL, "acpitaskq", taskqueue_acpi_run, NULL,
97                      SWI_TQ, 0, &taskqueue_acpi_ih));
98
99 #ifdef ACPI_USE_THREADS
100 STAILQ_HEAD(, acpi_task_queue) acpi_task_queue;
101 static struct mtx       acpi_task_mtx;
102
103 static void
104 acpi_task_thread(void *arg)
105 {
106     struct acpi_task_queue      *atq;
107     OSD_EXECUTION_CALLBACK      Function;
108     void                        *Context;
109
110     for (;;) {
111         mtx_lock(&acpi_task_mtx);
112         if ((atq = STAILQ_FIRST(&acpi_task_queue)) == NULL) {
113             msleep(&acpi_task_queue, &acpi_task_mtx, PCATCH, "actask", 0);
114             mtx_unlock(&acpi_task_mtx);
115             continue;
116         }
117
118         STAILQ_REMOVE_HEAD(&acpi_task_queue, at_q);
119         mtx_unlock(&acpi_task_mtx);
120
121         Function = (OSD_EXECUTION_CALLBACK)atq->at->at_function;
122         Context = atq->at->at_context;
123
124         mtx_lock(&Giant);
125         Function(Context);
126
127         free(atq->at, M_ACPITASK);
128         free(atq, M_ACPITASK);
129         mtx_unlock(&Giant);
130     }
131
132     kthread_exit(0);
133 }
134
135 int
136 acpi_task_thread_init(void)
137 {
138     int         i, err;
139     struct proc *acpi_kthread_proc;
140
141     err = 0;
142     STAILQ_INIT(&acpi_task_queue);
143     mtx_init(&acpi_task_mtx, "ACPI task", NULL, MTX_DEF);
144
145     for (i = 0; i < ACPI_MAX_THREADS; i++) {
146         err = kthread_create(acpi_task_thread, NULL, &acpi_kthread_proc,
147                              0, 0, "acpi_task%d", i);
148         if (err != 0) {
149             printf("%s: kthread_create failed(%d)\n", __func__, err);
150             break;
151         }
152     }
153     return (err);
154 }
155 #endif
156 #endif
157
158 ACPI_STATUS
159 AcpiOsQueueForExecution(UINT32 Priority, OSD_EXECUTION_CALLBACK Function, void *Context)
160 {
161     struct acpi_task    *at;
162     int pri;
163
164     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
165
166     if (Function == NULL)
167         return_ACPI_STATUS(AE_BAD_PARAMETER);
168
169     at = malloc(sizeof(*at), M_ACPITASK, M_NOWAIT);     /* Interrupt Context */
170     if (at == NULL)
171         return_ACPI_STATUS(AE_NO_MEMORY);
172     bzero(at, sizeof(*at));
173
174     at->at_function = Function;
175     at->at_context = Context;
176     switch (Priority) {
177     case OSD_PRIORITY_GPE:
178         pri = 4;
179         break;
180     case OSD_PRIORITY_HIGH:
181         pri = 3;
182         break;
183     case OSD_PRIORITY_MED:
184         pri = 2;
185         break;
186     case OSD_PRIORITY_LO:
187         pri = 1;
188         break;
189     default:
190         free(at, M_ACPITASK);
191         return_ACPI_STATUS(AE_BAD_PARAMETER);
192     }
193     TASK_INIT(&at->at_task, pri, AcpiOsExecuteQueue, at);
194
195 #if defined(__DragonFly__) || __FreeBSD_version < 500000
196     taskqueue_enqueue(taskqueue_swi, (struct task *)at);
197 #else
198     taskqueue_enqueue(taskqueue_acpi, (struct task *)at);
199 #endif
200     return_ACPI_STATUS(AE_OK);
201 }
202
203 static void
204 AcpiOsExecuteQueue(void *arg, int pending)
205 {
206     struct acpi_task            *at;
207     struct acpi_task_queue      *atq;
208     OSD_EXECUTION_CALLBACK      Function;
209     void                        *Context;
210
211     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
212
213     at = (struct acpi_task *)arg;
214     atq = NULL;
215     Function = NULL;
216     Context = NULL;
217
218 #ifdef ACPI_USE_THREADS
219     atq = malloc(sizeof(*atq), M_ACPITASK, M_NOWAIT);
220     if (atq == NULL) {
221         printf("%s: no memory\n", __func__);
222         return;
223     }
224
225     atq->at = at;
226
227     mtx_lock(&acpi_task_mtx);
228     STAILQ_INSERT_TAIL(&acpi_task_queue, atq, at_q);
229     mtx_unlock(&acpi_task_mtx);
230     wakeup_one(&acpi_task_queue);
231 #else
232     Function = (OSD_EXECUTION_CALLBACK)at->at_function;
233     Context = at->at_context;
234
235     Function(Context);
236     free(at, M_ACPITASK);
237 #endif
238
239     return_VOID;
240 }
241
242 /*
243  * We don't have any sleep granularity better than hz, so
244  * make do with that.
245  */
246 void
247 AcpiOsSleep (UINT32 Seconds, UINT32 Milliseconds)
248 {
249     int         timo;
250     static int  dummy;
251
252     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
253
254     timo = (Seconds * hz) + Milliseconds * hz / 1000;
255     if (timo == 0)
256         timo = 1;
257     tsleep(&dummy, 0, "acpislp", timo);
258     return_VOID;
259 }
260
261 void
262 AcpiOsStall (UINT32 Microseconds)
263 {
264     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
265
266     DELAY(Microseconds);
267     return_VOID;
268 }
269
270 UINT32
271 AcpiOsGetThreadId (void)
272 {
273     struct proc *p;
274     /* XXX do not add FUNCTION_TRACE here, results in recursive call */
275
276     p = curproc;
277 #if defined(__DragonFly__) || __FreeBSD_version < 500000
278     if (p == NULL)
279         p = &proc0;
280 #endif
281     KASSERT(p != NULL, ("%s: curproc is NULL!", __func__));
282     return(p->p_pid + 1);       /* can't return 0 */
283 }