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