speedstep-smi.c 9.55 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7 8 9 10 11 12
/*
 * Intel SpeedStep SMI driver.
 *
 * (C) 2003  Hiroshi Miura <miura@da-cha.org>
 */


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

13 14
#define pr_fmt(fmt) "cpufreq: " fmt

Linus Torvalds's avatar
Linus Torvalds committed
15
#include <linux/kernel.h>
16 17
#include <linux/module.h>
#include <linux/moduleparam.h>
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20
#include <linux/init.h>
#include <linux/cpufreq.h>
#include <linux/delay.h>
21
#include <linux/io.h>
Linus Torvalds's avatar
Linus Torvalds committed
22
#include <asm/ist.h>
23
#include <asm/cpu_device_id.h>
Linus Torvalds's avatar
Linus Torvalds committed
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
 *
Linus Torvalds's avatar
Linus Torvalds committed
32
 */
33 34 35
static int smi_port;
static int smi_cmd;
static unsigned int smi_sig;
Linus Torvalds's avatar
Linus Torvalds committed
36 37

/* info about the processor */
38
static enum speedstep_processor speedstep_processor;
Linus Torvalds's avatar
Linus Torvalds committed
39

40 41
/*
 * There are only two frequency states for each processor. Values
Linus Torvalds's avatar
Linus Torvalds committed
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},
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
63
{
64
	u32 command, result, magic, dummy;
Linus Torvalds's avatar
Linus Torvalds committed
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);
Linus Torvalds's avatar
Linus Torvalds committed
73 74

	__asm__ __volatile__(
75
		"push %%ebp\n"
Linus Torvalds's avatar
Linus Torvalds committed
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"
Linus Torvalds's avatar
Linus Torvalds committed
84 85
	);

86
	pr_debug("result is %x\n", result);
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
101
{
102
	u32 command, result = 0, edi, high_mhz, low_mhz, dummy;
103
	u32 state = 0;
Linus Torvalds's avatar
Linus Torvalds committed
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");
Linus Torvalds's avatar
Linus Torvalds committed
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);
Linus Torvalds's avatar
Linus Torvalds committed
115

116 117
	__asm__ __volatile__(
		"push %%ebp\n"
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
128 129
	);

130
	pr_debug("result %x, low_freq %u, high_freq %u\n",
131
			result, low_mhz, high_mhz);
Linus Torvalds's avatar
Linus Torvalds committed
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
}
Linus Torvalds's avatar
Linus Torvalds committed
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)
Linus Torvalds's avatar
Linus Torvalds committed
149
{
150
	unsigned int result = 0, command, new_state, dummy;
Linus Torvalds's avatar
Linus Torvalds committed
151
	unsigned long flags;
152
	unsigned int function = SET_SPEEDSTEP_STATE;
Linus Torvalds's avatar
Linus Torvalds committed
153 154 155 156 157 158
	unsigned int retry = 0;

	if (state > 0x1)
		return;

	/* Disable IRQs */
159
	preempt_disable();
Linus Torvalds's avatar
Linus Torvalds committed
160 161 162 163
	local_irq_save(flags);

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

164
	pr_debug("trying to set frequency to state %u "
165 166
		"with command %x at port %x\n",
		state, command, smi_port);
Linus Torvalds's avatar
Linus Torvalds committed
167 168 169

	do {
		if (retry) {
170 171 172 173 174 175 176 177
			/*
			 * We need to enable interrupts, otherwise the blockage
			 * won't resolve.
			 *
			 * We disable preemption so that other processes don't
			 * run. If other processes were running, they could
			 * submit more DMA requests, making the blockage worse.
			 */
178
			pr_debug("retry %u, previous result %u, waiting...\n",
179
					retry, result);
180
			local_irq_enable();
Linus Torvalds's avatar
Linus Torvalds committed
181
			mdelay(retry * 50);
182
			local_irq_disable();
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185
		}
		retry++;
		__asm__ __volatile__(
186
			"push %%ebp\n"
Linus Torvalds's avatar
Linus Torvalds committed
187
			"out %%al, (%%dx)\n"
188
			"pop %%ebp"
189 190 191 192 193
			: "=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)
Linus Torvalds's avatar
Linus Torvalds committed
194 195 196 197 198
			);
	} while ((new_state != state) && (retry <= SMI_TRIES));

	/* enable IRQs */
	local_irq_restore(flags);
199
	preempt_enable();
Linus Torvalds's avatar
Linus Torvalds committed
200

201
	if (new_state == state)
202
		pr_debug("change to %u MHz succeeded after %u tries "
203 204 205 206
			"with result %u\n",
			(speedstep_freqs[new_state].frequency / 1000),
			retry, result);
	else
207
		pr_err("change to state %u failed with new_state %u and result %u\n",
208
		       state, new_state, result);
Linus Torvalds's avatar
Linus Torvalds committed
209 210 211 212 213 214 215 216

	return;
}


/**
 * speedstep_target - set a new CPUFreq policy
 * @policy: new policy
217
 * @index: index of new freq
Linus Torvalds's avatar
Linus Torvalds committed
218 219 220
 *
 * Sets a new CPUFreq policy/freq.
 */
221
static int speedstep_target(struct cpufreq_policy *policy, unsigned int index)
Linus Torvalds's avatar
Linus Torvalds committed
222
{
223
	speedstep_set_state(index);
Linus Torvalds's avatar
Linus Torvalds committed
224 225 226 227 228 229 230 231

	return 0;
}


