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$ 28 */ 29 30 /* 31 * 6.3 : 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/proc.h> 43 #include <sys/taskqueue.h> 44 #include <machine/clock.h> 45 46 #include "acpi.h" 47 #include <dev/acpica/acpivar.h> 48 49 #define _COMPONENT ACPI_OS_SERVICES 50 ACPI_MODULE_NAME("SCHEDULE") 51 52 /* 53 * This is a little complicated due to the fact that we need to build and then 54 * free a 'struct task' for each task we enqueue. 55 */ 56 57 MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task"); 58 59 static void AcpiOsExecuteQueue(void *arg, int pending); 60 61 struct acpi_task { 62 struct task at_task; 63 OSD_EXECUTION_CALLBACK at_function; 64 void *at_context; 65 }; 66 67 struct acpi_task_queue { 68 STAILQ_ENTRY(acpi_task_queue) at_q; 69 struct acpi_task *at; 70 }; 71 72 #if __FreeBSD_version >= 500000 73 /* 74 * Private task queue definition for ACPI 75 */ 76 TASKQUEUE_DECLARE(acpi); 77 static void *taskqueue_acpi_ih; 78 79 static void 80 taskqueue_acpi_enqueue(void *context) 81 { 82 swi_sched(taskqueue_acpi_ih, 0); 83 } 84 85 static void 86 taskqueue_acpi_run(void *dummy) 87 { 88 taskqueue_run(taskqueue_acpi); 89 } 90 91 TASKQUEUE_DEFINE(acpi, taskqueue_acpi_enqueue, 0, 92 swi_add(NULL, "acpitaskq", taskqueue_acpi_run, NULL, 93 SWI_TQ, 0, &taskqueue_acpi_ih)); 94 95 #ifdef ACPI_USE_THREADS 96 static STAILQ_HEAD(, acpi_task_queue) acpi_task_queue; 97 static struct mtx acpi_task_mtx; 98 99 static void 100 acpi_task_thread(void *arg) 101 { 102 struct acpi_task_queue *atq; 103 OSD_EXECUTION_CALLBACK Function; 104 void *Context; 105 106 for (;;) { 107 mtx_lock(&acpi_task_mtx); 108 if ((atq = STAILQ_FIRST(&acpi_task_queue)) == NULL) { 109 msleep(&acpi_task_queue, &acpi_task_mtx, PCATCH, "actask", 0); 110 mtx_unlock(&acpi_task_mtx); 111 continue; 112 } 113 114 STAILQ_REMOVE_HEAD(&acpi_task_queue, at_q); 115 mtx_unlock(&acpi_task_mtx); 116 117 Function = (OSD_EXECUTION_CALLBACK)atq->at->at_function; 118 Context = atq->at->at_context; 119 120 mtx_lock(&Giant); 121 Function(Context); 122 123 free(atq->at, M_ACPITASK); 124 free(atq, M_ACPITASK); 125 mtx_unlock(&Giant); 126 } 127 128 kthread_exit(0); 129 } 130 131 int 132 acpi_task_thread_init(void) 133 { 134 int i, err; 135 struct proc *acpi_kthread_proc; 136 137 err = 0; 138 STAILQ_INIT(&acpi_task_queue); 139 mtx_init(&acpi_task_mtx, "ACPI task", NULL, MTX_DEF); 140 141 for (i = 0; i < ACPI_MAX_THREADS; i++) { 142 err = kthread_create(acpi_task_thread, NULL, &acpi_kthread_proc, 143 0, 0, "acpi_task%d", i); 144 if (err != 0) { 145 printf("%s: kthread_create failed(%d)\n", __func__, err); 146 break; 147 } 148 } 149 return (err); 150 } 151 #endif /* ACPI_USE_THREADS */ 152 #endif /* __FreeBSD_version >= 500000 */ 153 154 /* This function is called in interrupt context. */ 155 ACPI_STATUS 156 AcpiOsQueueForExecution(UINT32 Priority, OSD_EXECUTION_CALLBACK Function, 157 void *Context) 158 { 159 struct acpi_task *at; 160 int pri; 161 162 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 163 164 if (Function == NULL) 165 return_ACPI_STATUS (AE_BAD_PARAMETER); 166 167 at = malloc(sizeof(*at), M_ACPITASK, M_NOWAIT | M_ZERO); 168 if (at == NULL) 169 return_ACPI_STATUS (AE_NO_MEMORY); 170 171 at->at_function = Function; 172 at->at_context = Context; 173 switch (Priority) { 174 case OSD_PRIORITY_GPE: 175 pri = 4; 176 break; 177 case OSD_PRIORITY_HIGH: 178 pri = 3; 179 break; 180 case OSD_PRIORITY_MED: 181 pri = 2; 182 break; 183 case OSD_PRIORITY_LO: 184 pri = 1; 185 break; 186 default: 187 free(at, M_ACPITASK); 188 return_ACPI_STATUS (AE_BAD_PARAMETER); 189 } 190 TASK_INIT(&at->at_task, pri, AcpiOsExecuteQueue, at); 191 192 #if __FreeBSD_version >= 500000 193 taskqueue_enqueue(taskqueue_acpi, (struct task *)at); 194 #else 195 taskqueue_enqueue(taskqueue_swi, (struct task *)at); 196 #endif 197 198 return_ACPI_STATUS (AE_OK); 199 } 200 201 static void 202 AcpiOsExecuteQueue(void *arg, int pending) 203 { 204 struct acpi_task *at; 205 struct acpi_task_queue *atq; 206 OSD_EXECUTION_CALLBACK Function; 207 void *Context; 208 209 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 210 211 at = (struct acpi_task *)arg; 212 atq = NULL; 213 Function = NULL; 214 Context = NULL; 215 216 #ifdef ACPI_USE_THREADS 217 atq = malloc(sizeof(*atq), M_ACPITASK, M_NOWAIT); 218 if (atq == NULL) { 219 printf("%s: no memory\n", __func__); 220 return; 221 } 222 223 atq->at = at; 224 225 mtx_lock(&acpi_task_mtx); 226 STAILQ_INSERT_TAIL(&acpi_task_queue, atq, at_q); 227 mtx_unlock(&acpi_task_mtx); 228 wakeup_one(&acpi_task_queue); 229 #else 230 Function = (OSD_EXECUTION_CALLBACK)at->at_function; 231 Context = at->at_context; 232 233 Function(Context); 234 free(at, M_ACPITASK); 235 #endif 236 237 return_VOID; 238 } 239 240 void 241 AcpiOsSleep(UINT32 Seconds, UINT32 Milliseconds) 242 { 243 int timo; 244 static int dummy; 245 246 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 247 248 timo = (Seconds * hz) + Milliseconds * hz / 1000; 249 250 /* 251 * If requested sleep time is less than our hz resolution, use 252 * DELAY instead for better granularity. 253 */ 254 if (timo > 0) 255 tsleep(&dummy, 0, "acpislp", timo); 256 else 257 DELAY(Milliseconds * 1000); 258 259 return_VOID; 260 } 261 262 void 263 AcpiOsStall(UINT32 Microseconds) 264 { 265 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 266 267 DELAY(Microseconds); 268 return_VOID; 269 } 270 271 UINT32 272 AcpiOsGetThreadId(void) 273 { 274 struct proc *p; 275 276 /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */ 277 278 p = curproc; 279 #if __FreeBSD_version < 500000 280 if (p == NULL) 281 p = &proc0; 282 #endif 283 KASSERT(p != NULL, ("%s: curproc is NULL!", __func__)); 284 285 /* Returning 0 is not allowed. */ 286 return (p->p_pid + 1); 287 } 288