OsdSchedule.c revision 146021
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: head/sys/dev/acpica/Osd/OsdSchedule.c 146021 2005-05-09 07:34:04Z marks $ 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 50ACPI_MODULE_NAME("SCHEDULE") 51 52/* 53 * Allow the user to tune the number of task threads we start. It seems 54 * some systems have problems with increased parallelism. 55 */ 56static int acpi_max_threads = ACPI_MAX_THREADS; 57TUNABLE_INT("debug.acpi.max_threads", &acpi_max_threads); 58 59MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task"); 60 61struct acpi_task_ctx { 62 struct task at_task; 63 ACPI_OSD_EXEC_CALLBACK at_function; 64 void *at_context; 65}; 66 67/* 68 * Private task queue definition for ACPI 69 */ 70static struct proc * 71acpi_task_start_threads(struct taskqueue **tqp) 72{ 73 struct proc *acpi_kthread_proc; 74 int err, i; 75 76 KASSERT(*tqp != NULL, ("acpi taskqueue not created before threads")); 77 78 /* Start one or more threads to service our taskqueue. */ 79 for (i = 0; i < acpi_max_threads; i++) { 80 err = kthread_create(taskqueue_thread_loop, tqp, &acpi_kthread_proc, 81 0, 0, "acpi_task%d", i); 82 if (err) { 83 printf("%s: kthread_create failed (%d)\n", __func__, err); 84 break; 85 } 86 } 87 return (acpi_kthread_proc); 88} 89 90TASKQUEUE_DEFINE(acpi, taskqueue_thread_enqueue, &taskqueue_acpi, 91 taskqueue_acpi_proc = acpi_task_start_threads(&taskqueue_acpi)); 92 93/* 94 * Bounce through this wrapper function since ACPI-CA doesn't understand 95 * the pending argument for its callbacks. 96 */ 97static void 98acpi_task_execute(void *context, int pending) 99{ 100 struct acpi_task_ctx *at; 101 102 at = (struct acpi_task_ctx *)context; 103 at->at_function(at->at_context); 104 free(at, M_ACPITASK); 105} 106 107/* 108 * This function may be called in interrupt context, i.e. when a GPE fires. 109 * We allocate and queue a task for one of our taskqueue threads to process. 110 */ 111ACPI_STATUS 112AcpiOsQueueForExecution(UINT32 Priority, ACPI_OSD_EXEC_CALLBACK Function, 113 void *Context) 114{ 115 struct acpi_task_ctx *at; 116 int pri; 117 118 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 119 120 if (Function == NULL) 121 return_ACPI_STATUS (AE_BAD_PARAMETER); 122 123 at = malloc(sizeof(*at), M_ACPITASK, M_NOWAIT); 124 if (at == NULL) 125 return_ACPI_STATUS (AE_NO_MEMORY); 126 127 at->at_function = Function; 128 at->at_context = Context; 129 switch (Priority) { 130 case OSD_PRIORITY_GPE: 131 pri = 4; 132 break; 133 case OSD_PRIORITY_HIGH: 134 pri = 3; 135 break; 136 case OSD_PRIORITY_MED: 137 pri = 2; 138 break; 139 case OSD_PRIORITY_LO: 140 pri = 1; 141 break; 142 default: 143 free(at, M_ACPITASK); 144 return_ACPI_STATUS (AE_BAD_PARAMETER); 145 } 146 147 TASK_INIT(&at->at_task, pri, acpi_task_execute, at); 148 taskqueue_enqueue(taskqueue_acpi, &at->at_task); 149 150 return_ACPI_STATUS (AE_OK); 151} 152 153void 154AcpiOsSleep(ACPI_INTEGER Milliseconds) 155{ 156 int timo; 157 static int dummy; 158 159 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 160 161 timo = Milliseconds * hz / 1000; 162 163 /* 164 * If requested sleep time is less than our hz resolution, use 165 * DELAY instead for better granularity. 166 */ 167 if (timo > 0) 168 tsleep(&dummy, 0, "acpislp", timo); 169 else 170 DELAY(Milliseconds * 1000); 171 172 return_VOID; 173} 174 175/* 176 * Return the current time in 100 nanosecond units 177 */ 178UINT64 179AcpiOsGetTimer(void) 180{ 181 struct bintime bt; 182 UINT64 t; 183 184 /* XXX During early boot there is no (decent) timer available yet. */ 185 if (cold) 186 panic("acpi: timer op not yet supported during boot"); 187 188 binuptime(&bt); 189 t = ((UINT64)10000000 * (uint32_t)(bt.frac >> 32)) >> 32; 190 t += bt.sec * 10000000; 191 192 return (t); 193} 194 195void 196AcpiOsStall(UINT32 Microseconds) 197{ 198 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 199 200 DELAY(Microseconds); 201 return_VOID; 202} 203 204UINT32 205AcpiOsGetThreadId(void) 206{ 207 struct proc *p; 208 209 /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */ 210 211 p = curproc; 212 KASSERT(p != NULL, ("%s: curproc is NULL!", __func__)); 213 214 /* Returning 0 is not allowed. */ 215 return (p->p_pid + 1); 216} 217