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