Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_md.c revision 1.29
      1 /* $NetBSD: acpi_cpu_md.c,v 1.29 2010/08/21 18:25:45 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_md.c,v 1.29 2010/08/21 18:25:45 jruoho Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/kcore.h>
     35 #include <sys/sysctl.h>
     36 #include <sys/xcall.h>
     37 
     38 #include <x86/cpu.h>
     39 #include <x86/cpufunc.h>
     40 #include <x86/cputypes.h>
     41 #include <x86/cpuvar.h>
     42 #include <x86/cpu_msr.h>
     43 #include <x86/machdep.h>
     44 
     45 #include <dev/acpi/acpica.h>
     46 #include <dev/acpi/acpi_cpu.h>
     47 
     48 #include <dev/pci/pcivar.h>
     49 #include <dev/pci/pcidevs.h>
     50 
     51 #define CPUID_INTEL_TSC		__BIT(8)
     52 
     53 #define MSR_0FH_CONTROL		0xc0010041 /* Family 0Fh (and K7).  */
     54 #define MSR_0FH_STATUS		0xc0010042
     55 
     56 #define MSR_10H_LIMIT		0xc0010061 /* Families 10h and 11h. */
     57 #define MSR_10H_CONTROL		0xc0010062
     58 #define MSR_10H_STATUS		0xc0010063
     59 #define MSR_10H_CONFIG		0xc0010064
     60 
     61 static char	  native_idle_text[16];
     62 void		(*native_idle)(void) = NULL;
     63 
     64 static int	 acpicpu_md_quirks_piix4(struct pci_attach_args *);
     65 static void	 acpicpu_md_pstate_status(void *, void *);
     66 static void	 acpicpu_md_tstate_status(void *, void *);
     67 static int	 acpicpu_md_pstate_sysctl_init(void);
     68 static int	 acpicpu_md_pstate_sysctl_get(SYSCTLFN_PROTO);
     69 static int	 acpicpu_md_pstate_sysctl_set(SYSCTLFN_PROTO);
     70 static int	 acpicpu_md_pstate_sysctl_all(SYSCTLFN_PROTO);
     71 
     72 extern uint32_t cpus_running;
     73 extern struct acpicpu_softc **acpicpu_sc;
     74 static struct sysctllog *acpicpu_log = NULL;
     75 
     76 uint32_t
     77 acpicpu_md_cap(void)
     78 {
     79 	struct cpu_info *ci = curcpu();
     80 	uint32_t val = 0;
     81 
     82 	if (cpu_vendor != CPUVENDOR_IDT &&
     83 	    cpu_vendor != CPUVENDOR_INTEL)
     84 		return val;
     85 
     86 	/*
     87 	 * Basic SMP C-states (required for _CST).
     88 	 */
     89 	val |= ACPICPU_PDC_C_C1PT | ACPICPU_PDC_C_C2C3;
     90 
     91         /*
     92 	 * If MONITOR/MWAIT is available, announce
     93 	 * support for native instructions in all C-states.
     94 	 */
     95         if ((ci->ci_feat_val[1] & CPUID2_MONITOR) != 0)
     96 		val |= ACPICPU_PDC_C_C1_FFH | ACPICPU_PDC_C_C2C3_FFH;
     97 
     98 	/*
     99 	 * Set native P- and T-states, if available.
    100 	 */
    101         if ((ci->ci_feat_val[1] & CPUID2_EST) != 0)
    102 		val |= ACPICPU_PDC_P_FFH;
    103 
    104 	if ((ci->ci_feat_val[0] & CPUID_ACPI) != 0)
    105 		val |= ACPICPU_PDC_T_FFH;
    106 
    107 	return val;
    108 }
    109 
    110 uint32_t
    111 acpicpu_md_quirks(void)
    112 {
    113 	struct cpu_info *ci = curcpu();
    114 	struct pci_attach_args pa;
    115 	uint32_t family, val = 0;
    116 	uint32_t regs[4];
    117 
    118 	if (acpicpu_md_cpus_running() == 1)
    119 		val |= ACPICPU_FLAG_C_BM;
    120 
    121 	if ((ci->ci_feat_val[1] & CPUID2_MONITOR) != 0)
    122 		val |= ACPICPU_FLAG_C_FFH;
    123 
    124 	val |= ACPICPU_FLAG_C_APIC | ACPICPU_FLAG_C_TSC;
    125 
    126 	switch (cpu_vendor) {
    127 
    128 	case CPUVENDOR_IDT:
    129 
    130 		if ((ci->ci_feat_val[1] & CPUID2_EST) != 0)
    131 			val |= ACPICPU_FLAG_P_FFH;
    132 
    133 		if ((ci->ci_feat_val[0] & CPUID_ACPI) != 0)
    134 			val |= ACPICPU_FLAG_T_FFH;
    135 
    136 		break;
    137 
    138 	case CPUVENDOR_INTEL:
    139 
    140 		val |= ACPICPU_FLAG_C_BM | ACPICPU_FLAG_C_ARB;
    141 
    142 		if ((ci->ci_feat_val[1] & CPUID2_EST) != 0)
    143 			val |= ACPICPU_FLAG_P_FFH;
    144 
    145 		if ((ci->ci_feat_val[0] & CPUID_ACPI) != 0)
    146 			val |= ACPICPU_FLAG_T_FFH;
    147 
    148 		/*
    149 		 * Check whether MSR_APERF, MSR_MPERF, and Turbo
    150 		 * Boost are available. Also see if we might have
    151 		 * an invariant local APIC timer ("ARAT").
    152 		 */
    153 		if (cpuid_level >= 0x06) {
    154 
    155 			x86_cpuid(0x06, regs);
    156 
    157 			if ((regs[2] & __BIT(0)) != 0)		/* ECX.06[0] */
    158 				val |= ACPICPU_FLAG_P_HW;
    159 
    160 			if ((regs[0] & __BIT(1)) != 0)		/* EAX.06[1] */
    161 				val |= ACPICPU_FLAG_P_TURBO;
    162 
    163 			if ((regs[0] & __BIT(2)) != 0)		/* EAX.06[2] */
    164 				val &= ~ACPICPU_FLAG_C_APIC;
    165 		}
    166 
    167 		/*
    168 		 * Detect whether TSC is invariant. If it is not,
    169 		 * we keep the flag to note that TSC will not run
    170 		 * at constant rate. Depending on the CPU, this may
    171 		 * affect P- and T-state changes, but especially
    172 		 * relevant are C-states; with variant TSC, states
    173 		 * larger than C1 may completely stop the counter.
    174 		 */
    175 		x86_cpuid(0x80000000, regs);
    176 
    177 		if (regs[0] >= 0x80000007) {
    178 
    179 			x86_cpuid(0x80000007, regs);
    180 
    181 			if ((regs[3] & CPUID_INTEL_TSC) != 0)
    182 				val &= ~ACPICPU_FLAG_C_TSC;
    183 		}
    184 
    185 		break;
    186 
    187 	case CPUVENDOR_AMD:
    188 
    189 		family = CPUID2FAMILY(ci->ci_signature);
    190 
    191 		if (family == 0xf)
    192 			family += CPUID2EXTFAMILY(ci->ci_signature);
    193 
    194 		switch (family) {
    195 
    196 		case 0x0f:
    197 		case 0x10:
    198 		case 0x11:
    199 
    200 			x86_cpuid(0x80000007, regs);
    201 
    202 			if ((regs[3] & CPUID_APM_TSC) != 0)
    203 				val &= ~ACPICPU_FLAG_C_TSC;
    204 
    205 			if ((regs[3] & CPUID_APM_HWP) != 0)
    206 				val |= ACPICPU_FLAG_P_FFH;
    207 
    208 			if ((regs[3] & CPUID_APM_CPB) != 0)
    209 				val |= ACPICPU_FLAG_P_TURBO;
    210 		}
    211 
    212 		break;
    213 	}
    214 
    215 	/*
    216 	 * There are several erratums for PIIX4.
    217 	 */
    218 	if (pci_find_device(&pa, acpicpu_md_quirks_piix4) != 0)
    219 		val |= ACPICPU_FLAG_PIIX4;
    220 
    221 	return val;
    222 }
    223 
    224 static int
    225 acpicpu_md_quirks_piix4(struct pci_attach_args *pa)
    226 {
    227 
    228 	/*
    229 	 * XXX: The pci_find_device(9) function only
    230 	 *	deals with attached devices. Change this
    231 	 *	to use something like pci_device_foreach().
    232 	 */
    233 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    234 		return 0;
    235 
    236 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82371AB_ISA ||
    237 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82440MX_PMC)
    238 		return 1;
    239 
    240 	return 0;
    241 }
    242 
    243 uint32_t
    244 acpicpu_md_cpus_running(void)
    245 {
    246 
    247 	return popcount32(cpus_running);
    248 }
    249 
    250 int
    251 acpicpu_md_idle_start(void)
    252 {
    253 	const size_t size = sizeof(native_idle_text);
    254 
    255 	x86_disable_intr();
    256 	x86_cpu_idle_get(&native_idle, native_idle_text, size);
    257 	x86_cpu_idle_set(acpicpu_cstate_idle, "acpi");
    258 	x86_enable_intr();
    259 
    260 	return 0;
    261 }
    262 
    263 int
    264 acpicpu_md_idle_stop(void)
    265 {
    266 	uint64_t xc;
    267 
    268 	x86_disable_intr();
    269 	x86_cpu_idle_set(native_idle, native_idle_text);
    270 	x86_enable_intr();
    271 
    272 	/*
    273 	 * Run a cross-call to ensure that all CPUs are
    274 	 * out from the ACPI idle-loop before detachment.
    275 	 */
    276 	xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    277 	xc_wait(xc);
    278 
    279 	return 0;
    280 }
    281 
    282 /*
    283  * The MD idle loop. Called with interrupts disabled.
    284  */
    285 void
    286 acpicpu_md_idle_enter(int method, int state)
    287 {
    288 	struct cpu_info *ci = curcpu();
    289 
    290 	switch (method) {
    291 
    292 	case ACPICPU_C_STATE_FFH:
    293 
    294 		x86_enable_intr();
    295 		x86_monitor(&ci->ci_want_resched, 0, 0);
    296 
    297 		if (__predict_false(ci->ci_want_resched) != 0)
    298 			return;
    299 
    300 		x86_mwait((state - 1) << 4, 0);
    301 		break;
    302 
    303 	case ACPICPU_C_STATE_HALT:
    304 
    305 		if (__predict_false(ci->ci_want_resched) != 0) {
    306 			x86_enable_intr();
    307 			return;
    308 		}
    309 
    310 		x86_stihlt();
    311 		break;
    312 	}
    313 }
    314 
    315 int
    316 acpicpu_md_pstate_start(void)
    317 {
    318 	const uint64_t est = __BIT(16);
    319 	uint64_t val;
    320 
    321 	switch (cpu_vendor) {
    322 
    323 	case CPUVENDOR_IDT:
    324 	case CPUVENDOR_INTEL:
    325 
    326 		val = rdmsr(MSR_MISC_ENABLE);
    327 
    328 		if ((val & est) == 0) {
    329 
    330 			val |= est;
    331 
    332 			wrmsr(MSR_MISC_ENABLE, val);
    333 			val = rdmsr(MSR_MISC_ENABLE);
    334 
    335 			if ((val & est) == 0)
    336 				return ENOTTY;
    337 		}
    338 	}
    339 
    340 	return acpicpu_md_pstate_sysctl_init();
    341 }
    342 
    343 int
    344 acpicpu_md_pstate_stop(void)
    345 {
    346 
    347 	if (acpicpu_log != NULL)
    348 		sysctl_teardown(&acpicpu_log);
    349 
    350 	return 0;
    351 }
    352 
    353 int
    354 acpicpu_md_pstate_pss(struct acpicpu_softc *sc)
    355 {
    356 	struct acpicpu_pstate *ps, msr;
    357 	struct cpu_info *ci = curcpu();
    358 	uint32_t family, i = 0;
    359 
    360 	(void)memset(&msr, 0, sizeof(struct acpicpu_pstate));
    361 
    362 	switch (cpu_vendor) {
    363 
    364 	case CPUVENDOR_IDT:
    365 	case CPUVENDOR_INTEL:
    366 		msr.ps_control_addr = MSR_PERF_CTL;
    367 		msr.ps_control_mask = __BITS(0, 15);
    368 
    369 		msr.ps_status_addr  = MSR_PERF_STATUS;
    370 		msr.ps_status_mask  = __BITS(0, 15);
    371 		break;
    372 
    373 	case CPUVENDOR_AMD:
    374 
    375 		family = CPUID2FAMILY(ci->ci_signature);
    376 
    377 		if (family == 0xf)
    378 			family += CPUID2EXTFAMILY(ci->ci_signature);
    379 
    380 		switch (family) {
    381 
    382 		case 0x10:
    383 		case 0x11:
    384 			msr.ps_control_addr = MSR_10H_CONTROL;
    385 			msr.ps_control_mask = __BITS(0, 2);
    386 
    387 			msr.ps_status_addr  = MSR_10H_STATUS;
    388 			msr.ps_status_mask  = __BITS(0, 2);
    389 			break;
    390 
    391 		default:
    392 
    393 			if ((sc->sc_flags & ACPICPU_FLAG_P_XPSS) == 0)
    394 				return EOPNOTSUPP;
    395 		}
    396 
    397 		break;
    398 
    399 	default:
    400 		return ENODEV;
    401 	}
    402 
    403 	/*
    404 	 * Fill the P-state structures with MSR addresses that are
    405 	 * known to be correct. If we do not know the addresses,
    406 	 * leave the values intact. If a vendor uses XPSS, we do
    407 	 * not necessary need to do anything to support new CPUs.
    408 	 */
    409 	while (i < sc->sc_pstate_count) {
    410 
    411 		ps = &sc->sc_pstate[i];
    412 
    413 		if (msr.ps_status_addr != 0)
    414 			ps->ps_status_addr = msr.ps_status_addr;
    415 
    416 		if (msr.ps_status_mask != 0)
    417 			ps->ps_status_mask = msr.ps_status_mask;
    418 
    419 		if (msr.ps_control_addr != 0)
    420 			ps->ps_control_addr = msr.ps_control_addr;
    421 
    422 		if (msr.ps_control_mask != 0)
    423 			ps->ps_control_mask = msr.ps_control_mask;
    424 
    425 		i++;
    426 	}
    427 
    428 	/*
    429 	 * When the state is P0 and Turbo Boost has been
    430 	 * detected, we need to skip the status check as
    431 	 * BIOS may not report right comparison values for
    432 	 * the IA32_PERF_STATUS register. Note that this
    433 	 * issue is specific to Intel. For discussion, see:
    434 	 *
    435 	 *	Intel Corporation: Intel Turbo Boost Technology
    436 	 *	in Intel Core(tm) Microarchitectures (Nehalem)
    437 	 *	Based Processors. White Paper, November 2008.
    438 	 */
    439 	if (cpu_vendor != CPUVENDOR_INTEL)
    440 		return 0;
    441 
    442 	if ((sc->sc_flags & ACPICPU_FLAG_P_TURBO) == 0)
    443 		return 0;
    444 
    445 	if (sc->sc_pstate[1].ps_freq + 1 == sc->sc_pstate[0].ps_freq)
    446 		sc->sc_pstate[0].ps_flags |= ACPICPU_FLAG_P_TURBO;
    447 
    448 	return 0;
    449 }
    450 
    451 int
    452 acpicpu_md_pstate_get(struct acpicpu_softc *sc, uint32_t *freq)
    453 {
    454 	struct acpicpu_pstate *ps = NULL;
    455 	uint64_t val;
    456 	uint32_t i;
    457 
    458 	for (i = 0; i < sc->sc_pstate_count; i++) {
    459 
    460 		ps = &sc->sc_pstate[i];
    461 
    462 		if (ps->ps_freq != 0)
    463 			break;
    464 	}
    465 
    466 	if (__predict_false(ps == NULL))
    467 		return ENODEV;
    468 
    469 	if (__predict_false(ps->ps_status_addr == 0))
    470 		return EINVAL;
    471 
    472 	val = rdmsr(ps->ps_status_addr);
    473 
    474 	if (__predict_true(ps->ps_status_mask != 0))
    475 		val = val & ps->ps_status_mask;
    476 
    477 	for (i = 0; i < sc->sc_pstate_count; i++) {
    478 
    479 		ps = &sc->sc_pstate[i];
    480 
    481 		if (ps->ps_freq == 0)
    482 			continue;
    483 
    484 		if (val == ps->ps_status) {
    485 			*freq = ps->ps_freq;
    486 			return 0;
    487 		}
    488 	}
    489 
    490 	return EIO;
    491 }
    492 
    493 int
    494 acpicpu_md_pstate_set(struct acpicpu_pstate *ps)
    495 {
    496 	struct msr_rw_info msr;
    497 	uint64_t xc;
    498 	int rv = 0;
    499 
    500 	msr.msr_read  = false;
    501 	msr.msr_type  = ps->ps_control_addr;
    502 	msr.msr_value = ps->ps_control;
    503 
    504 	if (__predict_true(ps->ps_control_mask != 0)) {
    505 		msr.msr_mask = ps->ps_control_mask;
    506 		msr.msr_read = true;
    507 	}
    508 
    509 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
    510 	xc_wait(xc);
    511 
    512 	if (__predict_false(ps->ps_status == 0))
    513 		return 0;
    514 
    515 	if (__predict_false(ps->ps_status_addr == 0))
    516 		return 0;
    517 
    518 	if ((ps->ps_flags & ACPICPU_FLAG_P_TURBO) != 0)
    519 		return 0;
    520 
    521 	xc = xc_broadcast(0, (xcfunc_t)acpicpu_md_pstate_status, ps, &rv);
    522 	xc_wait(xc);
    523 
    524 	return rv;
    525 }
    526 
    527 static void
    528 acpicpu_md_pstate_status(void *arg1, void *arg2)
    529 {
    530 	struct acpicpu_pstate *ps = arg1;
    531 	uint64_t val;
    532 	int i;
    533 
    534 	for (i = val = 0; i < ACPICPU_P_STATE_RETRY; i++) {
    535 
    536 		val = rdmsr(ps->ps_status_addr);
    537 
    538 		if (__predict_true(ps->ps_status_mask != 0))
    539 			val = val & ps->ps_status_mask;
    540 
    541 		if (val == ps->ps_status)
    542 			return;
    543 
    544 		DELAY(ps->ps_latency);
    545 	}
    546 
    547 	*(uintptr_t *)arg2 = EAGAIN;
    548 }
    549 
    550 int
    551 acpicpu_md_tstate_get(struct acpicpu_softc *sc, uint32_t *percent)
    552 {
    553 	struct acpicpu_tstate *ts;
    554 	uint64_t val;
    555 	uint32_t i;
    556 
    557 	val = rdmsr(MSR_THERM_CONTROL);
    558 
    559 	for (i = 0; i < sc->sc_tstate_count; i++) {
    560 
    561 		ts = &sc->sc_tstate[i];
    562 
    563 		if (ts->ts_percent == 0)
    564 			continue;
    565 
    566 		if (val == ts->ts_status) {
    567 			*percent = ts->ts_percent;
    568 			return 0;
    569 		}
    570 	}
    571 
    572 	return EIO;
    573 }
    574 
    575 int
    576 acpicpu_md_tstate_set(struct acpicpu_tstate *ts)
    577 {
    578 	struct msr_rw_info msr;
    579 	uint64_t xc;
    580 	int rv = 0;
    581 
    582 	msr.msr_read  = true;
    583 	msr.msr_type  = MSR_THERM_CONTROL;
    584 	msr.msr_value = ts->ts_control;
    585 	msr.msr_mask = __BITS(1, 4);
    586 
    587 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
    588 	xc_wait(xc);
    589 
    590 	if (ts->ts_status == 0)
    591 		return 0;
    592 
    593 	xc = xc_broadcast(0, (xcfunc_t)acpicpu_md_tstate_status, ts, &rv);
    594 	xc_wait(xc);
    595 
    596 	return rv;
    597 }
    598 
    599 static void
    600 acpicpu_md_tstate_status(void *arg1, void *arg2)
    601 {
    602 	struct acpicpu_tstate *ts = arg1;
    603 	uint64_t val;
    604 	int i;
    605 
    606 	for (i = val = 0; i < ACPICPU_T_STATE_RETRY; i++) {
    607 
    608 		val = rdmsr(MSR_THERM_CONTROL);
    609 
    610 		if (val == ts->ts_status)
    611 			return;
    612 
    613 		DELAY(ts->ts_latency);
    614 	}
    615 
    616 	*(uintptr_t *)arg2 = EAGAIN;
    617 }
    618 
    619 /*
    620  * A kludge for backwards compatibility.
    621  */
    622 static int
    623 acpicpu_md_pstate_sysctl_init(void)
    624 {
    625 	const struct sysctlnode	*fnode, *mnode, *rnode;
    626 	const char *str;
    627 	int rv;
    628 
    629 	switch (cpu_vendor) {
    630 
    631 	case CPUVENDOR_IDT:
    632 	case CPUVENDOR_INTEL:
    633 		str = "est";
    634 		break;
    635 
    636 	case CPUVENDOR_AMD:
    637 		str = "powernow";
    638 		break;
    639 
    640 	default:
    641 		return ENODEV;
    642 	}
    643 
    644 
    645 	rv = sysctl_createv(&acpicpu_log, 0, NULL, &rnode,
    646 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    647 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
    648 
    649 	if (rv != 0)
    650 		goto fail;
    651 
    652 	rv = sysctl_createv(&acpicpu_log, 0, &rnode, &mnode,
    653 	    0, CTLTYPE_NODE, str, NULL,
    654 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    655 
    656 	if (rv != 0)
    657 		goto fail;
    658 
    659 	rv = sysctl_createv(&acpicpu_log, 0, &mnode, &fnode,
    660 	    0, CTLTYPE_NODE, "frequency", NULL,
    661 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    662 
    663 	if (rv != 0)
    664 		goto fail;
    665 
    666 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
    667 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
    668 	    acpicpu_md_pstate_sysctl_set, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    669 
    670 	if (rv != 0)
    671 		goto fail;
    672 
    673 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
    674 	    CTLFLAG_READONLY, CTLTYPE_INT, "current", NULL,
    675 	    acpicpu_md_pstate_sysctl_get, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    676 
    677 	if (rv != 0)
    678 		goto fail;
    679 
    680 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
    681 	    CTLFLAG_READONLY, CTLTYPE_STRING, "available", NULL,
    682 	    acpicpu_md_pstate_sysctl_all, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    683 
    684 	if (rv != 0)
    685 		goto fail;
    686 
    687 	return 0;
    688 
    689 fail:
    690 	if (acpicpu_log != NULL) {
    691 		sysctl_teardown(&acpicpu_log);
    692 		acpicpu_log = NULL;
    693 	}
    694 
    695 	return rv;
    696 }
    697 
    698 static int
    699 acpicpu_md_pstate_sysctl_get(SYSCTLFN_ARGS)
    700 {
    701 	struct cpu_info *ci = curcpu();
    702 	struct acpicpu_softc *sc;
    703 	struct sysctlnode node;
    704 	uint32_t freq;
    705 	int err;
    706 
    707 	sc = acpicpu_sc[ci->ci_acpiid];
    708 
    709 	if (sc == NULL)
    710 		return ENXIO;
    711 
    712 	err = acpicpu_pstate_get(sc, &freq);
    713 
    714 	if (err != 0)
    715 		return err;
    716 
    717 	node = *rnode;
    718 	node.sysctl_data = &freq;
    719 
    720 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
    721 
    722 	if (err != 0 || newp == NULL)
    723 		return err;
    724 
    725 	return 0;
    726 }
    727 
    728 static int
    729 acpicpu_md_pstate_sysctl_set(SYSCTLFN_ARGS)
    730 {
    731 	struct cpu_info *ci = curcpu();
    732 	struct acpicpu_softc *sc;
    733 	struct sysctlnode node;
    734 	uint32_t freq;
    735 	int err;
    736 
    737 	sc = acpicpu_sc[ci->ci_acpiid];
    738 
    739 	if (sc == NULL)
    740 		return ENXIO;
    741 
    742 	err = acpicpu_pstate_get(sc, &freq);
    743 
    744 	if (err != 0)
    745 		return err;
    746 
    747 	node = *rnode;
    748 	node.sysctl_data = &freq;
    749 
    750 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
    751 
    752 	if (err != 0 || newp == NULL)
    753 		return err;
    754 
    755 	err = acpicpu_pstate_set(sc, freq);
    756 
    757 	if (err != 0)
    758 		return err;
    759 
    760 	return 0;
    761 }
    762 
    763 static int
    764 acpicpu_md_pstate_sysctl_all(SYSCTLFN_ARGS)
    765 {
    766 	struct cpu_info *ci = curcpu();
    767 	struct acpicpu_softc *sc;
    768 	struct sysctlnode node;
    769 	char buf[1024];
    770 	size_t len;
    771 	uint32_t i;
    772 	int err;
    773 
    774 	sc = acpicpu_sc[ci->ci_acpiid];
    775 
    776 	if (sc == NULL)
    777 		return ENXIO;
    778 
    779 	(void)memset(&buf, 0, sizeof(buf));
    780 
    781 	mutex_enter(&sc->sc_mtx);
    782 
    783 	for (len = 0, i = sc->sc_pstate_max; i < sc->sc_pstate_count; i++) {
    784 
    785 		if (sc->sc_pstate[i].ps_freq == 0)
    786 			continue;
    787 
    788 		len += snprintf(buf + len, sizeof(buf) - len, "%u%s",
    789 		    sc->sc_pstate[i].ps_freq,
    790 		    i < (sc->sc_pstate_count - 1) ? " " : "");
    791 	}
    792 
    793 	mutex_exit(&sc->sc_mtx);
    794 
    795 	node = *rnode;
    796 	node.sysctl_data = buf;
    797 
    798 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
    799 
    800 	if (err != 0 || newp == NULL)
    801 		return err;
    802 
    803 	return 0;
    804 }
    805 
    806