blob: 2709b3c183b40672f7588fd37a63f19d1feb28fd [file] [log] [blame]
Luis Henriques21335d02009-04-23 19:45:04 +01001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Dave Jones1f729e02006-06-04 19:37:58 -04003 * (c) 2003-2006 Advanced Micro Devices, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Your use of this code is subject to the terms and conditions of the
5 * GNU general public license version 2. See "COPYING" or
6 * http://www.gnu.org/licenses/gpl.html
7 *
Dave Jones065b8072005-05-31 19:03:46 -07008 * Support : mark.langsdorf@amd.com
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * Based on the powernow-k7.c module written by Dave Jones.
Dave Jonesf4432c52008-10-20 13:31:45 -040011 * (C) 2003 Dave Jones on behalf of SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * (C) 2004 Dominik Brodowski <linux@brodo.de>
13 * (C) 2004 Pavel Machek <pavel@suse.cz>
14 * Licensed under the terms of the GNU GPL License version 2.
15 * Based upon datasheets & sample CPUs kindly provided by AMD.
16 *
17 * Valuable input gratefully received from Dave Jones, Pavel Machek,
Dave Jones1f729e02006-06-04 19:37:58 -040018 * Dominik Brodowski, Jacob Shin, and others.
Dave Jones065b8072005-05-31 19:03:46 -070019 * Originally developed by Paul Devriendt.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * Processor information obtained from Chapter 9 (Power and Thermal Management)
21 * of the "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
22 * Opteron Processors" available for download from www.amd.com
23 *
Dave Jones2e3f8fa2006-05-30 17:25:14 -040024 * Tables for specific CPUs can be inferred from
Dave Jones065b8072005-05-31 19:03:46 -070025 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/30430.pdf
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27
28#include <linux/kernel.h>
29#include <linux/smp.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/cpufreq.h>
33#include <linux/slab.h>
34#include <linux/string.h>
Dave Jones065b8072005-05-31 19:03:46 -070035#include <linux/cpumask.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080036#include <linux/sched.h> /* for current / set_cpus_allowed() */
Dave Jones0e64a0c2009-02-04 14:37:50 -050037#include <linux/io.h>
38#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/msr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/acpi.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080043#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define PFX "powernow-k8: "
Mark Langsdorfc5829cd2007-10-17 16:52:08 -050047#define VERSION "version 2.20.00"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "powernow-k8.h"
49
50/* serialize freq changes */
Ingo Molnar14cc3e22006-03-26 01:37:14 -080051static DEFINE_MUTEX(fidvid_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
travis@sgi.com2c6b8c02008-01-30 13:33:11 +010053static DEFINE_PER_CPU(struct powernow_k8_data *, powernow_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Dave Jones1f729e02006-06-04 19:37:58 -040055static int cpu_family = CPU_OPTERON;
56
Dave Jones065b8072005-05-31 19:03:46 -070057#ifndef CONFIG_SMP
Rusty Russell7ad728f2009-03-13 14:49:50 +103058static inline const struct cpumask *cpu_core_mask(int cpu)
59{
60 return cpumask_of(0);
61}
Dave Jones065b8072005-05-31 19:03:46 -070062#endif
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* Return a frequency in MHz, given an input fid */
65static u32 find_freq_from_fid(u32 fid)
66{
67 return 800 + (fid * 100);
68}
69
70/* Return a frequency in KHz, given an input fid */
71static u32 find_khz_freq_from_fid(u32 fid)
72{
73 return 1000 * find_freq_from_fid(fid);
74}
75
Dave Jones0e64a0c2009-02-04 14:37:50 -050076static u32 find_khz_freq_from_pstate(struct cpufreq_frequency_table *data,
77 u32 pstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Mark Langsdorfc5829cd2007-10-17 16:52:08 -050079 return data[pstate].frequency;
Dave Jones1f729e02006-06-04 19:37:58 -040080}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* Return the vco fid for an input fid
83 *
84 * Each "low" fid has corresponding "high" fid, and you can get to "low" fids
85 * only from corresponding high fids. This returns "high" fid corresponding to
86 * "low" one.
87 */
88static u32 convert_fid_to_vco_fid(u32 fid)
89{
Dave Jones32ee8c32006-02-28 00:43:23 -050090 if (fid < HI_FID_TABLE_BOTTOM)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 8 + (2 * fid);
Dave Jones32ee8c32006-02-28 00:43:23 -050092 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96/*
97 * Return 1 if the pending bit is set. Unless we just instructed the processor
98 * to transition to a new state, seeing this bit set is really bad news.
99 */
100static int pending_bit_stuck(void)
101{
102 u32 lo, hi;
103
Langsdorf, Marke7bdd7a2006-06-08 10:30:17 -0500104 if (cpu_family == CPU_HW_PSTATE)
Dave Jones1f729e02006-06-04 19:37:58 -0400105 return 0;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 rdmsr(MSR_FIDVID_STATUS, lo, hi);
108 return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0;
109}
110
111/*
112 * Update the global current fid / vid values from the status msr.
113 * Returns 1 on error.
114 */
115static int query_current_values_with_pending_wait(struct powernow_k8_data *data)
116{
117 u32 lo, hi;
118 u32 i = 0;
119
Langsdorf, Marke7bdd7a2006-06-08 10:30:17 -0500120 if (cpu_family == CPU_HW_PSTATE) {
Andreas Herrmanna266d9f2008-11-21 14:49:25 +0100121 if (data->currpstate == HW_PSTATE_INVALID) {
122 /* read (initial) hw pstate if not yet set */
123 rdmsr(MSR_PSTATE_STATUS, lo, hi);
124 i = lo & HW_PSTATE_MASK;
125
126 /*
127 * a workaround for family 11h erratum 311 might cause
128 * an "out-of-range Pstate if the core is in Pstate-0
129 */
130 if (i >= data->numps)
131 data->currpstate = HW_PSTATE_0;
132 else
133 data->currpstate = i;
134 }
Dave Jones1f729e02006-06-04 19:37:58 -0400135 return 0;
136 }
Dave Jones7153d962005-07-28 09:45:10 -0700137 do {
Dave Jones0213df72005-10-21 17:21:03 -0400138 if (i++ > 10000) {
139 dprintk("detected change pending stuck\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return 1;
141 }
142 rdmsr(MSR_FIDVID_STATUS, lo, hi);
Dave Jones7153d962005-07-28 09:45:10 -0700143 } while (lo & MSR_S_LO_CHANGE_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 data->currvid = hi & MSR_S_HI_CURRENT_VID;
146 data->currfid = lo & MSR_S_LO_CURRENT_FID;
147
148 return 0;
149}
150
151/* the isochronous relief time */
152static void count_off_irt(struct powernow_k8_data *data)
153{
154 udelay((1 << data->irt) * 10);
155 return;
156}
157
Simon Arlott27b46d72007-10-20 01:13:56 +0200158/* the voltage stabilization time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void count_off_vst(struct powernow_k8_data *data)
160{
161 udelay(data->vstable * VST_UNITS_20US);
162 return;
163}
164
165/* need to init the control msr to a safe value (for each cpu) */
166static void fidvid_msr_init(void)
167{
168 u32 lo, hi;
169 u8 fid, vid;
170
171 rdmsr(MSR_FIDVID_STATUS, lo, hi);
172 vid = hi & MSR_S_HI_CURRENT_VID;
173 fid = lo & MSR_S_LO_CURRENT_FID;
174 lo = fid | (vid << MSR_C_LO_VID_SHIFT);
175 hi = MSR_C_HI_STP_GNT_BENIGN;
176 dprintk("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi);
177 wrmsr(MSR_FIDVID_CTL, lo, hi);
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/* write the new fid value along with the other control fields to the msr */
181static int write_new_fid(struct powernow_k8_data *data, u32 fid)
182{
183 u32 lo;
184 u32 savevid = data->currvid;
Dave Jones0213df72005-10-21 17:21:03 -0400185 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if ((fid & INVALID_FID_MASK) || (data->currvid & INVALID_VID_MASK)) {
188 printk(KERN_ERR PFX "internal error - overflow on fid write\n");
189 return 1;
190 }
191
Dave Jones0e64a0c2009-02-04 14:37:50 -0500192 lo = fid;
193 lo |= (data->currvid << MSR_C_LO_VID_SHIFT);
194 lo |= MSR_C_LO_INIT_FID_VID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
197 fid, lo, data->plllock * PLL_LOCK_CONVERSION);
198
Dave Jones0213df72005-10-21 17:21:03 -0400199 do {
200 wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
201 if (i++ > 100) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500202 printk(KERN_ERR PFX
203 "Hardware error - pending bit very stuck - "
204 "no further pstate changes possible\n");
Chris Wright63172cb2005-10-21 16:56:08 -0700205 return 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500206 }
Dave Jones0213df72005-10-21 17:21:03 -0400207 } while (query_current_values_with_pending_wait(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 count_off_irt(data);
210
211 if (savevid != data->currvid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500212 printk(KERN_ERR PFX
213 "vid change on fid trans, old 0x%x, new 0x%x\n",
214 savevid, data->currvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return 1;
216 }
217
218 if (fid != data->currfid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500219 printk(KERN_ERR PFX
220 "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
221 data->currfid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 1;
223 }
224
225 return 0;
226}
227
228/* Write a new vid to the hardware */
229static int write_new_vid(struct powernow_k8_data *data, u32 vid)
230{
231 u32 lo;
232 u32 savefid = data->currfid;
Dave Jones0213df72005-10-21 17:21:03 -0400233 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
236 printk(KERN_ERR PFX "internal error - overflow on vid write\n");
237 return 1;
238 }
239
Dave Jones0e64a0c2009-02-04 14:37:50 -0500240 lo = data->currfid;
241 lo |= (vid << MSR_C_LO_VID_SHIFT);
242 lo |= MSR_C_LO_INIT_FID_VID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
245 vid, lo, STOP_GRANT_5NS);
246
Dave Jones0213df72005-10-21 17:21:03 -0400247 do {
248 wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
Dave Jones6df89002005-11-30 13:33:30 -0800249 if (i++ > 100) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500250 printk(KERN_ERR PFX "internal error - pending bit "
251 "very stuck - no further pstate "
252 "changes possible\n");
Dave Jones6df89002005-11-30 13:33:30 -0800253 return 1;
254 }
Dave Jones0213df72005-10-21 17:21:03 -0400255 } while (query_current_values_with_pending_wait(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 if (savefid != data->currfid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500258 printk(KERN_ERR PFX "fid changed on vid trans, old "
259 "0x%x new 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 savefid, data->currfid);
261 return 1;
262 }
263
264 if (vid != data->currvid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500265 printk(KERN_ERR PFX "vid trans failed, vid 0x%x, "
266 "curr 0x%x\n",
267 vid, data->currvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 1;
269 }
270
271 return 0;
272}
273
274/*
275 * Reduce the vid by the max of step or reqvid.
276 * Decreasing vid codes represent increasing voltages:
Dave Jones841e40b2005-07-28 09:40:04 -0700277 * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500279static int decrease_vid_code_by_step(struct powernow_k8_data *data,
280 u32 reqvid, u32 step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 if ((data->currvid - reqvid) > step)
283 reqvid = data->currvid - step;
284
285 if (write_new_vid(data, reqvid))
286 return 1;
287
288 count_off_vst(data);
289
290 return 0;
291}
292
Dave Jones1f729e02006-06-04 19:37:58 -0400293/* Change hardware pstate by single MSR write */
294static int transition_pstate(struct powernow_k8_data *data, u32 pstate)
295{
296 wrmsr(MSR_PSTATE_CTRL, pstate, 0);
Mark Langsdorfc5829cd2007-10-17 16:52:08 -0500297 data->currpstate = pstate;
Dave Jones1f729e02006-06-04 19:37:58 -0400298 return 0;
299}
300
301/* Change Opteron/Athlon64 fid and vid, by the 3 phases. */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500302static int transition_fid_vid(struct powernow_k8_data *data,
303 u32 reqfid, u32 reqvid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 if (core_voltage_pre_transition(data, reqvid))
306 return 1;
307
308 if (core_frequency_transition(data, reqfid))
309 return 1;
310
311 if (core_voltage_post_transition(data, reqvid))
312 return 1;
313
314 if (query_current_values_with_pending_wait(data))
315 return 1;
316
317 if ((reqfid != data->currfid) || (reqvid != data->currvid)) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500318 printk(KERN_ERR PFX "failed (cpu%d): req 0x%x 0x%x, "
319 "curr 0x%x 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 smp_processor_id(),
321 reqfid, reqvid, data->currfid, data->currvid);
322 return 1;
323 }
324
325 dprintk("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
326 smp_processor_id(), data->currfid, data->currvid);
327
328 return 0;
329}
330
331/* Phase 1 - core voltage transition ... setup voltage */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500332static int core_voltage_pre_transition(struct powernow_k8_data *data,
333 u32 reqvid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 u32 rvosteps = data->rvo;
336 u32 savefid = data->currfid;
Dave Jones065b8072005-05-31 19:03:46 -0700337 u32 maxvid, lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Dave Jones0e64a0c2009-02-04 14:37:50 -0500339 dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, "
340 "reqvid 0x%x, rvo 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 smp_processor_id(),
342 data->currfid, data->currvid, reqvid, data->rvo);
343
Dave Jones065b8072005-05-31 19:03:46 -0700344 rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
345 maxvid = 0x1f & (maxvid >> 16);
346 dprintk("ph1 maxvid=0x%x\n", maxvid);
347 if (reqvid < maxvid) /* lower numbers are higher voltages */
348 reqvid = maxvid;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 while (data->currvid > reqvid) {
351 dprintk("ph1: curr 0x%x, req vid 0x%x\n",
352 data->currvid, reqvid);
353 if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
354 return 1;
355 }
356
Dave Jones065b8072005-05-31 19:03:46 -0700357 while ((rvosteps > 0) && ((data->rvo + data->currvid) > reqvid)) {
358 if (data->currvid == maxvid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 rvosteps = 0;
360 } else {
361 dprintk("ph1: changing vid for rvo, req 0x%x\n",
362 data->currvid - 1);
Dave Jones0e64a0c2009-02-04 14:37:50 -0500363 if (decrease_vid_code_by_step(data, data->currvid-1, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 1;
365 rvosteps--;
366 }
367 }
368
369 if (query_current_values_with_pending_wait(data))
370 return 1;
371
372 if (savefid != data->currfid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500373 printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n",
374 data->currfid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 1;
376 }
377
378 dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
379 data->currfid, data->currvid);
380
381 return 0;
382}
383
384/* Phase 2 - core frequency transition */
385static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
386{
Dave Jones0e64a0c2009-02-04 14:37:50 -0500387 u32 vcoreqfid, vcocurrfid, vcofiddiff;
388 u32 fid_interval, savevid = data->currvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Dave Jones0e64a0c2009-02-04 14:37:50 -0500390 if ((reqfid < HI_FID_TABLE_BOTTOM) &&
391 (data->currfid < HI_FID_TABLE_BOTTOM)) {
392 printk(KERN_ERR PFX "ph2: illegal lo-lo transition "
393 "0x%x 0x%x\n", reqfid, data->currfid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return 1;
395 }
396
397 if (data->currfid == reqfid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500398 printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n",
399 data->currfid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0;
401 }
402
Dave Jones0e64a0c2009-02-04 14:37:50 -0500403 dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, "
404 "reqfid 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 smp_processor_id(),
406 data->currfid, data->currvid, reqfid);
407
408 vcoreqfid = convert_fid_to_vco_fid(reqfid);
409 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
410 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
411 : vcoreqfid - vcocurrfid;
412
413 while (vcofiddiff > 2) {
Langsdorf, Mark019a61b2005-11-29 14:18:03 -0600414 (data->currfid & 1) ? (fid_interval = 1) : (fid_interval = 2);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (reqfid > data->currfid) {
417 if (data->currfid > LO_FID_TABLE_TOP) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500418 if (write_new_fid(data,
419 data->currfid + fid_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 } else {
422 if (write_new_fid
Dave Jones0e64a0c2009-02-04 14:37:50 -0500423 (data,
424 2 + convert_fid_to_vco_fid(data->currfid)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 } else {
Langsdorf, Mark019a61b2005-11-29 14:18:03 -0600428 if (write_new_fid(data, data->currfid - fid_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 1;
430 }
431
432 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
433 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
434 : vcoreqfid - vcocurrfid;
435 }
436
437 if (write_new_fid(data, reqfid))
438 return 1;
439
440 if (query_current_values_with_pending_wait(data))
441 return 1;
442
443 if (data->currfid != reqfid) {
444 printk(KERN_ERR PFX
Dave Jones0e64a0c2009-02-04 14:37:50 -0500445 "ph2: mismatch, failed fid transition, "
446 "curr 0x%x, req 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 data->currfid, reqfid);
448 return 1;
449 }
450
451 if (savevid != data->currvid) {
452 printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
453 savevid, data->currvid);
454 return 1;
455 }
456
457 dprintk("ph2 complete, currfid 0x%x, currvid 0x%x\n",
458 data->currfid, data->currvid);
459
460 return 0;
461}
462
463/* Phase 3 - core voltage transition flow ... jump to the final vid. */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500464static int core_voltage_post_transition(struct powernow_k8_data *data,
465 u32 reqvid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 u32 savefid = data->currfid;
468 u32 savereqvid = reqvid;
469
470 dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
471 smp_processor_id(),
472 data->currfid, data->currvid);
473
474 if (reqvid != data->currvid) {
475 if (write_new_vid(data, reqvid))
476 return 1;
477
478 if (savefid != data->currfid) {
479 printk(KERN_ERR PFX
480 "ph3: bad fid change, save 0x%x, curr 0x%x\n",
481 savefid, data->currfid);
482 return 1;
483 }
484
485 if (data->currvid != reqvid) {
486 printk(KERN_ERR PFX
Dave Jones0e64a0c2009-02-04 14:37:50 -0500487 "ph3: failed vid transition\n, "
488 "req 0x%x, curr 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 reqvid, data->currvid);
490 return 1;
491 }
492 }
493
494 if (query_current_values_with_pending_wait(data))
495 return 1;
496
497 if (savereqvid != data->currvid) {
498 dprintk("ph3 failed, currvid 0x%x\n", data->currvid);
499 return 1;
500 }
501
502 if (savefid != data->currfid) {
503 dprintk("ph3 failed, currfid changed 0x%x\n",
504 data->currfid);
505 return 1;
506 }
507
508 dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
509 data->currfid, data->currvid);
510
511 return 0;
512}
513
514static int check_supported_cpu(unsigned int cpu)
515{
Mike Travisfc0e4742008-04-04 18:11:05 -0700516 cpumask_t oldmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 u32 eax, ebx, ecx, edx;
518 unsigned int rc = 0;
519
520 oldmask = current->cpus_allowed;
Mike Travis0bc3cc02008-07-24 18:21:31 -0700521 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if (smp_processor_id() != cpu) {
Jacob Shin8aae8282005-11-21 07:23:08 -0800524 printk(KERN_ERR PFX "limiting to cpu %u failed\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto out;
526 }
527
528 if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
529 goto out;
530
531 eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
Dave Jones1f729e02006-06-04 19:37:58 -0400532 if (((eax & CPUID_XFAM) != CPUID_XFAM_K8) &&
533 ((eax & CPUID_XFAM) < CPUID_XFAM_10H))
Dave Jones2c906ae2006-02-28 00:36:32 -0500534 goto out;
535
Dave Jones1f729e02006-06-04 19:37:58 -0400536 if ((eax & CPUID_XFAM) == CPUID_XFAM_K8) {
537 if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
Dave Jones99fbe1a2007-05-14 18:27:29 -0400538 ((eax & CPUID_XMOD) > CPUID_XMOD_REV_MASK)) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500539 printk(KERN_INFO PFX
540 "Processor cpuid %x not supported\n", eax);
Dave Jones1f729e02006-06-04 19:37:58 -0400541 goto out;
542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Dave Jones1f729e02006-06-04 19:37:58 -0400544 eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
545 if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
546 printk(KERN_INFO PFX
547 "No frequency change capabilities detected\n");
548 goto out;
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Dave Jones1f729e02006-06-04 19:37:58 -0400551 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
Dave Jones0e64a0c2009-02-04 14:37:50 -0500552 if ((edx & P_STATE_TRANSITION_CAPABLE)
553 != P_STATE_TRANSITION_CAPABLE) {
554 printk(KERN_INFO PFX
555 "Power state transitions not supported\n");
Dave Jones1f729e02006-06-04 19:37:58 -0400556 goto out;
557 }
558 } else { /* must be a HW Pstate capable processor */
559 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
560 if ((edx & USE_HW_PSTATE) == USE_HW_PSTATE)
561 cpu_family = CPU_HW_PSTATE;
562 else
563 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
566 rc = 1;
567
568out:
Mike Travisfc0e4742008-04-04 18:11:05 -0700569 set_cpus_allowed_ptr(current, &oldmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Dave Jones0e64a0c2009-02-04 14:37:50 -0500573static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst,
574 u8 maxvid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 unsigned int j;
577 u8 lastfid = 0xff;
578
579 for (j = 0; j < data->numps; j++) {
580 if (pst[j].vid > LEAST_VID) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200581 printk(KERN_ERR FW_BUG PFX "vid %d invalid : 0x%x\n",
582 j, pst[j].vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -EINVAL;
584 }
Dave Jones0e64a0c2009-02-04 14:37:50 -0500585 if (pst[j].vid < data->rvo) {
586 /* vid + rvo >= 0 */
Thomas Renninger2fd47092008-09-01 14:27:03 +0200587 printk(KERN_ERR FW_BUG PFX "0 vid exceeded with pstate"
588 " %d\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return -ENODEV;
590 }
Dave Jones0e64a0c2009-02-04 14:37:50 -0500591 if (pst[j].vid < maxvid + data->rvo) {
592 /* vid + rvo >= maxvid */
Thomas Renninger2fd47092008-09-01 14:27:03 +0200593 printk(KERN_ERR FW_BUG PFX "maxvid exceeded with pstate"
594 " %d\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return -ENODEV;
596 }
Jacob Shin8aae8282005-11-21 07:23:08 -0800597 if (pst[j].fid > MAX_FID) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200598 printk(KERN_ERR FW_BUG PFX "maxfid exceeded with pstate"
599 " %d\n", j);
Jacob Shin8aae8282005-11-21 07:23:08 -0800600 return -ENODEV;
601 }
Jacob Shin8aae8282005-11-21 07:23:08 -0800602 if (j && (pst[j].fid < HI_FID_TABLE_BOTTOM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* Only first fid is allowed to be in "low" range */
Thomas Renninger2fd47092008-09-01 14:27:03 +0200604 printk(KERN_ERR FW_BUG PFX "two low fids - %d : "
605 "0x%x\n", j, pst[j].fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return -EINVAL;
607 }
608 if (pst[j].fid < lastfid)
609 lastfid = pst[j].fid;
610 }
611 if (lastfid & 1) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200612 printk(KERN_ERR FW_BUG PFX "lastfid invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -EINVAL;
614 }
615 if (lastfid > LO_FID_TABLE_TOP)
Dave Jones0e64a0c2009-02-04 14:37:50 -0500616 printk(KERN_INFO FW_BUG PFX
617 "first fid not from lo freq table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 return 0;
620}
621
Dave Jones0e64a0c2009-02-04 14:37:50 -0500622static void invalidate_entry(struct powernow_k8_data *data, unsigned int entry)
623{
624 data->powernow_table[entry].frequency = CPUFREQ_ENTRY_INVALID;
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static void print_basics(struct powernow_k8_data *data)
628{
629 int j;
630 for (j = 0; j < data->numps; j++) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500631 if (data->powernow_table[j].frequency !=
632 CPUFREQ_ENTRY_INVALID) {
Langsdorf, Marke7bdd7a2006-06-08 10:30:17 -0500633 if (cpu_family == CPU_HW_PSTATE) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500634 printk(KERN_INFO PFX
635 " %d : pstate %d (%d MHz)\n", j,
Yinghai Lu4ae5c492007-11-20 19:38:27 -0800636 data->powernow_table[j].index,
Dave Jones9a60ddb2007-07-13 01:34:10 -0400637 data->powernow_table[j].frequency/1000);
Dave Jones1f729e02006-06-04 19:37:58 -0400638 } else {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500639 printk(KERN_INFO PFX
640 " %d : fid 0x%x (%d MHz), vid 0x%x\n",
Dave Jones9a60ddb2007-07-13 01:34:10 -0400641 j,
642 data->powernow_table[j].index & 0xff,
643 data->powernow_table[j].frequency/1000,
644 data->powernow_table[j].index >> 8);
Dave Jones1f729e02006-06-04 19:37:58 -0400645 }
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 if (data->batps)
Dave Jones0e64a0c2009-02-04 14:37:50 -0500649 printk(KERN_INFO PFX "Only %d pstates on battery\n",
650 data->batps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Andreas Herrmannca446d02009-04-22 13:48:33 +0200653static u32 freq_from_fid_did(u32 fid, u32 did)
654{
655 u32 mhz = 0;
656
657 if (boot_cpu_data.x86 == 0x10)
658 mhz = (100 * (fid + 0x10)) >> did;
659 else if (boot_cpu_data.x86 == 0x11)
660 mhz = (100 * (fid + 8)) >> did;
661 else
662 BUG();
663
664 return mhz * 1000;
665}
666
Dave Jones0e64a0c2009-02-04 14:37:50 -0500667static int fill_powernow_table(struct powernow_k8_data *data,
668 struct pst_s *pst, u8 maxvid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct cpufreq_frequency_table *powernow_table;
671 unsigned int j;
672
Dave Jones0e64a0c2009-02-04 14:37:50 -0500673 if (data->batps) {
674 /* use ACPI support to get full speed on mains power */
675 printk(KERN_WARNING PFX
676 "Only %d pstates usable (use ACPI driver for full "
677 "range\n", data->batps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 data->numps = data->batps;
679 }
680
Dave Jones0e64a0c2009-02-04 14:37:50 -0500681 for (j = 1; j < data->numps; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (pst[j-1].fid >= pst[j].fid) {
683 printk(KERN_ERR PFX "PST out of sequence\n");
684 return -EINVAL;
685 }
686 }
687
688 if (data->numps < 2) {
689 printk(KERN_ERR PFX "no p states to transition\n");
690 return -ENODEV;
691 }
692
693 if (check_pst_table(data, pst, maxvid))
694 return -EINVAL;
695
696 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
697 * (data->numps + 1)), GFP_KERNEL);
698 if (!powernow_table) {
699 printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
700 return -ENOMEM;
701 }
702
703 for (j = 0; j < data->numps; j++) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500704 int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 powernow_table[j].index = pst[j].fid; /* lower 8 bits */
706 powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500707 freq = find_khz_freq_from_fid(pst[j].fid);
708 powernow_table[j].frequency = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710 powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
711 powernow_table[data->numps].index = 0;
712
713 if (query_current_values_with_pending_wait(data)) {
714 kfree(powernow_table);
715 return -EIO;
716 }
717
718 dprintk("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
719 data->powernow_table = powernow_table;
Rusty Russell7ad728f2009-03-13 14:49:50 +1030720 if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
Mark Langsdorf2e497622007-04-30 14:15:05 -0500721 print_basics(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 for (j = 0; j < data->numps; j++)
Dave Jones0e64a0c2009-02-04 14:37:50 -0500724 if ((pst[j].fid == data->currfid) &&
725 (pst[j].vid == data->currvid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 0;
727
728 dprintk("currfid/vid do not match PST, ignoring\n");
729 return 0;
730}
731
732/* Find and validate the PSB/PST table in BIOS. */
733static int find_psb_table(struct powernow_k8_data *data)
734{
735 struct psb_s *psb;
736 unsigned int i;
737 u32 mvs;
738 u8 maxvid;
739 u32 cpst = 0;
740 u32 thiscpuid;
741
742 for (i = 0xc0000; i < 0xffff0; i += 0x10) {
743 /* Scan BIOS looking for the signature. */
744 /* It can not be at ffff0 - it is too big. */
745
746 psb = phys_to_virt(i);
747 if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
748 continue;
749
750 dprintk("found PSB header at 0x%p\n", psb);
751
752 dprintk("table vers: 0x%x\n", psb->tableversion);
753 if (psb->tableversion != PSB_VERSION_1_4) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200754 printk(KERN_ERR FW_BUG PFX "PSB table is not v1.4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -ENODEV;
756 }
757
758 dprintk("flags: 0x%x\n", psb->flags1);
759 if (psb->flags1) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200760 printk(KERN_ERR FW_BUG PFX "unknown flags\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return -ENODEV;
762 }
763
764 data->vstable = psb->vstable;
Dave Jones0e64a0c2009-02-04 14:37:50 -0500765 dprintk("voltage stabilization time: %d(*20us)\n",
766 data->vstable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 dprintk("flags2: 0x%x\n", psb->flags2);
769 data->rvo = psb->flags2 & 3;
770 data->irt = ((psb->flags2) >> 2) & 3;
771 mvs = ((psb->flags2) >> 4) & 3;
772 data->vidmvs = 1 << mvs;
773 data->batps = ((psb->flags2) >> 6) & 3;
774
775 dprintk("ramp voltage offset: %d\n", data->rvo);
776 dprintk("isochronous relief time: %d\n", data->irt);
777 dprintk("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
778
779 dprintk("numpst: 0x%x\n", psb->num_tables);
780 cpst = psb->num_tables;
Dave Jones0e64a0c2009-02-04 14:37:50 -0500781 if ((psb->cpuid == 0x00000fc0) ||
782 (psb->cpuid == 0x00000fe0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
Dave Jones0e64a0c2009-02-04 14:37:50 -0500784 if ((thiscpuid == 0x00000fc0) ||
785 (thiscpuid == 0x00000fe0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 cpst = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788 if (cpst != 1) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200789 printk(KERN_ERR FW_BUG PFX "numpst must be 1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return -ENODEV;
791 }
792
793 data->plllock = psb->plllocktime;
794 dprintk("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
795 dprintk("maxfid: 0x%x\n", psb->maxfid);
796 dprintk("maxvid: 0x%x\n", psb->maxvid);
797 maxvid = psb->maxvid;
798
799 data->numps = psb->numps;
800 dprintk("numpstates: 0x%x\n", data->numps);
Dave Jones0e64a0c2009-02-04 14:37:50 -0500801 return fill_powernow_table(data,
802 (struct pst_s *)(psb+1), maxvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 /*
805 * If you see this message, complain to BIOS manufacturer. If
806 * he tells you "we do not support Linux" or some similar
807 * nonsense, remember that Windows 2000 uses the same legacy
808 * mechanism that the old Linux PSB driver uses. Tell them it
809 * is broken with Windows 2000.
810 *
811 * The reference to the AMD documentation is chapter 9 in the
812 * BIOS and Kernel Developer's Guide, which is available on
813 * www.amd.com
814 */
Thomas Renninger79cc56a2009-02-04 11:56:11 +0100815 printk(KERN_ERR FW_BUG PFX "No PSB or ACPI _PSS objects\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return -ENODEV;
817}
818
Dave Jones0e64a0c2009-02-04 14:37:50 -0500819static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data,
820 unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Dave Jones0e64a0c2009-02-04 14:37:50 -0500822 acpi_integer control;
823
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700824 if (!data->acpi_data.state_count || (cpu_family == CPU_HW_PSTATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return;
826
Luis Henriques21335d02009-04-23 19:45:04 +0100827 control = data->acpi_data.states[index].control;
828 data->irt = (control >> IRT_SHIFT) & IRT_MASK;
829 data->rvo = (control >> RVO_SHIFT) & RVO_MASK;
830 data->exttype = (control >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
831 data->plllock = (control >> PLL_L_SHIFT) & PLL_L_MASK;
832 data->vidmvs = 1 << ((control >> MVS_SHIFT) & MVS_MASK);
833 data->vstable = (control >> VST_SHIFT) & VST_MASK;
834}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
837{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct cpufreq_frequency_table *powernow_table;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800839 int ret_val = -ENODEV;
Dave Jones2c701b12009-06-05 12:37:07 -0400840 acpi_integer control, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700842 if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
Dave Jones065b8072005-05-31 19:03:46 -0700843 dprintk("register performance failed: bad ACPI data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EIO;
845 }
846
847 /* verify the data contained in the ACPI structures */
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700848 if (data->acpi_data.state_count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 dprintk("No ACPI P-States\n");
850 goto err_out;
851 }
852
Dave Jones2c701b12009-06-05 12:37:07 -0400853 control = data->acpi_data.control_register.space_id;
854 status = data->acpi_data.status_register.space_id;
855
856 if ((control != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
857 (status != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 dprintk("Invalid control/status registers (%x - %x)\n",
Dave Jones2c701b12009-06-05 12:37:07 -0400859 control, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto err_out;
861 }
862
863 /* fill in data->powernow_table */
864 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700865 * (data->acpi_data.state_count + 1)), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (!powernow_table) {
867 dprintk("powernow_table memory alloc failure\n");
868 goto err_out;
869 }
870
Langsdorf, Marke7bdd7a2006-06-08 10:30:17 -0500871 if (cpu_family == CPU_HW_PSTATE)
Dave Jones1f729e02006-06-04 19:37:58 -0400872 ret_val = fill_powernow_table_pstate(data, powernow_table);
873 else
874 ret_val = fill_powernow_table_fidvid(data, powernow_table);
875 if (ret_val)
876 goto err_out_mem;
877
Dave Jones0e64a0c2009-02-04 14:37:50 -0500878 powernow_table[data->acpi_data.state_count].frequency =
879 CPUFREQ_TABLE_END;
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700880 powernow_table[data->acpi_data.state_count].index = 0;
Dave Jones1f729e02006-06-04 19:37:58 -0400881 data->powernow_table = powernow_table;
882
883 /* fill in data */
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700884 data->numps = data->acpi_data.state_count;
Rusty Russell7ad728f2009-03-13 14:49:50 +1030885 if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
Mark Langsdorf2e497622007-04-30 14:15:05 -0500886 print_basics(data);
Dave Jones1f729e02006-06-04 19:37:58 -0400887 powernow_k8_acpi_pst_values(data, 0);
888
889 /* notify BIOS that we exist */
890 acpi_processor_notify_smm(THIS_MODULE);
891
Yinghai Lueaa95842009-06-06 14:51:36 -0700892 if (!zalloc_cpumask_var(&data->acpi_data.shared_cpu_map, GFP_KERNEL)) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800893 printk(KERN_ERR PFX
894 "unable to alloc powernow_k8_data cpumask\n");
895 ret_val = -ENOMEM;
896 goto err_out_mem;
897 }
898
Dave Jones1f729e02006-06-04 19:37:58 -0400899 return 0;
900
901err_out_mem:
902 kfree(powernow_table);
903
904err_out:
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700905 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
Dave Jones1f729e02006-06-04 19:37:58 -0400906
Dave Jones0e64a0c2009-02-04 14:37:50 -0500907 /* data->acpi_data.state_count informs us at ->exit()
908 * whether ACPI was used */
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700909 data->acpi_data.state_count = 0;
Dave Jones1f729e02006-06-04 19:37:58 -0400910
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800911 return ret_val;
Dave Jones1f729e02006-06-04 19:37:58 -0400912}
913
Dave Jones0e64a0c2009-02-04 14:37:50 -0500914static int fill_powernow_table_pstate(struct powernow_k8_data *data,
915 struct cpufreq_frequency_table *powernow_table)
Dave Jones1f729e02006-06-04 19:37:58 -0400916{
917 int i;
Mark Langsdorfc5829cd2007-10-17 16:52:08 -0500918 u32 hi = 0, lo = 0;
919 rdmsr(MSR_PSTATE_CUR_LIMIT, hi, lo);
920 data->max_hw_pstate = (hi & HW_PSTATE_MAX_MASK) >> HW_PSTATE_MAX_SHIFT;
Dave Jones1f729e02006-06-04 19:37:58 -0400921
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700922 for (i = 0; i < data->acpi_data.state_count; i++) {
Dave Jones1f729e02006-06-04 19:37:58 -0400923 u32 index;
Dave Jones1f729e02006-06-04 19:37:58 -0400924
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700925 index = data->acpi_data.states[i].control & HW_PSTATE_MASK;
Mark Langsdorfc5829cd2007-10-17 16:52:08 -0500926 if (index > data->max_hw_pstate) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500927 printk(KERN_ERR PFX "invalid pstate %d - "
928 "bad value %d.\n", i, index);
929 printk(KERN_ERR PFX "Please report to BIOS "
930 "manufacturer\n");
931 invalidate_entry(data, i);
Mark Langsdorfc5829cd2007-10-17 16:52:08 -0500932 continue;
Dave Jones1f729e02006-06-04 19:37:58 -0400933 }
934 rdmsr(MSR_PSTATE_DEF_BASE + index, lo, hi);
935 if (!(hi & HW_PSTATE_VALID_MASK)) {
936 dprintk("invalid pstate %d, ignoring\n", index);
Dave Jones0e64a0c2009-02-04 14:37:50 -0500937 invalidate_entry(data, i);
Dave Jones1f729e02006-06-04 19:37:58 -0400938 continue;
939 }
940
Mark Langsdorfc5829cd2007-10-17 16:52:08 -0500941 powernow_table[i].index = index;
Dave Jones1f729e02006-06-04 19:37:58 -0400942
Andreas Herrmannca446d02009-04-22 13:48:33 +0200943 /* Frequency may be rounded for these */
944 if (boot_cpu_data.x86 == 0x10 || boot_cpu_data.x86 == 0x11) {
945 powernow_table[i].frequency =
946 freq_from_fid_did(lo & 0x3f, (lo >> 6) & 7);
947 } else
948 powernow_table[i].frequency =
949 data->acpi_data.states[i].core_frequency * 1000;
Dave Jones1f729e02006-06-04 19:37:58 -0400950 }
951 return 0;
952}
953
Dave Jones0e64a0c2009-02-04 14:37:50 -0500954static int fill_powernow_table_fidvid(struct powernow_k8_data *data,
955 struct cpufreq_frequency_table *powernow_table)
Dave Jones1f729e02006-06-04 19:37:58 -0400956{
957 int i;
958 int cntlofreq = 0;
Dave Jones0e64a0c2009-02-04 14:37:50 -0500959
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700960 for (i = 0; i < data->acpi_data.state_count; i++) {
Dave Jones094ce7f2005-07-29 12:55:40 -0700961 u32 fid;
962 u32 vid;
Dave Jones0e64a0c2009-02-04 14:37:50 -0500963 u32 freq, index;
964 acpi_integer status, control;
Dave Jones094ce7f2005-07-29 12:55:40 -0700965
966 if (data->exttype) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500967 status = data->acpi_data.states[i].status;
968 fid = status & EXT_FID_MASK;
969 vid = (status >> VID_SHIFT) & EXT_VID_MASK;
Dave Jones841e40b2005-07-28 09:40:04 -0700970 } else {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500971 control = data->acpi_data.states[i].control;
972 fid = control & FID_MASK;
973 vid = (control >> VID_SHIFT) & VID_MASK;
Dave Jones841e40b2005-07-28 09:40:04 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 dprintk(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
977
Dave Jones0e64a0c2009-02-04 14:37:50 -0500978 index = fid | (vid<<8);
979 powernow_table[i].index = index;
980
981 freq = find_khz_freq_from_fid(fid);
982 powernow_table[i].frequency = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 /* verify frequency is OK */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500985 if ((freq > (MAX_FREQ * 1000)) || (freq < (MIN_FREQ * 1000))) {
986 dprintk("invalid freq %u kHz, ignoring\n", freq);
987 invalidate_entry(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 continue;
989 }
990
Dave Jones0e64a0c2009-02-04 14:37:50 -0500991 /* verify voltage is OK -
992 * BIOSs are using "off" to indicate invalid */
Dave Jones841e40b2005-07-28 09:40:04 -0700993 if (vid == VID_OFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 dprintk("invalid vid %u, ignoring\n", vid);
Dave Jones0e64a0c2009-02-04 14:37:50 -0500995 invalidate_entry(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 continue;
997 }
998
Dave Jones065b8072005-05-31 19:03:46 -0700999 /* verify only 1 entry from the lo frequency table */
1000 if (fid < HI_FID_TABLE_BOTTOM) {
1001 if (cntlofreq) {
Dave Jones0e64a0c2009-02-04 14:37:50 -05001002 /* if both entries are the same,
1003 * ignore this one ... */
1004 if ((freq != powernow_table[cntlofreq].frequency) ||
1005 (index != powernow_table[cntlofreq].index)) {
1006 printk(KERN_ERR PFX
1007 "Too many lo freq table "
1008 "entries\n");
Dave Jones1f729e02006-06-04 19:37:58 -04001009 return 1;
Dave Jones065b8072005-05-31 19:03:46 -07001010 }
1011
Dave Jones0e64a0c2009-02-04 14:37:50 -05001012 dprintk("double low frequency table entry, "
1013 "ignoring it.\n");
1014 invalidate_entry(data, i);
Dave Jones065b8072005-05-31 19:03:46 -07001015 continue;
1016 } else
1017 cntlofreq = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019
Dave Jones0e64a0c2009-02-04 14:37:50 -05001020 if (freq != (data->acpi_data.states[i].core_frequency * 1000)) {
1021 printk(KERN_INFO PFX "invalid freq entries "
1022 "%u kHz vs. %u kHz\n", freq,
1023 (unsigned int)
1024 (data->acpi_data.states[i].core_frequency
1025 * 1000));
1026 invalidate_entry(data, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 continue;
1028 }
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
1034{
Linus Torvaldsf607e3a2008-08-19 13:34:59 -07001035 if (data->acpi_data.state_count)
Dave Jones0e64a0c2009-02-04 14:37:50 -05001036 acpi_processor_unregister_performance(&data->acpi_data,
1037 data->cpu);
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001038 free_cpumask_var(data->acpi_data.shared_cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Mark Langsdorf732553e2009-02-03 17:46:43 +01001041static int get_transition_latency(struct powernow_k8_data *data)
1042{
1043 int max_latency = 0;
1044 int i;
1045 for (i = 0; i < data->acpi_data.state_count; i++) {
1046 int cur_latency = data->acpi_data.states[i].transition_latency
1047 + data->acpi_data.states[i].bus_master_latency;
1048 if (cur_latency > max_latency)
1049 max_latency = cur_latency;
1050 }
Thomas Renninger86e13682009-04-22 13:48:30 +02001051 if (max_latency == 0) {
1052 /*
1053 * Fam 11h always returns 0 as transition latency.
1054 * This is intended and means "very fast". While cpufreq core
1055 * and governors currently can handle that gracefully, better
1056 * set it to 1 to avoid problems in the future.
1057 * For all others it's a BIOS bug.
1058 */
1059 if (!boot_cpu_data.x86 == 0x11)
1060 printk(KERN_ERR FW_WARN PFX "Invalid zero transition "
1061 "latency\n");
1062 max_latency = 1;
1063 }
Mark Langsdorf732553e2009-02-03 17:46:43 +01001064 /* value in usecs, needs to be in nanoseconds */
1065 return 1000 * max_latency;
1066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/* Take a frequency, and issue the fid/vid transition command */
Dave Jones0e64a0c2009-02-04 14:37:50 -05001069static int transition_frequency_fidvid(struct powernow_k8_data *data,
1070 unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Dave Jones1f729e02006-06-04 19:37:58 -04001072 u32 fid = 0;
1073 u32 vid = 0;
Dave Jones065b8072005-05-31 19:03:46 -07001074 int res, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct cpufreq_freqs freqs;
1076
1077 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
1078
Dave Jones1f729e02006-06-04 19:37:58 -04001079 /* fid/vid correctness check for k8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /* fid are the lower 8 bits of the index we stored into
Dave Jones1f729e02006-06-04 19:37:58 -04001081 * the cpufreq frequency table in find_psb_table, vid
1082 * are the upper 8 bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 fid = data->powernow_table[index].index & 0xFF;
1085 vid = (data->powernow_table[index].index & 0xFF00) >> 8;
1086
1087 dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
1088
1089 if (query_current_values_with_pending_wait(data))
1090 return 1;
1091
1092 if ((data->currvid == vid) && (data->currfid == fid)) {
1093 dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
1094 fid, vid);
1095 return 0;
1096 }
1097
Dave Jones0e64a0c2009-02-04 14:37:50 -05001098 if ((fid < HI_FID_TABLE_BOTTOM) &&
1099 (data->currfid < HI_FID_TABLE_BOTTOM)) {
Dave Jones065b8072005-05-31 19:03:46 -07001100 printk(KERN_ERR PFX
1101 "ignoring illegal change in lo freq table-%x to 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 data->currfid, fid);
1103 return 1;
1104 }
1105
1106 dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
1107 smp_processor_id(), fid, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 freqs.old = find_khz_freq_from_fid(data->currfid);
1109 freqs.new = find_khz_freq_from_fid(fid);
Dave Jones1f729e02006-06-04 19:37:58 -04001110
Mike Travis334ef7a2008-05-12 21:21:13 +02001111 for_each_cpu_mask_nr(i, *(data->available_cores)) {
Dave Jones065b8072005-05-31 19:03:46 -07001112 freqs.cpu = i;
1113 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 res = transition_fid_vid(data, fid, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 freqs.new = find_khz_freq_from_fid(data->currfid);
Dave Jones1f729e02006-06-04 19:37:58 -04001118
Mike Travis334ef7a2008-05-12 21:21:13 +02001119 for_each_cpu_mask_nr(i, *(data->available_cores)) {
Dave Jones1f729e02006-06-04 19:37:58 -04001120 freqs.cpu = i;
1121 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1122 }
1123 return res;
1124}
1125
1126/* Take a frequency, and issue the hardware pstate transition command */
Dave Jones0e64a0c2009-02-04 14:37:50 -05001127static int transition_frequency_pstate(struct powernow_k8_data *data,
1128 unsigned int index)
Dave Jones1f729e02006-06-04 19:37:58 -04001129{
Dave Jones1f729e02006-06-04 19:37:58 -04001130 u32 pstate = 0;
1131 int res, i;
1132 struct cpufreq_freqs freqs;
1133
1134 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
1135
Mark Langsdorfc5829cd2007-10-17 16:52:08 -05001136 /* get MSR index for hardware pstate transition */
Dave Jones1f729e02006-06-04 19:37:58 -04001137 pstate = index & HW_PSTATE_MASK;
Mark Langsdorfc5829cd2007-10-17 16:52:08 -05001138 if (pstate > data->max_hw_pstate)
Dave Jones1f729e02006-06-04 19:37:58 -04001139 return 0;
Dave Jones0e64a0c2009-02-04 14:37:50 -05001140 freqs.old = find_khz_freq_from_pstate(data->powernow_table,
1141 data->currpstate);
Mark Langsdorfc5829cd2007-10-17 16:52:08 -05001142 freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
Dave Jones1f729e02006-06-04 19:37:58 -04001143
Mike Travis334ef7a2008-05-12 21:21:13 +02001144 for_each_cpu_mask_nr(i, *(data->available_cores)) {
Dave Jones1f729e02006-06-04 19:37:58 -04001145 freqs.cpu = i;
1146 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1147 }
1148
1149 res = transition_pstate(data, pstate);
Mark Langsdorfc5829cd2007-10-17 16:52:08 -05001150 freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
Dave Jones1f729e02006-06-04 19:37:58 -04001151
Mike Travis334ef7a2008-05-12 21:21:13 +02001152 for_each_cpu_mask_nr(i, *(data->available_cores)) {
Dave Jones065b8072005-05-31 19:03:46 -07001153 freqs.cpu = i;
1154 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
Dave Jones2e3f8fa2006-05-30 17:25:14 -04001155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return res;
1157}
1158
1159/* Driver entry point to switch to the target frequency */
Dave Jones0e64a0c2009-02-04 14:37:50 -05001160static int powernowk8_target(struct cpufreq_policy *pol,
1161 unsigned targfreq, unsigned relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Mike Travisfc0e4742008-04-04 18:11:05 -07001163 cpumask_t oldmask;
travis@sgi.com2c6b8c02008-01-30 13:33:11 +01001164 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
Adrian Bunk91800532006-04-19 00:07:28 +02001165 u32 checkfid;
1166 u32 checkvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 unsigned int newstate;
1168 int ret = -EIO;
1169
Jacob Shin4211a302006-04-07 19:49:51 +02001170 if (!data)
1171 return -EINVAL;
1172
Adrian Bunk91800532006-04-19 00:07:28 +02001173 checkfid = data->currfid;
1174 checkvid = data->currvid;
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /* only run on specific CPU from here on */
1177 oldmask = current->cpus_allowed;
Mike Travis0bc3cc02008-07-24 18:21:31 -07001178 set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 if (smp_processor_id() != pol->cpu) {
Jacob Shin8aae8282005-11-21 07:23:08 -08001181 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 goto err_out;
1183 }
1184
1185 if (pending_bit_stuck()) {
1186 printk(KERN_ERR PFX "failing targ, change pending bit set\n");
1187 goto err_out;
1188 }
1189
1190 dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
1191 pol->cpu, targfreq, pol->min, pol->max, relation);
1192
Dave Jones83844512006-05-30 17:43:54 -04001193 if (query_current_values_with_pending_wait(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Mark Langsdorfc5829cd2007-10-17 16:52:08 -05001196 if (cpu_family != CPU_HW_PSTATE) {
Dave Jones1f729e02006-06-04 19:37:58 -04001197 dprintk("targ: curr fid 0x%x, vid 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 data->currfid, data->currvid);
1199
Dave Jones0e64a0c2009-02-04 14:37:50 -05001200 if ((checkvid != data->currvid) ||
1201 (checkfid != data->currfid)) {
Dave Jones1f729e02006-06-04 19:37:58 -04001202 printk(KERN_INFO PFX
Dave Jones0e64a0c2009-02-04 14:37:50 -05001203 "error - out of sync, fix 0x%x 0x%x, "
1204 "vid 0x%x 0x%x\n",
1205 checkfid, data->currfid,
1206 checkvid, data->currvid);
Dave Jones1f729e02006-06-04 19:37:58 -04001207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
Dave Jones0e64a0c2009-02-04 14:37:50 -05001210 if (cpufreq_frequency_table_target(pol, data->powernow_table,
1211 targfreq, relation, &newstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto err_out;
1213
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001214 mutex_lock(&fidvid_mutex);
Dave Jones065b8072005-05-31 19:03:46 -07001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 powernow_k8_acpi_pst_values(data, newstate);
1217
Langsdorf, Marke7bdd7a2006-06-08 10:30:17 -05001218 if (cpu_family == CPU_HW_PSTATE)
Dave Jones1f729e02006-06-04 19:37:58 -04001219 ret = transition_frequency_pstate(data, newstate);
1220 else
1221 ret = transition_frequency_fidvid(data, newstate);
1222 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 printk(KERN_ERR PFX "transition frequency failed\n");
1224 ret = 1;
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001225 mutex_unlock(&fidvid_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 goto err_out;
1227 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001228 mutex_unlock(&fidvid_mutex);
Dave Jones065b8072005-05-31 19:03:46 -07001229
Langsdorf, Marke7bdd7a2006-06-08 10:30:17 -05001230 if (cpu_family == CPU_HW_PSTATE)
Dave Jones0e64a0c2009-02-04 14:37:50 -05001231 pol->cur = find_khz_freq_from_pstate(data->powernow_table,
1232 newstate);
Dave Jones1f729e02006-06-04 19:37:58 -04001233 else
1234 pol->cur = find_khz_freq_from_fid(data->currfid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 ret = 0;
1236
1237err_out:
Mike Travisfc0e4742008-04-04 18:11:05 -07001238 set_cpus_allowed_ptr(current, &oldmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return ret;
1240}
1241
1242/* Driver entry point to verify the policy and range of frequencies */
1243static int powernowk8_verify(struct cpufreq_policy *pol)
1244{
travis@sgi.com2c6b8c02008-01-30 13:33:11 +01001245 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Jacob Shin4211a302006-04-07 19:49:51 +02001247 if (!data)
1248 return -EINVAL;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return cpufreq_frequency_table_verify(pol, data->powernow_table);
1251}
1252
1253/* per CPU init entry point to the driver */
Andi Kleenaa41eb92006-01-16 01:56:36 +01001254static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Andrew Mortonb394f1d2009-06-10 12:41:37 -07001256 static const char ACPI_PSS_BIOS_BUG_MSG[] =
1257 KERN_ERR FW_BUG PFX "No compatible ACPI _PSS objects found.\n"
1258 KERN_ERR FW_BUG PFX "Try again with latest BIOS.\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct powernow_k8_data *data;
Linus Torvaldsf607e3a2008-08-19 13:34:59 -07001260 cpumask_t oldmask;
Andi Kleend7fa7062006-04-07 19:49:48 +02001261 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jacob Shin8aae8282005-11-21 07:23:08 -08001263 if (!cpu_online(pol->cpu))
1264 return -ENODEV;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (!check_supported_cpu(pol->cpu))
1267 return -ENODEV;
1268
Dave Jonesbfdc7082005-10-20 15:16:15 -07001269 data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (!data) {
1271 printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
1272 return -ENOMEM;
1273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 data->cpu = pol->cpu;
Andreas Herrmanna266d9f2008-11-21 14:49:25 +01001276 data->currpstate = HW_PSTATE_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Rusty Russella0abd522009-02-16 17:31:58 -06001278 if (powernow_k8_cpu_init_acpi(data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
1280 * Use the PSB BIOS structure. This is only availabe on
1281 * an UP version, and is deprecated by AMD.
1282 */
Randy Dunlap9ed059e2006-06-20 22:32:56 -07001283 if (num_online_cpus() != 1) {
Thomas Renningerdf182972009-04-22 13:48:32 +02001284 printk_once(ACPI_PSS_BIOS_BUG_MSG);
Dave Jones0cb8bc22009-02-18 14:11:00 -05001285 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287 if (pol->cpu != 0) {
Thomas Renninger2fd47092008-09-01 14:27:03 +02001288 printk(KERN_ERR FW_BUG PFX "No ACPI _PSS objects for "
1289 "CPU other than CPU0. Complain to your BIOS "
1290 "vendor.\n");
Dave Jones0cb8bc22009-02-18 14:11:00 -05001291 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293 rc = find_psb_table(data);
Dave Jones0cb8bc22009-02-18 14:11:00 -05001294 if (rc)
1295 goto err_out;
1296
Mark Langsdorf732553e2009-02-03 17:46:43 +01001297 /* Take a crude guess here.
1298 * That guess was in microseconds, so multiply with 1000 */
1299 pol->cpuinfo.transition_latency = (
1300 ((data->rvo + 8) * data->vstable * VST_UNITS_20US) +
1301 ((1 << data->irt) * 30)) * 1000;
1302 } else /* ACPI _PSS objects available */
1303 pol->cpuinfo.transition_latency = get_transition_latency(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /* only run on specific CPU from here on */
1306 oldmask = current->cpus_allowed;
Mike Travis0bc3cc02008-07-24 18:21:31 -07001307 set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 if (smp_processor_id() != pol->cpu) {
Jacob Shin8aae8282005-11-21 07:23:08 -08001310 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
Dave Jones0cb8bc22009-02-18 14:11:00 -05001311 goto err_out_unmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
1314 if (pending_bit_stuck()) {
1315 printk(KERN_ERR PFX "failing init, change pending bit set\n");
Dave Jones0cb8bc22009-02-18 14:11:00 -05001316 goto err_out_unmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318
1319 if (query_current_values_with_pending_wait(data))
Dave Jones0cb8bc22009-02-18 14:11:00 -05001320 goto err_out_unmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Langsdorf, Marke7bdd7a2006-06-08 10:30:17 -05001322 if (cpu_family == CPU_OPTERON)
Dave Jones1f729e02006-06-04 19:37:58 -04001323 fidvid_msr_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /* run on any CPU again */
Mike Travisfc0e4742008-04-04 18:11:05 -07001326 set_cpus_allowed_ptr(current, &oldmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Linus Torvaldsf607e3a2008-08-19 13:34:59 -07001328 if (cpu_family == CPU_HW_PSTATE)
Rusty Russell835481d2009-01-04 05:18:06 -08001329 cpumask_copy(pol->cpus, cpumask_of(pol->cpu));
Linus Torvaldsf607e3a2008-08-19 13:34:59 -07001330 else
Rusty Russell7ad728f2009-03-13 14:49:50 +10301331 cpumask_copy(pol->cpus, cpu_core_mask(pol->cpu));
Rusty Russell835481d2009-01-04 05:18:06 -08001332 data->available_cores = pol->cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Langsdorf, Marke7bdd7a2006-06-08 10:30:17 -05001334 if (cpu_family == CPU_HW_PSTATE)
Dave Jones0e64a0c2009-02-04 14:37:50 -05001335 pol->cur = find_khz_freq_from_pstate(data->powernow_table,
1336 data->currpstate);
Dave Jones1f729e02006-06-04 19:37:58 -04001337 else
1338 pol->cur = find_khz_freq_from_fid(data->currfid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 dprintk("policy current frequency %d kHz\n", pol->cur);
1340
1341 /* min/max the cpu is capable of */
1342 if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
Thomas Renninger2fd47092008-09-01 14:27:03 +02001343 printk(KERN_ERR FW_BUG PFX "invalid powernow_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 powernow_k8_cpu_exit_acpi(data);
1345 kfree(data->powernow_table);
1346 kfree(data);
1347 return -EINVAL;
1348 }
1349
1350 cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
1351
Langsdorf, Marke7bdd7a2006-06-08 10:30:17 -05001352 if (cpu_family == CPU_HW_PSTATE)
Dave Jones0e64a0c2009-02-04 14:37:50 -05001353 dprintk("cpu_init done, current pstate 0x%x\n",
1354 data->currpstate);
Dave Jones1f729e02006-06-04 19:37:58 -04001355 else
1356 dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
1357 data->currfid, data->currvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
travis@sgi.com2c6b8c02008-01-30 13:33:11 +01001359 per_cpu(powernow_data, pol->cpu) = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 return 0;
1362
Dave Jones0cb8bc22009-02-18 14:11:00 -05001363err_out_unmask:
Mike Travisfc0e4742008-04-04 18:11:05 -07001364 set_cpus_allowed_ptr(current, &oldmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 powernow_k8_cpu_exit_acpi(data);
1366
Dave Jones0cb8bc22009-02-18 14:11:00 -05001367err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 kfree(data);
1369 return -ENODEV;
1370}
1371
Dave Jones0e64a0c2009-02-04 14:37:50 -05001372static int __devexit powernowk8_cpu_exit(struct cpufreq_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
travis@sgi.com2c6b8c02008-01-30 13:33:11 +01001374 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 if (!data)
1377 return -EINVAL;
1378
1379 powernow_k8_cpu_exit_acpi(data);
1380
1381 cpufreq_frequency_table_put_attr(pol->cpu);
1382
1383 kfree(data->powernow_table);
1384 kfree(data);
1385
1386 return 0;
1387}
1388
Dave Jones0e64a0c2009-02-04 14:37:50 -05001389static unsigned int powernowk8_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
shin, jacobeef51672006-03-27 09:57:20 -06001391 struct powernow_k8_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 cpumask_t oldmask = current->cpus_allowed;
1393 unsigned int khz = 0;
Dave Jones89c04842008-02-06 21:55:26 -05001394 unsigned int first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Rusty Russell7ad728f2009-03-13 14:49:50 +10301396 first = cpumask_first(cpu_core_mask(cpu));
Dave Jones89c04842008-02-06 21:55:26 -05001397 data = per_cpu(powernow_data, first);
shin, jacobeef51672006-03-27 09:57:20 -06001398
1399 if (!data)
1400 return -EINVAL;
1401
Mike Travis0bc3cc02008-07-24 18:21:31 -07001402 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (smp_processor_id() != cpu) {
Mike Travisfc0e4742008-04-04 18:11:05 -07001404 printk(KERN_ERR PFX
1405 "limiting to CPU %d failed in powernowk8_get\n", cpu);
1406 set_cpus_allowed_ptr(current, &oldmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return 0;
1408 }
Dave Jonesb9111b72005-09-23 11:10:42 -07001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (query_current_values_with_pending_wait(data))
1411 goto out;
1412
Joachim Deguara58389a82007-01-30 16:53:54 +01001413 if (cpu_family == CPU_HW_PSTATE)
Mike Travisfc0e4742008-04-04 18:11:05 -07001414 khz = find_khz_freq_from_pstate(data->powernow_table,
1415 data->currpstate);
Joachim Deguara58389a82007-01-30 16:53:54 +01001416 else
1417 khz = find_khz_freq_from_fid(data->currfid);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Dave Jonesb9111b72005-09-23 11:10:42 -07001420out:
Mike Travisfc0e4742008-04-04 18:11:05 -07001421 set_cpus_allowed_ptr(current, &oldmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return khz;
1423}
1424
Dave Jones0e64a0c2009-02-04 14:37:50 -05001425static struct freq_attr *powernow_k8_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 &cpufreq_freq_attr_scaling_available_freqs,
1427 NULL,
1428};
1429
Linus Torvalds221dee22007-02-26 14:55:48 -08001430static struct cpufreq_driver cpufreq_amd64_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 .verify = powernowk8_verify,
1432 .target = powernowk8_target,
1433 .init = powernowk8_cpu_init,
1434 .exit = __devexit_p(powernowk8_cpu_exit),
1435 .get = powernowk8_get,
1436 .name = "powernow-k8",
1437 .owner = THIS_MODULE,
1438 .attr = powernow_k8_attr,
1439};
1440
1441/* driver entry point for init */
Andi Kleenaa41eb92006-01-16 01:56:36 +01001442static int __cpuinit powernowk8_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 unsigned int i, supported_cpus = 0;
1445
Andrew Mortona7201152006-03-24 03:15:07 -08001446 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (check_supported_cpu(i))
1448 supported_cpus++;
1449 }
1450
1451 if (supported_cpus == num_online_cpus()) {
Dave Jones1f729e02006-06-04 19:37:58 -04001452 printk(KERN_INFO PFX "Found %d %s "
Dave Jones904f7a32007-05-18 13:22:28 -04001453 "processors (%d cpu cores) (" VERSION ")\n",
Yinghai Luc9254012007-08-22 18:44:20 -07001454 num_online_nodes(),
Dave Jones904f7a32007-05-18 13:22:28 -04001455 boot_cpu_data.x86_model_id, supported_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return cpufreq_register_driver(&cpufreq_amd64_driver);
1457 }
1458
1459 return -ENODEV;
1460}
1461
1462/* driver entry point for term */
1463static void __exit powernowk8_exit(void)
1464{
1465 dprintk("exit\n");
1466
1467 cpufreq_unregister_driver(&cpufreq_amd64_driver);
1468}
1469
Dave Jones0e64a0c2009-02-04 14:37:50 -05001470MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and "
1471 "Mark Langsdorf <mark.langsdorf@amd.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
1473MODULE_LICENSE("GPL");
1474
1475late_initcall(powernowk8_init);
1476module_exit(powernowk8_exit);