Home | History | Annotate | Line # | Download | only in acpi
acpi_machdep.c revision 1.18.6.2
      1 /* $NetBSD: acpi_machdep.c,v 1.18.6.2 2019/09/23 14:36:17 martin 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.18.6.2 2019/09/23 14:36:17 martin 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 #ifdef XEN
    104 	/*
    105 	 * This should use EFI_UUID_ACPI20 and EFI_UUID_ACPI10
    106 	 * from src/sys/arch/x86/x86/efi.c but we want the
    107 	 * ablility to build without this file included.
    108 	 */
    109 	const struct uuid UUID_ACPI20 = EFI_TABLE_ACPI20;
    110 	const struct uuid UUID_ACPI10 = EFI_TABLE_ACPI10;
    111 
    112 	/*
    113 	 * Obtain the ACPI RSDP from the hypervisor.
    114 	 * This is the only way to go if Xen booted from EFI: the
    115 	 * Extended BIOS Data Area (EBDA) is not mapped, and Xen
    116 	 * does not pass an EFI SystemTable to the kernel.
    117 	 */
    118         struct xen_platform_op op = {
    119                 .cmd = XENPF_firmware_info,
    120                 .u.firmware_info = {
    121                         .type = XEN_FW_EFI_INFO,
    122                         .index = XEN_FW_EFI_CONFIG_TABLE
    123                 }
    124         };
    125         union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
    126 
    127         if (HYPERVISOR_platform_op(&op) == 0) {
    128 		struct efi_cfgtbl *ct;
    129 		int i;
    130 
    131 		ct = AcpiOsMapMemory(info->cfg.addr,
    132 		    sizeof(*ct) * info->cfg.nent);
    133 
    134 		for (i = 0; i < info->cfg.nent; i++) {
    135                 	if (memcmp(&ct[i].ct_uuid,
    136 			    &UUID_ACPI20, sizeof(UUID_ACPI20)) == 0) {
    137 				PhysicalAddress = (ACPI_PHYSICAL_ADDRESS)
    138 				    (uintptr_t)ct[i].ct_data;
    139 				if (PhysicalAddress)
    140 					goto out;
    141 
    142 			}
    143 		}
    144 
    145 		for (i = 0; i < info->cfg.nent; i++) {
    146                 	if (memcmp(&ct[i].ct_uuid,
    147 			    &UUID_ACPI10, sizeof(UUID_ACPI10)) == 0) {
    148 				PhysicalAddress = (ACPI_PHYSICAL_ADDRESS)
    149 				    (uintptr_t)ct[i].ct_data;
    150 				if (PhysicalAddress)
    151 					goto out;
    152 
    153 			}
    154 		}
    155 out:
    156 		AcpiOsUnmapMemory(ct, sizeof(*ct) * info->cfg.nent);
    157 
    158 		if (PhysicalAddress)
    159 			return PhysicalAddress;
    160 	}
    161 #else
    162 	/*
    163 	 * Get the ACPI RSDP from EFI SystemTable. This works when the
    164 	 * kernel was loaded from EFI bootloader.
    165 	 */
    166 	if (efi_probe()) {
    167 		PhysicalAddress = efi_getcfgtblpa(&EFI_UUID_ACPI20);
    168 		if (!PhysicalAddress)
    169 			PhysicalAddress = efi_getcfgtblpa(&EFI_UUID_ACPI10);
    170 		if (PhysicalAddress)
    171 			return PhysicalAddress;
    172 	}
    173 
    174 #endif
    175 	/*
    176 	 * Find ACPI RSDP from Extended BIOS Data Area (EBDA). This
    177 	 * works when the kernel was started from BIOS bootloader,
    178 	 * or for Xen PV when Xen was started from BIOS bootloader.
    179 	 */
    180 	Status = AcpiFindRootPointer(&PhysicalAddress);
    181 	if (ACPI_FAILURE(Status))
    182 		PhysicalAddress = 0;
    183 
    184 	return PhysicalAddress;
    185 }
    186 
    187 struct acpi_md_override {
    188 	int irq;
    189 	int pin;
    190 	int flags;
    191 };
    192 
    193 #if NIOAPIC > 0
    194 static ACPI_STATUS
    195 acpi_md_findoverride(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
    196 {
    197 	ACPI_MADT_INTERRUPT_OVERRIDE *iop;
    198 	struct acpi_md_override *ovrp;
    199 
    200 	if (hdrp->Type != ACPI_MADT_TYPE_INTERRUPT_OVERRIDE) {
    201 		return AE_OK;
    202 	}
    203 
    204 	iop = (void *)hdrp;
    205 	ovrp = aux;
    206 	if (iop->SourceIrq == ovrp->irq) {
    207 		ovrp->pin = iop->GlobalIrq;
    208 		ovrp->flags = iop->IntiFlags;
    209 	}
    210 	return AE_OK;
    211 }
    212 #endif
    213 
    214 ACPI_STATUS
    215 acpi_md_OsInstallInterruptHandler(uint32_t InterruptNumber,
    216     ACPI_OSD_HANDLER ServiceRoutine, void *Context, void **cookiep)
    217 {
    218 	void *ih;
    219 	struct pic *pic;
    220 #if NIOAPIC > 0
    221 	struct ioapic_softc *sc;
    222 	struct acpi_md_override ovr;
    223 	struct mp_intr_map tmpmap, *mip, **mipp = NULL;
    224 #endif
    225 	int irq, pin, type, redir, mpflags;
    226 
    227 	/*
    228 	 * ACPI interrupts default to level-triggered active-low.
    229 	 */
    230 
    231 	type = IST_LEVEL;
    232 	mpflags = (MPS_INTTR_LEVEL << 2) | MPS_INTPO_ACTLO;
    233 	redir = IOAPIC_REDLO_LEVEL | IOAPIC_REDLO_ACTLO;
    234 
    235 #if NIOAPIC > 0
    236 
    237 	/*
    238 	 * Apply any MADT override setting.
    239 	 */
    240 
    241 	ovr.irq = InterruptNumber;
    242 	ovr.pin = -1;
    243 	if (acpi_madt_map() == AE_OK) {
    244 		acpi_madt_walk(acpi_md_findoverride, &ovr);
    245 		acpi_madt_unmap();
    246 	} else {
    247 		aprint_debug("acpi_madt_map() failed, can't check for MADT override\n");
    248 	}
    249 
    250 	if (ovr.pin != -1) {
    251 		bool sci = InterruptNumber == AcpiGbl_FADT.SciInterrupt;
    252 		int polarity = ovr.flags & ACPI_MADT_POLARITY_MASK;
    253 		int trigger = ovr.flags & ACPI_MADT_TRIGGER_MASK;
    254 
    255 		InterruptNumber = ovr.pin;
    256 		if (polarity == ACPI_MADT_POLARITY_ACTIVE_HIGH ||
    257 		    (!sci && polarity == ACPI_MADT_POLARITY_CONFORMS)) {
    258 			mpflags &= ~MPS_INTPO_ACTLO;
    259 			mpflags |= MPS_INTPO_ACTHI;
    260 			redir &= ~IOAPIC_REDLO_ACTLO;
    261 		}
    262 		if (trigger == ACPI_MADT_TRIGGER_EDGE ||
    263 		    (!sci && trigger == ACPI_MADT_TRIGGER_CONFORMS)) {
    264 			type = IST_EDGE;
    265 			mpflags &= ~(MPS_INTTR_LEVEL << 2);
    266 			mpflags |= (MPS_INTTR_EDGE << 2);
    267 			redir &= ~IOAPIC_REDLO_LEVEL;
    268 		}
    269 	}
    270 
    271 	/*
    272 	 * If the interrupt is handled via IOAPIC, update the map.
    273 	 * If the map isn't set up yet, install a temporary one.
    274 	 */
    275 
    276 	sc = ioapic_find_bybase(InterruptNumber);
    277 	if (sc != NULL) {
    278 		pic = &sc->sc_pic;
    279 
    280 		if (pic->pic_type == PIC_IOAPIC) {
    281 			pin = (int)InterruptNumber - pic->pic_vecbase;
    282 			irq = -1;
    283 		} else {
    284 			irq = pin = (int)InterruptNumber;
    285 		}
    286 
    287 		mip = sc->sc_pins[pin].ip_map;
    288 		if (mip) {
    289 			mip->flags &= ~0xf;
    290 			mip->flags |= mpflags;
    291 			mip->redir &= ~(IOAPIC_REDLO_LEVEL |
    292 					IOAPIC_REDLO_ACTLO);
    293 			mip->redir |= redir;
    294 		} else {
    295 			mipp = &sc->sc_pins[pin].ip_map;
    296 			*mipp = &tmpmap;
    297 			tmpmap.redir = redir;
    298 			tmpmap.flags = mpflags;
    299 		}
    300 	} else
    301 #endif
    302 	{
    303 		pic = &i8259_pic;
    304 		irq = pin = (int)InterruptNumber;
    305 	}
    306 
    307 	/*
    308 	 * XXX probably, IPL_BIO is enough.
    309 	 */
    310 	ih = intr_establish_xname(irq, pic, pin, type, IPL_TTY,
    311 	    (int (*)(void *)) ServiceRoutine, Context, false, "acpi SCI");
    312 
    313 #if NIOAPIC > 0
    314 	if (mipp) {
    315 		*mipp = NULL;
    316 	}
    317 #endif
    318 
    319 	if (ih == NULL)
    320 		return AE_NO_MEMORY;
    321 
    322 	*cookiep = ih;
    323 
    324 	return AE_OK;
    325 }
    326 
    327 void
    328 acpi_md_OsRemoveInterruptHandler(void *cookie)
    329 {
    330 	intr_disestablish(cookie);
    331 }
    332 
    333 ACPI_STATUS
    334 acpi_md_OsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress,
    335     uint32_t Length, void **LogicalAddress)
    336 {
    337 	int rv;
    338 
    339 	rv = _x86_memio_map(x86_bus_space_mem, PhysicalAddress,
    340 	    Length, 0, (bus_space_handle_t *)LogicalAddress);
    341 
    342 	return (rv != 0) ? AE_NO_MEMORY : AE_OK;
    343 }
    344 
    345 void
    346 acpi_md_OsUnmapMemory(void *LogicalAddress, uint32_t Length)
    347 {
    348 	(void) _x86_memio_unmap(x86_bus_space_mem,
    349 	    (bus_space_handle_t)LogicalAddress, Length, NULL);
    350 }
    351 
    352 ACPI_STATUS
    353 acpi_md_OsGetPhysicalAddress(void *LogicalAddress,
    354     ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
    355 {
    356 	paddr_t pa;
    357 
    358 	if (pmap_extract(pmap_kernel(), (vaddr_t) LogicalAddress, &pa)) {
    359 		*PhysicalAddress = pa;
    360 		return AE_OK;
    361 	}
    362 
    363 	return AE_ERROR;
    364 }
    365 
    366 BOOLEAN
    367 acpi_md_OsReadable(void *Pointer, uint32_t Length)
    368 {
    369 	BOOLEAN rv = TRUE;
    370 	vaddr_t sva, eva;
    371 	pt_entry_t *pte;
    372 
    373 	sva = trunc_page((vaddr_t) Pointer);
    374 	eva = round_page((vaddr_t) Pointer + Length);
    375 
    376 	if (sva < VM_MIN_KERNEL_ADDRESS)
    377 		return FALSE;
    378 
    379 	for (; sva < eva; sva += PAGE_SIZE) {
    380 		pte = kvtopte(sva);
    381 		if ((*pte & PG_V) == 0) {
    382 			rv = FALSE;
    383 			break;
    384 		}
    385 	}
    386 
    387 	return rv;
    388 }
    389 
    390 BOOLEAN
    391 acpi_md_OsWritable(void *Pointer, uint32_t Length)
    392 {
    393 	BOOLEAN rv = TRUE;
    394 	vaddr_t sva, eva;
    395 	pt_entry_t *pte;
    396 
    397 	sva = trunc_page((vaddr_t) Pointer);
    398 	eva = round_page((vaddr_t) Pointer + Length);
    399 
    400 	if (sva < VM_MIN_KERNEL_ADDRESS)
    401 		return FALSE;
    402 
    403 	for (; sva < eva; sva += PAGE_SIZE) {
    404 		pte = kvtopte(sva);
    405 		if ((*pte & (PG_V|PG_W)) != (PG_V|PG_W)) {
    406 			rv = FALSE;
    407 			break;
    408 		}
    409 	}
    410 
    411 	return rv;
    412 }
    413 
    414 void
    415 acpi_md_OsDisableInterrupt(void)
    416 {
    417 	x86_disable_intr();
    418 }
    419 
    420 void
    421 acpi_md_OsEnableInterrupt(void)
    422 {
    423 	x86_enable_intr();
    424 }
    425 
    426 uint32_t
    427 acpi_md_ncpus(void)
    428 {
    429 	return kcpuset_countset(kcpuset_attached);
    430 }
    431 
    432 static bool
    433 acpi_md_mcfg_validate(uint64_t addr, int bus_start, int *bus_end)
    434 {
    435 	struct btinfo_memmap *bim;
    436 	uint64_t size, mapaddr, mapsize;
    437 	uint32_t type;
    438 	int i, n;
    439 
    440 #ifndef XEN
    441 	if (lookup_bootinfo(BTINFO_EFIMEMMAP) != NULL)
    442 		bim = efi_get_e820memmap();
    443 	else
    444 #endif
    445 		bim = lookup_bootinfo(BTINFO_MEMMAP);
    446 	if (bim == NULL)
    447 		return false;
    448 
    449 	size = *bus_end - bus_start + 1;
    450 	size *= ACPIMCFG_SIZE_PER_BUS;
    451 	for (i = 0; i < bim->num; i++) {
    452 		mapaddr = bim->entry[i].addr;
    453 		mapsize = bim->entry[i].size;
    454 		type = bim->entry[i].type;
    455 
    456 		aprint_debug("MCFG: MEMMAP: 0x%016" PRIx64
    457 		    "-0x%016" PRIx64 ", size=0x%016" PRIx64
    458 		    ", type=%d(%s)\n",
    459 		    mapaddr, mapaddr + mapsize - 1, mapsize, type,
    460 		    (type == BIM_Memory) ?  "Memory" :
    461 		    (type == BIM_Reserved) ?  "Reserved" :
    462 		    (type == BIM_ACPI) ? "ACPI" :
    463 		    (type == BIM_NVS) ? "NVS" :
    464 		    (type == BIM_PMEM) ? "Persistent" :
    465 		    (type == BIM_PRAM) ? "Persistent (Legacy)" :
    466 		    "unknown");
    467 
    468 		switch (type) {
    469 		case BIM_ACPI:
    470 		case BIM_Reserved:
    471 			if (addr < mapaddr || addr >= mapaddr + mapsize)
    472 				break;
    473 
    474 			/* full map */
    475 			if (addr + size <= mapaddr + mapsize)
    476 				return true;
    477 
    478 			/* partial map */
    479 			n = (mapsize - (addr - mapaddr)) /
    480 			    ACPIMCFG_SIZE_PER_BUS;
    481 			/* bus_start == bus_end is not allowed. */
    482 			if (n > 1) {
    483 				*bus_end = bus_start + n - 1;
    484 				return true;
    485 			}
    486 			aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64
    487 			    ": invalid size: request 0x%016" PRIx64 ", "
    488 			    "actual 0x%016" PRIx64 "\n",
    489 			    bus_start, *bus_end, addr, size, mapsize);
    490 			break;
    491 		}
    492 	}
    493 	aprint_debug("MCFG: bus %d-%d, address 0x%016" PRIx64 ": "
    494 	    "no valid region\n", bus_start, *bus_end, addr);
    495 	return false;
    496 }
    497 
    498 static uint32_t
    499 acpi_md_mcfg_read(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr)
    500 {
    501 	vaddr_t va = bsh + addr;
    502 	uint32_t data = (uint32_t) -1;
    503 
    504 	KASSERT(bst == x86_bus_space_mem);
    505 
    506 	__asm("movl %1, %0" : "=a" (data) : "m" (*(volatile uint32_t *)va));
    507 
    508 	return data;
    509 }
    510 
    511 static void
    512 acpi_md_mcfg_write(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t addr,
    513     uint32_t data)
    514 {
    515 	vaddr_t va = bsh + addr;
    516 
    517 	KASSERT(bst == x86_bus_space_mem);
    518 
    519 	__asm("movl %1, %0" : "=m" (*(volatile uint32_t *)va) : "a" (data));
    520 }
    521 
    522 static const struct acpimcfg_ops acpi_md_mcfg_ops = {
    523 	.ao_validate = acpi_md_mcfg_validate,
    524 
    525 	.ao_read = acpi_md_mcfg_read,
    526 	.ao_write = acpi_md_mcfg_write,
    527 };
    528 
    529 void
    530 acpi_md_callback(struct acpi_softc *sc)
    531 {
    532 #ifdef MPBIOS
    533 	if (!mpbios_scanned)
    534 #endif
    535 	mpacpi_find_interrupts(sc);
    536 
    537 #ifndef XEN
    538 	acpi_md_sleep_init();
    539 #endif
    540 
    541 	acpimcfg_init(x86_bus_space_mem, &acpi_md_mcfg_ops);
    542 }
    543 
    544 #ifndef XEN
    545 void
    546 device_acpi_register(device_t dev, void *aux)
    547 {
    548 	device_t parent;
    549 	bool device_is_vga, device_is_pci, device_is_isa;
    550 
    551 	parent = device_parent(dev);
    552 	if (parent == NULL)
    553 		return;
    554 
    555 	device_is_vga = device_is_a(dev, "vga") || device_is_a(dev, "genfb");
    556 	device_is_pci = device_is_a(parent, "pci");
    557 	device_is_isa = device_is_a(parent, "isa");
    558 
    559 	if (device_is_vga && (device_is_pci || device_is_isa)) {
    560 		extern int acpi_md_vbios_reset;
    561 
    562 		acpi_md_vbios_reset = VBIOS_RESET_DEFAULT;
    563 	}
    564 }
    565 #endif
    566