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