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