OsdSchedule.c revision 246042
1/*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * Copyright (c) 2007-2012 Jung-uk Kim <jkim@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * 6.3 : Scheduling services
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/acpica/Osd/OsdSchedule.c 246042 2013-01-28 21:10:35Z jkim $");
35
36#include "opt_acpi.h"
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/bus.h>
40#include <sys/interrupt.h>
41#include <sys/kernel.h>
42#include <sys/kthread.h>
43#include <sys/malloc.h>
44#include <sys/proc.h>
45#include <sys/taskqueue.h>
46
47#include <contrib/dev/acpica/include/acpi.h>
48#include <contrib/dev/acpica/include/accommon.h>
49
50#include <dev/acpica/acpivar.h>
51
52#define _COMPONENT	ACPI_OS_SERVICES
53ACPI_MODULE_NAME("SCHEDULE")
54
55/*
56 * Allow the user to tune the maximum number of tasks we may enqueue.
57 */
58static int acpi_max_tasks = ACPI_MAX_TASKS;
59TUNABLE_INT("debug.acpi.max_tasks", &acpi_max_tasks);
60
61/*
62 * Allow the user to tune the number of task threads we start.  It seems
63 * some systems have problems with increased parallelism.
64 */
65static int acpi_max_threads = ACPI_MAX_THREADS;
66TUNABLE_INT("debug.acpi.max_threads", &acpi_max_threads);
67
68static MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
69
70struct acpi_task_ctx {
71    struct task			at_task;
72    ACPI_OSD_EXEC_CALLBACK	at_function;
73    void 			*at_context;
74    int				at_flag;
75#define	ACPI_TASK_FREE		0
76#define	ACPI_TASK_USED		1
77#define	ACPI_TASK_ENQUEUED	2
78};
79
80struct taskqueue		*acpi_taskq;
81static struct acpi_task_ctx	*acpi_tasks;
82static int			acpi_task_count;
83static int			acpi_taskq_started;
84
85/*
86 * Preallocate some memory for tasks early enough.
87 * malloc(9) cannot be used with spin lock held.
88 */
89static void
90acpi_task_init(void *arg)
91{
92
93    acpi_tasks = malloc(sizeof(*acpi_tasks) * acpi_max_tasks, M_ACPITASK,
94	M_WAITOK | M_ZERO);
95}
96
97SYSINIT(acpi_tasks, SI_SUB_DRIVERS, SI_ORDER_FIRST, acpi_task_init, NULL);
98
99/*
100 * Initialize ACPI task queue.
101 */
102static void
103acpi_taskq_init(void *arg)
104{
105    int i;
106
107    acpi_taskq = taskqueue_create_fast("acpi_task", M_NOWAIT,
108	&taskqueue_thread_enqueue, &acpi_taskq);
109    taskqueue_start_threads(&acpi_taskq, acpi_max_threads, PWAIT, "acpi_task");
110    if (acpi_task_count > 0) {
111	if (bootverbose)
112	    printf("AcpiOsExecute: enqueue %d pending tasks\n",
113		acpi_task_count);
114	for (i = 0; i < acpi_max_tasks; i++)
115	    if (atomic_cmpset_int(&acpi_tasks[i].at_flag, ACPI_TASK_USED,
116		ACPI_TASK_USED | ACPI_TASK_ENQUEUED))
117		taskqueue_enqueue(acpi_taskq, &acpi_tasks[i].at_task);
118    }
119    acpi_taskq_started = 1;
120}
121
122SYSINIT(acpi_taskq, SI_SUB_CONFIGURE, SI_ORDER_SECOND, acpi_taskq_init, NULL);
123
124/*
125 * Bounce through this wrapper function since ACPI-CA doesn't understand
126 * the pending argument for its callbacks.
127 */
128static void
129acpi_task_execute(void *context, int pending)
130{
131    struct acpi_task_ctx *at;
132
133    at = (struct acpi_task_ctx *)context;
134    at->at_function(at->at_context);
135    atomic_clear_int(&at->at_flag, ACPI_TASK_USED | ACPI_TASK_ENQUEUED);
136    acpi_task_count--;
137}
138
139static ACPI_STATUS
140acpi_task_enqueue(int priority, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
141{
142    struct acpi_task_ctx *at;
143    int i;
144
145    for (at = NULL, i = 0; i < acpi_max_tasks; i++)
146	if (atomic_cmpset_int(&acpi_tasks[i].at_flag, ACPI_TASK_FREE,
147	    ACPI_TASK_USED)) {
148	    at = &acpi_tasks[i];
149	    acpi_task_count++;
150	    break;
151	}
152    if (at == NULL) {
153	printf("AcpiOsExecute: failed to enqueue task, consider increasing "
154	    "the debug.acpi.max_tasks tunable\n");
155	return (AE_NO_MEMORY);
156    }
157
158    TASK_INIT(&at->at_task, priority, acpi_task_execute, at);
159    at->at_function = Function;
160    at->at_context = Context;
161
162    /*
163     * If the task queue is ready, enqueue it now.
164     */
165    if (acpi_taskq_started) {
166	atomic_set_int(&at->at_flag, ACPI_TASK_ENQUEUED);
167	taskqueue_enqueue(acpi_taskq, &at->at_task);
168	return (AE_OK);
169    }
170    if (bootverbose)
171	printf("AcpiOsExecute: task queue not started\n");
172
173    return (AE_OK);
174}
175
176/*
177 * This function may be called in interrupt context, i.e. when a GPE fires.
178 * We allocate and queue a task for one of our taskqueue threads to process.
179 */
180ACPI_STATUS
181AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
182    void *Context)
183{
184    ACPI_STATUS status;
185    int pri;
186
187    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
188
189    if (Function == NULL)
190	return_ACPI_STATUS(AE_BAD_PARAMETER);
191
192    switch (Type) {
193    case OSL_GPE_HANDLER:
194    case OSL_NOTIFY_HANDLER:
195	/*
196	 * Run GPEs and Notifies at the same priority.  This allows
197	 * Notifies that are generated by running a GPE's method (e.g., _L00)
198	 * to not be pre-empted by a later GPE that arrives during the
199	 * Notify handler execution.
200	 */
201	pri = 10;
202	break;
203    case OSL_GLOBAL_LOCK_HANDLER:
204    case OSL_EC_POLL_HANDLER:
205    case OSL_EC_BURST_HANDLER:
206	pri = 5;
207	break;
208    case OSL_DEBUGGER_THREAD:
209	pri = 0;
210	break;
211    default:
212	return_ACPI_STATUS(AE_BAD_PARAMETER);
213    }
214
215    status = acpi_task_enqueue(pri, Function, Context);
216    return_ACPI_STATUS(status);
217}
218
219void
220AcpiOsWaitEventsComplete(void)
221{
222	int i;
223
224	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
225
226	for (i = 0; i < acpi_max_tasks; i++)
227		if ((atomic_load_acq_int(&acpi_tasks[i].at_flag) &
228		    ACPI_TASK_ENQUEUED) != 0)
229			taskqueue_drain(acpi_taskq, &acpi_tasks[i].at_task);
230	return_VOID;
231}
232
233void
234AcpiOsSleep(UINT64 Milliseconds)
235{
236    int		timo;
237
238    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
239
240    timo = Milliseconds * hz / 1000;
241
242    /*
243     * If requested sleep time is less than our hz resolution, use
244     * DELAY instead for better granularity.
245     */
246    if (timo > 0)
247	pause("acpislp", timo);
248    else
249	DELAY(Milliseconds * 1000);
250
251    return_VOID;
252}
253
254/*
255 * Return the current time in 100 nanosecond units
256 */
257UINT64
258AcpiOsGetTimer(void)
259{
260    struct bintime bt;
261    UINT64 t;
262
263    /* XXX During early boot there is no (decent) timer available yet. */
264    KASSERT(cold == 0, ("acpi: timer op not yet supported during boot"));
265
266    binuptime(&bt);
267    t = (uint64_t)bt.sec * 10000000;
268    t += ((uint64_t)10000000 * (uint32_t)(bt.frac >> 32)) >> 32;
269
270    return (t);
271}
272
273void
274AcpiOsStall(UINT32 Microseconds)
275{
276    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
277
278    DELAY(Microseconds);
279    return_VOID;
280}
281
282ACPI_THREAD_ID
283AcpiOsGetThreadId(void)
284{
285
286    /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */
287
288    /* Returning 0 is not allowed. */
289    return (curthread->td_tid);
290}
291