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