xref: /titanic_50/usr/src/uts/i86pc/os/cpupm/pwrnow.c (revision 511588bb13d2462265d682dc1cb7ba5c7a27a771)
10e751525SEric Saxe /*
20e751525SEric Saxe  * CDDL HEADER START
30e751525SEric Saxe  *
40e751525SEric Saxe  * The contents of this file are subject to the terms of the
50e751525SEric Saxe  * Common Development and Distribution License (the "License").
60e751525SEric Saxe  * You may not use this file except in compliance with the License.
70e751525SEric Saxe  *
80e751525SEric Saxe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90e751525SEric Saxe  * or http://www.opensolaris.org/os/licensing.
100e751525SEric Saxe  * See the License for the specific language governing permissions
110e751525SEric Saxe  * and limitations under the License.
120e751525SEric Saxe  *
130e751525SEric Saxe  * When distributing Covered Code, include this CDDL HEADER in each
140e751525SEric Saxe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150e751525SEric Saxe  * If applicable, add the following below this CDDL HEADER, with the
160e751525SEric Saxe  * fields enclosed by brackets "[]" replaced with your own identifying
170e751525SEric Saxe  * information: Portions Copyright [yyyy] [name of copyright owner]
180e751525SEric Saxe  *
190e751525SEric Saxe  * CDDL HEADER END
200e751525SEric Saxe  */
210e751525SEric Saxe /*
227417cfdeSKuriakose Kuruvilla  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
230e751525SEric Saxe  */
240e751525SEric Saxe 
250e751525SEric Saxe #include <sys/x86_archext.h>
260e751525SEric Saxe #include <sys/machsystm.h>
270e751525SEric Saxe #include <sys/x_call.h>
280e751525SEric Saxe #include <sys/acpi/acpi.h>
290e751525SEric Saxe #include <sys/acpica.h>
300e751525SEric Saxe #include <sys/pwrnow.h>
310e751525SEric Saxe #include <sys/cpu_acpi.h>
320e751525SEric Saxe #include <sys/cpupm.h>
330e751525SEric Saxe #include <sys/dtrace.h>
340e751525SEric Saxe #include <sys/sdt.h>
350e751525SEric Saxe 
360e751525SEric Saxe static int pwrnow_init(cpu_t *);
370e751525SEric Saxe static void pwrnow_fini(cpu_t *);
380e751525SEric Saxe static void pwrnow_power(cpuset_t, uint32_t);
39444f66e7SMark Haywood static void pwrnow_stop(cpu_t *);
400e751525SEric Saxe 
415951ced0SHans Rosenfeld static boolean_t pwrnow_cpb_supported(void);
425951ced0SHans Rosenfeld 
430e751525SEric Saxe /*
440e751525SEric Saxe  * Interfaces for modules implementing AMD's PowerNow!.
450e751525SEric Saxe  */
460e751525SEric Saxe cpupm_state_ops_t pwrnow_ops = {
470e751525SEric Saxe 	"PowerNow! Technology",
480e751525SEric Saxe 	pwrnow_init,
490e751525SEric Saxe 	pwrnow_fini,
50444f66e7SMark Haywood 	pwrnow_power,
51444f66e7SMark Haywood 	pwrnow_stop
520e751525SEric Saxe };
530e751525SEric Saxe 
540e751525SEric Saxe /*
550e751525SEric Saxe  * Error returns
560e751525SEric Saxe  */
570e751525SEric Saxe #define	PWRNOW_RET_SUCCESS		0x00
580e751525SEric Saxe #define	PWRNOW_RET_NO_PM		0x01
590e751525SEric Saxe #define	PWRNOW_RET_UNSUP_STATE		0x02
600e751525SEric Saxe #define	PWRNOW_RET_TRANS_INCOMPLETE	0x03
610e751525SEric Saxe 
620e751525SEric Saxe #define	PWRNOW_LATENCY_WAIT		10
630e751525SEric Saxe 
640e751525SEric Saxe /*
650e751525SEric Saxe  * MSR registers for changing and reading processor power state.
660e751525SEric Saxe  */
670e751525SEric Saxe #define	PWRNOW_PERF_CTL_MSR		0xC0010062
680e751525SEric Saxe #define	PWRNOW_PERF_STATUS_MSR		0xC0010063
690e751525SEric Saxe 
700e751525SEric Saxe #define	AMD_CPUID_PSTATE_HARDWARE	(1<<7)
710e751525SEric Saxe #define	AMD_CPUID_TSC_CONSTANT		(1<<8)
725951ced0SHans Rosenfeld #define	AMD_CPUID_CPB			(1<<9)
730e751525SEric Saxe 
740e751525SEric Saxe /*
750e751525SEric Saxe  * Debugging support
760e751525SEric Saxe  */
770e751525SEric Saxe #ifdef	DEBUG
780e751525SEric Saxe volatile int pwrnow_debug = 0;
790e751525SEric Saxe #define	PWRNOW_DEBUG(arglist) if (pwrnow_debug) printf arglist;
800e751525SEric Saxe #else
810e751525SEric Saxe #define	PWRNOW_DEBUG(arglist)
820e751525SEric Saxe #endif
830e751525SEric Saxe 
840e751525SEric Saxe /*
850e751525SEric Saxe  * Write the ctrl register.
860e751525SEric Saxe  */
870e751525SEric Saxe static void
write_ctrl(cpu_acpi_handle_t handle,uint32_t ctrl)880e751525SEric Saxe write_ctrl(cpu_acpi_handle_t handle, uint32_t ctrl)
890e751525SEric Saxe {
900e751525SEric Saxe 	cpu_acpi_pct_t *pct_ctrl;
910e751525SEric Saxe 	uint64_t reg;
920e751525SEric Saxe 
930e751525SEric Saxe 	pct_ctrl = CPU_ACPI_PCT_CTRL(handle);
940e751525SEric Saxe 
950e751525SEric Saxe 	switch (pct_ctrl->cr_addrspace_id) {
960e751525SEric Saxe 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
970e751525SEric Saxe 		reg = ctrl;
980e751525SEric Saxe 		wrmsr(PWRNOW_PERF_CTL_MSR, reg);
990e751525SEric Saxe 		break;
1000e751525SEric Saxe 
1010e751525SEric Saxe 	default:
1020e751525SEric Saxe 		DTRACE_PROBE1(pwrnow_ctrl_unsupported_type, uint8_t,
1030e751525SEric Saxe 		    pct_ctrl->cr_addrspace_id);
1040e751525SEric Saxe 		return;
1050e751525SEric Saxe 	}
1060e751525SEric Saxe 
1070e751525SEric Saxe 	DTRACE_PROBE1(pwrnow_ctrl_write, uint32_t, ctrl);
1080e751525SEric Saxe }
1090e751525SEric Saxe 
1100e751525SEric Saxe /*
1110e751525SEric Saxe  * Transition the current processor to the requested state.
1120e751525SEric Saxe  */
1130e751525SEric Saxe static void
pwrnow_pstate_transition(uint32_t req_state)1140e751525SEric Saxe pwrnow_pstate_transition(uint32_t req_state)
1150e751525SEric Saxe {
1160e751525SEric Saxe 	cpupm_mach_state_t *mach_state =
1170e751525SEric Saxe 	    (cpupm_mach_state_t *)CPU->cpu_m.mcpu_pm_mach_state;
1180e751525SEric Saxe 	cpu_acpi_handle_t handle = mach_state->ms_acpi_handle;
1190e751525SEric Saxe 	cpu_acpi_pstate_t *req_pstate;
1200e751525SEric Saxe 	uint32_t ctrl;
1210e751525SEric Saxe 
1220e751525SEric Saxe 	req_pstate = (cpu_acpi_pstate_t *)CPU_ACPI_PSTATES(handle);
1230e751525SEric Saxe 	req_pstate += req_state;
1240e751525SEric Saxe 
1250e751525SEric Saxe 	DTRACE_PROBE1(pwrnow_transition_freq, uint32_t,
1260e751525SEric Saxe 	    CPU_ACPI_FREQ(req_pstate));
1270e751525SEric Saxe 
1280e751525SEric Saxe 	/*
1290e751525SEric Saxe 	 * Initiate the processor p-state change.
1300e751525SEric Saxe 	 */
1310e751525SEric Saxe 	ctrl = CPU_ACPI_PSTATE_CTRL(req_pstate);
1320e751525SEric Saxe 	write_ctrl(handle, ctrl);
1330e751525SEric Saxe 
1345951ced0SHans Rosenfeld 	if (mach_state->ms_turbo != NULL)
1355951ced0SHans Rosenfeld 		cpupm_record_turbo_info(mach_state->ms_turbo,
1365951ced0SHans Rosenfeld 		    mach_state->ms_pstate.cma_state.pstate, req_state);
1375951ced0SHans Rosenfeld 
1380e751525SEric Saxe 	mach_state->ms_pstate.cma_state.pstate = req_state;
1390e751525SEric Saxe 	cpu_set_curr_clock((uint64_t)CPU_ACPI_FREQ(req_pstate) * 1000000);
1400e751525SEric Saxe }
1410e751525SEric Saxe 
1420e751525SEric Saxe static void
pwrnow_power(cpuset_t set,uint32_t req_state)1430e751525SEric Saxe pwrnow_power(cpuset_t set, uint32_t req_state)
1440e751525SEric Saxe {
1450e751525SEric Saxe 	/*
1460e751525SEric Saxe 	 * If thread is already running on target CPU then just
1470e751525SEric Saxe 	 * make the transition request. Otherwise, we'll need to
1480e751525SEric Saxe 	 * make a cross-call.
1490e751525SEric Saxe 	 */
1500e751525SEric Saxe 	kpreempt_disable();
1510e751525SEric Saxe 	if (CPU_IN_SET(set, CPU->cpu_id)) {
1520e751525SEric Saxe 		pwrnow_pstate_transition(req_state);
1530e751525SEric Saxe 		CPUSET_DEL(set, CPU->cpu_id);
1540e751525SEric Saxe 	}
1550e751525SEric Saxe 	if (!CPUSET_ISNULL(set)) {
156f34a7178SJoe Bonasera 		xc_call((xc_arg_t)req_state, NULL, NULL,
157f34a7178SJoe Bonasera 		    CPUSET2BV(set), (xc_func_t)pwrnow_pstate_transition);
1580e751525SEric Saxe 	}
1590e751525SEric Saxe 	kpreempt_enable();
1600e751525SEric Saxe }
1610e751525SEric Saxe 
1620e751525SEric Saxe /*
1630e751525SEric Saxe  * Validate that this processor supports PowerNow! and if so,
1640e751525SEric Saxe  * get the P-state data from ACPI and cache it.
1650e751525SEric Saxe  */
1660e751525SEric Saxe static int
pwrnow_init(cpu_t * cp)1670e751525SEric Saxe pwrnow_init(cpu_t *cp)
1680e751525SEric Saxe {
1690e751525SEric Saxe 	cpupm_mach_state_t *mach_state =
1700e751525SEric Saxe 	    (cpupm_mach_state_t *)cp->cpu_m.mcpu_pm_mach_state;
1710e751525SEric Saxe 	cpu_acpi_handle_t handle = mach_state->ms_acpi_handle;
1720e751525SEric Saxe 	cpu_acpi_pct_t *pct_stat;
173*511588bbSYuri Pankov 	static int logged = 0;
1740e751525SEric Saxe 
1750e751525SEric Saxe 	PWRNOW_DEBUG(("pwrnow_init: processor %d\n", cp->cpu_id));
1760e751525SEric Saxe 
1770e751525SEric Saxe 	/*
1780e751525SEric Saxe 	 * Cache the P-state specific ACPI data.
1790e751525SEric Saxe 	 */
1800e751525SEric Saxe 	if (cpu_acpi_cache_pstate_data(handle) != 0) {
181*511588bbSYuri Pankov 		if (!logged) {
18200f97612SMark Haywood 			cmn_err(CE_NOTE, "!PowerNow! support is being "
183*511588bbSYuri Pankov 			    "disabled due to errors parsing ACPI P-state "
184*511588bbSYuri Pankov 			    "objects exported by BIOS.");
185*511588bbSYuri Pankov 			logged = 1;
186*511588bbSYuri Pankov 		}
1870e751525SEric Saxe 		pwrnow_fini(cp);
1880e751525SEric Saxe 		return (PWRNOW_RET_NO_PM);
1890e751525SEric Saxe 	}
1900e751525SEric Saxe 
1910e751525SEric Saxe 	pct_stat = CPU_ACPI_PCT_STATUS(handle);
1920e751525SEric Saxe 	switch (pct_stat->cr_addrspace_id) {
1930e751525SEric Saxe 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
1940e751525SEric Saxe 		PWRNOW_DEBUG(("Transitions will use fixed hardware\n"));
1950e751525SEric Saxe 		break;
1960e751525SEric Saxe 	default:
1970e751525SEric Saxe 		cmn_err(CE_WARN, "!_PCT configured for unsupported "
1980e751525SEric Saxe 		    "addrspace = %d.", pct_stat->cr_addrspace_id);
1990e751525SEric Saxe 		cmn_err(CE_NOTE, "!CPU power management will not function.");
2000e751525SEric Saxe 		pwrnow_fini(cp);
2010e751525SEric Saxe 		return (PWRNOW_RET_NO_PM);
2020e751525SEric Saxe 	}
2030e751525SEric Saxe 
2040e751525SEric Saxe 	cpupm_alloc_domains(cp, CPUPM_P_STATES);
2050e751525SEric Saxe 
2065951ced0SHans Rosenfeld 	/*
2075951ced0SHans Rosenfeld 	 * Check for Core Performance Boost support
2085951ced0SHans Rosenfeld 	 */
2095951ced0SHans Rosenfeld 	if (pwrnow_cpb_supported())
2105951ced0SHans Rosenfeld 		mach_state->ms_turbo = cpupm_turbo_init(cp);
2115951ced0SHans Rosenfeld 
2120e751525SEric Saxe 	PWRNOW_DEBUG(("Processor %d succeeded.\n", cp->cpu_id))
2130e751525SEric Saxe 	return (PWRNOW_RET_SUCCESS);
2140e751525SEric Saxe }
2150e751525SEric Saxe 
2160e751525SEric Saxe /*
2170e751525SEric Saxe  * Free resources allocated by pwrnow_init().
2180e751525SEric Saxe  */
2190e751525SEric Saxe static void
pwrnow_fini(cpu_t * cp)2200e751525SEric Saxe pwrnow_fini(cpu_t *cp)
2210e751525SEric Saxe {
2220e751525SEric Saxe 	cpupm_mach_state_t *mach_state =
2230e751525SEric Saxe 	    (cpupm_mach_state_t *)(cp->cpu_m.mcpu_pm_mach_state);
2240e751525SEric Saxe 	cpu_acpi_handle_t handle = mach_state->ms_acpi_handle;
2250e751525SEric Saxe 
2260e751525SEric Saxe 	cpupm_free_domains(&cpupm_pstate_domains);
2270e751525SEric Saxe 	cpu_acpi_free_pstate_data(handle);
2285951ced0SHans Rosenfeld 
2295951ced0SHans Rosenfeld 	if (mach_state->ms_turbo != NULL)
2305951ced0SHans Rosenfeld 		cpupm_turbo_fini(mach_state->ms_turbo);
2315951ced0SHans Rosenfeld 	mach_state->ms_turbo = NULL;
2320e751525SEric Saxe }
2330e751525SEric Saxe 
2340e751525SEric Saxe boolean_t
pwrnow_supported()2350e751525SEric Saxe pwrnow_supported()
2360e751525SEric Saxe {
2370e751525SEric Saxe 	struct cpuid_regs cpu_regs;
2380e751525SEric Saxe 
2390e751525SEric Saxe 	/* Required features */
2407417cfdeSKuriakose Kuruvilla 	if (!is_x86_feature(x86_featureset, X86FSET_CPUID) ||
2417417cfdeSKuriakose Kuruvilla 	    !is_x86_feature(x86_featureset, X86FSET_MSR)) {
2420e751525SEric Saxe 		PWRNOW_DEBUG(("No CPUID or MSR support."));
2430e751525SEric Saxe 		return (B_FALSE);
2440e751525SEric Saxe 	}
2450e751525SEric Saxe 
2460e751525SEric Saxe 	/*
2470e751525SEric Saxe 	 * Get the Advanced Power Management Information.
2480e751525SEric Saxe 	 */
2490e751525SEric Saxe 	cpu_regs.cp_eax = 0x80000007;
2500e751525SEric Saxe 	(void) __cpuid_insn(&cpu_regs);
2510e751525SEric Saxe 
2520e751525SEric Saxe 	/*
2530e751525SEric Saxe 	 * We currently only support CPU power management of
2540e751525SEric Saxe 	 * processors that are P-state TSC invariant
2550e751525SEric Saxe 	 */
2560e751525SEric Saxe 	if (!(cpu_regs.cp_edx & AMD_CPUID_TSC_CONSTANT)) {
2570e751525SEric Saxe 		PWRNOW_DEBUG(("No support for CPUs that are not P-state "
2580e751525SEric Saxe 		    "TSC invariant.\n"));
2590e751525SEric Saxe 		return (B_FALSE);
2600e751525SEric Saxe 	}
2610e751525SEric Saxe 
2620e751525SEric Saxe 	/*
2630e751525SEric Saxe 	 * We only support the "Fire and Forget" style of PowerNow! (i.e.,
2640e751525SEric Saxe 	 * single MSR write to change speed).
2650e751525SEric Saxe 	 */
2660e751525SEric Saxe 	if (!(cpu_regs.cp_edx & AMD_CPUID_PSTATE_HARDWARE)) {
2670e751525SEric Saxe 		PWRNOW_DEBUG(("Hardware P-State control is not supported.\n"));
2680e751525SEric Saxe 		return (B_FALSE);
2690e751525SEric Saxe 	}
2700e751525SEric Saxe 	return (B_TRUE);
2710e751525SEric Saxe }
272444f66e7SMark Haywood 
2735951ced0SHans Rosenfeld static boolean_t
pwrnow_cpb_supported(void)2745951ced0SHans Rosenfeld pwrnow_cpb_supported(void)
2755951ced0SHans Rosenfeld {
2765951ced0SHans Rosenfeld 	struct cpuid_regs cpu_regs;
2775951ced0SHans Rosenfeld 
2785951ced0SHans Rosenfeld 	/* Required features */
2795951ced0SHans Rosenfeld 	if (!is_x86_feature(x86_featureset, X86FSET_CPUID) ||
2805951ced0SHans Rosenfeld 	    !is_x86_feature(x86_featureset, X86FSET_MSR)) {
2815951ced0SHans Rosenfeld 		PWRNOW_DEBUG(("No CPUID or MSR support."));
2825951ced0SHans Rosenfeld 		return (B_FALSE);
2835951ced0SHans Rosenfeld 	}
2845951ced0SHans Rosenfeld 
2855951ced0SHans Rosenfeld 	/*
2865951ced0SHans Rosenfeld 	 * Get the Advanced Power Management Information.
2875951ced0SHans Rosenfeld 	 */
2885951ced0SHans Rosenfeld 	cpu_regs.cp_eax = 0x80000007;
2895951ced0SHans Rosenfeld 	(void) __cpuid_insn(&cpu_regs);
2905951ced0SHans Rosenfeld 
2915951ced0SHans Rosenfeld 	if (!(cpu_regs.cp_edx & AMD_CPUID_CPB))
2925951ced0SHans Rosenfeld 		return (B_FALSE);
2935951ced0SHans Rosenfeld 
2945951ced0SHans Rosenfeld 	return (B_TRUE);
2955951ced0SHans Rosenfeld }
2965951ced0SHans Rosenfeld 
297444f66e7SMark Haywood static void
pwrnow_stop(cpu_t * cp)298444f66e7SMark Haywood pwrnow_stop(cpu_t *cp)
299444f66e7SMark Haywood {
300444f66e7SMark Haywood 	cpupm_mach_state_t *mach_state =
301444f66e7SMark Haywood 	    (cpupm_mach_state_t *)(cp->cpu_m.mcpu_pm_mach_state);
302444f66e7SMark Haywood 	cpu_acpi_handle_t handle = mach_state->ms_acpi_handle;
303444f66e7SMark Haywood 
304444f66e7SMark Haywood 	cpupm_remove_domains(cp, CPUPM_P_STATES, &cpupm_pstate_domains);
305444f66e7SMark Haywood 	cpu_acpi_free_pstate_data(handle);
3065951ced0SHans Rosenfeld 
3075951ced0SHans Rosenfeld 	if (mach_state->ms_turbo != NULL)
3085951ced0SHans Rosenfeld 		cpupm_turbo_fini(mach_state->ms_turbo);
3095951ced0SHans Rosenfeld 	mach_state->ms_turbo = NULL;
310444f66e7SMark Haywood }
311