acpi_machdep.c revision 1.4 1 /* $NetBSD: acpi_machdep.c,v 1.4 2012/09/23 00:31:05 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.4 2012/09/23 00:31:05 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 ACPI_STATUS
97 acpi_md_OsInstallInterruptHandler(uint32_t InterruptNumber,
98 ACPI_OSD_HANDLER ServiceRoutine, void *Context, void **cookiep)
99 {
100 void *ih;
101 struct pic *pic;
102 #if NIOAPIC > 0
103 struct ioapic_softc *sc;
104 #endif
105 int irq, pin, trigger;
106
107 #if NIOAPIC > 0
108 /*
109 * Can only match on ACPI global interrupt numbers if the ACPI
110 * interrupt info was extracted, which is in the ACPI case.
111 */
112 if (mpacpi_sci_override != NULL) {
113 pic = mpacpi_sci_override->ioapic;
114 pin = mpacpi_sci_override->ioapic_pin;
115 if (mpacpi_sci_override->redir & IOAPIC_REDLO_LEVEL)
116 trigger = IST_LEVEL;
117 else
118 trigger = IST_EDGE;
119 if (pic->pic_type == PIC_IOAPIC)
120 irq = -1;
121 else
122 irq = (int)InterruptNumber;
123 goto sci_override;
124 }
125 #endif
126
127 /*
128 * There was no ACPI interrupt source override,
129 *
130 * If the interrupt is handled via IOAPIC, mark it
131 * as level-triggered, active low in the table.
132 */
133
134 #if NIOAPIC > 0
135 sc = ioapic_find_bybase(InterruptNumber);
136 if (sc != NULL) {
137 pic = &sc->sc_pic;
138 struct mp_intr_map *mip;
139
140 if (pic->pic_type == PIC_IOAPIC) {
141 pin = (int)InterruptNumber - pic->pic_vecbase;
142 irq = -1;
143 } else {
144 irq = pin = (int)InterruptNumber;
145 }
146
147 mip = sc->sc_pins[pin].ip_map;
148 if (mip) {
149 mip->flags &= ~3;
150 mip->flags |= MPS_INTPO_ACTLO;
151 mip->redir |= IOAPIC_REDLO_ACTLO;
152 }
153 } else
154 #endif
155 {
156 pic = &i8259_pic;
157 irq = pin = (int)InterruptNumber;
158 }
159
160 trigger = IST_LEVEL;
161
162 #if NIOAPIC > 0
163 sci_override:
164 #endif
165
166 /*
167 * XXX probably, IPL_BIO is enough.
168 */
169 ih = intr_establish(irq, pic, pin, trigger, IPL_TTY,
170 (int (*)(void *)) ServiceRoutine, Context, false);
171
172 if (ih == NULL)
173 return AE_NO_MEMORY;
174
175 *cookiep = ih;
176
177 return AE_OK;
178 }
179
180 void
181 acpi_md_OsRemoveInterruptHandler(void *cookie)
182 {
183 intr_disestablish(cookie);
184 }
185
186 ACPI_STATUS
187 acpi_md_OsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress,
188 uint32_t Length, void **LogicalAddress)
189 {
190 int rv;
191
192 rv = _x86_memio_map(x86_bus_space_mem, PhysicalAddress,
193 Length, 0, (bus_space_handle_t *)LogicalAddress);
194
195 return (rv != 0) ? AE_NO_MEMORY : AE_OK;
196 }
197
198 void
199 acpi_md_OsUnmapMemory(void *LogicalAddress, uint32_t Length)
200 {
201 (void) _x86_memio_unmap(x86_bus_space_mem,
202 (bus_space_handle_t)LogicalAddress, Length, NULL);
203 }
204
205 ACPI_STATUS
206 acpi_md_OsGetPhysicalAddress(void *LogicalAddress,
207 ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
208 {
209 paddr_t pa;
210
211 if (pmap_extract(pmap_kernel(), (vaddr_t) LogicalAddress, &pa)) {
212 *PhysicalAddress = pa;
213 return AE_OK;
214 }
215
216 return AE_ERROR;
217 }
218
219 BOOLEAN
220 acpi_md_OsReadable(void *Pointer, uint32_t Length)
221 {
222 BOOLEAN rv = TRUE;
223 vaddr_t sva, eva;
224 pt_entry_t *pte;
225
226 sva = trunc_page((vaddr_t) Pointer);
227 eva = round_page((vaddr_t) Pointer + Length);
228
229 if (sva < VM_MIN_KERNEL_ADDRESS)
230 return FALSE;
231
232 for (; sva < eva; sva += PAGE_SIZE) {
233 pte = kvtopte(sva);
234 if ((*pte & PG_V) == 0) {
235 rv = FALSE;
236 break;
237 }
238 }
239
240 return rv;
241 }
242
243 BOOLEAN
244 acpi_md_OsWritable(void *Pointer, uint32_t Length)
245 {
246 BOOLEAN rv = FALSE;
247 vaddr_t sva, eva;
248 pt_entry_t *pte;
249
250 sva = trunc_page((vaddr_t) Pointer);
251 eva = round_page((vaddr_t) Pointer + Length);
252
253 if (sva < VM_MIN_KERNEL_ADDRESS)
254 return FALSE;
255
256 for (; sva < eva; sva += PAGE_SIZE) {
257 pte = kvtopte(sva);
258 if ((*pte & (PG_V|PG_W)) != (PG_V|PG_W)) {
259 rv = FALSE;
260 break;
261 }
262 }
263
264 return rv;
265 }
266
267 void
268 acpi_md_OsDisableInterrupt(void)
269 {
270 x86_disable_intr();
271 }
272
273 void
274 acpi_md_OsEnableInterrupt(void)
275 {
276 x86_enable_intr();
277 }
278
279 uint32_t
280 acpi_md_ncpus(void)
281 {
282 return kcpuset_countset(kcpuset_attached);
283 }
284
285 void
286 acpi_md_callback(struct acpi_softc *sc)
287 {
288 #ifdef MPBIOS
289 if (!mpbios_scanned)
290 #endif
291 mpacpi_find_interrupts(sc);
292
293 #ifndef XEN
294 acpi_md_sleep_init();
295 #endif
296 }
297