OsdSchedule.c revision 1.1
11.1Skochi/*	$NetBSD: OsdSchedule.c,v 1.1 2006/03/23 13:41:13 kochi 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.1Skochi__KERNEL_RCSID(0, "$NetBSD: OsdSchedule.c,v 1.1 2006/03/23 13:41:13 kochi 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.1Skochi	ACPI_FUNCTION_TRACE(__FUNCTION__);
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.1Skochi	ACPI_FUNCTION_TRACE(__FUNCTION__);
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.1SkochiUINT32
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.1SkochiAcpiOsQueueForExecution(UINT32 Priority, ACPI_OSD_EXEC_CALLBACK Function,
1181.1Skochi    void *Context)
1191.1Skochi{
1201.1Skochi	int pri;
1211.1Skochi
1221.1Skochi	ACPI_FUNCTION_TRACE(__FUNCTION__);
1231.1Skochi
1241.1Skochi	switch (Priority) {
1251.1Skochi	case OSD_PRIORITY_GPE:
1261.1Skochi		pri = 3;
1271.1Skochi		break;
1281.1Skochi
1291.1Skochi	case OSD_PRIORITY_HIGH:
1301.1Skochi		pri = 2;
1311.1Skochi		break;
1321.1Skochi
1331.1Skochi	case OSD_PRIORITY_MED:
1341.1Skochi		pri = 1;
1351.1Skochi		break;
1361.1Skochi
1371.1Skochi	case OSD_PRIORITY_LO:
1381.1Skochi		pri = 0;
1391.1Skochi		break;
1401.1Skochi
1411.1Skochi	default:
1421.1Skochi		return_ACPI_STATUS(AE_BAD_PARAMETER);
1431.1Skochi	}
1441.1Skochi
1451.1Skochi	switch (sysmon_task_queue_sched(pri, Function, Context)) {
1461.1Skochi	case 0:
1471.1Skochi		return_ACPI_STATUS(AE_OK);
1481.1Skochi
1491.1Skochi	case ENOMEM:
1501.1Skochi		return_ACPI_STATUS(AE_NO_MEMORY);
1511.1Skochi
1521.1Skochi	default:
1531.1Skochi		return_ACPI_STATUS(AE_BAD_PARAMETER);
1541.1Skochi	}
1551.1Skochi}
1561.1Skochi
1571.1Skochi/*
1581.1Skochi * AcpiOsSleep:
1591.1Skochi *
1601.1Skochi *	Suspend the running task (coarse granularity).
1611.1Skochi */
1621.1Skochivoid
1631.1SkochiAcpiOsSleep(ACPI_INTEGER Milliseconds)
1641.1Skochi{
1651.1Skochi	int timo;
1661.1Skochi
1671.1Skochi	ACPI_FUNCTION_TRACE(__FUNCTION__);
1681.1Skochi
1691.1Skochi	timo = Milliseconds * hz / 1000;
1701.1Skochi	if (timo == 0)
1711.1Skochi		timo = 1;
1721.1Skochi
1731.1Skochi	(void) tsleep(&timo, PVM, "acpislp", timo);
1741.1Skochi
1751.1Skochi	return_VOID;
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.1Skochi	ACPI_FUNCTION_TRACE(__FUNCTION__);
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.1Skochi * AcpiOsStall:
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