kernel/acpi: Reduce code duplication with ACPICA.
[dragonfly.git] / sys / platform / pc64 / acpica / acpi_fadt.c
CommitLineData
05ba9093
SZ
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>
52234ee9 37#include <sys/interrupt.h>
05ba9093 38#include <sys/kernel.h>
5e2a2352 39#include <sys/machintr.h>
05ba9093 40#include <sys/systm.h>
6355d931 41#include <sys/thread2.h>
05ba9093 42
fc673eaa
SW
43#include <contrib/dev/acpica/source/include/acpi.h>
44
05ba9093 45#include "acpi_sdt_var.h"
5e2a2352 46#include "acpi_sci_var.h"
05ba9093
SZ
47
48#define FADT_VPRINTF(fmt, arg...) \
49do { \
50 if (bootverbose) \
51 kprintf("ACPI FADT: " fmt , ##arg); \
52} while (0)
53
52234ee9
SZ
54struct acpi_sci_mode {
55 enum intr_trigger sci_trig;
56 enum intr_polarity sci_pola;
57};
58
05ba9093 59static int acpi_sci_irq = -1;
5e2a2352
SZ
60static enum intr_trigger acpi_sci_trig = INTR_TRIGGER_CONFORM;
61static enum intr_polarity acpi_sci_pola = INTR_POLARITY_CONFORM;
c098b394 62static const struct acpi_sci_mode *acpi_sci_mode1 = NULL;
05ba9093 63
52234ee9
SZ
64static 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
05ba9093
SZ
77static void
78fadt_probe(void)
79{
fc673eaa 80 ACPI_TABLE_FADT *fadt;
05ba9093 81 vm_paddr_t fadt_paddr;
5e2a2352
SZ
82 enum intr_trigger trig;
83 enum intr_polarity pola;
84 int enabled = 1;
85 char *env;
05ba9093 86
fc673eaa 87 fadt_paddr = sdt_search(ACPI_SIG_FADT);
05ba9093
SZ
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 /*
df362704 97 * FADT in ACPI specification 1.0 - 5.0
05ba9093 98 */
fc673eaa 99 if (fadt->Header.Revision < 1 || fadt->Header.Revision > 5) {
fa90647a 100 kprintf("fadt_probe: unknown FADT revision %d\n",
fc673eaa 101 fadt->Header.Revision);
05ba9093
SZ
102 }
103
fc673eaa
SW
104 if (fadt->Header.Length < sizeof(*fadt)) {
105 kprintf("fadt_probe: invalid FADT length %u (< %zd)\n",
106 fadt->Header.Length, sizeof(*fadt));
05ba9093
SZ
107 goto back;
108 }
109
5e2a2352
SZ
110 kgetenv_int("hw.acpi.sci.enabled", &enabled);
111 if (!enabled)
112 goto back;
113
fc673eaa 114 acpi_sci_irq = fadt->SciInterrupt;
05ba9093 115
5e2a2352
SZ
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;
05ba9093 148back:
5e2a2352
SZ
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 }
fc673eaa 156 sdt_sdth_unmap(&fadt->Header);
05ba9093
SZ
157}
158SYSINIT(fadt_probe, SI_BOOT2_PRESMP, SI_ORDER_SECOND, fadt_probe, 0);
5e2a2352 159
52234ee9
SZ
160static void
161acpi_sci_dummy_intr(void *dummy __unused, void *frame __unused)
162{
163}
164
c098b394
SZ
165static boolean_t
166acpi_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
5e2a2352
SZ
201void
202acpi_sci_config(void)
203{
52234ee9
SZ
204 const struct acpi_sci_mode *mode;
205
6355d931
SZ
206 KKASSERT(mycpuid == 0);
207
03301eb8
SZ
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
52234ee9
SZ
216 if (acpi_sci_irq < 0)
217 return;
5e2a2352
SZ
218
219 if (acpi_sci_trig != INTR_TRIGGER_CONFORM) {
220 KKASSERT(acpi_sci_pola != INTR_POLARITY_CONFORM);
bec969af 221 machintr_legacy_intr_config(acpi_sci_irq,
5e2a2352
SZ
222 acpi_sci_trig, acpi_sci_pola);
223 return;
224 }
225
52234ee9 226 kprintf("ACPI FADT: SCI testing interrupt mode ...\n");
c098b394
SZ
227 if (acpi_sci_mode1 != NULL) {
228 if (acpi_sci_test(acpi_sci_mode1))
229 return;
230 }
52234ee9
SZ
231 for (mode = acpi_sci_modes; mode->sci_trig != INTR_TRIGGER_CONFORM;
232 ++mode) {
c098b394
SZ
233 if (mode == acpi_sci_mode1)
234 continue;
235 if (acpi_sci_test(mode))
52234ee9 236 return;
5e2a2352 237 }
52234ee9
SZ
238
239 kprintf("ACPI FADT: no suitable interrupt mode for SCI, disable\n");
240 acpi_sci_irq = -1;
5e2a2352
SZ
241}
242
243int
244acpi_sci_enabled(void)
245{
246 if (acpi_sci_irq >= 0)
247 return 1;
248 else
249 return 0;
250}
251
252int
737d9e74 253acpi_sci_pci_shareable(void)
5e2a2352
SZ
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}
9dba15ae
SZ
262
263int
264acpi_sci_irqno(void)
265{
266 return acpi_sci_irq;
267}
c098b394
SZ
268
269void
270acpi_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}