Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_cstate.c revision 1.28
      1 /* $NetBSD: acpi_cpu_cstate.c,v 1.28 2010/08/15 08:53:19 jruoho 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.28 2010/08/15 08:53:19 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 *);
     57 static void		 acpicpu_cstate_cst_bios(void);
     58 static void		 acpicpu_cstate_memset(struct acpicpu_softc *);
     59 static void		 acpicpu_cstate_fadt(struct acpicpu_softc *);
     60 static void		 acpicpu_cstate_quirks(struct acpicpu_softc *);
     61 static int		 acpicpu_cstate_latency(struct acpicpu_softc *);
     62 static bool		 acpicpu_cstate_bm_check(void);
     63 static void		 acpicpu_cstate_idle_enter(struct acpicpu_softc *,int);
     64 
     65 extern struct acpicpu_softc **acpicpu_sc;
     66 
     67 /*
     68  * XXX:	The local APIC timer (as well as TSC) is typically stopped in C3.
     69  *	For now, we cannot but disable C3. But there appears to be timer-
     70  *	related interrupt issues also in C2. The only entirely safe option
     71  *	at the moment is to use C1.
     72  */
     73 #ifdef ACPICPU_ENABLE_C3
     74 static int cs_state_max = ACPI_STATE_C3;
     75 #else
     76 static int cs_state_max = ACPI_STATE_C1;
     77 #endif
     78 
     79 void
     80 acpicpu_cstate_attach(device_t self)
     81 {
     82 	struct acpicpu_softc *sc = device_private(self);
     83 	ACPI_STATUS rv;
     84 
     85 	/*
     86 	 * Either use the preferred _CST or resort to FADT.
     87 	 */
     88 	rv = acpicpu_cstate_cst(sc);
     89 
     90 	switch (rv) {
     91 
     92 	case AE_OK:
     93 		acpicpu_cstate_cst_bios();
     94 		break;
     95 
     96 	default:
     97 		sc->sc_flags |= ACPICPU_FLAG_C_FADT;
     98 		acpicpu_cstate_fadt(sc);
     99 		break;
    100 	}
    101 
    102 	acpicpu_cstate_quirks(sc);
    103 	acpicpu_cstate_attach_evcnt(sc);
    104 	acpicpu_cstate_attach_print(sc);
    105 }
    106 
    107 void
    108 acpicpu_cstate_attach_print(struct acpicpu_softc *sc)
    109 {
    110 	struct acpicpu_cstate *cs;
    111 	static bool once = false;
    112 	const char *str;
    113 	int i;
    114 
    115 	if (once != false)
    116 		return;
    117 
    118 	for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
    119 
    120 		cs = &sc->sc_cstate[i];
    121 
    122 		if (cs->cs_method == 0)
    123 			continue;
    124 
    125 		switch (cs->cs_method) {
    126 
    127 		case ACPICPU_C_STATE_HALT:
    128 			str = "HLT";
    129 			break;
    130 
    131 		case ACPICPU_C_STATE_FFH:
    132 			str = "FFH";
    133 			break;
    134 
    135 		case ACPICPU_C_STATE_SYSIO:
    136 			str = "I/O";
    137 			break;
    138 
    139 		default:
    140 			panic("NOTREACHED");
    141 		}
    142 
    143 		aprint_debug_dev(sc->sc_dev, "C%d: %3s, "
    144 		    "lat %3u us, pow %5u mW, flags 0x%02x\n", i, str,
    145 		    cs->cs_latency, cs->cs_power, cs->cs_flags);
    146 	}
    147 
    148 	once = true;
    149 }
    150 
    151 static void
    152 acpicpu_cstate_attach_evcnt(struct acpicpu_softc *sc)
    153 {
    154 	struct acpicpu_cstate *cs;
    155 	const char *str;
    156 	int i;
    157 
    158 	for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
    159 
    160 		cs = &sc->sc_cstate[i];
    161 
    162 		if (cs->cs_method == 0)
    163 			continue;
    164 
    165 		str = "HALT";
    166 
    167 		if (cs->cs_method == ACPICPU_C_STATE_FFH)
    168 			str = "MWAIT";
    169 
    170 		if (cs->cs_method == ACPICPU_C_STATE_SYSIO)
    171 			str = "I/O";
    172 
    173 		(void)snprintf(cs->cs_name, sizeof(cs->cs_name),
    174 		    "C%d (%s)", i, str);
    175 
    176 		evcnt_attach_dynamic(&cs->cs_evcnt, EVCNT_TYPE_MISC,
    177 		    NULL, device_xname(sc->sc_dev), cs->cs_name);
    178 	}
    179 }
    180 
    181 int
    182 acpicpu_cstate_detach(device_t self)
    183 {
    184 	struct acpicpu_softc *sc = device_private(self);
    185 	static ONCE_DECL(once_detach);
    186 	int rv;
    187 
    188 	rv = RUN_ONCE(&once_detach, acpicpu_md_idle_stop);
    189 
    190 	if (rv != 0)
    191 		return rv;
    192 
    193 	sc->sc_flags &= ~ACPICPU_FLAG_C;
    194 	acpicpu_cstate_detach_evcnt(sc);
    195 
    196 	return 0;
    197 }
    198 
    199 static void
    200 acpicpu_cstate_detach_evcnt(struct acpicpu_softc *sc)
    201 {
    202 	struct acpicpu_cstate *cs;
    203 	int i;
    204 
    205 	for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
    206 
    207 		cs = &sc->sc_cstate[i];
    208 
    209 		if (cs->cs_method != 0)
    210 			evcnt_detach(&cs->cs_evcnt);
    211 	}
    212 }
    213 
    214 int
    215 acpicpu_cstate_start(device_t self)
    216 {
    217 	struct acpicpu_softc *sc = device_private(self);
    218 	static ONCE_DECL(once_start);
    219 	int rv;
    220 
    221 	/*
    222 	 * Save the existing idle-mechanism and claim the cpu_idle(9).
    223 	 * This should be called after all ACPI CPUs have been attached.
    224 	 */
    225 	rv = RUN_ONCE(&once_start, acpicpu_md_idle_start);
    226 
    227 	if (rv == 0)
    228 		sc->sc_flags |= ACPICPU_FLAG_C;
    229 
    230 	return rv;
    231 }
    232 
    233 bool
    234 acpicpu_cstate_suspend(device_t self)
    235 {
    236 
    237 	return true;
    238 }
    239 
    240 bool
    241 acpicpu_cstate_resume(device_t self)
    242 {
    243 	struct acpicpu_softc *sc = device_private(self);
    244 
    245 	if ((sc->sc_flags & ACPICPU_FLAG_C_FADT) == 0)
    246 		acpicpu_cstate_callback(self);
    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);
    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)
    337 {
    338 	const struct acpicpu_object *ao = &sc->sc_object;
    339 	struct acpicpu_cstate *cs = sc->sc_cstate;
    340 	struct acpicpu_cstate state;
    341 	struct acpicpu_reg *reg;
    342 	ACPI_STATUS rv = AE_OK;
    343 	ACPI_OBJECT *obj;
    344 	uint32_t type;
    345 
    346 	(void)memset(&state, 0, sizeof(*cs));
    347 
    348 	state.cs_flags = ACPICPU_FLAG_C_BM_STS;
    349 
    350 	if (elm->Type != ACPI_TYPE_PACKAGE) {
    351 		rv = AE_TYPE;
    352 		goto out;
    353 	}
    354 
    355 	if (elm->Package.Count != 4) {
    356 		rv = AE_LIMIT;
    357 		goto out;
    358 	}
    359 
    360 	/*
    361 	 * Type.
    362 	 */
    363 	obj = &elm->Package.Elements[1];
    364 
    365 	if (obj->Type != ACPI_TYPE_INTEGER) {
    366 		rv = AE_TYPE;
    367 		goto out;
    368 	}
    369 
    370 	type = obj->Integer.Value;
    371 
    372 	if (type < ACPI_STATE_C1 || type > ACPI_STATE_C3) {
    373 		rv = AE_TYPE;
    374 		goto out;
    375 	}
    376 
    377 	/*
    378 	 * Latency.
    379 	 */
    380 	obj = &elm->Package.Elements[2];
    381 
    382 	if (obj->Type != ACPI_TYPE_INTEGER) {
    383 		rv = AE_TYPE;
    384 		goto out;
    385 	}
    386 
    387 	state.cs_latency = obj->Integer.Value;
    388 
    389 	/*
    390 	 * Power.
    391 	 */
    392 	obj = &elm->Package.Elements[3];
    393 
    394 	if (obj->Type != ACPI_TYPE_INTEGER) {
    395 		rv = AE_TYPE;
    396 		goto out;
    397 	}
    398 
    399 	state.cs_power = obj->Integer.Value;
    400 
    401 	/*
    402 	 * Register.
    403 	 */
    404 	obj = &elm->Package.Elements[0];
    405 
    406 	if (obj->Type != ACPI_TYPE_BUFFER) {
    407 		rv = AE_TYPE;
    408 		goto out;
    409 	}
    410 
    411 	CTASSERT(sizeof(struct acpicpu_reg) == 15);
    412 
    413 	if (obj->Buffer.Length < sizeof(struct acpicpu_reg)) {
    414 		rv = AE_LIMIT;
    415 		goto out;
    416 	}
    417 
    418 	reg = (struct acpicpu_reg *)obj->Buffer.Pointer;
    419 
    420 	switch (reg->reg_spaceid) {
    421 
    422 	case ACPI_ADR_SPACE_SYSTEM_IO:
    423 		state.cs_method = ACPICPU_C_STATE_SYSIO;
    424 
    425 		if (reg->reg_addr == 0) {
    426 			rv = AE_AML_ILLEGAL_ADDRESS;
    427 			goto out;
    428 		}
    429 
    430 		if (reg->reg_bitwidth != 8) {
    431 			rv = AE_AML_BAD_RESOURCE_LENGTH;
    432 			goto out;
    433 		}
    434 
    435 		/*
    436 		 * Check only that the address is in the mapped space.
    437 		 * Systems are allowed to change it when operating
    438 		 * with _CST (see ACPI 4.0, pp. 94-95). For instance,
    439 		 * the offset of P_LVL3 may change depending on whether
    440 		 * acpiacad(4) is connected or disconnected.
    441 		 */
    442 		if (reg->reg_addr > ao->ao_pblkaddr + ao->ao_pblklen) {
    443 			rv = AE_BAD_ADDRESS;
    444 			goto out;
    445 		}
    446 
    447 		state.cs_addr = reg->reg_addr;
    448 		break;
    449 
    450 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    451 		state.cs_method = ACPICPU_C_STATE_FFH;
    452 
    453 		switch (type) {
    454 
    455 		case ACPI_STATE_C1:
    456 
    457 			if ((sc->sc_flags & ACPICPU_FLAG_C_FFH) == 0)
    458 				state.cs_method = ACPICPU_C_STATE_HALT;
    459 
    460 			break;
    461 
    462 		default:
    463 
    464 			if ((sc->sc_flags & ACPICPU_FLAG_C_FFH) == 0) {
    465 				rv = AE_SUPPORT;
    466 				goto out;
    467 			}
    468 		}
    469 
    470 		if (sc->sc_cap != 0) {
    471 
    472 			/*
    473 			 * The _CST FFH GAS encoding may contain
    474 			 * additional hints on Intel processors.
    475 			 * Use these to determine whether we can
    476 			 * avoid the bus master activity check.
    477 			 */
    478 			if ((reg->reg_accesssize & ACPICPU_PDC_GAS_BM) == 0)
    479 				state.cs_flags &= ~ACPICPU_FLAG_C_BM_STS;
    480 		}
    481 
    482 		break;
    483 
    484 	default:
    485 		rv = AE_AML_INVALID_SPACE_ID;
    486 		goto out;
    487 	}
    488 
    489 	if (cs[type].cs_method != 0) {
    490 		rv = AE_ALREADY_EXISTS;
    491 		goto out;
    492 	}
    493 
    494 	cs[type].cs_addr = state.cs_addr;
    495 	cs[type].cs_power = state.cs_power;
    496 	cs[type].cs_flags = state.cs_flags;
    497 	cs[type].cs_method = state.cs_method;
    498 	cs[type].cs_latency = state.cs_latency;
    499 
    500 out:
    501 	if (ACPI_FAILURE(rv))
    502 		aprint_debug_dev(sc->sc_dev, "invalid "
    503 		    "_CST: %s\n", AcpiFormatException(rv));
    504 
    505 	return rv;
    506 }
    507 
    508 static void
    509 acpicpu_cstate_cst_bios(void)
    510 {
    511 	const uint8_t val = AcpiGbl_FADT.CstControl;
    512 	const uint32_t addr = AcpiGbl_FADT.SmiCommand;
    513 
    514 	if (addr == 0 || val == 0)
    515 		return;
    516 
    517 	(void)AcpiOsWritePort(addr, val, 8);
    518 }
    519 
    520 static void
    521 acpicpu_cstate_memset(struct acpicpu_softc *sc)
    522 {
    523 	int i = 0;
    524 
    525 	while (i < ACPI_C_STATE_COUNT) {
    526 
    527 		sc->sc_cstate[i].cs_addr = 0;
    528 		sc->sc_cstate[i].cs_power = 0;
    529 		sc->sc_cstate[i].cs_flags = 0;
    530 		sc->sc_cstate[i].cs_method = 0;
    531 		sc->sc_cstate[i].cs_latency = 0;
    532 
    533 		i++;
    534 	}
    535 }
    536 
    537 static void
    538 acpicpu_cstate_fadt(struct acpicpu_softc *sc)
    539 {
    540 	struct acpicpu_cstate *cs = sc->sc_cstate;
    541 
    542 	acpicpu_cstate_memset(sc);
    543 
    544 	/*
    545 	 * All x86 processors should support C1 (a.k.a. HALT).
    546 	 */
    547 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_C1_SUPPORTED) != 0)
    548 		cs[ACPI_STATE_C1].cs_method = ACPICPU_C_STATE_HALT;
    549 
    550 	if (sc->sc_object.ao_pblkaddr == 0)
    551 		return;
    552 
    553 	if (acpicpu_md_cpus_running() > 1) {
    554 
    555 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_C2_MP_SUPPORTED) == 0)
    556 			return;
    557 	}
    558 
    559 	cs[ACPI_STATE_C2].cs_method = ACPICPU_C_STATE_SYSIO;
    560 	cs[ACPI_STATE_C3].cs_method = ACPICPU_C_STATE_SYSIO;
    561 
    562 	cs[ACPI_STATE_C2].cs_latency = AcpiGbl_FADT.C2Latency;
    563 	cs[ACPI_STATE_C3].cs_latency = AcpiGbl_FADT.C3Latency;
    564 
    565 	cs[ACPI_STATE_C2].cs_addr = sc->sc_object.ao_pblkaddr + 4;
    566 	cs[ACPI_STATE_C3].cs_addr = sc->sc_object.ao_pblkaddr + 5;
    567 
    568 	/*
    569 	 * The P_BLK length should always be 6. If it
    570 	 * is not, reduce functionality accordingly.
    571 	 */
    572 	if (sc->sc_object.ao_pblklen < 5)
    573 		cs[ACPI_STATE_C2].cs_method = 0;
    574 
    575 	if (sc->sc_object.ao_pblklen < 6)
    576 		cs[ACPI_STATE_C3].cs_method = 0;
    577 
    578 	/*
    579 	 * Sanity check the latency levels in FADT.
    580 	 * Values above the thresholds are used to
    581 	 * inform that C-states are not supported.
    582 	 */
    583 	CTASSERT(ACPICPU_C_C2_LATENCY_MAX == 100);
    584 	CTASSERT(ACPICPU_C_C3_LATENCY_MAX == 1000);
    585 
    586 	if (AcpiGbl_FADT.C2Latency > ACPICPU_C_C2_LATENCY_MAX)
    587 		cs[ACPI_STATE_C2].cs_method = 0;
    588 
    589 	if (AcpiGbl_FADT.C3Latency > ACPICPU_C_C3_LATENCY_MAX)
    590 		cs[ACPI_STATE_C3].cs_method = 0;
    591 }
    592 
    593 static void
    594 acpicpu_cstate_quirks(struct acpicpu_softc *sc)
    595 {
    596 	const uint32_t reg = AcpiGbl_FADT.Pm2ControlBlock;
    597 	const uint32_t len = AcpiGbl_FADT.Pm2ControlLength;
    598 
    599 	/*
    600 	 * Disable C3 for PIIX4.
    601 	 */
    602 	if ((sc->sc_flags & ACPICPU_FLAG_PIIX4) != 0) {
    603 		sc->sc_cstate[ACPI_STATE_C3].cs_method = 0;
    604 		return;
    605 	}
    606 
    607 	/*
    608 	 * Check bus master arbitration. If ARB_DIS
    609 	 * is not available, processor caches must be
    610 	 * flushed before C3 (ACPI 4.0, section 8.2).
    611 	 */
    612 	if (reg != 0 && len != 0) {
    613 		sc->sc_flags |= ACPICPU_FLAG_C_ARB;
    614 		return;
    615 	}
    616 
    617 	/*
    618 	 * Disable C3 entirely if WBINVD is not present.
    619 	 */
    620 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD) == 0)
    621 		sc->sc_cstate[ACPI_STATE_C3].cs_method = 0;
    622 	else {
    623 		/*
    624 		 * If WBINVD is present and functioning properly,
    625 		 * flush all processor caches before entering C3.
    626 		 */
    627 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD_FLUSH) == 0)
    628 			sc->sc_flags &= ~ACPICPU_FLAG_C_BM;
    629 		else
    630 			sc->sc_cstate[ACPI_STATE_C3].cs_method = 0;
    631 	}
    632 }
    633 
    634 static int
    635 acpicpu_cstate_latency(struct acpicpu_softc *sc)
    636 {
    637 	static const uint32_t cs_factor = 3;
    638 	struct acpicpu_cstate *cs;
    639 	int i;
    640 
    641 	for (i = cs_state_max; i > 0; i--) {
    642 
    643 		cs = &sc->sc_cstate[i];
    644 
    645 		if (__predict_false(cs->cs_method == 0))
    646 			continue;
    647 
    648 		/*
    649 		 * Choose a state if we have previously slept
    650 		 * longer than the worst case latency of the
    651 		 * state times an arbitrary multiplier.
    652 		 */
    653 		if (sc->sc_cstate_sleep > cs->cs_latency * cs_factor)
    654 			return i;
    655 	}
    656 
    657 	return ACPI_STATE_C1;
    658 }
    659 
    660 /*
    661  * The main idle loop.
    662  */
    663 void
    664 acpicpu_cstate_idle(void)
    665 {
    666         struct cpu_info *ci = curcpu();
    667 	struct acpicpu_softc *sc;
    668 	int state;
    669 
    670 	if (__predict_false(ci->ci_want_resched) != 0)
    671 		return;
    672 
    673 	acpi_md_OsDisableInterrupt();
    674 
    675 	KASSERT(acpicpu_sc != NULL);
    676 	KASSERT(ci->ci_acpiid < maxcpus);
    677 	KASSERT(ci->ci_ilevel == IPL_NONE);
    678 
    679 	sc = acpicpu_sc[ci->ci_acpiid];
    680 
    681 	if (__predict_false(sc == NULL))
    682 		goto halt;
    683 
    684 	if (__predict_false(sc->sc_cold != false))
    685 		goto halt;
    686 
    687 	if (__predict_false((sc->sc_flags & ACPICPU_FLAG_C) == 0))
    688 		goto halt;
    689 
    690 	if (__predict_false(mutex_tryenter(&sc->sc_mtx) == 0))
    691 		goto halt;
    692 
    693 	mutex_exit(&sc->sc_mtx);
    694 	state = acpicpu_cstate_latency(sc);
    695 
    696 	/*
    697 	 * Check for bus master activity. Note that particularly usb(4)
    698 	 * causes high activity, which may prevent the use of C3 states.
    699 	 */
    700 	if ((sc->sc_cstate[state].cs_flags & ACPICPU_FLAG_C_BM_STS) != 0) {
    701 
    702 		if (acpicpu_cstate_bm_check() != false)
    703 			state--;
    704 
    705 		if (__predict_false(sc->sc_cstate[state].cs_method == 0))
    706 			state = ACPI_STATE_C1;
    707 	}
    708 
    709 	KASSERT(state != ACPI_STATE_C0);
    710 
    711 	if (state != ACPI_STATE_C3) {
    712 		acpicpu_cstate_idle_enter(sc, state);
    713 		return;
    714 	}
    715 
    716 	/*
    717 	 * On all recent (Intel) CPUs caches are shared
    718 	 * by CPUs and bus master control is required to
    719 	 * keep these coherent while in C3. Flushing the
    720 	 * CPU caches is only the last resort.
    721 	 */
    722 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) == 0)
    723 		ACPI_FLUSH_CPU_CACHE();
    724 
    725 	/*
    726 	 * Allow the bus master to request that any given
    727 	 * CPU should return immediately to C0 from C3.
    728 	 */
    729 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
    730 		(void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
    731 
    732 	/*
    733 	 * It may be necessary to disable bus master arbitration
    734 	 * to ensure that bus master cycles do not occur while
    735 	 * sleeping in C3 (see ACPI 4.0, section 8.1.4).
    736 	 */
    737 	if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
    738 		(void)AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 1);
    739 
    740 	acpicpu_cstate_idle_enter(sc, state);
    741 
    742 	/*
    743 	 * Disable bus master wake and re-enable the arbiter.
    744 	 */
    745 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
    746 		(void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
    747 
    748 	if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
    749 		(void)AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 0);
    750 
    751 	return;
    752 
    753 halt:
    754 	acpicpu_md_idle_enter(ACPICPU_C_STATE_HALT, ACPI_STATE_C1);
    755 }
    756 
    757 static void
    758 acpicpu_cstate_idle_enter(struct acpicpu_softc *sc, int state)
    759 {
    760 	struct acpicpu_cstate *cs = &sc->sc_cstate[state];
    761 	uint32_t end, start, val;
    762 
    763 	start = acpitimer_read_safe(NULL);
    764 
    765 	switch (cs->cs_method) {
    766 
    767 	case ACPICPU_C_STATE_FFH:
    768 	case ACPICPU_C_STATE_HALT:
    769 		acpicpu_md_idle_enter(cs->cs_method, state);
    770 		break;
    771 
    772 	case ACPICPU_C_STATE_SYSIO:
    773 		(void)AcpiOsReadPort(cs->cs_addr, &val, 8);
    774 		break;
    775 
    776 	default:
    777 		acpicpu_md_idle_enter(ACPICPU_C_STATE_HALT, ACPI_STATE_C1);
    778 		break;
    779 	}
    780 
    781 	cs->cs_evcnt.ev_count++;
    782 
    783 	end = acpitimer_read_safe(NULL);
    784 	sc->sc_cstate_sleep = hztoms(acpitimer_delta(end, start)) * 1000;
    785 
    786 	acpi_md_OsEnableInterrupt();
    787 }
    788 
    789 static bool
    790 acpicpu_cstate_bm_check(void)
    791 {
    792 	uint32_t val = 0;
    793 	ACPI_STATUS rv;
    794 
    795 	rv = AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, &val);
    796 
    797 	if (ACPI_FAILURE(rv) || val == 0)
    798 		return false;
    799 
    800 	(void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1);
    801 
    802 	return true;
    803 }
    804