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 28 /* 29 * 6.3 : Scheduling services 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_acpi.h" 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/bus.h> 39 #include <sys/interrupt.h> 40 #include <sys/kernel.h> 41 #include <sys/kthread.h> 42 #include <sys/malloc.h> 43 #include <sys/proc.h> 44 #include <sys/taskqueue.h> 45 46 #include <contrib/dev/acpica/acpi.h> 47 #include <dev/acpica/acpivar.h> 48 49 #define _COMPONENT ACPI_OS_SERVICES 50 ACPI_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 */ 56 static int acpi_max_threads = ACPI_MAX_THREADS; 57 TUNABLE_INT("debug.acpi.max_threads", &acpi_max_threads); 58 59 MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task"); 60 61 struct acpi_task_ctx { 62 struct task at_task; 63 ACPI_OSD_EXEC_CALLBACK at_function; 64 void *at_context; 65 }; 66 67 TASKQUEUE_DEFINE(acpi, taskqueue_thread_enqueue, &taskqueue_acpi, 68 taskqueue_start_threads(&taskqueue_acpi, acpi_max_threads, PWAIT, 69 "acpi_task")); 70 71 /* 72 * Bounce through this wrapper function since ACPI-CA doesn't understand 73 * the pending argument for its callbacks. 74 */ 75 static void 76 acpi_task_execute(void *context, int pending) 77 { 78 struct acpi_task_ctx *at; 79 80 at = (struct acpi_task_ctx *)context; 81 at->at_function(at->at_context); 82 free(at, M_ACPITASK); 83 } 84 85 /* 86 * This function may be called in interrupt context, i.e. when a GPE fires. 87 * We allocate and queue a task for one of our taskqueue threads to process. 88 */ 89 ACPI_STATUS 90 AcpiOsQueueForExecution(UINT32 Priority, ACPI_OSD_EXEC_CALLBACK Function, 91 void *Context) 92 { 93 struct acpi_task_ctx *at; 94 int pri; 95 96 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 97 98 if (Function == NULL) 99 return_ACPI_STATUS (AE_BAD_PARAMETER); 100 101 at = malloc(sizeof(*at), M_ACPITASK, M_NOWAIT); 102 if (at == NULL) 103 return_ACPI_STATUS (AE_NO_MEMORY); 104 105 at->at_function = Function; 106 at->at_context = Context; 107 switch (Priority) { 108 case OSD_PRIORITY_GPE: 109 pri = 4; 110 break; 111 case OSD_PRIORITY_HIGH: 112 pri = 3; 113 break; 114 case OSD_PRIORITY_MED: 115 pri = 2; 116 break; 117 case OSD_PRIORITY_LO: 118 pri = 1; 119 break; 120 default: 121 free(at, M_ACPITASK); 122 return_ACPI_STATUS (AE_BAD_PARAMETER); 123 } 124 125 TASK_INIT(&at->at_task, pri, acpi_task_execute, at); 126 taskqueue_enqueue(taskqueue_acpi, &at->at_task); 127 128 return_ACPI_STATUS (AE_OK); 129 } 130 131 void 132 AcpiOsSleep(ACPI_INTEGER Milliseconds) 133 { 134 int timo; 135 136 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 137 138 timo = Milliseconds * hz / 1000; 139 140 /* 141 * If requested sleep time is less than our hz resolution, use 142 * DELAY instead for better granularity. 143 */ 144 if (timo > 0) 145 pause("acpislp", timo); 146 else 147 DELAY(Milliseconds * 1000); 148 149 return_VOID; 150 } 151 152 /* 153 * Return the current time in 100 nanosecond units 154 */ 155 UINT64 156 AcpiOsGetTimer(void) 157 { 158 struct bintime bt; 159 UINT64 t; 160 161 /* XXX During early boot there is no (decent) timer available yet. */ 162 if (cold) 163 panic("acpi: timer op not yet supported during boot"); 164 165 binuptime(&bt); 166 t = ((UINT64)10000000 * (uint32_t)(bt.frac >> 32)) >> 32; 167 t += bt.sec * 10000000; 168 169 return (t); 170 } 171 172 void 173 AcpiOsStall(UINT32 Microseconds) 174 { 175 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 176 177 DELAY(Microseconds); 178 return_VOID; 179 } 180 181 UINT32 182 AcpiOsGetThreadId(void) 183 { 184 struct proc *p; 185 186 /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */ 187 188 p = curproc; 189 KASSERT(p != NULL, ("%s: curproc is NULL!", __func__)); 190 191 /* Returning 0 is not allowed. */ 192 return (p->p_pid + 1); 193 } 194