AMD64 - Enable module building, sync i386 headers etc as needed.
[dragonfly.git] / sys / dev / acpica5 / Osd / OsdSchedule.c
CommitLineData
5ed44076
MD
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 *
f9d8cd12 27 * $FreeBSD: src/sys/dev/acpica/Osd/OsdSchedule.c,v 1.28 2004/05/06 02:18:58 njl Exp $
3641b7ca 28 * $DragonFly: src/sys/dev/acpica5/Osd/OsdSchedule.c,v 1.10 2008/06/05 18:06:31 swildner Exp $
5ed44076
MD
29 */
30
31/*
32 * 6.3 : Scheduling services
33 */
34
5ed44076
MD
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>
87b510d5 44#include <sys/msgport.h>
5ed44076
MD
45#include <sys/taskqueue.h>
46#include <machine/clock.h>
47
87b510d5
MD
48#include <sys/thread2.h>
49#include <sys/msgport2.h>
50
f9d8cd12 51#include "acpi.h"
5ed44076
MD
52#include <dev/acpica5/acpivar.h>
53
54#define _COMPONENT ACPI_OS_SERVICES
55ACPI_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
62MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
63
87b510d5
MD
64static void acpi_task_thread(void *arg);
65static void acpi_autofree_reply(lwkt_port_t port, lwkt_msg_t msg);
5ed44076
MD
66
67struct acpi_task {
87b510d5 68 struct lwkt_msg at_msg;
b18b56bd 69 ACPI_OSD_EXEC_CALLBACK at_function;
5ed44076 70 void *at_context;
e1eeedd0 71 ACPI_EXECUTE_TYPE at_type;
5ed44076
MD
72};
73
87b510d5
MD
74static struct thread *acpi_task_td;
75struct lwkt_port acpi_afree_rport;
5ed44076 76
5ed44076 77/*
87b510d5 78 * Initialize the ACPI helper thread.
5ed44076 79 */
87b510d5
MD
80int
81acpi_task_thread_init(void)
5ed44076 82{
fb0f29c4 83 lwkt_initport_replyonly(&acpi_afree_rport, acpi_autofree_reply);
87b510d5
MD
84 kthread_create(acpi_task_thread, NULL, &acpi_task_td,
85 0, 0, "acpi_task");
86 return (0);
5ed44076
MD
87}
88
87b510d5
MD
89/*
90 * The ACPI helper thread processes OSD execution callback messages.
91 */
5ed44076
MD
92static void
93acpi_task_thread(void *arg)
94{
b18b56bd 95 ACPI_OSD_EXEC_CALLBACK func;
87b510d5 96 struct acpi_task *at;
5ed44076
MD
97
98 for (;;) {
3641b7ca 99 at = (void *)lwkt_waitport(&curthread->td_msgport, 0);
b18b56bd 100 func = (ACPI_OSD_EXEC_CALLBACK)at->at_function;
87b510d5
MD
101 func((void *)at->at_context);
102 lwkt_replymsg(&at->at_msg, 0);
5ed44076 103 }
87b510d5 104 kthread_exit();
5ed44076 105}
5ed44076 106
87b510d5
MD
107/*
108 * Queue an ACPI message for execution by allocating a LWKT message structure
109 * and sending the message to the helper thread. The reply port is setup
110 * to automatically free the message.
111 */
5ed44076 112ACPI_STATUS
e1eeedd0
YT
113AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
114 void *Context)
5ed44076
MD
115{
116 struct acpi_task *at;
5ed44076 117
e1eeedd0
YT
118 switch (Type) {
119 case OSL_GLOBAL_LOCK_HANDLER:
120 case OSL_NOTIFY_HANDLER:
121 case OSL_GPE_HANDLER:
122 case OSL_DEBUGGER_THREAD:
123 case OSL_EC_POLL_HANDLER:
124 case OSL_EC_BURST_HANDLER:
5ed44076
MD
125 break;
126 default:
f9d8cd12 127 return_ACPI_STATUS (AE_BAD_PARAMETER);
5ed44076 128 }
5ed44076 129
87b510d5 130 /* Note: Interrupt Context */
efda3bd0 131 at = kmalloc(sizeof(*at), M_ACPITASK, M_INTWAIT | M_ZERO);
4599cf19 132 lwkt_initmsg(&at->at_msg, &acpi_afree_rport, 0);
87b510d5
MD
133 at->at_function = Function;
134 at->at_context = Context;
e1eeedd0 135 at->at_type = Type;
87b510d5 136 lwkt_sendmsg(&acpi_task_td->td_msgport, &at->at_msg);
f9d8cd12 137 return_ACPI_STATUS (AE_OK);
5ed44076
MD
138}
139
87b510d5
MD
140/*
141 * The message's reply port just frees the message.
142 */
5ed44076 143static void
87b510d5 144acpi_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
5ed44076 145{
efda3bd0 146 kfree(msg, M_ACPITASK);
5ed44076
MD
147}
148
b18b56bd
YT
149UINT64
150AcpiOsGetTimer (void)
151{
152 struct timeval time;
153
154 microtime(&time);
155
156 /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */
157
158 return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10));
159}
87b510d5 160
5ed44076 161void
b18b56bd 162AcpiOsSleep(ACPI_INTEGER Milliseconds)
5ed44076
MD
163{
164 int timo;
165 static int dummy;
166
167 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
168
b18b56bd 169 timo = Milliseconds * hz / 1000;
5ed44076
MD
170
171 /*
87b510d5
MD
172 * If requested sleep time is less than our hz resolution, or if
173 * the system is in early boot before the system tick is operational,
174 * use DELAY instead for better granularity.
5ed44076 175 */
87b510d5 176 if (clocks_running == 0) {
b18b56bd 177 while (timo > 1000000) {
87b510d5 178 DELAY(1000000);
b18b56bd 179 timo -= 1000000;
87b510d5 180 }
b18b56bd
YT
181 if (timo)
182 DELAY(timo * 1000);
183 } else if (timo > 1000) {
5ed44076 184 tsleep(&dummy, 0, "acpislp", timo);
87b510d5 185 } else {
5ed44076 186 DELAY(Milliseconds * 1000);
87b510d5 187 }
5ed44076
MD
188 return_VOID;
189}
190
191void
192AcpiOsStall(UINT32 Microseconds)
193{
194 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
195
196 DELAY(Microseconds);
197 return_VOID;
198}
199
e774ca6d 200ACPI_THREAD_ID
5ed44076
MD
201AcpiOsGetThreadId(void)
202{
203 struct proc *p;
f9d8cd12
MD
204
205 /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */
5ed44076
MD
206
207 p = curproc;
5ed44076
MD
208 if (p == NULL)
209 p = &proc0;
5ed44076 210 KASSERT(p != NULL, ("%s: curproc is NULL!", __func__));
f9d8cd12
MD
211
212 /* Returning 0 is not allowed. */
213 return (p->p_pid + 1);
5ed44076 214}