Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_cstate.c revision 1.25
      1 /* $NetBSD: acpi_cpu_cstate.c,v 1.25 2010/08/14 05:13:21 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.25 2010/08/14 05:13:21 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 	static const ACPI_OSD_EXEC_CALLBACK func = acpicpu_cstate_callback;
    244 	struct acpicpu_softc *sc = device_private(self);
    245 
    246 	if ((sc->sc_flags & ACPICPU_FLAG_C_FADT) == 0)
    247 		(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
    248 
    249 	return true;
    250 }
    251 
    252 void
    253 acpicpu_cstate_callback(void *aux)
    254 {
    255 	struct acpicpu_softc *sc;
    256 	device_t self = aux;
    257 
    258 	sc = device_private(self);
    259 
    260 	if ((sc->sc_flags & ACPICPU_FLAG_C_FADT) != 0)
    261 		return;
    262 
    263 	mutex_enter(&sc->sc_mtx);
    264 	(void)acpicpu_cstate_cst(sc);
    265 	mutex_exit(&sc->sc_mtx);
    266 }
    267 
    268 static ACPI_STATUS
    269 acpicpu_cstate_cst(struct acpicpu_softc *sc)
    270 {
    271 	ACPI_OBJECT *elm, *obj;
    272 	ACPI_BUFFER buf;
    273 	ACPI_STATUS rv;
    274 	uint32_t i, n;
    275 	uint8_t count;
    276 
    277 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_CST", &buf);
    278 
    279 	if (ACPI_FAILURE(rv))
    280 		return rv;
    281 
    282 	obj = buf.Pointer;
    283 
    284 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    285 		rv = AE_TYPE;
    286 		goto out;
    287 	}
    288 
    289 	if (obj->Package.Count < 2) {
    290 		rv = AE_LIMIT;
    291 		goto out;
    292 	}
    293 
    294 	elm = obj->Package.Elements;
    295 
    296 	if (elm[0].Type != ACPI_TYPE_INTEGER) {
    297 		rv = AE_TYPE;
    298 		goto out;
    299 	}
    300 
    301 	n = elm[0].Integer.Value;
    302 
    303 	if (n != obj->Package.Count - 1) {
    304 		rv = AE_BAD_VALUE;
    305 		goto out;
    306 	}
    307 
    308 	if (n > ACPI_C_STATES_MAX) {
    309 		rv = AE_LIMIT;
    310 		goto out;
    311 	}
    312 
    313 	acpicpu_cstate_memset(sc);
    314 
    315 	CTASSERT(ACPI_STATE_C0 == 0 && ACPI_STATE_C1 == 1);
    316 	CTASSERT(ACPI_STATE_C2 == 2 && ACPI_STATE_C3 == 3);
    317 
    318 	for (count = 0, i = 1; i <= n; i++) {
    319 
    320 		elm = &obj->Package.Elements[i];
    321 		rv = acpicpu_cstate_cst_add(sc, elm);
    322 
    323 		if (ACPI_SUCCESS(rv))
    324 			count++;
    325 	}
    326 
    327 	rv = (count != 0) ? AE_OK : AE_NOT_EXIST;
    328 
    329 out:
    330 	if (buf.Pointer != NULL)
    331 		ACPI_FREE(buf.Pointer);
    332 
    333 	return rv;
    334 }
    335 
    336 static ACPI_STATUS
    337 acpicpu_cstate_cst_add(struct acpicpu_softc *sc, ACPI_OBJECT *elm)
    338 {
    339 	const struct acpicpu_object *ao = &sc->sc_object;
    340 	struct acpicpu_cstate *cs = sc->sc_cstate;
    341 	struct acpicpu_cstate state;
    342 	struct acpicpu_reg *reg;
    343 	ACPI_STATUS rv = AE_OK;
    344 	ACPI_OBJECT *obj;
    345 	uint32_t type;
    346 
    347 	(void)memset(&state, 0, sizeof(*cs));
    348 
    349 	state.cs_flags = ACPICPU_FLAG_C_BM_STS;
    350 
    351 	if (elm->Type != ACPI_TYPE_PACKAGE) {
    352 		rv = AE_TYPE;
    353 		goto out;
    354 	}
    355 
    356 	if (elm->Package.Count != 4) {
    357 		rv = AE_LIMIT;
    358 		goto out;
    359 	}
    360 
    361 	/*
    362 	 * Type.
    363 	 */
    364 	obj = &elm->Package.Elements[1];
    365 
    366 	if (obj->Type != ACPI_TYPE_INTEGER) {
    367 		rv = AE_TYPE;
    368 		goto out;
    369 	}
    370 
    371 	type = obj->Integer.Value;
    372 
    373 	if (type < ACPI_STATE_C1 || type > ACPI_STATE_C3) {
    374 		rv = AE_TYPE;
    375 		goto out;
    376 	}
    377 
    378 	/*
    379 	 * Latency.
    380 	 */
    381 	obj = &elm->Package.Elements[2];
    382 
    383 	if (obj->Type != ACPI_TYPE_INTEGER) {
    384 		rv = AE_TYPE;
    385 		goto out;
    386 	}
    387 
    388 	state.cs_latency = obj->Integer.Value;
    389 
    390 	/*
    391 	 * Power.
    392 	 */
    393 	obj = &elm->Package.Elements[3];
    394 
    395 	if (obj->Type != ACPI_TYPE_INTEGER) {
    396 		rv = AE_TYPE;
    397 		goto out;
    398 	}
    399 
    400 	state.cs_power = obj->Integer.Value;
    401 
    402 	/*
    403 	 * Register.
    404 	 */
    405 	obj = &elm->Package.Elements[0];
    406 
    407 	if (obj->Type != ACPI_TYPE_BUFFER) {
    408 		rv = AE_TYPE;
    409 		goto out;
    410 	}
    411 
    412 	CTASSERT(sizeof(struct acpicpu_reg) == 15);
    413 
    414 	if (obj->Buffer.Length < sizeof(struct acpicpu_reg)) {
    415 		rv = AE_LIMIT;
    416 		goto out;
    417 	}
    418 
    419 	reg = (struct acpicpu_reg *)obj->Buffer.Pointer;
    420 
    421 	switch (reg->reg_spaceid) {
    422 
    423 	case ACPI_ADR_SPACE_SYSTEM_IO:
    424 		state.cs_method = ACPICPU_C_STATE_SYSIO;
    425 
    426 		if (reg->reg_addr == 0) {
    427 			rv = AE_AML_ILLEGAL_ADDRESS;
    428 			goto out;
    429 		}
    430 
    431 		if (reg->reg_bitwidth != 8) {
    432 			rv = AE_AML_BAD_RESOURCE_LENGTH;
    433 			goto out;
    434 		}
    435 
    436 		/*
    437 		 * Check only that the address is in the mapped space.
    438 		 * Systems are allowed to change it when operating
    439 		 * with _CST (see ACPI 4.0, pp. 94-95). For instance,
    440 		 * the offset of P_LVL3 may change depending on whether
    441 		 * acpiacad(4) is connected or disconnected.
    442 		 */
    443 		if (reg->reg_addr > ao->ao_pblkaddr + ao->ao_pblklen) {
    444 			rv = AE_BAD_ADDRESS;
    445 			goto out;
    446 		}
    447 
    448 		state.cs_addr = reg->reg_addr;
    449 		break;
    450 
    451 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    452 		state.cs_method = ACPICPU_C_STATE_FFH;
    453 
    454 		switch (type) {
    455 
    456 		case ACPI_STATE_C1:
    457 
    458 			if ((sc->sc_flags & ACPICPU_FLAG_C_FFH) == 0)
    459 				state.cs_method = ACPICPU_C_STATE_HALT;
    460 
    461 			break;
    462 
    463 		default:
    464 
    465 			if ((sc->sc_flags & ACPICPU_FLAG_C_FFH) == 0) {
    466 				rv = AE_SUPPORT;
    467 				goto out;
    468 			}
    469 		}
    470 
    471 		if (sc->sc_cap != 0) {
    472 
    473 			/*
    474 			 * The _CST FFH GAS encoding may contain
    475 			 * additional hints on Intel processors.
    476 			 * Use these to determine whether we can
    477 			 * avoid the bus master activity check.
    478 			 */
    479 			if ((reg->reg_accesssize & ACPICPU_PDC_GAS_BM) == 0)
    480 				state.cs_flags &= ~ACPICPU_FLAG_C_BM_STS;
    481 		}
    482 
    483 		break;
    484 
    485 	default:
    486 		rv = AE_AML_INVALID_SPACE_ID;
    487 		goto out;
    488 	}
    489 
    490 	if (cs[type].cs_method != 0) {
    491 		rv = AE_ALREADY_EXISTS;
    492 		goto out;
    493 	}
    494 
    495 	cs[type].cs_addr = state.cs_addr;
    496 	cs[type].cs_power = state.cs_power;
    497 	cs[type].cs_flags = state.cs_flags;
    498 	cs[type].cs_method = state.cs_method;
    499 	cs[type].cs_latency = state.cs_latency;
    500 
    501 out:
    502 	if (ACPI_FAILURE(rv))
    503 		aprint_debug_dev(sc->sc_dev, "invalid "
    504 		    "_CST: %s\n", AcpiFormatException(rv));
    505 
    506 	return rv;
    507 }
    508 
    509 static void
    510 acpicpu_cstate_cst_bios(void)
    511 {
    512 	const uint8_t val = AcpiGbl_FADT.CstControl;
    513 	const uint32_t addr = AcpiGbl_FADT.SmiCommand;
    514 
    515 	if (addr == 0)
    516 		return;
    517 
    518 	(void)AcpiOsWritePort(addr, val, 8);
    519 }
    520 
    521 static void
    522 acpicpu_cstate_memset(struct acpicpu_softc *sc)
    523 {
    524 	int i = 0;
    525 
    526 	while (i < ACPI_C_STATE_COUNT) {
    527 
    528 		sc->sc_cstate[i].cs_addr = 0;
    529 		sc->sc_cstate[i].cs_power = 0;
    530 		sc->sc_cstate[i].cs_flags = 0;
    531 		sc->sc_cstate[i].cs_method = 0;
    532 		sc->sc_cstate[i].cs_latency = 0;
    533 
    534 		i++;
    535 	}
    536 }
    537 
    538 static void
    539 acpicpu_cstate_fadt(struct acpicpu_softc *sc)
    540 {
    541 	struct acpicpu_cstate *cs = sc->sc_cstate;
    542 
    543 	acpicpu_cstate_memset(sc);
    544 
    545 	/*
    546 	 * All x86 processors should support C1 (a.k.a. HALT).
    547 	 */
    548 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_C1_SUPPORTED) != 0)
    549 		cs[ACPI_STATE_C1].cs_method = ACPICPU_C_STATE_HALT;
    550 
    551 	if (sc->sc_object.ao_pblkaddr == 0)
    552 		return;
    553 
    554 	if (acpicpu_md_cpus_running() > 1) {
    555 
    556 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_C2_MP_SUPPORTED) == 0)
    557 			return;
    558 	}
    559 
    560 	cs[ACPI_STATE_C2].cs_method = ACPICPU_C_STATE_SYSIO;
    561 	cs[ACPI_STATE_C3].cs_method = ACPICPU_C_STATE_SYSIO;
    562 
    563 	cs[ACPI_STATE_C2].cs_latency = AcpiGbl_FADT.C2Latency;
    564 	cs[ACPI_STATE_C3].cs_latency = AcpiGbl_FADT.C3Latency;
    565 
    566 	cs[ACPI_STATE_C2].cs_addr = sc->sc_object.ao_pblkaddr + 4;
    567 	cs[ACPI_STATE_C3].cs_addr = sc->sc_object.ao_pblkaddr + 5;
    568 
    569 	/*
    570 	 * The P_BLK length should always be 6. If it
    571 	 * is not, reduce functionality accordingly.
    572 	 * Sanity check also FADT's latency levels.
    573 	 */
    574 	if (sc->sc_object.ao_pblklen < 5)
    575 		cs[ACPI_STATE_C2].cs_method = 0;
    576 
    577 	if (sc->sc_object.ao_pblklen < 6)
    578 		cs[ACPI_STATE_C3].cs_method = 0;
    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 	if (__predict_false(ci->ci_want_resched) != 0)
    668 		return;
    669 
    670 	acpi_md_OsDisableInterrupt();
    671 
    672 	KASSERT(acpicpu_sc != NULL);
    673 	KASSERT(ci->ci_acpiid < maxcpus);
    674 	KASSERT(ci->ci_ilevel == IPL_NONE);
    675 
    676 	sc = acpicpu_sc[ci->ci_acpiid];
    677 
    678 	if (__predict_false(sc == NULL))
    679 		goto halt;
    680 
    681 	if (__predict_false(sc->sc_cold != false))
    682 		goto halt;
    683 
    684 	if (__predict_false((sc->sc_flags & ACPICPU_FLAG_C) == 0))
    685 		goto halt;
    686 
    687 	if (__predict_false(mutex_tryenter(&sc->sc_mtx) == 0))
    688 		goto halt;
    689 
    690 	mutex_exit(&sc->sc_mtx);
    691 	state = acpicpu_cstate_latency(sc);
    692 
    693 	/*
    694 	 * Check for bus master activity. Note that particularly usb(4)
    695 	 * causes high activity, which may prevent the use of C3 states.
    696 	 */
    697 	if ((sc->sc_cstate[state].cs_flags & ACPICPU_FLAG_C_BM_STS) != 0) {
    698 
    699 		if (acpicpu_cstate_bm_check() != false)
    700 			state--;
    701 
    702 		if (__predict_false(sc->sc_cstate[state].cs_method == 0))
    703 			state = ACPI_STATE_C1;
    704 	}
    705 
    706 	KASSERT(state != ACPI_STATE_C0);
    707 
    708 	if (state != ACPI_STATE_C3) {
    709 		acpicpu_cstate_idle_enter(sc, state);
    710 		return;
    711 	}
    712 
    713 	/*
    714 	 * On all recent (Intel) CPUs caches are shared
    715 	 * by CPUs and bus master control is required to
    716 	 * keep these coherent while in C3. Flushing the
    717 	 * CPU caches is only the last resort.
    718 	 */
    719 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) == 0)
    720 		ACPI_FLUSH_CPU_CACHE();
    721 
    722 	/*
    723 	 * Allow the bus master to request that any given
    724 	 * CPU should return immediately to C0 from C3.
    725 	 */
    726 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
    727 		(void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
    728 
    729 	/*
    730 	 * It may be necessary to disable bus master arbitration
    731 	 * to ensure that bus master cycles do not occur while
    732 	 * sleeping in C3 (see ACPI 4.0, section 8.1.4).
    733 	 */
    734 	if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
    735 		(void)AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 1);
    736 
    737 	acpicpu_cstate_idle_enter(sc, state);
    738 
    739 	/*
    740 	 * Disable bus master wake and re-enable the arbiter.
    741 	 */
    742 	if ((sc->sc_flags & ACPICPU_FLAG_C_BM) != 0)
    743 		(void)AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
    744 
    745 	if ((sc->sc_flags & ACPICPU_FLAG_C_ARB) != 0)
    746 		(void)AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 0);
    747 
    748 	return;
    749 
    750 halt:
    751 	acpicpu_md_idle_enter(ACPICPU_C_STATE_HALT, ACPI_STATE_C1);
    752 }
    753 
    754 static void
    755 acpicpu_cstate_idle_enter(struct acpicpu_softc *sc, int state)
    756 {
    757 	struct acpicpu_cstate *cs = &sc->sc_cstate[state];
    758 	uint32_t end, start, val;
    759 
    760 	start = acpitimer_read_safe(NULL);
    761 
    762 	switch (cs->cs_method) {
    763 
    764 	case ACPICPU_C_STATE_FFH:
    765 	case ACPICPU_C_STATE_HALT:
    766 		acpicpu_md_idle_enter(cs->cs_method, state);
    767 		break;
    768 
    769 	case ACPICPU_C_STATE_SYSIO:
    770 		(void)AcpiOsReadPort(cs->cs_addr, &val, 8);
    771 		break;
    772 
    773 	default:
    774 		acpicpu_md_idle_enter(ACPICPU_C_STATE_HALT, ACPI_STATE_C1);
    775 		break;
    776 	}
    777 
    778 	cs->cs_evcnt.ev_count++;
    779 
    780 	end = acpitimer_read_safe(NULL);
    781 	sc->sc_cstate_sleep = hztoms(acpitimer_delta(end, start)) * 1000;
    782 
    783 	acpi_md_OsEnableInterrupt();
    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