Fix NULL pointer constant issues in kernel/arch/x86
[unleashed.git] / kernel / arch / x86 / platform / i86pc / os / cpupm / speedstep.c
blob799e0da4ef42d86b3304ce40dd6ae09119d34563
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2009, Intel Corporation.
26 * All Rights Reserved.
29 #include <sys/x86_archext.h>
30 #include <sys/machsystm.h>
31 #include <sys/archsystm.h>
32 #include <sys/x_call.h>
33 #include <sys/acpi/acpi.h>
34 #include <sys/acpica.h>
35 #include <sys/speedstep.h>
36 #include <sys/cpu_acpi.h>
37 #include <sys/cpupm.h>
38 #include <sys/dtrace.h>
39 #include <sys/sdt.h>
41 static int speedstep_init(cpu_t *);
42 static void speedstep_fini(cpu_t *);
43 static void speedstep_power(cpuset_t, uint32_t);
44 static void speedstep_stop(cpu_t *);
45 static boolean_t speedstep_turbo_supported(void);
48 * Interfaces for modules implementing Intel's Enhanced SpeedStep.
50 cpupm_state_ops_t speedstep_ops = {
51 "Enhanced SpeedStep Technology",
52 speedstep_init,
53 speedstep_fini,
54 speedstep_power,
55 speedstep_stop
59 * Error returns
61 #define ESS_RET_SUCCESS 0x00
62 #define ESS_RET_NO_PM 0x01
63 #define ESS_RET_UNSUP_STATE 0x02
66 * MSR registers for changing and reading processor power state.
68 #define IA32_PERF_STAT_MSR 0x198
69 #define IA32_PERF_CTL_MSR 0x199
71 #define IA32_CPUID_TSC_CONSTANT 0xF30
72 #define IA32_MISC_ENABLE_MSR 0x1A0
73 #define IA32_MISC_ENABLE_EST (1<<16)
74 #define IA32_MISC_ENABLE_CXE (1<<25)
76 #define CPUID_TURBO_SUPPORT (1 << 1)
79 * Debugging support
81 #ifdef DEBUG
82 volatile int ess_debug = 0;
83 #define ESSDEBUG(arglist) if (ess_debug) printf arglist;
84 #else
85 #define ESSDEBUG(arglist)
86 #endif
89 * Write the ctrl register. How it is written, depends upon the _PCT
90 * APCI object value.
92 static void
93 write_ctrl(cpu_acpi_handle_t handle, uint32_t ctrl)
95 cpu_acpi_pct_t *pct_ctrl;
96 uint64_t reg;
98 pct_ctrl = CPU_ACPI_PCT_CTRL(handle);
100 switch (pct_ctrl->cr_addrspace_id) {
101 case ACPI_ADR_SPACE_FIXED_HARDWARE:
103 * Read current power state because reserved bits must be
104 * preserved, compose new value, and write it.
106 reg = rdmsr(IA32_PERF_CTL_MSR);
107 reg &= ~((uint64_t)0xFFFF);
108 reg |= ctrl;
109 wrmsr(IA32_PERF_CTL_MSR, reg);
110 break;
112 case ACPI_ADR_SPACE_SYSTEM_IO:
113 (void) cpu_acpi_write_port(pct_ctrl->cr_address, ctrl,
114 pct_ctrl->cr_width);
115 break;
117 default:
118 DTRACE_PROBE1(ess_ctrl_unsupported_type, uint8_t,
119 pct_ctrl->cr_addrspace_id);
120 return;
123 DTRACE_PROBE1(ess_ctrl_write, uint32_t, ctrl);
127 * Transition the current processor to the requested state.
129 void
130 speedstep_pstate_transition(uint32_t req_state)
132 cpupm_mach_state_t *mach_state =
133 (cpupm_mach_state_t *)CPU->cpu_m.mcpu_pm_mach_state;
134 cpu_acpi_handle_t handle = mach_state->ms_acpi_handle;
135 cpu_acpi_pstate_t *req_pstate;
136 uint32_t ctrl;
138 req_pstate = (cpu_acpi_pstate_t *)CPU_ACPI_PSTATES(handle);
139 req_pstate += req_state;
141 DTRACE_PROBE1(ess_transition, uint32_t, CPU_ACPI_FREQ(req_pstate));
144 * Initiate the processor p-state change.
146 ctrl = CPU_ACPI_PSTATE_CTRL(req_pstate);
147 write_ctrl(handle, ctrl);
149 if (mach_state->ms_turbo != NULL)
150 cpupm_record_turbo_info(mach_state->ms_turbo,
151 mach_state->ms_pstate.cma_state.pstate, req_state);
153 mach_state->ms_pstate.cma_state.pstate = req_state;
154 cpu_set_curr_clock(((uint64_t)CPU_ACPI_FREQ(req_pstate) * 1000000));
157 static void
158 speedstep_power(cpuset_t set, uint32_t req_state)
161 * If thread is already running on target CPU then just
162 * make the transition request. Otherwise, we'll need to
163 * make a cross-call.
165 kpreempt_disable();
166 if (CPU_IN_SET(set, CPU->cpu_id)) {
167 speedstep_pstate_transition(req_state);
168 CPUSET_DEL(set, CPU->cpu_id);
170 if (!CPUSET_ISNULL(set)) {
171 xc_call((xc_arg_t)req_state, (uintptr_t)NULL, (uintptr_t)NULL,
172 CPUSET2BV(set), (xc_func_t)speedstep_pstate_transition);
174 kpreempt_enable();
178 * Validate that this processor supports Speedstep and if so,
179 * get the P-state data from ACPI and cache it.
181 static int
182 speedstep_init(cpu_t *cp)
184 cpupm_mach_state_t *mach_state =
185 (cpupm_mach_state_t *)cp->cpu_m.mcpu_pm_mach_state;
186 cpu_acpi_handle_t handle = mach_state->ms_acpi_handle;
187 cpu_acpi_pct_t *pct_stat;
188 static int logged = 0;
190 ESSDEBUG(("speedstep_init: processor %d\n", cp->cpu_id));
193 * Cache the P-state specific ACPI data.
195 if (cpu_acpi_cache_pstate_data(handle) != 0) {
196 if (!logged) {
197 cmn_err(CE_NOTE, "!SpeedStep support is being "
198 "disabled due to errors parsing ACPI P-state "
199 "objects exported by BIOS.");
200 logged = 1;
202 speedstep_fini(cp);
203 return (ESS_RET_NO_PM);
206 pct_stat = CPU_ACPI_PCT_STATUS(handle);
207 switch (pct_stat->cr_addrspace_id) {
208 case ACPI_ADR_SPACE_FIXED_HARDWARE:
209 ESSDEBUG(("Transitions will use fixed hardware\n"));
210 break;
211 case ACPI_ADR_SPACE_SYSTEM_IO:
212 ESSDEBUG(("Transitions will use system IO\n"));
213 break;
214 default:
215 cmn_err(CE_WARN, "!_PCT conifgured for unsupported "
216 "addrspace = %d.", pct_stat->cr_addrspace_id);
217 cmn_err(CE_NOTE, "!CPU power management will not function.");
218 speedstep_fini(cp);
219 return (ESS_RET_NO_PM);
222 cpupm_alloc_domains(cp, CPUPM_P_STATES);
224 if (speedstep_turbo_supported())
225 mach_state->ms_turbo = cpupm_turbo_init(cp);
227 ESSDEBUG(("Processor %d succeeded.\n", cp->cpu_id))
228 return (ESS_RET_SUCCESS);
232 * Free resources allocated by speedstep_init().
234 static void
235 speedstep_fini(cpu_t *cp)
237 cpupm_mach_state_t *mach_state =
238 (cpupm_mach_state_t *)(cp->cpu_m.mcpu_pm_mach_state);
239 cpu_acpi_handle_t handle = mach_state->ms_acpi_handle;
241 cpupm_free_domains(&cpupm_pstate_domains);
242 cpu_acpi_free_pstate_data(handle);
244 if (mach_state->ms_turbo != NULL)
245 cpupm_turbo_fini(mach_state->ms_turbo);
246 mach_state->ms_turbo = NULL;
249 static void
250 speedstep_stop(cpu_t *cp)
252 cpupm_mach_state_t *mach_state =
253 (cpupm_mach_state_t *)(cp->cpu_m.mcpu_pm_mach_state);
254 cpu_acpi_handle_t handle = mach_state->ms_acpi_handle;
256 cpupm_remove_domains(cp, CPUPM_P_STATES, &cpupm_pstate_domains);
257 cpu_acpi_free_pstate_data(handle);
259 if (mach_state->ms_turbo != NULL)
260 cpupm_turbo_fini(mach_state->ms_turbo);
261 mach_state->ms_turbo = NULL;
264 boolean_t
265 speedstep_supported(uint_t family, uint_t model)
267 struct cpuid_regs cpu_regs;
269 /* Required features */
270 if (!is_x86_feature(x86_featureset, X86FSET_CPUID) ||
271 !is_x86_feature(x86_featureset, X86FSET_MSR)) {
272 return (B_FALSE);
276 * We only support family/model combinations which
277 * are P-state TSC invariant.
279 if (!((family == 0xf && model >= 0x3) ||
280 (family == 0x6 && model >= 0xe))) {
281 return (B_FALSE);
285 * Enhanced SpeedStep supported?
287 cpu_regs.cp_eax = 0x1;
288 (void) __cpuid_insn(&cpu_regs);
289 if (!(cpu_regs.cp_ecx & CPUID_INTC_ECX_EST)) {
290 return (B_FALSE);
293 return (B_TRUE);
296 boolean_t
297 speedstep_turbo_supported(void)
299 struct cpuid_regs cpu_regs;
301 /* Required features */
302 if (!is_x86_feature(x86_featureset, X86FSET_CPUID) ||
303 !is_x86_feature(x86_featureset, X86FSET_MSR)) {
304 return (B_FALSE);
308 * turbo mode supported?
310 cpu_regs.cp_eax = 0x6;
311 (void) __cpuid_insn(&cpu_regs);
312 if (!(cpu_regs.cp_eax & CPUID_TURBO_SUPPORT)) {
313 return (B_FALSE);
316 return (B_TRUE);