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