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