speedstep-ich.c 10.8 KB
Newer Older
L
Linus Torvalds 已提交
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
/*
 * (C) 2001  Dave Jones, Arjan van de ven.
 * (C) 2002 - 2003  Dominik Brodowski <linux@brodo.de>
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *  Based upon reverse engineered information, and on Intel documentation
 *  for chipsets ICH2-M and ICH3-M.
 *
 *  Many thanks to Ducrot Bruno for finding and fixing the last
 *  "missing link" for ICH2-M/ICH3-M support, and to Thomas Winkler
 *  for extensive testing.
 *
 *  BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
 */


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

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/cpufreq.h>
#include <linux/pci.h>
#include <linux/slab.h>
A
Alexey Dobriyan 已提交
27
#include <linux/sched.h>
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41

#include "speedstep-lib.h"


/* speedstep_chipset:
 *   It is necessary to know which chipset is used. As accesses to
 * this device occur at various places in this module, we need a
 * static struct pci_dev * pointing to that device.
 */
static struct pci_dev *speedstep_chipset_dev;


/* speedstep_processor
 */
42
static unsigned int speedstep_processor;
L
Linus Torvalds 已提交
43

44
static u32 pmbase;
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55 56

/*
 *   There are only two frequency states for each processor. Values
 * are in kHz for the time being.
 */
static struct cpufreq_frequency_table speedstep_freqs[] = {
	{SPEEDSTEP_HIGH,	0},
	{SPEEDSTEP_LOW,		0},
	{0,			CPUFREQ_TABLE_END},
};


57 58
#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
		"speedstep-ich", msg)
L
Linus Torvalds 已提交
59 60 61


/**
62
 * speedstep_find_register - read the PMBASE address
L
Linus Torvalds 已提交
63
 *
64
 * Returns: -ENODEV if no register could be found
L
Linus Torvalds 已提交
65
 */
66
static int speedstep_find_register(void)
L
Linus Torvalds 已提交
67
{
68 69
	if (!speedstep_chipset_dev)
		return -ENODEV;
L
Linus Torvalds 已提交
70 71 72 73 74

	/* get PMBASE */
	pci_read_config_dword(speedstep_chipset_dev, 0x40, &pmbase);
	if (!(pmbase & 0x01)) {
		printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
75
		return -ENODEV;
L
Linus Torvalds 已提交
76 77 78 79 80
	}

	pmbase &= 0xFFFFFFFE;
	if (!pmbase) {
		printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
81
		return -ENODEV;
L
Linus Torvalds 已提交
82 83
	}

84 85 86 87 88 89 90 91
	dprintk("pmbase is 0x%x\n", pmbase);
	return 0;
}

/**
 * speedstep_set_state - set the SpeedStep state
 * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
 *
92 93
 *   Tries to change the SpeedStep state.  Can be called from
 *   smp_call_function_single.
94
 */
