acpi_machdep.c revision 1.10 1 1.10 christos /* $NetBSD: acpi_machdep.c,v 1.10 2015/10/06 15:06:05 christos Exp $ */
2 1.1 jruoho
3 1.1 jruoho /*
4 1.1 jruoho * Copyright 2001 Wasabi Systems, Inc.
5 1.1 jruoho * All rights reserved.
6 1.1 jruoho *
7 1.1 jruoho * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 jruoho *
9 1.1 jruoho * Redistribution and use in source and binary forms, with or without
10 1.1 jruoho * modification, are permitted provided that the following conditions
11 1.1 jruoho * are met:
12 1.1 jruoho * 1. Redistributions of source code must retain the above copyright
13 1.1 jruoho * notice, this list of conditions and the following disclaimer.
14 1.1 jruoho * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jruoho * notice, this list of conditions and the following disclaimer in the
16 1.1 jruoho * documentation and/or other materials provided with the distribution.
17 1.1 jruoho * 3. All advertising materials mentioning features or use of this software
18 1.1 jruoho * must display the following acknowledgement:
19 1.1 jruoho * This product includes software developed for the NetBSD Project by
20 1.1 jruoho * Wasabi Systems, Inc.
21 1.1 jruoho * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 jruoho * or promote products derived from this software without specific prior
23 1.1 jruoho * written permission.
24 1.1 jruoho *
25 1.1 jruoho * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 jruoho * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 jruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 jruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 jruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 jruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 jruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 jruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 jruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 jruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 jruoho * POSSIBILITY OF SUCH DAMAGE.
36 1.1 jruoho */
37 1.1 jruoho
38 1.1 jruoho /*
39 1.1 jruoho * Machine-dependent routines for ACPICA.
40 1.1 jruoho */
41 1.1 jruoho
42 1.1 jruoho #include <sys/cdefs.h>
43 1.10 christos __KERNEL_RCSID(0, "$NetBSD: acpi_machdep.c,v 1.10 2015/10/06 15:06:05 christos Exp $");
44 1.1 jruoho
45 1.1 jruoho #include <sys/param.h>
46 1.1 jruoho #include <sys/systm.h>
47 1.2 dyoung #include <sys/bus.h>
48 1.3 rmind #include <sys/cpu.h>
49 1.1 jruoho #include <sys/device.h>
50 1.1 jruoho
51 1.1 jruoho #include <uvm/uvm_extern.h>
52 1.1 jruoho
53 1.1 jruoho #include <machine/cpufunc.h>
54 1.9 msaitoh #include <machine/bootinfo.h>
55 1.1 jruoho
56 1.1 jruoho #include <dev/acpi/acpica.h>
57 1.1 jruoho #include <dev/acpi/acpivar.h>
58 1.9 msaitoh #include <dev/acpi/acpi_mcfg.h>
59 1.1 jruoho
60 1.1 jruoho #include <machine/acpi_machdep.h>
61 1.1 jruoho #include <machine/mpbiosvar.h>
62 1.1 jruoho #include <machine/mpacpi.h>
63 1.1 jruoho #include <machine/i82093reg.h>
64 1.1 jruoho #include <machine/i82093var.h>
65 1.1 jruoho #include <machine/pic.h>
66 1.1 jruoho
67 1.1 jruoho #include <dev/pci/pcivar.h>
68 1.1 jruoho
69 1.1 jruoho #include <dev/isa/isareg.h>
70 1.1 jruoho #include <dev/isa/isavar.h>
71 1.1 jruoho
72 1.1 jruoho #include "ioapic.h"
73 1.1 jruoho
74 1.1 jruoho #include "acpica.h"
75 1.1 jruoho #include "opt_mpbios.h"
76 1.1 jruoho #include "opt_acpi.h"
77 1.1 jruoho
78 1.1 jruoho ACPI_STATUS
79 1.1 jruoho acpi_md_OsInitialize(void)
80 1.1 jruoho {
81 1.1 jruoho return AE_OK;
82 1.1 jruoho }
83 1.1 jruoho
84 1.1 jruoho ACPI_PHYSICAL_ADDRESS
85 1.1 jruoho acpi_md_OsGetRootPointer(void)
86 1.1 jruoho {
87 1.1 jruoho ACPI_PHYSICAL_ADDRESS PhysicalAddress;
88 1.1 jruoho ACPI_STATUS Status;
89 1.1 jruoho
90 1.1 jruoho Status = AcpiFindRootPointer(&PhysicalAddress);
91 1.1 jruoho
92 1.1 jruoho if (ACPI_FAILURE(Status))
93 1.1 jruoho PhysicalAddress = 0;
94 1.1 jruoho
95 1.1 jruoho return PhysicalAddress;
96 1.1 jruoho }
97 1.1 jruoho
98 1.5 chs struct acpi_md_override {
99 1.5 chs int irq;
100 1.5 chs int pin;
101 1.5 chs int flags;
102 1.5 chs };
103 1.5 chs
104 1.8 joerg #if NIOAPIC > 0
105 1.5 chs static ACPI_STATUS
106 1.5 chs acpi_md_findoverride(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
107 1.5 chs {
108 1.5 chs ACPI_MADT_INTERRUPT_OVERRIDE *iop;
109 1.5 chs struct acpi_md_override *ovrp;
110 1.5 chs
111 1.5 chs if (hdrp->Type != ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
112 1.5 chs return AE_OK;
113 1.5 chs }
114 1.5 chs
115 1.5 chs iop = (void *)hdrp;
116 1.5 chs ovrp = aux;
117 1.5 chs if (iop->SourceIrq == ovrp->irq) {
118 1.5 chs ovrp->pin = iop->GlobalIrq;
119 1.5 chs ovrp->flags = iop->IntiFlags;
120 1.5 chs }
121 1.5 chs return AE_OK;
122 1.5 chs }
123 1.8 joerg #endif
124 1.5 chs
125 1.1 jruoho ACPI_STATUS
126 1.1 jruoho acpi_md_OsInstallInterruptHandler(uint32_t InterruptNumber,
127 1.1 jruoho ACPI_OSD_HANDLER ServiceRoutine, void *Context, void **cookiep)
128 1.1 jruoho {
129 1.1 jruoho void *ih;
130 1.1 jruoho struct pic *pic;
131 1.1 jruoho #if NIOAPIC > 0
132 1.1 jruoho struct ioapic_softc *sc;
133 1.5 chs struct acpi_md_override ovr;
134 1.5 chs struct mp_intr_map tmpmap, *mip, **mipp = NULL;
135 1.1 jruoho #endif
136 1.5 chs int irq, pin, type, redir, mpflags;
137 1.5 chs
138 1.5 chs /*
139 1.5 chs * ACPI interrupts default to level-triggered active-low.
140 1.5 chs */
141 1.5 chs
142 1.5 chs type = IST_LEVEL;
143 1.5 chs mpflags = (MPS_INTTR_LEVEL << 2) | MPS_INTPO_ACTLO;
144 1.5 chs redir = IOAPIC_REDLO_LEVEL | IOAPIC_REDLO_ACTLO;
145 1.1 jruoho
146 1.1 jruoho #if NIOAPIC > 0
147 1.5 chs
148 1.1 jruoho /*
149 1.5 chs * Apply any MADT override setting.
150 1.1 jruoho */
151 1.5 chs
152 1.5 chs ovr.irq = InterruptNumber;
153 1.5 chs ovr.pin = -1;
154 1.5 chs if (acpi_madt_map() == AE_OK) {
155 1.5 chs acpi_madt_walk(acpi_md_findoverride, &ovr);
156 1.5 chs acpi_madt_unmap();
157 1.5 chs } else {
158 1.5 chs aprint_debug("acpi_madt_map() failed, can't check for MADT override\n");
159 1.5 chs }
160 1.5 chs
161 1.5 chs if (ovr.pin != -1) {
162 1.5 chs bool sci = InterruptNumber == AcpiGbl_FADT.SciInterrupt;
163 1.5 chs int polarity = ovr.flags & ACPI_MADT_POLARITY_MASK;
164 1.5 chs int trigger = ovr.flags & ACPI_MADT_TRIGGER_MASK;
165 1.5 chs
166 1.5 chs InterruptNumber = ovr.pin;
167 1.5 chs if (polarity == ACPI_MADT_POLARITY_ACTIVE_HIGH ||
168 1.5 chs (!sci && polarity == ACPI_MADT_POLARITY_CONFORMS)) {
169 1.5 chs mpflags &= ~MPS_INTPO_ACTLO;
170 1.6 chs mpflags |= MPS_INTPO_ACTHI;
171 1.5 chs redir &= ~IOAPIC_REDLO_ACTLO;
172 1.5 chs }
173 1.5 chs if (trigger == ACPI_MADT_TRIGGER_EDGE ||
174 1.5 chs (!sci && trigger == ACPI_MADT_TRIGGER_CONFORMS)) {
175 1.5 chs type = IST_EDGE;
176 1.5 chs mpflags &= ~(MPS_INTTR_LEVEL << 2);
177 1.6 chs mpflags |= (MPS_INTTR_EDGE << 2);
178 1.5 chs redir &= ~IOAPIC_REDLO_LEVEL;
179 1.5 chs }
180 1.1 jruoho }
181 1.1 jruoho
182 1.1 jruoho /*
183 1.5 chs * If the interrupt is handled via IOAPIC, update the map.
184 1.5 chs * If the map isn't set up yet, install a temporary one.
185 1.1 jruoho */
186 1.1 jruoho
187 1.1 jruoho sc = ioapic_find_bybase(InterruptNumber);
188 1.1 jruoho if (sc != NULL) {
189 1.1 jruoho pic = &sc->sc_pic;
190 1.1 jruoho
191 1.1 jruoho if (pic->pic_type == PIC_IOAPIC) {
192 1.1 jruoho pin = (int)InterruptNumber - pic->pic_vecbase;
193 1.1 jruoho irq = -1;
194 1.1 jruoho } else {
195 1.1 jruoho irq = pin = (int)InterruptNumber;
196 1.1 jruoho }
197 1.1 jruoho
198 1.1 jruoho mip = sc->sc_pins[pin].ip_map;
199 1.1 jruoho if (mip) {
200 1.5 chs mip->flags &= ~0xf;
201 1.5 chs mip->flags |= mpflags;
202 1.5 chs mip->redir &= ~(IOAPIC_REDLO_LEVEL |
203 1.5 chs IOAPIC_REDLO_ACTLO);
204 1.5 chs mip->redir |= redir;
205 1.5 chs } else {
206 1.5 chs mipp = &sc->sc_pins[pin].ip_map;
207 1.5 chs *mipp = &tmpmap;
208 1.5 chs tmpmap.redir = redir;
209 1.6 chs tmpmap.flags = mpflags;
210 1.1 jruoho }
211 1.1 jruoho } else
212 1.1 jruoho #endif
213 1.1 jruoho {
214 1.1 jruoho pic = &i8259_pic;
215 1.1 jruoho irq = pin = (int)InterruptNumber;
216 1.1 jruoho }
217 1.1 jruoho
218 1.1 jruoho /*
219 1.1 jruoho * XXX probably, IPL_BIO is enough.
220 1.1 jruoho */
221 1.5 chs ih = intr_establish(irq, pic, pin, type, IPL_TTY,
222 1.1 jruoho (int (*)(void *)) ServiceRoutine, Context, false);
223 1.1 jruoho
224 1.5 chs #if NIOAPIC > 0
225 1.5 chs if (mipp) {
226 1.5 chs *mipp = NULL;
227 1.5 chs }
228 1.5 chs #endif
229 1.5 chs
230 1.1 jruoho if (ih == NULL)
231 1.1 jruoho return AE_NO_MEMORY;
232 1.1 jruoho
233 1.1 jruoho *cookiep = ih;
234 1.1 jruoho
235 1.1 jruoho return AE_OK;
236 1.1 jruoho }
237 1.1 jruoho
238 1.1 jruoho void
239 1.1 jruoho acpi_md_OsRemoveInterruptHandler(void *cookie)
240 1.1 jruoho {
241 1.1 jruoho intr_disestablish(cookie);
242 1.1 jruoho }
243 1.1 jruoho
244 1.1 jruoho ACPI_STATUS
245 1.1 jruoho acpi_md_OsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress,
246 1.1 jruoho uint32_t Length, void **LogicalAddress)
247 1.1 jruoho {
248 1.1 jruoho int rv;
249 1.1 jruoho
250 1.1 jruoho rv = _x86_memio_map(x86_bus_space_mem, PhysicalAddress,
251 1.1 jruoho Length, 0, (bus_space_handle_t *)LogicalAddress);
252 1.1 jruoho
253 1.1 jruoho return (rv != 0) ? AE_NO_MEMORY : AE_OK;
254 1.1 jruoho }
255 1.1 jruoho
256 1.1 jruoho void
257 1.1 jruoho acpi_md_OsUnmapMemory(void *LogicalAddress, uint32_t Length)
258 1.1 jruoho {
259 1.1 jruoho (void) _x86_memio_unmap(x86_bus_space_mem,
260 1.1 jruoho (bus_space_handle_t)LogicalAddress, Length, NULL);
261 1.1 jruoho }
262 1.1 jruoho
263 1.1 jruoho ACPI_STATUS
264 1.1 jruoho acpi_md_OsGetPhysicalAddress(void *LogicalAddress,
265 1.1 jruoho ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
266 1.1 jruoho {
267 1.1 jruoho paddr_t pa;
268 1.1 jruoho
269 1.1 jruoho if (pmap_extract(pmap_kernel(), (vaddr_t) LogicalAddress, &pa)) {
270 1.1 jruoho *PhysicalAddress = pa;
271 1.1 jruoho return AE_OK;
272 1.1 jruoho }
273 1.1 jruoho
274 1.1 jruoho return AE_ERROR;
275 1.1 jruoho }
276 1.1 jruoho
277 1.1 jruoho BOOLEAN
278 1.1 jruoho acpi_md_OsReadable(void *Pointer, uint32_t Length)
279 1.1 jruoho {
280 1.1 jruoho BOOLEAN rv = TRUE;
281 1.1 jruoho vaddr_t sva, eva;
282 1.1 jruoho pt_entry_t *pte;
283 1.1 jruoho
284 1.1 jruoho sva = trunc_page((vaddr_t) Pointer);
285 1.1 jruoho eva = round_page((vaddr_t) Pointer + Length);
286 1.1 jruoho
287 1.1 jruoho if (sva < VM_MIN_KERNEL_ADDRESS)
288 1.1 jruoho return FALSE;
289 1.1 jruoho
290 1.1 jruoho for (; sva < eva; sva += PAGE_SIZE) {
291 1.1 jruoho pte = kvtopte(sva);
292 1.1 jruoho if ((*pte & PG_V) == 0) {
293 1.1 jruoho rv = FALSE;
294 1.1 jruoho break;
295 1.1 jruoho }
296 1.1 jruoho }
297 1.1 jruoho
298 1.1 jruoho return rv;
299 1.1 jruoho }
300 1.1 jruoho
301 1.1 jruoho BOOLEAN
302 1.1 jruoho acpi_md_OsWritable(void *Pointer, uint32_t Length)
303 1.1 jruoho {
304 1.7 jakllsch BOOLEAN rv = TRUE;
305 1.1 jruoho vaddr_t sva, eva;
306 1.1 jruoho pt_entry_t *pte;
307 1.1 jruoho
308 1.1 jruoho sva = trunc_page((vaddr_t) Pointer);
309 1.1 jruoho eva = round_page((vaddr_t) Pointer + Length);
310 1.1 jruoho
311 1.1 jruoho if (sva < VM_MIN_KERNEL_ADDRESS)
312 1.1 jruoho return FALSE;
313 1.1 jruoho
314 1.1 jruoho for (; sva < eva; sva += PAGE_SIZE) {
315 1.1 jruoho pte = kvtopte(sva);
316 1.1 jruoho if ((*pte & (PG_V|PG_W)) != (PG_V|PG_W)) {
317 1.1 jruoho rv = FALSE;
318 1.1 jruoho break;
319 1.1 jruoho }
320 1.1 jruoho }
321 1.1 jruoho
322 1.1 jruoho return rv;
323 1.1 jruoho }
324 1.1 jruoho
325 1.1 jruoho void
326 1.1 jruoho acpi_md_OsDisableInterrupt(void)
327 1.1 jruoho {
328 1.1 jruoho x86_disable_intr();
329 1.1 jruoho }
330 1.1 jruoho
331 1.1 jruoho void
332 1.1 jruoho acpi_md_OsEnableInterrupt(void)
333 1.1 jruoho {
334 1.1 jruoho x86_enable_intr();
335 1.1 jruoho }
336 1.1 jruoho
337 1.1 jruoho uint32_t
338 1.1 jruoho acpi_md_ncpus(void)
339 1.1 jruoho {
340 1.3 rmind return kcpuset_countset(kcpuset_attached);
341 1.1 jruoho }
342 1.1 jruoho
343 1.9 msaitoh static bool
344 1.9 msaitoh acpi_md_mcfg_validate(uint64_t addr, int bus_start, int *bus_end)
345 1.9 msaitoh {
346 1.9 msaitoh struct btinfo_memmap *bim;
347 1.9 msaitoh uint64_t size, mapaddr, mapsize;
348 1.9 msaitoh uint32_t type;
349 1.9 msaitoh int i, n;
350 1.9 msaitoh
351 1.9 msaitoh bim = lookup_bootinfo(BTINFO_MEMMAP);
352 1.9 msaitoh if (bim == NULL)
353 1.9 msaitoh return false;
354 1.9 msaitoh
355 1.10 christos size = *bus_end - bus_start + 1;
356 1.10 christos size *= ACPIMCFG_SIZE_PER_BUS;
357 1.9 msaitoh for (i = 0; i < bim->num; i++) {
358 1.9 msaitoh mapaddr = bim->entry[i].addr;
359 1.9 msaitoh mapsize = bim->entry[i].size;
360 1.9 msaitoh type = bim->entry[i].type;
361 1.9 msaitoh
362 1.9 msaitoh aprint_debug("MCFG: MEMMAP: 0x%016" PRIx64 "-0x%016" PRIx64
363 1.9 msaitoh ", size=0x%016" PRIx64 ", type=%d(%s)\n",
364 1.9 msaitoh mapaddr, mapaddr + mapsize - 1, mapsize, type,
365 1.9 msaitoh (type == BIM_Memory) ? "Memory" :
366 1.9 msaitoh (type == BIM_Reserved) ? "Reserved" :
367 1.9 msaitoh (type == BIM_ACPI) ? "ACPI" :
368 1.9 msaitoh (type == BIM_NVS) ? "NVS" :
369 1.9 msaitoh "unknown");
370 1.9 msaitoh
371 1.9 msaitoh switch (type) {
372 1.9 msaitoh case BIM_ACPI:
373 1.9 msaitoh case BIM_Reserved:
374 1.9 msaitoh if (addr < mapaddr || addr >= mapaddr + mapsize)
375 1.9 msaitoh break;
376 1.9 msaitoh
377 1.9 msaitoh /* full map */
378 1.9 msaitoh if (addr + size <= mapaddr + mapsize)
379 1.9 msaitoh return true;
380 1.9 msaitoh
381 1.9 msaitoh /* partial map */
382 1.9 msaitoh n = (mapsize - (addr - mapaddr)) /
383 1.9 msaitoh ACPIMCFG_SIZE_PER_BUS;
384 1.9 msaitoh /* bus_start == bus_end is not allowed. */
385 1.9 msaitoh if (n > 1) {
386 1.9 msaitoh *bus_end = bus_start + n - 1;
387 1.9 msaitoh return true;
388 1.9 msaitoh }
389 1.9 msaitoh aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64
390 1.9 msaitoh ": invalid size: request 0x%016" PRIx64 ", "
391 1.9 msaitoh "actual 0x%016" PRIx64 "\n",
392 1.9 msaitoh bus_start, *bus_end, addr, size, mapsize);
393 1.9 msaitoh break;
394 1.9 msaitoh }
395 1.9 msaitoh }
396 1.9 msaitoh aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64 ": "
397 1.9 msaitoh "no valid region\n", bus_start, *bus_end, addr);
398 1.9 msaitoh return false;
399 1.9 msaitoh }
400 1.9 msaitoh
401 1.9 msaitoh static uint32_t
402 1.9 msaitoh acpi_md_mcfg_read(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr)
403 1.9 msaitoh {
404 1.9 msaitoh vaddr_t va = bsh + addr;
405 1.9 msaitoh uint32_t data = (uint32_t) -1;
406 1.9 msaitoh
407 1.9 msaitoh KASSERT(bst == x86_bus_space_mem);
408 1.9 msaitoh
409 1.9 msaitoh __asm("movl %1, %0" : "=a" (data) : "m" (*(volatile uint32_t *)va));
410 1.9 msaitoh
411 1.9 msaitoh return data;
412 1.9 msaitoh }
413 1.9 msaitoh
414 1.9 msaitoh static void
415 1.9 msaitoh acpi_md_mcfg_write(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr,
416 1.9 msaitoh uint32_t data)
417 1.9 msaitoh {
418 1.9 msaitoh vaddr_t va = bsh + addr;
419 1.9 msaitoh
420 1.9 msaitoh KASSERT(bst == x86_bus_space_mem);
421 1.9 msaitoh
422 1.9 msaitoh __asm("movl %1, %0" : "=m" (*(volatile uint32_t *)va) : "a" (data));
423 1.9 msaitoh }
424 1.9 msaitoh
425 1.9 msaitoh static const struct acpimcfg_ops acpi_md_mcfg_ops = {
426 1.9 msaitoh .ao_validate = acpi_md_mcfg_validate,
427 1.9 msaitoh
428 1.9 msaitoh .ao_read = acpi_md_mcfg_read,
429 1.9 msaitoh .ao_write = acpi_md_mcfg_write,
430 1.9 msaitoh };
431 1.9 msaitoh
432 1.1 jruoho void
433 1.4 chs acpi_md_callback(struct acpi_softc *sc)
434 1.1 jruoho {
435 1.1 jruoho #ifdef MPBIOS
436 1.1 jruoho if (!mpbios_scanned)
437 1.1 jruoho #endif
438 1.4 chs mpacpi_find_interrupts(sc);
439 1.1 jruoho
440 1.1 jruoho #ifndef XEN
441 1.1 jruoho acpi_md_sleep_init();
442 1.1 jruoho #endif
443 1.9 msaitoh
444 1.9 msaitoh acpimcfg_init(x86_bus_space_mem, &acpi_md_mcfg_ops);
445 1.1 jruoho }
446