Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_tstate.c revision 1.13
      1 /* $NetBSD: acpi_cpu_tstate.c,v 1.13 2010/08/17 10:35: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.13 2010/08/17 10:35: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 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 static void		 acpicpu_tstate_reset(struct acpicpu_softc *);
     53 
     54 void
     55 acpicpu_tstate_attach(device_t self)
     56 {
     57 	struct acpicpu_softc *sc = device_private(self);
     58 	const char *str;
     59 	ACPI_HANDLE tmp;
     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 	rv  = acpicpu_tstate_tss(sc);
     69 
     70 	if (ACPI_FAILURE(rv)) {
     71 		str = "_TSS";
     72 		goto out;
     73 	}
     74 
     75 	rv = acpicpu_tstate_ptc(sc);
     76 
     77 	if (ACPI_FAILURE(rv)) {
     78 		str = "_PTC";
     79 		goto out;
     80 	}
     81 
     82 	/*
     83 	 * Comparable to P-states, the _TPC object may
     84 	 * be absent in some systems, even though it is
     85 	 * required by ACPI 3.0 along with _TSS and _PTC.
     86 	 */
     87 	rv = AcpiGetHandle(sc->sc_node->ad_handle, "_TPC", &tmp);
     88 
     89 	if (ACPI_FAILURE(rv)) {
     90 		aprint_debug_dev(self, "_TPC missing\n");
     91 		rv = AE_OK;
     92 	}
     93 
     94 out:
     95 	if (ACPI_FAILURE(rv)) {
     96 
     97 		if (rv != AE_NOT_FOUND)
     98 			aprint_error_dev(sc->sc_dev, "failed to evaluate "
     99 			    "%s: %s\n", str, AcpiFormatException(rv));
    100 
    101 		rv = acpicpu_tstate_fadt(sc);
    102 
    103 		if (ACPI_FAILURE(rv))
    104 			return;
    105 
    106 		sc->sc_flags |= ACPICPU_FLAG_T_FADT;
    107 	}
    108 
    109 	sc->sc_flags |= ACPICPU_FLAG_T;
    110 
    111 	acpicpu_tstate_reset(sc);
    112 	acpicpu_tstate_attach_evcnt(sc);
    113 	acpicpu_tstate_attach_print(sc);
    114 }
    115 
    116 static void
    117 acpicpu_tstate_attach_print(struct acpicpu_softc *sc)
    118 {
    119 	const uint8_t method = sc->sc_tstate_control.reg_spaceid;
    120 	struct acpicpu_tstate *ts;
    121 	static bool once = false;
    122 	const char *str;
    123 	uint32_t i;
    124 
    125 	if (once != false)
    126 		return;
    127 
    128 	str = (method != ACPI_ADR_SPACE_FIXED_HARDWARE) ? "I/O" : "FFH";
    129 
    130 	for (i = 0; i < sc->sc_tstate_count; i++) {
    131 
    132 		ts = &sc->sc_tstate[i];
    133 
    134 		if (ts->ts_percent == 0)
    135 			continue;
    136 
    137 		aprint_debug_dev(sc->sc_dev, "T%u: %3s, "
    138 		    "lat %3u us, pow %5u mW, %3u %%\n", i, str,
    139 		    ts->ts_latency, ts->ts_power, ts->ts_percent);
    140 	}
    141 
    142 	once = true;
    143 }
    144 
    145 static void
    146 acpicpu_tstate_attach_evcnt(struct acpicpu_softc *sc)
    147 {
    148 	struct acpicpu_tstate *ts;
    149 	uint32_t i;
    150 
    151 	for (i = 0; i < sc->sc_tstate_count; i++) {
    152 
    153 		ts = &sc->sc_tstate[i];
    154 
    155 		if (ts->ts_percent == 0)
    156 			continue;
    157 
    158 		(void)snprintf(ts->ts_name, sizeof(ts->ts_name),
    159 		    "T%u (%u %%)", i, ts->ts_percent);
    160 
    161 		evcnt_attach_dynamic(&ts->ts_evcnt, EVCNT_TYPE_MISC,
    162 		    NULL, device_xname(sc->sc_dev), ts->ts_name);
    163 	}
    164 }
    165 
    166 int
    167 acpicpu_tstate_detach(device_t self)
    168 {
    169 	struct acpicpu_softc *sc = device_private(self);
    170 	size_t size;
    171 
    172 	if ((sc->sc_flags & ACPICPU_FLAG_T) == 0)
    173 		return 0;
    174 
    175 	size = sc->sc_tstate_count * sizeof(*sc->sc_tstate);
    176 
    177 	if (sc->sc_tstate != NULL)
    178 		kmem_free(sc->sc_tstate, size);
    179 
    180 	sc->sc_flags &= ~ACPICPU_FLAG_T;
    181 	acpicpu_tstate_detach_evcnt(sc);
    182 
    183 	return 0;
    184 }
    185 
    186 static void
    187 acpicpu_tstate_detach_evcnt(struct acpicpu_softc *sc)
    188 {
    189 	struct acpicpu_tstate *ts;
    190 	uint32_t i;
    191 
    192 	for (i = 0; i < sc->sc_tstate_count; i++) {
    193 
    194 		ts = &sc->sc_tstate[i];
    195 
    196 		if (ts->ts_percent != 0)
    197 			evcnt_detach(&ts->ts_evcnt);
    198 	}
    199 }
    200 
    201 void
    202 acpicpu_tstate_start(device_t self)
    203 {
    204 	/* Nothing. */
    205 }
    206 
    207 bool
    208 acpicpu_tstate_suspend(device_t self)
    209 {
    210 	struct acpicpu_softc *sc = device_private(self);
    211 
    212 	acpicpu_tstate_reset(sc);
    213 
    214 	return true;
    215 }
    216 
    217 bool
    218 acpicpu_tstate_resume(device_t self)
    219 {
    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 	/*
    405 	 * The minimum should be around 100 / 8 = 12.5 %.
    406 	 */
    407         if (ts->ts_percent < 10 || ts->ts_percent > 100)
    408 		return AE_BAD_DECIMAL_CONSTANT;
    409 
    410 	if (ts->ts_latency < 1)
    411 		ts->ts_latency = 1;
    412 
    413 	return AE_OK;
    414 }
    415 
    416 ACPI_STATUS
    417 acpicpu_tstate_ptc(struct acpicpu_softc *sc)
    418 {
    419 	static const size_t size = sizeof(struct acpicpu_reg);
    420 	struct acpicpu_reg *reg[2];
    421 	ACPI_OBJECT *elm, *obj;
    422 	ACPI_BUFFER buf;
    423 	ACPI_STATUS rv;
    424 	int i;
    425 
    426 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PTC", &buf);
    427 
    428 	if (ACPI_FAILURE(rv))
    429 		return rv;
    430 
    431 	obj = buf.Pointer;
    432 
    433 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    434 		rv = AE_TYPE;
    435 		goto out;
    436 	}
    437 
    438 	if (obj->Package.Count != 2) {
    439 		rv = AE_LIMIT;
    440 		goto out;
    441 	}
    442 
    443 	for (i = 0; i < 2; i++) {
    444 
    445 		elm = &obj->Package.Elements[i];
    446 
    447 		if (elm->Type != ACPI_TYPE_BUFFER) {
    448 			rv = AE_TYPE;
    449 			goto out;
    450 		}
    451 
    452 		if (size > elm->Buffer.Length) {
    453 			rv = AE_AML_BAD_RESOURCE_LENGTH;
    454 			goto out;
    455 		}
    456 
    457 		reg[i] = (struct acpicpu_reg *)elm->Buffer.Pointer;
    458 
    459 		switch (reg[i]->reg_spaceid) {
    460 
    461 		case ACPI_ADR_SPACE_SYSTEM_IO:
    462 
    463 			if (reg[i]->reg_addr == 0) {
    464 				rv = AE_AML_ILLEGAL_ADDRESS;
    465 				goto out;
    466 			}
    467 
    468 			/*
    469 			 * Check that the values match the IA32 clock
    470 			 * modulation MSR, where the bit 0 is reserved,
    471 			 * bits 1 through 3 define the duty cycle, and
    472 			 * the fourth bit enables the modulation.
    473 			 */
    474 			if (reg[i]->reg_bitwidth != 4) {
    475 				rv = AE_AML_BAD_RESOURCE_VALUE;
    476 				goto out;
    477 			}
    478 
    479 			if (reg[i]->reg_bitoffset != 1) {
    480 				rv = AE_AML_BAD_RESOURCE_VALUE;
    481 				goto out;
    482 			}
    483 
    484 			break;
    485 
    486 		case ACPI_ADR_SPACE_FIXED_HARDWARE:
    487 
    488 			if ((sc->sc_flags & ACPICPU_FLAG_T_FFH) == 0) {
    489 				rv = AE_SUPPORT;
    490 				goto out;
    491 			}
    492 
    493 			break;
    494 
    495 		default:
    496 			rv = AE_AML_INVALID_SPACE_ID;
    497 			goto out;
    498 		}
    499 	}
    500 
    501 	if (reg[0]->reg_spaceid != reg[1]->reg_spaceid) {
    502 		rv = AE_AML_INVALID_SPACE_ID;
    503 		goto out;
    504 	}
    505 
    506 	(void)memcpy(&sc->sc_tstate_control, reg[0], size);
    507 	(void)memcpy(&sc->sc_tstate_status,  reg[1], size);
    508 
    509 out:
    510 	if (buf.Pointer != NULL)
    511 		ACPI_FREE(buf.Pointer);
    512 
    513 	return rv;
    514 }
    515 
    516 static ACPI_STATUS
    517 acpicpu_tstate_fadt(struct acpicpu_softc *sc)
    518 {
    519 	static const size_t size = sizeof(struct acpicpu_tstate);
    520 	const uint8_t offset = AcpiGbl_FADT.DutyOffset;
    521 	const uint8_t width = AcpiGbl_FADT.DutyWidth;
    522 	uint8_t beta, count, i;
    523 
    524 	if (sc->sc_object.ao_pblkaddr == 0)
    525 		return AE_AML_ILLEGAL_ADDRESS;
    526 
    527 	/*
    528 	 * A zero DUTY_WIDTH is used announce that
    529 	 * T-states are not available via FADT.
    530 	 */
    531 	if (width == 0 || width + offset > 4)
    532 		return AE_AML_BAD_RESOURCE_VALUE;
    533 
    534 	count = 1 << width;
    535 
    536 	if (count > ACPICPU_T_STATE_MAX)
    537 		return AE_LIMIT;
    538 
    539 	if (sc->sc_tstate != NULL)
    540 		kmem_free(sc->sc_tstate, sc->sc_tstate_count * size);
    541 
    542 	sc->sc_tstate = kmem_zalloc(count * size, KM_SLEEP);
    543 
    544 	if (sc->sc_tstate == NULL)
    545 		return ENOMEM;
    546 
    547 	sc->sc_tstate_count = count;
    548 
    549 	/*
    550 	 * Approximate duty cycles and set the MSR values.
    551 	 */
    552 	for (beta = 100 / count, i = 0; i < count; i++) {
    553 		sc->sc_tstate[i].ts_percent = 100 - beta * i;
    554 		sc->sc_tstate[i].ts_latency = 1;
    555 	}
    556 
    557 	for (i = 1; i < count; i++)
    558 		sc->sc_tstate[i].ts_control = (count - i) | __BIT(3);
    559 
    560 	/*
    561 	 * Fake values for throttling registers.
    562 	 */
    563 	(void)memset(&sc->sc_tstate_status, 0, sizeof(struct acpicpu_reg));
    564 	(void)memset(&sc->sc_tstate_control, 0, sizeof(struct acpicpu_reg));
    565 
    566 	sc->sc_tstate_status.reg_bitwidth = width;
    567 	sc->sc_tstate_status.reg_bitoffset = offset;
    568 	sc->sc_tstate_status.reg_addr = sc->sc_object.ao_pblkaddr;
    569 	sc->sc_tstate_status.reg_spaceid = ACPI_ADR_SPACE_SYSTEM_IO;
    570 
    571 	sc->sc_tstate_control.reg_bitwidth = width;
    572 	sc->sc_tstate_control.reg_bitoffset = offset;
    573 	sc->sc_tstate_control.reg_addr = sc->sc_object.ao_pblkaddr;
    574 	sc->sc_tstate_control.reg_spaceid = ACPI_ADR_SPACE_SYSTEM_IO;
    575 
    576 	return AE_OK;
    577 }
    578 
    579 static ACPI_STATUS
    580 acpicpu_tstate_change(struct acpicpu_softc *sc)
    581 {
    582 	ACPI_INTEGER val;
    583 	ACPI_STATUS rv;
    584 
    585 	acpicpu_tstate_reset(sc);
    586 
    587 	/*
    588 	 * Evaluate the available T-state window:
    589 	 *
    590 	 *   _TPC : either this maximum or any lower power
    591 	 *          (i.e. higher numbered) state may be used.
    592 	 *
    593 	 *   _TDL : either this minimum or any higher power
    594 	 *	    (i.e. lower numbered) state may be used.
    595 	 *
    596 	 *   _TDL >= _TPC || _TDL >= _TSS[last entry].
    597 	 */
    598 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_TPC", &val);
    599 
    600 	if (ACPI_SUCCESS(rv) && val < sc->sc_tstate_count) {
    601 
    602 		if (sc->sc_tstate[val].ts_percent != 0)
    603 			sc->sc_tstate_max = val;
    604 	}
    605 
    606 	if (sc->sc_passive != true)
    607 		return AE_OK;
    608 
    609 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_TDL", &val);
    610 
    611 	if (ACPI_SUCCESS(rv) && val < sc->sc_tstate_count) {
    612 
    613 		if (val >= sc->sc_tstate_max &&
    614 		    sc->sc_tstate[val].ts_percent != 0)
    615 			sc->sc_tstate_min = val;
    616 	}
    617 
    618 	return AE_OK;
    619 }
    620 
    621 static void
    622 acpicpu_tstate_reset(struct acpicpu_softc *sc)
    623 {
    624 
    625 	sc->sc_tstate_max = 0;
    626 	sc->sc_tstate_min = sc->sc_tstate_count - 1;
    627 }
    628 
    629 int
    630 acpicpu_tstate_get(struct acpicpu_softc *sc, uint32_t *percent)
    631 {
    632 	const uint8_t method = sc->sc_tstate_control.reg_spaceid;
    633 	struct acpicpu_tstate *ts = NULL;
    634 	uint32_t i, val = 0;
    635 	uint8_t offset;
    636 	uint64_t addr;
    637 	int rv;
    638 
    639 	if (sc->sc_cold != false) {
    640 		rv = EBUSY;
    641 		goto fail;
    642 	}
    643 
    644 	if ((sc->sc_flags & ACPICPU_FLAG_T) == 0) {
    645 		rv = ENODEV;
    646 		goto fail;
    647 	}
    648 
    649 	mutex_enter(&sc->sc_mtx);
    650 
    651 	if (sc->sc_tstate_current != ACPICPU_T_STATE_UNKNOWN) {
    652 		*percent = sc->sc_tstate_current;
    653 		mutex_exit(&sc->sc_mtx);
    654 		return 0;
    655 	}
    656 
    657 	mutex_exit(&sc->sc_mtx);
    658 
    659 	switch (method) {
    660 
    661 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    662 
    663 		rv = acpicpu_md_tstate_get(sc, percent);
    664 
    665 		if (rv != 0)
    666 			goto fail;
    667 
    668 		break;
    669 
    670 	case ACPI_ADR_SPACE_SYSTEM_IO:
    671 
    672 		addr   = sc->sc_tstate_status.reg_addr;
    673 		offset = sc->sc_tstate_status.reg_bitoffset;
    674 
    675 		(void)AcpiOsReadPort(addr, &val, 8);
    676 
    677 		val = (val >> offset) & 0x0F;
    678 
    679 		for (i = 0; i < sc->sc_tstate_count; i++) {
    680 
    681 			if (sc->sc_tstate[i].ts_percent == 0)
    682 				continue;
    683 
    684 			/*
    685 			 * As the status field may be zero, compare
    686 			 * against the control field value as well.
    687 			 */
    688 			if (val == sc->sc_tstate[i].ts_control) {
    689 				ts = &sc->sc_tstate[i];
    690 				break;
    691 			}
    692 
    693 			if (val == sc->sc_tstate[i].ts_status) {
    694 				ts = &sc->sc_tstate[i];
    695 				break;
    696 			}
    697 		}
    698 
    699 		if (__predict_false(ts == NULL)) {
    700 			rv = EIO;
    701 			goto fail;
    702 		}
    703 
    704 		*percent = ts->ts_percent;
    705 		break;
    706 
    707 	default:
    708 		rv = ENOTTY;
    709 		goto fail;
    710 	}
    711 
    712 	mutex_enter(&sc->sc_mtx);
    713 	sc->sc_tstate_current = *percent;
    714 	mutex_exit(&sc->sc_mtx);
    715 
    716 	return 0;
    717 
    718 fail:
    719 	aprint_error_dev(sc->sc_dev, "failed "
    720 	    "to get T-state (err %d)\n", rv);
    721 
    722 	mutex_enter(&sc->sc_mtx);
    723 	*percent = sc->sc_tstate_current = ACPICPU_T_STATE_UNKNOWN;
    724 	mutex_exit(&sc->sc_mtx);
    725 
    726 	return rv;
    727 }
    728 
    729 int
    730 acpicpu_tstate_set(struct acpicpu_softc *sc, uint32_t percent)
    731 {
    732 	const uint8_t method = sc->sc_tstate_control.reg_spaceid;
    733 	struct acpicpu_tstate *ts = NULL;
    734 	uint32_t i, val;
    735 	uint8_t offset;
    736 	uint64_t addr;
    737 	int rv;
    738 
    739 	if (sc->sc_cold != false) {
    740 		rv = EBUSY;
    741 		goto fail;
    742 	}
    743 
    744 	if ((sc->sc_flags & ACPICPU_FLAG_T) == 0) {
    745 		rv = ENODEV;
    746 		goto fail;
    747 	}
    748 
    749 	mutex_enter(&sc->sc_mtx);
    750 
    751 	for (i = sc->sc_tstate_max; i <= sc->sc_tstate_min; i++) {
    752 
    753 		if (sc->sc_tstate[i].ts_percent == 0)
    754 			continue;
    755 
    756 		if (sc->sc_tstate[i].ts_percent == percent) {
    757 			ts = &sc->sc_tstate[i];
    758 			break;
    759 		}
    760 	}
    761 
    762 	mutex_exit(&sc->sc_mtx);
    763 
    764 	if (__predict_false(ts == NULL)) {
    765 		rv = EINVAL;
    766 		goto fail;
    767 	}
    768 
    769 	switch (method) {
    770 
    771 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    772 
    773 		rv = acpicpu_md_tstate_set(ts);
    774 
    775 		if (rv != 0)
    776 			goto fail;
    777 
    778 		break;
    779 
    780 	case ACPI_ADR_SPACE_SYSTEM_IO:
    781 
    782 		addr   = sc->sc_tstate_control.reg_addr;
    783 		offset = sc->sc_tstate_control.reg_bitoffset;
    784 
    785 		val = (ts->ts_control & 0x0F) << offset;
    786 
    787 		if (ts->ts_percent != 100 && (val & __BIT(4)) == 0) {
    788 			rv = EINVAL;
    789 			goto fail;
    790 		}
    791 
    792 		(void)AcpiOsWritePort(addr, val, 8);
    793 
    794 		/*
    795 		 * If the status field is zero, the transition is
    796 		 * specified to be "asynchronous" and there is no
    797 		 * need to check the status (ACPI 4.0, 8.4.3.2).
    798 		 */
    799 		if (ts->ts_status == 0)
    800 			break;
    801 
    802 		addr   = sc->sc_tstate_status.reg_addr;
    803 		offset = sc->sc_tstate_status.reg_bitoffset;
    804 
    805 		for (i = val = 0; i < ACPICPU_T_STATE_RETRY; i++) {
    806 
    807 			(void)AcpiOsReadPort(addr, &val, 8);
    808 
    809 			val = (val >> offset) & 0x0F;
    810 
    811 			if (val == ts->ts_status)
    812 				break;
    813 
    814 			DELAY(ts->ts_latency);
    815 		}
    816 
    817 		if (i == ACPICPU_T_STATE_RETRY) {
    818 			rv = EAGAIN;
    819 			goto fail;
    820 		}
    821 
    822 		break;
    823 
    824 	default:
    825 		rv = ENOTTY;
    826 		goto fail;
    827 	}
    828 
    829 	mutex_enter(&sc->sc_mtx);
    830 	ts->ts_evcnt.ev_count++;
    831 	sc->sc_tstate_current = percent;
    832 	mutex_exit(&sc->sc_mtx);
    833 
    834 	return 0;
    835 
    836 fail:
    837 	aprint_error_dev(sc->sc_dev, "failed to "
    838 	    "throttle to %u %% (err %d)\n", percent, rv);
    839 
    840 	mutex_enter(&sc->sc_mtx);
    841 	sc->sc_tstate_current = ACPICPU_T_STATE_UNKNOWN;
    842 	mutex_exit(&sc->sc_mtx);
    843 
    844 	return rv;
    845 }
    846