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