2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/sys/dev/acpica/Osd/OsdSchedule.c,v 1.26 2003/10/02 05:09:37 njl Exp $
28 * $DragonFly: src/sys/dev/acpica5/Osd/OsdSchedule.c,v 1.2 2004/05/05 22:18:10 dillon Exp $
32 * 6.3 : Scheduling services
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/interrupt.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
44 #include <sys/malloc.h>
46 #include <sys/taskqueue.h>
47 #include <machine/clock.h>
51 #include <dev/acpica5/acpivar.h>
53 #define _COMPONENT ACPI_OS_SERVICES
54 ACPI_MODULE_NAME("SCHEDULE")
57 * This is a little complicated due to the fact that we need to build and then
58 * free a 'struct task' for each task we enqueue.
61 MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
63 static void AcpiOsExecuteQueue(void *arg, int pending);
67 OSD_EXECUTION_CALLBACK at_function;
71 struct acpi_task_queue {
72 STAILQ_ENTRY(acpi_task_queue) at_q;
76 #if __FreeBSD_version >= 500000
78 * Private task queue definition for ACPI
80 TASKQUEUE_DECLARE(acpi);
81 static void *taskqueue_acpi_ih;
84 taskqueue_acpi_enqueue(void *context)
86 swi_sched(taskqueue_acpi_ih, 0);
90 taskqueue_acpi_run(void *dummy)
92 taskqueue_run(taskqueue_acpi);
95 TASKQUEUE_DEFINE(acpi, taskqueue_acpi_enqueue, 0,
96 swi_add(NULL, "acpitaskq", taskqueue_acpi_run, NULL,
97 SWI_TQ, 0, &taskqueue_acpi_ih));
99 #ifdef ACPI_USE_THREADS
100 STAILQ_HEAD(, acpi_task_queue) acpi_task_queue;
101 static struct mtx acpi_task_mtx;
104 acpi_task_thread(void *arg)
106 struct acpi_task_queue *atq;
107 OSD_EXECUTION_CALLBACK Function;
111 mtx_lock(&acpi_task_mtx);
112 if ((atq = STAILQ_FIRST(&acpi_task_queue)) == NULL) {
113 msleep(&acpi_task_queue, &acpi_task_mtx, PCATCH, "actask", 0);
114 mtx_unlock(&acpi_task_mtx);
118 STAILQ_REMOVE_HEAD(&acpi_task_queue, at_q);
119 mtx_unlock(&acpi_task_mtx);
121 Function = (OSD_EXECUTION_CALLBACK)atq->at->at_function;
122 Context = atq->at->at_context;
127 free(atq->at, M_ACPITASK);
128 free(atq, M_ACPITASK);
136 acpi_task_thread_init(void)
139 struct proc *acpi_kthread_proc;
142 STAILQ_INIT(&acpi_task_queue);
143 mtx_init(&acpi_task_mtx, "ACPI task", NULL, MTX_DEF);
145 for (i = 0; i < ACPI_MAX_THREADS; i++) {
146 err = kthread_create(acpi_task_thread, NULL, &acpi_kthread_proc,
147 0, 0, "acpi_task%d", i);
149 printf("%s: kthread_create failed(%d)\n", __func__, err);
159 AcpiOsQueueForExecution(UINT32 Priority, OSD_EXECUTION_CALLBACK Function, void *Context)
161 struct acpi_task *at;
164 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
166 if (Function == NULL)
167 return_ACPI_STATUS(AE_BAD_PARAMETER);
169 /* Note: Interrupt Context */
170 at = malloc(sizeof(*at), M_ACPITASK, M_INTWAIT | M_ZERO);
171 at->at_function = Function;
172 at->at_context = Context;
174 case OSD_PRIORITY_GPE:
177 case OSD_PRIORITY_HIGH:
180 case OSD_PRIORITY_MED:
183 case OSD_PRIORITY_LO:
187 free(at, M_ACPITASK);
188 return_ACPI_STATUS(AE_BAD_PARAMETER);
190 TASK_INIT(&at->at_task, pri, AcpiOsExecuteQueue, at);
192 #if __FreeBSD_version < 500000
193 taskqueue_enqueue(taskqueue_swi, (struct task *)at);
195 taskqueue_enqueue(taskqueue_acpi, (struct task *)at);
197 return_ACPI_STATUS(AE_OK);
201 AcpiOsExecuteQueue(void *arg, int pending)
203 struct acpi_task *at;
204 struct acpi_task_queue *atq;
205 OSD_EXECUTION_CALLBACK Function;
208 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
210 at = (struct acpi_task *)arg;
215 #ifdef ACPI_USE_THREADS
216 atq = malloc(sizeof(*atq), M_ACPITASK, M_INTWAIT);
219 mtx_lock(&acpi_task_mtx);
220 STAILQ_INSERT_TAIL(&acpi_task_queue, atq, at_q);
221 mtx_unlock(&acpi_task_mtx);
222 wakeup_one(&acpi_task_queue);
224 Function = (OSD_EXECUTION_CALLBACK)at->at_function;
225 Context = at->at_context;
228 free(at, M_ACPITASK);
235 * We don't have any sleep granularity better than hz, so
239 AcpiOsSleep(UINT32 Seconds, UINT32 Milliseconds)
244 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
246 timo = (Seconds * hz) + Milliseconds * hz / 1000;
249 * If requested sleep time is less than our hz resolution, use
250 * DELAY instead for better granularity.
253 tsleep(&dummy, 0, "acpislp", timo);
255 DELAY(Milliseconds * 1000);
261 AcpiOsStall(UINT32 Microseconds)
263 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
270 AcpiOsGetThreadId(void)
273 /* XXX do not add FUNCTION_TRACE here, results in recursive call */
276 #if __FreeBSD_version < 500000
280 KASSERT(p != NULL, ("%s: curproc is NULL!", __func__));
281 return(p->p_pid + 1); /* can't return 0 */