Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_pstate.c revision 1.13.2.2
      1 /* $NetBSD: acpi_cpu_pstate.c,v 1.13.2.2 2010/08/11 22:53:15 yamt 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_pstate.c,v 1.13.2.2 2010/08/11 22:53:15 yamt 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 *sc);
     48 static ACPI_STATUS	 acpicpu_pstate_pss_add(struct acpicpu_pstate *,
     49 						ACPI_OBJECT *);
     50 static ACPI_STATUS	 acpicpu_pstate_pct(struct acpicpu_softc *);
     51 static int		 acpicpu_pstate_max(struct acpicpu_softc *);
     52 static void		 acpicpu_pstate_change(struct acpicpu_softc *);
     53 static void		 acpicpu_pstate_bios(void);
     54 
     55 void
     56 acpicpu_pstate_attach(device_t self)
     57 {
     58 	struct acpicpu_softc *sc = device_private(self);
     59 	const char *str;
     60 	ACPI_STATUS rv;
     61 
     62 	rv = acpicpu_pstate_pss(sc);
     63 
     64 	if (ACPI_FAILURE(rv)) {
     65 		str = "_PSS";
     66 		goto fail;
     67 	}
     68 
     69 	rv = acpicpu_pstate_pct(sc);
     70 
     71 	if (rv == AE_SUPPORT) {
     72 		aprint_error_dev(sc->sc_dev, "CPU not supported\n");
     73 		return;
     74 	}
     75 
     76 	if (ACPI_FAILURE(rv)) {
     77 		str = "_PCT";
     78 		goto fail;
     79 	}
     80 
     81 	rv = acpicpu_pstate_max(sc);
     82 
     83 	if (rv == 0)
     84 		sc->sc_flags |= ACPICPU_FLAG_P_PPC;
     85 
     86 	sc->sc_flags |= ACPICPU_FLAG_P;
     87 	sc->sc_pstate_current = sc->sc_pstate[0].ps_freq;
     88 
     89 	acpicpu_pstate_bios();
     90 	acpicpu_pstate_attach_evcnt(sc);
     91 	acpicpu_pstate_attach_print(sc);
     92 
     93 	return;
     94 
     95 fail:
     96 	aprint_error_dev(sc->sc_dev, "failed to evaluate "
     97 	    "%s: %s\n", str, AcpiFormatException(rv));
     98 }
     99 
    100 static void
    101 acpicpu_pstate_attach_print(struct acpicpu_softc *sc)
    102 {
    103 	const uint8_t method = sc->sc_pstate_control.reg_spaceid;
    104 	struct acpicpu_pstate *ps;
    105 	const char *str;
    106 	uint32_t i;
    107 
    108 	str = (method != ACPI_ADR_SPACE_SYSTEM_IO) ? "FFH" : "SYSIO";
    109 
    110 	for (i = 0; i < sc->sc_pstate_count; i++) {
    111 
    112 		ps = &sc->sc_pstate[i];
    113 
    114 		if (ps->ps_freq == 0)
    115 			continue;
    116 
    117 		aprint_debug_dev(sc->sc_dev, "P%d: %5s, "
    118 		    "lat %3u us, pow %5u mW, %4u MHz\n",
    119 		    i, str, ps->ps_latency, ps->ps_power, ps->ps_freq);
    120 	}
    121 }
    122 
    123 static void
    124 acpicpu_pstate_attach_evcnt(struct acpicpu_softc *sc)
    125 {
    126 	struct acpicpu_pstate *ps;
    127 	uint32_t i;
    128 
    129 	for (i = 0; i < sc->sc_pstate_count; i++) {
    130 
    131 		ps = &sc->sc_pstate[i];
    132 
    133 		if (ps->ps_freq == 0)
    134 			continue;
    135 
    136 		(void)snprintf(ps->ps_name, sizeof(ps->ps_name),
    137 		    "P%u (%u MHz)", i, ps->ps_freq);
    138 
    139 		evcnt_attach_dynamic(&ps->ps_evcnt, EVCNT_TYPE_MISC,
    140 		    NULL, device_xname(sc->sc_dev), ps->ps_name);
    141 	}
    142 }
    143 
    144 int
    145 acpicpu_pstate_detach(device_t self)
    146 {
    147 	struct acpicpu_softc *sc = device_private(self);
    148 	static ONCE_DECL(once_detach);
    149 	size_t size;
    150 	int rv;
    151 
    152 	if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
    153 		return 0;
    154 
    155 	rv = RUN_ONCE(&once_detach, acpicpu_md_pstate_stop);
    156 
    157 	if (rv != 0)
    158 		return rv;
    159 
    160 	size = sc->sc_pstate_count * sizeof(*sc->sc_pstate);
    161 
    162 	if (sc->sc_pstate != NULL)
    163 		kmem_free(sc->sc_pstate, size);
    164 
    165 	sc->sc_flags &= ~ACPICPU_FLAG_P;
    166 	acpicpu_pstate_detach_evcnt(sc);
    167 
    168 	return 0;
    169 }
    170 
    171 static void
    172 acpicpu_pstate_detach_evcnt(struct acpicpu_softc *sc)
    173 {
    174 	struct acpicpu_pstate *ps;
    175 	uint32_t i;
    176 
    177 	for (i = 0; i < sc->sc_pstate_count; i++) {
    178 
    179 		ps = &sc->sc_pstate[i];
    180 
    181 		if (ps->ps_freq != 0)
    182 			evcnt_detach(&ps->ps_evcnt);
    183 	}
    184 }
    185 
    186 int
    187 acpicpu_pstate_start(device_t self)
    188 {
    189 	struct acpicpu_softc *sc = device_private(self);
    190 	static ONCE_DECL(once_start);
    191 
    192 	if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
    193 		return 0;
    194 
    195 	return RUN_ONCE(&once_start, acpicpu_md_pstate_start);
    196 }
    197 
    198 bool
    199 acpicpu_pstate_suspend(device_t self)
    200 {
    201 
    202 	return true;
    203 }
    204 
    205 bool
    206 acpicpu_pstate_resume(device_t self)
    207 {
    208 	static const ACPI_OSD_EXEC_CALLBACK func = acpicpu_pstate_callback;
    209 	struct acpicpu_softc *sc = device_private(self);
    210 
    211 	KASSERT((sc->sc_flags & ACPICPU_FLAG_P) != 0);
    212 
    213 	if ((sc->sc_flags & ACPICPU_FLAG_P_PPC) != 0)
    214 		(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
    215 
    216 	return true;
    217 }
    218 
    219 void
    220 acpicpu_pstate_callback(void *aux)
    221 {
    222 	struct acpicpu_softc *sc;
    223 	device_t self = aux;
    224 	uint32_t old, new;
    225 
    226 	sc = device_private(self);
    227 
    228 	if ((sc->sc_flags & ACPICPU_FLAG_P_PPC) == 0)
    229 		return;
    230 
    231 	mutex_enter(&sc->sc_mtx);
    232 
    233 	old = sc->sc_pstate_max;
    234 	acpicpu_pstate_change(sc);
    235 	new = sc->sc_pstate_max;
    236 
    237 	mutex_exit(&sc->sc_mtx);
    238 
    239 #if 0
    240 	if (old != new) {
    241 
    242 		/*
    243 		 * If the maximum changed, proactively
    244 		 * raise or lower the target frequency.
    245 		 */
    246 		acpicpu_pstate_set(sc, sc->sc_pstate[new].ps_freq);
    247 
    248 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "frequency changed from "
    249 			"%u MHz to %u MHz\n", sc->sc_pstate[old].ps_freq,
    250 			sc->sc_pstate[sc->sc_pstate_max].ps_freq));
    251 	}
    252 #endif
    253 }
    254 
    255 ACPI_STATUS
    256 acpicpu_pstate_pss(struct acpicpu_softc *sc)
    257 {
    258 	struct acpicpu_pstate *ps;
    259 	ACPI_OBJECT *obj;
    260 	ACPI_BUFFER buf;
    261 	ACPI_STATUS rv;
    262 	uint32_t count;
    263 	uint32_t i, j;
    264 
    265 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PSS", &buf);
    266 
    267 	if (ACPI_FAILURE(rv))
    268 		return rv;
    269 
    270 	obj = buf.Pointer;
    271 
    272 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    273 		rv = AE_TYPE;
    274 		goto out;
    275 	}
    276 
    277 	sc->sc_pstate_count = obj->Package.Count;
    278 
    279 	if (sc->sc_pstate_count == 0) {
    280 		rv = AE_NOT_EXIST;
    281 		goto out;
    282 	}
    283 
    284 	if (sc->sc_pstate_count > 0xFF) {
    285 		rv = AE_LIMIT;
    286 		goto out;
    287 	}
    288 
    289 	sc->sc_pstate = kmem_zalloc(sc->sc_pstate_count *
    290 	    sizeof(struct acpicpu_pstate), KM_SLEEP);
    291 
    292 	if (sc->sc_pstate == NULL) {
    293 		rv = AE_NO_MEMORY;
    294 		goto out;
    295 	}
    296 
    297 	for (count = i = 0; i < sc->sc_pstate_count; i++) {
    298 
    299 		ps = &sc->sc_pstate[i];
    300 		rv = acpicpu_pstate_pss_add(ps, &obj->Package.Elements[i]);
    301 
    302 		if (ACPI_FAILURE(rv))
    303 			continue;
    304 
    305 		for (j = 0; j < i; j++) {
    306 
    307 			if (ps->ps_freq >= sc->sc_pstate[j].ps_freq) {
    308 				ps->ps_freq = 0;
    309 				break;
    310 			}
    311 		}
    312 
    313 		if (ps->ps_freq != 0)
    314 			count++;
    315 	}
    316 
    317 	rv = (count != 0) ? AE_OK : AE_NOT_EXIST;
    318 
    319 out:
    320 	if (buf.Pointer != NULL)
    321 		ACPI_FREE(buf.Pointer);
    322 
    323 	return rv;
    324 }
    325 
    326 static ACPI_STATUS
    327 acpicpu_pstate_pss_add(struct acpicpu_pstate *ps, ACPI_OBJECT *obj)
    328 {
    329 	ACPI_OBJECT *elm;
    330 	uint32_t val[6];
    331 	uint32_t *p;
    332 	int i;
    333 
    334 	if (obj->Type != ACPI_TYPE_PACKAGE)
    335 		return AE_TYPE;
    336 
    337 	if (obj->Package.Count != 6)
    338 		return AE_BAD_DATA;
    339 
    340 	elm = obj->Package.Elements;
    341 
    342 	for (i = 0; i < 6; i++) {
    343 
    344 		if (elm[i].Type != ACPI_TYPE_INTEGER)
    345 			return AE_TYPE;
    346 
    347 		if (elm[i].Integer.Value > UINT32_MAX)
    348 			return AE_AML_NUMERIC_OVERFLOW;
    349 
    350 		val[i] = elm[i].Integer.Value;
    351 	}
    352 
    353 	if (val[0] == 0 || val[0] >= 0xFFFF)
    354 		return AE_BAD_DECIMAL_CONSTANT;
    355 
    356 	CTASSERT(sizeof(val) == sizeof(struct acpicpu_pstate) -
    357 	         offsetof(struct acpicpu_pstate, ps_freq));
    358 
    359 	p = &ps->ps_freq;
    360 
    361 	for (i = 0; i < 6; i++, p++)
    362 		*p = val[i];
    363 
    364 	/*
    365 	 * The latency is typically around 10 usec
    366 	 * on Intel CPUs. Use that as the minimum.
    367 	 */
    368 	if (ps->ps_latency < 10)
    369 		ps->ps_latency = 10;
    370 
    371 	return AE_OK;
    372 }
    373 
    374 ACPI_STATUS
    375 acpicpu_pstate_pct(struct acpicpu_softc *sc)
    376 {
    377 	static const size_t size = sizeof(struct acpicpu_reg);
    378 	struct acpicpu_reg *reg[2];
    379 	ACPI_OBJECT *elm, *obj;
    380 	ACPI_BUFFER buf;
    381 	ACPI_STATUS rv;
    382 	uint8_t width;
    383 	int i;
    384 
    385 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_PCT", &buf);
    386 
    387 	if (ACPI_FAILURE(rv))
    388 		return rv;
    389 
    390 	obj = buf.Pointer;
    391 
    392 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    393 		rv = AE_TYPE;
    394 		goto out;
    395 	}
    396 
    397 	if (obj->Package.Count != 2) {
    398 		rv = AE_LIMIT;
    399 		goto out;
    400 	}
    401 
    402 	for (i = 0; i < 2; i++) {
    403 
    404 		elm = &obj->Package.Elements[i];
    405 
    406 		if (elm->Type != ACPI_TYPE_BUFFER) {
    407 			rv = AE_TYPE;
    408 			goto out;
    409 		}
    410 
    411 		if (size > elm->Buffer.Length) {
    412 			rv = AE_AML_BAD_RESOURCE_LENGTH;
    413 			goto out;
    414 		}
    415 
    416 		reg[i] = (struct acpicpu_reg *)elm->Buffer.Pointer;
    417 
    418 		switch (reg[i]->reg_spaceid) {
    419 
    420 		case ACPI_ADR_SPACE_SYSTEM_IO:
    421 
    422 			if (reg[i]->reg_addr == 0) {
    423 				rv = AE_AML_ILLEGAL_ADDRESS;
    424 				goto out;
    425 			}
    426 
    427 			width = reg[i]->reg_bitwidth;
    428 
    429 			if (width != 8 && width != 16 && width != 32) {
    430 				rv = AE_AML_BAD_RESOURCE_VALUE;
    431 				goto out;
    432 			}
    433 
    434 			break;
    435 
    436 		case ACPI_ADR_SPACE_FIXED_HARDWARE:
    437 
    438 			if ((sc->sc_flags & ACPICPU_FLAG_P_FFH) == 0) {
    439 				rv = AE_SUPPORT;
    440 				goto out;
    441 			}
    442 
    443 			break;
    444 
    445 		default:
    446 			rv = AE_AML_INVALID_SPACE_ID;
    447 			goto out;
    448 		}
    449 	}
    450 
    451 	if (reg[0]->reg_spaceid != reg[1]->reg_spaceid) {
    452 		rv = AE_AML_INVALID_SPACE_ID;
    453 		goto out;
    454 	}
    455 
    456 	(void)memcpy(&sc->sc_pstate_control, reg[0], size); /* PERF_CTRL   */
    457 	(void)memcpy(&sc->sc_pstate_status,  reg[1], size); /* PERF_STATUS */
    458 
    459 out:
    460 	if (buf.Pointer != NULL)
    461 		ACPI_FREE(buf.Pointer);
    462 
    463 	return rv;
    464 }
    465 
    466 static int
    467 acpicpu_pstate_max(struct acpicpu_softc *sc)
    468 {
    469 	ACPI_INTEGER val;
    470 	ACPI_STATUS rv;
    471 
    472 	/*
    473 	 * Evaluate the currently highest P-state that can be used.
    474 	 * If available, we can use either this state or any lower
    475 	 * power (i.e. higher numbered) state from the _PSS object.
    476 	 */
    477 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PPC", &val);
    478 
    479 	sc->sc_pstate_max = 0;
    480 
    481 	if (ACPI_FAILURE(rv))
    482 		return 1;
    483 
    484 	if (val > (uint64_t)sc->sc_pstate_count)
    485 		return 1;
    486 
    487 	if (sc->sc_pstate[val].ps_freq == 0)
    488 		return 1;
    489 
    490 	sc->sc_pstate_max = val;		/* XXX: sysctl(8) knob?  */
    491 
    492 	return 0;
    493 }
    494 
    495 static void
    496 acpicpu_pstate_change(struct acpicpu_softc *sc)
    497 {
    498 	ACPI_OBJECT_LIST arg;
    499 	ACPI_OBJECT obj[2];
    500 
    501 	arg.Count = 2;
    502 	arg.Pointer = obj;
    503 
    504 	obj[0].Type = ACPI_TYPE_INTEGER;
    505 	obj[1].Type = ACPI_TYPE_INTEGER;
    506 
    507 	obj[0].Integer.Value = ACPICPU_P_NOTIFY;
    508 	obj[1].Integer.Value = acpicpu_pstate_max(sc);
    509 
    510 	(void)AcpiEvaluateObject(sc->sc_node->ad_handle, "_OST", &arg, NULL);
    511 }
    512 
    513 static void
    514 acpicpu_pstate_bios(void)
    515 {
    516 	const uint8_t val = AcpiGbl_FADT.PstateControl;
    517 	const uint32_t addr = AcpiGbl_FADT.SmiCommand;
    518 
    519 	if (addr == 0)
    520 		return;
    521 
    522 	(void)AcpiOsWritePort(addr, val, 8);
    523 }
    524 
    525 int
    526 acpicpu_pstate_get(struct acpicpu_softc *sc, uint32_t *freq)
    527 {
    528 	const uint8_t method = sc->sc_pstate_control.reg_spaceid;
    529 	struct acpicpu_pstate *ps = NULL;
    530 	uint32_t i, val = 0;
    531 	uint64_t addr;
    532 	uint8_t width;
    533 	int rv;
    534 
    535 	if ((sc->sc_flags & ACPICPU_FLAG_P) == 0) {
    536 		rv = ENODEV;
    537 		goto fail;
    538 	}
    539 
    540 	if (sc->sc_pstate_current != ACPICPU_P_STATE_UNKNOWN) {
    541 		*freq = sc->sc_pstate_current;
    542 		return 0;
    543 	}
    544 
    545 	switch (method) {
    546 
    547 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    548 
    549 		rv = acpicpu_md_pstate_get(sc, freq);
    550 
    551 		if (rv != 0)
    552 			goto fail;
    553 
    554 		break;
    555 
    556 	case ACPI_ADR_SPACE_SYSTEM_IO:
    557 
    558 		addr  = sc->sc_pstate_status.reg_addr;
    559 		width = sc->sc_pstate_status.reg_bitwidth;
    560 
    561 		(void)AcpiOsReadPort(addr, &val, width);
    562 
    563 		if (val == 0) {
    564 			rv = EIO;
    565 			goto fail;
    566 		}
    567 
    568 		mutex_enter(&sc->sc_mtx);
    569 
    570 		for (i = 0; i < sc->sc_pstate_count; i++) {
    571 
    572 			if (sc->sc_pstate[i].ps_freq == 0)
    573 				continue;
    574 
    575 			if (val == sc->sc_pstate[i].ps_status) {
    576 				ps = &sc->sc_pstate[i];
    577 				break;
    578 			}
    579 		}
    580 
    581 		mutex_exit(&sc->sc_mtx);
    582 
    583 		if (ps == NULL) {
    584 			rv = EIO;
    585 			goto fail;
    586 		}
    587 
    588 		*freq = ps->ps_freq;
    589 		break;
    590 
    591 	default:
    592 		rv = ENOTTY;
    593 		goto fail;
    594 	}
    595 
    596 	sc->sc_pstate_current = *freq;
    597 
    598 	return 0;
    599 
    600 fail:
    601 	aprint_error_dev(sc->sc_dev, "failed "
    602 	    "to get frequency (err %d)\n", rv);
    603 
    604 	*freq = sc->sc_pstate_current = ACPICPU_P_STATE_UNKNOWN;
    605 
    606 	return rv;
    607 }
    608 
    609 int
    610 acpicpu_pstate_set(struct acpicpu_softc *sc, uint32_t freq)
    611 {
    612 	const uint8_t method = sc->sc_pstate_control.reg_spaceid;
    613 	struct acpicpu_pstate *ps = NULL;
    614 	uint32_t i, val;
    615 	uint64_t addr;
    616 	uint8_t width;
    617 	int rv;
    618 
    619 	if ((sc->sc_flags & ACPICPU_FLAG_P) == 0) {
    620 		rv = ENODEV;
    621 		goto fail;
    622 	}
    623 
    624 	mutex_enter(&sc->sc_mtx);
    625 
    626 	for (i = sc->sc_pstate_max; i < sc->sc_pstate_count; i++) {
    627 
    628 		if (sc->sc_pstate[i].ps_freq == 0)
    629 			continue;
    630 
    631 		if (sc->sc_pstate[i].ps_freq == freq) {
    632 			ps = &sc->sc_pstate[i];
    633 			break;
    634 		}
    635 	}
    636 
    637 	mutex_exit(&sc->sc_mtx);
    638 
    639 	if (ps == NULL) {
    640 		rv = EINVAL;
    641 		goto fail;
    642 	}
    643 
    644 	switch (method) {
    645 
    646 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
    647 
    648 		rv = acpicpu_md_pstate_set(ps);
    649 
    650 		if (rv != 0)
    651 			goto fail;
    652 
    653 		break;
    654 
    655 	case ACPI_ADR_SPACE_SYSTEM_IO:
    656 
    657 		addr  = sc->sc_pstate_control.reg_addr;
    658 		width = sc->sc_pstate_control.reg_bitwidth;
    659 
    660 		(void)AcpiOsWritePort(addr, ps->ps_control, width);
    661 
    662 		addr  = sc->sc_pstate_status.reg_addr;
    663 		width = sc->sc_pstate_status.reg_bitwidth;
    664 
    665 		/*
    666 		 * Some systems take longer to respond
    667 		 * than the reported worst-case latency.
    668 		 */
    669 		for (i = val = 0; i < ACPICPU_P_STATE_RETRY; i++) {
    670 
    671 			(void)AcpiOsReadPort(addr, &val, width);
    672 
    673 			if (val == ps->ps_status)
    674 				break;
    675 
    676 			DELAY(ps->ps_latency);
    677 		}
    678 
    679 		if (i == ACPICPU_P_STATE_RETRY) {
    680 			rv = EAGAIN;
    681 			goto fail;
    682 		}
    683 
    684 		break;
    685 
    686 	default:
    687 		rv = ENOTTY;
    688 		goto fail;
    689 	}
    690 
    691 	ps->ps_evcnt.ev_count++;
    692 	sc->sc_pstate_current = freq;
    693 
    694 	return 0;
    695 
    696 fail:
    697 	aprint_error_dev(sc->sc_dev, "failed to set "
    698 	    "frequency to %u (err %d)\n", freq, rv);
    699 
    700 	sc->sc_pstate_current = ACPICPU_P_STATE_UNKNOWN;
    701 
    702 	return rv;
    703 }
    704