OsdSchedule.c revision 267961
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 267961 2014-06-27 16:33:43Z hselasky $");
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;
59SYSCTL_INT(_debug_acpi, OID_AUTO, max_tasks, CTLFLAG_RDTUN, &acpi_max_tasks,
60    0, "Maximum acpi tasks");
61
62/*
63 * Allow the user to tune the number of task threads we start.  It seems
64 * some systems have problems with increased parallelism.
65 */
66static int acpi_max_threads = ACPI_MAX_THREADS;
67SYSCTL_INT(_debug_acpi, OID_AUTO, max_threads, CTLFLAG_RDTUN, &acpi_max_threads,
68    0, "Maximum acpi threads");
69
70static MALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task");
71
72struct acpi_task_ctx {
73    struct task			at_task;
74    ACPI_OSD_EXEC_CALLBACK	at_function;
75    void 			*at_context;
76    int				at_flag;
77#define	ACPI_TASK_FREE		0
78#define	ACPI_TASK_USED		1
79#define	ACPI_TASK_ENQUEUED	2
80};
81
82struct taskqueue		*acpi_taskq;
83static struct acpi_task_ctx	*acpi_tasks;
84static int			acpi_task_count;
85static int			acpi_taskq_started;
86
87/*
88 * Preallocate some memory for tasks early enough.
89 * malloc(9) cannot be used with spin lock held.
90 */
91static void
92acpi_task_init(void *arg)
93{
94
95    acpi_tasks = malloc(sizeof(*acpi_tasks) * acpi_max_tasks, M_ACPITASK,
96	M_WAITOK | M_ZERO);
97}
98
99SYSINIT(acpi_tasks, SI_SUB_DRIVERS, SI_ORDER_FIRST, acpi_task_init, NULL);
100
101/*
102 * Initialize ACPI task queue.
103 */
104static void
105acpi_taskq_init(void *arg)
106{
107    int i;
108
109    acpi_taskq = taskqueue_create_fast("acpi_task", M_NOWAIT,
110	&taskqueue_thread_enqueue, &acpi_taskq);
111    taskqueue_start_threads(&acpi_taskq, acpi_max_threads, PWAIT, "acpi_task");
112    if (acpi_task_count > 0) {
113	if (bootverbose)
114	    printf("AcpiOsExecute: enqueue %d pending tasks\n",
115		acpi_task_count);
116	for (i = 0; i < acpi_max_tasks; i++)
117	    if (atomic_cmpset_int(&acpi_tasks[i].at_flag, ACPI_TASK_USED,
118		ACPI_TASK_USED | ACPI_TASK_ENQUEUED))
119		taskqueue_enqueue(acpi_taskq, &acpi_tasks[i].at_task);
120    }
121    acpi_taskq_started = 1;
122}
123
124SYSINIT(acpi_taskq, SI_SUB_CONFIGURE, SI_ORDER_SECOND, acpi_taskq_init, NULL);
125
126/*
127 * Bounce through this wrapper function since ACPI-CA doesn't understand
128 * the pending argument for its callbacks.
129 */
130static void
131acpi_task_execute(void *context, int pending)
132{
133    struct acpi_task_ctx *at;
134
135    at = (struct acpi_task_ctx *)context;
136    at->at_function(at->at_context);
137    atomic_clear_int(&at->at_flag, ACPI_TASK_USED | ACPI_TASK_ENQUEUED);
138    acpi_task_count--;
139}
140
141static ACPI_STATUS
142acpi_task_enqueue(int priority, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
143{
144    struct acpi_task_ctx *at;
145    int i;
146
147    for (at = NULL, i = 0; i < acpi_max_tasks; i++)
148	if (atomic_cmpset_int(&acpi_tasks[i].at_flag, ACPI_TASK_FREE,
149	    ACPI_TASK_USED)) {
150	    at = &acpi_tasks[i];
151	    acpi_task_count++;
152	    break;
153	}
154    if (at == NULL) {
155	printf("AcpiOsExecute: failed to enqueue task, consider increasing "
156	    "the debug.acpi.max_tasks tunable\n");
157	return (AE_NO_MEMORY);
158    }
159
160    TASK_INIT(&at->at_task, priority, acpi_task_execute, at);
161    at->at_function = Function;
162    at->at_context = Context;
163
164    /*
165     * If the task queue is ready, enqueue it now.
166     */
167    if (acpi_taskq_started) {
168	atomic_set_int(&at->at_flag, ACPI_TASK_ENQUEUED);
169	taskqueue_enqueue(acpi_taskq, &at->at_task);
170	return (AE_OK);
171    }
172    if (bootverbose)
173	printf("AcpiOsExecute: task queue not started\n");
174
175    return (AE_OK);
176}
177
178/*
179 * This function may be called in interrupt context, i.e. when a GPE fires.
180 * We allocate and queue a task for one of our taskqueue threads to process.
181 */
182ACPI_STATUS
183AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
184    void *Context)
185{
186    ACPI_STATUS status;
187    int pri;
188
189    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
190
191    if (Function == NULL)
192	return_ACPI_STATUS(AE_BAD_PARAMETER);
193
194    switch (Type) {
195    case OSL_GPE_HANDLER:
196    case OSL_NOTIFY_HANDLER:
197	/*
198	 * Run GPEs and Notifies at the same priority.  This allows
199	 * Notifies that are generated by running a GPE's method (e.g., _L00)
200	 * to not be pre-empted by a later GPE that arrives during the
201	 * Notify handler execution.
202	 */
203	pri = 10;
204	break;
205    case OSL_GLOBAL_LOCK_HANDLER:
206    case OSL_EC_POLL_HANDLER:
207    case OSL_EC_BURST_HANDLER:
208	pri = 5;
209	break;
210    case OSL_DEBUGGER_THREAD:
211	pri = 0;
212	break;
213    default:
214	return_ACPI_STATUS(AE_BAD_PARAMETER);
215    }
216
217    status = acpi_task_enqueue(pri, Function, Context);
218    return_ACPI_STATUS(status);
219}
220
221void
222AcpiOsWaitEventsComplete(void)
223{
224	int i;
225
226	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
227
228	for (i = 0; i < acpi_max_tasks; i++)
229		if ((atomic_load_acq_int(&acpi_tasks[i].at_flag) &
230		    ACPI_TASK_ENQUEUED) != 0)
231			taskqueue_drain(acpi_taskq, &acpi_tasks[i].at_task);
232	return_VOID;
233}
234
235void
236AcpiOsSleep(UINT64 Milliseconds)
237{
238    int		timo;
239
240    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
241
242    timo = Milliseconds * hz / 1000;
243
244    /*
245     * If requested sleep time is less than our hz resolution, use
246     * DELAY instead for better granularity.
247     */
248    if (timo > 0)
249	pause("acpislp", timo);
250    else
251	DELAY(Milliseconds * 1000);
252
253    return_VOID;
254}
255
256/*
257 * Return the current time in 100 nanosecond units
258 */
259UINT64
260AcpiOsGetTimer(void)
261{
262    struct bintime bt;
263    UINT64 t;
264
265    /* XXX During early boot there is no (decent) timer available yet. */
266    KASSERT(cold == 0, ("acpi: timer op not yet supported during boot"));
267
268    binuptime(&bt);
269    t = (uint64_t)bt.sec * 10000000;
270    t += ((uint64_t)10000000 * (uint32_t)(bt.frac >> 32)) >> 32;
271
272    return (t);
273}
274
275void
276AcpiOsStall(UINT32 Microseconds)
277{
278    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
279
280    DELAY(Microseconds);
281    return_VOID;
282}
283
284ACPI_THREAD_ID
285AcpiOsGetThreadId(void)
286{
287
288    /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */
289
290    /* Returning 0 is not allowed. */
291    return (curthread->td_tid);
292}
293