1/* $NetBSD: acpi_machdep.c,v 1.14 2016/10/15 16:46:14 jdolecek 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.14 2016/10/15 16:46:14 jdolecek 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#include <machine/bootinfo.h>
55#include <machine/autoconf.h>
56
57#include <dev/acpi/acpica.h>
58#include <dev/acpi/acpivar.h>
59#include <dev/acpi/acpi_mcfg.h>
60
61#include <machine/acpi_machdep.h>
62#include <machine/mpbiosvar.h>
63#include <machine/mpacpi.h>
64#include <machine/i82093reg.h>
65#include <machine/i82093var.h>
66#include <machine/pic.h>
67
68#include <x86/efi.h>
69
70#include <dev/pci/pcivar.h>
71
72#include <dev/isa/isareg.h>
73#include <dev/isa/isavar.h>
74
75#include "ioapic.h"
76
77#include "acpica.h"
78#include "opt_mpbios.h"
79#include "opt_acpi.h"
80#include "opt_vga.h"
81
82/*
83 * Default VBIOS reset method for non-HW accelerated VGA drivers.
84 */
85#ifdef VGA_POST
86# define VBIOS_RESET_DEFAULT 2
87#else
88# define VBIOS_RESET_DEFAULT 1
89#endif
90
91ACPI_STATUS
92acpi_md_OsInitialize(void)
93{
94 return AE_OK;
95}
96
97ACPI_PHYSICAL_ADDRESS
98acpi_md_OsGetRootPointer(void)
99{
100 ACPI_PHYSICAL_ADDRESS PhysicalAddress;
101 ACPI_STATUS Status;
102
103#ifndef XEN
104 /* If EFI is available, attempt to use it to locate the ACPI table. */
105 if (efi_probe()) {
106 PhysicalAddress = efi_getcfgtblpa(&EFI_UUID_ACPI20);
107 if (!PhysicalAddress)
108 PhysicalAddress = efi_getcfgtblpa(&EFI_UUID_ACPI10);
109 if (PhysicalAddress)
110 return PhysicalAddress;
111 }
112
113#endif
114 Status = AcpiFindRootPointer(&PhysicalAddress);
115 if (ACPI_FAILURE(Status))
116 PhysicalAddress = 0;
117
118 return PhysicalAddress;
119}
120
121struct acpi_md_override {
122 int irq;
123 int pin;
124 int flags;
125};
126
127#if NIOAPIC > 0
128static ACPI_STATUS
129acpi_md_findoverride(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
130{
131 ACPI_MADT_INTERRUPT_OVERRIDE *iop;
132 struct acpi_md_override *ovrp;
133
134 if (hdrp->Type != ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
135 return AE_OK;
136 }
137
138 iop = (void *)hdrp;
139 ovrp = aux;
140 if (iop->SourceIrq == ovrp->irq) {
141 ovrp->pin = iop->GlobalIrq;
142 ovrp->flags = iop->IntiFlags;
143 }
144 return AE_OK;
145}
146#endif
147
148ACPI_STATUS
149acpi_md_OsInstallInterruptHandler(uint32_t InterruptNumber,
150 ACPI_OSD_HANDLER ServiceRoutine, void *Context, void **cookiep)
151{
152 void *ih;
153 struct pic *pic;
154#if NIOAPIC > 0
155 struct ioapic_softc *sc;
156 struct acpi_md_override ovr;
157 struct mp_intr_map tmpmap, *mip, **mipp = NULL;
158#endif
159 int irq, pin, type, redir, mpflags;
160
161 /*
162 * ACPI interrupts default to level-triggered active-low.
163 */
164
165 type = IST_LEVEL;
166 mpflags = (MPS_INTTR_LEVEL << 2) | MPS_INTPO_ACTLO;
167 redir = IOAPIC_REDLO_LEVEL | IOAPIC_REDLO_ACTLO;
168
169#if NIOAPIC > 0
170
171 /*
172 * Apply any MADT override setting.
173 */
174
175 ovr.irq = InterruptNumber;
176 ovr.pin = -1;
177 if (acpi_madt_map() == AE_OK) {
178 acpi_madt_walk(acpi_md_findoverride, &ovr);
179 acpi_madt_unmap();
180 } else {
181 aprint_debug("acpi_madt_map() failed, can't check for MADT override\n");
182 }
183
184 if (ovr.pin != -1) {
185 bool sci = InterruptNumber == AcpiGbl_FADT.SciInterrupt;
186 int polarity = ovr.flags & ACPI_MADT_POLARITY_MASK;
187 int trigger = ovr.flags & ACPI_MADT_TRIGGER_MASK;
188
189 InterruptNumber = ovr.pin;
190 if (polarity == ACPI_MADT_POLARITY_ACTIVE_HIGH ||
191 (!sci && polarity == ACPI_MADT_POLARITY_CONFORMS)) {
192 mpflags &= ~MPS_INTPO_ACTLO;
193 mpflags |= MPS_INTPO_ACTHI;
194 redir &= ~IOAPIC_REDLO_ACTLO;
195 }
196 if (trigger == ACPI_MADT_TRIGGER_EDGE ||
197 (!sci && trigger == ACPI_MADT_TRIGGER_CONFORMS)) {
198 type = IST_EDGE;
199 mpflags &= ~(MPS_INTTR_LEVEL << 2);
200 mpflags |= (MPS_INTTR_EDGE << 2);
201 redir &= ~IOAPIC_REDLO_LEVEL;
202 }
203 }
204
205 /*
206 * If the interrupt is handled via IOAPIC, update the map.
207 * If the map isn't set up yet, install a temporary one.
208 */
209
210 sc = ioapic_find_bybase(InterruptNumber);
211 if (sc != NULL) {
212 pic = &sc->sc_pic;
213
214 if (pic->pic_type == PIC_IOAPIC) {
215 pin = (int)InterruptNumber - pic->pic_vecbase;
216 irq = -1;
217 } else {
218 irq = pin = (int)InterruptNumber;
219 }
220
221 mip = sc->sc_pins[pin].ip_map;
222 if (mip) {
223 mip->flags &= ~0xf;
224 mip->flags |= mpflags;
225 mip->redir &= ~(IOAPIC_REDLO_LEVEL |
226 IOAPIC_REDLO_ACTLO);
227 mip->redir |= redir;
228 } else {
229 mipp = &sc->sc_pins[pin].ip_map;
230 *mipp = &tmpmap;
231 tmpmap.redir = redir;
232 tmpmap.flags = mpflags;
233 }
234 } else
235#endif
236 {
237 pic = &i8259_pic;
238 irq = pin = (int)InterruptNumber;
239 }
240
241 /*
242 * XXX probably, IPL_BIO is enough.
243 */
244 ih = intr_establish_xname(irq, pic, pin, type, IPL_TTY,
245 (int (*)(void *)) ServiceRoutine, Context, false, "acpi SCI");
246
247#if NIOAPIC > 0
248 if (mipp) {
249 *mipp = NULL;
250 }
251#endif
252
253 if (ih == NULL)
254 return AE_NO_MEMORY;
255
256 *cookiep = ih;
257
258 return AE_OK;
259}
260
261void
262acpi_md_OsRemoveInterruptHandler(void *cookie)
263{
264 intr_disestablish(cookie);
265}
266
267ACPI_STATUS
268acpi_md_OsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress,
269 uint32_t Length, void **LogicalAddress)
270{
271 int rv;
272
273 rv = _x86_memio_map(x86_bus_space_mem, PhysicalAddress,
274 Length, 0, (bus_space_handle_t *)LogicalAddress);
275
276 return (rv != 0) ? AE_NO_MEMORY : AE_OK;
277}
278
279void
280acpi_md_OsUnmapMemory(void *LogicalAddress, uint32_t Length)
281{
282 (void) _x86_memio_unmap(x86_bus_space_mem,
283 (bus_space_handle_t)LogicalAddress, Length, NULL);
284}
285
286ACPI_STATUS
287acpi_md_OsGetPhysicalAddress(void *LogicalAddress,
288 ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
289{
290 paddr_t pa;
291
292 if (pmap_extract(pmap_kernel(), (vaddr_t) LogicalAddress, &pa)) {
293 *PhysicalAddress = pa;
294 return AE_OK;
295 }
296
297 return AE_ERROR;
298}
299
300BOOLEAN
301acpi_md_OsReadable(void *Pointer, uint32_t Length)
302{
303 BOOLEAN rv = TRUE;
304 vaddr_t sva, eva;
305 pt_entry_t *pte;
306
307 sva = trunc_page((vaddr_t) Pointer);
308 eva = round_page((vaddr_t) Pointer + Length);
309
310 if (sva < VM_MIN_KERNEL_ADDRESS)
311 return FALSE;
312
313 for (; sva < eva; sva += PAGE_SIZE) {
314 pte = kvtopte(sva);
315 if ((*pte & PG_V) == 0) {
316 rv = FALSE;
317 break;
318 }
319 }
320
321 return rv;
322}
323
324BOOLEAN
325acpi_md_OsWritable(void *Pointer, uint32_t Length)
326{
327 BOOLEAN rv = TRUE;
328 vaddr_t sva, eva;
329 pt_entry_t *pte;
330
331 sva = trunc_page((vaddr_t) Pointer);
332 eva = round_page((vaddr_t) Pointer + Length);
333
334 if (sva < VM_MIN_KERNEL_ADDRESS)
335 return FALSE;
336
337 for (; sva < eva; sva += PAGE_SIZE) {
338 pte = kvtopte(sva);
339 if ((*pte & (PG_V|PG_W)) != (PG_V|PG_W)) {
340 rv = FALSE;
341 break;
342 }
343 }
344
345 return rv;
346}
347
348void
349acpi_md_OsDisableInterrupt(void)
350{
351 x86_disable_intr();
352}
353
354void
355acpi_md_OsEnableInterrupt(void)
356{
357 x86_enable_intr();
358}
359
360uint32_t
361acpi_md_ncpus(void)
362{
363 return kcpuset_countset(kcpuset_attached);
364}
365
366static bool
367acpi_md_mcfg_validate(uint64_t addr, int bus_start, int *bus_end)
368{
369 struct btinfo_memmap *bim;
370 uint64_t size, mapaddr, mapsize;
371 uint32_t type;
372 int i, n;
373
374 bim = lookup_bootinfo(BTINFO_MEMMAP);
375 if (bim == NULL)
376 return false;
377
378 size = *bus_end - bus_start + 1;
379 size *= ACPIMCFG_SIZE_PER_BUS;
380 for (i = 0; i < bim->num; i++) {
381 mapaddr = bim->entry[i].addr;
382 mapsize = bim->entry[i].size;
383 type = bim->entry[i].type;
384
385 aprint_debug("MCFG: MEMMAP: 0x%016" PRIx64 "-0x%016" PRIx64
386 ", size=0x%016" PRIx64 ", type=%d(%s)\n",
387 mapaddr, mapaddr + mapsize - 1, mapsize, type,
388 (type == BIM_Memory) ? "Memory" :
389 (type == BIM_Reserved) ? "Reserved" :
390 (type == BIM_ACPI) ? "ACPI" :
391 (type == BIM_NVS) ? "NVS" :
392 "unknown");
393
394 switch (type) {
395 case BIM_ACPI:
396 case BIM_Reserved:
397 if (addr < mapaddr || addr >= mapaddr + mapsize)
398 break;
399
400 /* full map */
401 if (addr + size <= mapaddr + mapsize)
402 return true;
403
404 /* partial map */
405 n = (mapsize - (addr - mapaddr)) /
406 ACPIMCFG_SIZE_PER_BUS;
407 /* bus_start == bus_end is not allowed. */
408 if (n > 1) {
409 *bus_end = bus_start + n - 1;
410 return true;
411 }
412 aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64
413 ": invalid size: request 0x%016" PRIx64 ", "
414 "actual 0x%016" PRIx64 "\n",
415 bus_start, *bus_end, addr, size, mapsize);
416 break;
417 }
418 }
419 aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64 ": "
420 "no valid region\n", bus_start, *bus_end, addr);
421 return false;
422}
423
424static uint32_t
425acpi_md_mcfg_read(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr)
426{
427 vaddr_t va = bsh + addr;
428 uint32_t data = (uint32_t) -1;
429
430 KASSERT(bst == x86_bus_space_mem);
431
432 __asm("movl %1, %0" : "=a" (data) : "m" (*(volatile uint32_t *)va));
433
434 return data;
435}
436
437static void
438acpi_md_mcfg_write(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr,
439 uint32_t data)
440{
441 vaddr_t va = bsh + addr;
442
443 KASSERT(bst == x86_bus_space_mem);
444
445 __asm("movl %1, %0" : "=m" (*(volatile uint32_t *)va) : "a" (data));
446}
447
448static const struct acpimcfg_ops acpi_md_mcfg_ops = {
449 .ao_validate = acpi_md_mcfg_validate,
450
451 .ao_read = acpi_md_mcfg_read,
452 .ao_write = acpi_md_mcfg_write,
453};
454
455void
456acpi_md_callback(struct acpi_softc *sc)
457{
458#ifdef MPBIOS
459 if (!mpbios_scanned)
460#endif
461 mpacpi_find_interrupts(sc);
462
463#ifndef XEN
464 acpi_md_sleep_init();
465#endif
466
467 acpimcfg_init(x86_bus_space_mem, &acpi_md_mcfg_ops);
468}
469
470#ifndef XEN
471void
472device_acpi_register(device_t dev, void *aux)
473{
474 device_t parent;
475 bool device_is_vga, device_is_pci, device_is_isa;
476
477 parent = device_parent(dev);
478 if (parent == NULL)
479 return;
480
481 device_is_vga = device_is_a(dev, "vga") || device_is_a(dev, "genfb");
482 device_is_pci = device_is_a(parent, "pci");
483 device_is_isa = device_is_a(parent, "isa");
484
485 if (device_is_vga && (device_is_pci || device_is_isa)) {
486 extern int acpi_md_vbios_reset;
487
488 acpi_md_vbios_reset = VBIOS_RESET_DEFAULT;
489 }
490}
491#endif
492