OsdSchedule.c revision 1.11
11.11Sjmcneill/* $NetBSD: OsdSchedule.c,v 1.11 2009/08/18 16:41:02 jmcneill Exp $ */ 21.1Skochi 31.1Skochi/* 41.1Skochi * Copyright 2001 Wasabi Systems, Inc. 51.1Skochi * All rights reserved. 61.1Skochi * 71.1Skochi * Written by Jason R. Thorpe for Wasabi Systems, Inc. 81.1Skochi * 91.1Skochi * Redistribution and use in source and binary forms, with or without 101.1Skochi * modification, are permitted provided that the following conditions 111.1Skochi * are met: 121.1Skochi * 1. Redistributions of source code must retain the above copyright 131.1Skochi * notice, this list of conditions and the following disclaimer. 141.1Skochi * 2. Redistributions in binary form must reproduce the above copyright 151.1Skochi * notice, this list of conditions and the following disclaimer in the 161.1Skochi * documentation and/or other materials provided with the distribution. 171.1Skochi * 3. All advertising materials mentioning features or use of this software 181.1Skochi * must display the following acknowledgement: 191.1Skochi * This product includes software developed for the NetBSD Project by 201.1Skochi * Wasabi Systems, Inc. 211.1Skochi * 4. The name of Wasabi Systems, Inc. may not be used to endorse 221.1Skochi * or promote products derived from this software without specific prior 231.1Skochi * written permission. 241.1Skochi * 251.1Skochi * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 261.1Skochi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 271.1Skochi * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 281.1Skochi * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 291.1Skochi * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 301.1Skochi * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 311.1Skochi * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 321.1Skochi * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 331.1Skochi * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 341.1Skochi * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 351.1Skochi * POSSIBILITY OF SUCH DAMAGE. 361.1Skochi */ 371.1Skochi 381.1Skochi/* 391.1Skochi * OS Services Layer 401.1Skochi * 411.1Skochi * 6.3: Scheduling services 421.1Skochi */ 431.1Skochi 441.1Skochi#include <sys/cdefs.h> 451.11Sjmcneill__KERNEL_RCSID(0, "$NetBSD: OsdSchedule.c,v 1.11 2009/08/18 16:41:02 jmcneill Exp $"); 461.1Skochi 471.1Skochi#include <sys/param.h> 481.1Skochi#include <sys/malloc.h> 491.1Skochi#include <sys/proc.h> 501.1Skochi#include <sys/systm.h> 511.1Skochi#include <sys/kernel.h> 521.5Sjmcneill#include <sys/condvar.h> 531.5Sjmcneill#include <sys/mutex.h> 541.1Skochi 551.1Skochi#include <dev/acpi/acpica.h> 561.1Skochi 571.1Skochi#include <dev/acpi/acpi_osd.h> 581.1Skochi 591.1Skochi#include <dev/sysmon/sysmon_taskq.h> 601.1Skochi 611.9Sjmcneillextern int acpi_suspended; 621.9Sjmcneill 631.1Skochi#define _COMPONENT ACPI_OS_SERVICES 641.1SkochiACPI_MODULE_NAME("SCHEDULE") 651.1Skochi 661.5Sjmcneillstatic kcondvar_t acpi_osd_sleep_cv; 671.5Sjmcneillstatic kmutex_t acpi_osd_sleep_mtx; 681.5Sjmcneill 691.1Skochi/* 701.1Skochi * acpi_osd_sched_init: 711.1Skochi * 721.1Skochi * Initialize the APCICA Osd scheduler. Called from AcpiOsInitialize(). 731.1Skochi */ 741.1Skochivoid 751.1Skochiacpi_osd_sched_init(void) 761.1Skochi{ 771.1Skochi sysmon_task_queue_init(); 781.5Sjmcneill mutex_init(&acpi_osd_sleep_mtx, MUTEX_DEFAULT, IPL_NONE); 791.5Sjmcneill cv_init(&acpi_osd_sleep_cv, "acpislp"); 801.1Skochi} 811.1Skochi 821.1Skochi/* 831.1Skochi * acpi_osd_sched_fini: 841.1Skochi * 851.1Skochi * Clean up the ACPICA Osd scheduler. Called from AcpiOsdTerminate(). 861.1Skochi */ 871.1Skochivoid 881.1Skochiacpi_osd_sched_fini(void) 891.1Skochi{ 901.1Skochi sysmon_task_queue_fini(); 911.1Skochi} 921.1Skochi 931.1Skochi/* 941.1Skochi * AcpiOsGetThreadId: 951.1Skochi * 961.1Skochi * Obtain the ID of the currently executing thread. 971.1Skochi */ 981.2SjmcneillACPI_THREAD_ID 991.1SkochiAcpiOsGetThreadId(void) 1001.1Skochi{ 1011.7Sjmcneill return (ACPI_THREAD_ID)curlwp; 1021.1Skochi} 1031.1Skochi 1041.1Skochi/* 1051.1Skochi * AcpiOsQueueForExecution: 1061.1Skochi * 1071.1Skochi * Schedule a procedure for deferred execution. 1081.1Skochi */ 1091.1SkochiACPI_STATUS 1101.2SjmcneillAcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, 1111.1Skochi void *Context) 1121.1Skochi{ 1131.1Skochi int pri; 1141.1Skochi 1151.2Sjmcneill switch (Type) { 1161.2Sjmcneill case OSL_GPE_HANDLER: 1171.2Sjmcneill pri = 10; 1181.1Skochi break; 1191.2Sjmcneill case OSL_GLOBAL_LOCK_HANDLER: 1201.2Sjmcneill case OSL_EC_POLL_HANDLER: 1211.2Sjmcneill case OSL_EC_BURST_HANDLER: 1221.2Sjmcneill pri = 5; 1231.1Skochi break; 1241.2Sjmcneill case OSL_NOTIFY_HANDLER: 1251.2Sjmcneill pri = 3; 1261.1Skochi break; 1271.2Sjmcneill case OSL_DEBUGGER_THREAD: 1281.1Skochi pri = 0; 1291.1Skochi break; 1301.1Skochi default: 1311.11Sjmcneill return AE_BAD_PARAMETER; 1321.1Skochi } 1331.1Skochi 1341.1Skochi switch (sysmon_task_queue_sched(pri, Function, Context)) { 1351.1Skochi case 0: 1361.11Sjmcneill return AE_OK; 1371.1Skochi 1381.1Skochi case ENOMEM: 1391.11Sjmcneill return AE_NO_MEMORY; 1401.1Skochi 1411.1Skochi default: 1421.11Sjmcneill return AE_BAD_PARAMETER; 1431.1Skochi } 1441.1Skochi} 1451.1Skochi 1461.1Skochi/* 1471.1Skochi * AcpiOsSleep: 1481.1Skochi * 1491.1Skochi * Suspend the running task (coarse granularity). 1501.1Skochi */ 1511.1Skochivoid 1521.1SkochiAcpiOsSleep(ACPI_INTEGER Milliseconds) 1531.1Skochi{ 1541.10Sdrochner if (cold || doing_shutdown || acpi_suspended) 1551.8Sjmcneill DELAY(Milliseconds * 1000); 1561.8Sjmcneill else { 1571.8Sjmcneill mutex_enter(&acpi_osd_sleep_mtx); 1581.8Sjmcneill cv_timedwait_sig(&acpi_osd_sleep_cv, &acpi_osd_sleep_mtx, 1591.8Sjmcneill MAX(mstohz(Milliseconds), 1)); 1601.8Sjmcneill mutex_exit(&acpi_osd_sleep_mtx); 1611.8Sjmcneill } 1621.1Skochi} 1631.1Skochi 1641.1Skochi/* 1651.1Skochi * AcpiOsStall: 1661.1Skochi * 1671.1Skochi * Suspend the running task (fine granularity). 1681.1Skochi */ 1691.1Skochivoid 1701.1SkochiAcpiOsStall(UINT32 Microseconds) 1711.1Skochi{ 1721.1Skochi /* 1731.1Skochi * sleep(9) isn't safe because AcpiOsStall may be called 1741.1Skochi * with interrupt-disabled. (eg. by AcpiEnterSleepState) 1751.1Skochi * we should watch out for long stall requests. 1761.1Skochi */ 1771.1Skochi#ifdef ACPI_DEBUG 1781.1Skochi if (Microseconds > 1000) 1791.1Skochi ACPI_DEBUG_PRINT((ACPI_DB_INFO, "long stall: %uus\n", 1801.1Skochi Microseconds)); 1811.1Skochi#endif 1821.1Skochi 1831.1Skochi delay(Microseconds); 1841.1Skochi} 1851.1Skochi 1861.1Skochi/* 1871.4Sjmcneill * AcpiOsGetTimer: 1881.1Skochi * 1891.1Skochi * Get the current system time in 100 nanosecond units 1901.1Skochi */ 1911.1SkochiUINT64 1921.1SkochiAcpiOsGetTimer(void) 1931.1Skochi{ 1941.1Skochi struct timeval tv; 1951.1Skochi UINT64 t; 1961.1Skochi 1971.1Skochi /* XXX During early boot there is no (decent) timer available yet. */ 1981.1Skochi if (cold) 1991.1Skochi panic("acpi: timer op not yet supported during boot"); 2001.1Skochi 2011.1Skochi microtime(&tv); 2021.1Skochi t = (UINT64)10 * tv.tv_usec; 2031.1Skochi t += (UINT64)10000000 * tv.tv_sec; 2041.1Skochi 2051.11Sjmcneill return t; 2061.1Skochi} 207