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