Update ACPI build wrappers to use new ACPICA code.
[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.5 2005/03/12 14:33:40 y0netan1 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/msgport.h>
45 #include <sys/taskqueue.h>
46 #include <machine/clock.h>
47
48 #include <sys/thread2.h>
49 #include <sys/msgport2.h>
50
51 #include "acpi.h"
52 #include <dev/acpica5/acpivar.h>
53
54 #define _COMPONENT      ACPI_OS_SERVICES
55 ACPI_MODULE_NAME("SCHEDULE")
56
57 /*
58  * This is a little complicated due to the fact that we need to build and then
59  * free a 'struct task' for each task we enqueue.
60  */
61
62 MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
63
64 static void     acpi_task_thread(void *arg);
65 static void     acpi_autofree_reply(lwkt_port_t port, lwkt_msg_t msg);
66
67 struct acpi_task {
68     struct lwkt_msg             at_msg;
69     ACPI_OSD_EXEC_CALLBACK      at_function;
70     void                        *at_context;
71     int                         at_priority;
72 };
73
74 static struct thread *acpi_task_td;
75 struct lwkt_port acpi_afree_rport;
76
77 /*
78  * Initialize the ACPI helper thread.
79  */
80 int
81 acpi_task_thread_init(void)
82 {
83     lwkt_initport(&acpi_afree_rport, NULL);
84     acpi_afree_rport.mp_replyport = acpi_autofree_reply;
85     kthread_create(acpi_task_thread, NULL, &acpi_task_td,
86                         0, 0, "acpi_task");
87     return (0);
88 }
89
90 /*
91  * The ACPI helper thread processes OSD execution callback messages.
92  */
93 static void
94 acpi_task_thread(void *arg)
95 {
96     ACPI_OSD_EXEC_CALLBACK func;
97     struct acpi_task *at;
98
99     for (;;) {
100         at = (void *)lwkt_waitport(&curthread->td_msgport, NULL);
101         func = (ACPI_OSD_EXEC_CALLBACK)at->at_function;
102         func((void *)at->at_context);
103         lwkt_replymsg(&at->at_msg, 0);
104     }
105     kthread_exit();
106 }
107
108 /*
109  * Queue an ACPI message for execution by allocating a LWKT message structure
110  * and sending the message to the helper thread.  The reply port is setup
111  * to automatically free the message.
112  */
113 ACPI_STATUS
114 AcpiOsQueueForExecution(UINT32 Priority, ACPI_OSD_EXEC_CALLBACK Function,
115     void *Context)
116 {
117     struct acpi_task    *at;
118     int pri;
119
120     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
121
122     if (Function == NULL)
123         return_ACPI_STATUS (AE_BAD_PARAMETER);
124
125     switch (Priority) {
126     case OSD_PRIORITY_GPE:
127         pri = 4;
128         break;
129     case OSD_PRIORITY_HIGH:
130         pri = 3;
131         break;
132     case OSD_PRIORITY_MED:
133         pri = 2;
134         break;
135     case OSD_PRIORITY_LO:
136         pri = 1;
137         break;
138     default:
139         return_ACPI_STATUS (AE_BAD_PARAMETER);
140     }
141
142     /* Note: Interrupt Context */
143     at = malloc(sizeof(*at), M_ACPITASK, M_INTWAIT | M_ZERO);
144     lwkt_initmsg(&at->at_msg, &acpi_afree_rport, 0, 
145                 lwkt_cmd_op_none, lwkt_cmd_op_none);
146     at->at_function = Function;
147     at->at_context = Context;
148     at->at_priority = pri;
149     lwkt_sendmsg(&acpi_task_td->td_msgport, &at->at_msg);
150     return_ACPI_STATUS (AE_OK);
151 }
152
153 /*
154  * The message's reply port just frees the message.
155  */
156 static void
157 acpi_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
158 {
159     free(msg, M_ACPITASK);
160 }
161
162 UINT64
163 AcpiOsGetTimer (void)
164 {
165     struct timeval  time;
166
167     microtime(&time);
168
169     /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */
170
171     return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10));
172 }
173
174 void
175 AcpiOsSleep(ACPI_INTEGER Milliseconds)
176 {
177     int         timo;
178     static int  dummy;
179
180     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
181
182     timo = Milliseconds * hz / 1000;
183
184     /* 
185      * If requested sleep time is less than our hz resolution, or if
186      * the system is in early boot before the system tick is operational,
187      * use DELAY instead for better granularity.
188      */
189     if (clocks_running == 0) {
190         while (timo > 1000000) {
191             DELAY(1000000);
192             timo -= 1000000;
193         }
194         if (timo)
195             DELAY(timo * 1000);
196     } else if (timo > 1000) {
197         tsleep(&dummy, 0, "acpislp", timo);
198     } else {
199         DELAY(Milliseconds * 1000);
200     }
201     return_VOID;
202 }
203
204 void
205 AcpiOsStall(UINT32 Microseconds)
206 {
207     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
208
209     DELAY(Microseconds);
210     return_VOID;
211 }
212
213 UINT32
214 AcpiOsGetThreadId(void)
215 {
216     struct proc *p;
217
218     /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */
219
220     p = curproc;
221     if (p == NULL)
222         p = &proc0;
223     KASSERT(p != NULL, ("%s: curproc is NULL!", __func__));
224
225     /* Returning 0 is not allowed. */
226     return (p->p_pid + 1);
227 }