OsdSchedule.c revision 133627
167760Smsmith/*- 267760Smsmith * Copyright (c) 2000 Michael Smith 367760Smsmith * Copyright (c) 2000 BSDi 467760Smsmith * All rights reserved. 567760Smsmith * 667760Smsmith * Redistribution and use in source and binary forms, with or without 767760Smsmith * modification, are permitted provided that the following conditions 867760Smsmith * are met: 967760Smsmith * 1. Redistributions of source code must retain the above copyright 1067760Smsmith * notice, this list of conditions and the following disclaimer. 1167760Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1267760Smsmith * notice, this list of conditions and the following disclaimer in the 1367760Smsmith * documentation and/or other materials provided with the distribution. 1467760Smsmith * 1567760Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1667760Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1767760Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1867760Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1967760Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2067760Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2167760Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2267760Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2367760Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2467760Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2567760Smsmith * SUCH DAMAGE. 2667760Smsmith * 2767760Smsmith * $FreeBSD: head/sys/dev/acpica/Osd/OsdSchedule.c 133627 2004-08-13 06:22:26Z njl $ 2867760Smsmith */ 2967760Smsmith 3067760Smsmith/* 3167760Smsmith * 6.3 : Scheduling services 3267760Smsmith */ 3367760Smsmith 3488420Siwasaki#include "opt_acpi.h" 3588420Siwasaki#include <sys/param.h> 3688420Siwasaki#include <sys/systm.h> 3788420Siwasaki#include <sys/bus.h> 3888420Siwasaki#include <sys/interrupt.h> 3967760Smsmith#include <sys/kernel.h> 4088420Siwasaki#include <sys/kthread.h> 4167760Smsmith#include <sys/malloc.h> 4277466Smsmith#include <sys/proc.h> 4367760Smsmith#include <sys/taskqueue.h> 4467760Smsmith#include <machine/clock.h> 4567760Smsmith 46128228Snjl#include "acpi.h" 4788420Siwasaki#include <dev/acpica/acpivar.h> 4888420Siwasaki 4977432Smsmith#define _COMPONENT ACPI_OS_SERVICES 5091128SmsmithACPI_MODULE_NAME("SCHEDULE") 5171875Smsmith 5267760Smsmith/* 5367760Smsmith * This is a little complicated due to the fact that we need to build and then 5467760Smsmith * free a 'struct task' for each task we enqueue. 5567760Smsmith */ 5667760Smsmith 5767760SmsmithMALLOC_DEFINE(M_ACPITASK, "acpitask", "ACPI deferred task"); 5867760Smsmith 5967760Smsmithstatic void AcpiOsExecuteQueue(void *arg, int pending); 6067760Smsmith 6167760Smsmithstruct acpi_task { 6267760Smsmith struct task at_task; 6367760Smsmith OSD_EXECUTION_CALLBACK at_function; 6467760Smsmith void *at_context; 6567760Smsmith}; 6667760Smsmith 6788420Siwasakistruct acpi_task_queue { 6888420Siwasaki STAILQ_ENTRY(acpi_task_queue) at_q; 6988420Siwasaki struct acpi_task *at; 7088420Siwasaki}; 7188420Siwasaki 7288420Siwasaki/* 7388420Siwasaki * Private task queue definition for ACPI 7488420Siwasaki */ 7588420SiwasakiTASKQUEUE_DECLARE(acpi); 7688420Siwasakistatic void *taskqueue_acpi_ih; 7788420Siwasaki 7888420Siwasakistatic void 7988420Siwasakitaskqueue_acpi_enqueue(void *context) 8088420Siwasaki{ 8188900Sjhb swi_sched(taskqueue_acpi_ih, 0); 8288420Siwasaki} 8388420Siwasaki 8488420Siwasakistatic void 8588420Siwasakitaskqueue_acpi_run(void *dummy) 8688420Siwasaki{ 8788420Siwasaki taskqueue_run(taskqueue_acpi); 8888420Siwasaki} 8988420Siwasaki 9088420SiwasakiTASKQUEUE_DEFINE(acpi, taskqueue_acpi_enqueue, 0, 9188420Siwasaki swi_add(NULL, "acpitaskq", taskqueue_acpi_run, NULL, 9288420Siwasaki SWI_TQ, 0, &taskqueue_acpi_ih)); 9388420Siwasaki 94128990Snjlstatic STAILQ_HEAD(, acpi_task_queue) acpi_task_queue; 95133627SnjlACPI_LOCK_DECL(taskq, "ACPI task queue"); 9688420Siwasaki 9788420Siwasakistatic void 9888420Siwasakiacpi_task_thread(void *arg) 9988420Siwasaki{ 10088420Siwasaki struct acpi_task_queue *atq; 10188420Siwasaki OSD_EXECUTION_CALLBACK Function; 10288420Siwasaki void *Context; 10388420Siwasaki 104133627Snjl ACPI_LOCK(taskq); 10588420Siwasaki for (;;) { 106133627Snjl while ((atq = STAILQ_FIRST(&acpi_task_queue)) == NULL) 107133627Snjl msleep(&acpi_task_queue, &taskq_mutex, PCATCH, "actask", 0); 10888420Siwasaki STAILQ_REMOVE_HEAD(&acpi_task_queue, at_q); 109133627Snjl ACPI_UNLOCK(taskq); 11088420Siwasaki 11188420Siwasaki Function = (OSD_EXECUTION_CALLBACK)atq->at->at_function; 11288420Siwasaki Context = atq->at->at_context; 11388420Siwasaki 11488420Siwasaki Function(Context); 11588420Siwasaki 11688420Siwasaki free(atq->at, M_ACPITASK); 11788420Siwasaki free(atq, M_ACPITASK); 118133627Snjl ACPI_LOCK(taskq); 11988420Siwasaki } 12088420Siwasaki 12188420Siwasaki kthread_exit(0); 12288420Siwasaki} 12388420Siwasaki 12488420Siwasakiint 12588420Siwasakiacpi_task_thread_init(void) 12688420Siwasaki{ 12788420Siwasaki int i, err; 12888420Siwasaki struct proc *acpi_kthread_proc; 12988420Siwasaki 13088420Siwasaki err = 0; 13188420Siwasaki STAILQ_INIT(&acpi_task_queue); 13288420Siwasaki 13388420Siwasaki for (i = 0; i < ACPI_MAX_THREADS; i++) { 13488420Siwasaki err = kthread_create(acpi_task_thread, NULL, &acpi_kthread_proc, 135104354Sscottl 0, 0, "acpi_task%d", i); 13688420Siwasaki if (err != 0) { 13788420Siwasaki printf("%s: kthread_create failed(%d)\n", __func__, err); 13888420Siwasaki break; 13988420Siwasaki } 14088420Siwasaki } 14188420Siwasaki return (err); 14288420Siwasaki} 14388420Siwasaki 144128228Snjl/* This function is called in interrupt context. */ 14567760SmsmithACPI_STATUS 146128228SnjlAcpiOsQueueForExecution(UINT32 Priority, OSD_EXECUTION_CALLBACK Function, 147128228Snjl void *Context) 14867760Smsmith{ 14967760Smsmith struct acpi_task *at; 15085503Sjhb int pri; 15167760Smsmith 15296926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 15371875Smsmith 15467760Smsmith if (Function == NULL) 155128228Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 15667760Smsmith 157128228Snjl at = malloc(sizeof(*at), M_ACPITASK, M_NOWAIT | M_ZERO); 15871418Speter if (at == NULL) 159128228Snjl return_ACPI_STATUS (AE_NO_MEMORY); 16067760Smsmith 16167760Smsmith at->at_function = Function; 16267760Smsmith at->at_context = Context; 16367760Smsmith switch (Priority) { 16470236Siwasaki case OSD_PRIORITY_GPE: 16585503Sjhb pri = 4; 16670236Siwasaki break; 16767760Smsmith case OSD_PRIORITY_HIGH: 16885503Sjhb pri = 3; 16967760Smsmith break; 17067760Smsmith case OSD_PRIORITY_MED: 17185503Sjhb pri = 2; 17267760Smsmith break; 17367760Smsmith case OSD_PRIORITY_LO: 17485503Sjhb pri = 1; 17567760Smsmith break; 17667760Smsmith default: 17767760Smsmith free(at, M_ACPITASK); 178128228Snjl return_ACPI_STATUS (AE_BAD_PARAMETER); 17967760Smsmith } 18085503Sjhb TASK_INIT(&at->at_task, pri, AcpiOsExecuteQueue, at); 18167760Smsmith 182128228Snjl taskqueue_enqueue(taskqueue_acpi, (struct task *)at); 183128228Snjl 184128228Snjl return_ACPI_STATUS (AE_OK); 18567760Smsmith} 18667760Smsmith 18767760Smsmithstatic void 18867760SmsmithAcpiOsExecuteQueue(void *arg, int pending) 18967760Smsmith{ 19088420Siwasaki struct acpi_task_queue *atq; 19167760Smsmith OSD_EXECUTION_CALLBACK Function; 19267760Smsmith void *Context; 19367760Smsmith 19496926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 19571875Smsmith 19688420Siwasaki atq = NULL; 19788420Siwasaki Function = NULL; 19888420Siwasaki Context = NULL; 19988420Siwasaki 20088420Siwasaki atq = malloc(sizeof(*atq), M_ACPITASK, M_NOWAIT); 20188420Siwasaki if (atq == NULL) { 20288420Siwasaki printf("%s: no memory\n", __func__); 20388420Siwasaki return; 20488420Siwasaki } 205133627Snjl atq->at = (struct acpi_task *)arg; 20688420Siwasaki 207133627Snjl ACPI_LOCK(taskq); 20888420Siwasaki STAILQ_INSERT_TAIL(&acpi_task_queue, atq, at_q); 20988420Siwasaki wakeup_one(&acpi_task_queue); 210133627Snjl ACPI_UNLOCK(taskq); 21167760Smsmith 21271875Smsmith return_VOID; 21367760Smsmith} 21467760Smsmith 21567760Smsmithvoid 216120662SnjlAcpiOsSleep(UINT32 Seconds, UINT32 Milliseconds) 21767760Smsmith{ 21867760Smsmith int timo; 21977432Smsmith static int dummy; 22067760Smsmith 22196926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22271875Smsmith 22377432Smsmith timo = (Seconds * hz) + Milliseconds * hz / 1000; 224120662Snjl 225120662Snjl /* 226120662Snjl * If requested sleep time is less than our hz resolution, use 227120662Snjl * DELAY instead for better granularity. 228120662Snjl */ 229120662Snjl if (timo > 0) 230120662Snjl tsleep(&dummy, 0, "acpislp", timo); 231120662Snjl else 232120662Snjl DELAY(Milliseconds * 1000); 233120662Snjl 23471875Smsmith return_VOID; 23567760Smsmith} 23667760Smsmith 23767760Smsmithvoid 238120662SnjlAcpiOsStall(UINT32 Microseconds) 23967760Smsmith{ 24096926Speter ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 24171875Smsmith 242120607Snjl DELAY(Microseconds); 24371875Smsmith return_VOID; 24467760Smsmith} 24577432Smsmith 24677432SmsmithUINT32 247120662SnjlAcpiOsGetThreadId(void) 24877432Smsmith{ 249105279Sjhb struct proc *p; 25077432Smsmith 251128228Snjl /* XXX do not add ACPI_FUNCTION_TRACE here, results in recursive call. */ 252128228Snjl 253105279Sjhb p = curproc; 254105279Sjhb KASSERT(p != NULL, ("%s: curproc is NULL!", __func__)); 255128228Snjl 256128228Snjl /* Returning 0 is not allowed. */ 257128228Snjl return (p->p_pid + 1); 25877432Smsmith} 259