summaryrefslogtreecommitdiff
path: root/usr/src/uts/i86pc/io/cpudrv/speedstep.c
blob: 7d9724c69e10003b8f17260be0ef51b4be54c2fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/x86_archext.h>
#include <sys/machsystm.h>
#include <sys/x_call.h>
#include <sys/acpi/acpi.h>
#include <sys/acpica.h>
#include <sys/cpudrv_mach.h>
#include <sys/speedstep.h>
#include <sys/cpu_acpi.h>
#include <sys/cpupm.h>
#include <sys/dtrace.h>
#include <sys/sdt.h>

static int speedstep_init(cpudrv_devstate_t *);
static void speedstep_fini(cpudrv_devstate_t *);
static int speedstep_power(cpudrv_devstate_t *, uint32_t);

/*
 * Interfaces for modules implementing Intel's Enhanced SpeedStep.
 */
cpudrv_pstate_ops_t speedstep_ops = {
	"Enhanced SpeedStep Technology",
	speedstep_init,
	speedstep_fini,
	speedstep_power
};

/*
 * Error returns
 */
#define	ESS_RET_SUCCESS		0x00
#define	ESS_RET_NO_PM		0x01
#define	ESS_RET_UNSUP_STATE	0x02

/*
 * Intel docs indicate that maximum latency of P-state changes should
 * be on the order of 10mS. When waiting, wait in 100uS increments.
 */
#define	ESS_MAX_LATENCY_MICROSECS	10000
#define	ESS_LATENCY_WAIT		100

/*
 * The SpeedStep related Processor Driver Capabilities (_PDC).
 * See Intel Processor Vendor-Specific ACPI Interface Specification
 * for details.
 */
#define	ESS_PDC_REVISION		0x1
#define	ESS_PDC_PS_MSR			(1<<0)
#define	ESS_PDC_IO_BEFORE_HALT		(1<<1)
#define	ESS_PDC_MP			(1<<3)
#define	ESS_PDC_PSD			(1<<5)

/*
 * MSR registers for changing and reading processor power state.
 */
#define	IA32_PERF_STAT_MSR		0x198
#define	IA32_PERF_CTL_MSR		0x199

#define	IA32_CPUID_TSC_CONSTANT		0xF30
#define	IA32_MISC_ENABLE_MSR		0x1A0
#define	IA32_MISC_ENABLE_EST		(1<<16)
#define	IA32_MISC_ENABLE_CXE		(1<<25)
/*
 * Debugging support
 */
#ifdef	DEBUG
volatile int ess_debug = 0;
#define	ESSDEBUG(arglist) if (ess_debug) printf arglist;
#else
#define	ESSDEBUG(arglist)
#endif

/*
 * Note that SpeedStep support requires the following _PDC bits be
 * enabled so that ACPI returns the proper objects. The requirement
 * that ESS_PDC_IO_BEFORE_HALT be enabled probably seems strange.
 * Unfortunately, the _PDC bit for this feature has been historically
 * misassociated with SpeedStep support and some BIOS implementations
 * erroneously check this bit when evaluating _PSS methods. Enabling
 * this bit is our only option as the likelihood of a BIOS fix on all
 * affected platforms is not very good.
 */
uint32_t ess_pdccap = ESS_PDC_PS_MSR | ESS_PDC_IO_BEFORE_HALT |
    ESS_PDC_MP | ESS_PDC_PSD;

/*
 * Read the status register. How it is read, depends upon the _PCT
 * APCI object value.
 */
static int
read_status(cpu_acpi_handle_t handle, uint32_t *stat)
{
	cpu_acpi_pct_t *pct_stat;
	uint64_t reg;
	int ret = 0;

	pct_stat = CPU_ACPI_PCT_STATUS(handle);

	switch (pct_stat->cr_addrspace_id) {
	case ACPI_ADR_SPACE_FIXED_HARDWARE:
		reg = rdmsr(IA32_PERF_STAT_MSR);
		*stat = reg & 0x1E;
		ret = 0;
		break;

	case ACPI_ADR_SPACE_SYSTEM_IO:
		ret = cpu_acpi_read_port(pct_stat->cr_address, stat,
		    pct_stat->cr_width);
		break;

	default:
		DTRACE_PROBE1(ess_status_unsupported_type, uint8_t,
		    pct_stat->cr_addrspace_id);
		return (-1);
	}

	DTRACE_PROBE1(ess_status_read, uint32_t, *stat);
	DTRACE_PROBE1(ess_status_read_err, int, ret);

	return (ret);
}

