OsdSchedule.c revision 1.4
11.4Sjmcneill/*	$NetBSD: OsdSchedule.c,v 1.4 2007/12/17 15:02:31 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.4Sjmcneill__KERNEL_RCSID(0, "$NetBSD: OsdSchedule.c,v 1.4 2007/12/17 15:02:31 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.1Skochi
531.1Skochi#include <dev/acpi/acpica.h>
541.1Skochi
551.1Skochi#include <dev/acpi/acpi_osd.h>
561.1Skochi
571.1Skochi#include <dev/sysmon/sysmon_taskq.h>
581.1Skochi
591.1Skochi#define	_COMPONENT	ACPI_OS_SERVICES
601.1SkochiACPI_MODULE_NAME("SCHEDULE")
611.1Skochi
621.1Skochi/*
631.1Skochi * acpi_osd_sched_init:
641.1Skochi *
651.1Skochi *	Initialize the APCICA Osd scheduler.  Called from AcpiOsInitialize().
661.1Skochi */
671.1Skochivoid
681.1Skochiacpi_osd_sched_init(void)
691.1Skochi{
701.1Skochi
711.3Sperry	ACPI_FUNCTION_TRACE(__func__);
721.1Skochi
731.1Skochi	sysmon_task_queue_init();
741.1Skochi
751.1Skochi	return_VOID;
761.1Skochi}
771.1Skochi
781.1Skochi/*
791.1Skochi * acpi_osd_sched_fini:
801.1Skochi *
811.1Skochi *	Clean up the ACPICA Osd scheduler.  Called from AcpiOsdTerminate().
821.1Skochi */
831.1Skochivoid
841.1Skochiacpi_osd_sched_fini(void)
851.1Skochi{
861.1Skochi
871.3Sperry	ACPI_FUNCTION_TRACE(__func__);
881.1Skochi
891.1Skochi	sysmon_task_queue_fini();
901.1Skochi
911.1Skochi	return_VOID;
921.1Skochi}
931.1Skochi
941.1Skochi/*
951.1Skochi * AcpiOsGetThreadId:
961.1Skochi *
971.1Skochi *	Obtain the ID of the currently executing thread.
981.1Skochi */
991.2SjmcneillACPI_THREAD_ID
1001.1SkochiAcpiOsGetThreadId(void)
1011.1Skochi{
1021.1Skochi
1031.1Skochi	/* XXX ACPI CA can call this function in interrupt context */
1041.1Skochi	if (curlwp == NULL)
1051.1Skochi		return 1;
1061.1Skochi
1071.1Skochi	/* XXX Bleh, we're not allowed to return 0 (how stupid!) */
1081.1Skochi	return (curlwp->l_proc->p_pid + 1);
1091.1Skochi}
1101.1Skochi
1111.1Skochi/*
1121.1Skochi * AcpiOsQueueForExecution:
1131.1Skochi *
1141.1Skochi *	Schedule a procedure for deferred execution.
1151.1Skochi */
1161.1SkochiACPI_STATUS
1171.2SjmcneillAcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
1181.1Skochi    void *Context)
1191.1Skochi{
1201.1Skochi	int pri;
1211.1Skochi
1221.3Sperry	ACPI_FUNCTION_TRACE(__func__);
1231.1Skochi
1241.2Sjmcneill	switch (Type) {
1251.2Sjmcneill	case OSL_GPE_HANDLER:
1261.2Sjmcneill		pri = 10;
1271.1Skochi		break;
1281.2Sjmcneill	case OSL_GLOBAL_LOCK_HANDLER:
1291.2Sjmcneill	case OSL_EC_POLL_HANDLER:
1301.2Sjmcneill	case OSL_EC_BURST_HANDLER:
1311.2Sjmcneill		pri = 5;
1321.1Skochi		break;
1331.2Sjmcneill	case OSL_NOTIFY_HANDLER:
1341.2Sjmcneill		pri = 3;
1351.1Skochi		break;
1361.2Sjmcneill	case OSL_DEBUGGER_THREAD:
1371.1Skochi		pri = 0;
1381.1Skochi		break;
1391.1Skochi	default:
1401.1Skochi		return_ACPI_STATUS(AE_BAD_PARAMETER);
1411.1Skochi	}
1421.1Skochi
1431.1Skochi	switch (sysmon_task_queue_sched(pri, Function, Context)) {
1441.1Skochi	case 0:
1451.1Skochi		return_ACPI_STATUS(AE_OK);
1461.1Skochi
1471.1Skochi	case ENOMEM:
1481.1Skochi		return_ACPI_STATUS(AE_NO_MEMORY);
1491.1Skochi
1501.1Skochi	default:
1511.1Skochi		return_ACPI_STATUS(AE_BAD_PARAMETER);
1521.1Skochi	}
1531.1Skochi}
1541.1Skochi
1551.1Skochi/*
1561.1Skochi * AcpiOsSleep:
1571.1Skochi *
1581.1Skochi *	Suspend the running task (coarse granularity).
1591.1Skochi */
1601.1Skochivoid
1611.1SkochiAcpiOsSleep(ACPI_INTEGER Milliseconds)
1621.1Skochi{
1631.1Skochi	int timo;
1641.1Skochi
1651.3Sperry	ACPI_FUNCTION_TRACE(__func__);
1661.1Skochi
1671.1Skochi	timo = Milliseconds * hz / 1000;
1681.1Skochi	if (timo == 0)
1691.1Skochi		timo = 1;
1701.1Skochi
1711.1Skochi	(void) tsleep(&timo, PVM, "acpislp", timo);
1721.1Skochi
1731.1Skochi	return_VOID;
1741.1Skochi}
1751.1Skochi
1761.1Skochi/*
1771.1Skochi * AcpiOsStall:
1781.1Skochi *
1791.1Skochi *	Suspend the running task (fine granularity).
1801.1Skochi */
1811.1Skochivoid
1821.1SkochiAcpiOsStall(UINT32 Microseconds)
1831.1Skochi{
1841.1Skochi
1851.3Sperry	ACPI_FUNCTION_TRACE(__func__);
1861.1Skochi
1871.1Skochi	/*
1881.1Skochi	 * sleep(9) isn't safe because AcpiOsStall may be called
1891.1Skochi	 * with interrupt-disabled. (eg. by AcpiEnterSleepState)
1901.1Skochi	 * we should watch out for long stall requests.
1911.1Skochi	 */
1921.1Skochi#ifdef ACPI_DEBUG
1931.1Skochi	if (Microseconds > 1000)
1941.1Skochi		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "long stall: %uus\n",
1951.1Skochi		    Microseconds));
1961.1Skochi#endif
1971.1Skochi
1981.1Skochi	delay(Microseconds);
1991.1Skochi
2001.1Skochi	return_VOID;
2011.1Skochi
2021.1Skochi}
2031.1Skochi
2041.1Skochi/*
2051.4Sjmcneill * AcpiOsGetTimer:
2061.1Skochi *
2071.1Skochi *	Get the current system time in 100 nanosecond units
2081.1Skochi */
2091.1SkochiUINT64
2101.1SkochiAcpiOsGetTimer(void)
2111.1Skochi{
2121.1Skochi	struct timeval tv;
2131.1Skochi	UINT64 t;
2141.1Skochi
2151.1Skochi	/* XXX During early boot there is no (decent) timer available yet. */
2161.1Skochi	if (cold)
2171.1Skochi		panic("acpi: timer op not yet supported during boot");
2181.1Skochi
2191.1Skochi	microtime(&tv);
2201.1Skochi	t = (UINT64)10 * tv.tv_usec;
2211.1Skochi	t += (UINT64)10000000 * tv.tv_sec;
2221.1Skochi
2231.1Skochi	return (t);
2241.1Skochi}
225