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