Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_md.c revision 1.31
      1 /* $NetBSD: acpi_cpu_md.c,v 1.31 2010/08/23 16:20:44 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.31 2010/08/23 16:20:44 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(struct acpicpu_softc *sc)
    252 {
    253 	const size_t size = sizeof(native_idle_text);
    254 	struct acpicpu_cstate *cs;
    255 	bool ipi = false;
    256 	int i;
    257 
    258 	x86_cpu_idle_get(&native_idle, native_idle_text, size);
    259 
    260 	for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
    261 
    262 		cs = &sc->sc_cstate[i];
    263 
    264 		if (cs->cs_method == ACPICPU_C_STATE_HALT) {
    265 			ipi = true;
    266 			break;
    267 		}
    268 	}
    269 
    270 	x86_cpu_idle_set(acpicpu_cstate_idle, "acpi", ipi);
    271 
    272 	return 0;
    273 }
    274 
    275 int
    276 acpicpu_md_idle_stop(void)
    277 {
    278 	uint64_t xc;
    279 	bool ipi;
    280 
    281 	ipi = (native_idle != x86_cpu_idle_halt) ? false : true;
    282 	x86_cpu_idle_set(native_idle, native_idle_text, ipi);
    283 
    284 	/*
    285 	 * Run a cross-call to ensure that all CPUs are
    286 	 * out from the ACPI idle-loop before detachment.
    287 	 */
    288 	xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    289 	xc_wait(xc);
    290 
    291 	return 0;
    292 }
    293 
    294 /*
    295  * Called with interrupts disabled.
    296  * Caller should enable interrupts after return.
    297  */
    298 void
    299 acpicpu_md_idle_enter(int method, int state)
    300 {
    301 	struct cpu_info *ci = curcpu();
    302 
    303 	switch (method) {
    304 
    305 	case ACPICPU_C_STATE_FFH:
    306 
    307 		x86_enable_intr();
    308 		x86_monitor(&ci->ci_want_resched, 0, 0);
    309 
    310 		if (__predict_false(ci->ci_want_resched != 0))
    311 			return;
    312 
    313 		x86_mwait((state - 1) << 4, 0);
    314 		break;
    315 
    316 	case ACPICPU_C_STATE_HALT:
    317 
    318 		if (__predict_false(ci->ci_want_resched != 0))
    319 			return;
    320 
    321 		x86_stihlt();
    322 		break;
    323 	}
    324 }
    325 
    326 int
    327 acpicpu_md_pstate_start(void)
    328 {
    329 	const uint64_t est = __BIT(16);
    330 	uint64_t val;
    331 
    332 	switch (cpu_vendor) {
    333 
    334 	case CPUVENDOR_IDT:
    335 	case CPUVENDOR_INTEL:
    336 
    337 		val = rdmsr(MSR_MISC_ENABLE);
    338 
    339 		if ((val & est) == 0) {
    340 
    341 			val |= est;
    342 
    343 			wrmsr(MSR_MISC_ENABLE, val);
    344 			val = rdmsr(MSR_MISC_ENABLE);
    345 
    346 			if ((val & est) == 0)
    347 				return ENOTTY;
    348 		}
    349 	}
    350 
    351 	return acpicpu_md_pstate_sysctl_init();
    352 }
    353 
    354 int
    355 acpicpu_md_pstate_stop(void)
    356 {
    357 
    358 	if (acpicpu_log != NULL)
    359 		sysctl_teardown(&acpicpu_log);
    360 
    361 	return 0;
    362 }
    363 
    364 int
    365 acpicpu_md_pstate_pss(struct acpicpu_softc *sc)
    366 {
    367 	struct acpicpu_pstate *ps, msr;
    368 	struct cpu_info *ci = curcpu();
    369 	uint32_t family, i = 0;
    370 
    371 	(void)memset(&msr, 0, sizeof(struct acpicpu_pstate));
    372 
    373 	switch (cpu_vendor) {
    374 
    375 	case CPUVENDOR_IDT:
    376 	case CPUVENDOR_INTEL:
    377 		msr.ps_control_addr = MSR_PERF_CTL;
    378 		msr.ps_control_mask = __BITS(0, 15);
    379 
    380 		msr.ps_status_addr  = MSR_PERF_STATUS;
    381 		msr.ps_status_mask  = __BITS(0, 15);
    382 		break;
    383 
    384 	case CPUVENDOR_AMD:
    385 
    386 		family = CPUID2FAMILY(ci->ci_signature);
    387 
    388 		if (family == 0xf)
    389 			family += CPUID2EXTFAMILY(ci->ci_signature);
    390 
    391 		switch (family) {
    392 
    393 		case 0x10:
    394 		case 0x11:
    395 			msr.ps_control_addr = MSR_10H_CONTROL;
    396 			msr.ps_control_mask = __BITS(0, 2);
    397 
    398 			msr.ps_status_addr  = MSR_10H_STATUS;
    399 			msr.ps_status_mask  = __BITS(0, 2);
    400 			break;
    401 
    402 		default:
    403 
    404 			if ((sc->sc_flags & ACPICPU_FLAG_P_XPSS) == 0)
    405 				return EOPNOTSUPP;
    406 		}
    407 
    408 		break;
    409 
    410 	default:
    411 		return ENODEV;
    412 	}
    413 
    414 	/*
    415 	 * Fill the P-state structures with MSR addresses that are
    416 	 * known to be correct. If we do not know the addresses,
    417 	 * leave the values intact. If a vendor uses XPSS, we do
    418 	 * not necessary need to do anything to support new CPUs.
    419 	 */
    420 	while (i < sc->sc_pstate_count) {
    421 
    422 		ps = &sc->sc_pstate[i];
    423 
    424 		if (msr.ps_status_addr != 0)
    425 			ps->ps_status_addr = msr.ps_status_addr;
    426 
    427 		if (msr.ps_status_mask != 0)
    428 			ps->ps_status_mask = msr.ps_status_mask;
    429 
    430 		if (msr.ps_control_addr != 0)
    431 			ps->ps_control_addr = msr.ps_control_addr;
    432 
    433 		if (msr.ps_control_mask != 0)
    434 			ps->ps_control_mask = msr.ps_control_mask;
    435 
    436 		i++;
    437 	}
    438 
    439 	/*
    440 	 * When the state is P0 and Turbo Boost has been
    441 	 * detected, we need to skip the status check as
    442 	 * BIOS may not report right comparison values for
    443 	 * the IA32_PERF_STATUS register. Note that this
    444 	 * issue is specific to Intel. For discussion, see:
    445 	 *
    446 	 *	Intel Corporation: Intel Turbo Boost Technology
    447 	 *	in Intel Core(tm) Microarchitectures (Nehalem)
    448 	 *	Based Processors. White Paper, November 2008.
    449 	 */
    450 	if (cpu_vendor != CPUVENDOR_INTEL)
    451 		return 0;
    452 
    453 	if ((sc->sc_flags & ACPICPU_FLAG_P_TURBO) == 0)
    454 		return 0;
    455 
    456 	if (sc->sc_pstate[1].ps_freq + 1 == sc->sc_pstate[0].ps_freq)
    457 		sc->sc_pstate[0].ps_flags |= ACPICPU_FLAG_P_TURBO;
    458 
    459 	return 0;
    460 }
    461 
    462 int
    463 acpicpu_md_pstate_get(struct acpicpu_softc *sc, uint32_t *freq)
    464 {
    465 	struct acpicpu_pstate *ps = NULL;
    466 	uint64_t val;
    467 	uint32_t i;
    468 
    469 	for (i = 0; i < sc->sc_pstate_count; i++) {
    470 
    471 		ps = &sc->sc_pstate[i];
    472 
    473 		if (ps->ps_freq != 0)
    474 			break;
    475 	}
    476 
    477 	if (__predict_false(ps == NULL))
    478 		return ENODEV;
    479 
    480 	if (__predict_false(ps->ps_status_addr == 0))
    481 		return EINVAL;
    482 
    483 	val = rdmsr(ps->ps_status_addr);
    484 
    485 	if (__predict_true(ps->ps_status_mask != 0))
    486 		val = val & ps->ps_status_mask;
    487 
    488 	for (i = 0; i < sc->sc_pstate_count; i++) {
    489 
    490 		ps = &sc->sc_pstate[i];
    491 
    492 		if (ps->ps_freq == 0)
    493 			continue;
    494 
    495 		if (val == ps->ps_status) {
    496 			*freq = ps->ps_freq;
    497 			return 0;
    498 		}
    499 	}
    500 
    501 	return EIO;
    502 }
    503 
    504 int
    505 acpicpu_md_pstate_set(struct acpicpu_pstate *ps)
    506 {
    507 	struct msr_rw_info msr;
    508 	uint64_t xc;
    509 	int rv = 0;
    510 
    511 	msr.msr_read  = false;
    512 	msr.msr_type  = ps->ps_control_addr;
    513 	msr.msr_value = ps->ps_control;
    514 
    515 	if (__predict_true(ps->ps_control_mask != 0)) {
    516 		msr.msr_mask = ps->ps_control_mask;
    517 		msr.msr_read = true;
    518 	}
    519 
    520 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
    521 	xc_wait(xc);
    522 
    523 	if (__predict_false(ps->ps_status == 0))
    524 		goto out;
    525 
    526 	if (__predict_false(ps->ps_status_addr == 0))
    527 		goto out;
    528 
    529 	if ((ps->ps_flags & ACPICPU_FLAG_P_TURBO) != 0)
    530 		goto out;
    531 
    532 	xc = xc_broadcast(0, (xcfunc_t)acpicpu_md_pstate_status, ps, &rv);
    533 	xc_wait(xc);
    534 
    535 	return rv;
    536 
    537 out:
    538 	DELAY(ps->ps_latency);
    539 
    540 	return 0;
    541 }
    542 
    543 static void
    544 acpicpu_md_pstate_status(void *arg1, void *arg2)
    545 {
    546 	struct acpicpu_pstate *ps = arg1;
    547 	uint64_t val;
    548 	int i;
    549 
    550 	for (i = val = 0; i < ACPICPU_P_STATE_RETRY; i++) {
    551 
    552 		val = rdmsr(ps->ps_status_addr);
    553 
    554 		if (__predict_true(ps->ps_status_mask != 0))
    555 			val = val & ps->ps_status_mask;
    556 
    557 		if (val == ps->ps_status)
    558 			return;
    559 
    560 		DELAY(ps->ps_latency);
    561 	}
    562 
    563 	*(uintptr_t *)arg2 = EAGAIN;
    564 }
    565 
    566 int
    567 acpicpu_md_tstate_get(struct acpicpu_softc *sc, uint32_t *percent)
    568 {
    569 	struct acpicpu_tstate *ts;
    570 	uint64_t val;
    571 	uint32_t i;
    572 
    573 	val = rdmsr(MSR_THERM_CONTROL);
    574 
    575 	for (i = 0; i < sc->sc_tstate_count; i++) {
    576 
    577 		ts = &sc->sc_tstate[i];
    578 
    579 		if (ts->ts_percent == 0)
    580 			continue;
    581 
    582 		if (val == ts->ts_status) {
    583 			*percent = ts->ts_percent;
    584 			return 0;
    585 		}
    586 	}
    587 
    588 	return EIO;
    589 }
    590 
    591 int
    592 acpicpu_md_tstate_set(struct acpicpu_tstate *ts)
    593 {
    594 	struct msr_rw_info msr;
    595 	uint64_t xc;
    596 	int rv = 0;
    597 
    598 	msr.msr_read  = true;
    599 	msr.msr_type  = MSR_THERM_CONTROL;
    600 	msr.msr_value = ts->ts_control;
    601 	msr.msr_mask = __BITS(1, 4);
    602 
    603 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
    604 	xc_wait(xc);
    605 
    606 	if (ts->ts_status == 0) {
    607 		DELAY(ts->ts_latency);
    608 		return 0;
    609 	}
    610 
    611 	xc = xc_broadcast(0, (xcfunc_t)acpicpu_md_tstate_status, ts, &rv);
    612 	xc_wait(xc);
    613 
    614 	return rv;
    615 }
    616 
    617 static void
    618 acpicpu_md_tstate_status(void *arg1, void *arg2)
    619 {
    620 	struct acpicpu_tstate *ts = arg1;
    621 	uint64_t val;
    622 	int i;
    623 
    624 	for (i = val = 0; i < ACPICPU_T_STATE_RETRY; i++) {
    625 
    626 		val = rdmsr(MSR_THERM_CONTROL);
    627 
    628 		if (val == ts->ts_status)
    629 			return;
    630 
    631 		DELAY(ts->ts_latency);
    632 	}
    633 
    634 	*(uintptr_t *)arg2 = EAGAIN;
    635 }
    636 
    637 /*
    638  * A kludge for backwards compatibility.
    639  */
    640 static int
    641 acpicpu_md_pstate_sysctl_init(void)
    642 {
    643 	const struct sysctlnode	*fnode, *mnode, *rnode;
    644 	const char *str;
    645 	int rv;
    646 
    647 	switch (cpu_vendor) {
    648 
    649 	case CPUVENDOR_IDT:
    650 	case CPUVENDOR_INTEL:
    651 		str = "est";
    652 		break;
    653 
    654 	case CPUVENDOR_AMD:
    655 		str = "powernow";
    656 		break;
    657 
    658 	default:
    659 		return ENODEV;
    660 	}
    661 
    662 
    663 	rv = sysctl_createv(&acpicpu_log, 0, NULL, &rnode,
    664 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    665 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
    666 
    667 	if (rv != 0)
    668 		goto fail;
    669 
    670 	rv = sysctl_createv(&acpicpu_log, 0, &rnode, &mnode,
    671 	    0, CTLTYPE_NODE, str, NULL,
    672 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    673 
    674 	if (rv != 0)
    675 		goto fail;
    676 
    677 	rv = sysctl_createv(&acpicpu_log, 0, &mnode, &fnode,
    678 	    0, CTLTYPE_NODE, "frequency", NULL,
    679 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    680 
    681 	if (rv != 0)
    682 		goto fail;
    683 
    684 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
    685 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
    686 	    acpicpu_md_pstate_sysctl_set, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    687 
    688 	if (rv != 0)
    689 		goto fail;
    690 
    691 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
    692 	    CTLFLAG_READONLY, CTLTYPE_INT, "current", NULL,
    693 	    acpicpu_md_pstate_sysctl_get, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    694 
    695 	if (rv != 0)
    696 		goto fail;
    697 
    698 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
    699 	    CTLFLAG_READONLY, CTLTYPE_STRING, "available", NULL,
    700 	    acpicpu_md_pstate_sysctl_all, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    701 
    702 	if (rv != 0)
    703 		goto fail;
    704 
    705 	return 0;
    706 
    707 fail:
    708 	if (acpicpu_log != NULL) {
    709 		sysctl_teardown(&acpicpu_log);
    710 		acpicpu_log = NULL;
    711 	}
    712 
    713 	return rv;
    714 }
    715 
    716 static int
    717 acpicpu_md_pstate_sysctl_get(SYSCTLFN_ARGS)
    718 {
    719 	struct cpu_info *ci = curcpu();
    720 	struct acpicpu_softc *sc;
    721 	struct sysctlnode node;
    722 	uint32_t freq;
    723 	int err;
    724 
    725 	sc = acpicpu_sc[ci->ci_acpiid];
    726 
    727 	if (sc == NULL)
    728 		return ENXIO;
    729 
    730 	err = acpicpu_pstate_get(sc, &freq);
    731 
    732 	if (err != 0)
    733 		return err;
    734 
    735 	node = *rnode;
    736 	node.sysctl_data = &freq;
    737 
    738 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
    739 
    740 	if (err != 0 || newp == NULL)
    741 		return err;
    742 
    743 	return 0;
    744 }
    745 
    746 static int
    747 acpicpu_md_pstate_sysctl_set(SYSCTLFN_ARGS)
    748 {
    749 	struct cpu_info *ci = curcpu();
    750 	struct acpicpu_softc *sc;
    751 	struct sysctlnode node;
    752 	uint32_t freq;
    753 	int err;
    754 
    755 	sc = acpicpu_sc[ci->ci_acpiid];
    756 
    757 	if (sc == NULL)
    758 		return ENXIO;
    759 
    760 	err = acpicpu_pstate_get(sc, &freq);
    761 
    762 	if (err != 0)
    763 		return err;
    764 
    765 	node = *rnode;
    766 	node.sysctl_data = &freq;
    767 
    768 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
    769 
    770 	if (err != 0 || newp == NULL)
    771 		return err;
    772 
    773 	err = acpicpu_pstate_set(sc, freq);
    774 
    775 	if (err != 0)
    776 		return err;
    777 
    778 	return 0;
    779 }
    780 
    781 static int
    782 acpicpu_md_pstate_sysctl_all(SYSCTLFN_ARGS)
    783 {
    784 	struct cpu_info *ci = curcpu();
    785 	struct acpicpu_softc *sc;
    786 	struct sysctlnode node;
    787 	char buf[1024];
    788 	size_t len;
    789 	uint32_t i;
    790 	int err;
    791 
    792 	sc = acpicpu_sc[ci->ci_acpiid];
    793 
    794 	if (sc == NULL)
    795 		return ENXIO;
    796 
    797 	(void)memset(&buf, 0, sizeof(buf));
    798 
    799 	mutex_enter(&sc->sc_mtx);
    800 
    801 	for (len = 0, i = sc->sc_pstate_max; i < sc->sc_pstate_count; i++) {
    802 
    803 		if (sc->sc_pstate[i].ps_freq == 0)
    804 			continue;
    805 
    806 		len += snprintf(buf + len, sizeof(buf) - len, "%u%s",
    807 		    sc->sc_pstate[i].ps_freq,
    808 		    i < (sc->sc_pstate_count - 1) ? " " : "");
    809 	}
    810 
    811 	mutex_exit(&sc->sc_mtx);
    812 
    813 	node = *rnode;
    814 	node.sysctl_data = buf;
    815 
    816 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
    817 
    818 	if (err != 0 || newp == NULL)
    819 		return err;
    820 
    821 	return 0;
    822 }
    823 
    824