1 /*
2  * acpi-cpufreq.c - ACPI Processor P-States Driver
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *  Copyright (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
7  *  Copyright (C) 2006       Denis Sadykov <denis.m.sadykov@intel.com>
8  *
9  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or (at
14  *  your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License along
22  *  with this program; if not, write to the Free Software Foundation, Inc.,
23  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24  *
25  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26  */
27 
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/smp.h>
34 #include <linux/sched.h>
35 #include <linux/cpufreq.h>
36 #include <linux/compiler.h>
37 #include <linux/dmi.h>
38 #include <linux/slab.h>
39 
40 #include <linux/acpi.h>
41 #include <linux/io.h>
42 #include <linux/delay.h>
43 #include <linux/uaccess.h>
44 
45 #include <acpi/processor.h>
46 
47 #include <asm/msr.h>
48 #include <asm/processor.h>
49 #include <asm/cpufeature.h>
50 #include <asm/cpu_device_id.h>
51 
52 MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
53 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
54 MODULE_LICENSE("GPL");
55 
56 enum {
57 	UNDEFINED_CAPABLE = 0,
58 	SYSTEM_INTEL_MSR_CAPABLE,
59 	SYSTEM_AMD_MSR_CAPABLE,
60 	SYSTEM_IO_CAPABLE,
61 };
62 
63 #define INTEL_MSR_RANGE		(0xffff)
64 #define AMD_MSR_RANGE		(0x7)
65 
66 #define MSR_K7_HWCR_CPB_DIS	(1ULL << 25)
67 
68 struct acpi_cpufreq_data {
69 	unsigned int resume;
70 	unsigned int cpu_feature;
71 	unsigned int acpi_perf_cpu;
72 	cpumask_var_t freqdomain_cpus;
73 	void (*cpu_freq_write)(struct acpi_pct_register *reg, u32 val);
74 	u32 (*cpu_freq_read)(struct acpi_pct_register *reg);
75 };
76 
77 /* acpi_perf_data is a pointer to percpu data. */
78 static struct acpi_processor_performance __percpu *acpi_perf_data;
79 
to_perf_data(struct acpi_cpufreq_data * data)80 static inline struct acpi_processor_performance *to_perf_data(struct acpi_cpufreq_data *data)
81 {
82 	return per_cpu_ptr(acpi_perf_data, data->acpi_perf_cpu);
83 }
84 
85 static struct cpufreq_driver acpi_cpufreq_driver;
86 
87 static unsigned int acpi_pstate_strict;
88 
boost_state(unsigned int cpu)89 static bool boost_state(unsigned int cpu)
90 {
91 	u32 lo, hi;
92 	u64 msr;
93 
94 	switch (boot_cpu_data.x86_vendor) {
95 	case X86_VENDOR_INTEL:
96 		rdmsr_on_cpu(cpu, MSR_IA32_MISC_ENABLE, &lo, &hi);
97 		msr = lo | ((u64)hi << 32);
98 		return !(msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
99 	case X86_VENDOR_AMD:
100 		rdmsr_on_cpu(cpu, MSR_K7_HWCR, &lo, &hi);
101 		msr = lo | ((u64)hi << 32);
102 		return !(msr & MSR_K7_HWCR_CPB_DIS);
103 	}
104 	return false;
105 }
106 
boost_set_msr(bool enable)107 static int boost_set_msr(bool enable)
108 {
109 	u32 msr_addr;
110 	u64 msr_mask, val;
111 
112 	switch (boot_cpu_data.x86_vendor) {
113 	case X86_VENDOR_INTEL:
114 		msr_addr = MSR_IA32_MISC_ENABLE;
115 		msr_mask = MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
116 		break;
117 	case X86_VENDOR_AMD:
118 		msr_addr = MSR_K7_HWCR;
119 		msr_mask = MSR_K7_HWCR_CPB_DIS;
120 		break;
121 	default:
122 		return -EINVAL;
123 	}
124 
125 	rdmsrl(msr_addr, val);
126 
127 	if (enable)
128 		val &= ~msr_mask;
129 	else
130 		val |= msr_mask;
131 
132 	wrmsrl(msr_addr, val);
133 	return 0;
134 }
135 
boost_set_msr_each(void * p_en)136 static void boost_set_msr_each(void *p_en)
137 {
138 	bool enable = (bool) p_en;
139 
140 	boost_set_msr(enable);
141 }
142 
set_boost(int val)143 static int set_boost(int val)
144 {
145 	get_online_cpus();
146 	on_each_cpu(boost_set_msr_each, (void *)(long)val, 1);
147 	put_online_cpus();
148 	pr_debug("Core Boosting %sabled.\n", val ? "en" : "dis");
149 
150 	return 0;
151 }
152 
show_freqdomain_cpus(struct cpufreq_policy * policy,char * buf)153 static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
154 {
155 	struct acpi_cpufreq_data *data = policy->driver_data;
156 
157 	if (unlikely(!data))
158 		return -ENODEV;
159 
160 	return cpufreq_show_cpus(data->freqdomain_cpus, buf);
161 }
162 
163 cpufreq_freq_attr_ro(freqdomain_cpus);
164 
165 #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
store_cpb(struct cpufreq_policy * policy,const char * buf,size_t count)166 static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf,
167 			 size_t count)
168 {
169 	int ret;
170 	unsigned int val = 0;
171 
172 	if (!acpi_cpufreq_driver.set_boost)
173 		return -EINVAL;
174 
175 	ret = kstrtouint(buf, 10, &val);
176 	if (ret || val > 1)
177 		return -EINVAL;
178 
179 	set_boost(val);
180 
181 	return count;
182 }
183 
show_cpb(struct cpufreq_policy * policy,char * buf)184 static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf)
185 {
186 	return sprintf(buf, "%u\n", acpi_cpufreq_driver.boost_enabled);
187 }
188 
189 cpufreq_freq_attr_rw(cpb);
190 #endif
191 
check_est_cpu(unsigned int cpuid)192 static int check_est_cpu(unsigned int cpuid)
193 {
194 	struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
195 
196 	return cpu_has(cpu, X86_FEATURE_EST);
197 }
198 
check_amd_hwpstate_cpu(unsigned int cpuid)199 static int check_amd_hwpstate_cpu(unsigned int cpuid)
200 {
201 	struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
202 
203 	return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
204 }
205 
extract_io(struct cpufreq_policy * policy,u32 value)206 static unsigned extract_io(struct cpufreq_policy *policy, u32 value)
207 {
208 	struct acpi_cpufreq_data *data = policy->driver_data;
209 	struct acpi_processor_performance *perf;
210 	int i;
211 
212 	perf = to_perf_data(data);
213 
214 	for (i = 0; i < perf->state_count; i++) {
215 		if (value == perf->states[i].status)
216 			return policy->freq_table[i].frequency;
217 	}
218 	return 0;
219 }
220 
extract_msr(struct cpufreq_policy * policy,u32 msr)221 static unsigned extract_msr(struct cpufreq_policy *policy, u32 msr)
222 {
223 	struct acpi_cpufreq_data *data = policy->driver_data;
224 	struct cpufreq_frequency_table *pos;
225 	struct acpi_processor_performance *perf;
226 
227 	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
228 		msr &= AMD_MSR_RANGE;
229 	else
230 		msr &= INTEL_MSR_RANGE;
231 
232 	perf = to_perf_data(data);
233 
234 	cpufreq_for_each_entry(pos, policy->freq_table)
235 		if (msr == perf->states[pos->driver_data].status)
236 			return pos->frequency;
237 	return policy->freq_table[0].frequency;
238 }
239 
extract_freq(struct cpufreq_policy * policy,u32 val)240 static unsigned extract_freq(struct cpufreq_policy *policy, u32 val)
241 {
242 	struct acpi_cpufreq_data *data = policy->driver_data;
243 
244 	switch (data->cpu_feature) {
245 	case SYSTEM_INTEL_MSR_CAPABLE:
246 	case SYSTEM_AMD_MSR_CAPABLE:
247 		return extract_msr(policy, val);
248 	case SYSTEM_IO_CAPABLE:
249 		return extract_io(policy, val);
250 	default:
251 		return 0;
252 	}
253 }
254 
cpu_freq_read_intel(struct acpi_pct_register * not_used)255 static u32 cpu_freq_read_intel(struct acpi_pct_register *not_used)
256 {
257 	u32 val, dummy;
258 
259 	rdmsr(MSR_IA32_PERF_CTL, val, dummy);
260 	return val;
261 }
262 
cpu_freq_write_intel(struct acpi_pct_register * not_used,u32 val)263 static void cpu_freq_write_intel(struct acpi_pct_register *not_used, u32 val)
264 {
265 	u32 lo, hi;
266 
267 	rdmsr(MSR_IA32_PERF_CTL, lo, hi);
268 	lo = (lo & ~INTEL_MSR_RANGE) | (val & INTEL_MSR_RANGE);
269 	wrmsr(MSR_IA32_PERF_CTL, lo, hi);
270 }
271 
cpu_freq_read_amd(struct acpi_pct_register * not_used)272 static u32 cpu_freq_read_amd(struct acpi_pct_register *not_used)
273 {
274 	u32 val, dummy;
275 
276 	rdmsr(MSR_AMD_PERF_CTL, val, dummy);
277 	return val;
278 }
279 
cpu_freq_write_amd(struct acpi_pct_register * not_used,u32 val)280 static void cpu_freq_write_amd(struct acpi_pct_register *not_used, u32 val)
281 {
282 	wrmsr(MSR_AMD_PERF_CTL, val, 0);
283 }
284 
cpu_freq_read_io(struct acpi_pct_register * reg)285 static u32 cpu_freq_read_io(struct acpi_pct_register *reg)
286 {
287 	u32 val;
288 
289 	acpi_os_read_port(reg->address, &val, reg->bit_width);
290 	return val;
291 }
292 
cpu_freq_write_io(struct acpi_pct_register * reg,u32 val)293 static void cpu_freq_write_io(struct acpi_pct_register *reg, u32 val)
294 {
295 	acpi_os_write_port(reg->address, val, reg->bit_width);
296 }
297 
298 struct drv_cmd {
299 	struct acpi_pct_register *reg;
300 	u32 val;
301 	union {
302 		void (*write)(struct acpi_pct_register *reg, u32 val);
303 		u32 (*read)(struct acpi_pct_register *reg);
304 	} func;
305 };
306 
307 /* Called via smp_call_function_single(), on the target CPU */
do_drv_read(void * _cmd)308 static void do_drv_read(void *_cmd)
309 {
310 	struct drv_cmd *cmd = _cmd;
311 
312 	cmd->val = cmd->func.read(cmd->reg);
313 }
314 
drv_read(struct acpi_cpufreq_data * data,const struct cpumask * mask)315 static u32 drv_read(struct acpi_cpufreq_data *data, const struct cpumask *mask)
316 {
317 	struct acpi_processor_performance *perf = to_perf_data(data);
318 	struct drv_cmd cmd = {
319 		.reg = &perf->control_register,
320 		.func.read = data->cpu_freq_read,
321 	};
322 	int err;
323 
324 	err = smp_call_function_any(mask, do_drv_read, &cmd, 1);
325 	WARN_ON_ONCE(err);	/* smp_call_function_any() was buggy? */
326 	return cmd.val;
327 }
328 
329 /* Called via smp_call_function_many(), on the target CPUs */
do_drv_write(void * _cmd)330 static void do_drv_write(void *_cmd)
331 {
332 	struct drv_cmd *cmd = _cmd;
333 
334 	cmd->func.write(cmd->reg, cmd->val);
335 }
336 
drv_write(struct acpi_cpufreq_data * data,const struct cpumask * mask,u32 val)337 static void drv_write(struct acpi_cpufreq_data *data,
338 		      const struct cpumask *mask, u32 val)
339 {
340 	struct acpi_processor_performance *perf = to_perf_data(data);
341 	struct drv_cmd cmd = {
342 		.reg = &perf->control_register,
343 		.val = val,
344 		.func.write = data->cpu_freq_write,
345 	};
346 	int this_cpu;
347 
348 	this_cpu = get_cpu();
349 	if (cpumask_test_cpu(this_cpu, mask))
350 		do_drv_write(&cmd);
351 
352 	smp_call_function_many(mask, do_drv_write, &cmd, 1);
353 	put_cpu();
354 }
355 
get_cur_val(const struct cpumask * mask,struct acpi_cpufreq_data * data)356 static u32 get_cur_val(const struct cpumask *mask, struct acpi_cpufreq_data *data)
357 {
358 	u32 val;
359 
360 	if (unlikely(cpumask_empty(mask)))
361 		return 0;
362 
363 	val = drv_read(data, mask);
364 
365 	pr_debug("get_cur_val = %u\n", val);
366 
367 	return val;
368 }
369 
get_cur_freq_on_cpu(unsigned int cpu)370 static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
371 {
372 	struct acpi_cpufreq_data *data;
373 	struct cpufreq_policy *policy;
374 	unsigned int freq;
375 	unsigned int cached_freq;
376 
377 	pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
378 
379 	policy = cpufreq_cpu_get_raw(cpu);
380 	if (unlikely(!policy))
381 		return 0;
382 
383 	data = policy->driver_data;
384 	if (unlikely(!data || !policy->freq_table))
385 		return 0;
386 
387 	cached_freq = policy->freq_table[to_perf_data(data)->state].frequency;
388 	freq = extract_freq(policy, get_cur_val(cpumask_of(cpu), data));
389 	if (freq != cached_freq) {
390 		/*
391 		 * The dreaded BIOS frequency change behind our back.
392 		 * Force set the frequency on next target call.
393 		 */
394 		data->resume = 1;
395 	}
396 
397 	pr_debug("cur freq = %u\n", freq);
398 
399 	return freq;
400 }
401 
check_freqs(struct cpufreq_policy * policy,const struct cpumask * mask,unsigned int freq)402 static unsigned int check_freqs(struct cpufreq_policy *policy,
403 				const struct cpumask *mask, unsigned int freq)
404 {
405 	struct acpi_cpufreq_data *data = policy->driver_data;
406 	unsigned int cur_freq;
407 	unsigned int i;
408 
409 	for (i = 0; i < 100; i++) {
410 		cur_freq = extract_freq(policy, get_cur_val(mask, data));
411 		if (cur_freq == freq)
412 			return 1;
413 		udelay(10);
414 	}
415 	return 0;
416 }
417 
acpi_cpufreq_target(struct cpufreq_policy * policy,unsigned int index)418 static int acpi_cpufreq_target(struct cpufreq_policy *policy,
419 			       unsigned int index)
420 {
421 	struct acpi_cpufreq_data *data = policy->driver_data;
422 	struct acpi_processor_performance *perf;
423 	const struct cpumask *mask;
424 	unsigned int next_perf_state = 0; /* Index into perf table */
425 	int result = 0;
426 
427 	if (unlikely(!data)) {
428 		return -ENODEV;
429 	}
430 
431 	perf = to_perf_data(data);
432 	next_perf_state = policy->freq_table[index].driver_data;
433 	if (perf->state == next_perf_state) {
434 		if (unlikely(data->resume)) {
435 			pr_debug("Called after resume, resetting to P%d\n",
436 				next_perf_state);
437 			data->resume = 0;
438 		} else {
439 			pr_debug("Already at target state (P%d)\n",
440 				next_perf_state);
441 			return 0;
442 		}
443 	}
444 
445 	/*
446 	 * The core won't allow CPUs to go away until the governor has been
447 	 * stopped, so we can rely on the stability of policy->cpus.
448 	 */
449 	mask = policy->shared_type == CPUFREQ_SHARED_TYPE_ANY ?
450 		cpumask_of(policy->cpu) : policy->cpus;
451 
452 	drv_write(data, mask, perf->states[next_perf_state].control);
453 
454 	if (acpi_pstate_strict) {
455 		if (!check_freqs(policy, mask,
456 				 policy->freq_table[index].frequency)) {
457 			pr_debug("acpi_cpufreq_target failed (%d)\n",
458 				policy->cpu);
459 			result = -EAGAIN;
460 		}
461 	}
462 
463 	if (!result)
464 		perf->state = next_perf_state;
465 
466 	return result;
467 }
468 
acpi_cpufreq_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)469 static unsigned int acpi_cpufreq_fast_switch(struct cpufreq_policy *policy,
470 					     unsigned int target_freq)
471 {
472 	struct acpi_cpufreq_data *data = policy->driver_data;
473 	struct acpi_processor_performance *perf;
474 	struct cpufreq_frequency_table *entry;
475 	unsigned int next_perf_state, next_freq, index;
476 
477 	/*
478 	 * Find the closest frequency above target_freq.
479 	 */
480 	if (policy->cached_target_freq == target_freq)
481 		index = policy->cached_resolved_idx;
482 	else
483 		index = cpufreq_table_find_index_dl(policy, target_freq);
484 
485 	entry = &policy->freq_table[index];
486 	next_freq = entry->frequency;
487 	next_perf_state = entry->driver_data;
488 
489 	perf = to_perf_data(data);
490 	if (perf->state == next_perf_state) {
491 		if (unlikely(data->resume))
492 			data->resume = 0;
493 		else
494 			return next_freq;
495 	}
496 
497 	data->cpu_freq_write(&perf->control_register,
498 			     perf->states[next_perf_state].control);
499 	perf->state = next_perf_state;
500 	return next_freq;
501 }
502 
503 static unsigned long
acpi_cpufreq_guess_freq(struct acpi_cpufreq_data * data,unsigned int cpu)504 acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
505 {
506 	struct acpi_processor_performance *perf;
507 
508 	perf = to_perf_data(data);
509 	if (cpu_khz) {
510 		/* search the closest match to cpu_khz */
511 		unsigned int i;
512 		unsigned long freq;
513 		unsigned long freqn = perf->states[0].core_frequency * 1000;
514 
515 		for (i = 0; i < (perf->state_count-1); i++) {
516 			freq = freqn;
517 			freqn = perf->states[i+1].core_frequency * 1000;
518 			if ((2 * cpu_khz) > (freqn + freq)) {
519 				perf->state = i;
520 				return freq;
521 			}
522 		}
523 		perf->state = perf->state_count-1;
524 		return freqn;
525 	} else {
526 		/* assume CPU is at P0... */
527 		perf->state = 0;
528 		return perf->states[0].core_frequency * 1000;
529 	}
530 }
531 
free_acpi_perf_data(void)532 static void free_acpi_perf_data(void)
533 {
534 	unsigned int i;
535 
536 	/* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
537 	for_each_possible_cpu(i)
538 		free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
539 				 ->shared_cpu_map);
540 	free_percpu(acpi_perf_data);
541 }
542 
cpufreq_boost_online(unsigned int cpu)543 static int cpufreq_boost_online(unsigned int cpu)
544 {
545 	/*
546 	 * On the CPU_UP path we simply keep the boost-disable flag
547 	 * in sync with the current global state.
548 	 */
549 	return boost_set_msr(acpi_cpufreq_driver.boost_enabled);
550 }
551 
cpufreq_boost_down_prep(unsigned int cpu)552 static int cpufreq_boost_down_prep(unsigned int cpu)
553 {
554 	/*
555 	 * Clear the boost-disable bit on the CPU_DOWN path so that
556 	 * this cpu cannot block the remaining ones from boosting.
557 	 */
558 	return boost_set_msr(1);
559 }
560 
561 /*
562  * acpi_cpufreq_early_init - initialize ACPI P-States library
563  *
564  * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
565  * in order to determine correct frequency and voltage pairings. We can
566  * do _PDC and _PSD and find out the processor dependency for the
567  * actual init that will happen later...
568  */
acpi_cpufreq_early_init(void)569 static int __init acpi_cpufreq_early_init(void)
570 {
571 	unsigned int i;
572 	pr_debug("acpi_cpufreq_early_init\n");
573 
574 	acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
575 	if (!acpi_perf_data) {
576 		pr_debug("Memory allocation error for acpi_perf_data.\n");
577 		return -ENOMEM;
578 	}
579 	for_each_possible_cpu(i) {
580 		if (!zalloc_cpumask_var_node(
581 			&per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
582 			GFP_KERNEL, cpu_to_node(i))) {
583 
584 			/* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
585 			free_acpi_perf_data();
586 			return -ENOMEM;
587 		}
588 	}
589 
590 	/* Do initialization in ACPI core */
591 	acpi_processor_preregister_performance(acpi_perf_data);
592 	return 0;
593 }
594 
595 #ifdef CONFIG_SMP
596 /*
597  * Some BIOSes do SW_ANY coordination internally, either set it up in hw
598  * or do it in BIOS firmware and won't inform about it to OS. If not
599  * detected, this has a side effect of making CPU run at a different speed
600  * than OS intended it to run at. Detect it and handle it cleanly.
601  */
602 static int bios_with_sw_any_bug;
603 
sw_any_bug_found(const struct dmi_system_id * d)604 static int sw_any_bug_found(const struct dmi_system_id *d)
605 {
606 	bios_with_sw_any_bug = 1;
607 	return 0;
608 }
609 
610 static const struct dmi_system_id sw_any_bug_dmi_table[] = {
611 	{
612 		.callback = sw_any_bug_found,
613 		.ident = "Supermicro Server X6DLP",
614 		.matches = {
615 			DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
616 			DMI_MATCH(DMI_BIOS_VERSION, "080010"),
617 			DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
618 		},
619 	},
620 	{ }
621 };
622 
acpi_cpufreq_blacklist(struct cpuinfo_x86 * c)623 static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
624 {
625 	/* Intel Xeon Processor 7100 Series Specification Update
626 	 * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
627 	 * AL30: A Machine Check Exception (MCE) Occurring during an
628 	 * Enhanced Intel SpeedStep Technology Ratio Change May Cause
629 	 * Both Processor Cores to Lock Up. */
630 	if (c->x86_vendor == X86_VENDOR_INTEL) {
631 		if ((c->x86 == 15) &&
632 		    (c->x86_model == 6) &&
633 		    (c->x86_stepping == 8)) {
634 			pr_info("Intel(R) Xeon(R) 7100 Errata AL30, processors may lock up on frequency changes: disabling acpi-cpufreq\n");
635 			return -ENODEV;
636 		    }
637 		}
638 	return 0;
639 }
640 #endif
641 
acpi_cpufreq_cpu_init(struct cpufreq_policy * policy)642 static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
643 {
644 	unsigned int i;
645 	unsigned int valid_states = 0;
646 	unsigned int cpu = policy->cpu;
647 	struct acpi_cpufreq_data *data;
648 	unsigned int result = 0;
649 	struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
650 	struct acpi_processor_performance *perf;
651 	struct cpufreq_frequency_table *freq_table;
652 #ifdef CONFIG_SMP
653 	static int blacklisted;
654 #endif
655 
656 	pr_debug("acpi_cpufreq_cpu_init\n");
657 
658 #ifdef CONFIG_SMP
659 	if (blacklisted)
660 		return blacklisted;
661 	blacklisted = acpi_cpufreq_blacklist(c);
662 	if (blacklisted)
663 		return blacklisted;
664 #endif
665 
666 	data = kzalloc(sizeof(*data), GFP_KERNEL);
667 	if (!data)
668 		return -ENOMEM;
669 
670 	if (!zalloc_cpumask_var(&data->freqdomain_cpus, GFP_KERNEL)) {
671 		result = -ENOMEM;
672 		goto err_free;
673 	}
674 
675 	perf = per_cpu_ptr(acpi_perf_data, cpu);
676 	data->acpi_perf_cpu = cpu;
677 	policy->driver_data = data;
678 
679 	if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
680 		acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
681 
682 	result = acpi_processor_register_performance(perf, cpu);
683 	if (result)
684 		goto err_free_mask;
685 
686 	policy->shared_type = perf->shared_type;
687 
688 	/*
689 	 * Will let policy->cpus know about dependency only when software
690 	 * coordination is required.
691 	 */
692 	if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
693 	    policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
694 		cpumask_copy(policy->cpus, perf->shared_cpu_map);
695 	}
696 	cpumask_copy(data->freqdomain_cpus, perf->shared_cpu_map);
697 
698 #ifdef CONFIG_SMP
699 	dmi_check_system(sw_any_bug_dmi_table);
700 	if (bios_with_sw_any_bug && !policy_is_shared(policy)) {
701 		policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
702 		cpumask_copy(policy->cpus, topology_core_cpumask(cpu));
703 	}
704 
705 	if (check_amd_hwpstate_cpu(cpu) && boot_cpu_data.x86 < 0x19 &&
706 	    !acpi_pstate_strict) {
707 		cpumask_clear(policy->cpus);
708 		cpumask_set_cpu(cpu, policy->cpus);
709 		cpumask_copy(data->freqdomain_cpus,
710 			     topology_sibling_cpumask(cpu));
711 		policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
712 		pr_info_once("overriding BIOS provided _PSD data\n");
713 	}
714 #endif
715 
716 	/* capability check */
717 	if (perf->state_count <= 1) {
718 		pr_debug("No P-States\n");
719 		result = -ENODEV;
720 		goto err_unreg;
721 	}
722 
723 	if (perf->control_register.space_id != perf->status_register.space_id) {
724 		result = -ENODEV;
725 		goto err_unreg;
726 	}
727 
728 	switch (perf->control_register.space_id) {
729 	case ACPI_ADR_SPACE_SYSTEM_IO:
730 		if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
731 		    boot_cpu_data.x86 == 0xf) {
732 			pr_debug("AMD K8 systems must use native drivers.\n");
733 			result = -ENODEV;
734 			goto err_unreg;
735 		}
736 		pr_debug("SYSTEM IO addr space\n");
737 		data->cpu_feature = SYSTEM_IO_CAPABLE;
738 		data->cpu_freq_read = cpu_freq_read_io;
739 		data->cpu_freq_write = cpu_freq_write_io;
740 		break;
741 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
742 		pr_debug("HARDWARE addr space\n");
743 		if (check_est_cpu(cpu)) {
744 			data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
745 			data->cpu_freq_read = cpu_freq_read_intel;
746 			data->cpu_freq_write = cpu_freq_write_intel;
747 			break;
748 		}
749 		if (check_amd_hwpstate_cpu(cpu)) {
750 			data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
751 			data->cpu_freq_read = cpu_freq_read_amd;
752 			data->cpu_freq_write = cpu_freq_write_amd;
753 			break;
754 		}
755 		result = -ENODEV;
756 		goto err_unreg;
757 	default:
758 		pr_debug("Unknown addr space %d\n",
759 			(u32) (perf->control_register.space_id));
760 		result = -ENODEV;
761 		goto err_unreg;
762 	}
763 
764 	freq_table = kcalloc(perf->state_count + 1, sizeof(*freq_table),
765 			     GFP_KERNEL);
766 	if (!freq_table) {
767 		result = -ENOMEM;
768 		goto err_unreg;
769 	}
770 
771 	/* detect transition latency */
772 	policy->cpuinfo.transition_latency = 0;
773 	for (i = 0; i < perf->state_count; i++) {
774 		if ((perf->states[i].transition_latency * 1000) >
775 		    policy->cpuinfo.transition_latency)
776 			policy->cpuinfo.transition_latency =
777 			    perf->states[i].transition_latency * 1000;
778 	}
779 
780 	/* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
781 	if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
782 	    policy->cpuinfo.transition_latency > 20 * 1000) {
783 		policy->cpuinfo.transition_latency = 20 * 1000;
784 		pr_info_once("P-state transition latency capped at 20 uS\n");
785 	}
786 
787 	/* table init */
788 	for (i = 0; i < perf->state_count; i++) {
789 		if (i > 0 && perf->states[i].core_frequency >=
790 		    freq_table[valid_states-1].frequency / 1000)
791 			continue;
792 
793 		freq_table[valid_states].driver_data = i;
794 		freq_table[valid_states].frequency =
795 		    perf->states[i].core_frequency * 1000;
796 		valid_states++;
797 	}
798 	freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
799 	policy->freq_table = freq_table;
800 	perf->state = 0;
801 
802 	switch (perf->control_register.space_id) {
803 	case ACPI_ADR_SPACE_SYSTEM_IO:
804 		/*
805 		 * The core will not set policy->cur, because
806 		 * cpufreq_driver->get is NULL, so we need to set it here.
807 		 * However, we have to guess it, because the current speed is
808 		 * unknown and not detectable via IO ports.
809 		 */
810 		policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
811 		break;
812 	case ACPI_ADR_SPACE_FIXED_HARDWARE:
813 		acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
814 		break;
815 	default:
816 		break;
817 	}
818 
819 	/* notify BIOS that we exist */
820 	acpi_processor_notify_smm(THIS_MODULE);
821 
822 	pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
823 	for (i = 0; i < perf->state_count; i++)
824 		pr_debug("     %cP%d: %d MHz, %d mW, %d uS\n",
825 			(i == perf->state ? '*' : ' '), i,
826 			(u32) perf->states[i].core_frequency,
827 			(u32) perf->states[i].power,
828 			(u32) perf->states[i].transition_latency);
829 
830 	/*
831 	 * the first call to ->target() should result in us actually
832 	 * writing something to the appropriate registers.
833 	 */
834 	data->resume = 1;
835 
836 	policy->fast_switch_possible = !acpi_pstate_strict &&
837 		!(policy_is_shared(policy) && policy->shared_type != CPUFREQ_SHARED_TYPE_ANY);
838 
839 	return result;
840 
841 err_unreg:
842 	acpi_processor_unregister_performance(cpu);
843 err_free_mask:
844 	free_cpumask_var(data->freqdomain_cpus);
845 err_free:
846 	kfree(data);
847 	policy->driver_data = NULL;
848 
849 	return result;
850 }
851 
acpi_cpufreq_cpu_exit(struct cpufreq_policy * policy)852 static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
853 {
854 	struct acpi_cpufreq_data *data = policy->driver_data;
855 
856 	pr_debug("acpi_cpufreq_cpu_exit\n");
857 
858 	policy->fast_switch_possible = false;
859 	policy->driver_data = NULL;
860 	acpi_processor_unregister_performance(data->acpi_perf_cpu);
861 	free_cpumask_var(data->freqdomain_cpus);
862 	kfree(policy->freq_table);
863 	kfree(data);
864 
865 	return 0;
866 }
867 
acpi_cpufreq_cpu_ready(struct cpufreq_policy * policy)868 static void acpi_cpufreq_cpu_ready(struct cpufreq_policy *policy)
869 {
870 	struct acpi_processor_performance *perf = per_cpu_ptr(acpi_perf_data,
871 							      policy->cpu);
872 
873 	if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
874 		pr_warn(FW_WARN "P-state 0 is not max freq\n");
875 }
876 
acpi_cpufreq_resume(struct cpufreq_policy * policy)877 static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
878 {
879 	struct acpi_cpufreq_data *data = policy->driver_data;
880 
881 	pr_debug("acpi_cpufreq_resume\n");
882 
883 	data->resume = 1;
884 
885 	return 0;
886 }
887 
888 static struct freq_attr *acpi_cpufreq_attr[] = {
889 	&cpufreq_freq_attr_scaling_available_freqs,
890 	&freqdomain_cpus,
891 #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
892 	&cpb,
893 #endif
894 	NULL,
895 };
896 
897 static struct cpufreq_driver acpi_cpufreq_driver = {
898 	.verify		= cpufreq_generic_frequency_table_verify,
899 	.target_index	= acpi_cpufreq_target,
900 	.fast_switch	= acpi_cpufreq_fast_switch,
901 	.bios_limit	= acpi_processor_get_bios_limit,
902 	.init		= acpi_cpufreq_cpu_init,
903 	.exit		= acpi_cpufreq_cpu_exit,
904 	.ready		= acpi_cpufreq_cpu_ready,
905 	.resume		= acpi_cpufreq_resume,
906 	.name		= "acpi-cpufreq",
907 	.attr		= acpi_cpufreq_attr,
908 };
909 
910 static enum cpuhp_state acpi_cpufreq_online;
911 
acpi_cpufreq_boost_init(void)912 static void __init acpi_cpufreq_boost_init(void)
913 {
914 	int ret;
915 
916 	if (!(boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA))) {
917 		pr_debug("Boost capabilities not present in the processor\n");
918 		return;
919 	}
920 
921 	acpi_cpufreq_driver.set_boost = set_boost;
922 	acpi_cpufreq_driver.boost_enabled = boost_state(0);
923 
924 	/*
925 	 * This calls the online callback on all online cpu and forces all
926 	 * MSRs to the same value.
927 	 */
928 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "cpufreq/acpi:online",
929 				cpufreq_boost_online, cpufreq_boost_down_prep);
930 	if (ret < 0) {
931 		pr_err("acpi_cpufreq: failed to register hotplug callbacks\n");
932 		return;
933 	}
934 	acpi_cpufreq_online = ret;
935 }
936 
acpi_cpufreq_boost_exit(void)937 static void acpi_cpufreq_boost_exit(void)
938 {
939 	if (acpi_cpufreq_online > 0)
940 		cpuhp_remove_state_nocalls(acpi_cpufreq_online);
941 }
942 
acpi_cpufreq_init(void)943 static int __init acpi_cpufreq_init(void)
944 {
945 	int ret;
946 
947 	if (acpi_disabled)
948 		return -ENODEV;
949 
950 	/* don't keep reloading if cpufreq_driver exists */
951 	if (cpufreq_get_current_driver())
952 		return -EEXIST;
953 
954 	pr_debug("acpi_cpufreq_init\n");
955 
956 	ret = acpi_cpufreq_early_init();
957 	if (ret)
958 		return ret;
959 
960 #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
961 	/* this is a sysfs file with a strange name and an even stranger
962 	 * semantic - per CPU instantiation, but system global effect.
963 	 * Lets enable it only on AMD CPUs for compatibility reasons and
964 	 * only if configured. This is considered legacy code, which
965 	 * will probably be removed at some point in the future.
966 	 */
967 	if (!check_amd_hwpstate_cpu(0)) {
968 		struct freq_attr **attr;
969 
970 		pr_debug("CPB unsupported, do not expose it\n");
971 
972 		for (attr = acpi_cpufreq_attr; *attr; attr++)
973 			if (*attr == &cpb) {
974 				*attr = NULL;
975 				break;
976 			}
977 	}
978 #endif
979 	acpi_cpufreq_boost_init();
980 
981 	ret = cpufreq_register_driver(&acpi_cpufreq_driver);
982 	if (ret) {
983 		free_acpi_perf_data();
984 		acpi_cpufreq_boost_exit();
985 	}
986 	return ret;
987 }
988 
acpi_cpufreq_exit(void)989 static void __exit acpi_cpufreq_exit(void)
990 {
991 	pr_debug("acpi_cpufreq_exit\n");
992 
993 	acpi_cpufreq_boost_exit();
994 
995 	cpufreq_unregister_driver(&acpi_cpufreq_driver);
996 
997 	free_acpi_perf_data();
998 }
999 
1000 module_param(acpi_pstate_strict, uint, 0644);
1001 MODULE_PARM_DESC(acpi_pstate_strict,
1002 	"value 0 or non-zero. non-zero -> strict ACPI checks are "
1003 	"performed during frequency changes.");
1004 
1005 late_initcall(acpi_cpufreq_init);
1006 module_exit(acpi_cpufreq_exit);
1007 
1008 static const struct x86_cpu_id acpi_cpufreq_ids[] = {
1009 	X86_FEATURE_MATCH(X86_FEATURE_ACPI),
1010 	X86_FEATURE_MATCH(X86_FEATURE_HW_PSTATE),
1011 	{}
1012 };
1013 MODULE_DEVICE_TABLE(x86cpu, acpi_cpufreq_ids);
1014 
1015 static const struct acpi_device_id processor_device_ids[] = {
1016 	{ACPI_PROCESSOR_OBJECT_HID, },
1017 	{ACPI_PROCESSOR_DEVICE_HID, },
1018 	{},
1019 };
1020 MODULE_DEVICE_TABLE(acpi, processor_device_ids);
1021 
1022 MODULE_ALIAS("acpi");
1023