Home | History | Annotate | Line # | Download | only in acpi
acpi_cpu_md.c revision 1.48
      1 /* $NetBSD: acpi_cpu_md.c,v 1.48 2011/02/27 18:32:54 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010, 2011 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.48 2011/02/27 18:32:54 jruoho Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/kcore.h>
     36 #include <sys/sysctl.h>
     37 #include <sys/xcall.h>
     38 
     39 #include <x86/cpu.h>
     40 #include <x86/cpufunc.h>
     41 #include <x86/cputypes.h>
     42 #include <x86/cpuvar.h>
     43 #include <x86/cpu_msr.h>
     44 #include <x86/machdep.h>
     45 
     46 #include <dev/acpi/acpica.h>
     47 #include <dev/acpi/acpi_cpu.h>
     48 
     49 #include <dev/pci/pcivar.h>
     50 #include <dev/pci/pcidevs.h>
     51 
     52 #include <machine/acpi_machdep.h>
     53 
     54 /*
     55  * AMD C1E.
     56  */
     57 #define MSR_CMPHALT		0xc0010055
     58 
     59 #define MSR_CMPHALT_SMI		__BIT(27)
     60 #define MSR_CMPHALT_C1E		__BIT(28)
     61 #define MSR_CMPHALT_BMSTS	__BIT(29)
     62 
     63 /*
     64  * AMD families 10h, 11h, and 14h
     65  */
     66 #define MSR_10H_LIMIT		0xc0010061
     67 #define MSR_10H_CONTROL		0xc0010062
     68 #define MSR_10H_STATUS		0xc0010063
     69 #define MSR_10H_CONFIG		0xc0010064
     70 
     71 /*
     72  * AMD family 0Fh.
     73  */
     74 #define MSR_0FH_CONTROL		0xc0010041
     75 #define MSR_0FH_STATUS		0xc0010042
     76 
     77 #define MSR_0FH_STATUS_CFID	__BITS( 0,  5)
     78 #define MSR_0FH_STATUS_CVID	__BITS(32, 36)
     79 #define MSR_0FH_STATUS_PENDING	__BITS(31, 31)
     80 
     81 #define MSR_0FH_CONTROL_FID	__BITS( 0,  5)
     82 #define MSR_0FH_CONTROL_VID	__BITS( 8, 12)
     83 #define MSR_0FH_CONTROL_CHG	__BITS(16, 16)
     84 #define MSR_0FH_CONTROL_CNT	__BITS(32, 51)
     85 
     86 #define ACPI_0FH_STATUS_FID	__BITS( 0,  5)
     87 #define ACPI_0FH_STATUS_VID	__BITS( 6, 10)
     88 
     89 #define ACPI_0FH_CONTROL_FID	__BITS( 0,  5)
     90 #define ACPI_0FH_CONTROL_VID	__BITS( 6, 10)
     91 #define ACPI_0FH_CONTROL_VST	__BITS(11, 17)
     92 #define ACPI_0FH_CONTROL_MVS	__BITS(18, 19)
     93 #define ACPI_0FH_CONTROL_PLL	__BITS(20, 26)
     94 #define ACPI_0FH_CONTROL_RVO	__BITS(28, 29)
     95 #define ACPI_0FH_CONTROL_IRT	__BITS(30, 31)
     96 
     97 #define FID_TO_VCO_FID(fidd)	(((fid) < 8) ? (8 + ((fid) << 1)) : (fid))
     98 
     99 static char	  native_idle_text[16];
    100 void		(*native_idle)(void) = NULL;
    101 
    102 static int	 acpicpu_md_quirk_piix4(struct pci_attach_args *);
    103 static void	 acpicpu_md_pstate_percent_reset(struct acpicpu_softc *);
    104 static void	 acpicpu_md_pstate_percent_status(void *, void *);
    105 static void	 acpicpu_md_pstate_status(void *, void *);
    106 static int	 acpicpu_md_pstate_fidvid_get(struct acpicpu_softc *,
    107                                               uint32_t *);
    108 static int	 acpicpu_md_pstate_fidvid_set(struct acpicpu_pstate *);
    109 static int	 acpicpu_md_pstate_fidvid_read(uint32_t *, uint32_t *);
    110 static void	 acpicpu_md_pstate_fidvid_write(uint32_t, uint32_t,
    111 					        uint32_t, uint32_t);
    112 static void	 acpicpu_md_tstate_status(void *, void *);
    113 static int	 acpicpu_md_pstate_sysctl_init(void);
    114 static int	 acpicpu_md_pstate_sysctl_get(SYSCTLFN_PROTO);
    115 static int	 acpicpu_md_pstate_sysctl_set(SYSCTLFN_PROTO);
    116 static int	 acpicpu_md_pstate_sysctl_all(SYSCTLFN_PROTO);
    117 
    118 extern struct acpicpu_softc **acpicpu_sc;
    119 static bool acpicpu_pstate_status = false;
    120 static struct sysctllog *acpicpu_log = NULL;
    121 
    122 struct cpu_info *
    123 acpicpu_md_match(device_t parent, cfdata_t match, void *aux)
    124 {
    125 	struct cpufeature_attach_args *cfaa = aux;
    126 
    127 	if (strcmp(cfaa->name, "frequency") != 0)
    128 		return NULL;
    129 
    130 	return cfaa->ci;
    131 }
    132 
    133 struct cpu_info *
    134 acpicpu_md_attach(device_t parent, device_t self, void *aux)
    135 {
    136 	struct cpufeature_attach_args *cfaa = aux;
    137 
    138 	return cfaa->ci;
    139 }
    140 
    141 uint32_t
    142 acpicpu_md_cap(void)
    143 {
    144 	struct cpu_info *ci = curcpu();
    145 	uint32_t regs[4];
    146 	uint32_t val = 0;
    147 
    148 	if (cpu_vendor != CPUVENDOR_IDT &&
    149 	    cpu_vendor != CPUVENDOR_INTEL)
    150 		return val;
    151 
    152 	/*
    153 	 * Basic SMP C-states (required for e.g. _CST).
    154 	 */
    155 	val |= ACPICPU_PDC_C_C1PT | ACPICPU_PDC_C_C2C3;
    156 
    157 	/*
    158 	 * Claim to support dependency coordination.
    159 	 */
    160 	val |= ACPICPU_PDC_P_SW | ACPICPU_PDC_C_SW | ACPICPU_PDC_T_SW;
    161 
    162         /*
    163 	 * If MONITOR/MWAIT is available, announce
    164 	 * support for native instructions in all C-states.
    165 	 */
    166         if ((ci->ci_feat_val[1] & CPUID2_MONITOR) != 0)
    167 		val |= ACPICPU_PDC_C_C1_FFH | ACPICPU_PDC_C_C2C3_FFH;
    168 
    169 	/*
    170 	 * Set native P- and T-states, if available.
    171 	 */
    172         if ((ci->ci_feat_val[1] & CPUID2_EST) != 0)
    173 		val |= ACPICPU_PDC_P_FFH;
    174 
    175 	if ((ci->ci_feat_val[0] & CPUID_ACPI) != 0)
    176 		val |= ACPICPU_PDC_T_FFH;
    177 
    178 	/*
    179 	 * Declare support for APERF and MPERF.
    180 	 */
    181 	if (cpuid_level >= 0x06) {
    182 
    183 		x86_cpuid(0x00000006, regs);
    184 
    185 		if ((regs[2] & CPUID_DSPM_HWF) != 0)
    186 			val |= ACPICPU_PDC_P_HW;
    187 	}
    188 
    189 	return val;
    190 }
    191 
    192 uint32_t
    193 acpicpu_md_flags(void)
    194 {
    195 	struct cpu_info *ci = curcpu();
    196 	struct pci_attach_args pa;
    197 	uint32_t family, val = 0;
    198 	uint32_t regs[4];
    199 
    200 	if (acpi_md_ncpus() == 1)
    201 		val |= ACPICPU_FLAG_C_BM;
    202 
    203 	if ((ci->ci_feat_val[1] & CPUID2_MONITOR) != 0)
    204 		val |= ACPICPU_FLAG_C_FFH;
    205 
    206 	/*
    207 	 * By default, assume that the local APIC timer
    208 	 * as well as TSC are stalled during C3 sleep.
    209 	 */
    210 	val |= ACPICPU_FLAG_C_APIC | ACPICPU_FLAG_C_TSC;
    211 
    212 	switch (cpu_vendor) {
    213 
    214 	case CPUVENDOR_IDT:
    215 
    216 		if ((ci->ci_feat_val[1] & CPUID2_EST) != 0)
    217 			val |= ACPICPU_FLAG_P_FFH;
    218 
    219 		if ((ci->ci_feat_val[0] & CPUID_ACPI) != 0)
    220 			val |= ACPICPU_FLAG_T_FFH;
    221 
    222 		break;
    223 
    224 	case CPUVENDOR_INTEL:
    225 
    226 		/*
    227 		 * Bus master control and arbitration should be
    228 		 * available on all supported Intel CPUs (to be
    229 		 * sure, this is double-checked later from the
    230 		 * firmware data). These flags imply that it is
    231 		 * not necessary to flush caches before C3 state.
    232 		 */
    233 		val |= ACPICPU_FLAG_C_BM | ACPICPU_FLAG_C_ARB;
    234 
    235 		/*
    236 		 * Check if we can use "native", MSR-based,
    237 		 * access. If not, we have to resort to I/O.
    238 		 */
    239 		if ((ci->ci_feat_val[1] & CPUID2_EST) != 0)
    240 			val |= ACPICPU_FLAG_P_FFH;
    241 
    242 		if ((ci->ci_feat_val[0] & CPUID_ACPI) != 0)
    243 			val |= ACPICPU_FLAG_T_FFH;
    244 
    245 		/*
    246 		 * Check whether MSR_APERF, MSR_MPERF, and Turbo
    247 		 * Boost are available. Also see if we might have
    248 		 * an invariant local APIC timer ("ARAT").
    249 		 */
    250 		if (cpuid_level >= 0x06) {
    251 
    252 			x86_cpuid(0x00000006, regs);
    253 
    254 			if ((regs[2] & CPUID_DSPM_HWF) != 0)
    255 				val |= ACPICPU_FLAG_P_HW;
    256 
    257 			if ((regs[0] & CPUID_DSPM_IDA) != 0)
    258 				val |= ACPICPU_FLAG_P_TURBO;
    259 
    260 			if ((regs[0] & CPUID_DSPM_ARAT) != 0)
    261 				val &= ~ACPICPU_FLAG_C_APIC;
    262 		}
    263 
    264 		/*
    265 		 * Detect whether TSC is invariant. If it is not,
    266 		 * we keep the flag to note that TSC will not run
    267 		 * at constant rate. Depending on the CPU, this may
    268 		 * affect P- and T-state changes, but especially
    269 		 * relevant are C-states; with variant TSC, states
    270 		 * larger than C1 may completely stop the counter.
    271 		 */
    272 		x86_cpuid(0x80000000, regs);
    273 
    274 		if (regs[0] >= 0x80000007) {
    275 
    276 			x86_cpuid(0x80000007, regs);
    277 
    278 			if ((regs[3] & __BIT(8)) != 0)
    279 				val &= ~ACPICPU_FLAG_C_TSC;
    280 		}
    281 
    282 		break;
    283 
    284 	case CPUVENDOR_AMD:
    285 
    286 		x86_cpuid(0x80000000, regs);
    287 
    288 		if (regs[0] < 0x80000007)
    289 			break;
    290 
    291 		x86_cpuid(0x80000007, regs);
    292 
    293 		family = CPUID2FAMILY(ci->ci_signature);
    294 
    295 		if (family == 0xf)
    296 			family += CPUID2EXTFAMILY(ci->ci_signature);
    297 
    298     		switch (family) {
    299 
    300 		case 0x0f:
    301 
    302 			/*
    303 			 * Evaluate support for the "FID/VID
    304 			 * algorithm" also used by powernow(4).
    305 			 */
    306 			if ((regs[3] & CPUID_APM_FID) == 0)
    307 				break;
    308 
    309 			if ((regs[3] & CPUID_APM_VID) == 0)
    310 				break;
    311 
    312 			val |= ACPICPU_FLAG_P_FFH | ACPICPU_FLAG_P_FIDVID;
    313 			break;
    314 
    315 		case 0x10:
    316 		case 0x11:
    317 			val |= ACPICPU_FLAG_C_C1E;
    318 			/* FALLTHROUGH */
    319 
    320 		case 0x14: /* AMD Fusion */
    321 
    322 			/*
    323 			 * Like with Intel, detect invariant TSC,
    324 			 * MSR-based P-states, and AMD's "turbo"
    325 			 * (Core Performance Boost), respectively.
    326 			 */
    327 			if ((regs[3] & CPUID_APM_TSC) != 0)
    328 				val &= ~ACPICPU_FLAG_C_TSC;
    329 
    330 			if ((regs[3] & CPUID_APM_HWP) != 0)
    331 				val |= ACPICPU_FLAG_P_FFH;
    332 
    333 			if ((regs[3] & CPUID_APM_CPB) != 0)
    334 				val |= ACPICPU_FLAG_P_TURBO;
    335 
    336 			/*
    337 			 * Also check for APERF and MPERF,
    338 			 * first available in the family 10h.
    339 			 */
    340 			if (cpuid_level >= 0x06) {
    341 
    342 				x86_cpuid(0x00000006, regs);
    343 
    344 				if ((regs[2] & CPUID_DSPM_HWF) != 0)
    345 					val |= ACPICPU_FLAG_P_HW;
    346 			}
    347 
    348 			break;
    349 		}
    350 
    351 		break;
    352 	}
    353 
    354 	/*
    355 	 * There are several erratums for PIIX4.
    356 	 */
    357 	if (pci_find_device(&pa, acpicpu_md_quirk_piix4) != 0)
    358 		val |= ACPICPU_FLAG_PIIX4;
    359 
    360 	return val;
    361 }
    362 
    363 static int
    364 acpicpu_md_quirk_piix4(struct pci_attach_args *pa)
    365 {
    366 
    367 	/*
    368 	 * XXX: The pci_find_device(9) function only
    369 	 *	deals with attached devices. Change this
    370 	 *	to use something like pci_device_foreach().
    371 	 */
    372 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    373 		return 0;
    374 
    375 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82371AB_ISA ||
    376 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82440MX_PMC)
    377 		return 1;
    378 
    379 	return 0;
    380 }
    381 
    382 void
    383 acpicpu_md_quirk_c1e(void)
    384 {
    385 	const uint64_t c1e = MSR_CMPHALT_SMI | MSR_CMPHALT_C1E;
    386 	uint64_t val;
    387 
    388 	val = rdmsr(MSR_CMPHALT);
    389 
    390 	if ((val & c1e) != 0)
    391 		wrmsr(MSR_CMPHALT, val & ~c1e);
    392 }
    393 
    394 int
    395 acpicpu_md_cstate_start(struct acpicpu_softc *sc)
    396 {
    397 	const size_t size = sizeof(native_idle_text);
    398 	struct acpicpu_cstate *cs;
    399 	bool ipi = false;
    400 	int i;
    401 
    402 	/*
    403 	 * Save the cpu_idle(9) loop used by default.
    404 	 */
    405 	x86_cpu_idle_get(&native_idle, native_idle_text, size);
    406 
    407 	for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
    408 
    409 		cs = &sc->sc_cstate[i];
    410 
    411 		if (cs->cs_method == ACPICPU_C_STATE_HALT) {
    412 			ipi = true;
    413 			break;
    414 		}
    415 	}
    416 
    417 	x86_cpu_idle_set(acpicpu_cstate_idle, "acpi", ipi);
    418 
    419 	return 0;
    420 }
    421 
    422 int
    423 acpicpu_md_cstate_stop(void)
    424 {
    425 	uint64_t xc;
    426 	bool ipi;
    427 
    428 	ipi = (native_idle != x86_cpu_idle_halt) ? false : true;
    429 	x86_cpu_idle_set(native_idle, native_idle_text, ipi);
    430 
    431 	/*
    432 	 * Run a cross-call to ensure that all CPUs are
    433 	 * out from the ACPI idle-loop before detachment.
    434 	 */
    435 	xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    436 	xc_wait(xc);
    437 
    438 	return 0;
    439 }
    440 
    441 /*
    442  * Called with interrupts disabled.
    443  * Caller should enable interrupts after return.
    444  */
    445 void
    446 acpicpu_md_cstate_enter(int method, int state)
    447 {
    448 	struct cpu_info *ci = curcpu();
    449 
    450 	switch (method) {
    451 
    452 	case ACPICPU_C_STATE_FFH:
    453 
    454 		x86_enable_intr();
    455 		x86_monitor(&ci->ci_want_resched, 0, 0);
    456 
    457 		if (__predict_false(ci->ci_want_resched != 0))
    458 			return;
    459 
    460 		x86_mwait((state - 1) << 4, 0);
    461 		break;
    462 
    463 	case ACPICPU_C_STATE_HALT:
    464 
    465 		if (__predict_false(ci->ci_want_resched != 0))
    466 			return;
    467 
    468 		x86_stihlt();
    469 		break;
    470 	}
    471 }
    472 
    473 int
    474 acpicpu_md_pstate_start(struct acpicpu_softc *sc)
    475 {
    476 	const uint64_t est = __BIT(16);
    477 	uint64_t val;
    478 
    479 	if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
    480 		return ENODEV;
    481 
    482 	switch (cpu_vendor) {
    483 
    484 	case CPUVENDOR_IDT:
    485 	case CPUVENDOR_INTEL:
    486 
    487 		/*
    488 		 * Make sure EST is enabled.
    489 		 */
    490 		val = rdmsr(MSR_MISC_ENABLE);
    491 
    492 		if ((val & est) == 0) {
    493 
    494 			val |= est;
    495 
    496 			wrmsr(MSR_MISC_ENABLE, val);
    497 			val = rdmsr(MSR_MISC_ENABLE);
    498 
    499 			if ((val & est) == 0)
    500 				return ENOTTY;
    501 		}
    502 	}
    503 
    504 	/*
    505 	 * Reset the APERF and MPERF counters.
    506 	 */
    507 	if ((sc->sc_flags & ACPICPU_FLAG_P_HW) != 0)
    508 		acpicpu_md_pstate_percent_reset(sc);
    509 
    510 	return acpicpu_md_pstate_sysctl_init();
    511 }
    512 
    513 int
    514 acpicpu_md_pstate_stop(void)
    515 {
    516 
    517 	if (acpicpu_log != NULL)
    518 		sysctl_teardown(&acpicpu_log);
    519 
    520 	return 0;
    521 }
    522 
    523 int
    524 acpicpu_md_pstate_pss(struct acpicpu_softc *sc)
    525 {
    526 	struct acpicpu_pstate *ps, msr;
    527 	struct cpu_info *ci = curcpu();
    528 	uint32_t family, i = 0;
    529 
    530 	(void)memset(&msr, 0, sizeof(struct acpicpu_pstate));
    531 
    532 	switch (cpu_vendor) {
    533 
    534 	case CPUVENDOR_IDT:
    535 	case CPUVENDOR_INTEL:
    536 
    537 		/*
    538 		 * If the so-called Turbo Boost is present,
    539 		 * the P0-state is always the "turbo state".
    540 		 *
    541 		 * For discussion, see:
    542 		 *
    543 		 *	Intel Corporation: Intel Turbo Boost Technology
    544 		 *	in Intel Core(tm) Microarchitectures (Nehalem)
    545 		 *	Based Processors. White Paper, November 2008.
    546 		 */
    547 		if ((sc->sc_flags & ACPICPU_FLAG_P_TURBO) != 0)
    548 			sc->sc_pstate[0].ps_flags |= ACPICPU_FLAG_P_TURBO;
    549 
    550 		msr.ps_control_addr = MSR_PERF_CTL;
    551 		msr.ps_control_mask = __BITS(0, 15);
    552 
    553 		msr.ps_status_addr  = MSR_PERF_STATUS;
    554 		msr.ps_status_mask  = __BITS(0, 15);
    555 		break;
    556 
    557 	case CPUVENDOR_AMD:
    558 
    559 		if ((sc->sc_flags & ACPICPU_FLAG_P_FIDVID) != 0)
    560 			msr.ps_flags |= ACPICPU_FLAG_P_FIDVID;
    561 
    562 		family = CPUID2FAMILY(ci->ci_signature);
    563 
    564 		if (family == 0xf)
    565 			family += CPUID2EXTFAMILY(ci->ci_signature);
    566 
    567 		switch (family) {
    568 
    569 		case 0x0f:
    570 			msr.ps_control_addr = MSR_0FH_CONTROL;
    571 			msr.ps_status_addr  = MSR_0FH_STATUS;
    572 			break;
    573 
    574 		case 0x10:
    575 		case 0x11:
    576 		case 0x14: /* AMD Fusion */
    577 			msr.ps_control_addr = MSR_10H_CONTROL;
    578 			msr.ps_control_mask = __BITS(0, 2);
    579 
    580 			msr.ps_status_addr  = MSR_10H_STATUS;
    581 			msr.ps_status_mask  = __BITS(0, 2);
    582 			break;
    583 
    584 		default:
    585 
    586 			if ((sc->sc_flags & ACPICPU_FLAG_P_XPSS) == 0)
    587 				return EOPNOTSUPP;
    588 		}
    589 
    590 		break;
    591 
    592 	default:
    593 		return ENODEV;
    594 	}
    595 
    596 	/*
    597 	 * Fill the P-state structures with MSR addresses that are
    598 	 * known to be correct. If we do not know the addresses,
    599 	 * leave the values intact. If a vendor uses XPSS, we do
    600 	 * not necessarily need to do anything to support new CPUs.
    601 	 */
    602 	while (i < sc->sc_pstate_count) {
    603 
    604 		ps = &sc->sc_pstate[i];
    605 
    606 		if (msr.ps_flags != 0)
    607 			ps->ps_flags |= msr.ps_flags;
    608 
    609 		if (msr.ps_status_addr != 0)
    610 			ps->ps_status_addr = msr.ps_status_addr;
    611 
    612 		if (msr.ps_status_mask != 0)
    613 			ps->ps_status_mask = msr.ps_status_mask;
    614 
    615 		if (msr.ps_control_addr != 0)
    616 			ps->ps_control_addr = msr.ps_control_addr;
    617 
    618 		if (msr.ps_control_mask != 0)
    619 			ps->ps_control_mask = msr.ps_control_mask;
    620 
    621 		i++;
    622 	}
    623 
    624 	return 0;
    625 }
    626 
    627 /*
    628  * Returns the percentage of the actual frequency in
    629  * terms of the maximum frequency of the calling CPU
    630  * since the last call. A value zero implies an error.
    631  */
    632 uint8_t
    633 acpicpu_md_pstate_percent(struct acpicpu_softc *sc)
    634 {
    635 	struct cpu_info *ci = sc->sc_ci;
    636 	uint64_t aperf, mperf;
    637 	uint64_t xc, rv = 0;
    638 
    639 	if (__predict_false((sc->sc_flags & ACPICPU_FLAG_P) == 0))
    640 		return 0;
    641 
    642 	if (__predict_false((sc->sc_flags & ACPICPU_FLAG_P_HW) == 0))
    643 		return 0;
    644 
    645 	/*
    646 	 * Read the IA32_APERF and IA32_MPERF counters. The first
    647 	 * increments at the rate of the fixed maximum frequency
    648 	 * configured during the boot, whereas APERF counts at the
    649 	 * rate of the actual frequency. Note that the MSRs must be
    650 	 * read without delay, and that only the ratio between
    651 	 * IA32_APERF and IA32_MPERF is architecturally defined.
    652 	 *
    653 	 * For further details, refer to:
    654 	 *
    655 	 *	Intel Corporation: Intel 64 and IA-32 Architectures
    656 	 *	Software Developer's Manual. Section 13.2, Volume 3A:
    657 	 *	System Programming Guide, Part 1. July, 2008.
    658 	 *
    659 	 *	Advanced Micro Devices: BIOS and Kernel Developer's
    660 	 *	Guide (BKDG) for AMD Family 10h Processors. Section
    661 	 *	2.4.5, Revision 3.48, April 2010.
    662 	 */
    663 	x86_disable_intr();
    664 
    665 	aperf = sc->sc_pstate_aperf;
    666 	mperf = sc->sc_pstate_mperf;
    667 
    668 	xc = xc_unicast(0, acpicpu_md_pstate_percent_status, sc, NULL, ci);
    669 	xc_wait(xc);
    670 
    671 	x86_enable_intr();
    672 
    673 	aperf = sc->sc_pstate_aperf - aperf;
    674 	mperf = sc->sc_pstate_mperf - mperf;
    675 
    676 	if (__predict_true(mperf != 0))
    677 		rv = (aperf * 100) / mperf;
    678 
    679 	return rv;
    680 }
    681 
    682 static void
    683 acpicpu_md_pstate_percent_status(void *arg1, void *arg2)
    684 {
    685 	struct acpicpu_softc *sc = arg1;
    686 
    687 	sc->sc_pstate_aperf = rdmsr(MSR_APERF);
    688 	sc->sc_pstate_mperf = rdmsr(MSR_MPERF);
    689 }
    690 
    691 static void
    692 acpicpu_md_pstate_percent_reset(struct acpicpu_softc *sc)
    693 {
    694 	struct msr_rw_info msr;
    695 	uint64_t xc;
    696 
    697 	KASSERT((sc->sc_flags & ACPICPU_FLAG_P) != 0);
    698 	KASSERT((sc->sc_flags & ACPICPU_FLAG_P_HW) != 0);
    699 
    700 	msr.msr_value = 0;
    701 	msr.msr_read = false;
    702 	msr.msr_type = MSR_APERF;
    703 
    704 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
    705 	xc_wait(xc);
    706 
    707 	msr.msr_value = 0;
    708 	msr.msr_read = false;
    709 	msr.msr_type = MSR_MPERF;
    710 
    711 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
    712 	xc_wait(xc);
    713 
    714 	sc->sc_pstate_aperf = 0;
    715 	sc->sc_pstate_mperf = 0;
    716 }
    717 
    718 int
    719 acpicpu_md_pstate_get(struct acpicpu_softc *sc, uint32_t *freq)
    720 {
    721 	struct acpicpu_pstate *ps = NULL;
    722 	uint64_t val;
    723 	uint32_t i;
    724 
    725 	if ((sc->sc_flags & ACPICPU_FLAG_P_FIDVID) != 0)
    726 		return acpicpu_md_pstate_fidvid_get(sc, freq);
    727 
    728 	for (i = 0; i < sc->sc_pstate_count; i++) {
    729 
    730 		ps = &sc->sc_pstate[i];
    731 
    732 		if (__predict_true(ps->ps_freq != 0))
    733 			break;
    734 	}
    735 
    736 	if (__predict_false(ps == NULL))
    737 		return ENODEV;
    738 
    739 	if (__predict_false(ps->ps_status_addr == 0))
    740 		return EINVAL;
    741 
    742 	val = rdmsr(ps->ps_status_addr);
    743 
    744 	if (__predict_true(ps->ps_status_mask != 0))
    745 		val = val & ps->ps_status_mask;
    746 
    747 	for (i = 0; i < sc->sc_pstate_count; i++) {
    748 
    749 		ps = &sc->sc_pstate[i];
    750 
    751 		if (__predict_false(ps->ps_freq == 0))
    752 			continue;
    753 
    754 		if (val == ps->ps_status) {
    755 			*freq = ps->ps_freq;
    756 			return 0;
    757 		}
    758 	}
    759 
    760 	return EIO;
    761 }
    762 
    763 int
    764 acpicpu_md_pstate_set(struct acpicpu_pstate *ps)
    765 {
    766 	struct msr_rw_info msr;
    767 	uint64_t xc;
    768 	int rv = 0;
    769 
    770 	if (__predict_false(ps->ps_control_addr == 0))
    771 		return EINVAL;
    772 
    773 	if ((ps->ps_flags & ACPICPU_FLAG_P_FIDVID) != 0)
    774 		return acpicpu_md_pstate_fidvid_set(ps);
    775 
    776 	msr.msr_read  = false;
    777 	msr.msr_type  = ps->ps_control_addr;
    778 	msr.msr_value = ps->ps_control;
    779 
    780 	if (__predict_true(ps->ps_control_mask != 0)) {
    781 		msr.msr_mask = ps->ps_control_mask;
    782 		msr.msr_read = true;
    783 	}
    784 
    785 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
    786 	xc_wait(xc);
    787 
    788 	/*
    789 	 * Due several problems, we bypass the
    790 	 * relatively expensive status check.
    791 	 */
    792 	if (acpicpu_pstate_status != true) {
    793 		DELAY(ps->ps_latency);
    794 		return 0;
    795 	}
    796 
    797 	xc = xc_broadcast(0, (xcfunc_t)acpicpu_md_pstate_status, ps, &rv);
    798 	xc_wait(xc);
    799 
    800 	return rv;
    801 }
    802 
    803 static void
    804 acpicpu_md_pstate_status(void *arg1, void *arg2)
    805 {
    806 	struct acpicpu_pstate *ps = arg1;
    807 	uint64_t val;
    808 	int i;
    809 
    810 	for (i = val = 0; i < ACPICPU_P_STATE_RETRY; i++) {
    811 
    812 		val = rdmsr(ps->ps_status_addr);
    813 
    814 		if (__predict_true(ps->ps_status_mask != 0))
    815 			val = val & ps->ps_status_mask;
    816 
    817 		if (val == ps->ps_status)
    818 			return;
    819 
    820 		DELAY(ps->ps_latency);
    821 	}
    822 
    823 	*(uintptr_t *)arg2 = EAGAIN;
    824 }
    825 
    826 static int
    827 acpicpu_md_pstate_fidvid_get(struct acpicpu_softc *sc, uint32_t *freq)
    828 {
    829 	struct acpicpu_pstate *ps;
    830 	uint32_t fid, i, vid;
    831 	uint32_t cfid, cvid;
    832 	int rv;
    833 
    834 	/*
    835 	 * AMD family 0Fh needs special treatment.
    836 	 * While it wants to use ACPI, it does not
    837 	 * comply with the ACPI specifications.
    838 	 */
    839 	rv = acpicpu_md_pstate_fidvid_read(&cfid, &cvid);
    840 
    841 	if (rv != 0)
    842 		return rv;
    843 
    844 	for (i = 0; i < sc->sc_pstate_count; i++) {
    845 
    846 		ps = &sc->sc_pstate[i];
    847 
    848 		if (__predict_false(ps->ps_freq == 0))
    849 			continue;
    850 
    851 		fid = __SHIFTOUT(ps->ps_status, ACPI_0FH_STATUS_FID);
    852 		vid = __SHIFTOUT(ps->ps_status, ACPI_0FH_STATUS_VID);
    853 
    854 		if (cfid == fid && cvid == vid) {
    855 			*freq = ps->ps_freq;
    856 			return 0;
    857 		}
    858 	}
    859 
    860 	return EIO;
    861 }
    862 
    863 static int
    864 acpicpu_md_pstate_fidvid_set(struct acpicpu_pstate *ps)
    865 {
    866 	const uint64_t ctrl = ps->ps_control;
    867 	uint32_t cfid, cvid, fid, i, irt;
    868 	uint32_t pll, vco_cfid, vco_fid;
    869 	uint32_t val, vid, vst;
    870 	int rv;
    871 
    872 	rv = acpicpu_md_pstate_fidvid_read(&cfid, &cvid);
    873 
    874 	if (rv != 0)
    875 		return rv;
    876 
    877 	fid = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_FID);
    878 	vid = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_VID);
    879 	irt = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_IRT);
    880 	vst = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_VST);
    881 	pll = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_PLL);
    882 
    883 	vst = vst * 20;
    884 	pll = pll * 1000 / 5;
    885 	irt = 10 * __BIT(irt);
    886 
    887 	/*
    888 	 * Phase 1.
    889 	 */
    890 	while (cvid > vid) {
    891 
    892 		val = 1 << __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_MVS);
    893 		val = (val > cvid) ? 0 : cvid - val;
    894 
    895 		acpicpu_md_pstate_fidvid_write(cfid, val, 1, vst);
    896 		rv = acpicpu_md_pstate_fidvid_read(NULL, &cvid);
    897 
    898 		if (rv != 0)
    899 			return rv;
    900 	}
    901 
    902 	i = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_RVO);
    903 
    904 	for (; i > 0 && cvid > 0; --i) {
    905 
    906 		acpicpu_md_pstate_fidvid_write(cfid, cvid - 1, 1, vst);
    907 		rv = acpicpu_md_pstate_fidvid_read(NULL, &cvid);
    908 
    909 		if (rv != 0)
    910 			return rv;
    911 	}
    912 
    913 	/*
    914 	 * Phase 2.
    915 	 */
    916 	if (cfid != fid) {
    917 
    918 		vco_fid  = FID_TO_VCO_FID(fid);
    919 		vco_cfid = FID_TO_VCO_FID(cfid);
    920 
    921 		while (abs(vco_fid - vco_cfid) > 2) {
    922 
    923 			if (fid <= cfid)
    924 				val = cfid - 2;
    925 			else {
    926 				val = (cfid > 6) ? cfid + 2 :
    927 				    FID_TO_VCO_FID(cfid) + 2;
    928 			}
    929 
    930 			acpicpu_md_pstate_fidvid_write(val, cvid, pll, irt);
    931 			rv = acpicpu_md_pstate_fidvid_read(&cfid, NULL);
    932 
    933 			if (rv != 0)
    934 				return rv;
    935 
    936 			vco_cfid = FID_TO_VCO_FID(cfid);
    937 		}
    938 
    939 		acpicpu_md_pstate_fidvid_write(fid, cvid, pll, irt);
    940 		rv = acpicpu_md_pstate_fidvid_read(&cfid, NULL);
    941 
    942 		if (rv != 0)
    943 			return rv;
    944 	}
    945 
    946 	/*
    947 	 * Phase 3.
    948 	 */
    949 	if (cvid != vid) {
    950 
    951 		acpicpu_md_pstate_fidvid_write(cfid, vid, 1, vst);
    952 		rv = acpicpu_md_pstate_fidvid_read(NULL, &cvid);
    953 
    954 		if (rv != 0)
    955 			return rv;
    956 	}
    957 
    958 	if (cfid != fid || cvid != vid)
    959 		return EIO;
    960 
    961 	return 0;
    962 }
    963 
    964 static int
    965 acpicpu_md_pstate_fidvid_read(uint32_t *cfid, uint32_t *cvid)
    966 {
    967 	int i = ACPICPU_P_STATE_RETRY * 100;
    968 	uint64_t val;
    969 
    970 	do {
    971 		val = rdmsr(MSR_0FH_STATUS);
    972 
    973 	} while (__SHIFTOUT(val, MSR_0FH_STATUS_PENDING) != 0 && --i >= 0);
    974 
    975 	if (i == 0)
    976 		return EAGAIN;
    977 
    978 	if (cfid != NULL)
    979 		*cfid = __SHIFTOUT(val, MSR_0FH_STATUS_CFID);
    980 
    981 	if (cvid != NULL)
    982 		*cvid = __SHIFTOUT(val, MSR_0FH_STATUS_CVID);
    983 
    984 	return 0;
    985 }
    986 
    987 static void
    988 acpicpu_md_pstate_fidvid_write(uint32_t fid,
    989     uint32_t vid, uint32_t cnt, uint32_t tmo)
    990 {
    991 	struct msr_rw_info msr;
    992 	uint64_t xc;
    993 
    994 	msr.msr_read  = false;
    995 	msr.msr_type  = MSR_0FH_CONTROL;
    996 	msr.msr_value = 0;
    997 
    998 	msr.msr_value |= __SHIFTIN(fid, MSR_0FH_CONTROL_FID);
    999 	msr.msr_value |= __SHIFTIN(vid, MSR_0FH_CONTROL_VID);
   1000 	msr.msr_value |= __SHIFTIN(cnt, MSR_0FH_CONTROL_CNT);
   1001 	msr.msr_value |= __SHIFTIN(0x1, MSR_0FH_CONTROL_CHG);
   1002 
   1003 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
   1004 	xc_wait(xc);
   1005 
   1006 	DELAY(tmo);
   1007 }
   1008 
   1009 int
   1010 acpicpu_md_tstate_get(struct acpicpu_softc *sc, uint32_t *percent)
   1011 {
   1012 	struct acpicpu_tstate *ts;
   1013 	uint64_t val;
   1014 	uint32_t i;
   1015 
   1016 	val = rdmsr(MSR_THERM_CONTROL);
   1017 
   1018 	for (i = 0; i < sc->sc_tstate_count; i++) {
   1019 
   1020 		ts = &sc->sc_tstate[i];
   1021 
   1022 		if (ts->ts_percent == 0)
   1023 			continue;
   1024 
   1025 		if (val == ts->ts_status) {
   1026 			*percent = ts->ts_percent;
   1027 			return 0;
   1028 		}
   1029 	}
   1030 
   1031 	return EIO;
   1032 }
   1033 
   1034 int
   1035 acpicpu_md_tstate_set(struct acpicpu_tstate *ts)
   1036 {
   1037 	struct msr_rw_info msr;
   1038 	uint64_t xc;
   1039 	int rv = 0;
   1040 
   1041 	msr.msr_read  = true;
   1042 	msr.msr_type  = MSR_THERM_CONTROL;
   1043 	msr.msr_value = ts->ts_control;
   1044 	msr.msr_mask = __BITS(1, 4);
   1045 
   1046 	xc = xc_broadcast(0, (xcfunc_t)x86_msr_xcall, &msr, NULL);
   1047 	xc_wait(xc);
   1048 
   1049 	if (ts->ts_status == 0) {
   1050 		DELAY(ts->ts_latency);
   1051 		return 0;
   1052 	}
   1053 
   1054 	xc = xc_broadcast(0, (xcfunc_t)acpicpu_md_tstate_status, ts, &rv);
   1055 	xc_wait(xc);
   1056 
   1057 	return rv;
   1058 }
   1059 
   1060 static void
   1061 acpicpu_md_tstate_status(void *arg1, void *arg2)
   1062 {
   1063 	struct acpicpu_tstate *ts = arg1;
   1064 	uint64_t val;
   1065 	int i;
   1066 
   1067 	for (i = val = 0; i < ACPICPU_T_STATE_RETRY; i++) {
   1068 
   1069 		val = rdmsr(MSR_THERM_CONTROL);
   1070 
   1071 		if (val == ts->ts_status)
   1072 			return;
   1073 
   1074 		DELAY(ts->ts_latency);
   1075 	}
   1076 
   1077 	*(uintptr_t *)arg2 = EAGAIN;
   1078 }
   1079 
   1080 /*
   1081  * A kludge for backwards compatibility.
   1082  */
   1083 static int
   1084 acpicpu_md_pstate_sysctl_init(void)
   1085 {
   1086 	const struct sysctlnode	*fnode, *mnode, *rnode;
   1087 	const char *str;
   1088 	int rv;
   1089 
   1090 	switch (cpu_vendor) {
   1091 
   1092 	case CPUVENDOR_IDT:
   1093 	case CPUVENDOR_INTEL:
   1094 		str = "est";
   1095 		break;
   1096 
   1097 	case CPUVENDOR_AMD:
   1098 		str = "powernow";
   1099 		break;
   1100 
   1101 	default:
   1102 		return ENODEV;
   1103 	}
   1104 
   1105 
   1106 	rv = sysctl_createv(&acpicpu_log, 0, NULL, &rnode,
   1107 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
   1108 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
   1109 
   1110 	if (rv != 0)
   1111 		goto fail;
   1112 
   1113 	rv = sysctl_createv(&acpicpu_log, 0, &rnode, &mnode,
   1114 	    0, CTLTYPE_NODE, str, NULL,
   1115 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
   1116 
   1117 	if (rv != 0)
   1118 		goto fail;
   1119 
   1120 	rv = sysctl_createv(&acpicpu_log, 0, &mnode, &fnode,
   1121 	    0, CTLTYPE_NODE, "frequency", NULL,
   1122 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
   1123 
   1124 	if (rv != 0)
   1125 		goto fail;
   1126 
   1127 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
   1128 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
   1129 	    acpicpu_md_pstate_sysctl_set, 0, NULL, 0, CTL_CREATE, CTL_EOL);
   1130 
   1131 	if (rv != 0)
   1132 		goto fail;
   1133 
   1134 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
   1135 	    CTLFLAG_READONLY, CTLTYPE_INT, "current", NULL,
   1136 	    acpicpu_md_pstate_sysctl_get, 0, NULL, 0, CTL_CREATE, CTL_EOL);
   1137 
   1138 	if (rv != 0)
   1139 		goto fail;
   1140 
   1141 	rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
   1142 	    CTLFLAG_READONLY, CTLTYPE_STRING, "available", NULL,
   1143 	    acpicpu_md_pstate_sysctl_all, 0, NULL, 0, CTL_CREATE, CTL_EOL);
   1144 
   1145 	if (rv != 0)
   1146 		goto fail;
   1147 
   1148 	return 0;
   1149 
   1150 fail:
   1151 	if (acpicpu_log != NULL) {
   1152 		sysctl_teardown(&acpicpu_log);
   1153 		acpicpu_log = NULL;
   1154 	}
   1155 
   1156 	return rv;
   1157 }
   1158 
   1159 static int
   1160 acpicpu_md_pstate_sysctl_get(SYSCTLFN_ARGS)
   1161 {
   1162 	struct cpu_info *ci = curcpu();
   1163 	struct acpicpu_softc *sc;
   1164 	struct sysctlnode node;
   1165 	uint32_t freq;
   1166 	int err;
   1167 
   1168 	sc = acpicpu_sc[ci->ci_acpiid];
   1169 
   1170 	if (sc == NULL)
   1171 		return ENXIO;
   1172 
   1173 	err = acpicpu_pstate_get(sc, &freq);
   1174 
   1175 	if (err != 0)
   1176 		return err;
   1177 
   1178 	node = *rnode;
   1179 	node.sysctl_data = &freq;
   1180 
   1181 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1182 
   1183 	if (err != 0 || newp == NULL)
   1184 		return err;
   1185 
   1186 	return 0;
   1187 }
   1188 
   1189 static int
   1190 acpicpu_md_pstate_sysctl_set(SYSCTLFN_ARGS)
   1191 {
   1192 	struct cpu_info *ci = curcpu();
   1193 	struct acpicpu_softc *sc;
   1194 	struct sysctlnode node;
   1195 	uint32_t freq;
   1196 	int err;
   1197 
   1198 	sc = acpicpu_sc[ci->ci_acpiid];
   1199 
   1200 	if (sc == NULL)
   1201 		return ENXIO;
   1202 
   1203 	err = acpicpu_pstate_get(sc, &freq);
   1204 
   1205 	if (err != 0)
   1206 		return err;
   1207 
   1208 	node = *rnode;
   1209 	node.sysctl_data = &freq;
   1210 
   1211 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1212 
   1213 	if (err != 0 || newp == NULL)
   1214 		return err;
   1215 
   1216 	err = acpicpu_pstate_set(sc, freq);
   1217 
   1218 	if (err != 0)
   1219 		return err;
   1220 
   1221 	return 0;
   1222 }
   1223 
   1224 static int
   1225 acpicpu_md_pstate_sysctl_all(SYSCTLFN_ARGS)
   1226 {
   1227 	struct cpu_info *ci = curcpu();
   1228 	struct acpicpu_softc *sc;
   1229 	struct sysctlnode node;
   1230 	char buf[1024];
   1231 	size_t len;
   1232 	uint32_t i;
   1233 	int err;
   1234 
   1235 	sc = acpicpu_sc[ci->ci_acpiid];
   1236 
   1237 	if (sc == NULL)
   1238 		return ENXIO;
   1239 
   1240 	(void)memset(&buf, 0, sizeof(buf));
   1241 
   1242 	mutex_enter(&sc->sc_mtx);
   1243 
   1244 	for (len = 0, i = sc->sc_pstate_max; i < sc->sc_pstate_count; i++) {
   1245 
   1246 		if (sc->sc_pstate[i].ps_freq == 0)
   1247 			continue;
   1248 
   1249 		len += snprintf(buf + len, sizeof(buf) - len, "%u%s",
   1250 		    sc->sc_pstate[i].ps_freq,
   1251 		    i < (sc->sc_pstate_count - 1) ? " " : "");
   1252 	}
   1253 
   1254 	mutex_exit(&sc->sc_mtx);
   1255 
   1256 	node = *rnode;
   1257 	node.sysctl_data = buf;
   1258 
   1259 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1260 
   1261 	if (err != 0 || newp == NULL)
   1262 		return err;
   1263 
   1264 	return 0;
   1265 }
   1266 
   1267