acpi-cpufreq.c 17.2 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * acpi-cpufreq.c - ACPI Processor P-States Driver ($Revision: 1.4 $)
L
Linus Torvalds 已提交
3 4 5 6
 *
 *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
 *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
 *  Copyright (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
7
 *  Copyright (C) 2006       Denis Sadykov <denis.m.sadykov@intel.com>
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or (at
 *  your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
31 32
#include <linux/smp.h>
#include <linux/sched.h>
L
Linus Torvalds 已提交
33
#include <linux/cpufreq.h>
34
#include <linux/compiler.h>
T
Tim Schmielau 已提交
35
#include <linux/sched.h>	/* current */
36
#include <linux/dmi.h>
L
Linus Torvalds 已提交
37 38 39 40

#include <linux/acpi.h>
#include <acpi/processor.h>

41
#include <asm/io.h>
42
#include <asm/msr.h>
43 44 45 46 47
#include <asm/processor.h>
#include <asm/cpufeature.h>
#include <asm/delay.h>
#include <asm/uaccess.h>

L
Linus Torvalds 已提交
48 49 50 51 52 53
#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)

MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
MODULE_DESCRIPTION("ACPI Processor P-States Driver");
MODULE_LICENSE("GPL");

54 55 56 57 58 59 60 61
enum {
	UNDEFINED_CAPABLE = 0,
	SYSTEM_INTEL_MSR_CAPABLE,
	SYSTEM_IO_CAPABLE,
};

#define INTEL_MSR_RANGE		(0xffff)

62
struct acpi_cpufreq_data {
63 64 65 66
	struct acpi_processor_performance *acpi_data;
	struct cpufreq_frequency_table *freq_table;
	unsigned int resume;
	unsigned int cpu_feature;
L
Linus Torvalds 已提交
67 68
};

69 70
static struct acpi_cpufreq_data *drv_data[NR_CPUS];
static struct acpi_processor_performance *acpi_perf_data[NR_CPUS];
L
Linus Torvalds 已提交
71 72 73

static struct cpufreq_driver acpi_cpufreq_driver;

74 75
static unsigned int acpi_pstate_strict;

76 77 78 79 80
static int check_est_cpu(unsigned int cpuid)
{
	struct cpuinfo_x86 *cpu = &cpu_data[cpuid];

	if (cpu->x86_vendor != X86_VENDOR_INTEL ||
81
	    !cpu_has(cpu, X86_FEATURE_EST))
82 83 84 85 86 87
		return 0;

	return 1;
}

static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
88
{
89 90
	struct acpi_processor_performance *perf;
	int i;
91 92 93 94 95 96 97 98 99 100

	perf = data->acpi_data;

	for (i = 0; i < perf->state_count; i++) {
		if (value == perf->states[i].status)
			return data->freq_table[i].frequency;
	}
	return 0;
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
{
	int i;

	msr &= INTEL_MSR_RANGE;
	for (i = 0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
		if (msr == data->freq_table[i].index)
			return data->freq_table[i].frequency;
	}
	return data->freq_table[0].frequency;
}

static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
{
	switch (data->cpu_feature) {
116
	case SYSTEM_INTEL_MSR_CAPABLE:
117
		return extract_msr(val, data);
118
	case SYSTEM_IO_CAPABLE:
119
		return extract_io(val, data);
120
	default:
121 122 123 124
		return 0;
	}
}

125
static void wrport(u16 port, u8 bit_width, u32 value)
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133 134 135
{
	if (bit_width <= 8) {
		outb(value, port);
	} else if (bit_width <= 16) {
		outw(value, port);
	} else if (bit_width <= 32) {
		outl(value, port);
	}
}

136
static void rdport(u16 port, u8 bit_width, u32 * ret)
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147
{
	*ret = 0;
	if (bit_width <= 8) {
		*ret = inb(port);
	} else if (bit_width <= 16) {
		*ret = inw(port);
	} else if (bit_width <= 32) {
		*ret = inl(port);
	}
}

148 149 150 151
struct msr_addr {
	u32 reg;
};

152 153 154 155 156
struct io_addr {
	u16 port;
	u8 bit_width;
};

157 158 159 160 161
typedef union {
	struct msr_addr msr;
	struct io_addr io;
} drv_addr_union;

162
struct drv_cmd {
163
	unsigned int type;
164
	cpumask_t mask;
165
	drv_addr_union addr;
166 167 168 169
	u32 val;
};

static void do_drv_read(struct drv_cmd *cmd)
L
Linus Torvalds 已提交
170
{
171 172 173
	u32 h;

	switch (cmd->type) {
174
	case SYSTEM_INTEL_MSR_CAPABLE:
175 176
		rdmsr(cmd->addr.msr.reg, cmd->val, h);
		break;
177
	case SYSTEM_IO_CAPABLE:
178 179
		rdport(cmd->addr.io.port, cmd->addr.io.bit_width, &cmd->val);
		break;
180
	default:
181 182
		break;
	}
183
}
L
Linus Torvalds 已提交
184

185 186
static void do_drv_write(struct drv_cmd *cmd)
{
187 188 189
	u32 h = 0;

	switch (cmd->type) {
190
	case SYSTEM_INTEL_MSR_CAPABLE:
191 192
		wrmsr(cmd->addr.msr.reg, cmd->val, h);
		break;
193
	case SYSTEM_IO_CAPABLE:
194 195
		wrport(cmd->addr.io.port, cmd->addr.io.bit_width, cmd->val);
		break;
196
	default:
197 198
		break;
	}
199
}
L
Linus Torvalds 已提交
200

201 202
static inline void drv_read(struct drv_cmd *cmd)
{
203
	cpumask_t saved_mask = current->cpus_allowed;
204 205 206 207 208 209 210 211 212 213
	cmd->val = 0;

	set_cpus_allowed(current, cmd->mask);
	do_drv_read(cmd);
	set_cpus_allowed(current, saved_mask);

}

static void drv_write(struct drv_cmd *cmd)
{
214 215
	cpumask_t saved_mask = current->cpus_allowed;
	unsigned int i;
216 217 218 219

	for_each_cpu_mask(i, cmd->mask) {
		set_cpus_allowed(current, cpumask_of_cpu(i));
		do_drv_write(cmd);
L
Linus Torvalds 已提交
220 221
	}

222 223 224
	set_cpus_allowed(current, saved_mask);
	return;
}
L
Linus Torvalds 已提交
225

226 227
static u32 get_cur_val(cpumask_t mask)
{
228 229
	struct acpi_processor_performance *perf;
	struct drv_cmd cmd;
L
Linus Torvalds 已提交
230

231 232
	if (unlikely(cpus_empty(mask)))
		return 0;
L
Linus Torvalds 已提交
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	switch (drv_data[first_cpu(mask)]->cpu_feature) {
	case SYSTEM_INTEL_MSR_CAPABLE:
		cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
		cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
		break;
	case SYSTEM_IO_CAPABLE:
		cmd.type = SYSTEM_IO_CAPABLE;
		perf = drv_data[first_cpu(mask)]->acpi_data;
		cmd.addr.io.port = perf->control_register.address;
		cmd.addr.io.bit_width = perf->control_register.bit_width;
		break;
	default:
		return 0;
	}

249
	cmd.mask = mask;
L
Linus Torvalds 已提交
250

251
	drv_read(&cmd);
L
Linus Torvalds 已提交
252

253 254 255 256
	dprintk("get_cur_val = %u\n", cmd.val);

	return cmd.val;
}
L
Linus Torvalds 已提交
257

258 259
static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
{
260 261
	struct acpi_cpufreq_data *data = drv_data[cpu];
	unsigned int freq;
262 263 264 265

	dprintk("get_cur_freq_on_cpu (%d)\n", cpu);

	if (unlikely(data == NULL ||
266
		     data->acpi_data == NULL || data->freq_table == NULL)) {
267
		return 0;
L
Linus Torvalds 已提交
268 269
	}

270 271
	freq = extract_freq(get_cur_val(cpumask_of_cpu(cpu)), data);
	dprintk("cur freq = %u\n", freq);
L
Linus Torvalds 已提交
272

273
	return freq;
L
Linus Torvalds 已提交
274 275
}

276
static unsigned int check_freqs(cpumask_t mask, unsigned int freq,
277
				struct acpi_cpufreq_data *data)
278
{
279 280
	unsigned int cur_freq;
	unsigned int i;
L
Linus Torvalds 已提交
281

282 283 284 285 286 287 288 289 290 291
	for (i = 0; i < 100; i++) {
		cur_freq = extract_freq(get_cur_val(mask), data);
		if (cur_freq == freq)
			return 1;
		udelay(10);
	}
	return 0;
}

static int acpi_cpufreq_target(struct cpufreq_policy *policy,
292
			       unsigned int target_freq, unsigned int relation)
L
Linus Torvalds 已提交
293
{
294 295 296 297 298 299 300 301 302 303
	struct acpi_cpufreq_data *data = drv_data[policy->cpu];
	struct acpi_processor_performance *perf;
	struct cpufreq_freqs freqs;
	cpumask_t online_policy_cpus;
	struct drv_cmd cmd;
	unsigned int msr;
	unsigned int next_state = 0;
	unsigned int next_perf_state = 0;
	unsigned int i;
	int result = 0;
304 305 306 307

	dprintk("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);

	if (unlikely(data == NULL ||
308
		     data->acpi_data == NULL || data->freq_table == NULL)) {
309 310
		return -ENODEV;
	}
L
Linus Torvalds 已提交
311

312
	perf = data->acpi_data;
L
Linus Torvalds 已提交
313
	result = cpufreq_frequency_table_target(policy,
314 315 316
						data->freq_table,
						target_freq,
						relation, &next_state);
317
	if (unlikely(result))
318
		return -ENODEV;
319

320
#ifdef CONFIG_HOTPLUG_CPU
321 322
	/* cpufreq holds the hotplug lock, so we are safe from here on */
	cpus_and(online_policy_cpus, cpu_online_map, policy->cpus);
323 324 325
#else
	online_policy_cpus = policy->cpus;
#endif
L
Linus Torvalds 已提交
326

327 328 329 330 331 332
	cmd.val = get_cur_val(online_policy_cpus);
	freqs.old = extract_freq(cmd.val, data);
	freqs.new = data->freq_table[next_state].frequency;
	next_perf_state = data->freq_table[next_state].index;
	if (freqs.new == freqs.old) {
		if (unlikely(data->resume)) {
333 334
			dprintk("Called after resume, resetting to P%d\n",
				next_perf_state);
335 336
			data->resume = 0;
		} else {
337 338
			dprintk("Already at target state (P%d)\n",
				next_perf_state);
339 340
			return 0;
		}
341 342
	}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	switch (data->cpu_feature) {
	case SYSTEM_INTEL_MSR_CAPABLE:
		cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
		cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
		msr =
		    (u32) perf->states[next_perf_state].
		    control & INTEL_MSR_RANGE;
		cmd.val = (cmd.val & ~INTEL_MSR_RANGE) | msr;
		break;
	case SYSTEM_IO_CAPABLE:
		cmd.type = SYSTEM_IO_CAPABLE;
		cmd.addr.io.port = perf->control_register.address;
		cmd.addr.io.bit_width = perf->control_register.bit_width;
		cmd.val = (u32) perf->states[next_perf_state].control;
		break;
	default:
		return -ENODEV;
	}
361

362
	cpus_clear(cmd.mask);
363

364 365 366 367
	if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
		cmd.mask = online_policy_cpus;
	else
		cpu_set(policy->cpu, cmd.mask);
368

369 370 371
	for_each_cpu_mask(i, cmd.mask) {
		freqs.cpu = i;
		cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
372
	}
L
Linus Torvalds 已提交
373

374
	drv_write(&cmd);
375

376 377 378
	if (acpi_pstate_strict) {
		if (!check_freqs(cmd.mask, freqs.new, data)) {
			dprintk("acpi_cpufreq_target failed (%d)\n",
379
				policy->cpu);
380
			return -EAGAIN;
381 382 383
		}
	}

384 385 386 387 388 389 390
	for_each_cpu_mask(i, cmd.mask) {
		freqs.cpu = i;
		cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
	}
	perf->state = next_perf_state;

	return result;
L
Linus Torvalds 已提交
391 392
}

393
static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
L
Linus Torvalds 已提交
394
{
395
	struct acpi_cpufreq_data *data = drv_data[policy->cpu];
L
Linus Torvalds 已提交
396 397 398

	dprintk("acpi_cpufreq_verify\n");

399
	return cpufreq_frequency_table_verify(policy, data->freq_table);
L
Linus Torvalds 已提交
400 401 402
}

static unsigned long
403
acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
L
Linus Torvalds 已提交
404
{
405
	struct acpi_processor_performance *perf = data->acpi_data;
406

L
Linus Torvalds 已提交
407 408 409 410
	if (cpu_khz) {
		/* search the closest match to cpu_khz */
		unsigned int i;
		unsigned long freq;
411
		unsigned long freqn = perf->states[0].core_frequency * 1000;
L
Linus Torvalds 已提交
412

413
		for (i = 0; i < (perf->state_count - 1); i++) {
L
Linus Torvalds 已提交
414
			freq = freqn;
415
			freqn = perf->states[i + 1].core_frequency * 1000;
L
Linus Torvalds 已提交
416
			if ((2 * cpu_khz) > (freqn + freq)) {
417
				perf->state = i;
418
				return freq;
L
Linus Torvalds 已提交
419 420
			}
		}
421
		perf->state = perf->state_count - 1;
422
		return freqn;
423
	} else {
L
Linus Torvalds 已提交
424
		/* assume CPU is at P0... */
425 426 427
		perf->state = 0;
		return perf->states[0].core_frequency * 1000;
	}
L
Linus Torvalds 已提交
428 429
}

430 431 432 433 434 435 436 437
/*
 * acpi_cpufreq_early_init - initialize ACPI P-States library
 *
 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
 * in order to determine correct frequency and voltage pairings. We can
 * do _PDC and _PSD and find out the processor dependency for the
 * actual init that will happen later...
 */
438
static int acpi_cpufreq_early_init(void)
439
{
440 441 442
	struct acpi_processor_performance *data;
	cpumask_t covered;
	unsigned int i, j;
443 444 445

	dprintk("acpi_cpufreq_early_init\n");

A
Andrew Morton 已提交
446
	for_each_possible_cpu(i) {
447 448
		data = kzalloc(sizeof(struct acpi_processor_performance),
			       GFP_KERNEL);
449
		if (!data) {
450
			for_each_cpu_mask(j, covered) {
451 452 453
				kfree(acpi_perf_data[j]);
				acpi_perf_data[j] = NULL;
			}
454
			return -ENOMEM;
455 456
		}
		acpi_perf_data[i] = data;
457
		cpu_set(i, covered);
458 459 460
	}

	/* Do initialization in ACPI core */
461 462
	acpi_processor_preregister_performance(acpi_perf_data);
	return 0;
463 464
}

465 466 467 468 469 470 471 472
/*
 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
 * or do it in BIOS firmware and won't inform about it to OS. If not
 * detected, this has a side effect of making CPU run at a different speed
 * than OS intended it to run at. Detect it and handle it cleanly.
 */
static int bios_with_sw_any_bug;

473
static int sw_any_bug_found(struct dmi_system_id *d)
474 475 476 477 478
{
	bios_with_sw_any_bug = 1;
	return 0;
}

479
static struct dmi_system_id sw_any_bug_dmi_table[] = {
480 481 482 483 484 485 486 487 488 489 490 491
	{
		.callback = sw_any_bug_found,
		.ident = "Supermicro Server X6DLP",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
			DMI_MATCH(DMI_BIOS_VERSION, "080010"),
			DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
		},
	},
	{ }
};

492
static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
L
Linus Torvalds 已提交
493
{
494 495 496 497 498 499 500 501
	unsigned int i;
	unsigned int valid_states = 0;
	unsigned int cpu = policy->cpu;
	struct acpi_cpufreq_data *data;
	unsigned int l, h;
	unsigned int result = 0;
	struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
	struct acpi_processor_performance *perf;
L
Linus Torvalds 已提交
502 503 504

	dprintk("acpi_cpufreq_cpu_init\n");

505
	if (!acpi_perf_data[cpu])
506
		return -ENODEV;
507

508
	data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
L
Linus Torvalds 已提交
509
	if (!data)
510
		return -ENOMEM;
L
Linus Torvalds 已提交
511

512
	data->acpi_data = acpi_perf_data[cpu];
513
	drv_data[cpu] = data;
L
Linus Torvalds 已提交
514

515 516 517
	if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
		acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
	}
L
Linus Torvalds 已提交
518

519
	result = acpi_processor_register_performance(data->acpi_data, cpu);
L
Linus Torvalds 已提交
520 521 522
	if (result)
		goto err_free;

523 524
	perf = data->acpi_data;
	policy->shared_type = perf->shared_type;
525 526 527 528 529
	/*
	 * Will let policy->cpus know about dependency only when software 
	 * coordination is required.
	 */
	if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
530
	    policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
531
		policy->cpus = perf->shared_cpu_map;
532 533 534 535 536 537 538 539 540
	}

#ifdef CONFIG_SMP
	dmi_check_system(sw_any_bug_dmi_table);
	if (bios_with_sw_any_bug && cpus_weight(policy->cpus) == 1) {
		policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
		policy->cpus = cpu_core_map[cpu];
	}
#endif
541

L
Linus Torvalds 已提交
542
	/* capability check */
543
	if (perf->state_count <= 1) {
L
Linus Torvalds 已提交
544 545 546 547
		dprintk("No P-States\n");
		result = -ENODEV;
		goto err_unreg;
	}
548

549 550 551 552 553 554
	if (perf->control_register.space_id != perf->status_register.space_id) {
		result = -ENODEV;
		goto err_unreg;
	}

	switch (perf->control_register.space_id) {
555
	case ACPI_ADR_SPACE_SYSTEM_IO:
556
		dprintk("SYSTEM IO addr space\n");
557 558
		data->cpu_feature = SYSTEM_IO_CAPABLE;
		break;
559
	case ACPI_ADR_SPACE_FIXED_HARDWARE:
560 561 562 563 564 565
		dprintk("HARDWARE addr space\n");
		if (!check_est_cpu(cpu)) {
			result = -ENODEV;
			goto err_unreg;
		}
		data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
566
		break;
567
	default:
568
		dprintk("Unknown addr space %d\n",
569
			(u32) (perf->control_register.space_id));
L
Linus Torvalds 已提交
570 571 572 573
		result = -ENODEV;
		goto err_unreg;
	}

574 575 576
	data->freq_table =
	    kmalloc(sizeof(struct cpufreq_frequency_table) *
		    (perf->state_count + 1), GFP_KERNEL);
L
Linus Torvalds 已提交
577 578 579 580 581 582 583
	if (!data->freq_table) {
		result = -ENOMEM;
		goto err_unreg;
	}

	/* detect transition latency */
	policy->cpuinfo.transition_latency = 0;
584 585 586 587 588
	for (i = 0; i < perf->state_count; i++) {
		if ((perf->states[i].transition_latency * 1000) >
		    policy->cpuinfo.transition_latency)
			policy->cpuinfo.transition_latency =
			    perf->states[i].transition_latency * 1000;
L
Linus Torvalds 已提交
589 590 591 592
	}
	policy->governor = CPUFREQ_DEFAULT_GOVERNOR;

	/* table init */
593 594 595
	for (i = 0; i < perf->state_count; i++) {
		if (i > 0 && perf->states[i].core_frequency ==
		    perf->states[i - 1].core_frequency)
596 597 598 599
			continue;

		data->freq_table[valid_states].index = i;
		data->freq_table[valid_states].frequency =
600
		    perf->states[i].core_frequency * 1000;
601
		valid_states++;
L
Linus Torvalds 已提交
602
	}
603
	data->freq_table[perf->state_count].frequency = CPUFREQ_TABLE_END;
L
Linus Torvalds 已提交
604 605 606 607 608 609

	result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
	if (result) {
		goto err_freqfree;
	}

610
	switch (data->cpu_feature) {
611
	case ACPI_ADR_SPACE_SYSTEM_IO:
612 613 614
		/* Current speed is unknown and not detectable by IO port */
		policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
		break;
615
	case ACPI_ADR_SPACE_FIXED_HARDWARE:
616 617
		get_cur_freq_on_cpu(cpu);
		break;
618
	default:
619 620 621
		break;
	}

L
Linus Torvalds 已提交
622 623 624
	/* notify BIOS that we exist */
	acpi_processor_notify_smm(THIS_MODULE);

625
	dprintk("CPU%u - ACPI performance management activated.\n", cpu);
626
	for (i = 0; i < perf->state_count; i++)
L
Linus Torvalds 已提交
627
		dprintk("     %cP%d: %d MHz, %d mW, %d uS\n",
628
			(i == perf->state ? '*' : ' '), i,
629 630 631
			(u32) perf->states[i].core_frequency,
			(u32) perf->states[i].power,
			(u32) perf->states[i].transition_latency);
L
Linus Torvalds 已提交
632 633

	cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
634

635 636 637 638 639
	/*
	 * the first call to ->target() should result in us actually
	 * writing something to the appropriate registers.
	 */
	data->resume = 1;
640

641
	return result;
L
Linus Torvalds 已提交
642

643
      err_freqfree:
L
Linus Torvalds 已提交
644
	kfree(data->freq_table);
645
      err_unreg:
646
	acpi_processor_unregister_performance(perf, cpu);
647
      err_free:
L
Linus Torvalds 已提交
648
	kfree(data);
649
	drv_data[cpu] = NULL;
L
Linus Torvalds 已提交
650

651
	return result;
L
Linus Torvalds 已提交
652 653
}

654
static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
L
Linus Torvalds 已提交
655
{
656
	struct acpi_cpufreq_data *data = drv_data[policy->cpu];
L
Linus Torvalds 已提交
657 658 659 660 661

	dprintk("acpi_cpufreq_cpu_exit\n");

	if (data) {
		cpufreq_frequency_table_put_attr(policy->cpu);
662
		drv_data[policy->cpu] = NULL;
663 664
		acpi_processor_unregister_performance(data->acpi_data,
						      policy->cpu);
L
Linus Torvalds 已提交
665 666 667
		kfree(data);
	}

668
	return 0;
L
Linus Torvalds 已提交
669 670
}

671
static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
L
Linus Torvalds 已提交
672
{
673
	struct acpi_cpufreq_data *data = drv_data[policy->cpu];
L
Linus Torvalds 已提交
674 675 676 677 678

	dprintk("acpi_cpufreq_resume\n");

	data->resume = 1;

679
	return 0;
L
Linus Torvalds 已提交
680 681
}

682
static struct freq_attr *acpi_cpufreq_attr[] = {
L
Linus Torvalds 已提交
683 684 685 686 687
	&cpufreq_freq_attr_scaling_available_freqs,
	NULL,
};

static struct cpufreq_driver acpi_cpufreq_driver = {
688 689 690 691 692 693 694 695 696
	.verify = acpi_cpufreq_verify,
	.target = acpi_cpufreq_target,
	.get = get_cur_freq_on_cpu,
	.init = acpi_cpufreq_cpu_init,
	.exit = acpi_cpufreq_cpu_exit,
	.resume = acpi_cpufreq_resume,
	.name = "acpi-cpufreq",
	.owner = THIS_MODULE,
	.attr = acpi_cpufreq_attr,
L
Linus Torvalds 已提交
697 698
};

699
static int __init acpi_cpufreq_init(void)
L
Linus Torvalds 已提交
700 701 702
{
	dprintk("acpi_cpufreq_init\n");

703
	acpi_cpufreq_early_init();
704

705
	return cpufreq_register_driver(&acpi_cpufreq_driver);
L
Linus Torvalds 已提交
706 707
}

708
static void __exit acpi_cpufreq_exit(void)
L
Linus Torvalds 已提交
709
{
710
	unsigned int i;
L
Linus Torvalds 已提交
711 712 713 714
	dprintk("acpi_cpufreq_exit\n");

	cpufreq_unregister_driver(&acpi_cpufreq_driver);

A
Andrew Morton 已提交
715
	for_each_possible_cpu(i) {
716 717 718
		kfree(acpi_perf_data[i]);
		acpi_perf_data[i] = NULL;
	}
L
Linus Torvalds 已提交
719 720 721
	return;
}

722
module_param(acpi_pstate_strict, uint, 0644);
723 724
MODULE_PARM_DESC(acpi_pstate_strict,
		 "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
L
Linus Torvalds 已提交
725 726 727 728 729

late_initcall(acpi_cpufreq_init);
module_exit(acpi_cpufreq_exit);

MODULE_ALIAS("acpi");