Home | History | Annotate | Line # | Download | only in acpi
acpi_machdep.c revision 1.20
      1 /* $NetBSD: acpi_machdep.c,v 1.20 2018/11/16 23:03:55 jmcneill 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.20 2018/11/16 23:03:55 jmcneill 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 
     91 ACPI_STATUS
     92 acpi_md_OsInitialize(void)
     93 {
     94 	return AE_OK;
     95 }
     96 
     97 ACPI_PHYSICAL_ADDRESS
     98 acpi_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 
    121 struct acpi_md_override {
    122 	int irq;
    123 	int pin;
    124 	int flags;
    125 };
    126 
    127 #if NIOAPIC > 0
    128 static ACPI_STATUS
    129 acpi_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 
    148 ACPI_STATUS
    149 acpi_md_OsInstallInterruptHandler(uint32_t InterruptNumber,
    150     ACPI_OSD_HANDLER ServiceRoutine, void *Context, void **cookiep,
    151     const char *xname)
    152 {
    153 	void *ih;
    154 	struct pic *pic;
    155 #if NIOAPIC > 0
    156 	struct ioapic_softc *sc;
    157 	struct acpi_md_override ovr;
    158 	struct mp_intr_map tmpmap, *mip, **mipp = NULL;
    159 #endif
    160 	int irq, pin, type, redir, mpflags;
    161 
    162 	/*
    163 	 * ACPI interrupts default to level-triggered active-low.
    164 	 */
    165 
    166 	type = IST_LEVEL;
    167 	mpflags = (MPS_INTTR_LEVEL << 2) | MPS_INTPO_ACTLO;
    168 	redir = IOAPIC_REDLO_LEVEL | IOAPIC_REDLO_ACTLO;
    169 
    170 #if NIOAPIC > 0
    171 
    172 	/*
    173 	 * Apply any MADT override setting.
    174 	 */
    175 
    176 	ovr.irq = InterruptNumber;
    177 	ovr.pin = -1;
    178 	if (acpi_madt_map() == AE_OK) {
    179 		acpi_madt_walk(acpi_md_findoverride, &ovr);
    180 		acpi_madt_unmap();
    181 	} else {
    182 		aprint_debug("acpi_madt_map() failed, can't check for MADT override\n");
    183 	}
    184 
    185 	if (ovr.pin != -1) {
    186 		bool sci = InterruptNumber == AcpiGbl_FADT.SciInterrupt;
    187 		int polarity = ovr.flags & ACPI_MADT_POLARITY_MASK;
    188 		int trigger = ovr.flags & ACPI_MADT_TRIGGER_MASK;
    189 
    190 		InterruptNumber = ovr.pin;
    191 		if (polarity == ACPI_MADT_POLARITY_ACTIVE_HIGH ||
    192 		    (!sci && polarity == ACPI_MADT_POLARITY_CONFORMS)) {
    193 			mpflags &= ~MPS_INTPO_ACTLO;
    194 			mpflags |= MPS_INTPO_ACTHI;
    195 			redir &= ~IOAPIC_REDLO_ACTLO;
    196 		}
    197 		if (trigger == ACPI_MADT_TRIGGER_EDGE ||
    198 		    (!sci && trigger == ACPI_MADT_TRIGGER_CONFORMS)) {
    199 			type = IST_EDGE;
    200 			mpflags &= ~(MPS_INTTR_LEVEL << 2);
    201 			mpflags |= (MPS_INTTR_EDGE << 2);
    202 			redir &= ~IOAPIC_REDLO_LEVEL;
    203 		}
    204 	}
    205 
    206 	/*
    207 	 * If the interrupt is handled via IOAPIC, update the map.
    208 	 * If the map isn't set up yet, install a temporary one.
    209 	 */
    210 
    211 	sc = ioapic_find_bybase(InterruptNumber);
    212 	if (sc != NULL) {
    213 		pic = &sc->sc_pic;
    214 
    215 		if (pic->pic_type == PIC_IOAPIC) {
    216 			pin = (int)InterruptNumber - pic->pic_vecbase;
    217 			irq = -1;
    218 		} else {
    219 			irq = pin = (int)InterruptNumber;
    220 		}
    221 
    222 		mip = sc->sc_pins[pin].ip_map;
    223 		if (mip) {
    224 			mip->flags &= ~0xf;
    225 			mip->flags |= mpflags;
    226 			mip->redir &= ~(IOAPIC_REDLO_LEVEL |
    227 					IOAPIC_REDLO_ACTLO);
    228 			mip->redir |= redir;
    229 		} else {
    230 			mipp = &sc->sc_pins[pin].ip_map;
    231 			*mipp = &tmpmap;
    232 			tmpmap.redir = redir;
    233 			tmpmap.flags = mpflags;
    234 		}
    235 	} else
    236 #endif
    237 	{
    238 		pic = &i8259_pic;
    239 		irq = pin = (int)InterruptNumber;
    240 	}
    241 
    242 	/*
    243 	 * XXX probably, IPL_BIO is enough.
    244 	 */
    245 	ih = intr_establish_xname(irq, pic, pin, type, IPL_TTY,
    246 	    (int (*)(void *)) ServiceRoutine, Context, false, xname);
    247 
    248 #if NIOAPIC > 0
    249 	if (mipp) {
    250 		*mipp = NULL;
    251 	}
    252 #endif
    253 
    254 	if (ih == NULL)
    255 		return AE_NO_MEMORY;
    256 
    257 	*cookiep = ih;
    258 
    259 	return AE_OK;
    260 }
    261 
    262 void
    263 acpi_md_OsRemoveInterruptHandler(void *cookie)
    264 {
    265 	intr_disestablish(cookie);
    266 }
    267 
    268 void *
    269 acpi_md_intr_establish(uint32_t irq, int ipl, int type, int (*handler)(void *),
    270     void *arg, bool mpsafe, const char *xname)
    271 {
    272 	struct pic *pic;
    273 	int pin;
    274 
    275 	pic = intr_findpic(irq);
    276 	if (pic == NULL)
    277 		return NULL;
    278 	pin = irq - pic->pic_vecbase;
    279 
    280 	return intr_establish_xname(irq, pic, pin, type, ipl, handler, arg, mpsafe, xname);
    281 }
    282 
    283 void
    284 acpi_md_intr_disestablish(void *ih)
    285 {
    286 	intr_disestablish(ih);
    287 }
    288 
    289 ACPI_STATUS
    290 acpi_md_OsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress,
    291     uint32_t Length, void **LogicalAddress)
    292 {
    293 	int rv;
    294 
    295 	rv = _x86_memio_map(x86_bus_space_mem, PhysicalAddress,
    296 	    Length, 0, (bus_space_handle_t *)LogicalAddress);
    297 
    298 	return (rv != 0) ? AE_NO_MEMORY : AE_OK;
    299 }
    300 
    301 void
    302 acpi_md_OsUnmapMemory(void *LogicalAddress, uint32_t Length)
    303 {
    304 	(void) _x86_memio_unmap(x86_bus_space_mem,
    305 	    (bus_space_handle_t)LogicalAddress, Length, NULL);
    306 }
    307 
    308 ACPI_STATUS
    309 acpi_md_OsGetPhysicalAddress(void *LogicalAddress,
    310     ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
    311 {
    312 	paddr_t pa;
    313 
    314 	if (pmap_extract(pmap_kernel(), (vaddr_t) LogicalAddress, &pa)) {
    315 		*PhysicalAddress = pa;
    316 		return AE_OK;
    317 	}
    318 
    319 	return AE_ERROR;
    320 }
    321 
    322 BOOLEAN
    323 acpi_md_OsReadable(void *Pointer, uint32_t Length)
    324 {
    325 	BOOLEAN rv = TRUE;
    326 	vaddr_t sva, eva;
    327 	pt_entry_t *pte;
    328 
    329 	sva = trunc_page((vaddr_t) Pointer);
    330 	eva = round_page((vaddr_t) Pointer + Length);
    331 
    332 	if (sva < VM_MIN_KERNEL_ADDRESS)
    333 		return FALSE;
    334 
    335 	for (; sva < eva; sva += PAGE_SIZE) {
    336 		pte = kvtopte(sva);
    337 		if ((*pte & PG_V) == 0) {
    338 			rv = FALSE;
    339 			break;
    340 		}
    341 	}
    342 
    343 	return rv;
    344 }
    345 
    346 BOOLEAN
    347 acpi_md_OsWritable(void *Pointer, uint32_t Length)
    348 {
    349 	BOOLEAN rv = TRUE;
    350 	vaddr_t sva, eva;
    351 	pt_entry_t *pte;
    352 
    353 	sva = trunc_page((vaddr_t) Pointer);
    354 	eva = round_page((vaddr_t) Pointer + Length);
    355 
    356 	if (sva < VM_MIN_KERNEL_ADDRESS)
    357 		return FALSE;
    358 
    359 	for (; sva < eva; sva += PAGE_SIZE) {
    360 		pte = kvtopte(sva);
    361 		if ((*pte & (PG_V|PG_W)) != (PG_V|PG_W)) {
    362 			rv = FALSE;
    363 			break;
    364 		}
    365 	}
    366 
    367 	return rv;
    368 }
    369 
    370 void
    371 acpi_md_OsDisableInterrupt(void)
    372 {
    373 	x86_disable_intr();
    374 }
    375 
    376 void
    377 acpi_md_OsEnableInterrupt(void)
    378 {
    379 	x86_enable_intr();
    380 }
    381 
    382 uint32_t
    383 acpi_md_ncpus(void)
    384 {
    385 	return kcpuset_countset(kcpuset_attached);
    386 }
    387 
    388 static bool
    389 acpi_md_mcfg_validate(uint64_t addr, int bus_start, int *bus_end)
    390 {
    391 	struct btinfo_memmap *bim;
    392 	uint64_t size, mapaddr, mapsize;
    393 	uint32_t type;
    394 	int i, n;
    395 
    396 #ifndef XEN
    397 	if (lookup_bootinfo(BTINFO_EFIMEMMAP) != NULL)
    398 		bim = efi_get_e820memmap();
    399 	else
    400 #endif
    401 		bim = lookup_bootinfo(BTINFO_MEMMAP);
    402 	if (bim == NULL)
    403 		return false;
    404 
    405 	size = *bus_end - bus_start + 1;
    406 	size *= ACPIMCFG_SIZE_PER_BUS;
    407 	for (i = 0; i < bim->num; i++) {
    408 		mapaddr = bim->entry[i].addr;
    409 		mapsize = bim->entry[i].size;
    410 		type = bim->entry[i].type;
    411 
    412 		aprint_debug("MCFG: MEMMAP: 0x%016" PRIx64
    413 		    "-0x%016" PRIx64 ", size=0x%016" PRIx64
    414 		    ", type=%d(%s)\n",
    415 		    mapaddr, mapaddr + mapsize - 1, mapsize, type,
    416 		    (type == BIM_Memory) ?  "Memory" :
    417 		    (type == BIM_Reserved) ?  "Reserved" :
    418 		    (type == BIM_ACPI) ? "ACPI" :
    419 		    (type == BIM_NVS) ? "NVS" :
    420 		    (type == BIM_PMEM) ? "Persistent" :
    421 		    (type == BIM_PRAM) ? "Persistent (Legacy)" :
    422 		    "unknown");
    423 
    424 		switch (type) {
    425 		case BIM_ACPI:
    426 		case BIM_Reserved:
    427 			if (addr < mapaddr || addr >= mapaddr + mapsize)
    428 				break;
    429 
    430 			/* full map */
    431 			if (addr + size <= mapaddr + mapsize)
    432 				return true;
    433 
    434 			/* partial map */
    435 			n = (mapsize - (addr - mapaddr)) /
    436 			    ACPIMCFG_SIZE_PER_BUS;
    437 			/* bus_start == bus_end is not allowed. */
    438 			if (n > 1) {
    439 				*bus_end = bus_start + n - 1;
    440 				return true;
    441 			}
    442 			aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64
    443 			    ": invalid size: request 0x%016" PRIx64 ", "
    444 			    "actual 0x%016" PRIx64 "\n",
    445 			    bus_start, *bus_end, addr, size, mapsize);
    446 			break;
    447 		}
    448 	}
    449 	aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64 ": "
    450 	    "no valid region\n", bus_start, *bus_end, addr);
    451 	return false;
    452 }
    453 
    454 static uint32_t
    455 acpi_md_mcfg_read(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr)
    456 {
    457 	vaddr_t va = bsh + addr;
    458 	uint32_t data = (uint32_t) -1;
    459 
    460 	KASSERT(bst == x86_bus_space_mem);
    461 
    462 	__asm("movl %1, %0" : "=a" (data) : "m" (*(volatile uint32_t *)va));
    463 
    464 	return data;
    465 }
    466 
    467 static void
    468 acpi_md_mcfg_write(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr,
    469     uint32_t data)
    470 {
    471 	vaddr_t va = bsh + addr;
    472 
    473 	KASSERT(bst == x86_bus_space_mem);
    474 
    475 	__asm("movl %1, %0" : "=m" (*(volatile uint32_t *)va) : "a" (data));
    476 }
    477 
    478 static const struct acpimcfg_ops acpi_md_mcfg_ops = {
    479 	.ao_validate = acpi_md_mcfg_validate,
    480 
    481 	.ao_read = acpi_md_mcfg_read,
    482 	.ao_write = acpi_md_mcfg_write,
    483 };
    484 
    485 void
    486 acpi_md_callback(struct acpi_softc *sc)
    487 {
    488 #ifdef MPBIOS
    489 	if (!mpbios_scanned)
    490 #endif
    491 	mpacpi_find_interrupts(sc);
    492 
    493 #ifndef XEN
    494 	acpi_md_sleep_init();
    495 #endif
    496 
    497 	acpimcfg_init(x86_bus_space_mem, &acpi_md_mcfg_ops);
    498 }
    499 
    500 #ifndef XEN
    501 void
    502 device_acpi_register(device_t dev, void *aux)
    503 {
    504 	device_t parent;
    505 	bool device_is_vga, device_is_pci, device_is_isa;
    506 
    507 	parent = device_parent(dev);
    508 	if (parent == NULL)
    509 		return;
    510 
    511 	device_is_vga = device_is_a(dev, "vga") || device_is_a(dev, "genfb");
    512 	device_is_pci = device_is_a(parent, "pci");
    513 	device_is_isa = device_is_a(parent, "isa");
    514 
    515 	if (device_is_vga && (device_is_pci || device_is_isa)) {
    516 		extern int acpi_md_vbios_reset;
    517 
    518 		acpi_md_vbios_reset = VBIOS_RESET_DEFAULT;
    519 	}
    520 }
    521 #endif
    522