blob: 385f17aca06b7cbf232ccb749a06421a014aa43a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -07002 * acpi-cpufreq.c - ACPI Processor P-States Driver ($Revision: 1.4 $)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -07007 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070031#include <linux/smp.h>
32#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/cpufreq.h>
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -040034#include <linux/compiler.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080035#include <linux/sched.h> /* current */
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -070036#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <linux/acpi.h>
39#include <acpi/processor.h>
40
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070041#include <asm/io.h>
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070042#include <asm/msr.h>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070043#include <asm/processor.h>
44#include <asm/cpufeature.h>
45#include <asm/delay.h>
46#include <asm/uaccess.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
49
50MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
51MODULE_DESCRIPTION("ACPI Processor P-States Driver");
52MODULE_LICENSE("GPL");
53
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070054enum {
55 UNDEFINED_CAPABLE = 0,
56 SYSTEM_INTEL_MSR_CAPABLE,
57 SYSTEM_IO_CAPABLE,
58};
59
60#define INTEL_MSR_RANGE (0xffff)
61
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070062struct acpi_cpufreq_data {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070063 struct acpi_processor_performance *acpi_data;
64 struct cpufreq_frequency_table *freq_table;
65 unsigned int resume;
66 unsigned int cpu_feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070069static struct acpi_cpufreq_data *drv_data[NR_CPUS];
70static struct acpi_processor_performance *acpi_perf_data[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static struct cpufreq_driver acpi_cpufreq_driver;
73
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -040074static unsigned int acpi_pstate_strict;
75
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070076static int check_est_cpu(unsigned int cpuid)
77{
78 struct cpuinfo_x86 *cpu = &cpu_data[cpuid];
79
80 if (cpu->x86_vendor != X86_VENDOR_INTEL ||
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070081 !cpu_has(cpu, X86_FEATURE_EST))
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070082 return 0;
83
84 return 1;
85}
86
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070087static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070088{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070089 struct acpi_processor_performance *perf;
90 int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070091
92 perf = data->acpi_data;
93
94 for (i = 0; i < perf->state_count; i++) {
95 if (value == perf->states[i].status)
96 return data->freq_table[i].frequency;
97 }
98 return 0;
99}
100
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700101static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
102{
103 int i;
104
105 msr &= INTEL_MSR_RANGE;
106 for (i = 0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
107 if (msr == data->freq_table[i].index)
108 return data->freq_table[i].frequency;
109 }
110 return data->freq_table[0].frequency;
111}
112
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700113static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
114{
115 switch (data->cpu_feature) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700116 case SYSTEM_INTEL_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700117 return extract_msr(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700118 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700119 return extract_io(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700120 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700121 return 0;
122 }
123}
124
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700125static void wrport(u16 port, u8 bit_width, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 if (bit_width <= 8) {
128 outb(value, port);
129 } else if (bit_width <= 16) {
130 outw(value, port);
131 } else if (bit_width <= 32) {
132 outl(value, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700136static void rdport(u16 port, u8 bit_width, u32 * ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 *ret = 0;
139 if (bit_width <= 8) {
140 *ret = inb(port);
141 } else if (bit_width <= 16) {
142 *ret = inw(port);
143 } else if (bit_width <= 32) {
144 *ret = inl(port);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700145 }
146}
147
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700148struct msr_addr {
149 u32 reg;
150};
151
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700152struct io_addr {
153 u16 port;
154 u8 bit_width;
155};
156
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700157typedef union {
158 struct msr_addr msr;
159 struct io_addr io;
160} drv_addr_union;
161
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700162struct drv_cmd {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700163 unsigned int type;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700164 cpumask_t mask;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700165 drv_addr_union addr;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700166 u32 val;
167};
168
169static void do_drv_read(struct drv_cmd *cmd)
170{
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700171 u32 h;
172
173 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700174 case SYSTEM_INTEL_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700175 rdmsr(cmd->addr.msr.reg, cmd->val, h);
176 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700177 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700178 rdport(cmd->addr.io.port, cmd->addr.io.bit_width, &cmd->val);
179 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700180 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700181 break;
182 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700183}
184
185static void do_drv_write(struct drv_cmd *cmd)
186{
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700187 u32 h = 0;
188
189 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700190 case SYSTEM_INTEL_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700191 wrmsr(cmd->addr.msr.reg, cmd->val, h);
192 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700193 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700194 wrport(cmd->addr.io.port, cmd->addr.io.bit_width, cmd->val);
195 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700196 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700197 break;
198 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700199}
200
201static inline void drv_read(struct drv_cmd *cmd)
202{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700203 cpumask_t saved_mask = current->cpus_allowed;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700204 cmd->val = 0;
205
206 set_cpus_allowed(current, cmd->mask);
207 do_drv_read(cmd);
208 set_cpus_allowed(current, saved_mask);
209
210}
211
212static void drv_write(struct drv_cmd *cmd)
213{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700214 cpumask_t saved_mask = current->cpus_allowed;
215 unsigned int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700216
217 for_each_cpu_mask(i, cmd->mask) {
218 set_cpus_allowed(current, cpumask_of_cpu(i));
219 do_drv_write(cmd);
220 }
221
222 set_cpus_allowed(current, saved_mask);
223 return;
224}
225
226static u32 get_cur_val(cpumask_t mask)
227{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700228 struct acpi_processor_performance *perf;
229 struct drv_cmd cmd;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700230
231 if (unlikely(cpus_empty(mask)))
232 return 0;
233
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700234 switch (drv_data[first_cpu(mask)]->cpu_feature) {
235 case SYSTEM_INTEL_MSR_CAPABLE:
236 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
237 cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
238 break;
239 case SYSTEM_IO_CAPABLE:
240 cmd.type = SYSTEM_IO_CAPABLE;
241 perf = drv_data[first_cpu(mask)]->acpi_data;
242 cmd.addr.io.port = perf->control_register.address;
243 cmd.addr.io.bit_width = perf->control_register.bit_width;
244 break;
245 default:
246 return 0;
247 }
248
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700249 cmd.mask = mask;
250
251 drv_read(&cmd);
252
253 dprintk("get_cur_val = %u\n", cmd.val);
254
255 return cmd.val;
256}
257
258static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
259{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700260 struct acpi_cpufreq_data *data = drv_data[cpu];
261 unsigned int freq;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700262
263 dprintk("get_cur_freq_on_cpu (%d)\n", cpu);
264
265 if (unlikely(data == NULL ||
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700266 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700267 return 0;
268 }
269
270 freq = extract_freq(get_cur_val(cpumask_of_cpu(cpu)), data);
271 dprintk("cur freq = %u\n", freq);
272
273 return freq;
274}
275
276static unsigned int check_freqs(cpumask_t mask, unsigned int freq,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700277 struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700278{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700279 unsigned int cur_freq;
280 unsigned int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700281
282 for (i = 0; i < 100; i++) {
283 cur_freq = extract_freq(get_cur_val(mask), data);
284 if (cur_freq == freq)
285 return 1;
286 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 return 0;
289}
290
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700291static int acpi_cpufreq_target(struct cpufreq_policy *policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700292 unsigned int target_freq, unsigned int relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700294 struct acpi_cpufreq_data *data = drv_data[policy->cpu];
295 struct acpi_processor_performance *perf;
296 struct cpufreq_freqs freqs;
297 cpumask_t online_policy_cpus;
298 struct drv_cmd cmd;
299 unsigned int msr;
300 unsigned int next_state = 0;
301 unsigned int next_perf_state = 0;
302 unsigned int i;
303 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700305 dprintk("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700307 if (unlikely(data == NULL ||
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700308 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700309 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500312 perf = data->acpi_data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700313 result = cpufreq_frequency_table_target(policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700314 data->freq_table,
315 target_freq,
316 relation, &next_state);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700317 if (unlikely(result))
318 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Andrew Morton7e1f19e2006-03-28 17:03:00 -0500320#ifdef CONFIG_HOTPLUG_CPU
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500321 /* cpufreq holds the hotplug lock, so we are safe from here on */
322 cpus_and(online_policy_cpus, cpu_online_map, policy->cpus);
Andrew Morton7e1f19e2006-03-28 17:03:00 -0500323#else
324 online_policy_cpus = policy->cpus;
325#endif
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500326
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700327 cmd.val = get_cur_val(online_policy_cpus);
328 freqs.old = extract_freq(cmd.val, data);
329 freqs.new = data->freq_table[next_state].frequency;
330 next_perf_state = data->freq_table[next_state].index;
331 if (freqs.new == freqs.old) {
332 if (unlikely(data->resume)) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700333 dprintk("Called after resume, resetting to P%d\n",
334 next_perf_state);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700335 data->resume = 0;
336 } else {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700337 dprintk("Already at target state (P%d)\n",
338 next_perf_state);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700339 return 0;
340 }
341 }
342
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700343 switch (data->cpu_feature) {
344 case SYSTEM_INTEL_MSR_CAPABLE:
345 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
346 cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
347 msr =
348 (u32) perf->states[next_perf_state].
349 control & INTEL_MSR_RANGE;
350 cmd.val = (cmd.val & ~INTEL_MSR_RANGE) | msr;
351 break;
352 case SYSTEM_IO_CAPABLE:
353 cmd.type = SYSTEM_IO_CAPABLE;
354 cmd.addr.io.port = perf->control_register.address;
355 cmd.addr.io.bit_width = perf->control_register.bit_width;
356 cmd.val = (u32) perf->states[next_perf_state].control;
357 break;
358 default:
359 return -ENODEV;
360 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700361
362 cpus_clear(cmd.mask);
363
364 if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
365 cmd.mask = online_policy_cpus;
366 else
367 cpu_set(policy->cpu, cmd.mask);
368
369 for_each_cpu_mask(i, cmd.mask) {
370 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500371 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
372 }
373
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700374 drv_write(&cmd);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500375
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700376 if (acpi_pstate_strict) {
377 if (!check_freqs(cmd.mask, freqs.new, data)) {
378 dprintk("acpi_cpufreq_target failed (%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700379 policy->cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700380 return -EAGAIN;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500381 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500382 }
383
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700384 for_each_cpu_mask(i, cmd.mask) {
385 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500386 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
387 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700388 perf->state = next_perf_state;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500389
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700390 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700393static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700395 struct acpi_cpufreq_data *data = drv_data[policy->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 dprintk("acpi_cpufreq_verify\n");
398
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700399 return cpufreq_frequency_table_verify(policy, data->freq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402static unsigned long
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700403acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700405 struct acpi_processor_performance *perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (cpu_khz) {
408 /* search the closest match to cpu_khz */
409 unsigned int i;
410 unsigned long freq;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500411 unsigned long freqn = perf->states[0].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500413 for (i = 0; i < (perf->state_count - 1); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 freq = freqn;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700415 freqn = perf->states[i + 1].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if ((2 * cpu_khz) > (freqn + freq)) {
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500417 perf->state = i;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700418 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500421 perf->state = perf->state_count - 1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700422 return freqn;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500423 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* assume CPU is at P0... */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500425 perf->state = 0;
426 return perf->states[0].core_frequency * 1000;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500430/*
431 * acpi_cpufreq_early_init - initialize ACPI P-States library
432 *
433 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
434 * in order to determine correct frequency and voltage pairings. We can
435 * do _PDC and _PSD and find out the processor dependency for the
436 * actual init that will happen later...
437 */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700438static int acpi_cpufreq_early_init(void)
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500439{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700440 struct acpi_processor_performance *data;
441 cpumask_t covered;
442 unsigned int i, j;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500443
444 dprintk("acpi_cpufreq_early_init\n");
445
Andrew Mortonfb1bb342006-06-25 05:46:43 -0700446 for_each_possible_cpu(i) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700447 data = kzalloc(sizeof(struct acpi_processor_performance),
448 GFP_KERNEL);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500449 if (!data) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700450 for_each_cpu_mask(j, covered) {
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500451 kfree(acpi_perf_data[j]);
452 acpi_perf_data[j] = NULL;
453 }
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700454 return -ENOMEM;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500455 }
456 acpi_perf_data[i] = data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700457 cpu_set(i, covered);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500458 }
459
460 /* Do initialization in ACPI core */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700461 acpi_processor_preregister_performance(acpi_perf_data);
462 return 0;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500463}
464
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700465/*
466 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
467 * or do it in BIOS firmware and won't inform about it to OS. If not
468 * detected, this has a side effect of making CPU run at a different speed
469 * than OS intended it to run at. Detect it and handle it cleanly.
470 */
471static int bios_with_sw_any_bug;
472
Venkatesh Pallipadi0497c8c2006-09-25 11:23:32 -0700473static int sw_any_bug_found(struct dmi_system_id *d)
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700474{
475 bios_with_sw_any_bug = 1;
476 return 0;
477}
478
Venkatesh Pallipadi0497c8c2006-09-25 11:23:32 -0700479static struct dmi_system_id sw_any_bug_dmi_table[] = {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700480 {
481 .callback = sw_any_bug_found,
482 .ident = "Supermicro Server X6DLP",
483 .matches = {
484 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
485 DMI_MATCH(DMI_BIOS_VERSION, "080010"),
486 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
487 },
488 },
489 { }
490};
491
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700492static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700494 unsigned int i;
495 unsigned int valid_states = 0;
496 unsigned int cpu = policy->cpu;
497 struct acpi_cpufreq_data *data;
498 unsigned int l, h;
499 unsigned int result = 0;
500 struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
501 struct acpi_processor_performance *perf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 dprintk("acpi_cpufreq_cpu_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500505 if (!acpi_perf_data[cpu])
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700506 return -ENODEV;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500507
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700508 data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!data)
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700510 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500512 data->acpi_data = acpi_perf_data[cpu];
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700513 drv_data[cpu] = data;
514
515 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
516 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500519 result = acpi_processor_register_performance(data->acpi_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (result)
521 goto err_free;
522
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500523 perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500524 policy->shared_type = perf->shared_type;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400525 /*
526 * Will let policy->cpus know about dependency only when software
527 * coordination is required.
528 */
529 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700530 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400531 policy->cpus = perf->shared_cpu_map;
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700532 }
533
534#ifdef CONFIG_SMP
535 dmi_check_system(sw_any_bug_dmi_table);
536 if (bios_with_sw_any_bug && cpus_weight(policy->cpus) == 1) {
537 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
538 policy->cpus = cpu_core_map[cpu];
539 }
540#endif
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* capability check */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500543 if (perf->state_count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 dprintk("No P-States\n");
545 result = -ENODEV;
546 goto err_unreg;
547 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500548
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700549 if (perf->control_register.space_id != perf->status_register.space_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 result = -ENODEV;
551 goto err_unreg;
552 }
553
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700554 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700555 case ACPI_ADR_SPACE_SYSTEM_IO:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700556 dprintk("SYSTEM IO addr space\n");
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700557 data->cpu_feature = SYSTEM_IO_CAPABLE;
558 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700559 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700560 dprintk("HARDWARE addr space\n");
561 if (!check_est_cpu(cpu)) {
562 result = -ENODEV;
563 goto err_unreg;
564 }
565 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700566 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700567 default:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700568 dprintk("Unknown addr space %d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700569 (u32) (perf->control_register.space_id));
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700570 result = -ENODEV;
571 goto err_unreg;
572 }
573
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700574 data->freq_table =
575 kmalloc(sizeof(struct cpufreq_frequency_table) *
576 (perf->state_count + 1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (!data->freq_table) {
578 result = -ENOMEM;
579 goto err_unreg;
580 }
581
582 /* detect transition latency */
583 policy->cpuinfo.transition_latency = 0;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700584 for (i = 0; i < perf->state_count; i++) {
585 if ((perf->states[i].transition_latency * 1000) >
586 policy->cpuinfo.transition_latency)
587 policy->cpuinfo.transition_latency =
588 perf->states[i].transition_latency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* table init */
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700593 for (i = 0; i < perf->state_count; i++) {
594 if (i > 0 && perf->states[i].core_frequency ==
595 perf->states[i - 1].core_frequency)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700596 continue;
597
598 data->freq_table[valid_states].index = i;
599 data->freq_table[valid_states].frequency =
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700600 perf->states[i].core_frequency * 1000;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700601 valid_states++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700603 data->freq_table[perf->state_count].frequency = CPUFREQ_TABLE_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
606 if (result) {
607 goto err_freqfree;
608 }
609
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700610 switch (data->cpu_feature) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700611 case ACPI_ADR_SPACE_SYSTEM_IO:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700612 /* Current speed is unknown and not detectable by IO port */
613 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
614 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700615 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700616 get_cur_freq_on_cpu(cpu);
617 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700618 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700619 break;
620 }
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /* notify BIOS that we exist */
623 acpi_processor_notify_smm(THIS_MODULE);
624
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700625 dprintk("CPU%u - ACPI performance management activated.\n", cpu);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500626 for (i = 0; i < perf->state_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700628 (i == perf->state ? '*' : ' '), i,
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500629 (u32) perf->states[i].core_frequency,
630 (u32) perf->states[i].power,
631 (u32) perf->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700634
Dominik Brodowski4b31e772005-05-18 13:49:00 -0400635 /*
636 * the first call to ->target() should result in us actually
637 * writing something to the appropriate registers.
638 */
639 data->resume = 1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700640
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700641 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700643 err_freqfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 kfree(data->freq_table);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700645 err_unreg:
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500646 acpi_processor_unregister_performance(perf, cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700647 err_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 kfree(data);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700649 drv_data[cpu] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700651 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700654static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700656 struct acpi_cpufreq_data *data = drv_data[policy->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 dprintk("acpi_cpufreq_cpu_exit\n");
659
660 if (data) {
661 cpufreq_frequency_table_put_attr(policy->cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700662 drv_data[policy->cpu] = NULL;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700663 acpi_processor_unregister_performance(data->acpi_data,
664 policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 kfree(data);
666 }
667
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700671static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700673 struct acpi_cpufreq_data *data = drv_data[policy->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 dprintk("acpi_cpufreq_resume\n");
676
677 data->resume = 1;
678
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700682static struct freq_attr *acpi_cpufreq_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 &cpufreq_freq_attr_scaling_available_freqs,
684 NULL,
685};
686
687static struct cpufreq_driver acpi_cpufreq_driver = {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700688 .verify = acpi_cpufreq_verify,
689 .target = acpi_cpufreq_target,
690 .get = get_cur_freq_on_cpu,
691 .init = acpi_cpufreq_cpu_init,
692 .exit = acpi_cpufreq_cpu_exit,
693 .resume = acpi_cpufreq_resume,
694 .name = "acpi-cpufreq",
695 .owner = THIS_MODULE,
696 .attr = acpi_cpufreq_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697};
698
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700699static int __init acpi_cpufreq_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 dprintk("acpi_cpufreq_init\n");
702
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700703 acpi_cpufreq_early_init();
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500704
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700705 return cpufreq_register_driver(&acpi_cpufreq_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700708static void __exit acpi_cpufreq_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700710 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 dprintk("acpi_cpufreq_exit\n");
712
713 cpufreq_unregister_driver(&acpi_cpufreq_driver);
714
Andrew Mortonfb1bb342006-06-25 05:46:43 -0700715 for_each_possible_cpu(i) {
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500716 kfree(acpi_perf_data[i]);
717 acpi_perf_data[i] = NULL;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return;
720}
721
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -0400722module_param(acpi_pstate_strict, uint, 0644);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700723MODULE_PARM_DESC(acpi_pstate_strict,
724 "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726late_initcall(acpi_cpufreq_init);
727module_exit(acpi_cpufreq_exit);
728
729MODULE_ALIAS("acpi");