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