OsdInterrupt.c revision 105282
1/*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *	$FreeBSD: head/sys/dev/acpica/Osd/OsdInterrupt.c 105282 2002-10-16 17:28:53Z jhb $
28 */
29
30/*
31 * 6.5 : Interrupt handling
32 */
33
34#include "acpi.h"
35
36#include <sys/bus.h>
37#include <machine/resource.h>
38#include <machine/bus.h>
39#include <sys/rman.h>
40
41#include <dev/acpica/acpivar.h>
42
43#define _COMPONENT	ACPI_OS_SERVICES
44ACPI_MODULE_NAME("INTERRUPT")
45
46static void		InterruptWrapper(void *arg);
47static OSD_HANDLER	InterruptHandler;
48
49/*
50 * XXX this does not correctly free resources in the case of partically successful
51 * attachment.
52 */
53ACPI_STATUS
54AcpiOsInstallInterruptHandler(UINT32 InterruptNumber, OSD_HANDLER ServiceRoutine, void *Context)
55{
56    struct acpi_softc	*sc;
57
58    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
59
60    if ((sc = devclass_get_softc(devclass_find("acpi"), 0)) == NULL)
61	panic("can't find ACPI device to register interrupt");
62    if (sc->acpi_dev == NULL)
63	panic("acpi softc has invalid device");
64
65    if ((InterruptNumber < 0) || (InterruptNumber > 255))
66	return_ACPI_STATUS(AE_BAD_PARAMETER);
67    if (ServiceRoutine == NULL)
68	return_ACPI_STATUS(AE_BAD_PARAMETER);
69    if (InterruptHandler != NULL && InterruptHandler != ServiceRoutine) {
70	device_printf(sc->acpi_dev, "can't register more than one ACPI interrupt\n");
71	return_ACPI_STATUS(AE_BAD_PARAMETER);
72    }
73    InterruptHandler = ServiceRoutine;
74
75    /*
76     * This isn't strictly true, as we ought to be able to handle > 1 interrupt.  The ACPI
77     * spec doesn't call for this though.
78     */
79    if (sc->acpi_irq != NULL) {
80	device_printf(sc->acpi_dev, "attempt to register more than one interrupt handler\n");
81	return_ACPI_STATUS(AE_ALREADY_EXISTS);
82    }
83    sc->acpi_irq_rid = 0;
84    bus_set_resource(sc->acpi_dev, SYS_RES_IRQ, 0, InterruptNumber, 1);
85    if ((sc->acpi_irq = bus_alloc_resource(sc->acpi_dev, SYS_RES_IRQ, &sc->acpi_irq_rid, 0, ~0, 1,
86					   RF_SHAREABLE | RF_ACTIVE)) == NULL) {
87	device_printf(sc->acpi_dev, "could not allocate SCI interrupt\n");
88	return_ACPI_STATUS(AE_ALREADY_EXISTS);
89    }
90    if (bus_setup_intr(sc->acpi_dev, sc->acpi_irq, INTR_TYPE_MISC, (driver_intr_t *)InterruptWrapper,
91		       Context, &sc->acpi_irq_handle)) {
92	device_printf(sc->acpi_dev, "could not set up SCI interrupt\n");
93	return_ACPI_STATUS(AE_ALREADY_EXISTS);
94    }
95
96    return_ACPI_STATUS(AE_OK);
97}
98
99ACPI_STATUS
100AcpiOsRemoveInterruptHandler (UINT32 InterruptNumber, OSD_HANDLER ServiceRoutine)
101{
102    struct acpi_softc	*sc;
103
104    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
105
106    if ((InterruptNumber < 0) || (InterruptNumber > 255))
107	return_ACPI_STATUS(AE_BAD_PARAMETER);
108    if (ServiceRoutine == NULL)
109	return_ACPI_STATUS(AE_BAD_PARAMETER);
110
111    if ((sc = devclass_get_softc(devclass_find("acpi"), 0)) == NULL)
112	panic("can't find ACPI device to deregister interrupt");
113
114    if (sc->acpi_irq == NULL)
115	return_ACPI_STATUS(AE_NOT_EXIST);
116
117    bus_teardown_intr(sc->acpi_dev, sc->acpi_irq, sc->acpi_irq_handle);
118    bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, 0, sc->acpi_irq);
119    bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, 0);
120
121    sc->acpi_irq = NULL;
122
123    return_ACPI_STATUS(AE_OK);
124}
125
126/*
127 * Interrupt handler wrapper.
128 */
129static void
130InterruptWrapper(void *arg)
131{
132    ACPI_LOCK_DECL;
133
134    ACPI_LOCK;
135    InterruptHandler(arg);
136    ACPI_UNLOCK;
137}
138