1/*- 2 * Copyright (c) 2000 Michael Smith 3 * Copyright (c) 2000 BSDi 4 * Copyright (c) 2011 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.5 : Interrupt handling 31 */ 32 33#include <sys/cdefs.h>
|
34__FBSDID("$FreeBSD: head/sys/dev/acpica/Osd/OsdInterrupt.c 217240 2011-01-10 21:01:41Z jkim $");
|
34__FBSDID("$FreeBSD: head/sys/dev/acpica/Osd/OsdInterrupt.c 217241 2011-01-10 21:09:38Z jkim $"); |
35 36#include <sys/param.h> 37#include <sys/kernel.h> 38#include <sys/bus.h> 39#include <sys/lock.h> 40#include <sys/malloc.h> 41#include <sys/mutex.h> 42#include <machine/bus.h> 43#include <machine/resource.h> 44#include <sys/rman.h> 45 46#include <contrib/dev/acpica/include/acpi.h> 47#include <contrib/dev/acpica/include/accommon.h> 48 49#include <dev/acpica/acpivar.h> 50 51#define _COMPONENT ACPI_OS_SERVICES 52ACPI_MODULE_NAME("INTERRUPT") 53 54MALLOC_DEFINE(M_ACPIINTR, "acpiintr", "ACPI interrupt"); 55 56struct acpi_intr { 57 SLIST_ENTRY(acpi_intr) ai_link; 58 struct resource *ai_irq; 59 int ai_rid; 60 void *ai_handle; 61 int ai_number; 62 ACPI_OSD_HANDLER ai_handler; 63 void *ai_context; 64}; 65static SLIST_HEAD(, acpi_intr) acpi_intr_list = 66 SLIST_HEAD_INITIALIZER(acpi_intr_list); 67static struct mtx acpi_intr_lock; 68 69static UINT32 InterruptOverride; 70 71static void 72acpi_intr_init(struct mtx *lock) 73{ 74 75 mtx_init(lock, "ACPI interrupt lock", NULL, MTX_DEF); 76} 77 78SYSINIT(acpi_intr, SI_SUB_DRIVERS, SI_ORDER_FIRST, acpi_intr_init, 79 &acpi_intr_lock); 80 81static int 82acpi_intr_handler(void *arg) 83{ 84 struct acpi_intr *ai; 85 86 ai = arg; 87 KASSERT(ai != NULL && ai->ai_handler != NULL, 88 ("invalid ACPI interrupt handler")); 89 if (ai->ai_handler(ai->ai_context) == ACPI_INTERRUPT_HANDLED) 90 return (FILTER_HANDLED); 91 return (FILTER_STRAY); 92} 93 94ACPI_STATUS 95AcpiOsInstallInterruptHandler(UINT32 InterruptNumber, 96 ACPI_OSD_HANDLER ServiceRoutine, void *Context) 97{ 98 struct acpi_softc *sc; 99 struct acpi_intr *ai, *ap; 100 101 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 102 103 sc = devclass_get_softc(devclass_find("acpi"), 0); 104 KASSERT(sc != NULL && sc->acpi_dev != NULL, 105 ("can't find ACPI device to register interrupt")); 106
|
107 if (InterruptNumber < 0 || InterruptNumber > 255 ||
108 ServiceRoutine == NULL)
|
107 if (InterruptNumber > 255 || ServiceRoutine == NULL) |
108 return_ACPI_STATUS (AE_BAD_PARAMETER); 109 110 ai = malloc(sizeof(*ai), M_ACPIINTR, M_WAITOK | M_ZERO); 111 mtx_lock(&acpi_intr_lock); 112 SLIST_FOREACH(ap, &acpi_intr_list, ai_link) { 113 if (InterruptNumber == ap->ai_number || 114 (InterruptNumber == InterruptOverride && 115 InterruptNumber != AcpiGbl_FADT.SciInterrupt)) { 116 mtx_unlock(&acpi_intr_lock); 117 free(ai, M_ACPIINTR); 118 return_ACPI_STATUS (AE_ALREADY_EXISTS); 119 } 120 if (ai->ai_rid <= ap->ai_rid) 121 ai->ai_rid = ap->ai_rid + 1; 122 } 123 ai->ai_number = InterruptNumber; 124 ai->ai_handler = ServiceRoutine; 125 ai->ai_context = Context; 126 127 /* 128 * If the MADT contained an interrupt override directive for the SCI, 129 * we use that value instead of the one from the FADT. 130 */ 131 if (InterruptOverride != 0 && 132 InterruptNumber == AcpiGbl_FADT.SciInterrupt) { 133 device_printf(sc->acpi_dev, 134 "Overriding SCI from IRQ %u to IRQ %u\n", 135 InterruptNumber, InterruptOverride); 136 InterruptNumber = InterruptOverride; 137 } 138 139 /* Set up the interrupt resource. */ 140 bus_set_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid, 141 InterruptNumber, 1); 142 ai->ai_irq = bus_alloc_resource_any(sc->acpi_dev, SYS_RES_IRQ, 143 &ai->ai_rid, RF_SHAREABLE | RF_ACTIVE); 144 if (ai->ai_irq == NULL) { 145 device_printf(sc->acpi_dev, "could not allocate interrupt\n"); 146 goto error; 147 } 148 if (bus_setup_intr(sc->acpi_dev, ai->ai_irq, 149 INTR_TYPE_MISC | INTR_MPSAFE, acpi_intr_handler, NULL, ai, 150 &ai->ai_handle) != 0) { 151 device_printf(sc->acpi_dev, "could not set up interrupt\n"); 152 goto error; 153 } 154 SLIST_INSERT_HEAD(&acpi_intr_list, ai, ai_link); 155 mtx_unlock(&acpi_intr_lock); 156 return_ACPI_STATUS (AE_OK); 157 158error: 159 mtx_unlock(&acpi_intr_lock); 160 if (ai->ai_handle != NULL) 161 bus_teardown_intr(sc->acpi_dev, ai->ai_irq, ai->ai_handle); 162 if (ai->ai_irq != NULL) 163 bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid, 164 ai->ai_irq); 165 bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid); 166 free(ai, M_ACPIINTR); 167 return_ACPI_STATUS (AE_ALREADY_EXISTS); 168} 169 170ACPI_STATUS 171AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber, 172 ACPI_OSD_HANDLER ServiceRoutine) 173{ 174 struct acpi_softc *sc; 175 struct acpi_intr *ai; 176 177 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 178 179 sc = devclass_get_softc(devclass_find("acpi"), 0); 180 KASSERT(sc != NULL && sc->acpi_dev != NULL, 181 ("can't find ACPI device to deregister interrupt")); 182
|
184 if (InterruptNumber < 0 || InterruptNumber > 255 ||
185 ServiceRoutine == NULL)
|
183 if (InterruptNumber > 255 || ServiceRoutine == NULL) |
184 return_ACPI_STATUS (AE_BAD_PARAMETER); 185 mtx_lock(&acpi_intr_lock); 186 SLIST_FOREACH(ai, &acpi_intr_list, ai_link) 187 if (InterruptNumber == ai->ai_number) { 188 if (ServiceRoutine != ai->ai_handler) { 189 mtx_unlock(&acpi_intr_lock); 190 return_ACPI_STATUS (AE_BAD_PARAMETER); 191 } 192 SLIST_REMOVE(&acpi_intr_list, ai, acpi_intr, ai_link); 193 break; 194 } 195 mtx_unlock(&acpi_intr_lock); 196 if (ai == NULL) 197 return_ACPI_STATUS (AE_NOT_EXIST); 198 bus_teardown_intr(sc->acpi_dev, ai->ai_irq, ai->ai_handle); 199 bus_release_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid, ai->ai_irq); 200 bus_delete_resource(sc->acpi_dev, SYS_RES_IRQ, ai->ai_rid); 201 free(ai, M_ACPIINTR); 202 return_ACPI_STATUS (AE_OK); 203} 204 205ACPI_STATUS 206acpi_OverrideInterruptLevel(UINT32 InterruptNumber) 207{ 208 209 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 210 211 if (InterruptOverride != 0) 212 return_ACPI_STATUS (AE_ALREADY_EXISTS); 213 InterruptOverride = InterruptNumber; 214 return_ACPI_STATUS (AE_OK); 215}
|