kernel/acpica: Return curthread as thread id from AcpiOsGetThreadId().
[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.28 2004/05/06 02:18:58 njl Exp $
28  */
29
30 /*
31  * Multithreading and Scheduling Services
32  */
33
34 #include "opt_acpi.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
40 #include <sys/kthread.h>
41 #include <sys/malloc.h>
42 #include <sys/proc.h>
43 #include <sys/msgport.h>
44 #include <sys/taskqueue.h>
45 #include <machine/clock.h>
46
47 #include <sys/thread2.h>
48 #include <sys/msgport2.h>
49
50 #include "acpi.h"
51 #include "accommon.h"
52 #include <dev/acpica/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     ACPI_EXECUTE_TYPE           at_type;
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_replyonly(&acpi_afree_rport, acpi_autofree_reply);
84     acpi_task_td = kmalloc(sizeof(struct thread), M_DEVBUF, M_INTWAIT | M_ZERO);
85     lwkt_create(acpi_task_thread, NULL, NULL, acpi_task_td, TDF_NOSTART, 0,
86         "acpi_task");
87     return (0);
88 }
89
90 void
91 acpi_task_thread_schedule(void)
92 {
93     lwkt_schedule(acpi_task_td);
94 }
95
96 /*
97  * The ACPI helper thread processes OSD execution callback messages.
98  */
99 static void
100 acpi_task_thread(void *arg)
101 {
102     ACPI_OSD_EXEC_CALLBACK func;
103     struct acpi_task *at;
104
105     lwkt_gettoken(&acpi_token);
106     for (;;) {
107         at = (void *)lwkt_waitport(&curthread->td_msgport, 0);
108         func = at->at_function;
109         func(at->at_context);
110         lwkt_replymsg(&at->at_msg, 0);
111     }
112     lwkt_reltoken(&acpi_token);
113 }
114
115 /*
116  * Queue an ACPI message for execution by allocating a LWKT message structure
117  * and sending the message to the helper thread.  The reply port is setup
118  * to automatically free the message.
119  */
120 ACPI_STATUS
121 AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
122               void *Context)
123 {
124     struct acpi_task    *at;
125
126     switch (Type) {
127     case OSL_GLOBAL_LOCK_HANDLER:
128     case OSL_NOTIFY_HANDLER:
129     case OSL_GPE_HANDLER:
130     case OSL_DEBUGGER_EXEC_THREAD:
131     case OSL_EC_POLL_HANDLER:
132     case OSL_EC_BURST_HANDLER:
133         break;
134     default:
135         return_ACPI_STATUS (AE_BAD_PARAMETER);
136     }
137
138     /* Note: Interrupt Context */
139     at = kmalloc(sizeof(*at), M_ACPITASK, M_INTWAIT | M_ZERO);
140     lwkt_initmsg(&at->at_msg, &acpi_afree_rport, 0);
141     at->at_function = Function;
142     at->at_context = Context;
143     at->at_type = Type;
144     lwkt_sendmsg(&acpi_task_td->td_msgport, &at->at_msg);
145     return_ACPI_STATUS (AE_OK);
146 }
147
148 /*
149  * The message's reply port just frees the message.
150  */
151 static void
152 acpi_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
153 {
154     kfree(msg, M_ACPITASK);
155 }
156
157 UINT64
158 AcpiOsGetTimer (void)
159 {
160     struct timeval  time;
161
162     microtime(&time);
163
164     /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */
165
166     return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10));
167 }
168
169 void
170 AcpiOsSleep(UINT64 Milliseconds)
171 {
172     int         timo;
173     static int  dummy;
174
175     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
176
177     timo = Milliseconds * hz / 1000;
178
179     /* 
180      * If requested sleep time is less than our hz resolution, or if
181      * the system is in early boot before the system tick is operational,
182      * use DELAY instead for better granularity.
183      */
184     if (clocks_running == 0) {
185         while (timo > 1000000) {
186             DELAY(1000000);
187             timo -= 1000000;
188         }
189         if (timo)
190             DELAY(timo * 1000);
191     } else if (timo > 0) {
192         tsleep(&dummy, 0, "acpislp", timo);
193     } else {
194         DELAY(Milliseconds * 1000);
195     }
196     return_VOID;
197 }
198
199 void
200 AcpiOsStall(UINT32 Microseconds)
201 {
202     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
203
204     DELAY(Microseconds);
205     return_VOID;
206 }
207
208 ACPI_THREAD_ID
209 AcpiOsGetThreadId(void)
210 {
211     /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */
212
213     return ((uintptr_t)curthread);
214 }