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
29 * 6.3 : Scheduling services
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/interrupt.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/malloc.h>
44 #include <sys/taskqueue.h>
46 #include <contrib/dev/acpica/acpi.h>
47 #include <dev/acpica/acpivar.h>
49 #define _COMPONENT ACPI_OS_SERVICES
50 ACPI_MODULE_NAME("SCHEDULE")
53 * Allow the user to tune the number of task threads we start. It seems
54 * some systems have problems with increased parallelism.
56 static int acpi_max_threads = ACPI_MAX_THREADS;
57 TUNABLE_INT("debug.acpi.max_threads", &acpi_max_threads);
59 MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
61 struct acpi_task_ctx {
63 ACPI_OSD_EXEC_CALLBACK at_function;
67 TASKQUEUE_DEFINE(acpi, taskqueue_thread_enqueue, &taskqueue_acpi,
68 taskqueue_start_threads(&taskqueue_acpi, acpi_max_threads, PWAIT,
72 * Bounce through this wrapper function since ACPI-CA doesn't understand
73 * the pending argument for its callbacks.
76 acpi_task_execute(void *context, int pending)
78 struct acpi_task_ctx *at;
80 at = (struct acpi_task_ctx *)context;
81 at->at_function(at->at_context);
86 * This function may be called in interrupt context, i.e. when a GPE fires.
87 * We allocate and queue a task for one of our taskqueue threads to process.
90 AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
93 struct acpi_task_ctx *at;
96 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
99 return_ACPI_STATUS (AE_BAD_PARAMETER);
101 at = malloc(sizeof(*at), M_ACPITASK, M_NOWAIT);
103 return_ACPI_STATUS (AE_NO_MEMORY);
105 at->at_function = Function;
106 at->at_context = Context;
108 case OSL_GPE_HANDLER:
109 case OSL_NOTIFY_HANDLER:
111 * Run GPEs and Notifies at the same priority. This allows
112 * Notifies that are generated by running a GPE's method (e.g., _L00)
113 * to not be pre-empted by a later GPE that arrives during the
114 * Notify handler execution.
118 case OSL_GLOBAL_LOCK_HANDLER:
119 case OSL_EC_POLL_HANDLER:
120 case OSL_EC_BURST_HANDLER:
123 case OSL_DEBUGGER_THREAD:
127 free(at, M_ACPITASK);
128 return_ACPI_STATUS (AE_BAD_PARAMETER);
131 TASK_INIT(&at->at_task, pri, acpi_task_execute, at);
132 taskqueue_enqueue(taskqueue_acpi, &at->at_task);
134 return_ACPI_STATUS (AE_OK);
138 AcpiOsSleep(ACPI_INTEGER Milliseconds)
142 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
144 timo = Milliseconds * hz / 1000;
147 * If requested sleep time is less than our hz resolution, use
148 * DELAY instead for better granularity.
151 pause("acpislp", timo);
153 DELAY(Milliseconds * 1000);
159 * Return the current time in 100 nanosecond units
167 /* XXX During early boot there is no (decent) timer available yet. */
169 panic("acpi: timer op not yet supported during boot");
172 t = ((UINT64)10000000 * (uint32_t)(bt.frac >> 32)) >> 32;
173 t += bt.sec * 10000000;
179 AcpiOsStall(UINT32 Microseconds)
181 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
188 AcpiOsGetThreadId(void)
192 /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */
195 KASSERT(p != NULL, ("%s: curproc is NULL!", __func__));
197 /* Returning 0 is not allowed. */
198 return (p->p_pid + 1);