OsdSchedule.c revision 70237
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 70237 2000-12-20 20:22:47Z iwasaki $
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/taskqueue.h>
39#include <machine/clock.h>
40
41/*
42 * This is a little complicated due to the fact that we need to build and then
43 * free a 'struct task' for each task we enqueue.
44 *
45 * We use the default taskqueue_swi queue, since it really doesn't matter what
46 * else we're queued along with.
47 */
48
49MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
50
51static void	AcpiOsExecuteQueue(void *arg, int pending);
52
53struct acpi_task {
54    struct task			at_task;
55    OSD_EXECUTION_CALLBACK	at_function;
56    void			*at_context;
57};
58
59ACPI_STATUS
60AcpiOsQueueForExecution(UINT32 Priority, OSD_EXECUTION_CALLBACK Function, void *Context)
61{
62    struct acpi_task	*at;
63
64    if (Function == NULL)
65	return(AE_BAD_PARAMETER);
66
67    /* XXX is it OK to block here? */
68    at = malloc(sizeof(*at), M_ACPITASK, M_WAITOK);
69    bzero(at, sizeof(*at));
70
71    at->at_function = Function;
72    at->at_context = Context;
73    at->at_task.ta_func = AcpiOsExecuteQueue;
74    at->at_task.ta_context = at;
75    switch (Priority) {
76#if 0
77    case OSD_PRIORITY_GPE:
78	at->at_task.ta_priority = 4;
79	break;
80#endif
81    case OSD_PRIORITY_HIGH:
82	at->at_task.ta_priority = 3;
83	break;
84    case OSD_PRIORITY_MED:
85	at->at_task.ta_priority = 2;
86	break;
87    case OSD_PRIORITY_LO:
88	at->at_task.ta_priority = 1;
89	break;
90    default:
91	free(at, M_ACPITASK);
92	return(AE_BAD_PARAMETER);
93    }
94
95    taskqueue_enqueue(taskqueue_swi, (struct task *)at);
96    return(AE_OK);
97}
98
99static void
100AcpiOsExecuteQueue(void *arg, int pending)
101{
102    struct acpi_task		*at = (struct acpi_task *)arg;
103    OSD_EXECUTION_CALLBACK	Function;
104    void			*Context;
105
106    Function = (OSD_EXECUTION_CALLBACK)at->at_function;
107    Context = at->at_context;
108
109    free(at, M_ACPITASK);
110
111    Function(Context);
112}
113
114/*
115 * We don't have any sleep granularity better than hz, so
116 * make do with that.
117 */
118void
119AcpiOsSleep (UINT32 Seconds, UINT32 Milliseconds)
120{
121    int		timo;
122
123    timo = (Seconds * hz) + Milliseconds / (1000 * hz);
124    if (timo == 0)
125	timo = 1;
126    tsleep(NULL, 0, "acpislp", timo);
127}
128
129void
130AcpiOsSleepUsec (UINT32 Microseconds)
131{
132    if (Microseconds > 1000) {	/* long enough to be worth the overhead of sleeping */
133	AcpiOsSleep(0, Microseconds / 1000);
134    } else {
135	DELAY(Microseconds);
136    }
137}
138