speedstep-smi.c 9.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Intel SpeedStep SMI driver.
 *
 * (C) 2003  Hiroshi Miura <miura@da-cha.org>
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */


/*********************************************************************
 *                        SPEEDSTEP - DEFINITIONS                    *
 *********************************************************************/

#include <linux/kernel.h>
16 17
#include <linux/module.h>
#include <linux/moduleparam.h>
L
Linus Torvalds 已提交
18 19 20
#include <linux/init.h>
#include <linux/cpufreq.h>
#include <linux/delay.h>
21
#include <linux/io.h>
L
Linus Torvalds 已提交
22
#include <asm/ist.h>
23
#include <asm/cpu_device_id.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29 30

#include "speedstep-lib.h"

/* speedstep system management interface port/command.
 *
 * These parameters are got from IST-SMI BIOS call.
 * If user gives it, these are used.
31
 *
L
Linus Torvalds 已提交
32
 */
33 34 35
static int smi_port;
static int smi_cmd;
static unsigned int smi_sig;
L
Linus Torvalds 已提交
36 37

/* info about the processor */
38
static enum speedstep_processor speedstep_processor;
L
Linus Torvalds 已提交
39

40 41
/*
 * There are only two frequency states for each processor. Values
L
Linus Torvalds 已提交
42 43 44
 * are in kHz for the time being.
 */
static struct cpufreq_frequency_table speedstep_freqs[] = {
45 46 47
	{0, SPEEDSTEP_HIGH,	0},
	{0, SPEEDSTEP_LOW,	0},
	{0, 0,			CPUFREQ_TABLE_END},
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
};

#define GET_SPEEDSTEP_OWNER 0
#define GET_SPEEDSTEP_STATE 1
#define SET_SPEEDSTEP_STATE 2
#define GET_SPEEDSTEP_FREQS 4

/* how often shall the SMI call be tried if it failed, e.g. because
 * of DMA activity going on? */
#define SMI_TRIES 5

/**
 * speedstep_smi_ownership
 */
62
static int speedstep_smi_ownership(void)
L
Linus Torvalds 已提交
63
{
64
	u32 command, result, magic, dummy;
L
Linus Torvalds 已提交
65 66 67 68 69 70
	u32 function = GET_SPEEDSTEP_OWNER;
	unsigned char magic_data[] = "Copyright (c) 1999 Intel Corporation";

	command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
	magic = virt_to_phys(magic_data);

71
	pr_debug("trying to obtain ownership with command %x at port %x\n",
72
			command, smi_port);
L
Linus Torvalds 已提交
73 74

	__asm__ __volatile__(
75
		"push %%ebp\n"
L
Linus Torvalds 已提交
76
		"out %%al, (%%dx)\n"
77
		"pop %%ebp\n"
78 79 80
		: "=D" (result),
		  "=a" (dummy), "=b" (dummy), "=c" (dummy), "=d" (dummy),
		  "=S" (dummy)
81
		: "a" (command), "b" (function), "c" (0), "d" (smi_port),
82
		  "D" (0), "S" (magic)
83
		: "memory"
L
Linus Torvalds 已提交
84 85
	);

86
	pr_debug("result is %x\n", result);
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99

	return result;
}

/**
 * speedstep_smi_get_freqs - get SpeedStep preferred & current freq.
 * @low: the low frequency value is placed here
 * @high: the high frequency value is placed here
 *
 * Only available on later SpeedStep-enabled systems, returns false results or
 * even hangs [cf. bugme.osdl.org # 1422] on earlier systems. Empirical testing
 * shows that the latter occurs if !(ist_info.event & 0xFFFF).
 */
100
static int speedstep_smi_get_freqs(unsigned int *low, unsigned int *high)
L
Linus Torvalds 已提交
101
{
102
	u32 command, result = 0, edi, high_mhz, low_mhz, dummy;
103
	u32 state = 0;
L
Linus Torvalds 已提交
104 105 106
	u32 function = GET_SPEEDSTEP_FREQS;

	if (!(ist_info.event & 0xFFFF)) {
107
		pr_debug("bug #1422 -- can't read freqs from BIOS\n");
L
Linus Torvalds 已提交
108 109 110 111 112
		return -ENODEV;
	}

	command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);

113
	pr_debug("trying to determine frequencies with command %x at port %x\n",
114
			command, smi_port);
L
Linus Torvalds 已提交
115

116 117
	__asm__ __volatile__(
		"push %%ebp\n"
L
Linus Torvalds 已提交
118
		"out %%al, (%%dx)\n"
119
		"pop %%ebp"
120 121 122 123 124 125 126 127
		: "=a" (result),
		  "=b" (high_mhz),
		  "=c" (low_mhz),
		  "=d" (state), "=D" (edi), "=S" (dummy)
		: "a" (command),
		  "b" (function),
		  "c" (state),
		  "d" (smi_port), "S" (0), "D" (0)
L
Linus Torvalds 已提交
128 129
	);

