Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_pstate.c revision 1.40
      1 /* $NetBSD: acpi_cpu_pstate.c,v 1.40 2011/02/25 19:55:06 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010, 2011 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_pstate.c,v 1.40 2011/02/25 19:55:06 jruoho Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/evcnt.h>
     34 #include <sys/kmem.h>
     35 #include <sys/once.h>
     36 
     37 #include <dev/acpi/acpireg.h>
     38 #include <dev/acpi/acpivar.h>
     39 #include <dev/acpi/acpi_cpu.h>
     40 
     41 #define _COMPONENT	 ACPI_BUS_COMPONENT
     42 ACPI_MODULE_NAME	 ("acpi_cpu_pstate")
     43 
     44 static void		 acpicpu_pstate_attach_print(struct acpicpu_softc *);
     45 static void		 acpicpu_pstate_attach_evcnt(struct acpicpu_softc *);
     46 static void		 acpicpu_pstate_detach_evcnt(struct acpicpu_softc *);
     47 static ACPI_STATUS	 acpicpu_pstate_pss(struct acpicpu_softc *);
     48 static ACPI_STATUS	 acpicpu_pstate_pss_add(struct acpicpu_pstate *,
     49 						ACPI_OBJECT *);
     50 static ACPI_STATUS	 acpicpu_pstate_xpss(struct acpicpu_softc *);
     51 static ACPI_STATUS	 acpicpu_pstate_xpss_add(struct acpicpu_pstate *,
     52 						 ACPI_OBJECT *);
     53 static ACPI_STATUS	 acpicpu_pstate_pct(struct acpicpu_softc *);
     54 static ACPI_STATUS	 acpicpu_pstate_dep(struct acpicpu_softc *);
     55 static int		 acpicpu_pstate_max(struct acpicpu_softc *);
     56 static int		 acpicpu_pstate_min(struct acpicpu_softc *);
     57 static void		 acpicpu_pstate_change(struct acpicpu_softc *);
     58 static void		 acpicpu_pstate_reset(struct acpicpu_softc *);
     59 static void		 acpicpu_pstate_bios(void);
     60 
     61 static uint32_t		 acpicpu_pstate_saved = 0;
     62 
     63 void
     64 acpicpu_pstate_attach(device_t self)
     65 {
     66 	struct acpicpu_softc *sc = device_private(self);
     67 	const char *str;
     68 	ACPI_HANDLE tmp;
     69 	ACPI_STATUS rv;
     70 
     71 	rv = acpicpu_pstate_pss(sc);
     72 
     73 	if (ACPI_FAILURE(rv)) {
     74 		str = "_PSS";
     75 		goto fail;
     76 	}
     77 
     78 	/*
     79 	 * Append additional information from the extended _PSS,
     80 	 * if available. Note that XPSS can not be used on Intel
     81 	 * systems that use either _PDC or _OSC. From the XPSS
     82 	 * method specification:
     83 	 *
     84 	 *   "The platform must not require the use of the
     85 	 *    optional _PDC or _OSC methods to coordinate
     86 	 *    between the operating system and firmware for
     87 	 *    the purposes of enabling specific processor
     88 	 *    power management features or implementations."
     89 	 */
     90 	if (sc->sc_cap == 0) {
     91 
     92 		rv = acpicpu_pstate_xpss(sc);
     93 
     94 		if (ACPI_SUCCESS(rv))
     95 			sc->sc_flags |= ACPICPU_FLAG_P_XPSS;
     96 	}
     97 
     98 	rv = acpicpu_pstate_pct(sc);
     99 
    100 	if (ACPI_FAILURE(rv)) {
    101 		str = "_PCT";
    102 		goto fail;
    103 	}
    104 
    105 	/*
    106 	 * The ACPI 3.0 and 4.0 specifications mandate three
    107 	 * objects for P-states: _PSS, _PCT, and _PPC. A less
    108 	 * strict wording is however used in the earlier 2.0
    109 	 * standard, and some systems conforming to ACPI 2.0
    110 	 * do not have _PPC, the method for dynamic maximum.
    111 	 */
    112 	rv = AcpiGetHandle(sc->sc_node->ad_handle, "_PPC", &tmp);
    113 
    114 	if (ACPI_FAILURE(rv))
    115 		aprint_debug_dev(self, "_PPC missing\n");
    116 
    117 	/*
    118 	 * Employ the XPSS structure by filling
    119 	 * it with MD information required for FFH.
    120 	 */
    121 	rv = acpicpu_md_pstate_pss(sc);
    122 
    123 	if (rv != 0) {
    124 		rv = AE_SUPPORT;
    125 		goto fail;
    126 	}
    127 
    128 	/*
    129 	 * Query the optional _PSD.
    130 	 */
    131 	rv = acpicpu_pstate_dep(sc);
    132 
    133 	if (ACPI_SUCCESS(rv))
    134 		sc->sc_flags |= ACPICPU_FLAG_P_DEP;
    135 
    136 	sc->sc_flags |= ACPICPU_FLAG_P;
    137 
    138 	acpicpu_pstate_bios();
    139 	acpicpu_pstate_reset(sc);
    140 	acpicpu_pstate_attach_evcnt(sc);
    141 	acpicpu_pstate_attach_print(sc);
    142 
    143 	return;
    144 
    145 fail:
    146 	switch (rv) {
    147 
    148 	case AE_NOT_FOUND:
    149 		return;
    150 
    151 	case AE_SUPPORT:
    152 		aprint_verbose_dev(self, "P-states not supported\n");
    153 		return;
    154 
    155 	default:
    156 		aprint_error_dev(self, "failed to evaluate "
    157 		    "%s: %s\n", str, AcpiFormatException(rv));
    158 	}
    159 }
    160 
    161 static void
    162 acpicpu_pstate_attach_print(struct acpicpu_softc *sc)
    163 {
    164 	const uint8_t method = sc->sc_pstate_control.reg_spaceid;
    165 	struct acpicpu_pstate *ps;
    166 	static bool once = false;
    167 	const char *str;
    168 	uint32_t i;
    169 
    170 	if (once != false)
    171 		return;
    172 
    173 	str = (method != ACPI_ADR_SPACE_SYSTEM_IO) ? "FFH" : "I/O";
    174 
    175 	for (i = 0; i < sc->sc_pstate_count; i++) {
    176 
    177 		ps = &sc->sc_pstate[i];
    178 
    179 		if (ps->ps_freq == 0)
    180 			continue;
    181 
    182 		aprint_verbose_dev(sc->sc_dev, "P%d: %3s, "
    183 		    "lat %3u us, pow %5u mW, %4u MHz\n", i, str,
    184 		    ps->ps_latency, ps->ps_power, ps->ps_freq);
    185 	}
    186 
    187 	once = true;
    188 }
    189 
    190 static void
    191 acpicpu_pstate_attach_evcnt(struct acpicpu_softc *sc)
    192 {
    193 	struct acpicpu_pstate *ps;
    194 	uint32_t i;
    195 
    196 	for (i = 0; i < sc->sc_pstate_count; i++) {
    197 
    198 		ps = &sc->sc_pstate[i];
    199 
    200 		if (ps->ps_freq == 0)
    201 			continue;
    202 
    203 		(void)snprintf(ps->ps_name, sizeof(ps->ps_name),
    204 		    "P%u (%u MHz)", i, ps->ps_freq);
    205 
    206 		evcnt_attach_dynamic(&ps->ps_evcnt, EVCNT_TYPE_MISC,
    207 		    NULL, device_xname(sc->sc_dev), ps->ps_name);
    208 	}
    209 }
    210 
    211 int
    212 acpicpu_pstate_detach(device_t self)
    213 {
    214 	struct acpicpu_softc *sc = device_private(self);
    215 	static ONCE_DECL(once_detach);
    216 	size_t size;
    217 	int rv;
    218 
    219 	if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
    220 		return 0;
    221 
    222 	rv = RUN_ONCE(&once_detach, acpicpu_md_pstate_stop);
    223 
    224 	if (rv != 0)
    225 		return rv;
    226 
    227 	size = sc->sc_pstate_count * sizeof(*sc->sc_pstate);
    228 
    229 	if (sc->sc_pstate != NULL)
    230 		kmem_free(sc->sc_pstate, size);
    231 
    232 	sc->sc_flags &= ~ACPICPU_FLAG_P;
    233 	acpicpu_pstate_detach_evcnt(sc);
    234 
    235 	return 0;
    236 }
    237 
    238 static void
    239 acpicpu_pstate_detach_evcnt(struct acpicpu_softc *sc)
    240 {
    241 	struct acpicpu_pstate *ps;
    242 	uint32_t i;
    243 
    244 	for (i = 0; i < sc->sc_pstate_count; i++) {
    245 
    246 		ps = &sc->sc_pstate[i];
    247 
    248 		if (ps->ps_freq != 0)
    249 			evcnt_detach(&ps->ps_evcnt);
    250 	}
    251 }
    252 
    253 void
    254 acpicpu_pstate_start(device_t self)
    255 {
    256 	struct acpicpu_softc *sc = device_private(self);
    257 	struct acpicpu_pstate *ps;
    258 	uint32_t i;
    259 	int rv;
    260 
    261 	rv = acpicpu_md_pstate_start(sc);
    262 
    263 	if (rv != 0)
    264 		goto fail;
    265 
    266 	/*
    267 	 * Initialize the state to P0.
    268 	 */
    269 	for (i = 0, rv = ENXIO; i < sc->sc_pstate_count; i++) {
    270 
    271 		ps = &sc->sc_pstate[i];
    272 
    273 		if (ps->ps_freq != 0) {
    274 			sc->sc_cold = false;
    275 			rv = acpicpu_pstate_set(sc, ps->ps_freq);
    276 			break;
    277 		}
    278 	}
    279 
    280 	if (rv != 0)
    281 		goto fail;
    282 
    283 	return;
    284 
    285 fail:
    286 	sc->sc_flags &= ~ACPICPU_FLAG_P;
    287 
    288 	if (rv == EEXIST) {
    289 		aprint_error_dev(self, "driver conflicts with existing one\n");
    290 		return;
    291 	}
    292 
    293 	aprint_error_dev(self, "failed to start P-states (err %d)\n", rv);
    294 }
    295 
    296 bool
    297 acpicpu_pstate_suspend(device_t self)
    298 {
    299 	struct acpicpu_softc *sc = device_private(self);
    300 	struct acpicpu_pstate *ps = NULL;
    301 	int32_t i;
    302 
    303 	mutex_enter(&sc->sc_mtx);
    304 	acpicpu_pstate_reset(sc);
    305 	mutex_exit(&sc->sc_mtx);
    306 
    307 	if (acpicpu_pstate_saved != 0)
    308 		return true;
    309 
    310 	/*
    311 	 * Following design notes for Windows, we set the highest
    312 	 * P-state when entering any of the system sleep states.
    313 	 * When resuming, the saved P-state will be restored.
    314 	 *
    315 	 *	Microsoft Corporation: Windows Native Processor
    316 	 *	Performance Control. Version 1.1a, November, 2002.
    317 	 */
    318 	for (i = sc->sc_pstate_count - 1; i >= 0; i--) {
    319 
    320 		if (sc->sc_pstate[i].ps_freq != 0) {
    321 			ps = &sc->sc_pstate[i];
    322 			break;
    323 		}
    324 	}
    325 
    326 	if (__predict_false(ps == NULL))
    327 		return true;
    328 
    329 	mutex_enter(&sc->sc_mtx);
    330 	acpicpu_pstate_saved = sc->sc_pstate_current;
    331 	mutex_exit(&sc->sc_mtx);
    332 
    333 	if (acpicpu_pstate_saved == ps->ps_freq)
    334 		return true;
    335 
    336 	(void)acpicpu_pstate_set(sc, ps->ps_freq);
    337 
    338 	return true;
    339 }
    340 
    341 bool
    342 acpicpu_pstate_resume(device_t self)
    343 {
    344 	struct acpicpu_softc *sc = device_private(self);
    345 
    346 	if (acpicpu_pstate_saved != 0) {
    347 		(void)acpicpu_pstate_set(sc, acpicpu_pstate_saved);
    348 		acpicpu_pstate_saved = 0;
    349 	}
    350 
    351 	return true;
    352 }
    353 
    354 void
    355 acpicpu_pstate_callback(void *aux)
    356 {
    357 	struct acpicpu_softc *sc;
    358 	device_t self = aux;
    359 	uint32_t old, new;
    360 
    361 	sc = device_private(self);
    362 
    363 	mutex_enter(&sc->sc_mtx);
    364 
    365 	old = sc->sc_pstate_max;
    366 	acpicpu_pstate_change(sc);
    367 	new = sc->sc_pstate_max;
    368 
    369 	if (old == new) {
    370 		mutex_exit(&sc->sc_mtx);
    371 		return;
    372 	}
    373 
    374 	mutex_exit(&sc->sc_mtx);
    375 
    376 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "maximum frequency "
    377 		"changed from P%u (%u MHz) to P%u (%u MHz)\n",
    378 		old, sc->sc_pstate[old].ps_freq, new,
    379 		sc->sc_pstate[sc->sc_pstate_max].ps_freq));
    380 
    381 	(void)acpicpu_pstate_set(sc, sc->sc_pstate[new].ps_freq);
    382 }
    383 
    384 ACPI_STATUS
    385 acpicpu_pstate_pss(struct acpicpu_softc *sc)
    386 {
    387 	struct acpicpu_pstate *ps;
    388 	ACPI_OBJECT *obj;
    389 	ACPI_BUFFER buf;
    390 	ACPI_STATUS rv;
    391 	uint32_t count;
    392 	uint32_t i, j;
    393 
    394 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PSS", &buf);
    395 
    396 	if (ACPI_FAILURE(rv))
    397 		return rv;
    398 
    399 	obj = buf.Pointer;
    400 
    401 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    402 		rv = AE_TYPE;
    403 		goto out;
    404 	}
    405 
    406 	sc->sc_pstate_count = obj->Package.Count;
    407 
    408 	if (sc->sc_pstate_count == 0) {
    409 		rv = AE_NOT_EXIST;
    410 		goto out;
    411 	}
    412 
    413 	if (sc->sc_pstate_count > ACPICPU_P_STATE_MAX) {
    414 		rv = AE_LIMIT;
    415 		goto out;
    416 	}
    417 
    418 	sc->sc_pstate = kmem_zalloc(sc->sc_pstate_count *
    419 	    sizeof(struct acpicpu_pstate), KM_SLEEP);
    420 
    421 	if (sc->sc_pstate == NULL) {
    422 		rv = AE_NO_MEMORY;
    423 		goto out;
    424 	}
    425 
    426 	for (count = i = 0; i < sc->sc_pstate_count; i++) {
    427 
    428 		ps = &sc->sc_pstate[i];
    429 		rv = acpicpu_pstate_pss_add(ps, &obj->Package.Elements[i]);
    430 
    431 		if (ACPI_FAILURE(rv)) {
    432 			aprint_error_dev(sc->sc_dev, "failed to add "
    433 			    "P-state: %s\n", AcpiFormatException(rv));
    434 			ps->ps_freq = 0;
    435 			continue;
    436 		}
    437 
    438 		for (j = 0; j < i; j++) {
    439 
    440 			if (ps->ps_freq >= sc->sc_pstate[j].ps_freq) {
    441 				ps->ps_freq = 0;
    442 				break;
    443 			}
    444 		}
    445 
    446 		if (ps->ps_freq != 0)
    447 			count++;
    448 	}
    449 
    450 	rv = (count != 0) ? AE_OK : AE_NOT_EXIST;
    451 
    452 out:
    453 	if (buf.Pointer != NULL)
    454 		ACPI_FREE(buf.Pointer);
    455 
    456 	return rv;
    457 }
    458 
    459 static ACPI_STATUS
    460 acpicpu_pstate_pss_add(struct acpicpu_pstate *ps, ACPI_OBJECT *obj)
    461 {
    462 	ACPI_OBJECT *elm;
    463 	int i;
    464 
    465 	if (obj->Type != ACPI_TYPE_PACKAGE)
    466 		return AE_TYPE;
    467 
    468 	if (obj->Package.Count != 6)
    469 		return AE_BAD_DATA;
    470 
    471 	elm = obj->Package.Elements;
    472 
    473 	for (i = 0; i < 6; i++) {
    474 
    475 		if (elm[i].Type != ACPI_TYPE_INTEGER)
    476 			return AE_TYPE;
    477 
    478 		if (elm[i].Integer.Value > UINT32_MAX)
    479 			return AE_AML_NUMERIC_OVERFLOW;
    480 	}
    481 
    482 	ps->ps_freq       = elm[0].Integer.Value;
    483 	ps->ps_power      = elm[1].Integer.Value;
    484 	ps->ps_latency    = elm[2].Integer.Value;
    485 	ps->ps_latency_bm = elm[3].Integer.Value;
    486 	ps->ps_control    = elm[4].Integer.Value;
    487 	ps->ps_status     = elm[5].Integer.Value;
    488 
    489 	if (ps->ps_freq == 0 || ps->ps_freq > 9999)
    490 		return AE_BAD_DECIMAL_CONSTANT;
    491 
    492 	if (ps->ps_latency == 0 || ps->ps_latency > 1000)
    493 		ps->ps_latency = 1;
    494 
    495 	return AE_OK;
    496 }
    497 
    498 static ACPI_STATUS
    499 acpicpu_pstate_xpss(struct acpicpu_softc *sc)
    500 {
    501 	struct acpicpu_pstate *ps;
    502 	ACPI_OBJECT *obj;
    503 	ACPI_BUFFER buf;
    504 	ACPI_STATUS rv;
    505 	uint32_t i = 0;
    506 
    507 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "XPSS", &buf);
    508 
    509 	if (ACPI_FAILURE(rv))
    510 		goto out;
    511 
    512 	obj = buf.Pointer;
    513 
    514 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    515 		rv = AE_TYPE;
    516 		goto out;
    517 	}
    518 
    519 	if (obj->Package.Count != sc->sc_pstate_count) {
    520 		rv = AE_LIMIT;
    521 		goto out;
    522 	}
    523 
    524 	while (i < sc->sc_pstate_count) {
    525 
    526 		ps = &sc->sc_pstate[i];
    527 		acpicpu_pstate_xpss_add(ps, &obj->Package.Elements[i]);
    528 
    529 		i++;
    530 	}
    531 
    532 out:
    533 	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
    534 		aprint_error_dev(sc->sc_dev, "failed to evaluate "
    535 		    "XPSS: %s\n", AcpiFormatException(rv));
    536 
    537 	if (buf.Pointer != NULL)
    538 		ACPI_FREE(buf.Pointer);
    539 
    540 	return rv;
    541 }
    542 
    543 static ACPI_STATUS
    544 acpicpu_pstate_xpss_add(struct acpicpu_pstate *ps, ACPI_OBJECT *obj)
    545 {
    546 	ACPI_OBJECT *elm;
    547 	int i;
    548 
    549 	if (obj->Type != ACPI_TYPE_PACKAGE)
    550 		return AE_TYPE;
    551 
    552 	if (obj->Package.Count != 8)
    553 		return AE_BAD_DATA;
    554 
    555 	elm = obj->Package.Elements;
    556 
    557 	for (i = 0; i < 4; i++) {
    558 
    559 		if (elm[i].Type != ACPI_TYPE_INTEGER)
    560 			return AE_TYPE;
    561 
    562 		if (elm[i].Integer.Value > UINT32_MAX)
    563 			return AE_AML_NUMERIC_OVERFLOW;
    564 	}
    565 
    566 	for (; i < 8; i++) {
    567 
    568 		if (elm[i].Type != ACPI_TYPE_BUFFER)
    569 			return AE_TYPE;
    570 
    571 		if (elm[i].Buffer.Length != 8)
    572 			return AE_LIMIT;
    573 	}
    574 
    575 	/*
    576 	 * Only overwrite the elements that were
    577 	 * not available from the conventional _PSS.
    578 	 */
    579 	if (ps->ps_freq == 0)
    580 		ps->ps_freq = elm[0].Integer.Value;
    581 
    582 	if (ps->ps_power == 0)
    583 		ps->ps_power = elm[1].Integer.Value;
    584 
    585 	if (ps->ps_latency == 0)
    586 		ps->ps_latency = elm[2].Integer.Value;
    587 
    588 	if (ps->ps_latency_bm == 0)
    589 		ps->ps_latency_bm = elm[3].Integer.Value;
    590 
    591 	if (ps->ps_control == 0)
    592 		ps->ps_control = ACPI_GET64(elm[4].Buffer.Pointer);
    593 
    594 	if (ps->ps_status == 0)
    595 		ps->ps_status = ACPI_GET64(elm[5].Buffer.Pointer);
    596 
    597 	if (ps->ps_control_mask == 0)
    598 		ps->ps_control_mask = ACPI_GET64(elm[6].Buffer.Pointer);
    599 
    600 	if (ps->ps_status_mask == 0)
    601 		ps->ps_status_mask = ACPI_GET64(elm[7].Buffer.Pointer);
    602 
    603 	ps->ps_flags |= ACPICPU_FLAG_P_XPSS;
    604 
    605 	if (ps->ps_freq == 0 || ps->ps_freq > 9999)
    606 		return AE_BAD_DECIMAL_CONSTANT;
    607 
    608 	if (ps->ps_latency == 0 || ps->ps_latency > 1000)
    609 		ps->ps_latency = 1;
    610 
    611 	return AE_OK;
    612 }
    613 
    614 ACPI_STATUS
    615 acpicpu_pstate_pct(struct acpicpu_softc *sc)
    616 {
    617 	static const size_t size = sizeof(struct acpicpu_reg);
    618 	struct acpicpu_reg *reg[2];
    619 	struct acpicpu_pstate *ps;
    620 	ACPI_OBJECT *elm, *obj;
    621 	ACPI_BUFFER buf;
    622 	ACPI_STATUS rv;
    623 	uint8_t width;
    624 	uint32_t i;
    625 
    626 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PCT", &buf);
    627 
    628 	if (ACPI_FAILURE(rv))
    629 		return rv;
    630 
    631 	obj = buf.Pointer;
    632 
    633 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    634 		rv = AE_TYPE;
    635 		goto out;
    636 	}
    637 
    638 	if (obj->Package.Count != 2) {
    639 		rv = AE_LIMIT;
    640 		goto out;
    641 	}
    642 
    643 	for (i = 0; i < 2; i++) {
    644 
    645 		elm = &obj->Package.Elements[i];
    646 
    647 		if (elm->Type != ACPI_TYPE_BUFFER) {
    648 			rv = AE_TYPE;
    649 			goto out;
    650 		}
    651 
    652 		if (size > elm->Buffer.Length) {
    653 			rv = AE_AML_BAD_RESOURCE_LENGTH;
    654 			goto out;
    655 		}
    656 
    657 		reg[i] = (struct acpicpu_reg *)elm->Buffer.Pointer;
    658 
    659 		switch (reg[i]->reg_spaceid) {
    660 
    661 		case ACPI_ADR_SPACE_SYSTEM_IO:
    662 
    663 			if (reg[i]->reg_addr == 0) {
    664 				rv = AE_AML_ILLEGAL_ADDRESS;
    665 				goto out;
    666 			}
    667 
    668 			width = reg[i]->reg_bitwidth;
    669 
    670 			if (width + reg[i]->reg_bitoffset > 32) {
    671 				rv = AE_AML_BAD_RESOURCE_VALUE;
    672 				goto out;
    673 			}
    674 
    675 			if (width != 8 && width != 16 && width != 32) {
    676 				rv = AE_AML_BAD_RESOURCE_VALUE;
    677 				goto out;
    678 			}
    679 
    680 			break;
    681 
    682 		case ACPI_ADR_SPACE_FIXED_HARDWARE:
    683 
    684 			if ((sc->sc_flags & ACPICPU_FLAG_P_XPSS) != 0) {
    685 
    686 				if (reg[i]->reg_bitwidth != 64) {
    687 					rv = AE_AML_BAD_RESOURCE_VALUE;
    688 					goto out;
    689 				}
    690 
    691 				if (reg[i]->reg_bitoffset != 0) {
    692 					rv = AE_AML_BAD_RESOURCE_VALUE;
    693 					goto out;
    694 				}
    695 
    696 				break;
    697 			}
    698 
    699 			if ((sc->sc_flags & ACPICPU_FLAG_P_FFH) == 0) {
    700 				rv = AE_SUPPORT;
    701 				goto out;
    702 			}
    703 
    704 			break;
    705 
    706 		default:
    707 			rv = AE_AML_INVALID_SPACE_ID;
    708 			goto out;
    709 		}
    710 	}
    711 
    712 	if (reg[0]->reg_spaceid != reg[1]->reg_spaceid) {
    713 		rv = AE_AML_INVALID_SPACE_ID;
    714 		goto out;
    715 	}
    716 
    717 	(void)memcpy(&sc->sc_pstate_control, reg[0], size);
    718 	(void)memcpy(&sc->sc_pstate_status,  reg[1], size);
    719 
    720 	if ((sc->sc_flags & ACPICPU_FLAG_P_XPSS) == 0)
    721 		goto out;
    722 
    723 	/*
    724 	 * In XPSS the control address can not be zero,
    725 	 * but the status address may be. In this case,
    726 	 * comparable to T-states, we can ignore the status
    727 	 * check during the P-state (FFH) transition.
    728 	 */
    729 	if (sc->sc_pstate_control.reg_addr == 0) {
    730 		rv = AE_AML_BAD_RESOURCE_LENGTH;
    731 		goto out;
    732 	}
    733 
    734 	/*
    735 	 * If XPSS is present, copy the MSR addresses
    736 	 * to the P-state structures for convenience.
    737 	 */
    738 	for (i = 0; i < sc->sc_pstate_count; i++) {
    739 
    740 		ps = &sc->sc_pstate[i];
    741 
    742 		if (ps->ps_freq == 0)
    743 			continue;
    744 
    745 		ps->ps_status_addr  = sc->sc_pstate_status.reg_addr;
    746 		ps->ps_control_addr = sc->sc_pstate_control.reg_addr;
    747 	}
    748 
    749 out:
    750 	if (buf.Pointer != NULL)
    751 		ACPI_FREE(buf.Pointer);
    752 
    753 	return rv;
    754 }
    755 
    756 static ACPI_STATUS
    757 acpicpu_pstate_dep(struct acpicpu_softc *sc)
    758 {
    759 	ACPI_OBJECT *elm, *obj;
    760 	ACPI_BUFFER buf;
    761 	ACPI_STATUS rv;
    762 	uint32_t val;
    763 	uint8_t i, n;
    764 
    765 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PSD", &buf);
    766 
    767 	if (ACPI_FAILURE(rv))
    768 		goto out;
    769 
    770 	obj = buf.Pointer;
    771 
    772 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    773 		rv = AE_TYPE;
    774 		goto out;
    775 	}
    776 
    777 	if (obj->Package.Count != 1) {
    778 		rv = AE_LIMIT;
    779 		goto out;
    780 	}
    781 
    782 	elm = &obj->Package.Elements[0];
    783 
    784 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    785 		rv = AE_TYPE;
    786 		goto out;
    787 	}
    788 
    789 	n = elm->Package.Count;
    790 
    791 	if (n != 5) {
    792 		rv = AE_LIMIT;
    793 		goto out;
    794 	}
    795 
    796 	elm = elm->Package.Elements;
    797 
    798 	for (i = 0; i < n; i++) {
    799 
    800 		if (elm[i].Type != ACPI_TYPE_INTEGER) {
    801 			rv = AE_TYPE;
    802 			goto out;
    803 		}
    804 
    805 		if (elm[i].Integer.Value > UINT32_MAX) {
    806 			rv = AE_AML_NUMERIC_OVERFLOW;
    807 			goto out;
    808 		}
    809 	}
    810 
    811 	val = elm[1].Integer.Value;
    812 
    813 	if (val != 0)
    814 		aprint_debug_dev(sc->sc_dev, "invalid revision in _PSD\n");
    815 
    816 	val = elm[3].Integer.Value;
    817 
    818 	if (val < ACPICPU_DEP_SW_ALL || val > ACPICPU_DEP_HW_ALL) {
    819 		rv = AE_AML_BAD_RESOURCE_VALUE;
    820 		goto out;
    821 	}
    822 
    823 	val = elm[4].Integer.Value;
    824 
    825 	if (val > sc->sc_ncpus) {
    826 		rv = AE_BAD_VALUE;
    827 		goto out;
    828 	}
    829 
    830 	sc->sc_pstate_dep.dep_domain = elm[2].Integer.Value;
    831 	sc->sc_pstate_dep.dep_type   = elm[3].Integer.Value;
    832 	sc->sc_pstate_dep.dep_ncpus  = elm[4].Integer.Value;
    833 
    834 out:
    835 	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
    836 		aprint_debug_dev(sc->sc_dev, "failed to evaluate "
    837 		    "_PSD: %s\n", AcpiFormatException(rv));
    838 
    839 	if (buf.Pointer != NULL)
    840 		ACPI_FREE(buf.Pointer);
    841 
    842 	return rv;
    843 }
    844 
    845 static int
    846 acpicpu_pstate_max(struct acpicpu_softc *sc)
    847 {
    848 	ACPI_INTEGER val;
    849 	ACPI_STATUS rv;
    850 
    851 	/*
    852 	 * Evaluate the currently highest P-state that can be used.
    853 	 * If available, we can use either this state or any lower
    854 	 * power (i.e. higher numbered) state from the _PSS object.
    855 	 * Note that the return value must match the _OST parameter.
    856 	 */
    857 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PPC", &val);
    858 
    859 	if (ACPI_SUCCESS(rv) && val < sc->sc_pstate_count) {
    860 
    861 		if (sc->sc_pstate[val].ps_freq != 0) {
    862 			sc->sc_pstate_max = val;
    863 			return 0;
    864 		}
    865 	}
    866 
    867 	return 1;
    868 }
    869 
    870 static int
    871 acpicpu_pstate_min(struct acpicpu_softc *sc)
    872 {
    873 	ACPI_INTEGER val;
    874 	ACPI_STATUS rv;
    875 
    876 	/*
    877 	 * The _PDL object defines the minimum when passive cooling
    878 	 * is being performed. If available, we can use the returned
    879 	 * state or any higher power (i.e. lower numbered) state.
    880 	 */
    881 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PDL", &val);
    882 
    883 	if (ACPI_SUCCESS(rv) && val < sc->sc_pstate_count) {
    884 
    885 		if (sc->sc_pstate[val].ps_freq == 0)
    886 			return 1;
    887 
    888 		if (val >= sc->sc_pstate_max) {
    889 			sc->sc_pstate_min = val;
    890 			return 0;
    891 		}
    892 	}
    893 
    894 	return 1;
    895 }
    896 
    897 static void
    898 acpicpu_pstate_change(struct acpicpu_softc *sc)
    899 {
    900 	static ACPI_STATUS rv = AE_OK;
    901 	ACPI_OBJECT_LIST arg;
    902 	ACPI_OBJECT obj[2];
    903 	static int val = 0;
    904 
    905 	acpicpu_pstate_reset(sc);
    906 
    907 	/*
    908 	 * Cache the checks as the optional
    909 	 * _PDL and _OST are rarely present.
    910 	 */
    911 	if (val == 0)
    912 		val = acpicpu_pstate_min(sc);
    913 
    914 	arg.Count = 2;
    915 	arg.Pointer = obj;
    916 
    917 	obj[0].Type = ACPI_TYPE_INTEGER;
    918 	obj[1].Type = ACPI_TYPE_INTEGER;
    919 
    920 	obj[0].Integer.Value = ACPICPU_P_NOTIFY;
    921 	obj[1].Integer.Value = acpicpu_pstate_max(sc);
    922 
    923 	if (ACPI_FAILURE(rv))
    924 		return;
    925 
    926 	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_OST", &arg, NULL);
    927 }
    928 
    929 static void
    930 acpicpu_pstate_reset(struct acpicpu_softc *sc)
    931 {
    932 
    933 	sc->sc_pstate_max = 0;
    934 	sc->sc_pstate_min = sc->sc_pstate_count - 1;
    935 
    936 }
    937 
    938 static void
    939 acpicpu_pstate_bios(void)
    940 {
    941 	const uint8_t val = AcpiGbl_FADT.PstateControl;
    942 	const uint32_t addr = AcpiGbl_FADT.SmiCommand;
    943 
    944 	if (addr == 0 || val == 0)
    945 		return;
    946 
    947 	(void)AcpiOsWritePort(addr, val, 8);
    948 }
    949 
    950 int
    951 acpicpu_pstate_get(struct acpicpu_softc *sc, uint32_t *freq)
    952 {
    953 	const uint8_t method = sc->sc_pstate_control.reg_spaceid;
    954 	struct acpicpu_pstate *ps = NULL;
    955 	uint32_t i, val = 0;
    956 	uint64_t addr;
    957 	uint8_t width;
    958 	int rv;
    959 
    960 	if (__predict_false(sc->sc_cold != false)) {
    961 		rv = EBUSY;
    962 		goto fail;
    963 	}
    964 
    965 	if (__predict_false((sc->sc_flags & ACPICPU_FLAG_P) == 0)) {
    966 		rv = ENODEV;
    967 		goto fail;
    968 	}
    969 
    970 	mutex_enter(&sc->sc_mtx);
    971 
    972 	/*
    973 	 * Use the cached value, if available.
    974 	 */
    975 	if (sc->sc_pstate_current != ACPICPU_P_STATE_UNKNOWN) {
    976 		*freq = sc->sc_pstate_current;
    977 		mutex_exit(&sc->sc_mtx);
    978 		return 0;
    979 	}
    980 
    981 	mutex_exit(&sc->sc_mtx);
    982 
    983 	switch (method) {
    984 
    985 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    986 
    987 		rv = acpicpu_md_pstate_get(sc, freq);
    988 
    989 		if (__predict_false(rv != 0))
    990 			goto fail;
    991 
    992 		break;
    993 
    994 	case ACPI_ADR_SPACE_SYSTEM_IO:
    995 
    996 		addr  = sc->sc_pstate_status.reg_addr;
    997 		width = sc->sc_pstate_status.reg_bitwidth;
    998 
    999 		(void)AcpiOsReadPort(addr, &val, width);
   1000 
   1001 		if (val == 0) {
   1002 			rv = EIO;
   1003 			goto fail;
   1004 		}
   1005 
   1006 		for (i = 0; i < sc->sc_pstate_count; i++) {
   1007 
   1008 			if (sc->sc_pstate[i].ps_freq == 0)
   1009 				continue;
   1010 
   1011 			if (val == sc->sc_pstate[i].ps_status) {
   1012 				ps = &sc->sc_pstate[i];
   1013 				break;
   1014 			}
   1015 		}
   1016 
   1017 		if (ps == NULL) {
   1018 			rv = EIO;
   1019 			goto fail;
   1020 		}
   1021 
   1022 		*freq = ps->ps_freq;
   1023 		break;
   1024 
   1025 	default:
   1026 		rv = ENOTTY;
   1027 		goto fail;
   1028 	}
   1029 
   1030 	mutex_enter(&sc->sc_mtx);
   1031 	sc->sc_pstate_current = *freq;
   1032 	mutex_exit(&sc->sc_mtx);
   1033 
   1034 	return 0;
   1035 
   1036 fail:
   1037 	aprint_error_dev(sc->sc_dev, "failed "
   1038 	    "to get frequency (err %d)\n", rv);
   1039 
   1040 	mutex_enter(&sc->sc_mtx);
   1041 	*freq = sc->sc_pstate_current = ACPICPU_P_STATE_UNKNOWN;
   1042 	mutex_exit(&sc->sc_mtx);
   1043 
   1044 	return rv;
   1045 }
   1046 
   1047 int
   1048 acpicpu_pstate_set(struct acpicpu_softc *sc, uint32_t freq)
   1049 {
   1050 	const uint8_t method = sc->sc_pstate_control.reg_spaceid;
   1051 	struct acpicpu_pstate *ps = NULL;
   1052 	uint32_t i, val;
   1053 	uint64_t addr;
   1054 	uint8_t width;
   1055 	int rv;
   1056 
   1057 	if (__predict_false(sc->sc_cold != false)) {
   1058 		rv = EBUSY;
   1059 		goto fail;
   1060 	}
   1061 
   1062 	if (__predict_false((sc->sc_flags & ACPICPU_FLAG_P) == 0)) {
   1063 		rv = ENODEV;
   1064 		goto fail;
   1065 	}
   1066 
   1067 	mutex_enter(&sc->sc_mtx);
   1068 
   1069 	if (sc->sc_pstate_current == freq) {
   1070 		mutex_exit(&sc->sc_mtx);
   1071 		return 0;
   1072 	}
   1073 
   1074 	/*
   1075 	 * Verify that the requested frequency is available.
   1076 	 *
   1077 	 * The access needs to be protected since the currently
   1078 	 * available maximum and minimum may change dynamically.
   1079 	 */
   1080 	for (i = sc->sc_pstate_max; i <= sc->sc_pstate_min; i++) {
   1081 
   1082 		if (__predict_false(sc->sc_pstate[i].ps_freq == 0))
   1083 			continue;
   1084 
   1085 		if (sc->sc_pstate[i].ps_freq == freq) {
   1086 			ps = &sc->sc_pstate[i];
   1087 			break;
   1088 		}
   1089 	}
   1090 
   1091 	mutex_exit(&sc->sc_mtx);
   1092 
   1093 	if (__predict_false(ps == NULL)) {
   1094 		rv = EINVAL;
   1095 		goto fail;
   1096 	}
   1097 
   1098 	switch (method) {
   1099 
   1100 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
   1101 
   1102 		rv = acpicpu_md_pstate_set(ps);
   1103 
   1104 		if (__predict_false(rv != 0))
   1105 			goto fail;
   1106 
   1107 		break;
   1108 
   1109 	case ACPI_ADR_SPACE_SYSTEM_IO:
   1110 
   1111 		addr  = sc->sc_pstate_control.reg_addr;
   1112 		width = sc->sc_pstate_control.reg_bitwidth;
   1113 
   1114 		(void)AcpiOsWritePort(addr, ps->ps_control, width);
   1115 
   1116 		addr  = sc->sc_pstate_status.reg_addr;
   1117 		width = sc->sc_pstate_status.reg_bitwidth;
   1118 
   1119 		/*
   1120 		 * Some systems take longer to respond
   1121 		 * than the reported worst-case latency.
   1122 		 */
   1123 		for (i = val = 0; i < ACPICPU_P_STATE_RETRY; i++) {
   1124 
   1125 			(void)AcpiOsReadPort(addr, &val, width);
   1126 
   1127 			if (val == ps->ps_status)
   1128 				break;
   1129 
   1130 			DELAY(ps->ps_latency);
   1131 		}
   1132 
   1133 		if (i == ACPICPU_P_STATE_RETRY) {
   1134 			rv = EAGAIN;
   1135 			goto fail;
   1136 		}
   1137 
   1138 		break;
   1139 
   1140 	default:
   1141 		rv = ENOTTY;
   1142 		goto fail;
   1143 	}
   1144 
   1145 	mutex_enter(&sc->sc_mtx);
   1146 	ps->ps_evcnt.ev_count++;
   1147 	sc->sc_pstate_current = freq;
   1148 	mutex_exit(&sc->sc_mtx);
   1149 
   1150 	return 0;
   1151 
   1152 fail:
   1153 	aprint_error_dev(sc->sc_dev, "failed to set "
   1154 	    "frequency to %u (err %d)\n", freq, rv);
   1155 
   1156 	mutex_enter(&sc->sc_mtx);
   1157 	sc->sc_pstate_current = ACPICPU_P_STATE_UNKNOWN;
   1158 	mutex_exit(&sc->sc_mtx);
   1159 
   1160 	return rv;
   1161 }
   1162