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