95
static void speedstep_set_state(unsigned int state)
96 97 98 99 100 101 102 103
{
	u8 pm2_blk;
	u8 value;
	unsigned long flags;

	if (state > 0x1)
		return;

L
Linus Torvalds 已提交
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
	/* Disable IRQs */
	local_irq_save(flags);

	/* read state */
	value = inb(pmbase + 0x50);

	dprintk("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);

	/* write new state */
	value &= 0xFE;
	value |= state;

	dprintk("writing 0x%x to pmbase 0x%x + 0x50\n", value, pmbase);

	/* Disable bus master arbitration */
	pm2_blk = inb(pmbase + 0x20);
	pm2_blk |= 0x01;
	outb(pm2_blk, (pmbase + 0x20));

	/* Actual transition */
	outb(value, (pmbase + 0x50));

	/* Restore bus master arbitration */
	pm2_blk &= 0xfe;
	outb(pm2_blk, (pmbase + 0x20));

	/* check if transition was successful */
	value = inb(pmbase + 0x50);

	/* Enable IRQs */
	local_irq_restore(flags);

	dprintk("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);

138 139 140 141 142
	if (state == (value & 0x1))
		dprintk("change to %u MHz succeeded\n",
			speedstep_get_frequency(speedstep_processor) / 1000);
	else
		printk(KERN_ERR "cpufreq: change failed - I/O error\n");
L
Linus Torvalds 已提交
143 144 145 146

	return;
}

147 148 149 150 151
/* Wrapper for smp_call_function_single. */
static void _speedstep_set_state(void *_state)
{
	speedstep_set_state(*(unsigned int *)_state);
}
L
Linus Torvalds 已提交
152 153 154 155 156 157 158

/**
 * speedstep_activate - activate SpeedStep control in the chipset
 *
 *   Tries to activate the SpeedStep status and control registers.
 * Returns -EINVAL on an unsupported chipset, and zero on success.
 */
159
static int speedstep_activate(void)
L
Linus Torvalds 已提交
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
{
	u16 value = 0;

	if (!speedstep_chipset_dev)
		return -EINVAL;

	pci_read_config_word(speedstep_chipset_dev, 0x00A0, &value);
	if (!(value & 0x08)) {
		value |= 0x08;
		dprintk("activating SpeedStep (TM) registers\n");
		pci_write_config_word(speedstep_chipset_dev, 0x00A0, value);
	}

	return 0;
}


/**
 * speedstep_detect_chipset - detect the Southbridge which contains SpeedStep logic
 *
 *   Detects ICH2-M, ICH3-M and ICH4-M so far. The pci_dev points to
 * the LPC bridge / PM module which contains all power-management
 * functions. Returns the SPEEDSTEP_CHIPSET_-number for the detected
 * chipset, or zero on failure.
 */
185
static unsigned int speedstep_detect_chipset(void)
L
Linus Torvalds 已提交
186 187 188
{
	speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
			      PCI_DEVICE_ID_INTEL_82801DB_12,
189
			      PCI_ANY_ID, PCI_ANY_ID,
L
Linus Torvalds 已提交
190 191 192 193 194 195
			      NULL);
	if (speedstep_chipset_dev)
		return 4; /* 4-M */

	speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
			      PCI_DEVICE_ID_INTEL_82801CA_12,
196
			      PCI_ANY_ID, PCI_ANY_ID,
L
Linus Torvalds 已提交
197 198 199 200 201 202 203
			      NULL);
	if (speedstep_chipset_dev)
		return 3; /* 3-M */


	speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
			      PCI_DEVICE_ID_INTEL_82801BA_10,
204
			      PCI_ANY_ID, PCI_ANY_ID,
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214
			      NULL);
	if (speedstep_chipset_dev) {
		/* speedstep.c causes lockups on Dell Inspirons 8000 and
		 * 8100 which use a pretty old revision of the 82815
		 * host brige. Abort on these systems.
		 */
		static struct pci_dev *hostbridge;

		hostbridge  = pci_get_subsys(PCI_VENDOR_ID_INTEL,
			      PCI_DEVICE_ID_INTEL_82815_MC,
215
			      PCI_ANY_ID, PCI_ANY_ID,
L
Linus Torvalds 已提交
216 217 218 219 220
			      NULL);

		if (!hostbridge)
			return 2; /* 2-M */

221
		if (hostbridge->revision < 5) {
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234
			dprintk("hostbridge does not support speedstep\n");
			speedstep_chipset_dev = NULL;
			pci_dev_put(hostbridge);
			return 0;
		}

		pci_dev_put(hostbridge);
		return 2; /* 2-M */
	}

	return 0;
}

235
static void get_freq_data(void *_speed)
236
{
237
	unsigned int *speed = _speed;
238

239
	*speed = speedstep_get_frequency(speedstep_processor);
L
Linus Torvalds 已提交
240 241 242 243
}

static unsigned int speedstep_get(unsigned int cpu)
{
244
	unsigned int speed;
245 246

	/* You're supposed to ensure CPU is online. */
247
	if (smp_call_function_single(cpu, get_freq_data, &speed, 1) != 0)
248 249
		BUG();

250 251
	dprintk("detected %u kHz as current frequency\n", speed);
	return speed;
L
Linus Torvalds 已提交
252 253 254 255 256 257
}

/**
 * speedstep_target - set a new CPUFreq policy
 * @policy: new policy
 * @target_freq: the target frequency
258 259
 * @relation: how that frequency relates to achieved frequency
 *	(CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
L
Linus Torvalds 已提交
260 261 262
 *
 * Sets a new CPUFreq policy.
 */
263
static int speedstep_target(struct cpufreq_policy *policy,
L
Linus Torvalds 已提交
264 265 266
			     unsigned int target_freq,
			     unsigned int relation)
{
267
	unsigned int newstate = 0, policy_cpu;
L
Linus Torvalds 已提交
268 269 270
	struct cpufreq_freqs freqs;
	int i;

271 272
	if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0],
				target_freq, relation, &newstate))
L
Linus Torvalds 已提交
273 274
		return -EINVAL;

275 276
	policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
	freqs.old = speedstep_get(policy_cpu);
L
Linus Torvalds 已提交
277 278 279 280 281 282 283 284 285
	freqs.new = speedstep_freqs[newstate].frequency;
	freqs.cpu = policy->cpu;

	dprintk("transiting from %u to %u kHz\n", freqs.old, freqs.new);

	/* no transition necessary */
	if (freqs.old == freqs.new)
		return 0;

286
	for_each_cpu(i, policy->cpus) {
L
Linus Torvalds 已提交
287 288 289 290
		freqs.cpu = i;
		cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
	}

291 292
	smp_call_function_single(policy_cpu, _speedstep_set_state, &newstate,
				 true);
L
Linus Torvalds 已提交
293

