1 /* $NetBSD: OsdSchedule.c,v 1.7 2008/04/24 21:42:05 jmcneill Exp $ */ 2 3 /* 4 * Copyright 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * OS Services Layer 40 * 41 * 6.3: Scheduling services 42 */ 43 44 #include <sys/cdefs.h> 45 __KERNEL_RCSID(0, "$NetBSD: OsdSchedule.c,v 1.7 2008/04/24 21:42:05 jmcneill Exp $"); 46 47 #include <sys/param.h> 48 #include <sys/malloc.h> 49 #include <sys/proc.h> 50 #include <sys/systm.h> 51 #include <sys/kernel.h> 52 #include <sys/condvar.h> 53 #include <sys/mutex.h> 54 55 #include <dev/acpi/acpica.h> 56 57 #include <dev/acpi/acpi_osd.h> 58 59 #include <dev/sysmon/sysmon_taskq.h> 60 61 #define _COMPONENT ACPI_OS_SERVICES 62 ACPI_MODULE_NAME("SCHEDULE") 63 64 static kcondvar_t acpi_osd_sleep_cv; 65 static kmutex_t acpi_osd_sleep_mtx; 66 67 /* 68 * acpi_osd_sched_init: 69 * 70 * Initialize the APCICA Osd scheduler. Called from AcpiOsInitialize(). 71 */ 72 void 73 acpi_osd_sched_init(void) 74 { 75 76 ACPI_FUNCTION_TRACE(__func__); 77 78 sysmon_task_queue_init(); 79 mutex_init(&acpi_osd_sleep_mtx, MUTEX_DEFAULT, IPL_NONE); 80 cv_init(&acpi_osd_sleep_cv, "acpislp"); 81 82 return_VOID; 83 } 84 85 /* 86 * acpi_osd_sched_fini: 87 * 88 * Clean up the ACPICA Osd scheduler. Called from AcpiOsdTerminate(). 89 */ 90 void 91 acpi_osd_sched_fini(void) 92 { 93 94 ACPI_FUNCTION_TRACE(__func__); 95 96 sysmon_task_queue_fini(); 97 98 return_VOID; 99 } 100 101 /* 102 * AcpiOsGetThreadId: 103 * 104 * Obtain the ID of the currently executing thread. 105 */ 106 ACPI_THREAD_ID 107 AcpiOsGetThreadId(void) 108 { 109 return (ACPI_THREAD_ID)curlwp; 110 } 111 112 /* 113 * AcpiOsQueueForExecution: 114 * 115 * Schedule a procedure for deferred execution. 116 */ 117 ACPI_STATUS 118 AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, 119 void *Context) 120 { 121 int pri; 122 123 ACPI_FUNCTION_TRACE(__func__); 124 125 switch (Type) { 126 case OSL_GPE_HANDLER: 127 pri = 10; 128 break; 129 case OSL_GLOBAL_LOCK_HANDLER: 130 case OSL_EC_POLL_HANDLER: 131 case OSL_EC_BURST_HANDLER: 132 pri = 5; 133 break; 134 case OSL_NOTIFY_HANDLER: 135 pri = 3; 136 break; 137 case OSL_DEBUGGER_THREAD: 138 pri = 0; 139 break; 140 default: 141 return_ACPI_STATUS(AE_BAD_PARAMETER); 142 } 143 144 switch (sysmon_task_queue_sched(pri, Function, Context)) { 145 case 0: 146 return_ACPI_STATUS(AE_OK); 147 148 case ENOMEM: 149 return_ACPI_STATUS(AE_NO_MEMORY); 150 151 default: 152 return_ACPI_STATUS(AE_BAD_PARAMETER); 153 } 154 } 155 156 /* 157 * AcpiOsSleep: 158 * 159 * Suspend the running task (coarse granularity). 160 */ 161 void 162 AcpiOsSleep(ACPI_INTEGER Milliseconds) 163 { 164 ACPI_FUNCTION_TRACE(__func__); 165 166 mutex_enter(&acpi_osd_sleep_mtx); 167 cv_timedwait_sig(&acpi_osd_sleep_cv, &acpi_osd_sleep_mtx, 168 MAX(mstohz(Milliseconds), 1)); 169 mutex_exit(&acpi_osd_sleep_mtx); 170 } 171 172 /* 173 * AcpiOsStall: 174 * 175 * Suspend the running task (fine granularity). 176 */ 177 void 178 AcpiOsStall(UINT32 Microseconds) 179 { 180 181 ACPI_FUNCTION_TRACE(__func__); 182 183 /* 184 * sleep(9) isn't safe because AcpiOsStall may be called 185 * with interrupt-disabled. (eg. by AcpiEnterSleepState) 186 * we should watch out for long stall requests. 187 */ 188 #ifdef ACPI_DEBUG 189 if (Microseconds > 1000) 190 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "long stall: %uus\n", 191 Microseconds)); 192 #endif 193 194 delay(Microseconds); 195 196 return_VOID; 197 198 } 199 200 /* 201 * AcpiOsGetTimer: 202 * 203 * Get the current system time in 100 nanosecond units 204 */ 205 UINT64 206 AcpiOsGetTimer(void) 207 { 208 struct timeval tv; 209 UINT64 t; 210 211 /* XXX During early boot there is no (decent) timer available yet. */ 212 if (cold) 213 panic("acpi: timer op not yet supported during boot"); 214 215 microtime(&tv); 216 t = (UINT64)10 * tv.tv_usec; 217 t += (UINT64)10000000 * tv.tv_sec; 218 219 return (t); 220 } 221