Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_tstate.c revision 1.8
      1 /* $NetBSD: acpi_cpu_tstate.c,v 1.8 2010/08/15 04:35:16 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_tstate.c,v 1.8 2010/08/15 04:35:16 jruoho Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/evcnt.h>
     34 #include <sys/kmem.h>
     35 
     36 #include <dev/acpi/acpireg.h>
     37 #include <dev/acpi/acpivar.h>
     38 #include <dev/acpi/acpi_cpu.h>
     39 
     40 #define _COMPONENT	 ACPI_BUS_COMPONENT
     41 ACPI_MODULE_NAME	 ("acpi_cpu_tstate")
     42 
     43 static void		 acpicpu_tstate_attach_print(struct acpicpu_softc *);
     44 static void		 acpicpu_tstate_attach_evcnt(struct acpicpu_softc *);
     45 static void		 acpicpu_tstate_detach_evcnt(struct acpicpu_softc *);
     46 static ACPI_STATUS	 acpicpu_tstate_tss(struct acpicpu_softc *);
     47 static ACPI_STATUS	 acpicpu_tstate_tss_add(struct acpicpu_tstate *,
     48 						ACPI_OBJECT *);
     49 static ACPI_STATUS	 acpicpu_tstate_ptc(struct acpicpu_softc *);
     50 static ACPI_STATUS	 acpicpu_tstate_fadt(struct acpicpu_softc *);
     51 static ACPI_STATUS	 acpicpu_tstate_change(struct acpicpu_softc *);
     52 
     53 void
     54 acpicpu_tstate_attach(device_t self)
     55 {
     56 	struct acpicpu_softc *sc = device_private(self);
     57 	const char *str;
     58 	ACPI_STATUS rv;
     59 
     60 	/*
     61 	 * Disable T-states for PIIX4.
     62 	 */
     63 	if ((sc->sc_flags & ACPICPU_FLAG_PIIX4) != 0)
     64 		return;
     65 
     66 	/*
     67 	 * If either _TSS, _PTC, or _TPC is not
     68 	 * available, we have to resort to FADT.
     69 	 */
     70 	rv  = acpicpu_tstate_tss(sc);
     71 
     72 	if (ACPI_FAILURE(rv)) {
     73 		str = "_TSS";
     74 		goto out;
     75 	}
     76 
     77 	rv = acpicpu_tstate_ptc(sc);
     78 
     79 	if (ACPI_FAILURE(rv)) {
     80 		str = "_PTC";
     81 		goto out;
     82 	}
     83 
     84 	rv = acpicpu_tstate_change(sc);
     85 
     86 	if (ACPI_FAILURE(rv)) {
     87 		str = "_TPC";
     88 		goto out;
     89 	}
     90 
     91 out:
     92 	if (ACPI_FAILURE(rv)) {
     93 
     94 		if (rv != AE_NOT_FOUND)
     95 			aprint_error_dev(sc->sc_dev, "failed to evaluate "
     96 			    "%s: %s\n", str, AcpiFormatException(rv));
     97 
     98 		rv = acpicpu_tstate_fadt(sc);
     99 
    100 		if (ACPI_FAILURE(rv))
    101 			return;
    102 
    103 		sc->sc_flags |= ACPICPU_FLAG_T_FADT;
    104 	}
    105 
    106 	sc->sc_flags |= ACPICPU_FLAG_T;
    107 
    108 	acpicpu_tstate_attach_evcnt(sc);
    109 	acpicpu_tstate_attach_print(sc);
    110 }
    111 
    112 static void
    113 acpicpu_tstate_attach_print(struct acpicpu_softc *sc)
    114 {
    115 	const uint8_t method = sc->sc_tstate_control.reg_spaceid;
    116 	struct acpicpu_tstate *ts;
    117 	static bool once = false;
    118 	const char *str;
    119 	uint32_t i;
    120 
    121 	if (once != false)
    122 		return;
    123 
    124 	str = (method != ACPI_ADR_SPACE_FIXED_HARDWARE) ? "I/O" : "FFH";
    125 
    126 	for (i = 0; i < sc->sc_tstate_count; i++) {
    127 
    128 		ts = &sc->sc_tstate[i];
    129 
    130 		if (ts->ts_percent == 0)
    131 			continue;
    132 
    133 		aprint_debug_dev(sc->sc_dev, "T%u: %3s, "
    134 		    "lat %3u us, pow %5u mW, %3u %%\n", i, str,
    135 		    ts->ts_latency, ts->ts_power, ts->ts_percent);
    136 	}
    137 
    138 	once = true;
    139 }
    140 
    141 static void
    142 acpicpu_tstate_attach_evcnt(struct acpicpu_softc *sc)
    143 {
    144 	struct acpicpu_tstate *ts;
    145 	uint32_t i;
    146 
    147 	for (i = 0; i < sc->sc_tstate_count; i++) {
    148 
    149 		ts = &sc->sc_tstate[i];
    150 
    151 		if (ts->ts_percent == 0)
    152 			continue;
    153 
    154 		(void)snprintf(ts->ts_name, sizeof(ts->ts_name),
    155 		    "T%u (%u %%)", i, ts->ts_percent);
    156 
    157 		evcnt_attach_dynamic(&ts->ts_evcnt, EVCNT_TYPE_MISC,
    158 		    NULL, device_xname(sc->sc_dev), ts->ts_name);
    159 	}
    160 }
    161 
    162 int
    163 acpicpu_tstate_detach(device_t self)
    164 {
    165 	struct acpicpu_softc *sc = device_private(self);
    166 	size_t size;
    167 
    168 	if ((sc->sc_flags & ACPICPU_FLAG_T) == 0)
    169 		return 0;
    170 
    171 	size = sc->sc_tstate_count * sizeof(*sc->sc_tstate);
    172 
    173 	if (sc->sc_tstate != NULL)
    174 		kmem_free(sc->sc_tstate, size);
    175 
    176 	sc->sc_flags &= ~ACPICPU_FLAG_T;
    177 	acpicpu_tstate_detach_evcnt(sc);
    178 
    179 	return 0;
    180 }
    181 
    182 static void
    183 acpicpu_tstate_detach_evcnt(struct acpicpu_softc *sc)
    184 {
    185 	struct acpicpu_tstate *ts;
    186 	uint32_t i;
    187 
    188 	for (i = 0; i < sc->sc_tstate_count; i++) {
    189 
    190 		ts = &sc->sc_tstate[i];
    191 
    192 		if (ts->ts_percent != 0)
    193 			evcnt_detach(&ts->ts_evcnt);
    194 	}
    195 }
    196 
    197 int
    198 acpicpu_tstate_start(device_t self)
    199 {
    200 
    201 	return 0;
    202 }
    203 
    204 bool
    205 acpicpu_tstate_suspend(device_t self)
    206 {
    207 
    208 	return true;
    209 }
    210 
    211 bool
    212 acpicpu_tstate_resume(device_t self)
    213 {
    214 	struct acpicpu_softc *sc = device_private(self);
    215 
    216 	if ((sc->sc_flags & ACPICPU_FLAG_T_FADT) == 0)
    217 		acpicpu_tstate_callback(self);
    218 
    219 	return true;
    220 }
    221 
    222 void
    223 acpicpu_tstate_callback(void *aux)
    224 {
    225 	struct acpicpu_softc *sc;
    226 	device_t self = aux;
    227 	uint32_t omax, omin;
    228 	int i;
    229 
    230 	sc = device_private(self);
    231 
    232 	if ((sc->sc_flags & ACPICPU_FLAG_T_FADT) != 0)
    233 		return;
    234 
    235 	mutex_enter(&sc->sc_mtx);
    236 
    237 	/*
    238 	 * If P-states are in use, we should ignore
    239 	 * the interrupt unless we are in the highest
    240 	 * P-state (see ACPI 4.0, section 8.4.3.3).
    241 	 */
    242 	if ((sc->sc_flags & ACPICPU_FLAG_P) != 0) {
    243 
    244 		for (i = sc->sc_pstate_count - 1; i >= 0; i--) {
    245 
    246 			if (sc->sc_pstate[i].ps_freq != 0)
    247 				break;
    248 		}
    249 
    250 		if (sc->sc_pstate_current != sc->sc_pstate[i].ps_freq) {
    251 			mutex_exit(&sc->sc_mtx);
    252 			return;
    253 		}
    254 	}
    255 
    256 	omax = sc->sc_tstate_max;
    257 	omin = sc->sc_tstate_min;
    258 
    259 	(void)acpicpu_tstate_change(sc);
    260 
    261 	if (omax != sc->sc_tstate_max || omin != sc->sc_tstate_min) {
    262 
    263 		aprint_debug_dev(sc->sc_dev, "throttling window "
    264 		    "changed from %u-%u %% to %u-%u %%\n",
    265 		    sc->sc_tstate[omax].ts_percent,
    266 		    sc->sc_tstate[omin].ts_percent,
    267 		    sc->sc_tstate[sc->sc_tstate_max].ts_percent,
    268 		    sc->sc_tstate[sc->sc_tstate_min].ts_percent);
    269 	}
    270 
    271 	mutex_exit(&sc->sc_mtx);
    272 }
    273 
    274 static ACPI_STATUS
    275 acpicpu_tstate_tss(struct acpicpu_softc *sc)
    276 {
    277 	struct acpicpu_tstate *ts;
    278 	ACPI_OBJECT *obj;
    279 	ACPI_BUFFER buf;
    280 	ACPI_STATUS rv;
    281 	uint32_t count;
    282 	uint32_t i, j;
    283 
    284 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_TSS", &buf);
    285 
    286 	if (ACPI_FAILURE(rv))
    287 		return rv;
    288 
    289 	obj = buf.Pointer;
    290 
    291 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    292 		rv = AE_TYPE;
    293 		goto out;
    294 	}
    295 
    296 	sc->sc_tstate_count = obj->Package.Count;
    297 
    298 	if (sc->sc_tstate_count == 0) {
    299 		rv = AE_NOT_EXIST;
    300 		goto out;
    301 	}
    302 
    303 	if (sc->sc_tstate_count > ACPICPU_T_STATE_MAX) {
    304 		rv = AE_LIMIT;
    305 		goto out;
    306 	}
    307 
    308 	sc->sc_tstate = kmem_zalloc(sc->sc_tstate_count *
    309 	    sizeof(struct acpicpu_tstate), KM_SLEEP);
    310 
    311 	if (sc->sc_tstate == NULL) {
    312 		rv = AE_NO_MEMORY;
    313 		goto out;
    314 	}
    315 
    316 	for (count = i = 0; i < sc->sc_tstate_count; i++) {
    317 
    318 		ts = &sc->sc_tstate[i];
    319 		rv = acpicpu_tstate_tss_add(ts, &obj->Package.Elements[i]);
    320 
    321 		if (ACPI_FAILURE(rv)) {
    322 			ts->ts_percent = 0;
    323 			continue;
    324 		}
    325 
    326 		for (j = 0; j < i; j++) {
    327 
    328 			if (ts->ts_percent >= sc->sc_tstate[j].ts_percent) {
    329 				ts->ts_percent = 0;
    330 				break;
    331 			}
    332 		}
    333 
    334 		if (ts->ts_percent != 0)
    335 			count++;
    336 	}
    337 
    338 	if (count == 0) {
    339 		rv = AE_NOT_EXIST;
    340 		goto out;
    341 	}
    342 
    343 	/*
    344 	 * There must be an entry with the percent
    345 	 * field of 100. If this is not true, and if
    346 	 * this entry is not in the expected index,
    347 	 * invalidate the use of T-states via _TSS.
    348 	 */
    349 	if (sc->sc_tstate[0].ts_percent != 100) {
    350 		rv = AE_BAD_DECIMAL_CONSTANT;
    351 		goto out;
    352 	}
    353 
    354 	/*
    355 	 * The first entry with 100 % duty cycle
    356 	 * should have zero in the control field.
    357 	 */
    358 	if (sc->sc_tstate[0].ts_control != 0) {
    359 		rv = AE_AML_BAD_RESOURCE_VALUE;
    360 		goto out;
    361 	}
    362 
    363 out:
    364 	if (buf.Pointer != NULL)
    365 		ACPI_FREE(buf.Pointer);
    366 
    367 	return rv;
    368 }
    369 
    370 static ACPI_STATUS
    371 acpicpu_tstate_tss_add(struct acpicpu_tstate *ts, ACPI_OBJECT *obj)
    372 {
    373 	ACPI_OBJECT *elm;
    374 	uint32_t val[5];
    375 	uint32_t *p;
    376 	int i;
    377 
    378 	if (obj->Type != ACPI_TYPE_PACKAGE)
    379 		return AE_TYPE;
    380 
    381 	if (obj->Package.Count != 5)
    382 		return AE_BAD_DATA;
    383 
    384 	elm = obj->Package.Elements;
    385 
    386 	for (i = 0; i < 5; i++) {
    387 
    388 		if (elm[i].Type != ACPI_TYPE_INTEGER)
    389 			return AE_TYPE;
    390 
    391 		if (elm[i].Integer.Value > UINT32_MAX)
    392 			return AE_AML_NUMERIC_OVERFLOW;
    393 
    394 		val[i] = elm[i].Integer.Value;
    395 	}
    396 
    397 	p = &ts->ts_percent;
    398 
    399 	for (i = 0; i < 5; i++, p++)
    400 		*p = val[i];
    401 
    402         if (ts->ts_percent < 1 || ts->ts_percent > 100)
    403 		return AE_BAD_DECIMAL_CONSTANT;
    404 
    405 	if (ts->ts_latency < 1)
    406 		ts->ts_latency = 1;
    407 
    408 	return AE_OK;
    409 }
    410 
    411 ACPI_STATUS
    412 acpicpu_tstate_ptc(struct acpicpu_softc *sc)
    413 {
    414 	static const size_t size = sizeof(struct acpicpu_reg);
    415 	struct acpicpu_reg *reg[2];
    416 	ACPI_OBJECT *elm, *obj;
    417 	ACPI_BUFFER buf;
    418 	ACPI_STATUS rv;
    419 	int i;
    420 
    421 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PTC", &buf);
    422 
    423 	if (ACPI_FAILURE(rv))
    424 		return rv;
    425 
    426 	obj = buf.Pointer;
    427 
    428 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    429 		rv = AE_TYPE;
    430 		goto out;
    431 	}
    432 
    433 	if (obj->Package.Count != 2) {
    434 		rv = AE_LIMIT;
    435 		goto out;
    436 	}
    437 
    438 	for (i = 0; i < 2; i++) {
    439 
    440 		elm = &obj->Package.Elements[i];
    441 
    442 		if (elm->Type != ACPI_TYPE_BUFFER) {
    443 			rv = AE_TYPE;
    444 			goto out;
    445 		}
    446 
    447 		if (size > elm->Buffer.Length) {
    448 			rv = AE_AML_BAD_RESOURCE_LENGTH;
    449 			goto out;
    450 		}
    451 
    452 		reg[i] = (struct acpicpu_reg *)elm->Buffer.Pointer;
    453 
    454 		switch (reg[i]->reg_spaceid) {
    455 
    456 		case ACPI_ADR_SPACE_SYSTEM_IO:
    457 
    458 			if (reg[i]->reg_addr == 0) {
    459 				rv = AE_AML_ILLEGAL_ADDRESS;
    460 				goto out;
    461 			}
    462 
    463 			/*
    464 			 * Check that the values match the IA32 clock
    465 			 * modulation MSR, where the bit 0 is reserved,
    466 			 * bits 1 through 3 define the duty cycle, and
    467 			 * the fourth bit enables the modulation.
    468 			 */
    469 			if (reg[i]->reg_bitwidth != 4) {
    470 				rv = AE_AML_BAD_RESOURCE_VALUE;
    471 				goto out;
    472 			}
    473 
    474 			if (reg[i]->reg_bitoffset != 1) {
    475 				rv = AE_AML_BAD_RESOURCE_VALUE;
    476 				goto out;
    477 			}
    478 
    479 			break;
    480 
    481 		case ACPI_ADR_SPACE_FIXED_HARDWARE:
    482 
    483 			if ((sc->sc_flags & ACPICPU_FLAG_T_FFH) == 0) {
    484 				rv = AE_SUPPORT;
    485 				goto out;
    486 			}
    487 
    488 			break;
    489 
    490 		default:
    491 			rv = AE_AML_INVALID_SPACE_ID;
    492 			goto out;
    493 		}
    494 	}
    495 
    496 	if (reg[0]->reg_spaceid != reg[1]->reg_spaceid) {
    497 		rv = AE_AML_INVALID_SPACE_ID;
    498 		goto out;
    499 	}
    500 
    501 	(void)memcpy(&sc->sc_tstate_control, reg[0], size);
    502 	(void)memcpy(&sc->sc_tstate_status,  reg[1], size);
    503 
    504 out:
    505 	if (buf.Pointer != NULL)
    506 		ACPI_FREE(buf.Pointer);
    507 
    508 	return rv;
    509 }
    510 
    511 static ACPI_STATUS
    512 acpicpu_tstate_fadt(struct acpicpu_softc *sc)
    513 {
    514 	static const size_t size = sizeof(struct acpicpu_tstate);
    515 	const uint8_t offset = AcpiGbl_FADT.DutyOffset;
    516 	const uint8_t width = AcpiGbl_FADT.DutyWidth;
    517 	uint8_t beta, count, i;
    518 
    519 	if (sc->sc_object.ao_pblkaddr == 0)
    520 		return AE_AML_ILLEGAL_ADDRESS;
    521 
    522 	if (width == 0 || width + offset > 4)
    523 		return AE_AML_BAD_RESOURCE_VALUE;
    524 
    525 	count = 1 << width;
    526 
    527 	if (count > ACPICPU_T_STATE_MAX)
    528 		return AE_LIMIT;
    529 
    530 	if (sc->sc_tstate != NULL)
    531 		kmem_free(sc->sc_tstate, sc->sc_tstate_count * size);
    532 
    533 	sc->sc_tstate = kmem_zalloc(count * size, KM_SLEEP);
    534 
    535 	if (sc->sc_tstate == NULL)
    536 		return ENOMEM;
    537 
    538 	sc->sc_tstate_count = count;
    539 
    540 	/*
    541 	 * Approximate duty cycles and set the MSR values.
    542 	 */
    543 	for (beta = 100 / count, i = 0; i < count; i++) {
    544 		sc->sc_tstate[i].ts_percent = 100 - beta * i;
    545 		sc->sc_tstate[i].ts_latency = 1;
    546 	}
    547 
    548 	for (i = 1; i < count; i++)
    549 		sc->sc_tstate[i].ts_control = (count - i) | __BIT(3);
    550 
    551 	/*
    552 	 * Fake values for throttling registers.
    553 	 */
    554 	(void)memset(&sc->sc_tstate_status, 0, sizeof(struct acpicpu_reg));
    555 	(void)memset(&sc->sc_tstate_control, 0, sizeof(struct acpicpu_reg));
    556 
    557 	sc->sc_tstate_status.reg_bitwidth = width;
    558 	sc->sc_tstate_status.reg_bitoffset = offset;
    559 	sc->sc_tstate_status.reg_addr = sc->sc_object.ao_pblkaddr;
    560 	sc->sc_tstate_status.reg_spaceid = ACPI_ADR_SPACE_SYSTEM_IO;
    561 
    562 	sc->sc_tstate_control.reg_bitwidth = width;
    563 	sc->sc_tstate_control.reg_bitoffset = offset;
    564 	sc->sc_tstate_control.reg_addr = sc->sc_object.ao_pblkaddr;
    565 	sc->sc_tstate_control.reg_spaceid = ACPI_ADR_SPACE_SYSTEM_IO;
    566 
    567 	return AE_OK;
    568 }
    569 
    570 static ACPI_STATUS
    571 acpicpu_tstate_change(struct acpicpu_softc *sc)
    572 {
    573 	ACPI_INTEGER val;
    574 	ACPI_STATUS rv;
    575 
    576 	sc->sc_tstate_max = 0;
    577 	sc->sc_tstate_min = sc->sc_tstate_count - 1;
    578 
    579 	/*
    580 	 * Evaluate the available T-state window:
    581 	 *
    582 	 *   _TPC : either this maximum or any lower power
    583 	 *          (i.e. higher numbered) state may be used.
    584 	 *
    585 	 *   _TDL : either this minimum or any higher power
    586 	 *	    (i.e. lower numbered) state may be used.
    587 	 *
    588 	 *   _TDL >= _TPC || _TDL >= _TSS[last entry].
    589 	 */
    590 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_TPC", &val);
    591 
    592 	if (ACPI_FAILURE(rv))
    593 		return rv;
    594 
    595 	if (val < sc->sc_tstate_count) {
    596 
    597 		if (sc->sc_tstate[val].ts_percent != 0)
    598 			sc->sc_tstate_max = val;
    599 	}
    600 
    601 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_TDL", &val);
    602 
    603 	if (ACPI_SUCCESS(rv) && val < sc->sc_tstate_count) {
    604 
    605 		if (val >= sc->sc_tstate_max &&
    606 		    sc->sc_tstate[val].ts_percent != 0)
    607 			sc->sc_tstate_min = val;
    608 	}
    609 
    610 	return AE_OK;
    611 }
    612 
    613 int
    614 acpicpu_tstate_get(struct acpicpu_softc *sc, uint32_t *percent)
    615 {
    616 	const uint8_t method = sc->sc_tstate_control.reg_spaceid;
    617 	struct acpicpu_tstate *ts = NULL;
    618 	uint32_t i, val = 0;
    619 	uint8_t offset;
    620 	uint64_t addr;
    621 	int rv;
    622 
    623 	if (sc->sc_cold != false) {
    624 		rv = EBUSY;
    625 		goto fail;
    626 	}
    627 
    628 	if ((sc->sc_flags & ACPICPU_FLAG_T) == 0) {
    629 		rv = ENODEV;
    630 		goto fail;
    631 	}
    632 
    633 	mutex_enter(&sc->sc_mtx);
    634 
    635 	if (sc->sc_tstate_current != ACPICPU_T_STATE_UNKNOWN) {
    636 		*percent = sc->sc_tstate_current;
    637 		mutex_exit(&sc->sc_mtx);
    638 		return 0;
    639 	}
    640 
    641 	mutex_exit(&sc->sc_mtx);
    642 
    643 	switch (method) {
    644 
    645 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    646 
    647 		rv = acpicpu_md_tstate_get(sc, percent);
    648 
    649 		if (rv != 0)
    650 			goto fail;
    651 
    652 		break;
    653 
    654 	case ACPI_ADR_SPACE_SYSTEM_IO:
    655 
    656 		addr   = sc->sc_tstate_status.reg_addr;
    657 		offset = sc->sc_tstate_status.reg_bitoffset;
    658 
    659 		(void)AcpiOsReadPort(addr, &val, 8);
    660 
    661 		val = (val >> offset) & 0x0F;
    662 
    663 		for (i = 0; i < sc->sc_tstate_count; i++) {
    664 
    665 			if (sc->sc_tstate[i].ts_percent == 0)
    666 				continue;
    667 
    668 			/*
    669 			 * As the status field may be zero, compare
    670 			 * against the control field value as well.
    671 			 */
    672 			if (val == sc->sc_tstate[i].ts_control) {
    673 				ts = &sc->sc_tstate[i];
    674 				break;
    675 			}
    676 
    677 			if (val == sc->sc_tstate[i].ts_status) {
    678 				ts = &sc->sc_tstate[i];
    679 				break;
    680 			}
    681 		}
    682 
    683 		if (__predict_false(ts == NULL)) {
    684 			rv = EIO;
    685 			goto fail;
    686 		}
    687 
    688 		*percent = ts->ts_percent;
    689 		break;
    690 
    691 	default:
    692 		rv = ENOTTY;
    693 		goto fail;
    694 	}
    695 
    696 	mutex_enter(&sc->sc_mtx);
    697 	sc->sc_tstate_current = *percent;
    698 	mutex_exit(&sc->sc_mtx);
    699 
    700 	return 0;
    701 
    702 fail:
    703 	aprint_error_dev(sc->sc_dev, "failed "
    704 	    "to get T-state (err %d)\n", rv);
    705 
    706 	mutex_enter(&sc->sc_mtx);
    707 	*percent = sc->sc_tstate_current = ACPICPU_T_STATE_UNKNOWN;
    708 	mutex_exit(&sc->sc_mtx);
    709 
    710 	return rv;
    711 }
    712 
    713 int
    714 acpicpu_tstate_set(struct acpicpu_softc *sc, uint32_t percent)
    715 {
    716 	const uint8_t method = sc->sc_tstate_control.reg_spaceid;
    717 	struct acpicpu_tstate *ts = NULL;
    718 	uint32_t i, val;
    719 	uint8_t offset;
    720 	uint64_t addr;
    721 	int rv;
    722 
    723 	if (sc->sc_cold != false) {
    724 		rv = EBUSY;
    725 		goto fail;
    726 	}
    727 
    728 	if ((sc->sc_flags & ACPICPU_FLAG_T) == 0) {
    729 		rv = ENODEV;
    730 		goto fail;
    731 	}
    732 
    733 	mutex_enter(&sc->sc_mtx);
    734 
    735 	for (i = sc->sc_tstate_max; i <= sc->sc_tstate_min; i++) {
    736 
    737 		if (sc->sc_tstate[i].ts_percent == 0)
    738 			continue;
    739 
    740 		if (sc->sc_tstate[i].ts_percent == percent) {
    741 			ts = &sc->sc_tstate[i];
    742 			break;
    743 		}
    744 	}
    745 
    746 	mutex_exit(&sc->sc_mtx);
    747 
    748 	if (__predict_false(ts == NULL)) {
    749 		rv = EINVAL;
    750 		goto fail;
    751 	}
    752 
    753 	switch (method) {
    754 
    755 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    756 
    757 		rv = acpicpu_md_tstate_set(ts);
    758 
    759 		if (rv != 0)
    760 			goto fail;
    761 
    762 		break;
    763 
    764 	case ACPI_ADR_SPACE_SYSTEM_IO:
    765 
    766 		addr   = sc->sc_tstate_control.reg_addr;
    767 		offset = sc->sc_tstate_control.reg_bitoffset;
    768 
    769 		val = (ts->ts_control & 0x0F) << offset;
    770 
    771 		if (ts->ts_percent != 100 && (val & __BIT(4)) == 0) {
    772 			rv = EINVAL;
    773 			goto fail;
    774 		}
    775 
    776 		(void)AcpiOsWritePort(addr, val, 8);
    777 
    778 		/*
    779 		 * If the status field is zero, the transition is
    780 		 * specified to be "asynchronous" and there is no
    781 		 * need to check the status (ACPI 4.0, 8.4.3.2).
    782 		 */
    783 		if (ts->ts_status == 0)
    784 			break;
    785 
    786 		addr   = sc->sc_tstate_status.reg_addr;
    787 		offset = sc->sc_tstate_status.reg_bitoffset;
    788 
    789 		for (i = val = 0; i < ACPICPU_T_STATE_RETRY; i++) {
    790 
    791 			(void)AcpiOsReadPort(addr, &val, 8);
    792 
    793 			val = (val >> offset) & 0x0F;
    794 
    795 			if (val == ts->ts_status)
    796 				break;
    797 
    798 			DELAY(ts->ts_latency);
    799 		}
    800 
    801 		if (i == ACPICPU_T_STATE_RETRY) {
    802 			rv = EAGAIN;
    803 			goto fail;
    804 		}
    805 
    806 		break;
    807 
    808 	default:
    809 		rv = ENOTTY;
    810 		goto fail;
    811 	}
    812 
    813 	mutex_enter(&sc->sc_mtx);
    814 	ts->ts_evcnt.ev_count++;
    815 	sc->sc_tstate_current = percent;
    816 	mutex_exit(&sc->sc_mtx);
    817 
    818 	return 0;
    819 
    820 fail:
    821 	aprint_error_dev(sc->sc_dev, "failed to "
    822 	    "throttle to %u %% (err %d)\n", percent, rv);
    823 
    824 	mutex_enter(&sc->sc_mtx);
    825 	sc->sc_tstate_current = ACPICPU_T_STATE_UNKNOWN;
    826 	mutex_exit(&sc->sc_mtx);
    827 
    828 	return rv;
    829 }
    830