Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_cstate.c revision 1.5
      1 /* $NetBSD: acpi_cpu_cstate.c,v 1.5 2010/07/19 00:59:32 christos Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 Jukka Ruohonen <jruohonen (at) iki.fi>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  *
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: acpi_cpu_cstate.c,v 1.5 2010/07/19 00:59:32 christos Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/cpu.h>
     34 #include <sys/device.h>
     35 #include <sys/kernel.h>
     36 #include <sys/once.h>
     37 #include <sys/timetc.h>
     38 
     39 #include <dev/pci/pcivar.h>
     40 #include <dev/pci/pcidevs.h>
     41 
     42 #include <dev/acpi/acpireg.h>
     43 #include <dev/acpi/acpivar.h>
     44 #include <dev/acpi/acpi_cpu.h>
     45 #include <dev/acpi/acpi_timer.h>
     46 
     47 #include <machine/acpi_machdep.h>
     48 
     49 #define _COMPONENT	 ACPI_BUS_COMPONENT
     50 ACPI_MODULE_NAME	 ("acpi_cpu_cstate")
     51 
     52 /*
     53  * This is AML_RESOURCE_GENERIC_REGISTER,
     54  * included here separately for convenience.
     55  */
     56 struct acpicpu_reg {
     57 	uint8_t		 reg_desc;
     58 	uint16_t	 reg_reslen;
     59 	uint8_t		 reg_spaceid;
     60 	uint8_t		 reg_bitwidth;
     61 	uint8_t		 reg_bitoffset;
     62 	uint8_t		 reg_accesssize;
     63 	uint64_t	 reg_addr;
     64 } __packed;
     65 
     66 static void		 acpicpu_cstate_attach_print(struct acpicpu_softc *);
     67 static ACPI_STATUS	 acpicpu_cstate_cst(struct acpicpu_softc *);
     68 static ACPI_STATUS	 acpicpu_cstate_cst_add(struct acpicpu_softc *,
     69 						ACPI_OBJECT *);
     70 static void		 acpicpu_cstate_cst_bios(void);
     71 static ACPI_STATUS	 acpicpu_cstate_csd(ACPI_HANDLE, struct acpicpu_csd *);
     72 static void		 acpicpu_cstate_fadt(struct acpicpu_softc *);
     73 static void		 acpicpu_cstate_quirks(struct acpicpu_softc *);
     74 static int		 acpicpu_cstate_quirks_piix4(struct pci_attach_args *);
     75 static int		 acpicpu_cstate_latency(struct acpicpu_softc *);
     76 static bool		 acpicpu_cstate_bm_check(void);
     77 static void		 acpicpu_cstate_idle_enter(struct acpicpu_softc *,int);
     78 
     79 extern kmutex_t		      acpicpu_mtx;
     80 extern struct acpicpu_softc **acpicpu_sc;
     81 extern int		      acpi_suspended;
     82 
     83 void
     84 acpicpu_cstate_attach(device_t self)
     85 {
     86 	struct acpicpu_softc *sc = device_private(self);
     87 	ACPI_STATUS rv;
     88 
     89 	/*
     90 	 * Either use the preferred _CST or resort to FADT.
     91 	 */
     92 	rv = acpicpu_cstate_cst(sc);
     93 
     94 	switch (rv) {
     95 
     96 	case AE_OK:
     97 		sc->sc_flags |= ACPICPU_FLAG_C | ACPICPU_FLAG_C_CST;
     98 		acpicpu_cstate_cst_bios();
     99 		break;
    100 
    101 	default:
    102 		sc->sc_flags |= ACPICPU_FLAG_C | ACPICPU_FLAG_C_FADT;
    103 		acpicpu_cstate_fadt(sc);
    104 		break;
    105 	}
    106 
    107 	acpicpu_cstate_quirks(sc);
    108 	acpicpu_cstate_attach_print(sc);
    109 }
    110 
    111 void
    112 acpicpu_cstate_attach_print(struct acpicpu_softc *sc)
    113 {
    114 	struct acpicpu_cstate *cs;
    115 	struct acpicpu_csd csd;
    116 	const char *method;
    117 	ACPI_STATUS rv;
    118 	int i;
    119 
    120 	(void)memset(&csd, 0, sizeof(struct acpicpu_csd));
    121 
    122 	rv = acpicpu_cstate_csd(sc->sc_node->ad_handle, &csd);
    123 
    124 	if (ACPI_SUCCESS(rv)) {
    125 		aprint_debug_dev(sc->sc_dev, "C%u:  _CSD, "
    126 		    "domain 0x%02x / 0x%02x, type 0x%02x\n",
    127 		    csd.csd_index, csd.csd_domain,
    128 		    csd.csd_ncpu, csd.csd_coord);
    129 	}
    130 
    131 	aprint_debug_dev(sc->sc_dev, "Cx: %5s",
    132 	    (sc->sc_flags & ACPICPU_FLAG_C_FADT) != 0 ? "FADT" : "_CST");
    133 
    134 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
    135 		aprint_debug(", BM control");
    136 
    137 	if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
    138 		aprint_debug(", BM arbitration");
    139 
    140 	if ((sc->sc_flags & ACPICPU_FLAG_C_C1E) != 0)
    141 		aprint_debug(", C1E");
    142 
    143 	if ((sc->sc_flags & ACPICPU_FLAG_C_NOC3) != 0)
    144 		aprint_debug(", C3 disabled (quirk)");
    145 
    146 	aprint_debug("\n");
    147 
    148 	for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
    149 
    150 		cs = &sc->sc_cstate[i];
    151 
    152 		if (cs->cs_method == 0)
    153 			continue;
    154 
    155 		switch (cs->cs_method) {
    156 
    157 		case ACPICPU_C_STATE_HALT:
    158 			method = "HALT";
    159 			break;
    160 
    161 		case ACPICPU_C_STATE_FFH:
    162 			method = "FFH";
    163 			break;
    164 
    165 		case ACPICPU_C_STATE_SYSIO:
    166 			method = "SYSIO";
    167 			break;
    168 
    169 		default:
    170 			panic("NOTREACHED");
    171 		}
    172 
    173 		aprint_debug_dev(sc->sc_dev, "C%d: %5s, "
    174 		    "latency %4u, power %4u, addr 0x%06x\n", i, method,
    175 		    cs->cs_latency, cs->cs_power, (uint32_t)cs->cs_addr);
    176 	}
    177 }
    178 
    179 int
    180 acpicpu_cstate_detach(device_t self)
    181 {
    182 	static ONCE_DECL(once_detach);
    183 
    184 	return RUN_ONCE(&once_detach, acpicpu_md_idle_stop);
    185 }
    186 
    187 int
    188 acpicpu_cstate_start(device_t self)
    189 {
    190 	struct acpicpu_softc *sc = device_private(self);
    191 	static ONCE_DECL(once_start);
    192 	static ONCE_DECL(once_save);
    193 	int rv;
    194 
    195 	if ((sc->sc_flags & ACPICPU_FLAG_C) == 0)
    196 		return 0;
    197 
    198 	/*
    199 	 * Save the existing idle-mechanism and claim the idle_loop(9).
    200 	 * This should be called after all ACPI CPUs have been attached.
    201 	 */
    202 	rv = RUN_ONCE(&once_save, acpicpu_md_idle_init);
    203 
    204 	if (rv != 0)
    205 		return rv;
    206 
    207 	rv = RUN_ONCE(&once_start, acpicpu_md_idle_start);
    208 	if (rv == 0)
    209 		sc->sc_flags |= ACPICPU_FLAG_INIT;
    210 	return rv;
    211 }
    212 
    213 bool
    214 acpicpu_cstate_suspend(device_t self)
    215 {
    216 
    217 	return true;
    218 }
    219 
    220 bool
    221 acpicpu_cstate_resume(device_t self)
    222 {
    223 	static const ACPI_OSD_EXEC_CALLBACK func = acpicpu_cstate_callback;
    224 	struct acpicpu_softc *sc = device_private(self);
    225 
    226 	KASSERT((sc->sc_flags & ACPICPU_FLAG_C) != 0);
    227 
    228 	if ((sc->sc_flags & ACPICPU_FLAG_C_CST) != 0)
    229 		(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
    230 
    231 	return true;
    232 }
    233 
    234 void
    235 acpicpu_cstate_callback(void *aux)
    236 {
    237 	struct acpicpu_softc *sc;
    238 	device_t self = aux;
    239 
    240 	sc = device_private(self);
    241 
    242 	KASSERT((sc->sc_flags & ACPICPU_FLAG_C) != 0);
    243 
    244 	if ((sc->sc_flags & ACPICPU_FLAG_C_FADT) != 0) {
    245 		KASSERT((sc->sc_flags & ACPICPU_FLAG_C_CST) == 0);
    246 		return;
    247 	}
    248 
    249 	(void)acpicpu_md_idle_stop();
    250 	(void)acpicpu_cstate_cst(sc);
    251 	(void)acpicpu_md_idle_start();
    252 }
    253 
    254 static ACPI_STATUS
    255 acpicpu_cstate_cst(struct acpicpu_softc *sc)
    256 {
    257 	ACPI_OBJECT *elm, *obj;
    258 	ACPI_BUFFER buf;
    259 	ACPI_STATUS rv;
    260 	uint32_t i, n;
    261 	uint8_t count;
    262 
    263 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_CST", &buf);
    264 
    265 	if (ACPI_FAILURE(rv))
    266 		return rv;
    267 
    268 	obj = buf.Pointer;
    269 
    270 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    271 		rv = AE_TYPE;
    272 		goto out;
    273 	}
    274 
    275 	if (obj->Package.Count < 2) {
    276 		rv = AE_LIMIT;
    277 		goto out;
    278 	}
    279 
    280 	elm = obj->Package.Elements;
    281 
    282 	if (elm[0].Type != ACPI_TYPE_INTEGER) {
    283 		rv = AE_TYPE;
    284 		goto out;
    285 	}
    286 
    287 	n = elm[0].Integer.Value;
    288 
    289 	if (n != obj->Package.Count - 1) {
    290 		rv = AE_BAD_VALUE;
    291 		goto out;
    292 	}
    293 
    294 	if (n > ACPI_C_STATES_MAX) {
    295 		rv = AE_LIMIT;
    296 		goto out;
    297 	}
    298 
    299 	(void)memset(sc->sc_cstate, 0,
    300 	    sizeof(*sc->sc_cstate) * ACPI_C_STATE_COUNT);
    301 
    302 	CTASSERT(ACPI_STATE_C0 == 0 && ACPI_STATE_C1 == 1);
    303 	CTASSERT(ACPI_STATE_C2 == 2 && ACPI_STATE_C3 == 3);
    304 
    305 	for (count = 0, i = 1; i <= n; i++) {
    306 
    307 		elm = &obj->Package.Elements[i];
    308 		rv = acpicpu_cstate_cst_add(sc, elm);
    309 
    310 		if (ACPI_SUCCESS(rv))
    311 			count++;
    312 	}
    313 
    314 	rv = (count != 0) ? AE_OK : AE_NOT_EXIST;
    315 
    316 out:
    317 	if (buf.Pointer != NULL)
    318 		ACPI_FREE(buf.Pointer);
    319 
    320 	if (ACPI_FAILURE(rv))
    321 		aprint_error_dev(sc->sc_dev, "failed to evaluate "
    322 		    "_CST: %s\n", AcpiFormatException(rv));
    323 
    324 	return rv;
    325 }
    326 
    327 static ACPI_STATUS
    328 acpicpu_cstate_cst_add(struct acpicpu_softc *sc, ACPI_OBJECT *elm)
    329 {
    330 	const struct acpicpu_object *ao = &sc->sc_object;
    331 	struct acpicpu_cstate *cs = sc->sc_cstate;
    332 	struct acpicpu_cstate state;
    333 	struct acpicpu_reg *reg;
    334 	ACPI_STATUS rv = AE_OK;
    335 	ACPI_OBJECT *obj;
    336 	uint32_t type;
    337 
    338 	(void)memset(&state, 0, sizeof(*cs));
    339 
    340 	if (elm->Type != ACPI_TYPE_PACKAGE) {
    341 		rv = AE_TYPE;
    342 		goto out;
    343 	}
    344 
    345 	if (elm->Package.Count != 4) {
    346 		rv = AE_LIMIT;
    347 		goto out;
    348 	}
    349 
    350 	/*
    351 	 * Type.
    352 	 */
    353 	obj = &elm->Package.Elements[1];
    354 
    355 	if (obj->Type != ACPI_TYPE_INTEGER) {
    356 		rv = AE_TYPE;
    357 		goto out;
    358 	}
    359 
    360 	type = obj->Integer.Value;
    361 
    362 	if (type < ACPI_STATE_C1 || type > ACPI_STATE_C3) {
    363 		rv = AE_TYPE;
    364 		goto out;
    365 	}
    366 
    367 	/*
    368 	 * Latency.
    369 	 */
    370 	obj = &elm->Package.Elements[2];
    371 
    372 	if (obj->Type != ACPI_TYPE_INTEGER) {
    373 		rv = AE_TYPE;
    374 		goto out;
    375 	}
    376 
    377 	state.cs_latency = obj->Integer.Value;
    378 
    379 	/*
    380 	 * Power.
    381 	 */
    382 	obj = &elm->Package.Elements[3];
    383 
    384 	if (obj->Type != ACPI_TYPE_INTEGER) {
    385 		rv = AE_TYPE;
    386 		goto out;
    387 	}
    388 
    389 	state.cs_power = obj->Integer.Value;
    390 
    391 	/*
    392 	 * Register.
    393 	 */
    394 	obj = &elm->Package.Elements[0];
    395 
    396 	if (obj->Type != ACPI_TYPE_BUFFER) {
    397 		rv = AE_TYPE;
    398 		goto out;
    399 	}
    400 
    401 	/*
    402 	 * "When specifically directed by the CPU manufacturer, the
    403 	 *  system firmware may define an interface as functional
    404 	 *  fixed hardware by supplying a special address space
    405 	 *  identifier, FfixedHW (0x7F), in the address space ID
    406 	 *  field for register definitions (ACPI 3.0, p. 46)".
    407 	 */
    408 	reg = (struct acpicpu_reg *)obj->Buffer.Pointer;
    409 
    410 	switch (reg->reg_spaceid) {
    411 
    412 	case ACPI_ADR_SPACE_SYSTEM_IO:
    413 		state.cs_method = ACPICPU_C_STATE_SYSIO;
    414 
    415 		if (reg->reg_addr == 0) {
    416 			rv = AE_AML_ILLEGAL_ADDRESS;
    417 			goto out;
    418 		}
    419 
    420 		if (reg->reg_bitwidth != 8) {
    421 			rv = AE_AML_BAD_RESOURCE_LENGTH;
    422 			goto out;
    423 		}
    424 
    425 		/*
    426 		 * Check only that the address is in the mapped space.
    427 		 * Systems are allowed to change it when operating
    428 		 * with _CST (see ACPI 4.0, pp. 94-95). For instance,
    429 		 * the offset of P_LVL3 may change depending on whether
    430 		 * acpiacad(4) is connected or disconnected.
    431 		 */
    432 		if (reg->reg_addr > ao->ao_pblkaddr + ao->ao_pblklen) {
    433 			rv = AE_BAD_ADDRESS;
    434 			goto out;
    435 		}
    436 
    437 		state.cs_addr = reg->reg_addr;
    438 		break;
    439 
    440 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    441 		state.cs_method = ACPICPU_C_STATE_FFH;
    442 
    443 		switch (type) {
    444 
    445 		case ACPI_STATE_C1:
    446 
    447 			if ((sc->sc_flags & ACPICPU_FLAG_C_MWAIT) == 0)
    448 				state.cs_method = ACPICPU_C_STATE_HALT;
    449 
    450 			break;
    451 
    452 		default:
    453 
    454 			if ((sc->sc_flags & ACPICPU_FLAG_C_MWAIT) == 0) {
    455 				rv = AE_AML_BAD_RESOURCE_VALUE;
    456 				goto out;
    457 			}
    458 		}
    459 
    460 		break;
    461 
    462 	default:
    463 		rv = AE_AML_INVALID_SPACE_ID;
    464 		goto out;
    465 	}
    466 
    467 	if (cs[type].cs_method != 0) {
    468 		rv = AE_ALREADY_EXISTS;
    469 		goto out;
    470 	}
    471 
    472 #ifndef ACPICPU_ENABLE_C3
    473 	/*
    474 	 * XXX: The local APIC timer (as well as TSC) is typically
    475 	 *	stopped in C3, causing the timer interrupt to fire
    476 	 *	haphazardly, depending on how long the system slept.
    477 	 *	For now, we disable the C3 state unconditionally.
    478 	 */
    479 	if (type == ACPI_STATE_C3) {
    480 		sc->sc_flags |= ACPICPU_FLAG_C_NOC3;
    481 		goto out;
    482 	}
    483 #endif
    484 
    485 	cs[type].cs_addr = state.cs_addr;
    486 	cs[type].cs_power = state.cs_power;
    487 	cs[type].cs_latency = state.cs_latency;
    488 	cs[type].cs_method = state.cs_method;
    489 
    490 out:
    491 	if (ACPI_FAILURE(rv))
    492 		aprint_verbose_dev(sc->sc_dev,
    493 		    "invalid _CST: %s\n", AcpiFormatException(rv));
    494 
    495 	return rv;
    496 }
    497 
    498 static void
    499 acpicpu_cstate_cst_bios(void)
    500 {
    501 	const uint8_t val = AcpiGbl_FADT.CstControl;
    502 	const uint32_t addr = AcpiGbl_FADT.SmiCommand;
    503 
    504 	if (addr == 0)
    505 		return;
    506 
    507 	(void)AcpiOsWritePort(addr, val, 8);
    508 }
    509 
    510 static ACPI_STATUS
    511 acpicpu_cstate_csd(ACPI_HANDLE hdl, struct acpicpu_csd *csd)
    512 {
    513 	ACPI_OBJECT *elm, *obj;
    514 	ACPI_BUFFER buf;
    515 	ACPI_STATUS rv;
    516 	int i, n;
    517 
    518 	/*
    519 	 * Query the optional _CSD for heuristics.
    520 	 */
    521 	rv = acpi_eval_struct(hdl, "_CSD", &buf);
    522 
    523 	if (ACPI_FAILURE(rv))
    524 		return rv;
    525 
    526 	obj = buf.Pointer;
    527 
    528 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    529 		rv = AE_TYPE;
    530 		goto out;
    531 	}
    532 
    533 	n = obj->Package.Count;
    534 
    535 	if (n != 6) {
    536 		rv = AE_LIMIT;
    537 		goto out;
    538 	}
    539 
    540 	elm = obj->Package.Elements;
    541 
    542 	for (i = 0; i < n; i++) {
    543 
    544 		if (elm[i].Type != ACPI_TYPE_INTEGER) {
    545 			rv = AE_TYPE;
    546 			goto out;
    547 		}
    548 
    549 		KDASSERT((uint64_t)elm[i].Integer.Value <= UINT32_MAX);
    550 	}
    551 
    552 	if (elm[0].Integer.Value != 6 || elm[1].Integer.Value != 0) {
    553 		rv = AE_BAD_DATA;
    554 		goto out;
    555 	}
    556 
    557 	csd->csd_domain = elm[2].Integer.Value;
    558 	csd->csd_coord  = elm[3].Integer.Value;
    559 	csd->csd_ncpu   = elm[4].Integer.Value;
    560 	csd->csd_index  = elm[5].Integer.Value;
    561 
    562 out:
    563 	if (buf.Pointer != NULL)
    564 		ACPI_FREE(buf.Pointer);
    565 
    566 	return rv;
    567 }
    568 
    569 static void
    570 acpicpu_cstate_fadt(struct acpicpu_softc *sc)
    571 {
    572 	struct acpicpu_cstate *cs = sc->sc_cstate;
    573 
    574 	(void)memset(cs, 0, sizeof(*cs) * ACPI_C_STATE_COUNT);
    575 
    576 	/*
    577 	 * All x86 processors should support C1 (a.k.a. HALT).
    578 	 */
    579 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_C1_SUPPORTED) != 0)
    580 		cs[ACPI_STATE_C1].cs_method = ACPICPU_C_STATE_HALT;
    581 
    582 	if ((acpicpu_md_cpus_running() > 1) &&
    583 	    (AcpiGbl_FADT.Flags & ACPI_FADT_C2_MP_SUPPORTED) == 0)
    584 		return;
    585 
    586 	cs[ACPI_STATE_C2].cs_method = ACPICPU_C_STATE_SYSIO;
    587 	cs[ACPI_STATE_C3].cs_method = ACPICPU_C_STATE_SYSIO;
    588 
    589 	cs[ACPI_STATE_C2].cs_latency = AcpiGbl_FADT.C2Latency;
    590 	cs[ACPI_STATE_C3].cs_latency = AcpiGbl_FADT.C3Latency;
    591 
    592 	cs[ACPI_STATE_C2].cs_addr = sc->sc_object.ao_pblkaddr + 4;
    593 	cs[ACPI_STATE_C3].cs_addr = sc->sc_object.ao_pblkaddr + 5;
    594 
    595 	/*
    596 	 * The P_BLK length should always be 6. If it
    597 	 * is not, reduce functionality accordingly.
    598 	 * Sanity check also FADT's latency levels.
    599 	 */
    600 	if (sc->sc_object.ao_pblklen < 5)
    601 		cs[ACPI_STATE_C2].cs_method = 0;
    602 
    603 	if (sc->sc_object.ao_pblklen < 6)
    604 		cs[ACPI_STATE_C3].cs_method = 0;
    605 
    606 	CTASSERT(ACPICPU_C_C2_LATENCY_MAX == 100);
    607 	CTASSERT(ACPICPU_C_C3_LATENCY_MAX == 1000);
    608 
    609 	if (AcpiGbl_FADT.C2Latency > ACPICPU_C_C2_LATENCY_MAX)
    610 		cs[ACPI_STATE_C2].cs_method = 0;
    611 
    612 	if (AcpiGbl_FADT.C3Latency > ACPICPU_C_C3_LATENCY_MAX)
    613 		cs[ACPI_STATE_C3].cs_method = 0;
    614 
    615 #ifndef ACPICPU_ENABLE_C3
    616 	cs[ACPI_STATE_C3].cs_method = 0;
    617 	sc->sc_flags |= ACPICPU_FLAG_C_NOC3;	/* XXX. */
    618 #endif
    619 }
    620 
    621 static void
    622 acpicpu_cstate_quirks(struct acpicpu_softc *sc)
    623 {
    624 	const uint32_t reg = AcpiGbl_FADT.Pm2ControlBlock;
    625 	const uint32_t len = AcpiGbl_FADT.Pm2ControlLength;
    626 	struct pci_attach_args pa;
    627 
    628 	/*
    629 	 * Check bus master arbitration.
    630 	 */
    631 	if (reg != 0 && len != 0)
    632 		sc->sc_flags |= ACPICPU_FLAG_C_ARB;
    633 	else {
    634 		/*
    635 		 * Disable C3 entirely if WBINVD is not present.
    636 		 */
    637 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD) == 0)
    638 			sc->sc_flags |= ACPICPU_FLAG_C_NOC3;
    639 		else {
    640 			/*
    641 			 * If WBINVD is present, but not functioning
    642 			 * properly according to FADT, flush all CPU
    643 			 * caches before entering the C3 state.
    644 			 */
    645 			if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD_FLUSH) == 0)
    646 				sc->sc_flags &= ~ACPICPU_FLAG_C_BM;
    647 		}
    648 	}
    649 
    650 	/*
    651 	 * There are several erratums for PIIX4.
    652 	 */
    653 	if (pci_find_device(&pa, acpicpu_cstate_quirks_piix4) != 0)
    654 		sc->sc_flags |= ACPICPU_FLAG_C_NOC3;
    655 
    656 	if ((sc->sc_flags & ACPICPU_FLAG_C_NOC3) != 0)
    657 		sc->sc_cstate[ACPI_STATE_C3].cs_method = 0;
    658 }
    659 
    660 static int
    661 acpicpu_cstate_quirks_piix4(struct pci_attach_args *pa)
    662 {
    663 
    664 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    665 		return 0;
    666 
    667 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82371AB_ISA ||
    668 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82440MX_PMC)
    669 		return 1;
    670 
    671 	return 0;
    672 }
    673 
    674 static int
    675 acpicpu_cstate_latency(struct acpicpu_softc *sc)
    676 {
    677 	static const uint32_t cs_factor = 3;
    678 	struct acpicpu_cstate *cs;
    679 	int i;
    680 
    681 	for (i = ACPI_STATE_C3; i > 0; i--) {
    682 
    683 		cs = &sc->sc_cstate[i];
    684 
    685 		if (__predict_false(cs->cs_method == 0))
    686 			continue;
    687 
    688 		/*
    689 		 * Choose a state if we have previously slept
    690 		 * longer than the worst case latency of the
    691 		 * state times an arbitrary multiplier.
    692 		 */
    693 		if (sc->sc_sleep > cs->cs_latency * cs_factor)
    694 			return i;
    695 	}
    696 
    697 	return ACPI_STATE_C1;
    698 }
    699 
    700 /*
    701  * The main idle loop.
    702  */
    703 void
    704 acpicpu_cstate_idle(void)
    705 {
    706         struct cpu_info *ci = curcpu();
    707 	struct acpicpu_softc *sc;
    708 	int state;
    709 
    710 	if (__predict_false(ci->ci_want_resched) != 0)
    711 		return;
    712 
    713 	KASSERT(acpicpu_sc != NULL);
    714 	KASSERT(ci->ci_cpuid < maxcpus);
    715 	KASSERT(ci->ci_ilevel == IPL_NONE);
    716 
    717 	if (__predict_false(acpi_suspended != 0)) {
    718 		acpicpu_md_idle_enter(0, 0);
    719 		return;
    720 	}
    721 
    722 	sc = acpicpu_sc[ci->ci_cpuid];
    723 
    724 	/*
    725 	 * If all CPUs do not have an ACPI counterpart,
    726 	 * the softc may be NULL. In this case use C1.
    727 	 */
    728 	if (__predict_false(sc == NULL)) {
    729 		acpicpu_md_idle_enter(0, 0);
    730 		return;
    731 	}
    732 
    733 	acpi_md_OsDisableInterrupt();
    734 	state = acpicpu_cstate_latency(sc);
    735 
    736 	/*
    737 	 * Check for bus master activity. Note that
    738 	 * particularly usb(4) causes high activity,
    739 	 * which may prevent the use of C3 states.
    740 	 */
    741 	if (acpicpu_cstate_bm_check() != false) {
    742 
    743 		state--;
    744 
    745 		if (__predict_false(sc->sc_cstate[state].cs_method == 0))
    746 			state = ACPI_STATE_C1;
    747 	}
    748 
    749 	KASSERT(state != ACPI_STATE_C0);
    750 
    751 	if (state != ACPI_STATE_C3) {
    752 		acpicpu_cstate_idle_enter(sc, state);
    753 		return;
    754 	}
    755 
    756 	/*
    757 	 * On all recent (Intel) CPUs caches are shared
    758 	 * by CPUs and bus master control is required to
    759 	 * keep these coherent while in C3. Flushing the
    760 	 * CPU caches is only the last resort.
    761 	 */
    762 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) == 0)
    763 		ACPI_FLUSH_CPU_CACHE();
    764 
    765 	/*
    766 	 * Some chipsets may not return back to C0
    767 	 * from C3 if bus master wake is not enabled.
    768 	 */
    769 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
    770 		(void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
    771 
    772 	/*
    773 	 * It may be necessary to disable bus master arbitration
    774 	 * to ensure that bus master cycles do not occur while
    775 	 * sleeping in C3 (see ACPI 4.0, section 8.1.4).
    776 	 */
    777 	if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
    778 		(void)AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 1);
    779 
    780 	acpicpu_cstate_idle_enter(sc, state);
    781 
    782 	/*
    783 	 * Disable bus master wake and re-enable the arbiter.
    784 	 */
    785 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
    786 		(void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
    787 
    788 	if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
    789 		(void)AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 0);
    790 }
    791 
    792 static void
    793 acpicpu_cstate_idle_enter(struct acpicpu_softc *sc, int state)
    794 {
    795 	struct acpicpu_cstate *cs = &sc->sc_cstate[state];
    796 	uint32_t end, start, val;
    797 
    798 	start = acpitimer_read_safe(NULL);
    799 
    800 	switch (cs->cs_method) {
    801 
    802 	case ACPICPU_C_STATE_FFH:
    803 	case ACPICPU_C_STATE_HALT:
    804 		acpicpu_md_idle_enter(cs->cs_method, state);
    805 		break;
    806 
    807 	case ACPICPU_C_STATE_SYSIO:
    808 		(void)AcpiOsReadPort(cs->cs_addr, &val, 8);
    809 		break;
    810 
    811 	default:
    812 		acpicpu_md_idle_enter(0, 0);
    813 		break;
    814 	}
    815 
    816 	cs->cs_stat++;
    817 
    818 	end = acpitimer_read_safe(NULL);
    819 	sc->sc_sleep = hztoms(acpitimer_delta(end, start)) * 1000;
    820 
    821 	acpi_md_OsEnableInterrupt();
    822 }
    823 
    824 static bool
    825 acpicpu_cstate_bm_check(void)
    826 {
    827 	uint32_t val = 0;
    828 	ACPI_STATUS rv;
    829 
    830 	rv = AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, &val);
    831 
    832 	if (ACPI_FAILURE(rv) || val == 0)
    833 		return false;
    834 
    835 	(void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1);
    836 
    837 	return true;
    838 }
    839