acpi_machdep.c revision 1.5 1 /* $NetBSD: acpi_machdep.c,v 1.5 2013/03/25 01:30:37 chs Exp $ */
2
3 /*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Machine-dependent routines for ACPICA.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: acpi_machdep.c,v 1.5 2013/03/25 01:30:37 chs Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/bus.h>
48 #include <sys/cpu.h>
49 #include <sys/device.h>
50
51 #include <uvm/uvm_extern.h>
52
53 #include <machine/cpufunc.h>
54
55 #include <dev/acpi/acpica.h>
56 #include <dev/acpi/acpivar.h>
57
58 #include <machine/acpi_machdep.h>
59 #include <machine/mpbiosvar.h>
60 #include <machine/mpacpi.h>
61 #include <machine/i82093reg.h>
62 #include <machine/i82093var.h>
63 #include <machine/pic.h>
64
65 #include <dev/pci/pcivar.h>
66
67 #include <dev/isa/isareg.h>
68 #include <dev/isa/isavar.h>
69
70 #include "ioapic.h"
71
72 #include "acpica.h"
73 #include "opt_mpbios.h"
74 #include "opt_acpi.h"
75
76 ACPI_STATUS
77 acpi_md_OsInitialize(void)
78 {
79 return AE_OK;
80 }
81
82 ACPI_PHYSICAL_ADDRESS
83 acpi_md_OsGetRootPointer(void)
84 {
85 ACPI_PHYSICAL_ADDRESS PhysicalAddress;
86 ACPI_STATUS Status;
87
88 Status = AcpiFindRootPointer(&PhysicalAddress);
89
90 if (ACPI_FAILURE(Status))
91 PhysicalAddress = 0;
92
93 return PhysicalAddress;
94 }
95
96 struct acpi_md_override {
97 int irq;
98 int pin;
99 int flags;
100 };
101
102 static ACPI_STATUS
103 acpi_md_findoverride(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
104 {
105 ACPI_MADT_INTERRUPT_OVERRIDE *iop;
106 struct acpi_md_override *ovrp;
107
108 if (hdrp->Type != ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
109 return AE_OK;
110 }
111
112 iop = (void *)hdrp;
113 ovrp = aux;
114 if (iop->SourceIrq == ovrp->irq) {
115 ovrp->pin = iop->GlobalIrq;
116 ovrp->flags = iop->IntiFlags;
117 }
118 return AE_OK;
119 }
120
121 ACPI_STATUS
122 acpi_md_OsInstallInterruptHandler(uint32_t InterruptNumber,
123 ACPI_OSD_HANDLER ServiceRoutine, void *Context, void **cookiep)
124 {
125 void *ih;
126 struct pic *pic;
127 #if NIOAPIC > 0
128 struct ioapic_softc *sc;
129 struct acpi_md_override ovr;
130 struct mp_intr_map tmpmap, *mip, **mipp = NULL;
131 #endif
132 int irq, pin, type, redir, mpflags;
133
134 /*
135 * ACPI interrupts default to level-triggered active-low.
136 */
137
138 type = IST_LEVEL;
139 mpflags = (MPS_INTTR_LEVEL << 2) | MPS_INTPO_ACTLO;
140 redir = IOAPIC_REDLO_LEVEL | IOAPIC_REDLO_ACTLO;
141
142 #if NIOAPIC > 0
143
144 /*
145 * Apply any MADT override setting.
146 */
147
148 ovr.irq = InterruptNumber;
149 ovr.pin = -1;
150 if (acpi_madt_map() == AE_OK) {
151 acpi_madt_walk(acpi_md_findoverride, &ovr);
152 acpi_madt_unmap();
153 } else {
154 aprint_debug("acpi_madt_map() failed, can't check for MADT override\n");
155 }
156
157 if (ovr.pin != -1) {
158 bool sci = InterruptNumber == AcpiGbl_FADT.SciInterrupt;
159 int polarity = ovr.flags & ACPI_MADT_POLARITY_MASK;
160 int trigger = ovr.flags & ACPI_MADT_TRIGGER_MASK;
161
162 InterruptNumber = ovr.pin;
163 if (polarity == ACPI_MADT_POLARITY_ACTIVE_HIGH ||
164 (!sci && polarity == ACPI_MADT_POLARITY_CONFORMS)) {
165 mpflags &= ~MPS_INTPO_ACTLO;
166 redir &= ~IOAPIC_REDLO_ACTLO;
167 }
168 if (trigger == ACPI_MADT_TRIGGER_EDGE ||
169 (!sci && trigger == ACPI_MADT_TRIGGER_CONFORMS)) {
170 type = IST_EDGE;
171 mpflags &= ~(MPS_INTTR_LEVEL << 2);
172 redir &= ~IOAPIC_REDLO_LEVEL;
173 }
174 }
175
176 /*
177 * If the interrupt is handled via IOAPIC, update the map.
178 * If the map isn't set up yet, install a temporary one.
179 */
180
181 sc = ioapic_find_bybase(InterruptNumber);
182 if (sc != NULL) {
183 pic = &sc->sc_pic;
184
185 if (pic->pic_type == PIC_IOAPIC) {
186 pin = (int)InterruptNumber - pic->pic_vecbase;
187 irq = -1;
188 } else {
189 irq = pin = (int)InterruptNumber;
190 }
191
192 mip = sc->sc_pins[pin].ip_map;
193 if (mip) {
194 mip->flags &= ~0xf;
195 mip->flags |= mpflags;
196 mip->redir &= ~(IOAPIC_REDLO_LEVEL |
197 IOAPIC_REDLO_ACTLO);
198 mip->redir |= redir;
199 } else {
200 mipp = &sc->sc_pins[pin].ip_map;
201 *mipp = &tmpmap;
202 tmpmap.redir = redir;
203 }
204 } else
205 #endif
206 {
207 pic = &i8259_pic;
208 irq = pin = (int)InterruptNumber;
209 }
210
211 /*
212 * XXX probably, IPL_BIO is enough.
213 */
214 ih = intr_establish(irq, pic, pin, type, IPL_TTY,
215 (int (*)(void *)) ServiceRoutine, Context, false);
216
217 #if NIOAPIC > 0
218 if (mipp) {
219 *mipp = NULL;
220 }
221 #endif
222
223 if (ih == NULL)
224 return AE_NO_MEMORY;
225
226 *cookiep = ih;
227
228 return AE_OK;
229 }
230
231 void
232 acpi_md_OsRemoveInterruptHandler(void *cookie)
233 {
234 intr_disestablish(cookie);
235 }
236
237 ACPI_STATUS
238 acpi_md_OsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress,
239 uint32_t Length, void **LogicalAddress)
240 {
241 int rv;
242
243 rv = _x86_memio_map(x86_bus_space_mem, PhysicalAddress,
244 Length, 0, (bus_space_handle_t *)LogicalAddress);
245
246 return (rv != 0) ? AE_NO_MEMORY : AE_OK;
247 }
248
249 void
250 acpi_md_OsUnmapMemory(void *LogicalAddress, uint32_t Length)
251 {
252 (void) _x86_memio_unmap(x86_bus_space_mem,
253 (bus_space_handle_t)LogicalAddress, Length, NULL);
254 }
255
256 ACPI_STATUS
257 acpi_md_OsGetPhysicalAddress(void *LogicalAddress,
258 ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
259 {
260 paddr_t pa;
261
262 if (pmap_extract(pmap_kernel(), (vaddr_t) LogicalAddress, &pa)) {
263 *PhysicalAddress = pa;
264 return AE_OK;
265 }
266
267 return AE_ERROR;
268 }
269
270 BOOLEAN
271 acpi_md_OsReadable(void *Pointer, uint32_t Length)
272 {
273 BOOLEAN rv = TRUE;
274 vaddr_t sva, eva;
275 pt_entry_t *pte;
276
277 sva = trunc_page((vaddr_t) Pointer);
278 eva = round_page((vaddr_t) Pointer + Length);
279
280 if (sva < VM_MIN_KERNEL_ADDRESS)
281 return FALSE;
282
283 for (; sva < eva; sva += PAGE_SIZE) {
284 pte = kvtopte(sva);
285 if ((*pte & PG_V) == 0) {
286 rv = FALSE;
287 break;
288 }
289 }
290
291 return rv;
292 }
293
294 BOOLEAN
295 acpi_md_OsWritable(void *Pointer, uint32_t Length)
296 {
297 BOOLEAN rv = FALSE;
298 vaddr_t sva, eva;
299 pt_entry_t *pte;
300
301 sva = trunc_page((vaddr_t) Pointer);
302 eva = round_page((vaddr_t) Pointer + Length);
303
304 if (sva < VM_MIN_KERNEL_ADDRESS)
305 return FALSE;
306
307 for (; sva < eva; sva += PAGE_SIZE) {
308 pte = kvtopte(sva);
309 if ((*pte & (PG_V|PG_W)) != (PG_V|PG_W)) {
310 rv = FALSE;
311 break;
312 }
313 }
314
315 return rv;
316 }
317
318 void
319 acpi_md_OsDisableInterrupt(void)
320 {
321 x86_disable_intr();
322 }
323
324 void
325 acpi_md_OsEnableInterrupt(void)
326 {
327 x86_enable_intr();
328 }
329
330 uint32_t
331 acpi_md_ncpus(void)
332 {
333 return kcpuset_countset(kcpuset_attached);
334 }
335
336 void
337 acpi_md_callback(struct acpi_softc *sc)
338 {
339 #ifdef MPBIOS
340 if (!mpbios_scanned)
341 #endif
342 mpacpi_find_interrupts(sc);
343
344 #ifndef XEN
345 acpi_md_sleep_init();
346 #endif
347 }
348