OsdSchedule.c revision 1.5
11.5Sjmcneill/*	$NetBSD: OsdSchedule.c,v 1.5 2007/12/21 18:42:38 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.5Sjmcneill__KERNEL_RCSID(0, "$NetBSD: OsdSchedule.c,v 1.5 2007/12/21 18:42:38 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.1Skochi#define	_COMPONENT	ACPI_OS_SERVICES
621.1SkochiACPI_MODULE_NAME("SCHEDULE")
631.1Skochi
641.5Sjmcneillstatic kcondvar_t	acpi_osd_sleep_cv;
651.5Sjmcneillstatic kmutex_t		acpi_osd_sleep_mtx;
661.5Sjmcneill
671.1Skochi/*
681.1Skochi * acpi_osd_sched_init:
691.1Skochi *
701.1Skochi *	Initialize the APCICA Osd scheduler.  Called from AcpiOsInitialize().
711.1Skochi */
721.1Skochivoid
731.1Skochiacpi_osd_sched_init(void)
741.1Skochi{
751.1Skochi
761.3Sperry	ACPI_FUNCTION_TRACE(__func__);
771.1Skochi
781.1Skochi	sysmon_task_queue_init();
791.5Sjmcneill	mutex_init(&acpi_osd_sleep_mtx, MUTEX_DEFAULT, IPL_NONE);
801.5Sjmcneill	cv_init(&acpi_osd_sleep_cv, "acpislp");
811.1Skochi
821.1Skochi	return_VOID;
831.1Skochi}
841.1Skochi
851.1Skochi/*
861.1Skochi * acpi_osd_sched_fini:
871.1Skochi *
881.1Skochi *	Clean up the ACPICA Osd scheduler.  Called from AcpiOsdTerminate().
891.1Skochi */
901.1Skochivoid
911.1Skochiacpi_osd_sched_fini(void)
921.1Skochi{
931.1Skochi
941.3Sperry	ACPI_FUNCTION_TRACE(__func__);
951.1Skochi
961.1Skochi	sysmon_task_queue_fini();
971.1Skochi
981.1Skochi	return_VOID;
991.1Skochi}
1001.1Skochi
1011.1Skochi/*
1021.1Skochi * AcpiOsGetThreadId:
1031.1Skochi *
1041.1Skochi *	Obtain the ID of the currently executing thread.
1051.1Skochi */
1061.2SjmcneillACPI_THREAD_ID
1071.1SkochiAcpiOsGetThreadId(void)
1081.1Skochi{
1091.1Skochi
1101.1Skochi	/* XXX ACPI CA can call this function in interrupt context */
1111.1Skochi	if (curlwp == NULL)
1121.1Skochi		return 1;
1131.1Skochi
1141.1Skochi	/* XXX Bleh, we're not allowed to return 0 (how stupid!) */
1151.1Skochi	return (curlwp->l_proc->p_pid + 1);
1161.1Skochi}
1171.1Skochi
1181.1Skochi/*
1191.1Skochi * AcpiOsQueueForExecution:
1201.1Skochi *
1211.1Skochi *	Schedule a procedure for deferred execution.
1221.1Skochi */
1231.1SkochiACPI_STATUS
1241.2SjmcneillAcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
1251.1Skochi    void *Context)
1261.1Skochi{
1271.1Skochi	int pri;
1281.1Skochi
1291.3Sperry	ACPI_FUNCTION_TRACE(__func__);
1301.1Skochi
1311.2Sjmcneill	switch (Type) {
1321.2Sjmcneill	case OSL_GPE_HANDLER:
1331.2Sjmcneill		pri = 10;
1341.1Skochi		break;
1351.2Sjmcneill	case OSL_GLOBAL_LOCK_HANDLER:
1361.2Sjmcneill	case OSL_EC_POLL_HANDLER:
1371.2Sjmcneill	case OSL_EC_BURST_HANDLER:
1381.2Sjmcneill		pri = 5;
1391.1Skochi		break;
1401.2Sjmcneill	case OSL_NOTIFY_HANDLER:
1411.2Sjmcneill		pri = 3;
1421.1Skochi		break;
1431.2Sjmcneill	case OSL_DEBUGGER_THREAD:
1441.1Skochi		pri = 0;
1451.1Skochi		break;
1461.1Skochi	default:
1471.1Skochi		return_ACPI_STATUS(AE_BAD_PARAMETER);
1481.1Skochi	}
1491.1Skochi
1501.1Skochi	switch (sysmon_task_queue_sched(pri, Function, Context)) {
1511.1Skochi	case 0:
1521.1Skochi		return_ACPI_STATUS(AE_OK);
1531.1Skochi
1541.1Skochi	case ENOMEM:
1551.1Skochi		return_ACPI_STATUS(AE_NO_MEMORY);
1561.1Skochi
1571.1Skochi	default:
1581.1Skochi		return_ACPI_STATUS(AE_BAD_PARAMETER);
1591.1Skochi	}
1601.1Skochi}
1611.1Skochi
1621.1Skochi/*
1631.1Skochi * AcpiOsSleep:
1641.1Skochi *
1651.1Skochi *	Suspend the running task (coarse granularity).
1661.1Skochi */
1671.1Skochivoid
1681.1SkochiAcpiOsSleep(ACPI_INTEGER Milliseconds)
1691.1Skochi{
1701.3Sperry	ACPI_FUNCTION_TRACE(__func__);
1711.1Skochi
1721.5Sjmcneill	mutex_enter(&acpi_osd_sleep_mtx);
1731.5Sjmcneill	cv_timedwait_sig(&acpi_osd_sleep_cv, &acpi_osd_sleep_mtx,
1741.5Sjmcneill	    MAX(mstohz(Milliseconds), 1));
1751.5Sjmcneill	mutex_exit(&acpi_osd_sleep_mtx);
1761.1Skochi}
1771.1Skochi
1781.1Skochi/*
1791.1Skochi * AcpiOsStall:
1801.1Skochi *
1811.1Skochi *	Suspend the running task (fine granularity).
1821.1Skochi */
1831.1Skochivoid
1841.1SkochiAcpiOsStall(UINT32 Microseconds)
1851.1Skochi{
1861.1Skochi
1871.3Sperry	ACPI_FUNCTION_TRACE(__func__);
1881.1Skochi
1891.1Skochi	/*
1901.1Skochi	 * sleep(9) isn't safe because AcpiOsStall may be called
1911.1Skochi	 * with interrupt-disabled. (eg. by AcpiEnterSleepState)
1921.1Skochi	 * we should watch out for long stall requests.
1931.1Skochi	 */
1941.1Skochi#ifdef ACPI_DEBUG
1951.1Skochi	if (Microseconds > 1000)
1961.1Skochi		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "long stall: %uus\n",
1971.1Skochi		    Microseconds));
1981.1Skochi#endif
1991.1Skochi
2001.1Skochi	delay(Microseconds);
2011.1Skochi
2021.1Skochi	return_VOID;
2031.1Skochi
2041.1Skochi}
2051.1Skochi
2061.1Skochi/*
2071.4Sjmcneill * AcpiOsGetTimer:
2081.1Skochi *
2091.1Skochi *	Get the current system time in 100 nanosecond units
2101.1Skochi */
2111.1SkochiUINT64
2121.1SkochiAcpiOsGetTimer(void)
2131.1Skochi{
2141.1Skochi	struct timeval tv;
2151.1Skochi	UINT64 t;
2161.1Skochi
2171.1Skochi	/* XXX During early boot there is no (decent) timer available yet. */
2181.1Skochi	if (cold)
2191.1Skochi		panic("acpi: timer op not yet supported during boot");
2201.1Skochi
2211.1Skochi	microtime(&tv);
2221.1Skochi	t = (UINT64)10 * tv.tv_usec;
2231.1Skochi	t += (UINT64)10000000 * tv.tv_sec;
2241.1Skochi
2251.1Skochi	return (t);
2261.1Skochi}
227