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