speedstep-ich.c 9.14 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 13 14 15 16 17 18 19 20
/*
 * (C) 2001  Dave Jones, Arjan van de ven.
 * (C) 2002 - 2003  Dominik Brodowski <linux@brodo.de>
 *
 *  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                    *
 *********************************************************************/

21 22
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

Linus Torvalds's avatar
Linus Torvalds committed
23 24 25 26 27
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/cpufreq.h>
#include <linux/pci.h>
28
#include <linux/sched.h>
Linus Torvalds's avatar
Linus Torvalds committed
29

30 31
#include <asm/cpu_device_id.h>

Linus Torvalds's avatar
Linus Torvalds committed
32 33 34 35 36 37 38 39 40 41 42 43 44
#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
 */
45
static enum speedstep_processor speedstep_processor;
Linus Torvalds's avatar
Linus Torvalds committed
46

47
static u32 pmbase;
Linus Torvalds's avatar
Linus Torvalds committed
48 49 50 51 52 53

/*
 *   There are only two frequency states for each processor. Values
 * are in kHz for the time being.
 */
static struct cpufreq_frequency_table speedstep_freqs[] = {
54 55 56
	{0, SPEEDSTEP_HIGH,	0},
	{0, SPEEDSTEP_LOW,	0},
	{0, 0,			CPUFREQ_TABLE_END},
Linus Torvalds's avatar
Linus Torvalds committed
57 58 59 60
};


/**
61
 * speedstep_find_register - read the PMBASE address
Linus Torvalds's avatar
Linus Torvalds committed
62
 *
63
 * Returns: -ENODEV if no register could be found
Linus Torvalds's avatar
Linus Torvalds committed
64
 */
65
static int speedstep_find_register(void)
Linus Torvalds's avatar
Linus Torvalds committed
66
{
67 68
	if (!speedstep_chipset_dev)
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
69 70 71 72

	/* get PMBASE */
	pci_read_config_dword(speedstep_chipset_dev, 0x40, &pmbase);
	if (!(pmbase & 0x01)) {
73
		pr_err("could not find speedstep register\n");
74
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77 78
	}

	pmbase &= 0xFFFFFFFE;
	if (!pmbase) {
79
		pr_err("could not find speedstep register\n");
80
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
81 82
	}

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

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

	if (state > 0x1)
		return;

Linus Torvalds's avatar
Linus Torvalds committed
103 104 105 106 107 108
	/* Disable IRQs */
	local_irq_save(flags);

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

109
	pr_debug("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
Linus Torvalds's avatar
Linus Torvalds committed
110 111 112 113 114

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

115
	pr_debug("writing 0x%x to pmbase 0x%x + 0x50\n", value, pmbase);
Linus Torvalds's avatar
Linus Torvalds committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

	/* 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);

135
	pr_debug("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
Linus Torvalds's avatar
Linus Torvalds committed
136

137
	if (state == (value & 0x1))
138
		pr_debug("change to %u MHz succeeded\n",
139 140
			speedstep_get_frequency(speedstep_processor) / 1000);
	else
141
		pr_err("change failed - I/O error\n");
Linus Torvalds's avatar
Linus Torvalds committed
142 143 144 145

	return;
}

146 147 148 149 150
/* Wrapper for smp_call_function_single. */
static void _speedstep_set_state(void *_state)
{
	speedstep_set_state(*(unsigned int *)_state);
}
Linus Torvalds's avatar
Linus Torvalds committed
151 152 153 154 155 156 157

/**
 * 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.
 */
158
static int speedstep_activate(void)
Linus Torvalds's avatar
Linus Torvalds committed
159 160 161 162 163 164 165 166 167
{
	u16 value = 0;

	if (!speedstep_chipset_dev)
		return -EINVAL;

	pci_read_config_word(speedstep_chipset_dev, 0x00A0, &value);
	if (!(value & 0x08)) {
		value |= 0x08;
168
		pr_debug("activating SpeedStep (TM) registers\n");
Linus Torvalds's avatar
Linus Torvalds committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
		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.
 */
184
static unsigned int speedstep_detect_chipset(void)
Linus Torvalds's avatar
Linus Torvalds committed
185 186 187
{
	speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
			      PCI_DEVICE_ID_INTEL_82801DB_12,
188
			      PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds's avatar
Linus Torvalds committed
189 190 191 192 193 194
			      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,
195
			      PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds's avatar
Linus Torvalds committed
196 197 198 199 200 201 202
			      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,
203
			      PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds's avatar
Linus Torvalds committed
204 205 206 207
			      NULL);
	if (speedstep_chipset_dev) {
		/* speedstep.c causes lockups on Dell Inspirons 8000 and
		 * 8100 which use a pretty old revision of the 82815
208
		 * host bridge. Abort on these systems.
Linus Torvalds's avatar
Linus Torvalds committed
209
		 */
210
		struct pci_dev *hostbridge;
Linus Torvalds's avatar
Linus Torvalds committed
211 212 213

		hostbridge  = pci_get_subsys(PCI_VENDOR_ID_INTEL,
			      PCI_DEVICE_ID_INTEL_82815_MC,
214
			      PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds's avatar
Linus Torvalds committed
215 216 217 218 219
			      NULL);

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

220
		if (hostbridge->revision < 5) {
221
			pr_debug("hostbridge does not support speedstep\n");
Linus Torvalds's avatar
Linus Torvalds committed
222 223 224 225 226 227 228 229 230 231 232 233
			speedstep_chipset_dev = NULL;
			pci_dev_put(hostbridge);
			return 0;
		}

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

	return 0;
}

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

238
	*speed = speedstep_get_frequency(speedstep_processor);
Linus Torvalds's avatar
Linus Torvalds committed
239 240 241 242
}

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

	/* You're supposed to ensure CPU is online. */
246
	BUG_ON(smp_call_function_single(cpu, get_freq_data, &speed, 1));
247

248
	pr_debug("detected %u kHz as current frequency\n", speed);
249
	return speed;
Linus Torvalds's avatar
Linus Torvalds committed
250 251 252 253 254
}

/**
 * speedstep_target - set a new CPUFreq policy
 * @policy: new policy
255
 * @index: index of target frequency
Linus Torvalds's avatar
Linus Torvalds committed
256 257 258
 *
 * Sets a new CPUFreq policy.
 */
259
static int speedstep_target(struct cpufreq_policy *policy, unsigned int index)
Linus Torvalds's avatar
Linus Torvalds committed
260
{
261
	unsigned int policy_cpu;
Linus Torvalds's avatar
Linus Torvalds committed
262

263
	policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
Linus Torvalds's avatar
Linus Torvalds committed
264

265
	smp_call_function_single(policy_cpu, _speedstep_set_state, &index,
266
				 true);
Linus Torvalds's avatar
Linus Torvalds committed
267 268 269 270 271

	return 0;
}


272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
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);
}
Linus Torvalds's avatar
Linus Torvalds committed
288 289 290

