Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_tstate.c revision 1.7
      1 /* $NetBSD: acpi_cpu_tstate.c,v 1.7 2010/08/14 17:50:57 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.7 2010/08/14 17:50:57 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 THROTTLE_CTLR.
    553 	 */
    554 	sc->sc_tstate_control.reg_bitwidth = width;
    555 	sc->sc_tstate_control.reg_bitoffset = offset;
    556 	sc->sc_tstate_control.reg_spaceid = ACPI_ADR_SPACE_SYSTEM_IO;
    557 
    558 	sc->sc_tstate_status.reg_addr = sc->sc_object.ao_pblkaddr;
    559 	sc->sc_tstate_control.reg_addr = sc->sc_object.ao_pblkaddr;
    560 
    561 	return AE_OK;
    562 }
    563 
    564 static ACPI_STATUS
    565 acpicpu_tstate_change(struct acpicpu_softc *sc)
    566 {
    567 	ACPI_INTEGER val;
    568 	ACPI_STATUS rv;
    569 
    570 	sc->sc_tstate_max = 0;
    571 	sc->sc_tstate_min = sc->sc_tstate_count - 1;
    572 
    573 	/*
    574 	 * Evaluate the available T-state window:
    575 	 *
    576 	 *   _TPC : either this maximum or any lower power
    577 	 *          (i.e. higher numbered) state may be used.
    578 	 *
    579 	 *   _TDL : either this minimum or any higher power
    580 	 *	    (i.e. lower numbered) state may be used.
    581 	 *
    582 	 *   _TDL >= _TPC || _TDL >= _TSS[last entry].
    583 	 */
    584 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_TPC", &val);
    585 
    586 	if (ACPI_FAILURE(rv))
    587 		return rv;
    588 
    589 	if (val < sc->sc_tstate_count) {
    590 
    591 		if (sc->sc_tstate[val].ts_percent != 0)
    592 			sc->sc_tstate_max = val;
    593 	}
    594 
    595 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_TDL", &val);
    596 
    597 	if (ACPI_SUCCESS(rv) && val < sc->sc_tstate_count) {
    598 
    599 		if (val >= sc->sc_tstate_max &&
    600 		    sc->sc_tstate[val].ts_percent != 0)
    601 			sc->sc_tstate_min = val;
    602 	}
    603 
    604 	return AE_OK;
    605 }
    606 
    607 int
    608 acpicpu_tstate_get(struct acpicpu_softc *sc, uint32_t *percent)
    609 {
    610 	const uint8_t method = sc->sc_tstate_control.reg_spaceid;
    611 	struct acpicpu_tstate *ts = NULL;
    612 	uint32_t i, val = 0;
    613 	uint8_t offset;
    614 	uint64_t addr;
    615 	int rv;
    616 
    617 	if (sc->sc_cold != false) {
    618 		rv = EBUSY;
    619 		goto fail;
    620 	}
    621 
    622 	if ((sc->sc_flags & ACPICPU_FLAG_T) == 0) {
    623 		rv = ENODEV;
    624 		goto fail;
    625 	}
    626 
    627 	mutex_enter(&sc->sc_mtx);
    628 
    629 	if (sc->sc_tstate_current != ACPICPU_T_STATE_UNKNOWN) {
    630 		*percent = sc->sc_tstate_current;
    631 		mutex_exit(&sc->sc_mtx);
    632 		return 0;
    633 	}
    634 
    635 	mutex_exit(&sc->sc_mtx);
    636 
    637 	switch (method) {
    638 
    639 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    640 
    641 		rv = acpicpu_md_tstate_get(sc, percent);
    642 
    643 		if (rv != 0)
    644 			goto fail;
    645 
    646 		break;
    647 
    648 	case ACPI_ADR_SPACE_SYSTEM_IO:
    649 
    650 		addr   = sc->sc_tstate_status.reg_addr;
    651 		offset = sc->sc_tstate_status.reg_bitoffset;
    652 
    653 		(void)AcpiOsReadPort(addr, &val, 8);
    654 
    655 		val = (val >> offset) & 0x0F;
    656 
    657 		for (i = 0; i < sc->sc_tstate_count; i++) {
    658 
    659 			if (sc->sc_tstate[i].ts_percent == 0)
    660 				continue;
    661 
    662 			/*
    663 			 * As the status field may be zero, compare
    664 			 * against the control field value as well.
    665 			 */
    666 			if (val == sc->sc_tstate[i].ts_control) {
    667 				ts = &sc->sc_tstate[i];
    668 				break;
    669 			}
    670 
    671 			if (val == sc->sc_tstate[i].ts_status) {
    672 				ts = &sc->sc_tstate[i];
    673 				break;
    674 			}
    675 		}
    676 
    677 		if (__predict_false(ts == NULL)) {
    678 			rv = EIO;
    679 			goto fail;
    680 		}
    681 
    682 		*percent = ts->ts_percent;
    683 		break;
    684 
    685 	default:
    686 		rv = ENOTTY;
    687 		goto fail;
    688 	}
    689 
    690 	mutex_enter(&sc->sc_mtx);
    691 	sc->sc_tstate_current = *percent;
    692 	mutex_exit(&sc->sc_mtx);
    693 
    694 	return 0;
    695 
    696 fail:
    697 	aprint_error_dev(sc->sc_dev, "failed "
    698 	    "to get T-state (err %d)\n", rv);
    699 
    700 	mutex_enter(&sc->sc_mtx);
    701 	*percent = sc->sc_tstate_current = ACPICPU_T_STATE_UNKNOWN;
    702 	mutex_exit(&sc->sc_mtx);
    703 
    704 	return rv;
    705 }
    706 
    707 int
    708 acpicpu_tstate_set(struct acpicpu_softc *sc, uint32_t percent)
    709 {
    710 	const uint8_t method = sc->sc_tstate_control.reg_spaceid;
    711 	struct acpicpu_tstate *ts = NULL;
    712 	uint32_t i, val;
    713 	uint8_t offset;
    714 	uint64_t addr;
    715 	int rv;
    716 
    717 	if (sc->sc_cold != false) {
    718 		rv = EBUSY;
    719 		goto fail;
    720 	}
    721 
    722 	if ((sc->sc_flags & ACPICPU_FLAG_T) == 0) {
    723 		rv = ENODEV;
    724 		goto fail;
    725 	}
    726 
    727 	mutex_enter(&sc->sc_mtx);
    728 
    729 	for (i = sc->sc_tstate_max; i <= sc->sc_tstate_min; i++) {
    730 
    731 		if (sc->sc_tstate[i].ts_percent == 0)
    732 			continue;
    733 
    734 		if (sc->sc_tstate[i].ts_percent == percent) {
    735 			ts = &sc->sc_tstate[i];
    736 			break;
    737 		}
    738 	}
    739 
    740 	mutex_exit(&sc->sc_mtx);
    741 
    742 	if (__predict_false(ts == NULL)) {
    743 		rv = EINVAL;
    744 		goto fail;
    745 	}
    746 
    747 	switch (method) {
    748 
    749 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    750 
    751 		rv = acpicpu_md_tstate_set(ts);
    752 
    753 		if (rv != 0)
    754 			goto fail;
    755 
    756 		break;
    757 
    758 	case ACPI_ADR_SPACE_SYSTEM_IO:
    759 
    760 		addr   = sc->sc_tstate_control.reg_addr;
    761 		offset = sc->sc_tstate_control.reg_bitoffset;
    762 
    763 		val = (ts->ts_control & 0x0F) << offset;
    764 
    765 		if (ts->ts_percent != 100 && (val & __BIT(4)) == 0) {
    766 			rv = EINVAL;
    767 			goto fail;
    768 		}
    769 
    770 		(void)AcpiOsWritePort(addr, val, 8);
    771 
    772 		/*
    773 		 * If the status field is zero, the transition is
    774 		 * specified to be "asynchronous" and there is no
    775 		 * need to check the status (ACPI 4.0, 8.4.3.2).
    776 		 */
    777 		if (ts->ts_status == 0)
    778 			break;
    779 
    780 		addr   = sc->sc_tstate_status.reg_addr;
    781 		offset = sc->sc_tstate_status.reg_bitoffset;
    782 
    783 		for (i = val = 0; i < ACPICPU_T_STATE_RETRY; i++) {
    784 
    785 			(void)AcpiOsReadPort(addr, &val, 8);
    786 
    787 			val = (val >> offset) & 0x0F;
    788 
    789 			if (val == ts->ts_status)
    790 				break;
    791 
    792 			DELAY(ts->ts_latency);
    793 		}
    794 
    795 		if (i == ACPICPU_T_STATE_RETRY) {
    796 			rv = EAGAIN;
    797 			goto fail;
    798 		}
    799 
    800 		break;
    801 
    802 	default:
    803 		rv = ENOTTY;
    804 		goto fail;
    805 	}
    806 
    807 	mutex_enter(&sc->sc_mtx);
    808 	ts->ts_evcnt.ev_count++;
    809 	sc->sc_tstate_current = percent;
    810 	mutex_exit(&sc->sc_mtx);
    811 
    812 	return 0;
    813 
    814 fail:
    815 	aprint_error_dev(sc->sc_dev, "failed to "
    816 	    "throttle to %u %% (err %d)\n", percent, rv);
    817 
    818 	mutex_enter(&sc->sc_mtx);
    819 	sc->sc_tstate_current = ACPICPU_T_STATE_UNKNOWN;
    820 	mutex_exit(&sc->sc_mtx);
    821 
    822 	return rv;
    823 }
    824