Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_cstate.c revision 1.46
      1 /* $NetBSD: acpi_cpu_cstate.c,v 1.46 2011/02/25 19:55:06 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010, 2011 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.46 2011/02/25 19:55:06 jruoho Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/cpu.h>
     34 #include <sys/device.h>
     35 #include <sys/evcnt.h>
     36 #include <sys/kernel.h>
     37 #include <sys/once.h>
     38 #include <sys/mutex.h>
     39 #include <sys/timetc.h>
     40 
     41 #include <dev/acpi/acpireg.h>
     42 #include <dev/acpi/acpivar.h>
     43 #include <dev/acpi/acpi_cpu.h>
     44 #include <dev/acpi/acpi_timer.h>
     45 
     46 #include <machine/acpi_machdep.h>
     47 
     48 #define _COMPONENT	 ACPI_BUS_COMPONENT
     49 ACPI_MODULE_NAME	 ("acpi_cpu_cstate")
     50 
     51 static void		 acpicpu_cstate_attach_print(struct acpicpu_softc *);
     52 static void		 acpicpu_cstate_attach_evcnt(struct acpicpu_softc *);
     53 static void		 acpicpu_cstate_detach_evcnt(struct acpicpu_softc *);
     54 static ACPI_STATUS	 acpicpu_cstate_cst(struct acpicpu_softc *);
     55 static ACPI_STATUS	 acpicpu_cstate_cst_add(struct acpicpu_softc *,
     56 						ACPI_OBJECT *, int );
     57 static void		 acpicpu_cstate_cst_bios(void);
     58 static void		 acpicpu_cstate_memset(struct acpicpu_softc *);
     59 static ACPI_STATUS	 acpicpu_cstate_dep(struct acpicpu_softc *);
     60 static void		 acpicpu_cstate_fadt(struct acpicpu_softc *);
     61 static void		 acpicpu_cstate_quirks(struct acpicpu_softc *);
     62 static int		 acpicpu_cstate_latency(struct acpicpu_softc *);
     63 static bool		 acpicpu_cstate_bm_check(void);
     64 static void		 acpicpu_cstate_idle_enter(struct acpicpu_softc *,int);
     65 
     66 extern struct acpicpu_softc **acpicpu_sc;
     67 
     68 /*
     69  * XXX:	The local APIC timer (as well as TSC) is typically stopped in C3.
     70  *	For now, we cannot but disable C3. But there appears to be timer-
     71  *	related interrupt issues also in C2. The only entirely safe option
     72  *	at the moment is to use C1.
     73  */
     74 #ifdef ACPICPU_ENABLE_C3
     75 static int cs_state_max = ACPI_STATE_C3;
     76 #else
     77 static int cs_state_max = ACPI_STATE_C1;
     78 #endif
     79 
     80 void
     81 acpicpu_cstate_attach(device_t self)
     82 {
     83 	struct acpicpu_softc *sc = device_private(self);
     84 	ACPI_STATUS rv;
     85 
     86 	/*
     87 	 * Either use the preferred _CST or resort to FADT.
     88 	 */
     89 	rv = acpicpu_cstate_cst(sc);
     90 
     91 	switch (rv) {
     92 
     93 	case AE_OK:
     94 		acpicpu_cstate_cst_bios();
     95 		break;
     96 
     97 	default:
     98 		sc->sc_flags |= ACPICPU_FLAG_C_FADT;
     99 		acpicpu_cstate_fadt(sc);
    100 		break;
    101 	}
    102 
    103 	/*
    104 	 * Query the optional _CSD.
    105 	 */
    106 	rv = acpicpu_cstate_dep(sc);
    107 
    108 	if (ACPI_SUCCESS(rv))
    109 		sc->sc_flags |= ACPICPU_FLAG_C_DEP;
    110 
    111 	sc->sc_flags |= ACPICPU_FLAG_C;
    112 
    113 	acpicpu_cstate_quirks(sc);
    114 	acpicpu_cstate_attach_evcnt(sc);
    115 	acpicpu_cstate_attach_print(sc);
    116 }
    117 
    118 void
    119 acpicpu_cstate_attach_print(struct acpicpu_softc *sc)
    120 {
    121 	struct acpicpu_cstate *cs;
    122 	static bool once = false;
    123 	const char *str;
    124 	int i;
    125 
    126 	if (once != false)
    127 		return;
    128 
    129 	for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
    130 
    131 		cs = &sc->sc_cstate[i];
    132 
    133 		if (cs->cs_method == 0)
    134 			continue;
    135 
    136 		switch (cs->cs_method) {
    137 
    138 		case ACPICPU_C_STATE_HALT:
    139 			str = "HLT";
    140 			break;
    141 
    142 		case ACPICPU_C_STATE_FFH:
    143 			str = "FFH";
    144 			break;
    145 
    146 		case ACPICPU_C_STATE_SYSIO:
    147 			str = "I/O";
    148 			break;
    149 
    150 		default:
    151 			panic("NOTREACHED");
    152 		}
    153 
    154 		aprint_verbose_dev(sc->sc_dev, "C%d: %3s, "
    155 		    "lat %3u us, pow %5u mW, flags 0x%02x\n", i, str,
    156 		    cs->cs_latency, cs->cs_power, cs->cs_flags);
    157 	}
    158 
    159 	once = true;
    160 }
    161 
    162 static void
    163 acpicpu_cstate_attach_evcnt(struct acpicpu_softc *sc)
    164 {
    165 	struct acpicpu_cstate *cs;
    166 	const char *str;
    167 	int i;
    168 
    169 	for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
    170 
    171 		cs = &sc->sc_cstate[i];
    172 
    173 		if (cs->cs_method == 0)
    174 			continue;
    175 
    176 		str = "HALT";
    177 
    178 		if (cs->cs_method == ACPICPU_C_STATE_FFH)
    179 			str = "MWAIT";
    180 
    181 		if (cs->cs_method == ACPICPU_C_STATE_SYSIO)
    182 			str = "I/O";
    183 
    184 		(void)snprintf(cs->cs_name, sizeof(cs->cs_name),
    185 		    "C%d (%s)", i, str);
    186 
    187 		evcnt_attach_dynamic(&cs->cs_evcnt, EVCNT_TYPE_MISC,
    188 		    NULL, device_xname(sc->sc_dev), cs->cs_name);
    189 	}
    190 }
    191 
    192 int
    193 acpicpu_cstate_detach(device_t self)
    194 {
    195 	struct acpicpu_softc *sc = device_private(self);
    196 	static ONCE_DECL(once_detach);
    197 	int rv;
    198 
    199 	rv = RUN_ONCE(&once_detach, acpicpu_md_cstate_stop);
    200 
    201 	if (rv != 0)
    202 		return rv;
    203 
    204 	sc->sc_flags &= ~ACPICPU_FLAG_C;
    205 	acpicpu_cstate_detach_evcnt(sc);
    206 
    207 	return 0;
    208 }
    209 
    210 static void
    211 acpicpu_cstate_detach_evcnt(struct acpicpu_softc *sc)
    212 {
    213 	struct acpicpu_cstate *cs;
    214 	int i;
    215 
    216 	for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
    217 
    218 		cs = &sc->sc_cstate[i];
    219 
    220 		if (cs->cs_method != 0)
    221 			evcnt_detach(&cs->cs_evcnt);
    222 	}
    223 }
    224 
    225 void
    226 acpicpu_cstate_start(device_t self)
    227 {
    228 	struct acpicpu_softc *sc = device_private(self);
    229 
    230 	(void)acpicpu_md_cstate_start(sc);
    231 }
    232 
    233 bool
    234 acpicpu_cstate_suspend(device_t self)
    235 {
    236 	return true;
    237 }
    238 
    239 bool
    240 acpicpu_cstate_resume(device_t self)
    241 {
    242 	static const ACPI_OSD_EXEC_CALLBACK func = acpicpu_cstate_callback;
    243 	struct acpicpu_softc *sc = device_private(self);
    244 
    245 	if ((sc->sc_flags & ACPICPU_FLAG_C_FADT) == 0)
    246 		(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
    247 
    248 	return true;
    249 }
    250 
    251 void
    252 acpicpu_cstate_callback(void *aux)
    253 {
    254 	struct acpicpu_softc *sc;
    255 	device_t self = aux;
    256 
    257 	sc = device_private(self);
    258 
    259 	if ((sc->sc_flags & ACPICPU_FLAG_C_FADT) != 0)
    260 		return;
    261 
    262 	mutex_enter(&sc->sc_mtx);
    263 	(void)acpicpu_cstate_cst(sc);
    264 	mutex_exit(&sc->sc_mtx);
    265 }
    266 
    267 static ACPI_STATUS
    268 acpicpu_cstate_cst(struct acpicpu_softc *sc)
    269 {
    270 	ACPI_OBJECT *elm, *obj;
    271 	ACPI_BUFFER buf;
    272 	ACPI_STATUS rv;
    273 	uint32_t i, n;
    274 	uint8_t count;
    275 
    276 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_CST", &buf);
    277 
    278 	if (ACPI_FAILURE(rv))
    279 		return rv;
    280 
    281 	obj = buf.Pointer;
    282 
    283 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    284 		rv = AE_TYPE;
    285 		goto out;
    286 	}
    287 
    288 	if (obj->Package.Count < 2) {
    289 		rv = AE_LIMIT;
    290 		goto out;
    291 	}
    292 
    293 	elm = obj->Package.Elements;
    294 
    295 	if (elm[0].Type != ACPI_TYPE_INTEGER) {
    296 		rv = AE_TYPE;
    297 		goto out;
    298 	}
    299 
    300 	n = elm[0].Integer.Value;
    301 
    302 	if (n != obj->Package.Count - 1) {
    303 		rv = AE_BAD_VALUE;
    304 		goto out;
    305 	}
    306 
    307 	if (n > ACPI_C_STATES_MAX) {
    308 		rv = AE_LIMIT;
    309 		goto out;
    310 	}
    311 
    312 	acpicpu_cstate_memset(sc);
    313 
    314 	CTASSERT(ACPI_STATE_C0 == 0 && ACPI_STATE_C1 == 1);
    315 	CTASSERT(ACPI_STATE_C2 == 2 && ACPI_STATE_C3 == 3);
    316 
    317 	for (count = 0, i = 1; i <= n; i++) {
    318 
    319 		elm = &obj->Package.Elements[i];
    320 		rv = acpicpu_cstate_cst_add(sc, elm, i);
    321 
    322 		if (ACPI_SUCCESS(rv))
    323 			count++;
    324 	}
    325 
    326 	rv = (count != 0) ? AE_OK : AE_NOT_EXIST;
    327 
    328 out:
    329 	if (buf.Pointer != NULL)
    330 		ACPI_FREE(buf.Pointer);
    331 
    332 	return rv;
    333 }
    334 
    335 static ACPI_STATUS
    336 acpicpu_cstate_cst_add(struct acpicpu_softc *sc, ACPI_OBJECT *elm, int i)
    337 {
    338 	struct acpicpu_cstate *cs = sc->sc_cstate;
    339 	struct acpicpu_cstate state;
    340 	struct acpicpu_reg *reg;
    341 	ACPI_STATUS rv = AE_OK;
    342 	ACPI_OBJECT *obj;
    343 	uint32_t type;
    344 
    345 	(void)memset(&state, 0, sizeof(*cs));
    346 
    347 	state.cs_flags = ACPICPU_FLAG_C_BM_STS;
    348 
    349 	if (elm->Type != ACPI_TYPE_PACKAGE) {
    350 		rv = AE_TYPE;
    351 		goto out;
    352 	}
    353 
    354 	if (elm->Package.Count != 4) {
    355 		rv = AE_LIMIT;
    356 		goto out;
    357 	}
    358 
    359 	/*
    360 	 * Type.
    361 	 */
    362 	obj = &elm->Package.Elements[1];
    363 
    364 	if (obj->Type != ACPI_TYPE_INTEGER) {
    365 		rv = AE_TYPE;
    366 		goto out;
    367 	}
    368 
    369 	type = obj->Integer.Value;
    370 
    371 	if (type < ACPI_STATE_C1 || type > ACPI_STATE_C3) {
    372 		rv = AE_TYPE;
    373 		goto out;
    374 	}
    375 
    376 	/*
    377 	 * Latency.
    378 	 */
    379 	obj = &elm->Package.Elements[2];
    380 
    381 	if (obj->Type != ACPI_TYPE_INTEGER) {
    382 		rv = AE_TYPE;
    383 		goto out;
    384 	}
    385 
    386 	state.cs_latency = obj->Integer.Value;
    387 
    388 	/*
    389 	 * Power.
    390 	 */
    391 	obj = &elm->Package.Elements[3];
    392 
    393 	if (obj->Type != ACPI_TYPE_INTEGER) {
    394 		rv = AE_TYPE;
    395 		goto out;
    396 	}
    397 
    398 	state.cs_power = obj->Integer.Value;
    399 
    400 	/*
    401 	 * Register.
    402 	 */
    403 	obj = &elm->Package.Elements[0];
    404 
    405 	if (obj->Type != ACPI_TYPE_BUFFER) {
    406 		rv = AE_TYPE;
    407 		goto out;
    408 	}
    409 
    410 	CTASSERT(sizeof(struct acpicpu_reg) == 15);
    411 
    412 	if (obj->Buffer.Length < sizeof(struct acpicpu_reg)) {
    413 		rv = AE_LIMIT;
    414 		goto out;
    415 	}
    416 
    417 	reg = (struct acpicpu_reg *)obj->Buffer.Pointer;
    418 
    419 	switch (reg->reg_spaceid) {
    420 
    421 	case ACPI_ADR_SPACE_SYSTEM_IO:
    422 		state.cs_method = ACPICPU_C_STATE_SYSIO;
    423 
    424 		if (reg->reg_addr == 0) {
    425 			rv = AE_AML_ILLEGAL_ADDRESS;
    426 			goto out;
    427 		}
    428 
    429 		if (reg->reg_bitwidth != 8) {
    430 			rv = AE_AML_BAD_RESOURCE_LENGTH;
    431 			goto out;
    432 		}
    433 
    434 		state.cs_addr = reg->reg_addr;
    435 		break;
    436 
    437 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    438 		state.cs_method = ACPICPU_C_STATE_FFH;
    439 
    440 		switch (type) {
    441 
    442 		case ACPI_STATE_C1:
    443 
    444 			if ((sc->sc_flags & ACPICPU_FLAG_C_FFH) == 0)
    445 				state.cs_method = ACPICPU_C_STATE_HALT;
    446 
    447 			break;
    448 
    449 		default:
    450 
    451 			if ((sc->sc_flags & ACPICPU_FLAG_C_FFH) == 0) {
    452 				rv = AE_SUPPORT;
    453 				goto out;
    454 			}
    455 		}
    456 
    457 		if (sc->sc_cap != 0) {
    458 
    459 			/*
    460 			 * The _CST FFH GAS encoding may contain
    461 			 * additional hints on Intel processors.
    462 			 * Use these to determine whether we can
    463 			 * avoid the bus master activity check.
    464 			 */
    465 			if ((reg->reg_accesssize & ACPICPU_PDC_GAS_BM) == 0)
    466 				state.cs_flags &= ~ACPICPU_FLAG_C_BM_STS;
    467 		}
    468 
    469 		break;
    470 
    471 	default:
    472 		rv = AE_AML_INVALID_SPACE_ID;
    473 		goto out;
    474 	}
    475 
    476 	/*
    477 	 * As some systems define the type arbitrarily,
    478 	 * we use a sequential counter instead of the
    479 	 * BIOS data. For instance, AMD family 14h is
    480 	 * instructed to only use the value 2; see
    481 	 *
    482 	 *	Advanced Micro Devices: BIOS and Kernel
    483 	 *	Developer's Guide (BKDG) for AMD Family
    484 	 *	14h Models 00h-0Fh Processors. Revision
    485 	 *	3.00, January 4, 2011.
    486 	 */
    487 	if (i != (int)type) {
    488 
    489 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
    490 			"C%d != C%u from BIOS", i, type));
    491 	}
    492 
    493 	KASSERT(cs[i].cs_method == 0);
    494 
    495 	cs[i].cs_addr = state.cs_addr;
    496 	cs[i].cs_power = state.cs_power;
    497 	cs[i].cs_flags = state.cs_flags;
    498 	cs[i].cs_method = state.cs_method;
    499 	cs[i].cs_latency = state.cs_latency;
    500 
    501 out:
    502 	if (ACPI_FAILURE(rv))
    503 		aprint_error_dev(sc->sc_dev, "failed to add "
    504 		    "C-state: %s\n", AcpiFormatException(rv));
    505 
    506 	i++;
    507 
    508 	return rv;
    509 }
    510 
    511 static void
    512 acpicpu_cstate_cst_bios(void)
    513 {
    514 	const uint8_t val = AcpiGbl_FADT.CstControl;
    515 	const uint32_t addr = AcpiGbl_FADT.SmiCommand;
    516 
    517 	if (addr == 0 || val == 0)
    518 		return;
    519 
    520 	(void)AcpiOsWritePort(addr, val, 8);
    521 }
    522 
    523 static void
    524 acpicpu_cstate_memset(struct acpicpu_softc *sc)
    525 {
    526 	int i = 0;
    527 
    528 	while (i < ACPI_C_STATE_COUNT) {
    529 
    530 		sc->sc_cstate[i].cs_addr = 0;
    531 		sc->sc_cstate[i].cs_power = 0;
    532 		sc->sc_cstate[i].cs_flags = 0;
    533 		sc->sc_cstate[i].cs_method = 0;
    534 		sc->sc_cstate[i].cs_latency = 0;
    535 
    536 		i++;
    537 	}
    538 }
    539 
    540 static ACPI_STATUS
    541 acpicpu_cstate_dep(struct acpicpu_softc *sc)
    542 {
    543 	ACPI_OBJECT *elm, *obj;
    544 	ACPI_BUFFER buf;
    545 	ACPI_STATUS rv;
    546 	uint32_t val;
    547 	uint8_t i, n;
    548 
    549 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_CSD", &buf);
    550 
    551 	if (ACPI_FAILURE(rv))
    552 		goto out;
    553 
    554 	obj = buf.Pointer;
    555 
    556 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    557 		rv = AE_TYPE;
    558 		goto out;
    559 	}
    560 
    561 	if (obj->Package.Count != 1) {
    562 		rv = AE_LIMIT;
    563 		goto out;
    564 	}
    565 
    566 	elm = &obj->Package.Elements[0];
    567 
    568 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    569 		rv = AE_TYPE;
    570 		goto out;
    571 	}
    572 
    573 	n = elm->Package.Count;
    574 
    575 	if (n != 6) {
    576 		rv = AE_LIMIT;
    577 		goto out;
    578 	}
    579 
    580 	elm = elm->Package.Elements;
    581 
    582 	for (i = 0; i < n; i++) {
    583 
    584 		if (elm[i].Type != ACPI_TYPE_INTEGER) {
    585 			rv = AE_TYPE;
    586 			goto out;
    587 		}
    588 
    589 		if (elm[i].Integer.Value > UINT32_MAX) {
    590 			rv = AE_AML_NUMERIC_OVERFLOW;
    591 			goto out;
    592 		}
    593 	}
    594 
    595 	val = elm[1].Integer.Value;
    596 
    597 	if (val != 0)
    598 		aprint_debug_dev(sc->sc_dev, "invalid revision in _CSD\n");
    599 
    600 	val = elm[3].Integer.Value;
    601 
    602 	if (val < ACPICPU_DEP_SW_ALL || val > ACPICPU_DEP_HW_ALL) {
    603 		rv = AE_AML_BAD_RESOURCE_VALUE;
    604 		goto out;
    605 	}
    606 
    607 	val = elm[4].Integer.Value;
    608 
    609 	if (val > sc->sc_ncpus) {
    610 		rv = AE_BAD_VALUE;
    611 		goto out;
    612 	}
    613 
    614 	sc->sc_cstate_dep.dep_domain = elm[2].Integer.Value;
    615 	sc->sc_cstate_dep.dep_type   = elm[3].Integer.Value;
    616 	sc->sc_cstate_dep.dep_ncpus  = elm[4].Integer.Value;
    617 	sc->sc_cstate_dep.dep_index  = elm[5].Integer.Value;
    618 
    619 out:
    620 	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
    621 		aprint_debug_dev(sc->sc_dev, "failed to evaluate "
    622 		    "_CSD: %s\n", AcpiFormatException(rv));
    623 
    624 	if (buf.Pointer != NULL)
    625 		ACPI_FREE(buf.Pointer);
    626 
    627 	return rv;
    628 }
    629 
    630 static void
    631 acpicpu_cstate_fadt(struct acpicpu_softc *sc)
    632 {
    633 	struct acpicpu_cstate *cs = sc->sc_cstate;
    634 
    635 	acpicpu_cstate_memset(sc);
    636 
    637 	/*
    638 	 * All x86 processors should support C1 (a.k.a. HALT).
    639 	 */
    640 	cs[ACPI_STATE_C1].cs_method = ACPICPU_C_STATE_HALT;
    641 
    642 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_C1_SUPPORTED) == 0)
    643 		aprint_debug_dev(sc->sc_dev, "HALT not supported?\n");
    644 
    645 	if (sc->sc_object.ao_pblkaddr == 0)
    646 		return;
    647 
    648 	if (sc->sc_ncpus > 1) {
    649 
    650 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_C2_MP_SUPPORTED) == 0)
    651 			return;
    652 	}
    653 
    654 	cs[ACPI_STATE_C2].cs_method = ACPICPU_C_STATE_SYSIO;
    655 	cs[ACPI_STATE_C3].cs_method = ACPICPU_C_STATE_SYSIO;
    656 
    657 	cs[ACPI_STATE_C2].cs_latency = AcpiGbl_FADT.C2Latency;
    658 	cs[ACPI_STATE_C3].cs_latency = AcpiGbl_FADT.C3Latency;
    659 
    660 	cs[ACPI_STATE_C2].cs_addr = sc->sc_object.ao_pblkaddr + 4;
    661 	cs[ACPI_STATE_C3].cs_addr = sc->sc_object.ao_pblkaddr + 5;
    662 
    663 	/*
    664 	 * The P_BLK length should always be 6. If it
    665 	 * is not, reduce functionality accordingly.
    666 	 */
    667 	if (sc->sc_object.ao_pblklen < 5)
    668 		cs[ACPI_STATE_C2].cs_method = 0;
    669 
    670 	if (sc->sc_object.ao_pblklen < 6)
    671 		cs[ACPI_STATE_C3].cs_method = 0;
    672 
    673 	/*
    674 	 * Sanity check the latency levels in FADT.
    675 	 * Values above the thresholds are used to
    676 	 * inform that C-states are not supported.
    677 	 */
    678 	CTASSERT(ACPICPU_C_C2_LATENCY_MAX == 100);
    679 	CTASSERT(ACPICPU_C_C3_LATENCY_MAX == 1000);
    680 
    681 	if (AcpiGbl_FADT.C2Latency > ACPICPU_C_C2_LATENCY_MAX)
    682 		cs[ACPI_STATE_C2].cs_method = 0;
    683 
    684 	if (AcpiGbl_FADT.C3Latency > ACPICPU_C_C3_LATENCY_MAX)
    685 		cs[ACPI_STATE_C3].cs_method = 0;
    686 }
    687 
    688 static void
    689 acpicpu_cstate_quirks(struct acpicpu_softc *sc)
    690 {
    691 	const uint32_t reg = AcpiGbl_FADT.Pm2ControlBlock;
    692 	const uint32_t len = AcpiGbl_FADT.Pm2ControlLength;
    693 
    694 	/*
    695 	 * Disable C3 for PIIX4.
    696 	 */
    697 	if ((sc->sc_flags & ACPICPU_FLAG_PIIX4) != 0) {
    698 		sc->sc_cstate[ACPI_STATE_C3].cs_method = 0;
    699 		return;
    700 	}
    701 
    702 	/*
    703 	 * Check bus master arbitration. If ARB_DIS
    704 	 * is not available, processor caches must be
    705 	 * flushed before C3 (ACPI 4.0, section 8.2).
    706 	 */
    707 	if (reg != 0 && len != 0) {
    708 		sc->sc_flags |= ACPICPU_FLAG_C_ARB;
    709 		return;
    710 	}
    711 
    712 	/*
    713 	 * Disable C3 entirely if WBINVD is not present.
    714 	 */
    715 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD) == 0)
    716 		sc->sc_cstate[ACPI_STATE_C3].cs_method = 0;
    717 	else {
    718 		/*
    719 		 * If WBINVD is present and functioning properly,
    720 		 * flush all processor caches before entering C3.
    721 		 */
    722 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD_FLUSH) == 0)
    723 			sc->sc_flags &= ~ACPICPU_FLAG_C_BM;
    724 		else
    725 			sc->sc_cstate[ACPI_STATE_C3].cs_method = 0;
    726 	}
    727 }
    728 
    729 static int
    730 acpicpu_cstate_latency(struct acpicpu_softc *sc)
    731 {
    732 	static const uint32_t cs_factor = 3;
    733 	struct acpicpu_cstate *cs;
    734 	int i;
    735 
    736 	for (i = cs_state_max; i > 0; i--) {
    737 
    738 		cs = &sc->sc_cstate[i];
    739 
    740 		if (__predict_false(cs->cs_method == 0))
    741 			continue;
    742 
    743 		/*
    744 		 * Choose a state if we have previously slept
    745 		 * longer than the worst case latency of the
    746 		 * state times an arbitrary multiplier.
    747 		 */
    748 		if (sc->sc_cstate_sleep > cs->cs_latency * cs_factor)
    749 			return i;
    750 	}
    751 
    752 	return ACPI_STATE_C1;
    753 }
    754 
    755 /*
    756  * The main idle loop.
    757  */
    758 void
    759 acpicpu_cstate_idle(void)
    760 {
    761 	struct cpu_info *ci = curcpu();
    762 	struct acpicpu_softc *sc;
    763 	int state;
    764 
    765 	acpi_md_OsDisableInterrupt();
    766 
    767 	if (__predict_false(ci->ci_want_resched != 0))
    768 		goto out;
    769 
    770 	KASSERT(acpicpu_sc != NULL);
    771 	KASSERT(ci->ci_acpiid < maxcpus);
    772 
    773 	sc = acpicpu_sc[ci->ci_acpiid];
    774 
    775 	if (__predict_false(sc == NULL))
    776 		goto out;
    777 
    778 	KASSERT(ci->ci_ilevel == IPL_NONE);
    779 	KASSERT((sc->sc_flags & ACPICPU_FLAG_C) != 0);
    780 
    781 	if (__predict_false(sc->sc_cold != false))
    782 		goto out;
    783 
    784 	if (__predict_false(mutex_tryenter(&sc->sc_mtx) == 0))
    785 		goto out;
    786 
    787 	mutex_exit(&sc->sc_mtx);
    788 	state = acpicpu_cstate_latency(sc);
    789 
    790 	/*
    791 	 * Apply AMD C1E quirk.
    792 	 */
    793 	if ((sc->sc_flags & ACPICPU_FLAG_C_C1E) != 0)
    794 		acpicpu_md_quirk_c1e();
    795 
    796 	/*
    797 	 * Check for bus master activity. Note that particularly usb(4)
    798 	 * causes high activity, which may prevent the use of C3 states.
    799 	 */
    800 	if ((sc->sc_cstate[state].cs_flags & ACPICPU_FLAG_C_BM_STS) != 0) {
    801 
    802 		if (acpicpu_cstate_bm_check() != false)
    803 			state--;
    804 
    805 		if (__predict_false(sc->sc_cstate[state].cs_method == 0))
    806 			state = ACPI_STATE_C1;
    807 	}
    808 
    809 	KASSERT(state != ACPI_STATE_C0);
    810 
    811 	if (state != ACPI_STATE_C3) {
    812 		acpicpu_cstate_idle_enter(sc, state);
    813 		return;
    814 	}
    815 
    816 	/*
    817 	 * On all recent (Intel) CPUs caches are shared
    818 	 * by CPUs and bus master control is required to
    819 	 * keep these coherent while in C3. Flushing the
    820 	 * CPU caches is only the last resort.
    821 	 */
    822 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) == 0)
    823 		ACPI_FLUSH_CPU_CACHE();
    824 
    825 	/*
    826 	 * Allow the bus master to request that any given
    827 	 * CPU should return immediately to C0 from C3.
    828 	 */
    829 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
    830 		(void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
    831 
    832 	/*
    833 	 * It may be necessary to disable bus master arbitration
    834 	 * to ensure that bus master cycles do not occur while
    835 	 * sleeping in C3 (see ACPI 4.0, section 8.1.4).
    836 	 */
    837 	if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
    838 		(void)AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 1);
    839 
    840 	acpicpu_cstate_idle_enter(sc, state);
    841 
    842 	/*
    843 	 * Disable bus master wake and re-enable the arbiter.
    844 	 */
    845 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
    846 		(void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
    847 
    848 	if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
    849 		(void)AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 0);
    850 
    851 	return;
    852 
    853 out:
    854 	acpi_md_OsEnableInterrupt();
    855 }
    856 
    857 static void
    858 acpicpu_cstate_idle_enter(struct acpicpu_softc *sc, int state)
    859 {
    860 	struct acpicpu_cstate *cs = &sc->sc_cstate[state];
    861 	uint32_t end, start, val;
    862 
    863 	start = acpitimer_read_fast(NULL);
    864 
    865 	switch (cs->cs_method) {
    866 
    867 	case ACPICPU_C_STATE_FFH:
    868 	case ACPICPU_C_STATE_HALT:
    869 		acpicpu_md_cstate_enter(cs->cs_method, state);
    870 		break;
    871 
    872 	case ACPICPU_C_STATE_SYSIO:
    873 		(void)AcpiOsReadPort(cs->cs_addr, &val, 8);
    874 		break;
    875 	}
    876 
    877 	acpi_md_OsEnableInterrupt();
    878 
    879 	cs->cs_evcnt.ev_count++;
    880 	end = acpitimer_read_fast(NULL);
    881 	sc->sc_cstate_sleep = hztoms(acpitimer_delta(end, start)) * 1000;
    882 }
    883 
    884 static bool
    885 acpicpu_cstate_bm_check(void)
    886 {
    887 	uint32_t val = 0;
    888 	ACPI_STATUS rv;
    889 
    890 	rv = AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, &val);
    891 
    892 	if (ACPI_FAILURE(rv) || val == 0)
    893 		return false;
    894 
    895 	(void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1);
    896 
    897 	return true;
    898 }
    899