static int speedstep_cpu_init(struct cpufreq_policy *policy)
{
	int result;
232
	unsigned int *low, *high;
Linus Torvalds's avatar
Linus Torvalds committed
233 234 235 236 237 238 239

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

	result = speedstep_smi_ownership();
	if (result) {
240
		pr_debug("fails in acquiring ownership of a SMI interface.\n");
Linus Torvalds's avatar
Linus Torvalds committed
241 242 243 244
		return -EINVAL;
	}

	/* detect low and high frequency */
245 246 247 248
	low = &speedstep_freqs[SPEEDSTEP_LOW].frequency;
	high = &speedstep_freqs[SPEEDSTEP_HIGH].frequency;

	result = speedstep_smi_get_freqs(low, high);
Linus Torvalds's avatar
Linus Torvalds committed
249
	if (result) {
250 251
		/* fall back to speedstep_lib.c dection mechanism:
		 * try both states out */
252
		pr_debug("could not detect low and high frequencies "
253
				"by SMI call.\n");
Linus Torvalds's avatar
Linus Torvalds committed
254
		result = speedstep_get_freqs(speedstep_processor,
255
				low, high,
256
				NULL,
Linus Torvalds's avatar
Linus Torvalds committed
257 258 259
				&speedstep_set_state);

		if (result) {
260
			pr_debug("could not detect two different speeds"
261
					" -- aborting.\n");
Linus Torvalds's avatar
Linus Torvalds committed
262 263
			return result;
		} else
264
			pr_debug("workaround worked.\n");
Linus Torvalds's avatar
Linus Torvalds committed
265 266
	}

267 268 269
	policy->freq_table = speedstep_freqs;

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
270 271 272 273 274 275
}

static unsigned int speedstep_get(unsigned int cpu)
{
	if (cpu)
		return -ENODEV;
276
	return speedstep_get_frequency(speedstep_processor);
Linus Torvalds's avatar
Linus Torvalds committed
277 278 279 280 281 282 283 284
}


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

	if (result)
285
		pr_debug("fails in re-acquiring ownership of a SMI interface.\n");
Linus Torvalds's avatar
Linus Torvalds committed
286 287 288 289

	return result;
}

290
static struct cpufreq_driver speedstep_driver = {
Linus Torvalds's avatar
Linus Torvalds committed
291
	.name		= "speedstep-smi",
292
	.flags		= CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING,
293
	.verify		= cpufreq_generic_frequency_table_verify,
294
	.target_index	= speedstep_target,
Linus Torvalds's avatar
Linus Torvalds committed
295 296 297
	.init		= speedstep_cpu_init,
	.get		= speedstep_get,
	.resume		= speedstep_resume,
298
	.attr		= cpufreq_generic_attr,
Linus Torvalds's avatar
Linus Torvalds committed
299 300
};

301 302 303 304 305 306 307 308 309 310 311
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

Linus Torvalds's avatar
Linus Torvalds committed
312 313 314 315 316 317 318 319 320
/**
 * 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)
{
321 322 323
	if (!x86_match_cpu(ss_smi_ids))
		return -ENODEV;

Linus Torvalds's avatar
Linus Torvalds committed
324 325 326
	speedstep_processor = speedstep_detect_processor();

	switch (speedstep_processor) {
327 328 329
	case SPEEDSTEP_CPU_PIII_T:
	case SPEEDSTEP_CPU_PIII_C:
	case SPEEDSTEP_CPU_PIII_C_EARLY:
Linus Torvalds's avatar
Linus Torvalds committed
330 331 332 333 334 335
		break;
	default:
		speedstep_processor = 0;
	}

	if (!speedstep_processor) {
336
		pr_debug("No supported Intel CPU detected.\n");
Linus Torvalds's avatar
Linus Torvalds committed
337 338 339
		return -ENODEV;
	}

340 341
	pr_debug("signature:0x%.8x, command:0x%.8x, "
		"event:0x%.8x, perf_level:0x%.8x.\n",
342 343
		ist_info.signature, ist_info.command,
		ist_info.event, ist_info.perf_level);
Linus Torvalds's avatar
Linus Torvalds committed
344

345
	/* Error if no IST-SMI BIOS or no PARM
Linus Torvalds's avatar
Linus Torvalds committed
346
		 sig= 'ISGE' aka 'Intel Speedstep Gate E' */
347
	if ((ist_info.signature !=  0x47534943) && (
Linus Torvalds's avatar
Linus Torvalds committed
348 349 350 351 352 353 354 355 356
	    (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 */
357
	if ((smi_port > 0xff) || (smi_port < 0))
Linus Torvalds's avatar
Linus Torvalds committed
358
		return -EINVAL;
359
	else if (smi_port == 0)
Linus Torvalds's avatar
Linus Torvalds committed
360 361
		smi_port = ist_info.command & 0xff;

362
	if ((smi_cmd > 0xff) || (smi_cmd < 0))
Linus Torvalds's avatar
Linus Torvalds committed
363
		return -EINVAL;
364
	else if (smi_cmd == 0)
Linus Torvalds's avatar
Linus Torvalds committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
		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);
}

381
module_param_hw(smi_port, int, ioport, 0444);
382 383
module_param(smi_cmd,  int, 0444);
module_param(smi_sig, uint, 0444);
Linus Torvalds's avatar
Linus Torvalds committed
384

385 386 387 388 389 390
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.");
Linus Torvalds's avatar
Linus Torvalds committed
391

392 393 394
MODULE_AUTHOR("Hiroshi Miura");
MODULE_DESCRIPTION("Speedstep driver for IST applet SMI interface.");
MODULE_LICENSE("GPL");
Linus Torvalds's avatar
Linus Torvalds committed
395 396 397

module_init(speedstep_init);
module_exit(speedstep_exit);