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