130
	pr_debug("result %x, low_freq %u, high_freq %u\n",
131
			result, low_mhz, high_mhz);
L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139 140

	/* abort if results are obviously incorrect... */
	if ((high_mhz + low_mhz) < 600)
		return -EINVAL;

	*high = high_mhz * 1000;
	*low  = low_mhz  * 1000;

	return result;
141
}
L
Linus Torvalds 已提交
142 143 144 145 146 147

/**
 * speedstep_set_state - set the SpeedStep state
 * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
 *
 */
148
static void speedstep_set_state(unsigned int state)
L
Linus Torvalds 已提交
149
{
150
	unsigned int result = 0, command, new_state, dummy;
L
Linus Torvalds 已提交
151
	unsigned long flags;
152
	unsigned int function = SET_SPEEDSTEP_STATE;
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162
	unsigned int retry = 0;

	if (state > 0x1)
		return;

	/* Disable IRQs */
	local_irq_save(flags);

	command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);

163
	pr_debug("trying to set frequency to state %u "
164 165
		"with command %x at port %x\n",
		state, command, smi_port);
L
Linus Torvalds 已提交
166 167 168

	do {
		if (retry) {
169
			pr_debug("retry %u, previous result %u, waiting...\n",
170
					retry, result);
L
Linus Torvalds 已提交
171 172 173 174
			mdelay(retry * 50);
		}
		retry++;
		__asm__ __volatile__(
175
			"push %%ebp\n"
L
Linus Torvalds 已提交
176
			"out %%al, (%%dx)\n"
177
			"pop %%ebp"
178 179 180 181 182
			: "=b" (new_state), "=D" (result),
			  "=c" (dummy), "=a" (dummy),
			  "=d" (dummy), "=S" (dummy)
			: "a" (command), "b" (function), "c" (state),
			  "d" (smi_port), "S" (0), "D" (0)
L
Linus Torvalds 已提交
183 184 185 186 187 188
			);
	} while ((new_state != state) && (retry <= SMI_TRIES));

	/* enable IRQs */
	local_irq_restore(flags);

189
	if (new_state == state)
190
		pr_debug("change to %u MHz succeeded after %u tries "
191 192 193 194 195 196 197
			"with result %u\n",
			(speedstep_freqs[new_state].frequency / 1000),
			retry, result);
	else
		printk(KERN_ERR "cpufreq: change to state %u "
			"failed with new_state %u and result %u\n",
			state, new_state, result);
L
Linus Torvalds 已提交
198 199 200 201 202 203 204 205

	return;
}


/**
 * speedstep_target - set a new CPUFreq policy
 * @policy: new policy
206
 * @index: index of new freq
L
Linus Torvalds 已提交
207 208 209
 *
 * Sets a new CPUFreq policy/freq.
 */
210
static int speedstep_target(struct cpufreq_policy *policy, unsigned int index)
L
Linus Torvalds 已提交
211
{
212
	speedstep_set_state(index);
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220

	return 0;
}


static int speedstep_cpu_init(struct cpufreq_policy *policy)
{
	int result;
221
	unsigned int *low, *high;
L
Linus Torvalds 已提交
222 223 224 225 226 227 228

	/* capability check */
	if (policy->cpu != 0)
		return -ENODEV;

	result = speedstep_smi_ownership();
	if (result) {
229
		pr_debug("fails in acquiring ownership of a SMI interface.\n");
L
Linus Torvalds 已提交
230 231 232 233
		return -EINVAL;
	}

	/* detect low and high frequency */
234 235 236 237
	low = &speedstep_freqs[SPEEDSTEP_LOW].frequency;
	high = &speedstep_freqs[SPEEDSTEP_HIGH].frequency;

	result = speedstep_smi_get_freqs(low, high);
L
Linus Torvalds 已提交
238
	if (result) {
239 240
		/* fall back to speedstep_lib.c dection mechanism:
		 * try both states out */
241
		pr_debug("could not detect low and high frequencies "
242
				"by SMI call.\n");
L
Linus Torvalds 已提交
243
		result = speedstep_get_freqs(speedstep_processor,
244
				low, high,
245
				NULL,
L
Linus Torvalds 已提交
246 247 248
				&speedstep_set_state);

		if (result) {
249
			pr_debug("could not detect two different speeds"
250
					" -- aborting.\n");
L
Linus Torvalds 已提交
251 252
			return result;
		} else
253
			pr_debug("workaround worked.\n");
L
Linus Torvalds 已提交
254 255 256
	}

	policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
257
	return cpufreq_table_validate_and_show(policy, speedstep_freqs);
L
Linus Torvalds 已提交
258 259 260 261 262 263
}

static unsigned int speedstep_get(unsigned int cpu)
{
	if (cpu)
		return -ENODEV;
264
	return speedstep_get_frequency(speedstep_processor);
L
Linus Torvalds 已提交
265 266 267 268 269 270 271 272
}


