xref: /freebsd/sys/dev/acpica/Osd/OsdSchedule.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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$
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 
49 MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
50 
51 static void	AcpiOsExecuteQueue(void *arg, int pending);
52 
53 struct acpi_task {
54     struct task			at_task;
55     OSD_EXECUTION_CALLBACK	at_function;
56     void			*at_context;
57 };
58 
59 ACPI_STATUS
60 AcpiOsQueueForExecution(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     case OSD_PRIORITY_HIGH:
77 	at->at_task.ta_priority = 3;
78 	break;
79     case OSD_PRIORITY_MED:
80 	at->at_task.ta_priority = 2;
81 	break;
82     case OSD_PRIORITY_LO:
83 	at->at_task.ta_priority = 1;
84 	break;
85     default:
86 	free(at, M_ACPITASK);
87 	return(AE_BAD_PARAMETER);
88     }
89 
90     taskqueue_enqueue(taskqueue_swi, (struct task *)at);
91     return(AE_OK);
92 }
93 
94 static void
95 AcpiOsExecuteQueue(void *arg, int pending)
96 {
97     struct acpi_task		*at = (struct acpi_task *)arg;
98     OSD_EXECUTION_CALLBACK	Function;
99     void			*Context;
100 
101     Function = (OSD_EXECUTION_CALLBACK)at->at_function;
102     Context = at->at_context;
103 
104     free(at, M_ACPITASK);
105 
106     Function(Context);
107 }
108 
109 /*
110  * We don't have any sleep granularity better than hz, so
111  * make do with that.
112  */
113 void
114 AcpiOsSleep (UINT32 Seconds, UINT32 Milliseconds)
115 {
116     int		timo;
117 
118     timo = (Seconds * hz) + Milliseconds / (1000 * hz);
119     if (timo == 0)
120 	timo = 1;
121     tsleep(NULL, 0, "acpislp", timo);
122 }
123 
124 void
125 AcpiOsSleepUsec (UINT32 Microseconds)
126 {
127     if (Microseconds > 1000) {	/* long enough to be worth the overhead of sleeping */
128 	AcpiOsSleep(0, Microseconds / 1000);
129     } else {
130 	DELAY(Microseconds);
131     }
132 }
133