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