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