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