294
	for_each_cpu(i, policy->cpus) {
L
Linus Torvalds 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
		freqs.cpu = i;
		cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
	}

	return 0;
}


/**
 * speedstep_verify - verifies a new CPUFreq policy
 * @policy: new policy
 *
 * Limit must be within speedstep_low_freq and speedstep_high_freq, with
 * at least one border included.
 */
310
static int speedstep_verify(struct cpufreq_policy *policy)
L
Linus Torvalds 已提交
311 312 313 314
{
	return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
}

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
struct get_freqs {
	struct cpufreq_policy *policy;
	int ret;
};

static void get_freqs_on_cpu(void *_get_freqs)
{
	struct get_freqs *get_freqs = _get_freqs;

	get_freqs->ret =
		speedstep_get_freqs(speedstep_processor,
			    &speedstep_freqs[SPEEDSTEP_LOW].frequency,
			    &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
			    &get_freqs->policy->cpuinfo.transition_latency,
			    &speedstep_set_state);
}
L
Linus Torvalds 已提交
331 332 333

static int speedstep_cpu_init(struct cpufreq_policy *policy)
{
334 335 336
	int result;
	unsigned int policy_cpu, speed;
	struct get_freqs gf;
L
Linus Torvalds 已提交
337 338 339

	/* only run on CPU to be set, or on its sibling */
#ifdef CONFIG_SMP
340
	cpumask_copy(policy->cpus, cpu_sibling_mask(policy->cpu));
L
Linus Torvalds 已提交
341
#endif
342
	policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
L
Linus Torvalds 已提交
343

344
	/* detect low and high frequency and transition latency */
345 346 347 348
	gf.policy = policy;
	smp_call_function_single(policy_cpu, get_freqs_on_cpu, &gf, 1);
	if (gf.ret)
		return gf.ret;
L
Linus Torvalds 已提交
349 350

	/* get current speed setting */
351
	speed = speedstep_get(policy_cpu);
L
Linus Torvalds 已提交
352 353 354 355
	if (!speed)
		return -EIO;

	dprintk("currently at %s speed setting - %i MHz\n",
356 357
		(speed == speedstep_freqs[SPEEDSTEP_LOW].frequency)
		? "low" : "high",
L
Linus Torvalds 已提交
358 359 360 361 362 363 364
		(speed / 1000));

	/* cpuinfo and default policy values */
	policy->cur = speed;

	result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
	if (result)
365
		return result;
L
Linus Torvalds 已提交
366

367
	cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376 377 378

	return 0;
}


static int speedstep_cpu_exit(struct cpufreq_policy *policy)
{
	cpufreq_frequency_table_put_attr(policy->cpu);
	return 0;
}

379
static struct freq_attr *speedstep_attr[] = {
L
Linus Torvalds 已提交
380 381 382 383 384
	&cpufreq_freq_attr_scaling_available_freqs,
	NULL,
};


385
static struct cpufreq_driver speedstep_driver = {
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	.name	= "speedstep-ich",
	.verify	= speedstep_verify,
	.target	= speedstep_target,
	.init	= speedstep_cpu_init,
	.exit	= speedstep_cpu_exit,
	.get	= speedstep_get,
	.owner	= THIS_MODULE,
	.attr	= speedstep_attr,
};


/**
 * speedstep_init - initializes the SpeedStep CPUFreq driver
 *
 *   Initializes the SpeedStep support. Returns -ENODEV on unsupported
 * devices, -EINVAL on problems during initiatization, and zero on
 * success.
 */
static int __init speedstep_init(void)
{
	/* detect processor */
	speedstep_processor = speedstep_detect_processor();
	if (!speedstep_processor) {
409 410
		dprintk("Intel(R) SpeedStep(TM) capable processor "
				"not found\n");
L
Linus Torvalds 已提交
411 412 413 414 415
		return -ENODEV;
	}

	/* detect chipset */
	if (!speedstep_detect_chipset()) {
416 417
		dprintk("Intel(R) SpeedStep(TM) for this chipset not "
				"(yet) available.\n");
L
Linus Torvalds 已提交
418 419 420 421 422 423 424 425 426
		return -ENODEV;
	}

	/* activate speedstep support */
	if (speedstep_activate()) {
		pci_dev_put(speedstep_chipset_dev);
		return -EINVAL;
	}

427 428 429
	if (speedstep_find_register())
		return -ENODEV;

L
Linus Torvalds 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	return cpufreq_register_driver(&speedstep_driver);
}


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


446 447 448 449 450
MODULE_AUTHOR("Dave Jones <davej@redhat.com>, "
		"Dominik Brodowski <linux@brodo.de>");
MODULE_DESCRIPTION("Speedstep driver for Intel mobile processors on chipsets "
		"with ICH-M southbridges.");
MODULE_LICENSE("GPL");
L
Linus Torvalds 已提交
451 452 453

module_init(speedstep_init);
module_exit(speedstep_exit);