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