/*
 * Write the ctrl register. How it is written, depends upon the _PCT
 * APCI object value.
 */
static int
write_ctrl(cpu_acpi_handle_t handle, uint32_t ctrl)
{
	cpu_acpi_pct_t *pct_ctrl;
	uint64_t reg;
	int ret = 0;

	pct_ctrl = CPU_ACPI_PCT_CTRL(handle);

	switch (pct_ctrl->cr_addrspace_id) {
	case ACPI_ADR_SPACE_FIXED_HARDWARE:
		/*
		 * Read current power state because reserved bits must be
		 * preserved, compose new value, and write it.
		 */
		reg = rdmsr(IA32_PERF_CTL_MSR);
		reg &= ~((uint64_t)0xFFFF);
		reg |= ctrl;
		wrmsr(IA32_PERF_CTL_MSR, reg);
		ret = 0;
		break;

	case ACPI_ADR_SPACE_SYSTEM_IO:
		ret = cpu_acpi_write_port(pct_ctrl->cr_address, ctrl,
		    pct_ctrl->cr_width);
		break;

	default:
		DTRACE_PROBE1(ess_ctrl_unsupported_type, uint8_t,
		    pct_ctrl->cr_addrspace_id);
		return (-1);
	}

	DTRACE_PROBE1(ess_ctrl_write, uint32_t, ctrl);
	DTRACE_PROBE1(ess_ctrl_write_err, int, ret);

	return (ret);
}

/*
 * Transition the current processor to the requested state.
 */
void
speedstep_pstate_transition(int *ret, cpudrv_devstate_t *cpudsp,
    uint32_t req_state)
{
	cpudrv_mach_state_t *mach_state = cpudsp->mach_state;
	cpu_acpi_handle_t handle = mach_state->acpi_handle;
	cpu_acpi_pstate_t *req_pstate;
	uint32_t ctrl;
	uint32_t stat;
	int i;

	req_pstate = (cpu_acpi_pstate_t *)CPU_ACPI_PSTATES(handle);
	req_pstate += req_state;
	DTRACE_PROBE1(ess_transition, uint32_t, CPU_ACPI_FREQ(req_pstate));

	/*
	 * Initiate the processor p-state change.
	 */
	ctrl = CPU_ACPI_PSTATE_CTRL(req_pstate);
	if (write_ctrl(handle, ctrl) != 0) {
		*ret = ESS_RET_UNSUP_STATE;
		return;
	}

	/* Wait until switch is complete, but bound the loop just in case. */
	for (i = CPU_ACPI_PSTATE_TRANSLAT(req_pstate) * 2; i >= 0;
	    i -= ESS_LATENCY_WAIT) {
		if (read_status(handle, &stat) == 0 &&
		    CPU_ACPI_PSTATE_STAT(req_pstate) == stat)
			break;
		drv_usecwait(ESS_LATENCY_WAIT);
	}
	if (i >= ESS_MAX_LATENCY_MICROSECS) {
		DTRACE_PROBE(ess_transition_incomplete);
	}

	mach_state->pstate = req_state;
	CPU->cpu_curr_clock =
	    (((uint64_t)CPU_ACPI_FREQ(req_pstate) * 1000000));
	*ret = ESS_RET_SUCCESS;
}

static int
speedstep_power(cpudrv_devstate_t *cpudsp, uint32_t req_state)
{
	cpuset_t cpus;
	int ret;

	CPUSET_ONLY(cpus, cpudsp->cpu_id);

	kpreempt_disable();
	xc_call((xc_arg_t)&ret, (xc_arg_t)cpudsp, (xc_arg_t)req_state,
	    X_CALL_HIPRI, cpus, (xc_func_t)speedstep_pstate_transition);
	kpreempt_enable();

	return (ret);
}

