1 /* 2 * Copyright (c) 2009 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Sepherosa Ziehau <sepherosa@gmail.com> 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 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/bus.h> 37 #include <sys/interrupt.h> 38 #include <sys/kernel.h> 39 #include <sys/machintr.h> 40 #include <sys/systm.h> 41 #include <sys/thread2.h> 42 43 #include <contrib/dev/acpica/source/include/acpi.h> 44 45 #include "acpi_sdt_var.h" 46 #include "acpi_sci_var.h" 47 48 #define FADT_VPRINTF(fmt, arg...) \ 49 do { \ 50 if (bootverbose) \ 51 kprintf("ACPI FADT: " fmt , ##arg); \ 52 } while (0) 53 54 struct acpi_sci_mode { 55 enum intr_trigger sci_trig; 56 enum intr_polarity sci_pola; 57 }; 58 59 static int acpi_sci_irq = -1; 60 static enum intr_trigger acpi_sci_trig = INTR_TRIGGER_CONFORM; 61 static enum intr_polarity acpi_sci_pola = INTR_POLARITY_CONFORM; 62 static const struct acpi_sci_mode *acpi_sci_mode1 = NULL; 63 64 static const struct acpi_sci_mode acpi_sci_modes[] = { 65 /* 66 * NOTE: Order is critical 67 */ 68 { INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW }, 69 { INTR_TRIGGER_LEVEL, INTR_POLARITY_HIGH }, 70 { INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH }, 71 { INTR_TRIGGER_EDGE, INTR_POLARITY_LOW }, 72 73 /* Required last entry */ 74 { INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM } 75 }; 76 77 /* Defaulting to 1, to stop atkbdc from being configured early, via cninit() */ 78 int acpi_fadt_8042_nolegacy = 1; 79 80 static void 81 fadt_probe(void) 82 { 83 ACPI_TABLE_FADT *fadt; 84 vm_paddr_t fadt_paddr; 85 enum intr_trigger trig; 86 enum intr_polarity pola; 87 int enabled = 1; 88 char *env; 89 90 fadt_paddr = sdt_search(ACPI_SIG_FADT); 91 if (fadt_paddr == 0) { 92 acpi_fadt_8042_nolegacy = 0; 93 kprintf("fadt_probe: can't locate FADT\n"); 94 return; 95 } 96 97 fadt = sdt_sdth_map(fadt_paddr); 98 KKASSERT(fadt != NULL); 99 100 /* 101 * FADT in ACPI specification 1.0 - 6.0 102 */ 103 if (fadt->Header.Revision < 1 || fadt->Header.Revision > 6) { 104 kprintf("fadt_probe: unknown FADT revision %d\n", 105 fadt->Header.Revision); 106 } 107 108 if (fadt->Header.Length < ACPI_FADT_V1_SIZE) { 109 acpi_fadt_8042_nolegacy = 0; 110 kprintf("fadt_probe: invalid FADT length %u (< %u)\n", 111 fadt->Header.Length, ACPI_FADT_V1_SIZE); 112 goto back; 113 } 114 115 if (fadt->Header.Length >= ACPI_FADT_V3_SIZE && 116 fadt->Header.Revision >= 3) { 117 if ((fadt->BootFlags & ACPI_FADT_8042) != 0) 118 acpi_fadt_8042_nolegacy = 0; 119 } else { 120 acpi_fadt_8042_nolegacy = 0; 121 } 122 123 kgetenv_int("hw.acpi.sci.enabled", &enabled); 124 if (!enabled) 125 goto back; 126 127 acpi_sci_irq = fadt->SciInterrupt; 128 129 env = kgetenv("hw.acpi.sci.trigger"); 130 if (env == NULL) 131 goto back; 132 133 trig = INTR_TRIGGER_CONFORM; 134 if (strcmp(env, "edge") == 0) 135 trig = INTR_TRIGGER_EDGE; 136 else if (strcmp(env, "level") == 0) 137 trig = INTR_TRIGGER_LEVEL; 138 139 kfreeenv(env); 140 141 if (trig == INTR_TRIGGER_CONFORM) 142 goto back; 143 144 env = kgetenv("hw.acpi.sci.polarity"); 145 if (env == NULL) 146 goto back; 147 148 pola = INTR_POLARITY_CONFORM; 149 if (strcmp(env, "high") == 0) 150 pola = INTR_POLARITY_HIGH; 151 else if (strcmp(env, "low") == 0) 152 pola = INTR_POLARITY_LOW; 153 154 kfreeenv(env); 155 156 if (pola == INTR_POLARITY_CONFORM) 157 goto back; 158 159 acpi_sci_trig = trig; 160 acpi_sci_pola = pola; 161 back: 162 if (acpi_sci_irq >= 0) { 163 FADT_VPRINTF("SCI irq %d, %s/%s\n", acpi_sci_irq, 164 intr_str_trigger(acpi_sci_trig), 165 intr_str_polarity(acpi_sci_pola)); 166 } else { 167 FADT_VPRINTF("SCI is disabled\n"); 168 } 169 sdt_sdth_unmap(&fadt->Header); 170 } 171 SYSINIT(fadt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, fadt_probe, 0); 172 173 static void 174 acpi_sci_dummy_intr(void *dummy __unused, void *frame __unused) 175 { 176 } 177 178 static boolean_t 179 acpi_sci_test(const struct acpi_sci_mode *mode) 180 { 181 void *sci_desc; 182 long last_cnt; 183 184 FADT_VPRINTF("SCI testing %s/%s\n", 185 intr_str_trigger(mode->sci_trig), 186 intr_str_polarity(mode->sci_pola)); 187 188 last_cnt = get_interrupt_counter(acpi_sci_irq, 0); 189 190 machintr_legacy_intr_config(acpi_sci_irq, 191 mode->sci_trig, mode->sci_pola); 192 193 sci_desc = register_int(acpi_sci_irq, 194 acpi_sci_dummy_intr, NULL, "sci", NULL, 195 INTR_EXCL | INTR_CLOCK | 196 INTR_NOPOLL | INTR_MPSAFE | INTR_NOENTROPY, 0); 197 198 DELAY(100 * 1000); 199 200 unregister_int(sci_desc, 0); 201 202 if (get_interrupt_counter(acpi_sci_irq, 0) - last_cnt < 20) { 203 acpi_sci_trig = mode->sci_trig; 204 acpi_sci_pola = mode->sci_pola; 205 206 kprintf("ACPI FADT: SCI select %s/%s\n", 207 intr_str_trigger(acpi_sci_trig), 208 intr_str_polarity(acpi_sci_pola)); 209 return TRUE; 210 } 211 return FALSE; 212 } 213 214 void 215 acpi_sci_config(void) 216 { 217 const struct acpi_sci_mode *mode; 218 219 KKASSERT(mycpuid == 0); 220 221 if (machintr_legacy_intr_find(acpi_sci_irq, 222 INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM) < 0) { 223 kprintf("ACPI FADT: SCI irq %d is invalid, disable\n", 224 acpi_sci_irq); 225 acpi_sci_irq = -1; 226 return; 227 } 228 229 if (acpi_sci_irq < 0) 230 return; 231 232 if (acpi_sci_trig != INTR_TRIGGER_CONFORM) { 233 KKASSERT(acpi_sci_pola != INTR_POLARITY_CONFORM); 234 machintr_legacy_intr_config(acpi_sci_irq, 235 acpi_sci_trig, acpi_sci_pola); 236 return; 237 } 238 239 kprintf("ACPI FADT: SCI testing interrupt mode ...\n"); 240 if (acpi_sci_mode1 != NULL) { 241 if (acpi_sci_test(acpi_sci_mode1)) 242 return; 243 } 244 for (mode = acpi_sci_modes; mode->sci_trig != INTR_TRIGGER_CONFORM; 245 ++mode) { 246 if (mode == acpi_sci_mode1) 247 continue; 248 if (acpi_sci_test(mode)) 249 return; 250 } 251 252 kprintf("ACPI FADT: no suitable interrupt mode for SCI, disable\n"); 253 acpi_sci_irq = -1; 254 } 255 256 int 257 acpi_sci_enabled(void) 258 { 259 if (acpi_sci_irq >= 0) 260 return 1; 261 else 262 return 0; 263 } 264 265 int 266 acpi_sci_pci_shareable(void) 267 { 268 if (acpi_sci_irq >= 0 && 269 acpi_sci_trig == INTR_TRIGGER_LEVEL && 270 acpi_sci_pola == INTR_POLARITY_LOW) 271 return 1; 272 else 273 return 0; 274 } 275 276 int 277 acpi_sci_irqno(void) 278 { 279 return acpi_sci_irq; 280 } 281 282 void 283 acpi_sci_setmode1(enum intr_trigger trig, enum intr_polarity pola) 284 { 285 const struct acpi_sci_mode *mode; 286 287 for (mode = acpi_sci_modes; mode->sci_trig != INTR_TRIGGER_CONFORM; 288 ++mode) { 289 if (mode->sci_trig == trig && mode->sci_pola == pola) { 290 acpi_sci_mode1 = mode; 291 return; 292 } 293 } 294 } 295