static int speedstep_resume(struct cpufreq_policy *policy)
{
	int result = speedstep_smi_ownership();

	if (result)
273
		pr_debug("fails in re-acquiring ownership of a SMI interface.\n");
L
Linus Torvalds 已提交
274 275 276 277

	return result;
}

278
static struct cpufreq_driver speedstep_driver = {
L
Linus Torvalds 已提交
279
	.name		= "speedstep-smi",
280
	.verify		= cpufreq_generic_frequency_table_verify,
281
	.target_index	= speedstep_target,
L
Linus Torvalds 已提交
282 283 284
	.init		= speedstep_cpu_init,
	.get		= speedstep_get,
	.resume		= speedstep_resume,
285
	.attr		= cpufreq_generic_attr,
L
Linus Torvalds 已提交
286 287
};

288 289 290 291 292 293 294 295 296 297 298
static const struct x86_cpu_id ss_smi_ids[] = {
	{ X86_VENDOR_INTEL, 6, 0xb, },
	{ X86_VENDOR_INTEL, 6, 0x8, },
	{ X86_VENDOR_INTEL, 15, 2 },
	{}
};
#if 0
/* Not auto loaded currently */
MODULE_DEVICE_TABLE(x86cpu, ss_smi_ids);
#endif

L
Linus Torvalds 已提交
299 300 301 302 303 304 305 306 307
/**
 * speedstep_init - initializes the SpeedStep CPUFreq driver
 *
 *   Initializes the SpeedStep support. Returns -ENODEV on unsupported
 * BIOS, -EINVAL on problems during initiatization, and zero on
 * success.
 */
static int __init speedstep_init(void)
{
308 309 310
	if (!x86_match_cpu(ss_smi_ids))
		return -ENODEV;

L
Linus Torvalds 已提交
311 312 313
	speedstep_processor = speedstep_detect_processor();

	switch (speedstep_processor) {
314 315 316
	case SPEEDSTEP_CPU_PIII_T:
	case SPEEDSTEP_CPU_PIII_C:
	case SPEEDSTEP_CPU_PIII_C_EARLY:
L
Linus Torvalds 已提交
317 318 319 320 321 322
		break;
	default:
		speedstep_processor = 0;
	}

	if (!speedstep_processor) {
323
		pr_debug("No supported Intel CPU detected.\n");
L
Linus Torvalds 已提交
324 325 326
		return -ENODEV;
	}

327 328
	pr_debug("signature:0x%.8ulx, command:0x%.8ulx, "
		"event:0x%.8ulx, perf_level:0x%.8ulx.\n",
329 330
		ist_info.signature, ist_info.command,
		ist_info.event, ist_info.perf_level);
L
Linus Torvalds 已提交
331

332
	/* Error if no IST-SMI BIOS or no PARM
L
Linus Torvalds 已提交
333
		 sig= 'ISGE' aka 'Intel Speedstep Gate E' */
334
	if ((ist_info.signature !=  0x47534943) && (
L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342 343
	    (smi_port == 0) || (smi_cmd == 0)))
		return -ENODEV;

	if (smi_sig == 1)
		smi_sig = 0x47534943;
	else
		smi_sig = ist_info.signature;

	/* setup smi_port from MODLULE_PARM or BIOS */
344
	if ((smi_port > 0xff) || (smi_port < 0))
L
Linus Torvalds 已提交
345
		return -EINVAL;
346
	else if (smi_port == 0)
L
Linus Torvalds 已提交
347 348
		smi_port = ist_info.command & 0xff;

349
	if ((smi_cmd > 0xff) || (smi_cmd < 0))
L
Linus Torvalds 已提交
350
		return -EINVAL;
351
	else if (smi_cmd == 0)
L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
		smi_cmd = (ist_info.command >> 16) & 0xff;

	return cpufreq_register_driver(&speedstep_driver);
}


/**
 * speedstep_exit - unregisters SpeedStep support
 *
 *   Unregisters SpeedStep support.
 */
static void __exit speedstep_exit(void)
{
	cpufreq_unregister_driver(&speedstep_driver);
}

368 369 370
module_param(smi_port, int, 0444);
module_param(smi_cmd,  int, 0444);
module_param(smi_sig, uint, 0444);
L
Linus Torvalds 已提交
371

372 373 374 375 376 377
MODULE_PARM_DESC(smi_port, "Override the BIOS-given IST port with this value "
		"-- Intel's default setting is 0xb2");
MODULE_PARM_DESC(smi_cmd, "Override the BIOS-given IST command with this value "
		"-- Intel's default setting is 0x82");
MODULE_PARM_DESC(smi_sig, "Set to 1 to fake the IST signature when using the "
		"SMI interface.");
L
Linus Torvalds 已提交
378

379 380 381
MODULE_AUTHOR("Hiroshi Miura");
MODULE_DESCRIPTION("Speedstep driver for IST applet SMI interface.");
MODULE_LICENSE("GPL");
L
Linus Torvalds 已提交
382 383 384

module_init(speedstep_init);
module_exit(speedstep_exit);