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 "acpi_sdt.h" 44 #include "acpi_sdt_var.h" 45 #include "acpi_sci_var.h" 46 47 #define FADT_VPRINTF(fmt, arg...) \ 48 do { \ 49 if (bootverbose) \ 50 kprintf("ACPI FADT: " fmt , ##arg); \ 51 } while (0) 52 53 /* Fixed ACPI Description Table */ 54 struct acpi_fadt { 55 struct acpi_sdth fadt_hdr; 56 uint32_t fadt_fw_ctrl; 57 uint32_t fadt_dsdt; 58 uint8_t fadt_rsvd1; 59 uint8_t fadt_pm_prof; 60 uint16_t fadt_sci_int; 61 uint32_t fadt_smi_cmd; 62 uint8_t fadt_acpi_en; 63 uint8_t fadt_acpi_dis; 64 uint8_t fadt_s4bios; 65 uint8_t fadt_pstate; 66 /* More ... */ 67 } __packed; 68 69 struct acpi_sci_mode { 70 enum intr_trigger sci_trig; 71 enum intr_polarity sci_pola; 72 }; 73 74 static int acpi_sci_irq = -1; 75 static enum intr_trigger acpi_sci_trig = INTR_TRIGGER_CONFORM; 76 static enum intr_polarity acpi_sci_pola = INTR_POLARITY_CONFORM; 77 static const struct acpi_sci_mode *acpi_sci_mode1 = NULL; 78 79 static const struct acpi_sci_mode acpi_sci_modes[] = { 80 /* 81 * NOTE: Order is critical 82 */ 83 { INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW }, 84 { INTR_TRIGGER_LEVEL, INTR_POLARITY_HIGH }, 85 { INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH }, 86 { INTR_TRIGGER_EDGE, INTR_POLARITY_LOW }, 87 88 /* Required last entry */ 89 { INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM } 90 }; 91 92 static void 93 fadt_probe(void) 94 { 95 struct acpi_fadt *fadt; 96 vm_paddr_t fadt_paddr; 97 enum intr_trigger trig; 98 enum intr_polarity pola; 99 int enabled = 1; 100 char *env; 101 102 fadt_paddr = sdt_search(ACPI_FADT_SIG); 103 if (fadt_paddr == 0) { 104 kprintf("fadt_probe: can't locate FADT\n"); 105 return; 106 } 107 108 fadt = sdt_sdth_map(fadt_paddr); 109 KKASSERT(fadt != NULL); 110 111 /* 112 * FADT in ACPI specification 1.0 - 4.0 113 */ 114 if (fadt->fadt_hdr.sdth_rev < 1 || fadt->fadt_hdr.sdth_rev > 4) { 115 kprintf("fadt_probe: unsupported FADT revision %d\n", 116 fadt->fadt_hdr.sdth_rev); 117 goto back; 118 } 119 120 if (fadt->fadt_hdr.sdth_len < sizeof(*fadt)) { 121 kprintf("fadt_probe: invalid FADT length %u\n", 122 fadt->fadt_hdr.sdth_len); 123 goto back; 124 } 125 126 kgetenv_int("hw.acpi.sci.enabled", &enabled); 127 if (!enabled) 128 goto back; 129 130 acpi_sci_irq = fadt->fadt_sci_int; 131 132 env = kgetenv("hw.acpi.sci.trigger"); 133 if (env == NULL) 134 goto back; 135 136 trig = INTR_TRIGGER_CONFORM; 137 if (strcmp(env, "edge") == 0) 138 trig = INTR_TRIGGER_EDGE; 139 else if (strcmp(env, "level") == 0) 140 trig = INTR_TRIGGER_LEVEL; 141 142 kfreeenv(env); 143 144 if (trig == INTR_TRIGGER_CONFORM) 145 goto back; 146 147 env = kgetenv("hw.acpi.sci.polarity"); 148 if (env == NULL) 149 goto back; 150 151 pola = INTR_POLARITY_CONFORM; 152 if (strcmp(env, "high") == 0) 153 pola = INTR_POLARITY_HIGH; 154 else if (strcmp(env, "low") == 0) 155 pola = INTR_POLARITY_LOW; 156 157 kfreeenv(env); 158 159 if (pola == INTR_POLARITY_CONFORM) 160 goto back; 161 162 acpi_sci_trig = trig; 163 acpi_sci_pola = pola; 164 back: 165 if (acpi_sci_irq >= 0) { 166 FADT_VPRINTF("SCI irq %d, %s/%s\n", acpi_sci_irq, 167 intr_str_trigger(acpi_sci_trig), 168 intr_str_polarity(acpi_sci_pola)); 169 } else { 170 FADT_VPRINTF("SCI is disabled\n"); 171 } 172 sdt_sdth_unmap(&fadt->fadt_hdr); 173 } 174 SYSINIT(fadt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, fadt_probe, 0); 175 176 static void 177 acpi_sci_dummy_intr(void *dummy __unused, void *frame __unused) 178 { 179 } 180 181 static boolean_t 182 acpi_sci_test(const struct acpi_sci_mode *mode) 183 { 184 void *sci_desc; 185 long last_cnt; 186 187 FADT_VPRINTF("SCI testing %s/%s\n", 188 intr_str_trigger(mode->sci_trig), 189 intr_str_polarity(mode->sci_pola)); 190 191 last_cnt = get_interrupt_counter(acpi_sci_irq, 0); 192 193 machintr_legacy_intr_config(acpi_sci_irq, 194 mode->sci_trig, mode->sci_pola); 195 196 sci_desc = register_int(acpi_sci_irq, 197 acpi_sci_dummy_intr, NULL, "sci", NULL, 198 INTR_EXCL | INTR_CLOCK | 199 INTR_NOPOLL | INTR_MPSAFE | INTR_NOENTROPY, 0); 200 201 DELAY(100 * 1000); 202 203 unregister_int(sci_desc, 0); 204 205 if (get_interrupt_counter(acpi_sci_irq, 0) - last_cnt < 20) { 206 acpi_sci_trig = mode->sci_trig; 207 acpi_sci_pola = mode->sci_pola; 208 209 kprintf("ACPI FADT: SCI select %s/%s\n", 210 intr_str_trigger(acpi_sci_trig), 211 intr_str_polarity(acpi_sci_pola)); 212 return TRUE; 213 } 214 return FALSE; 215 } 216 217 void 218 acpi_sci_config(void) 219 { 220 const struct acpi_sci_mode *mode; 221 222 KKASSERT(mycpuid == 0); 223 224 if (machintr_legacy_intr_find(acpi_sci_irq, 225 INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM) < 0) { 226 kprintf("ACPI FADT: SCI irq %d is invalid, disable\n", 227 acpi_sci_irq); 228 acpi_sci_irq = -1; 229 return; 230 } 231 232 if (acpi_sci_irq < 0) 233 return; 234 235 if (acpi_sci_trig != INTR_TRIGGER_CONFORM) { 236 KKASSERT(acpi_sci_pola != INTR_POLARITY_CONFORM); 237 machintr_legacy_intr_config(acpi_sci_irq, 238 acpi_sci_trig, acpi_sci_pola); 239 return; 240 } 241 242 kprintf("ACPI FADT: SCI testing interrupt mode ...\n"); 243 if (acpi_sci_mode1 != NULL) { 244 if (acpi_sci_test(acpi_sci_mode1)) 245 return; 246 } 247 for (mode = acpi_sci_modes; mode->sci_trig != INTR_TRIGGER_CONFORM; 248 ++mode) { 249 if (mode == acpi_sci_mode1) 250 continue; 251 if (acpi_sci_test(mode)) 252 return; 253 } 254 255 kprintf("ACPI FADT: no suitable interrupt mode for SCI, disable\n"); 256 acpi_sci_irq = -1; 257 } 258 259 int 260 acpi_sci_enabled(void) 261 { 262 if (acpi_sci_irq >= 0) 263 return 1; 264 else 265 return 0; 266 } 267 268 int 269 acpi_sci_pci_shariable(void) 270 { 271 if (acpi_sci_irq >= 0 && 272 acpi_sci_trig == INTR_TRIGGER_LEVEL && 273 acpi_sci_pola == INTR_POLARITY_LOW) 274 return 1; 275 else 276 return 0; 277 } 278 279 int 280 acpi_sci_irqno(void) 281 { 282 return acpi_sci_irq; 283 } 284 285 void 286 acpi_sci_setmode1(enum intr_trigger trig, enum intr_polarity pola) 287 { 288 const struct acpi_sci_mode *mode; 289 290 for (mode = acpi_sci_modes; mode->sci_trig != INTR_TRIGGER_CONFORM; 291 ++mode) { 292 if (mode->sci_trig == trig && mode->sci_pola == pola) { 293 acpi_sci_mode1 = mode; 294 return; 295 } 296 } 297 } 298