Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_md.c revision 1.35
      1 /* $NetBSD: acpi_cpu_md.c,v 1.35 2010/11/30 04:31:00 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.35 2010/11/30 04:31:00 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 /*
     52  * AMD C1E.
     53  */
     54 #define MSR_CMPHALT		0xc0010055
     55 
     56 #define MSR_CMPHALT_SMI		__BIT(27)
     57 #define MSR_CMPHALT_C1E		__BIT(28)
     58 #define MSR_CMPHALT_BMSTS	__BIT(29)
     59 
     60 /*
     61  * AMD families 10h and 11h.
     62  */
     63 #define MSR_10H_LIMIT		0xc0010061
     64 #define MSR_10H_CONTROL		0xc0010062
     65 #define MSR_10H_STATUS		0xc0010063
     66 #define MSR_10H_CONFIG		0xc0010064
     67 
     68 /*
     69  * AMD family 0Fh.
     70  */
     71 #define MSR_0FH_CONTROL		0xc0010041
     72 #define MSR_0FH_STATUS		0xc0010042
     73 
     74 #define MSR_0FH_STATUS_CFID	__BITS( 0,  5)
     75 #define MSR_0FH_STATUS_CVID	__BITS(32, 36)
     76 #define MSR_0FH_STATUS_PENDING	__BITS(31, 31)
     77 
     78 #define MSR_0FH_CONTROL_FID	__BITS( 0,  5)
     79 #define MSR_0FH_CONTROL_VID	__BITS( 8, 12)
     80 #define MSR_0FH_CONTROL_CHG	__BITS(16, 16)
     81 #define MSR_0FH_CONTROL_CNT	__BITS(32, 51)
     82 
     83 #define ACPI_0FH_STATUS_FID	__BITS( 0,  5)
     84 #define ACPI_0FH_STATUS_VID	__BITS( 6, 10)
     85 
     86 #define ACPI_0FH_CONTROL_FID	__BITS( 0,  5)
     87 #define ACPI_0FH_CONTROL_VID	__BITS( 6, 10)
     88 #define ACPI_0FH_CONTROL_VST	__BITS(11, 17)
     89 #define ACPI_0FH_CONTROL_MVS	__BITS(18, 19)
     90 #define ACPI_0FH_CONTROL_PLL	__BITS(20, 26)
     91 #define ACPI_0FH_CONTROL_RVO	__BITS(28, 29)
     92 #define ACPI_0FH_CONTROL_IRT	__BITS(30, 31)
     93 
     94 #define FID_TO_VCO_FID(fidd)	(((fid) < 8) ? (8 + ((fid) << 1)) : (fid))
     95 
     96 static char	  native_idle_text[16];
     97 void		(*native_idle)(void) = NULL;
     98 
     99 static int	 acpicpu_md_quirks_piix4(struct pci_attach_args *);
    100 static void	 acpicpu_md_pstate_status(void *, void *);
    101 static int	 acpicpu_md_pstate_fidvid_get(struct acpicpu_softc *,
    102                                               uint32_t *);
    103 static int	 acpicpu_md_pstate_fidvid_set(struct acpicpu_pstate *);
    104 static int	 acpicpu_md_pstate_fidvid_read(uint32_t *, uint32_t *);
    105 static void	 acpicpu_md_pstate_fidvid_write(uint32_t, uint32_t,
    106 					        uint32_t, uint32_t);
    107 static void	 acpicpu_md_tstate_status(void *, void *);
    108 static int	 acpicpu_md_pstate_sysctl_init(void);
    109 static int	 acpicpu_md_pstate_sysctl_get(SYSCTLFN_PROTO);
    110 static int	 acpicpu_md_pstate_sysctl_set(SYSCTLFN_PROTO);
    111 static int	 acpicpu_md_pstate_sysctl_all(SYSCTLFN_PROTO);
    112 
    113 extern uint32_t cpus_running;
    114 extern struct acpicpu_softc **acpicpu_sc;
    115 static bool acpicpu_pstate_status = false;
    116 static struct sysctllog *acpicpu_log = NULL;
    117 
    118 uint32_t
    119 acpicpu_md_cap(void)
    120 {
    121 	struct cpu_info *ci = curcpu();
    122 	uint32_t val = 0;
    123 
    124 	if (cpu_vendor != CPUVENDOR_IDT &&
    125 	    cpu_vendor != CPUVENDOR_INTEL)
    126 		return val;
    127 
    128 	/*
    129 	 * Basic SMP C-states (required for _CST).
    130 	 */
    131 	val |= ACPICPU_PDC_C_C1PT | ACPICPU_PDC_C_C2C3;
    132 
    133         /*
    134 	 * If MONITOR/MWAIT is available, announce
    135 	 * support for native instructions in all C-states.
    136 	 */
    137         if ((ci->ci_feat_val[1] & CPUID2_MONITOR) != 0)
    138 		val |= ACPICPU_PDC_C_C1_FFH | ACPICPU_PDC_C_C2C3_FFH;
    139 
    140 	/*
    141 	 * Set native P- and T-states, if available.
    142 	 */
    143         if ((ci->ci_feat_val[1] & CPUID2_EST) != 0)
    144 		val |= ACPICPU_PDC_P_FFH;
    145 
    146 	if ((ci->ci_feat_val[0] & CPUID_ACPI) != 0)
    147 		val |= ACPICPU_PDC_T_FFH;
    148 
    149 	return val;
    150 }
    151 
    152 uint32_t
    153 acpicpu_md_quirks(void)
    154 {
    155 	struct cpu_info *ci = curcpu();
    156 	struct pci_attach_args pa;
    157 	uint32_t family, val = 0;
    158 	uint32_t regs[4];
    159 
    160 	if (acpicpu_md_cpus_running() == 1)
    161 		val |= ACPICPU_FLAG_C_BM;
    162 
    163 	if ((ci->ci_feat_val[1] & CPUID2_MONITOR) != 0)
    164 		val |= ACPICPU_FLAG_C_FFH;
    165 
    166 	val |= ACPICPU_FLAG_C_APIC | ACPICPU_FLAG_C_TSC;
    167 
    168 	switch (cpu_vendor) {
    169 
    170 	case CPUVENDOR_IDT:
    171 
    172 		if ((ci->ci_feat_val[1] & CPUID2_EST) != 0)
    173 			val |= ACPICPU_FLAG_P_FFH;
    174 
    175 		if ((ci->ci_feat_val[0] & CPUID_ACPI) != 0)
    176 			val |= ACPICPU_FLAG_T_FFH;
    177 
    178 		break;
    179 
    180 	case CPUVENDOR_INTEL:
    181 
    182 		val |= ACPICPU_FLAG_C_BM | ACPICPU_FLAG_C_ARB;
    183 
    184 		if ((ci->ci_feat_val[1] & CPUID2_EST) != 0)
    185 			val |= ACPICPU_FLAG_P_FFH;
    186 
    187 		if ((ci->ci_feat_val[0] & CPUID_ACPI) != 0)
    188 			val |= ACPICPU_FLAG_T_FFH;
    189 
    190 		/*
    191 		 * Check whether MSR_APERF, MSR_MPERF, and Turbo
    192 		 * Boost are available. Also see if we might have
    193 		 * an invariant local APIC timer ("ARAT").
    194 		 */
    195 		if (cpuid_level >= 0x06) {
    196 
    197 			x86_cpuid(0x06, regs);
    198 
    199 			if ((regs[2] & CPUID_DSPM_HWF) != 0)
    200 				val |= ACPICPU_FLAG_P_HW;
    201 
    202 			if ((regs[0] & CPUID_DSPM_IDA) != 0)
    203 				val |= ACPICPU_FLAG_P_TURBO;
    204 
    205 			if ((regs[0] & CPUID_DSPM_ARAT) != 0)
    206 				val &= ~ACPICPU_FLAG_C_APIC;
    207 		}
    208 
    209 		/*
    210 		 * Detect whether TSC is invariant. If it is not,
    211 		 * we keep the flag to note that TSC will not run
    212 		 * at constant rate. Depending on the CPU, this may
    213 		 * affect P- and T-state changes, but especially
    214 		 * relevant are C-states; with variant TSC, states
    215 		 * larger than C1 may completely stop the counter.
    216 		 */
    217 		x86_cpuid(0x80000000, regs);
    218 
    219 		if (regs[0] >= 0x80000007) {
    220 
    221 			x86_cpuid(0x80000007, regs);
    222 
    223 			if ((regs[3] & __BIT(8)) != 0)
    224 				val &= ~ACPICPU_FLAG_C_TSC;
    225 		}
    226 
    227 		break;
    228 
    229 	case CPUVENDOR_AMD:
    230 
    231 		x86_cpuid(0x80000000, regs);
    232 
    233 		if (regs[0] < 0x80000007)
    234 			break;
    235 
    236 		x86_cpuid(0x80000007, regs);
    237 
    238 		family = CPUID2FAMILY(ci->ci_signature);
    239 
    240 		if (family == 0xf)
    241 			family += CPUID2EXTFAMILY(ci->ci_signature);
    242 
    243     		switch (family) {
    244 
    245 		case 0x0f:
    246 
    247 			if ((regs[3] & CPUID_APM_FID) == 0)
    248 				break;
    249 
    250 			if ((regs[3] & CPUID_APM_VID) == 0)
    251 				break;
    252 
    253 			val |= ACPICPU_FLAG_P_FFH | ACPICPU_FLAG_P_FIDVID;
    254 			break;
    255 
    256 		case 0x10:
    257 		case 0x11:
    258 
    259 			if ((regs[3] & CPUID_APM_TSC) != 0)
    260 				val &= ~ACPICPU_FLAG_C_TSC;
    261 
    262 			if ((regs[3] & CPUID_APM_HWP) != 0)
    263 				val |= ACPICPU_FLAG_P_FFH;
    264 
    265 			if ((regs[3] & CPUID_APM_CPB) != 0)
    266 				val |= ACPICPU_FLAG_P_TURBO;
    267 
    268 			val |= ACPICPU_FLAG_C_C1E;
    269 			break;
    270 		}
    271 
    272 		break;
    273 	}
    274 
    275 	/*
    276 	 * There are several erratums for PIIX4.
    277 	 */
    278 	if (pci_find_device(&pa, acpicpu_md_quirks_piix4) != 0)
    279 		val |= ACPICPU_FLAG_PIIX4;
    280 
    281 	return val;
    282 }
    283 
    284 static int
    285 acpicpu_md_quirks_piix4(struct pci_attach_args *pa)
    286 {
    287 
    288 	/*
    289 	 * XXX: The pci_find_device(9) function only
    290 	 *	deals with attached devices. Change this
    291 	 *	to use something like pci_device_foreach().
    292 	 */
    293 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    294 		return 0;
    295 
    296 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82371AB_ISA ||
    297 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82440MX_PMC)
    298 		return 1;
    299 
    300 	return 0;
    301 }
    302 
    303 void
    304 acpicpu_md_quirks_c1e(void)
    305 {
    306 	const uint64_t c1e = MSR_CMPHALT_SMI | MSR_CMPHALT_C1E;
    307 	uint64_t val;
    308 
    309 	val = rdmsr(MSR_CMPHALT);
    310 
    311 	if ((val & c1e) != 0)
    312 		wrmsr(MSR_CMPHALT, val & ~c1e);
    313 }
    314 
    315 uint32_t
    316 acpicpu_md_cpus_running(void)
    317 {
    318 
    319 	return popcount32(cpus_running);
    320 }
    321 
    322 int
    323 acpicpu_md_idle_start(struct acpicpu_softc *sc)
    324 {
    325 	const size_t size = sizeof(native_idle_text);
    326 	struct acpicpu_cstate *cs;
    327 	bool ipi = false;
    328 	int i;
    329 
    330 	x86_cpu_idle_get(&native_idle, native_idle_text, size);
    331 
    332 	for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
    333 
    334 		cs = &sc->sc_cstate[i];
    335 
    336 		if (cs->cs_method == ACPICPU_C_STATE_HALT) {
    337 			ipi = true;
    338 			break;
    339 		}
    340 	}
    341 
    342 	x86_cpu_idle_set(acpicpu_cstate_idle, "acpi", ipi);
    343 
    344 	return 0;
    345 }
    346 
    347 int
    348 acpicpu_md_idle_stop(void)
    349 {
    350 	uint64_t xc;
    351 	bool ipi;
    352 
    353 	ipi = (native_idle != x86_cpu_idle_halt) ? false : true;
    354 	x86_cpu_idle_set(native_idle, native_idle_text, ipi);
    355 
    356 	/*
    357 	 * Run a cross-call to ensure that all CPUs are
    358 	 * out from the ACPI idle-loop before detachment.
    359 	 */
    360 	xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    361 	xc_wait(xc);
    362 
    363 	return 0;
    364 }
    365 
    366 /*
    367  * Called with interrupts disabled.
    368  * Caller should enable interrupts after return.
    369  */
    370 void
    371 acpicpu_md_idle_enter(int method, int state)
    372 {
    373 	struct cpu_info *ci = curcpu();
    374 
    375 	switch (method) {
    376 
    377 	case ACPICPU_C_STATE_FFH:
    378 
    379 		x86_enable_intr();
    380 		x86_monitor(&ci->ci_want_resched, 0, 0);
    381 
    382 		if (__predict_false(ci->ci_want_resched != 0))
    383 			return;
    384 
    385 		x86_mwait((state - 1) << 4, 0);
    386 		break;
    387 
    388 	case ACPICPU_C_STATE_HALT:
    389 
    390 		if (__predict_false(ci->ci_want_resched != 0))
    391 			return;
    392 
    393 		x86_stihlt();
    394 		break;
    395 	}
    396 }
    397 
    398 int
    399 acpicpu_md_pstate_start(void)
    400 {
    401 	const uint64_t est = __BIT(16);
    402 	uint64_t val;
    403 
    404 	switch (cpu_vendor) {
    405 
    406 	case CPUVENDOR_IDT:
    407 	case CPUVENDOR_INTEL:
    408 
    409 		val = rdmsr(MSR_MISC_ENABLE);
    410 
    411 		if ((val & est) == 0) {
    412 
    413 			val |= est;
    414 
    415 			wrmsr(MSR_MISC_ENABLE, val);
    416 			val = rdmsr(MSR_MISC_ENABLE);
    417 
    418 			if ((val & est) == 0)
    419 				return ENOTTY;
    420 		}
    421 	}
    422 
    423 	return acpicpu_md_pstate_sysctl_init();
    424 }
    425 
    426 int
    427 acpicpu_md_pstate_stop(void)
    428 {
    429 
    430 	if (acpicpu_log != NULL)
    431 		sysctl_teardown(&acpicpu_log);
    432 
    433 	return 0;
    434 }
    435 
    436 int
    437 acpicpu_md_pstate_pss(struct acpicpu_softc *sc)
    438 {
    439 	struct acpicpu_pstate *ps, msr;
    440 	struct cpu_info *ci = curcpu();
    441 	uint32_t family, i = 0;
    442 
    443 	(void)memset(&msr, 0, sizeof(struct acpicpu_pstate));
    444 
    445 	switch (cpu_vendor) {
    446 
    447 	case CPUVENDOR_IDT:
    448 	case CPUVENDOR_INTEL:
    449 
    450 		/*
    451 		 * If the so-called Turbo Boost is present,
    452 		 * the P0-state is always the "turbo state".
    453 		 *
    454 		 * For discussion, see:
    455 		 *
    456 		 *	Intel Corporation: Intel Turbo Boost Technology
    457 		 *	in Intel Core(tm) Microarchitectures (Nehalem)
    458 		 *	Based Processors. White Paper, November 2008.
    459 		 */
    460 		if ((sc->sc_flags & ACPICPU_FLAG_P_TURBO) != 0)
    461 			sc->sc_pstate[0].ps_flags |= ACPICPU_FLAG_P_TURBO;
    462 
    463 		msr.ps_control_addr = MSR_PERF_CTL;
    464 		msr.ps_control_mask = __BITS(0, 15);
    465 
    466 		msr.ps_status_addr  = MSR_PERF_STATUS;
    467 		msr.ps_status_mask  = __BITS(0, 15);
    468 		break;
    469 
    470 	case CPUVENDOR_AMD:
    471 
    472 		if ((sc->sc_flags & ACPICPU_FLAG_P_FIDVID) != 0)
    473 			msr.ps_flags |= ACPICPU_FLAG_P_FIDVID;
    474 
    475 		family = CPUID2FAMILY(ci->ci_signature);
    476 
    477 		if (family == 0xf)
    478 			family += CPUID2EXTFAMILY(ci->ci_signature);
    479 
    480 		switch (family) {
    481 
    482 		case 0x0f:
    483 			msr.ps_control_addr = MSR_0FH_CONTROL;
    484 			msr.ps_status_addr  = MSR_0FH_STATUS;
    485 			break;
    486 
    487 		case 0x10:
    488 		case 0x11:
    489 			msr.ps_control_addr = MSR_10H_CONTROL;
    490 			msr.ps_control_mask = __BITS(0, 2);
    491 
    492 			msr.ps_status_addr  = MSR_10H_STATUS;
    493 			msr.ps_status_mask  = __BITS(0, 2);
    494 			break;
    495 
    496 		default:
    497 
    498 			if ((sc->sc_flags & ACPICPU_FLAG_P_XPSS) == 0)
    499 				return EOPNOTSUPP;
    500 		}
    501 
    502 		break;
    503 
    504 	default:
    505 		return ENODEV;
    506 	}
    507 
    508 	/*
    509 	 * Fill the P-state structures with MSR addresses that are
    510 	 * known to be correct. If we do not know the addresses,
    511 	 * leave the values intact. If a vendor uses XPSS, we do
    512 	 * not necessary need to do anything to support new CPUs.
    513 	 */
    514 	while (i < sc->sc_pstate_count) {
    515 
    516 		ps = &sc->sc_pstate[i];
    517 
    518 		if (msr.ps_flags != 0)
    519 			ps->ps_flags |= msr.ps_flags;
    520 
    521 		if (msr.ps_status_addr != 0)
    522 			ps->ps_status_addr = msr.ps_status_addr;
    523 
    524 		if (msr.ps_status_mask != 0)
    525 			ps->ps_status_mask = msr.ps_status_mask;
    526 
    527 		if (msr.ps_control_addr != 0)
    528 			ps->ps_control_addr = msr.ps_control_addr;
    529 
    530 		if (msr.ps_control_mask != 0)
    531 			ps->ps_control_mask = msr.ps_control_mask;
    532 
    533 		i++;
    534 	}
    535 
    536 	return 0;
    537 }
    538 
    539 int
    540 acpicpu_md_pstate_get(struct acpicpu_softc *sc, uint32_t *freq)
    541 {
    542 	struct acpicpu_pstate *ps = NULL;
    543 	uint64_t val;
    544 	uint32_t i;
    545 
    546 	if ((sc->sc_flags & ACPICPU_FLAG_P_FIDVID) != 0)
    547 		return acpicpu_md_pstate_fidvid_get(sc, freq);
    548 
    549 	for (i = 0; i < sc->sc_pstate_count; i++) {
    550 
    551 		ps = &sc->sc_pstate[i];
    552 
    553 		if (__predict_true(ps->ps_freq != 0))
    554 			break;
    555 	}
    556 
    557 	if (__predict_false(ps == NULL))
    558 		return ENODEV;
    559 
    560 	if (__predict_false(ps->ps_status_addr == 0))
    561 		return EINVAL;
    562 
    563 	val = rdmsr(ps->ps_status_addr);
    564 
    565 	if (__predict_true(ps->ps_status_mask != 0))
    566 		val = val & ps->ps_status_mask;
    567 
    568 	for (i = 0; i < sc->sc_pstate_count; i++) {
    569 
    570 		ps = &sc->sc_pstate[i];
    571 
    572 		if (__predict_false(ps->ps_freq == 0))
    573 			continue;
    574 
    575 		if (val == ps->ps_status) {
    576 			*freq = ps->ps_freq;
    577 			return 0;
    578 		}
    579 	}
    580 
    581 	return EIO;
    582 }
    583 
    584 int
    585 acpicpu_md_pstate_set(struct acpicpu_pstate *ps)
    586 {
    587 	struct msr_rw_info msr;
    588 	uint64_t xc;
    589 	int rv = 0;
    590 
    591 	if ((ps->ps_flags & ACPICPU_FLAG_P_FIDVID) != 0)
    592 		return acpicpu_md_pstate_fidvid_set(ps);
    593 
    594 	msr.msr_read  = false;
    595 	msr.msr_type  = ps->ps_control_addr;
    596 	msr.msr_value = ps->ps_control;
    597 
    598 	if (__predict_true(ps->ps_control_mask != 0)) {
    599 		msr.msr_mask = ps->ps_control_mask;
    600 		msr.msr_read = true;
    601 	}
    602 
    603 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
    604 	xc_wait(xc);
    605 
    606 	if (acpicpu_pstate_status != false) {
    607 		DELAY(ps->ps_latency);
    608 		return 0;
    609 	}
    610 
    611 	xc = xc_broadcast(0, (xcfunc_t)acpicpu_md_pstate_status, ps, &rv);
    612 	xc_wait(xc);
    613 
    614 	return rv;
    615 }
    616 
    617 static void
    618 acpicpu_md_pstate_status(void *arg1, void *arg2)
    619 {
    620 	struct acpicpu_pstate *ps = arg1;
    621 	uint64_t val;
    622 	int i;
    623 
    624 	for (i = val = 0; i < ACPICPU_P_STATE_RETRY; i++) {
    625 
    626 		val = rdmsr(ps->ps_status_addr);
    627 
    628 		if (__predict_true(ps->ps_status_mask != 0))
    629 			val = val & ps->ps_status_mask;
    630 
    631 		if (val == ps->ps_status)
    632 			return;
    633 
    634 		DELAY(ps->ps_latency);
    635 	}
    636 
    637 	*(uintptr_t *)arg2 = EAGAIN;
    638 }
    639 
    640 static int
    641 acpicpu_md_pstate_fidvid_get(struct acpicpu_softc *sc, uint32_t *freq)
    642 {
    643 	struct acpicpu_pstate *ps;
    644 	uint32_t fid, i, vid;
    645 	uint32_t cfid, cvid;
    646 	int rv;
    647 
    648 	/*
    649 	 * AMD family 0Fh needs special treatment.
    650 	 * While it wants to use ACPI, it does not
    651 	 * comply with the ACPI specifications.
    652 	 */
    653 	rv = acpicpu_md_pstate_fidvid_read(&cfid, &cvid);
    654 
    655 	if (rv != 0)
    656 		return rv;
    657 
    658 	for (i = 0; i < sc->sc_pstate_count; i++) {
    659 
    660 		ps = &sc->sc_pstate[i];
    661 
    662 		if (__predict_false(ps->ps_freq == 0))
    663 			continue;
    664 
    665 		fid = __SHIFTOUT(ps->ps_status, ACPI_0FH_STATUS_FID);
    666 		vid = __SHIFTOUT(ps->ps_status, ACPI_0FH_STATUS_VID);
    667 
    668 		if (cfid == fid && cvid == vid) {
    669 			*freq = ps->ps_freq;
    670 			return 0;
    671 		}
    672 	}
    673 
    674 	return EIO;
    675 }
    676 
    677 static int
    678 acpicpu_md_pstate_fidvid_set(struct acpicpu_pstate *ps)
    679 {
    680 	const uint64_t ctrl = ps->ps_control;
    681 	uint32_t cfid, cvid, fid, i, irt;
    682 	uint32_t pll, vco_cfid, vco_fid;
    683 	uint32_t val, vid, vst;
    684 	int rv;
    685 
    686 	rv = acpicpu_md_pstate_fidvid_read(&cfid, &cvid);
    687 
    688 	if (rv != 0)
    689 		return rv;
    690 
    691 	fid = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_FID);
    692 	vid = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_VID);
    693 	irt = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_IRT);
    694 	vst = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_VST);
    695 	pll = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_PLL);
    696 
    697 	vst = vst * 20;
    698 	pll = pll * 1000 / 5;
    699 	irt = 10 * __BIT(irt);
    700 
    701 	/*
    702 	 * Phase 1.
    703 	 */
    704 	while (cvid > vid) {
    705 
    706 		val = 1 << __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_MVS);
    707 		val = (val > cvid) ? 0 : cvid - val;
    708 
    709 		acpicpu_md_pstate_fidvid_write(cfid, val, 1, vst);
    710 		rv = acpicpu_md_pstate_fidvid_read(NULL, &cvid);
    711 
    712 		if (rv != 0)
    713 			return rv;
    714 	}
    715 
    716 	i = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_RVO);
    717 
    718 	for (; i > 0 && cvid > 0; --i) {
    719 
    720 		acpicpu_md_pstate_fidvid_write(cfid, cvid - 1, 1, vst);
    721 		rv = acpicpu_md_pstate_fidvid_read(NULL, &cvid);
    722 
    723 		if (rv != 0)
    724 			return rv;
    725 	}
    726 
    727 	/*
    728 	 * Phase 2.
    729 	 */
    730 	if (cfid != fid) {
    731 
    732 		vco_fid  = FID_TO_VCO_FID(fid);
    733 		vco_cfid = FID_TO_VCO_FID(cfid);
    734 
    735 		while (abs(vco_fid - vco_cfid) > 2) {
    736 
    737 			if (fid <= cfid)
    738 				val = cfid - 2;
    739 			else {
    740 				val = (cfid > 6) ? cfid + 2 :
    741 				    FID_TO_VCO_FID(cfid) + 2;
    742 			}
    743 
    744 			acpicpu_md_pstate_fidvid_write(val, cvid, pll, irt);
    745 			rv = acpicpu_md_pstate_fidvid_read(&cfid, NULL);
    746 
    747 			if (rv != 0)
    748 				return rv;
    749 
    750 			vco_cfid = FID_TO_VCO_FID(cfid);
    751 		}
    752 
    753 		acpicpu_md_pstate_fidvid_write(fid, cvid, pll, irt);
    754 		rv = acpicpu_md_pstate_fidvid_read(&cfid, NULL);
    755 
    756 		if (rv != 0)
    757 			return rv;
    758 	}
    759 
    760 	/*
    761 	 * Phase 3.
    762 	 */
    763 	if (cvid != vid) {
    764 
    765 		acpicpu_md_pstate_fidvid_write(cfid, vid, 1, vst);
    766 		rv = acpicpu_md_pstate_fidvid_read(NULL, &cvid);
    767 
    768 		if (rv != 0)
    769 			return rv;
    770 	}
    771 
    772 	if (cfid != fid || cvid != vid)
    773 		return EIO;
    774 
    775 	return 0;
    776 }
    777 
    778 static int
    779 acpicpu_md_pstate_fidvid_read(uint32_t *cfid, uint32_t *cvid)
    780 {
    781 	int i = ACPICPU_P_STATE_RETRY * 100;
    782 	uint64_t val;
    783 
    784 	do {
    785 		val = rdmsr(MSR_0FH_STATUS);
    786 
    787 	} while (__SHIFTOUT(val, MSR_0FH_STATUS_PENDING) != 0 && --i >= 0);
    788 
    789 	if (i == 0)
    790 		return EAGAIN;
    791 
    792 	if (cfid != NULL)
    793 		*cfid = __SHIFTOUT(val, MSR_0FH_STATUS_CFID);
    794 
    795 	if (cvid != NULL)
    796 		*cvid = __SHIFTOUT(val, MSR_0FH_STATUS_CVID);
    797 
    798 	return 0;
    799 }
    800 
    801 static void
    802 acpicpu_md_pstate_fidvid_write(uint32_t fid,
    803     uint32_t vid, uint32_t cnt, uint32_t tmo)
    804 {
    805 	struct msr_rw_info msr;
    806 	uint64_t xc;
    807 
    808 	msr.msr_read  = false;
    809 	msr.msr_type  = MSR_0FH_CONTROL;
    810 	msr.msr_value = 0;
    811 
    812 	msr.msr_value |= __SHIFTIN(fid, MSR_0FH_CONTROL_FID);
    813 	msr.msr_value |= __SHIFTIN(vid, MSR_0FH_CONTROL_VID);
    814 	msr.msr_value |= __SHIFTIN(cnt, MSR_0FH_CONTROL_CNT);
    815 	msr.msr_value |= __SHIFTIN(0x1, MSR_0FH_CONTROL_CHG);
    816 
    817 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
    818 	xc_wait(xc);
    819 
    820 	DELAY(tmo);
    821 }
    822 
    823 int
    824 acpicpu_md_tstate_get(struct acpicpu_softc *sc, uint32_t *percent)
    825 {
    826 	struct acpicpu_tstate *ts;
    827 	uint64_t val;
    828 	uint32_t i;
    829 
    830 	val = rdmsr(MSR_THERM_CONTROL);
    831 
    832 	for (i = 0; i < sc->sc_tstate_count; i++) {
    833 
    834 		ts = &sc->sc_tstate[i];
    835 
    836 		if (ts->ts_percent == 0)
    837 			continue;
    838 
    839 		if (val == ts->ts_status) {
    840 			*percent = ts->ts_percent;
    841 			return 0;
    842 		}
    843 	}
    844 
    845 	return EIO;
    846 }
    847 
    848 int
    849 acpicpu_md_tstate_set(struct acpicpu_tstate *ts)
    850 {
    851 	struct msr_rw_info msr;
    852 	uint64_t xc;
    853 	int rv = 0;
    854 
    855 	msr.msr_read  = true;
    856 	msr.msr_type  = MSR_THERM_CONTROL;
    857 	msr.msr_value = ts->ts_control;
    858 	msr.msr_mask = __BITS(1, 4);
    859 
    860 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
    861 	xc_wait(xc);
    862 
    863 	if (ts->ts_status == 0) {
    864 		DELAY(ts->ts_latency);
    865 		return 0;
    866 	}
    867 
    868 	xc = xc_broadcast(0, (xcfunc_t)acpicpu_md_tstate_status, ts, &rv);
    869 	xc_wait(xc);
    870 
    871 	return rv;
    872 }
    873 
    874 static void
    875 acpicpu_md_tstate_status(void *arg1, void *arg2)
    876 {
    877 	struct acpicpu_tstate *ts = arg1;
    878 	uint64_t val;
    879 	int i;
    880 
    881 	for (i = val = 0; i < ACPICPU_T_STATE_RETRY; i++) {
    882 
    883 		val = rdmsr(MSR_THERM_CONTROL);
    884 
    885 		if (val == ts->ts_status)
    886 			return;
    887 
    888 		DELAY(ts->ts_latency);
    889 	}
    890 
    891 	*(uintptr_t *)arg2 = EAGAIN;
    892 }
    893 
    894 /*
    895  * A kludge for backwards compatibility.
    896  */
    897 static int
    898 acpicpu_md_pstate_sysctl_init(void)
    899 {
    900 	const struct sysctlnode	*fnode, *mnode, *rnode;
    901 	const char *str;
    902 	int rv;
    903 
    904 	switch (cpu_vendor) {
    905 
    906 	case CPUVENDOR_IDT:
    907 	case CPUVENDOR_INTEL:
    908 		str = "est";
    909 		break;
    910 
    911 	case CPUVENDOR_AMD:
    912 		str = "powernow";
    913 		break;
    914 
    915 	default:
    916 		return ENODEV;
    917 	}
    918 
    919 
    920 	rv = sysctl_createv(&acpicpu_log, 0, NULL, &rnode,
    921 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    922 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
    923 
    924 	if (rv != 0)
    925 		goto fail;
    926 
    927 	rv = sysctl_createv(&acpicpu_log, 0, &rnode, &mnode,
    928 	    0, CTLTYPE_NODE, str, NULL,
    929 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    930 
    931 	if (rv != 0)
    932 		goto fail;
    933 
    934 	rv = sysctl_createv(&acpicpu_log, 0, &mnode, &fnode,
    935 	    0, CTLTYPE_NODE, "frequency", NULL,
    936 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    937 
    938 	if (rv != 0)
    939 		goto fail;
    940 
    941 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
    942 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
    943 	    acpicpu_md_pstate_sysctl_set, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    944 
    945 	if (rv != 0)
    946 		goto fail;
    947 
    948 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
    949 	    CTLFLAG_READONLY, CTLTYPE_INT, "current", NULL,
    950 	    acpicpu_md_pstate_sysctl_get, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    951 
    952 	if (rv != 0)
    953 		goto fail;
    954 
    955 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
    956 	    CTLFLAG_READONLY, CTLTYPE_STRING, "available", NULL,
    957 	    acpicpu_md_pstate_sysctl_all, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    958 
    959 	if (rv != 0)
    960 		goto fail;
    961 
    962 	return 0;
    963 
    964 fail:
    965 	if (acpicpu_log != NULL) {
    966 		sysctl_teardown(&acpicpu_log);
    967 		acpicpu_log = NULL;
    968 	}
    969 
    970 	return rv;
    971 }
    972 
    973 static int
    974 acpicpu_md_pstate_sysctl_get(SYSCTLFN_ARGS)
    975 {
    976 	struct cpu_info *ci = curcpu();
    977 	struct acpicpu_softc *sc;
    978 	struct sysctlnode node;
    979 	uint32_t freq;
    980 	int err;
    981 
    982 	sc = acpicpu_sc[ci->ci_acpiid];
    983 
    984 	if (sc == NULL)
    985 		return ENXIO;
    986 
    987 	err = acpicpu_pstate_get(sc, &freq);
    988 
    989 	if (err != 0)
    990 		return err;
    991 
    992 	node = *rnode;
    993 	node.sysctl_data = &freq;
    994 
    995 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
    996 
    997 	if (err != 0 || newp == NULL)
    998 		return err;
    999 
   1000 	return 0;
   1001 }
   1002 
   1003 static int
   1004 acpicpu_md_pstate_sysctl_set(SYSCTLFN_ARGS)
   1005 {
   1006 	struct cpu_info *ci = curcpu();
   1007 	struct acpicpu_softc *sc;
   1008 	struct sysctlnode node;
   1009 	uint32_t freq;
   1010 	int err;
   1011 
   1012 	sc = acpicpu_sc[ci->ci_acpiid];
   1013 
   1014 	if (sc == NULL)
   1015 		return ENXIO;
   1016 
   1017 	err = acpicpu_pstate_get(sc, &freq);
   1018 
   1019 	if (err != 0)
   1020 		return err;
   1021 
   1022 	node = *rnode;
   1023 	node.sysctl_data = &freq;
   1024 
   1025 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1026 
   1027 	if (err != 0 || newp == NULL)
   1028 		return err;
   1029 
   1030 	err = acpicpu_pstate_set(sc, freq);
   1031 
   1032 	if (err != 0)
   1033 		return err;
   1034 
   1035 	return 0;
   1036 }
   1037 
   1038 static int
   1039 acpicpu_md_pstate_sysctl_all(SYSCTLFN_ARGS)
   1040 {
   1041 	struct cpu_info *ci = curcpu();
   1042 	struct acpicpu_softc *sc;
   1043 	struct sysctlnode node;
   1044 	char buf[1024];
   1045 	size_t len;
   1046 	uint32_t i;
   1047 	int err;
   1048 
   1049 	sc = acpicpu_sc[ci->ci_acpiid];
   1050 
   1051 	if (sc == NULL)
   1052 		return ENXIO;
   1053 
   1054 	(void)memset(&buf, 0, sizeof(buf));
   1055 
   1056 	mutex_enter(&sc->sc_mtx);
   1057 
   1058 	for (len = 0, i = sc->sc_pstate_max; i < sc->sc_pstate_count; i++) {
   1059 
   1060 		if (sc->sc_pstate[i].ps_freq == 0)
   1061 			continue;
   1062 
   1063 		len += snprintf(buf + len, sizeof(buf) - len, "%u%s",
   1064 		    sc->sc_pstate[i].ps_freq,
   1065 		    i < (sc->sc_pstate_count - 1) ? " " : "");
   1066 	}
   1067 
   1068 	mutex_exit(&sc->sc_mtx);
   1069 
   1070 	node = *rnode;
   1071 	node.sysctl_data = buf;
   1072 
   1073 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1074 
   1075 	if (err != 0 || newp == NULL)
   1076 		return err;
   1077 
   1078 	return 0;
   1079 }
   1080 
   1081