sys/dev/acpica/Osd/OsdSchedule.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | /*- * Copyright (c) 2000 Michael Smith * Copyright (c) 2000 BSDi * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD: src/sys/dev/acpica/Osd/OsdSchedule.c,v 1.28 2004/05/06 02:18:58 njl Exp $ */ /* * Multithreading and Scheduling Services */ #include "opt_acpi.h" #include <sys/param.h> #include <sys/systm.h> #include <sys/bus.h> #include <sys/interrupt.h> #include <sys/kernel.h> #include <sys/kthread.h> #include <sys/malloc.h> #include <sys/msgport.h> #include <sys/taskqueue.h> #include <machine/clock.h> #include <sys/msgport2.h> #include "acpi.h" #include "accommon.h" #include <dev/acpica/acpivar.h> #define _COMPONENT ACPI_OS_SERVICES ACPI_MODULE_NAME("SCHEDULE") /* * This is a little complicated due to the fact that we need to build and then * free a 'struct task' for each task we enqueue. */ MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task"); static void acpi_task_thread(void *arg); static void acpi_autofree_reply(lwkt_port_t port, lwkt_msg_t msg); struct acpi_task { struct lwkt_msg at_msg; ACPI_OSD_EXEC_CALLBACK at_function; void *at_context; ACPI_EXECUTE_TYPE at_type; }; static struct thread *acpi_task_td; struct lwkt_port acpi_afree_rport; /* * Initialize the ACPI helper thread. */ int acpi_task_thread_init(void) { lwkt_initport_replyonly(&acpi_afree_rport, acpi_autofree_reply); acpi_task_td = kmalloc(sizeof(struct thread), M_DEVBUF, M_INTWAIT | M_ZERO); lwkt_create(acpi_task_thread, NULL, NULL, acpi_task_td, TDF_NOSTART, 0, "acpi_task"); return (0); } void acpi_task_thread_schedule(void) { lwkt_schedule(acpi_task_td); } /* * The ACPI helper thread processes OSD execution callback messages. */ static void acpi_task_thread(void *arg) { ACPI_OSD_EXEC_CALLBACK func; struct acpi_task *at; lwkt_gettoken(&acpi_token); for (;;) { at = (void *)lwkt_waitport(&curthread->td_msgport, 0); func = at->at_function; func(at->at_context); lwkt_replymsg(&at->at_msg, 0); } lwkt_reltoken(&acpi_token); } /* * Queue an ACPI message for execution by allocating a LWKT message structure * and sending the message to the helper thread. The reply port is setup * to automatically free the message. */ ACPI_STATUS AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, void *Context) { struct acpi_task *at; switch (Type) { case OSL_GLOBAL_LOCK_HANDLER: case OSL_NOTIFY_HANDLER: case OSL_GPE_HANDLER: case OSL_DEBUGGER_EXEC_THREAD: case OSL_EC_POLL_HANDLER: case OSL_EC_BURST_HANDLER: break; default: return_ACPI_STATUS (AE_BAD_PARAMETER); } /* Note: Interrupt Context */ at = kmalloc(sizeof(*at), M_ACPITASK, M_INTWAIT | M_ZERO); lwkt_initmsg(&at->at_msg, &acpi_afree_rport, 0); at->at_function = Function; at->at_context = Context; at->at_type = Type; lwkt_sendmsg(&acpi_task_td->td_msgport, &at->at_msg); return_ACPI_STATUS (AE_OK); } /* * The message's reply port just frees the message. */ static void acpi_autofree_reply(lwkt_port_t port, lwkt_msg_t msg) { kfree(msg, M_ACPITASK); } UINT64 AcpiOsGetTimer (void) { struct timeval time; microtime(&time); /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */ return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10)); } void AcpiOsSleep(UINT64 Milliseconds) { int timo; static int dummy; ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); timo = Milliseconds * hz / 1000; /* * If requested sleep time is less than our hz resolution, or if * the system is in early boot before the system tick is operational, * use DELAY instead for better granularity. */ if (clocks_running == 0) { while (timo > 1000000) { DELAY(1000000); timo -= 1000000; } if (timo) DELAY(timo * 1000); } else if (timo > 0) { tsleep(&dummy, 0, "acpislp", timo); } else { DELAY(Milliseconds * 1000); } return_VOID; } void AcpiOsStall(UINT32 Microseconds) { ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); DELAY(Microseconds); return_VOID; } ACPI_THREAD_ID AcpiOsGetThreadId(void) { /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */ return ((uintptr_t)curthread); } |