static int speedstep_cpu_init(struct cpufreq_policy *policy)
{
291
	unsigned int policy_cpu;
292
	struct get_freqs gf;
Linus Torvalds's avatar
Linus Torvalds committed
293 294 295

	/* only run on CPU to be set, or on its sibling */
#ifdef CONFIG_SMP
296
	cpumask_copy(policy->cpus, topology_sibling_cpumask(policy->cpu));
Linus Torvalds's avatar
Linus Torvalds committed
297
#endif
298
	policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
Linus Torvalds's avatar
Linus Torvalds committed
299

300
	/* detect low and high frequency and transition latency */
301 302 303 304
	gf.policy = policy;
	smp_call_function_single(policy_cpu, get_freqs_on_cpu, &gf, 1);
	if (gf.ret)
		return gf.ret;
Linus Torvalds's avatar
Linus Torvalds committed
305

306 307 308
	policy->freq_table = speedstep_freqs;

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
309 310 311
}


312
static struct cpufreq_driver speedstep_driver = {
Linus Torvalds's avatar
Linus Torvalds committed
313
	.name	= "speedstep-ich",
314
	.verify	= cpufreq_generic_frequency_table_verify,
315
	.target_index = speedstep_target,
Linus Torvalds's avatar
Linus Torvalds committed
316 317
	.init	= speedstep_cpu_init,
	.get	= speedstep_get,
318
	.attr	= cpufreq_generic_attr,
Linus Torvalds's avatar
Linus Torvalds committed
319 320
};

321 322 323 324 325 326 327 328 329 330
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
/* Autoload or not? Do not for now. */
MODULE_DEVICE_TABLE(x86cpu, ss_smi_ids);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
331 332 333 334 335 336 337 338 339 340

/**
 * 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)
{
341 342 343
	if (!x86_match_cpu(ss_smi_ids))
		return -ENODEV;

Linus Torvalds's avatar
Linus Torvalds committed
344 345 346
	/* detect processor */
	speedstep_processor = speedstep_detect_processor();
	if (!speedstep_processor) {
347
		pr_debug("Intel(R) SpeedStep(TM) capable processor "
348
				"not found\n");
Linus Torvalds's avatar
Linus Torvalds committed
349 350 351 352 353
		return -ENODEV;
	}

	/* detect chipset */
	if (!speedstep_detect_chipset()) {
354
		pr_debug("Intel(R) SpeedStep(TM) for this chipset not "
355
				"(yet) available.\n");
Linus Torvalds's avatar
Linus Torvalds committed
356 357 358 359 360 361 362 363 364
		return -ENODEV;
	}

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

365 366 367
	if (speedstep_find_register())
		return -ENODEV;

Linus Torvalds's avatar
Linus Torvalds committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	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);
}


384
MODULE_AUTHOR("Dave Jones, Dominik Brodowski <linux@brodo.de>");
385 386 387
MODULE_DESCRIPTION("Speedstep driver for Intel mobile processors on chipsets "
		"with ICH-M southbridges.");
MODULE_LICENSE("GPL");
Linus Torvalds's avatar
Linus Torvalds committed
388 389 390

module_init(speedstep_init);
module_exit(speedstep_exit);