OsdInterrupt.c revision 79386
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/OsdInterrupt.c 79386 2001-07-07 10:18:10Z msmith $
2867760Smsmith */
2967760Smsmith
3067760Smsmith/*
3167760Smsmith * 6.5 : Interrupt handling
3267760Smsmith */
3367760Smsmith
3467760Smsmith#include "acpi.h"
3567760Smsmith
3667760Smsmith#include <sys/bus.h>
3767760Smsmith#include <machine/resource.h>
3867760Smsmith#include <machine/bus.h>
3967760Smsmith#include <sys/rman.h>
4067760Smsmith
4167760Smsmith#include <dev/acpica/acpivar.h>
4267760Smsmith
4377432Smsmith#define _COMPONENT	ACPI_OS_SERVICES
4471875SmsmithMODULE_NAME("INTERRUPT")
4571875Smsmith
4679000Smsmithstatic void		InterruptWrapper(void *arg);
4779000Smsmithstatic OSD_HANDLER	InterruptHandler;
4879000Smsmith
4967760Smsmith/*
5067760Smsmith * XXX this does not correctly free resources in the case of partically successful
5167760Smsmith * attachment.
5267760Smsmith */
5367760SmsmithACPI_STATUS
5467760SmsmithAcpiOsInstallInterruptHandler(UINT32 InterruptNumber, OSD_HANDLER ServiceRoutine, void *Context)
5567760Smsmith{
5667760Smsmith    struct acpi_softc	*sc;
5767760Smsmith
5877432Smsmith    FUNCTION_TRACE(__func__);
5971875Smsmith
6079386Smsmith    if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL)
6179386Smsmith	panic("can't find ACPI device to register interrupt");
6279386Smsmith    if (sc->acpi_dev == NULL)
6379386Smsmith	panic("acpi softc has invalid device");
6479386Smsmith
6567760Smsmith    if ((InterruptNumber < 0) || (InterruptNumber > 255))
6671875Smsmith	return_ACPI_STATUS(AE_BAD_PARAMETER);
6767760Smsmith    if (ServiceRoutine == NULL)
6871875Smsmith	return_ACPI_STATUS(AE_BAD_PARAMETER);
6979000Smsmith    if (InterruptHandler != NULL) {
7079000Smsmith	device_printf(sc->acpi_dev, "can't register more than one ACPI interrupt\n");
7179000Smsmith	return_ACPI_STATUS(AE_BAD_PARAMETER);
7279000Smsmith    }
7379000Smsmith    InterruptHandler = ServiceRoutine;
7467760Smsmith
7567760Smsmith    /*
7667760Smsmith     * This isn't strictly true, as we ought to be able to handle > 1 interrupt.  The ACPI
7767760Smsmith     * spec doesn't call for this though.
7867760Smsmith     */
7967760Smsmith    if (sc->acpi_irq != NULL) {
8067760Smsmith	device_printf(sc->acpi_dev, "attempt to register more than one interrupt handler\n");
8171875Smsmith	return_ACPI_STATUS(AE_EXIST);
8267760Smsmith    }
8367760Smsmith    sc->acpi_irq_rid = 0;
8467760Smsmith    bus_set_resource(sc->acpi_dev, SYS_RES_IRQ, 0, InterruptNumber, 1);
8567760Smsmith    if ((sc->acpi_irq = bus_alloc_resource(sc->acpi_dev, SYS_RES_IRQ, &sc->acpi_irq_rid, 0, ~0, 1,
8667760Smsmith					   RF_SHAREABLE | RF_ACTIVE)) == NULL) {
8767760Smsmith	device_printf(sc->acpi_dev, "could not allocate SCI interrupt\n");
8871875Smsmith	return_ACPI_STATUS(AE_EXIST);
8967760Smsmith    }
9079000Smsmith    if (bus_setup_intr(sc->acpi_dev, sc->acpi_irq, INTR_TYPE_MISC, (driver_intr_t *)InterruptWrapper,
9179000Smsmith		       Context, &sc->acpi_irq_handle)) {
9267760Smsmith	device_printf(sc->acpi_dev, "could not set up SCI interrupt\n");
9371875Smsmith	return_ACPI_STATUS(AE_EXIST);
9467760Smsmith    }
9567760Smsmith
9671875Smsmith    return_ACPI_STATUS(AE_OK);
9767760Smsmith}
9867760Smsmith
9967760SmsmithACPI_STATUS
10067760SmsmithAcpiOsRemoveInterruptHandler (UINT32 InterruptNumber, OSD_HANDLER ServiceRoutine)
10167760Smsmith{
10267760Smsmith    struct acpi_softc	*sc;
10367760Smsmith
10477432Smsmith    FUNCTION_TRACE(__func__);
10571875Smsmith
10667760Smsmith    if ((InterruptNumber < 0) || (InterruptNumber > 255))
10771875Smsmith	return_ACPI_STATUS(AE_BAD_PARAMETER);
10867760Smsmith    if (ServiceRoutine == NULL)
10971875Smsmith	return_ACPI_STATUS(AE_BAD_PARAMETER);
11067760Smsmith
11167760Smsmith    if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL)
11267760Smsmith	panic("can't find ACPI device to deregister interrupt");
11367760Smsmith
11467760Smsmith    if (sc->acpi_irq == NULL)
11571875Smsmith	return_ACPI_STATUS(AE_NOT_EXIST);
11667760Smsmith
11767760Smsmith    bus_teardown_intr(sc->acpi_dev, sc->acpi_irq, sc->acpi_irq_handle);
11867760Smsmith    bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, 0, sc->acpi_irq);
11967760Smsmith    bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, 0);
12067760Smsmith
12167760Smsmith    sc->acpi_irq = NULL;
12267760Smsmith
12371875Smsmith    return_ACPI_STATUS(AE_OK);
12467760Smsmith}
12567760Smsmith
12679000Smsmith/*
12779000Smsmith * Interrupt handler wrapper.
12879000Smsmith */
12979000Smsmithstatic void
13079000SmsmithInterruptWrapper(void *arg)
13179000Smsmith{
13279000Smsmith    ACPI_LOCK;
13379000Smsmith    InterruptHandler(arg);
13479000Smsmith    ACPI_UNLOCK;
13579000Smsmith}
136