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