xref: /dragonfly/sys/platform/pc64/acpica/acpi_fadt.c (revision 0fe46dc6)
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 static void
78 fadt_probe(void)
79 {
80 	ACPI_TABLE_FADT *fadt;
81 	vm_paddr_t fadt_paddr;
82 	enum intr_trigger trig;
83 	enum intr_polarity pola;
84 	int enabled = 1;
85 	char *env;
86 
87 	fadt_paddr = sdt_search(ACPI_SIG_FADT);
88 	if (fadt_paddr == 0) {
89 		kprintf("fadt_probe: can't locate FADT\n");
90 		return;
91 	}
92 
93 	fadt = sdt_sdth_map(fadt_paddr);
94 	KKASSERT(fadt != NULL);
95 
96 	/*
97 	 * FADT in ACPI specification 1.0 - 6.0
98 	 */
99 	if (fadt->Header.Revision < 1 || fadt->Header.Revision > 6) {
100 		kprintf("fadt_probe: unknown FADT revision %d\n",
101 			fadt->Header.Revision);
102 	}
103 
104 	if (fadt->Header.Length < ACPI_FADT_V1_SIZE) {
105 		kprintf("fadt_probe: invalid FADT length %u (< %u)\n",
106 		    fadt->Header.Length, ACPI_FADT_V1_SIZE);
107 		goto back;
108 	}
109 
110 	kgetenv_int("hw.acpi.sci.enabled", &enabled);
111 	if (!enabled)
112 		goto back;
113 
114 	acpi_sci_irq = fadt->SciInterrupt;
115 
116 	env = kgetenv("hw.acpi.sci.trigger");
117 	if (env == NULL)
118 		goto back;
119 
120 	trig = INTR_TRIGGER_CONFORM;
121 	if (strcmp(env, "edge") == 0)
122 		trig = INTR_TRIGGER_EDGE;
123 	else if (strcmp(env, "level") == 0)
124 		trig = INTR_TRIGGER_LEVEL;
125 
126 	kfreeenv(env);
127 
128 	if (trig == INTR_TRIGGER_CONFORM)
129 		goto back;
130 
131 	env = kgetenv("hw.acpi.sci.polarity");
132 	if (env == NULL)
133 		goto back;
134 
135 	pola = INTR_POLARITY_CONFORM;
136 	if (strcmp(env, "high") == 0)
137 		pola = INTR_POLARITY_HIGH;
138 	else if (strcmp(env, "low") == 0)
139 		pola = INTR_POLARITY_LOW;
140 
141 	kfreeenv(env);
142 
143 	if (pola == INTR_POLARITY_CONFORM)
144 		goto back;
145 
146 	acpi_sci_trig = trig;
147 	acpi_sci_pola = pola;
148 back:
149 	if (acpi_sci_irq >= 0) {
150 		FADT_VPRINTF("SCI irq %d, %s/%s\n", acpi_sci_irq,
151 			     intr_str_trigger(acpi_sci_trig),
152 			     intr_str_polarity(acpi_sci_pola));
153 	} else {
154 		FADT_VPRINTF("SCI is disabled\n");
155 	}
156 	sdt_sdth_unmap(&fadt->Header);
157 }
158 SYSINIT(fadt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, fadt_probe, 0);
159 
160 static void
161 acpi_sci_dummy_intr(void *dummy __unused, void *frame __unused)
162 {
163 }
164 
165 static boolean_t
166 acpi_sci_test(const struct acpi_sci_mode *mode)
167 {
168 	void *sci_desc;
169 	long last_cnt;
170 
171 	FADT_VPRINTF("SCI testing %s/%s\n",
172 	    intr_str_trigger(mode->sci_trig),
173 	    intr_str_polarity(mode->sci_pola));
174 
175 	last_cnt = get_interrupt_counter(acpi_sci_irq, 0);
176 
177 	machintr_legacy_intr_config(acpi_sci_irq,
178 	    mode->sci_trig, mode->sci_pola);
179 
180 	sci_desc = register_int(acpi_sci_irq,
181 	    acpi_sci_dummy_intr, NULL, "sci", NULL,
182 	    INTR_EXCL | INTR_CLOCK |
183 	    INTR_NOPOLL | INTR_MPSAFE | INTR_NOENTROPY, 0);
184 
185 	DELAY(100 * 1000);
186 
187 	unregister_int(sci_desc, 0);
188 
189 	if (get_interrupt_counter(acpi_sci_irq, 0) - last_cnt < 20) {
190 		acpi_sci_trig = mode->sci_trig;
191 		acpi_sci_pola = mode->sci_pola;
192 
193 		kprintf("ACPI FADT: SCI select %s/%s\n",
194 		    intr_str_trigger(acpi_sci_trig),
195 		    intr_str_polarity(acpi_sci_pola));
196 		return TRUE;
197 	}
198 	return FALSE;
199 }
200 
201 void
202 acpi_sci_config(void)
203 {
204 	const struct acpi_sci_mode *mode;
205 
206 	KKASSERT(mycpuid == 0);
207 
208 	if (machintr_legacy_intr_find(acpi_sci_irq,
209 	    INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM) < 0) {
210 		kprintf("ACPI FADT: SCI irq %d is invalid, disable\n",
211 		    acpi_sci_irq);
212 		acpi_sci_irq = -1;
213 		return;
214 	}
215 
216 	if (acpi_sci_irq < 0)
217 		return;
218 
219 	if (acpi_sci_trig != INTR_TRIGGER_CONFORM) {
220 		KKASSERT(acpi_sci_pola != INTR_POLARITY_CONFORM);
221 		machintr_legacy_intr_config(acpi_sci_irq,
222 		    acpi_sci_trig, acpi_sci_pola);
223 		return;
224 	}
225 
226 	kprintf("ACPI FADT: SCI testing interrupt mode ...\n");
227 	if (acpi_sci_mode1 != NULL) {
228 		if (acpi_sci_test(acpi_sci_mode1))
229 			return;
230 	}
231 	for (mode = acpi_sci_modes; mode->sci_trig != INTR_TRIGGER_CONFORM;
232 	     ++mode) {
233 		if (mode == acpi_sci_mode1)
234 			continue;
235 		if (acpi_sci_test(mode))
236 			return;
237 	}
238 
239 	kprintf("ACPI FADT: no suitable interrupt mode for SCI, disable\n");
240 	acpi_sci_irq = -1;
241 }
242 
243 int
244 acpi_sci_enabled(void)
245 {
246 	if (acpi_sci_irq >= 0)
247 		return 1;
248 	else
249 		return 0;
250 }
251 
252 int
253 acpi_sci_pci_shareable(void)
254 {
255 	if (acpi_sci_irq >= 0 &&
256 	    acpi_sci_trig == INTR_TRIGGER_LEVEL &&
257 	    acpi_sci_pola == INTR_POLARITY_LOW)
258 		return 1;
259 	else
260 		return 0;
261 }
262 
263 int
264 acpi_sci_irqno(void)
265 {
266 	return acpi_sci_irq;
267 }
268 
269 void
270 acpi_sci_setmode1(enum intr_trigger trig, enum intr_polarity pola)
271 {
272 	const struct acpi_sci_mode *mode;
273 
274 	for (mode = acpi_sci_modes; mode->sci_trig != INTR_TRIGGER_CONFORM;
275 	     ++mode) {
276 		if (mode->sci_trig == trig && mode->sci_pola == pola) {
277 			acpi_sci_mode1 = mode;
278 			return;
279 		}
280 	}
281 }
282