OsdSchedule.c revision 85503
1/*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *	$FreeBSD: head/sys/dev/acpica/Osd/OsdSchedule.c 85503 2001-10-25 19:56:02Z jhb $
28 */
29
30/*
31 * 6.3 : Scheduling services
32 */
33
34#include "acpi.h"
35
36#include <sys/kernel.h>
37#include <sys/malloc.h>
38#include <sys/proc.h>
39#include <sys/taskqueue.h>
40#include <machine/clock.h>
41
42#define _COMPONENT	ACPI_OS_SERVICES
43MODULE_NAME("SCHEDULE")
44
45/*
46 * This is a little complicated due to the fact that we need to build and then
47 * free a 'struct task' for each task we enqueue.
48 *
49 * We use the default taskqueue_swi queue, since it really doesn't matter what
50 * else we're queued along with.
51 */
52
53MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
54
55static void	AcpiOsExecuteQueue(void *arg, int pending);
56
57struct acpi_task {
58    struct task			at_task;
59    OSD_EXECUTION_CALLBACK	at_function;
60    void			*at_context;
61};
62
63ACPI_STATUS
64AcpiOsQueueForExecution(UINT32 Priority, OSD_EXECUTION_CALLBACK Function, void *Context)
65{
66    struct acpi_task	*at;
67    int pri;
68
69    FUNCTION_TRACE(__func__);
70
71    if (Function == NULL)
72	return_ACPI_STATUS(AE_BAD_PARAMETER);
73
74    at = malloc(sizeof(*at), M_ACPITASK, M_NOWAIT);	/* Interrupt Context */
75    if (at == NULL)
76	return_ACPI_STATUS(AE_NO_MEMORY);
77    bzero(at, sizeof(*at));
78
79    at->at_function = Function;
80    at->at_context = Context;
81    switch (Priority) {
82    case OSD_PRIORITY_GPE:
83	pri = 4;
84	break;
85    case OSD_PRIORITY_HIGH:
86	pri = 3;
87	break;
88    case OSD_PRIORITY_MED:
89	pri = 2;
90	break;
91    case OSD_PRIORITY_LO:
92	pri = 1;
93	break;
94    default:
95	free(at, M_ACPITASK);
96	return_ACPI_STATUS(AE_BAD_PARAMETER);
97    }
98    TASK_INIT(&at->at_task, pri, AcpiOsExecuteQueue, at);
99
100    taskqueue_enqueue(taskqueue_swi, (struct task *)at);
101    return_ACPI_STATUS(AE_OK);
102}
103
104static void
105AcpiOsExecuteQueue(void *arg, int pending)
106{
107    struct acpi_task		*at = (struct acpi_task *)arg;
108    OSD_EXECUTION_CALLBACK	Function;
109    void			*Context;
110
111    FUNCTION_TRACE(__func__);
112
113    Function = (OSD_EXECUTION_CALLBACK)at->at_function;
114    Context = at->at_context;
115
116    free(at, M_ACPITASK);
117
118    Function(Context);
119    return_VOID;
120}
121
122/*
123 * We don't have any sleep granularity better than hz, so
124 * make do with that.
125 */
126void
127AcpiOsSleep (UINT32 Seconds, UINT32 Milliseconds)
128{
129    int		timo;
130    static int	dummy;
131
132    FUNCTION_TRACE(__func__);
133
134    timo = (Seconds * hz) + Milliseconds * hz / 1000;
135    if (timo == 0)
136	timo = 1;
137    tsleep(&dummy, 0, "acpislp", timo);
138    return_VOID;
139}
140
141void
142AcpiOsStall (UINT32 Microseconds)
143{
144    FUNCTION_TRACE(__func__);
145
146    DELAY(Microseconds);
147    return_VOID;
148}
149
150UINT32
151AcpiOsGetThreadId (void)
152{
153    /* XXX do not add FUNCTION_TRACE here, results in recursive call */
154
155    KASSERT(curproc != NULL, (__func__ ": curproc is NULL!"));
156    return(curproc->p_pid + 1);	/* can't return 0 */
157}
158