OsdInterrupt.c revision 77432
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 77432 2001-05-29 20:13:42Z 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
4667760Smsmith/*
4767760Smsmith * XXX this does not correctly free resources in the case of partically successful
4867760Smsmith * attachment.
4967760Smsmith */
5067760SmsmithACPI_STATUS
5167760SmsmithAcpiOsInstallInterruptHandler(UINT32 InterruptNumber, OSD_HANDLER ServiceRoutine, void *Context)
5267760Smsmith{
5367760Smsmith    struct acpi_softc	*sc;
5467760Smsmith
5577432Smsmith    FUNCTION_TRACE(__func__);
5671875Smsmith
5767760Smsmith    if ((InterruptNumber < 0) || (InterruptNumber > 255))
5871875Smsmith	return_ACPI_STATUS(AE_BAD_PARAMETER);
5967760Smsmith    if (ServiceRoutine == NULL)
6071875Smsmith	return_ACPI_STATUS(AE_BAD_PARAMETER);
6167760Smsmith
6267760Smsmith    if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL)
6367760Smsmith	panic("can't find ACPI device to register interrupt");
6467760Smsmith    if (sc->acpi_dev == NULL)
6567760Smsmith	panic("acpi softc has invalid device");
6667760Smsmith
6767760Smsmith    /*
6867760Smsmith     * This isn't strictly true, as we ought to be able to handle > 1 interrupt.  The ACPI
6967760Smsmith     * spec doesn't call for this though.
7067760Smsmith     */
7167760Smsmith    if (sc->acpi_irq != NULL) {
7267760Smsmith	device_printf(sc->acpi_dev, "attempt to register more than one interrupt handler\n");
7371875Smsmith	return_ACPI_STATUS(AE_EXIST);
7467760Smsmith    }
7567760Smsmith    sc->acpi_irq_rid = 0;
7667760Smsmith    bus_set_resource(sc->acpi_dev, SYS_RES_IRQ, 0, InterruptNumber, 1);
7767760Smsmith    if ((sc->acpi_irq = bus_alloc_resource(sc->acpi_dev, SYS_RES_IRQ, &sc->acpi_irq_rid, 0, ~0, 1,
7867760Smsmith					   RF_SHAREABLE | RF_ACTIVE)) == NULL) {
7967760Smsmith	device_printf(sc->acpi_dev, "could not allocate SCI interrupt\n");
8071875Smsmith	return_ACPI_STATUS(AE_EXIST);
8167760Smsmith    }
8267760Smsmith    if (bus_setup_intr(sc->acpi_dev, sc->acpi_irq, INTR_TYPE_MISC, (driver_intr_t *)ServiceRoutine, Context,
8367760Smsmith		       &sc->acpi_irq_handle)) {
8467760Smsmith	device_printf(sc->acpi_dev, "could not set up SCI interrupt\n");
8571875Smsmith	return_ACPI_STATUS(AE_EXIST);
8667760Smsmith    }
8767760Smsmith
8871875Smsmith    return_ACPI_STATUS(AE_OK);
8967760Smsmith}
9067760Smsmith
9167760SmsmithACPI_STATUS
9267760SmsmithAcpiOsRemoveInterruptHandler (UINT32 InterruptNumber, OSD_HANDLER ServiceRoutine)
9367760Smsmith{
9467760Smsmith    struct acpi_softc	*sc;
9567760Smsmith
9677432Smsmith    FUNCTION_TRACE(__func__);
9771875Smsmith
9867760Smsmith    if ((InterruptNumber < 0) || (InterruptNumber > 255))
9971875Smsmith	return_ACPI_STATUS(AE_BAD_PARAMETER);
10067760Smsmith    if (ServiceRoutine == NULL)
10171875Smsmith	return_ACPI_STATUS(AE_BAD_PARAMETER);
10267760Smsmith
10367760Smsmith    if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL)
10467760Smsmith	panic("can't find ACPI device to deregister interrupt");
10567760Smsmith
10667760Smsmith    if (sc->acpi_irq == NULL)
10771875Smsmith	return_ACPI_STATUS(AE_NOT_EXIST);
10867760Smsmith
10967760Smsmith    bus_teardown_intr(sc->acpi_dev, sc->acpi_irq, sc->acpi_irq_handle);
11067760Smsmith    bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, 0, sc->acpi_irq);
11167760Smsmith    bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, 0);
11267760Smsmith
11367760Smsmith    sc->acpi_irq = NULL;
11467760Smsmith
11571875Smsmith    return_ACPI_STATUS(AE_OK);
11667760Smsmith}
11767760Smsmith
118