2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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.10 2008/06/05 18:06:31 swildner Exp $
32 * 6.3 : Scheduling services
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/interrupt.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/malloc.h>
44 #include <sys/msgport.h>
45 #include <sys/taskqueue.h>
46 #include <machine/clock.h>
48 #include <sys/thread2.h>
49 #include <sys/msgport2.h>
50 #include <sys/mplock2.h>
54 #include <dev/acpica5/acpivar.h>
56 #define _COMPONENT ACPI_OS_SERVICES
57 ACPI_MODULE_NAME("SCHEDULE")
60 * This is a little complicated due to the fact that we need to build and then
61 * free a 'struct task' for each task we enqueue.
64 MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
66 static void acpi_task_thread(void *arg);
67 static void acpi_autofree_reply(lwkt_port_t port, lwkt_msg_t msg);
70 struct lwkt_msg at_msg;
71 ACPI_OSD_EXEC_CALLBACK at_function;
73 ACPI_EXECUTE_TYPE at_type;
76 static struct thread *acpi_task_td;
77 struct lwkt_port acpi_afree_rport;
80 * Initialize the ACPI helper thread.
83 acpi_task_thread_init(void)
85 lwkt_initport_replyonly(&acpi_afree_rport, acpi_autofree_reply);
86 kthread_create(acpi_task_thread, NULL, &acpi_task_td,
92 * The ACPI helper thread processes OSD execution callback messages.
95 acpi_task_thread(void *arg)
97 ACPI_OSD_EXEC_CALLBACK func;
102 at = (void *)lwkt_waitport(&curthread->td_msgport, 0);
103 func = (ACPI_OSD_EXEC_CALLBACK)at->at_function;
104 func((void *)at->at_context);
105 lwkt_replymsg(&at->at_msg, 0);
111 * Queue an ACPI message for execution by allocating a LWKT message structure
112 * and sending the message to the helper thread. The reply port is setup
113 * to automatically free the message.
116 AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
119 struct acpi_task *at;
122 case OSL_GLOBAL_LOCK_HANDLER:
123 case OSL_NOTIFY_HANDLER:
124 case OSL_GPE_HANDLER:
125 case OSL_DEBUGGER_THREAD:
126 case OSL_EC_POLL_HANDLER:
127 case OSL_EC_BURST_HANDLER:
130 return_ACPI_STATUS (AE_BAD_PARAMETER);
133 /* Note: Interrupt Context */
134 at = kmalloc(sizeof(*at), M_ACPITASK, M_INTWAIT | M_ZERO);
135 lwkt_initmsg(&at->at_msg, &acpi_afree_rport, 0);
136 at->at_function = Function;
137 at->at_context = Context;
139 lwkt_sendmsg(&acpi_task_td->td_msgport, &at->at_msg);
140 return_ACPI_STATUS (AE_OK);
144 * The message's reply port just frees the message.
147 acpi_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
149 kfree(msg, M_ACPITASK);
153 AcpiOsGetTimer (void)
159 /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */
161 return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10));
165 AcpiOsSleep(ACPI_INTEGER Milliseconds)
170 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
172 timo = Milliseconds * hz / 1000;
175 * If requested sleep time is less than our hz resolution, or if
176 * the system is in early boot before the system tick is operational,
177 * use DELAY instead for better granularity.
179 if (clocks_running == 0) {
180 while (timo > 1000000) {
186 } else if (timo > 0) {
187 tsleep(&dummy, 0, "acpislp", timo);
189 DELAY(Milliseconds * 1000);
195 AcpiOsStall(UINT32 Microseconds)
197 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
204 AcpiOsGetThreadId(void)
208 /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */
213 KASSERT(p != NULL, ("%s: curproc is NULL!", __func__));
215 /* Returning 0 is not allowed. */
216 return (p->p_pid + 1);