/*
 * Validate that this processor supports Speedstep and if so,
 * get the P-state data from ACPI and cache it.
 */
static int
speedstep_init(cpudrv_devstate_t *cpudsp)
{
	cpudrv_mach_state_t *mach_state = cpudsp->mach_state;
	cpu_acpi_handle_t handle = mach_state->acpi_handle;
	cpu_acpi_pct_t *pct_stat;
	cpu_t *cp;
	int dependency;

	ESSDEBUG(("speedstep_init: instance %d\n",
	    ddi_get_instance(cpudsp->dip)));

	/*
	 * Cache the P-state specific ACPI data.
	 */
	if (cpu_acpi_cache_pstate_data(handle) != 0) {
		ESSDEBUG(("Failed to cache ACPI data\n"));
		speedstep_fini(cpudsp);
		return (ESS_RET_NO_PM);
	}

	pct_stat = CPU_ACPI_PCT_STATUS(handle);
	switch (pct_stat->cr_addrspace_id) {
	case ACPI_ADR_SPACE_FIXED_HARDWARE:
		ESSDEBUG(("Transitions will use fixed hardware\n"));
		break;
	case ACPI_ADR_SPACE_SYSTEM_IO:
		ESSDEBUG(("Transitions will use system IO\n"));
		break;
	default:
		cmn_err(CE_WARN, "!_PCT conifgured for unsupported "
		    "addrspace = %d.", pct_stat->cr_addrspace_id);
		cmn_err(CE_NOTE, "!CPU power management will not function.");
		speedstep_fini(cpudsp);
		return (ESS_RET_NO_PM);
	}

	if (CPU_ACPI_IS_OBJ_CACHED(handle, CPU_ACPI_PSD_CACHED))
		dependency = CPU_ACPI_PSD(handle).sd_domain;
	else {
		mutex_enter(&cpu_lock);
		cp = cpu[CPU->cpu_id];
		dependency = cpuid_get_chipid(cp);
		mutex_exit(&cpu_lock);
	}
	cpupm_add_cpu2dependency(cpudsp->dip, dependency);

	ESSDEBUG(("Instance %d succeeded.\n", ddi_get_instance(cpudsp->dip)));
	return (ESS_RET_SUCCESS);
}

/*
 * Free resources allocated by speedstep_init().
 */
static void
speedstep_fini(cpudrv_devstate_t *cpudsp)
{
	cpudrv_mach_state_t *mach_state = cpudsp->mach_state;
	cpu_acpi_handle_t handle = mach_state->acpi_handle;

	cpupm_free_cpu_dependencies();
	cpu_acpi_free_pstate_data(handle);
}

boolean_t
speedstep_supported(uint_t family, uint_t model)
{
	struct cpuid_regs cpu_regs;
	uint64_t reg;

	/* Required features */
	if (!(x86_feature & X86_CPUID) ||
	    !(x86_feature & X86_MSR)) {
		return (B_FALSE);
	}

	/*
	 * We only support family/model combinations which
	 * are P-state TSC invariant.
	 */
	if (!((family == 0xf && model >= 0x3) ||
	    (family == 0x6 && model >= 0xe))) {
		return (B_FALSE);
	}

	/*
	 * Enhanced SpeedStep supported?
	 */
	cpu_regs.cp_eax = 0x1;
	(void) __cpuid_insn(&cpu_regs);
	if (!(cpu_regs.cp_ecx & CPUID_INTC_ECX_EST)) {
		return (B_FALSE);
	}

	/*
	 * If Enhanced SpeedStep has not been enabled on the system,
	 * then we probably should not override the BIOS setting.
	 */
	reg = rdmsr(IA32_MISC_ENABLE_MSR);
	if (! (reg & IA32_MISC_ENABLE_EST)) {
		cmn_err(CE_NOTE, "!Enhanced Intel SpeedStep not enabled.");
		cmn_err(CE_NOTE, "!CPU power management will not function.");
		return (B_FALSE);
	}

	return (B_TRUE);
}