blob: 0b19faf002eeb30d44c28e56b57a8a907814989d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andreas Herrmannb2bd68e2012-01-06 15:59:33 +01002 * (c) 2003-2012 Advanced Micro Devices, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Your use of this code is subject to the terms and conditions of the
4 * GNU general public license version 2. See "COPYING" or
5 * http://www.gnu.org/licenses/gpl.html
6 *
Andreas Herrmannb2bd68e2012-01-06 15:59:33 +01007 * Maintainer:
8 * Andreas Herrmann <andreas.herrmann3@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>
Pavel Macheka2531292010-07-18 14:27:13 +020013 * (C) 2004 Pavel Machek <pavel@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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 *
Andreas Herrmannb2bd68e2012-01-06 15:59:33 +010021 * Processor information obtained from Chapter 9 (Power and Thermal
22 * Management) of the "BIOS and Kernel Developer's Guide (BKDG) for
23 * the AMD Athlon 64 and AMD Opteron Processors" and section "2.x
24 * Power Management" in BKDGs for newer AMD CPU families.
25 *
26 * Tables for specific CPUs can be inferred from AMD's processor
27 * power and thermal data sheets, (e.g. 30417.pdf, 30430.pdf, 43375.pdf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 */
29
30#include <linux/kernel.h>
31#include <linux/smp.h>
32#include <linux/module.h>
33#include <linux/init.h>
34#include <linux/cpufreq.h>
35#include <linux/slab.h>
36#include <linux/string.h>
Dave Jones065b8072005-05-31 19:03:46 -070037#include <linux/cpumask.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080038#include <linux/sched.h> /* for current / set_cpus_allowed() */
Dave Jones0e64a0c2009-02-04 14:37:50 -050039#include <linux/io.h>
40#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/msr.h>
Andi Kleenfa8031a2012-01-26 00:09:12 +010043#include <asm/cpu_device_id.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/acpi.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080046#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <acpi/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define PFX "powernow-k8: "
Mark Langsdorfc5829cd2007-10-17 16:52:08 -050050#define VERSION "version 2.20.00"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "powernow-k8.h"
52
53/* serialize freq changes */
Ingo Molnar14cc3e22006-03-26 01:37:14 -080054static DEFINE_MUTEX(fidvid_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
travis@sgi.com2c6b8c02008-01-30 13:33:11 +010056static DEFINE_PER_CPU(struct powernow_k8_data *, powernow_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Mark Langsdorfa2fed572010-03-18 18:41:46 +010058static struct cpufreq_driver cpufreq_amd64_driver;
59
Dave Jones065b8072005-05-31 19:03:46 -070060#ifndef CONFIG_SMP
Rusty Russell7ad728f2009-03-13 14:49:50 +103061static inline const struct cpumask *cpu_core_mask(int cpu)
62{
63 return cpumask_of(0);
64}
Dave Jones065b8072005-05-31 19:03:46 -070065#endif
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* Return a frequency in MHz, given an input fid */
68static u32 find_freq_from_fid(u32 fid)
69{
70 return 800 + (fid * 100);
71}
72
73/* Return a frequency in KHz, given an input fid */
74static u32 find_khz_freq_from_fid(u32 fid)
75{
76 return 1000 * find_freq_from_fid(fid);
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Return the vco fid for an input fid
80 *
81 * Each "low" fid has corresponding "high" fid, and you can get to "low" fids
82 * only from corresponding high fids. This returns "high" fid corresponding to
83 * "low" one.
84 */
85static u32 convert_fid_to_vco_fid(u32 fid)
86{
Dave Jones32ee8c32006-02-28 00:43:23 -050087 if (fid < HI_FID_TABLE_BOTTOM)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 8 + (2 * fid);
Dave Jones32ee8c32006-02-28 00:43:23 -050089 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * Return 1 if the pending bit is set. Unless we just instructed the processor
95 * to transition to a new state, seeing this bit set is really bad news.
96 */
97static int pending_bit_stuck(void)
98{
99 u32 lo, hi;
100
101 rdmsr(MSR_FIDVID_STATUS, lo, hi);
102 return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0;
103}
104
105/*
106 * Update the global current fid / vid values from the status msr.
107 * Returns 1 on error.
108 */
109static int query_current_values_with_pending_wait(struct powernow_k8_data *data)
110{
111 u32 lo, hi;
112 u32 i = 0;
113
Dave Jones7153d962005-07-28 09:45:10 -0700114 do {
Dave Jones0213df72005-10-21 17:21:03 -0400115 if (i++ > 10000) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200116 pr_debug("detected change pending stuck\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 1;
118 }
119 rdmsr(MSR_FIDVID_STATUS, lo, hi);
Dave Jones7153d962005-07-28 09:45:10 -0700120 } while (lo & MSR_S_LO_CHANGE_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 data->currvid = hi & MSR_S_HI_CURRENT_VID;
123 data->currfid = lo & MSR_S_LO_CURRENT_FID;
124
125 return 0;
126}
127
128/* the isochronous relief time */
129static void count_off_irt(struct powernow_k8_data *data)
130{
131 udelay((1 << data->irt) * 10);
132 return;
133}
134
Simon Arlott27b46d72007-10-20 01:13:56 +0200135/* the voltage stabilization time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static void count_off_vst(struct powernow_k8_data *data)
137{
138 udelay(data->vstable * VST_UNITS_20US);
139 return;
140}
141
142/* need to init the control msr to a safe value (for each cpu) */
143static void fidvid_msr_init(void)
144{
145 u32 lo, hi;
146 u8 fid, vid;
147
148 rdmsr(MSR_FIDVID_STATUS, lo, hi);
149 vid = hi & MSR_S_HI_CURRENT_VID;
150 fid = lo & MSR_S_LO_CURRENT_FID;
151 lo = fid | (vid << MSR_C_LO_VID_SHIFT);
152 hi = MSR_C_HI_STP_GNT_BENIGN;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200153 pr_debug("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 wrmsr(MSR_FIDVID_CTL, lo, hi);
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* write the new fid value along with the other control fields to the msr */
158static int write_new_fid(struct powernow_k8_data *data, u32 fid)
159{
160 u32 lo;
161 u32 savevid = data->currvid;
Dave Jones0213df72005-10-21 17:21:03 -0400162 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if ((fid & INVALID_FID_MASK) || (data->currvid & INVALID_VID_MASK)) {
165 printk(KERN_ERR PFX "internal error - overflow on fid write\n");
166 return 1;
167 }
168
Dave Jones0e64a0c2009-02-04 14:37:50 -0500169 lo = fid;
170 lo |= (data->currvid << MSR_C_LO_VID_SHIFT);
171 lo |= MSR_C_LO_INIT_FID_VID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200173 pr_debug("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 fid, lo, data->plllock * PLL_LOCK_CONVERSION);
175
Dave Jones0213df72005-10-21 17:21:03 -0400176 do {
177 wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
178 if (i++ > 100) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500179 printk(KERN_ERR PFX
180 "Hardware error - pending bit very stuck - "
181 "no further pstate changes possible\n");
Chris Wright63172cb2005-10-21 16:56:08 -0700182 return 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500183 }
Dave Jones0213df72005-10-21 17:21:03 -0400184 } while (query_current_values_with_pending_wait(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 count_off_irt(data);
187
188 if (savevid != data->currvid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500189 printk(KERN_ERR PFX
190 "vid change on fid trans, old 0x%x, new 0x%x\n",
191 savevid, data->currvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return 1;
193 }
194
195 if (fid != data->currfid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500196 printk(KERN_ERR PFX
197 "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
198 data->currfid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return 1;
200 }
201
202 return 0;
203}
204
205/* Write a new vid to the hardware */
206static int write_new_vid(struct powernow_k8_data *data, u32 vid)
207{
208 u32 lo;
209 u32 savefid = data->currfid;
Dave Jones0213df72005-10-21 17:21:03 -0400210 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
213 printk(KERN_ERR PFX "internal error - overflow on vid write\n");
214 return 1;
215 }
216
Dave Jones0e64a0c2009-02-04 14:37:50 -0500217 lo = data->currfid;
218 lo |= (vid << MSR_C_LO_VID_SHIFT);
219 lo |= MSR_C_LO_INIT_FID_VID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200221 pr_debug("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 vid, lo, STOP_GRANT_5NS);
223
Dave Jones0213df72005-10-21 17:21:03 -0400224 do {
225 wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
Dave Jones6df89002005-11-30 13:33:30 -0800226 if (i++ > 100) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500227 printk(KERN_ERR PFX "internal error - pending bit "
228 "very stuck - no further pstate "
229 "changes possible\n");
Dave Jones6df89002005-11-30 13:33:30 -0800230 return 1;
231 }
Dave Jones0213df72005-10-21 17:21:03 -0400232 } while (query_current_values_with_pending_wait(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (savefid != data->currfid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500235 printk(KERN_ERR PFX "fid changed on vid trans, old "
236 "0x%x new 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 savefid, data->currfid);
238 return 1;
239 }
240
241 if (vid != data->currvid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500242 printk(KERN_ERR PFX "vid trans failed, vid 0x%x, "
243 "curr 0x%x\n",
244 vid, data->currvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 1;
246 }
247
248 return 0;
249}
250
251/*
252 * Reduce the vid by the max of step or reqvid.
253 * Decreasing vid codes represent increasing voltages:
Dave Jones841e40b2005-07-28 09:40:04 -0700254 * 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 -0700255 */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500256static int decrease_vid_code_by_step(struct powernow_k8_data *data,
257 u32 reqvid, u32 step)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 if ((data->currvid - reqvid) > step)
260 reqvid = data->currvid - step;
261
262 if (write_new_vid(data, reqvid))
263 return 1;
264
265 count_off_vst(data);
266
267 return 0;
268}
269
Dave Jones1f729e02006-06-04 19:37:58 -0400270/* Change Opteron/Athlon64 fid and vid, by the 3 phases. */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500271static int transition_fid_vid(struct powernow_k8_data *data,
272 u32 reqfid, u32 reqvid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Mark Langsdorfa2e1b4c2009-07-26 10:55:25 -0500274 if (core_voltage_pre_transition(data, reqvid, reqfid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 1;
276
277 if (core_frequency_transition(data, reqfid))
278 return 1;
279
280 if (core_voltage_post_transition(data, reqvid))
281 return 1;
282
283 if (query_current_values_with_pending_wait(data))
284 return 1;
285
286 if ((reqfid != data->currfid) || (reqvid != data->currvid)) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500287 printk(KERN_ERR PFX "failed (cpu%d): req 0x%x 0x%x, "
288 "curr 0x%x 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 smp_processor_id(),
290 reqfid, reqvid, data->currfid, data->currvid);
291 return 1;
292 }
293
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200294 pr_debug("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 smp_processor_id(), data->currfid, data->currvid);
296
297 return 0;
298}
299
300/* Phase 1 - core voltage transition ... setup voltage */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500301static int core_voltage_pre_transition(struct powernow_k8_data *data,
Mark Langsdorfa2e1b4c2009-07-26 10:55:25 -0500302 u32 reqvid, u32 reqfid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 u32 rvosteps = data->rvo;
305 u32 savefid = data->currfid;
Mark Langsdorfa2e1b4c2009-07-26 10:55:25 -0500306 u32 maxvid, lo, rvomult = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200308 pr_debug("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, "
Dave Jones0e64a0c2009-02-04 14:37:50 -0500309 "reqvid 0x%x, rvo 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 smp_processor_id(),
311 data->currfid, data->currvid, reqvid, data->rvo);
312
Mark Langsdorfa2e1b4c2009-07-26 10:55:25 -0500313 if ((savefid < LO_FID_TABLE_TOP) && (reqfid < LO_FID_TABLE_TOP))
314 rvomult = 2;
315 rvosteps *= rvomult;
Dave Jones065b8072005-05-31 19:03:46 -0700316 rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
317 maxvid = 0x1f & (maxvid >> 16);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200318 pr_debug("ph1 maxvid=0x%x\n", maxvid);
Dave Jones065b8072005-05-31 19:03:46 -0700319 if (reqvid < maxvid) /* lower numbers are higher voltages */
320 reqvid = maxvid;
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 while (data->currvid > reqvid) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200323 pr_debug("ph1: curr 0x%x, req vid 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 data->currvid, reqvid);
325 if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
326 return 1;
327 }
328
Mark Langsdorfa2e1b4c2009-07-26 10:55:25 -0500329 while ((rvosteps > 0) &&
330 ((rvomult * data->rvo + data->currvid) > reqvid)) {
Dave Jones065b8072005-05-31 19:03:46 -0700331 if (data->currvid == maxvid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 rvosteps = 0;
333 } else {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200334 pr_debug("ph1: changing vid for rvo, req 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 data->currvid - 1);
Dave Jones0e64a0c2009-02-04 14:37:50 -0500336 if (decrease_vid_code_by_step(data, data->currvid-1, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 1;
338 rvosteps--;
339 }
340 }
341
342 if (query_current_values_with_pending_wait(data))
343 return 1;
344
345 if (savefid != data->currfid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500346 printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n",
347 data->currfid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 1;
349 }
350
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200351 pr_debug("ph1 complete, currfid 0x%x, currvid 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 data->currfid, data->currvid);
353
354 return 0;
355}
356
357/* Phase 2 - core frequency transition */
358static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
359{
Dave Jones0e64a0c2009-02-04 14:37:50 -0500360 u32 vcoreqfid, vcocurrfid, vcofiddiff;
361 u32 fid_interval, savevid = data->currvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (data->currfid == reqfid) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500364 printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n",
365 data->currfid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367 }
368
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200369 pr_debug("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, "
Dave Jones0e64a0c2009-02-04 14:37:50 -0500370 "reqfid 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 smp_processor_id(),
372 data->currfid, data->currvid, reqfid);
373
374 vcoreqfid = convert_fid_to_vco_fid(reqfid);
375 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
376 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
377 : vcoreqfid - vcocurrfid;
378
Mark Langsdorfa2e1b4c2009-07-26 10:55:25 -0500379 if ((reqfid <= LO_FID_TABLE_TOP) && (data->currfid <= LO_FID_TABLE_TOP))
380 vcofiddiff = 0;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 while (vcofiddiff > 2) {
Langsdorf, Mark019a61b2005-11-29 14:18:03 -0600383 (data->currfid & 1) ? (fid_interval = 1) : (fid_interval = 2);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (reqfid > data->currfid) {
386 if (data->currfid > LO_FID_TABLE_TOP) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500387 if (write_new_fid(data,
388 data->currfid + fid_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 } else {
391 if (write_new_fid
Dave Jones0e64a0c2009-02-04 14:37:50 -0500392 (data,
393 2 + convert_fid_to_vco_fid(data->currfid)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 } else {
Langsdorf, Mark019a61b2005-11-29 14:18:03 -0600397 if (write_new_fid(data, data->currfid - fid_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 1;
399 }
400
401 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
402 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
403 : vcoreqfid - vcocurrfid;
404 }
405
406 if (write_new_fid(data, reqfid))
407 return 1;
408
409 if (query_current_values_with_pending_wait(data))
410 return 1;
411
412 if (data->currfid != reqfid) {
413 printk(KERN_ERR PFX
Dave Jones0e64a0c2009-02-04 14:37:50 -0500414 "ph2: mismatch, failed fid transition, "
415 "curr 0x%x, req 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 data->currfid, reqfid);
417 return 1;
418 }
419
420 if (savevid != data->currvid) {
421 printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
422 savevid, data->currvid);
423 return 1;
424 }
425
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200426 pr_debug("ph2 complete, currfid 0x%x, currvid 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 data->currfid, data->currvid);
428
429 return 0;
430}
431
432/* Phase 3 - core voltage transition flow ... jump to the final vid. */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500433static int core_voltage_post_transition(struct powernow_k8_data *data,
434 u32 reqvid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 u32 savefid = data->currfid;
437 u32 savereqvid = reqvid;
438
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200439 pr_debug("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 smp_processor_id(),
441 data->currfid, data->currvid);
442
443 if (reqvid != data->currvid) {
444 if (write_new_vid(data, reqvid))
445 return 1;
446
447 if (savefid != data->currfid) {
448 printk(KERN_ERR PFX
449 "ph3: bad fid change, save 0x%x, curr 0x%x\n",
450 savefid, data->currfid);
451 return 1;
452 }
453
454 if (data->currvid != reqvid) {
455 printk(KERN_ERR PFX
Dave Jones0e64a0c2009-02-04 14:37:50 -0500456 "ph3: failed vid transition\n, "
457 "req 0x%x, curr 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 reqvid, data->currvid);
459 return 1;
460 }
461 }
462
463 if (query_current_values_with_pending_wait(data))
464 return 1;
465
466 if (savereqvid != data->currvid) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200467 pr_debug("ph3 failed, currvid 0x%x\n", data->currvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 1;
469 }
470
471 if (savefid != data->currfid) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200472 pr_debug("ph3 failed, currfid changed 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 data->currfid);
474 return 1;
475 }
476
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200477 pr_debug("ph3 complete, currfid 0x%x, currvid 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 data->currfid, data->currvid);
479
480 return 0;
481}
482
Andi Kleenfa8031a2012-01-26 00:09:12 +0100483static const struct x86_cpu_id powernow_k8_ids[] = {
484 /* IO based frequency switching */
485 { X86_VENDOR_AMD, 0xf },
Andi Kleenfa8031a2012-01-26 00:09:12 +0100486 {}
487};
488MODULE_DEVICE_TABLE(x86cpu, powernow_k8_ids);
489
Rusty Russell1ff6e972009-06-12 20:55:37 +0930490static void check_supported_cpu(void *_rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 u32 eax, ebx, ecx, edx;
Rusty Russell1ff6e972009-06-12 20:55:37 +0930493 int *rc = _rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Rusty Russell1ff6e972009-06-12 20:55:37 +0930495 *rc = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
Dave Jones2c906ae2006-02-28 00:36:32 -0500498
Dave Jones1f729e02006-06-04 19:37:58 -0400499 if ((eax & CPUID_XFAM) == CPUID_XFAM_K8) {
500 if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
Dave Jones99fbe1a2007-05-14 18:27:29 -0400501 ((eax & CPUID_XMOD) > CPUID_XMOD_REV_MASK)) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500502 printk(KERN_INFO PFX
503 "Processor cpuid %x not supported\n", eax);
Rusty Russell1ff6e972009-06-12 20:55:37 +0930504 return;
Dave Jones1f729e02006-06-04 19:37:58 -0400505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Dave Jones1f729e02006-06-04 19:37:58 -0400507 eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
508 if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
509 printk(KERN_INFO PFX
510 "No frequency change capabilities detected\n");
Rusty Russell1ff6e972009-06-12 20:55:37 +0930511 return;
Dave Jones1f729e02006-06-04 19:37:58 -0400512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Dave Jones1f729e02006-06-04 19:37:58 -0400514 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
Dave Jones0e64a0c2009-02-04 14:37:50 -0500515 if ((edx & P_STATE_TRANSITION_CAPABLE)
516 != P_STATE_TRANSITION_CAPABLE) {
517 printk(KERN_INFO PFX
518 "Power state transitions not supported\n");
Rusty Russell1ff6e972009-06-12 20:55:37 +0930519 return;
Dave Jones1f729e02006-06-04 19:37:58 -0400520 }
Matthew Garrette1f0b8e2012-09-04 08:28:09 +0000521 *rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Dave Jones0e64a0c2009-02-04 14:37:50 -0500525static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst,
526 u8 maxvid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 unsigned int j;
529 u8 lastfid = 0xff;
530
531 for (j = 0; j < data->numps; j++) {
532 if (pst[j].vid > LEAST_VID) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200533 printk(KERN_ERR FW_BUG PFX "vid %d invalid : 0x%x\n",
534 j, pst[j].vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return -EINVAL;
536 }
Dave Jones0e64a0c2009-02-04 14:37:50 -0500537 if (pst[j].vid < data->rvo) {
538 /* vid + rvo >= 0 */
Thomas Renninger2fd47092008-09-01 14:27:03 +0200539 printk(KERN_ERR FW_BUG PFX "0 vid exceeded with pstate"
540 " %d\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -ENODEV;
542 }
Dave Jones0e64a0c2009-02-04 14:37:50 -0500543 if (pst[j].vid < maxvid + data->rvo) {
544 /* vid + rvo >= maxvid */
Thomas Renninger2fd47092008-09-01 14:27:03 +0200545 printk(KERN_ERR FW_BUG PFX "maxvid exceeded with pstate"
546 " %d\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -ENODEV;
548 }
Jacob Shin8aae8282005-11-21 07:23:08 -0800549 if (pst[j].fid > MAX_FID) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200550 printk(KERN_ERR FW_BUG PFX "maxfid exceeded with pstate"
551 " %d\n", j);
Jacob Shin8aae8282005-11-21 07:23:08 -0800552 return -ENODEV;
553 }
Jacob Shin8aae8282005-11-21 07:23:08 -0800554 if (j && (pst[j].fid < HI_FID_TABLE_BOTTOM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Only first fid is allowed to be in "low" range */
Thomas Renninger2fd47092008-09-01 14:27:03 +0200556 printk(KERN_ERR FW_BUG PFX "two low fids - %d : "
557 "0x%x\n", j, pst[j].fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return -EINVAL;
559 }
560 if (pst[j].fid < lastfid)
561 lastfid = pst[j].fid;
562 }
563 if (lastfid & 1) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200564 printk(KERN_ERR FW_BUG PFX "lastfid invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EINVAL;
566 }
567 if (lastfid > LO_FID_TABLE_TOP)
Dave Jones0e64a0c2009-02-04 14:37:50 -0500568 printk(KERN_INFO FW_BUG PFX
569 "first fid not from lo freq table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 return 0;
572}
573
Kurt Roeckxf0adb132009-09-16 11:09:32 -0400574static void invalidate_entry(struct cpufreq_frequency_table *powernow_table,
575 unsigned int entry)
Dave Jones0e64a0c2009-02-04 14:37:50 -0500576{
Kurt Roeckxf0adb132009-09-16 11:09:32 -0400577 powernow_table[entry].frequency = CPUFREQ_ENTRY_INVALID;
Dave Jones0e64a0c2009-02-04 14:37:50 -0500578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static void print_basics(struct powernow_k8_data *data)
581{
582 int j;
583 for (j = 0; j < data->numps; j++) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500584 if (data->powernow_table[j].frequency !=
585 CPUFREQ_ENTRY_INVALID) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500586 printk(KERN_INFO PFX
Thomas Renninger9e918692011-03-03 21:31:24 +0100587 "fid 0x%x (%d MHz), vid 0x%x\n",
Dave Jones9a60ddb2007-07-13 01:34:10 -0400588 data->powernow_table[j].index & 0xff,
589 data->powernow_table[j].frequency/1000,
590 data->powernow_table[j].index >> 8);
Dave Jones1f729e02006-06-04 19:37:58 -0400591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 if (data->batps)
Dave Jones0e64a0c2009-02-04 14:37:50 -0500594 printk(KERN_INFO PFX "Only %d pstates on battery\n",
595 data->batps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Dave Jones0e64a0c2009-02-04 14:37:50 -0500598static int fill_powernow_table(struct powernow_k8_data *data,
599 struct pst_s *pst, u8 maxvid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 struct cpufreq_frequency_table *powernow_table;
602 unsigned int j;
603
Dave Jones0e64a0c2009-02-04 14:37:50 -0500604 if (data->batps) {
605 /* use ACPI support to get full speed on mains power */
606 printk(KERN_WARNING PFX
607 "Only %d pstates usable (use ACPI driver for full "
608 "range\n", data->batps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 data->numps = data->batps;
610 }
611
Dave Jones0e64a0c2009-02-04 14:37:50 -0500612 for (j = 1; j < data->numps; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (pst[j-1].fid >= pst[j].fid) {
614 printk(KERN_ERR PFX "PST out of sequence\n");
615 return -EINVAL;
616 }
617 }
618
619 if (data->numps < 2) {
620 printk(KERN_ERR PFX "no p states to transition\n");
621 return -ENODEV;
622 }
623
624 if (check_pst_table(data, pst, maxvid))
625 return -EINVAL;
626
627 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
628 * (data->numps + 1)), GFP_KERNEL);
629 if (!powernow_table) {
630 printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
631 return -ENOMEM;
632 }
633
634 for (j = 0; j < data->numps; j++) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500635 int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 powernow_table[j].index = pst[j].fid; /* lower 8 bits */
637 powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500638 freq = find_khz_freq_from_fid(pst[j].fid);
639 powernow_table[j].frequency = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
642 powernow_table[data->numps].index = 0;
643
644 if (query_current_values_with_pending_wait(data)) {
645 kfree(powernow_table);
646 return -EIO;
647 }
648
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200649 pr_debug("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 data->powernow_table = powernow_table;
Rusty Russell7ad728f2009-03-13 14:49:50 +1030651 if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
Mark Langsdorf2e497622007-04-30 14:15:05 -0500652 print_basics(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 for (j = 0; j < data->numps; j++)
Dave Jones0e64a0c2009-02-04 14:37:50 -0500655 if ((pst[j].fid == data->currfid) &&
656 (pst[j].vid == data->currvid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return 0;
658
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200659 pr_debug("currfid/vid do not match PST, ignoring\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 0;
661}
662
663/* Find and validate the PSB/PST table in BIOS. */
664static int find_psb_table(struct powernow_k8_data *data)
665{
666 struct psb_s *psb;
667 unsigned int i;
668 u32 mvs;
669 u8 maxvid;
670 u32 cpst = 0;
671 u32 thiscpuid;
672
673 for (i = 0xc0000; i < 0xffff0; i += 0x10) {
674 /* Scan BIOS looking for the signature. */
675 /* It can not be at ffff0 - it is too big. */
676
677 psb = phys_to_virt(i);
678 if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
679 continue;
680
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200681 pr_debug("found PSB header at 0x%p\n", psb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200683 pr_debug("table vers: 0x%x\n", psb->tableversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (psb->tableversion != PSB_VERSION_1_4) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200685 printk(KERN_ERR FW_BUG PFX "PSB table is not v1.4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return -ENODEV;
687 }
688
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200689 pr_debug("flags: 0x%x\n", psb->flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (psb->flags1) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200691 printk(KERN_ERR FW_BUG PFX "unknown flags\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return -ENODEV;
693 }
694
695 data->vstable = psb->vstable;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200696 pr_debug("voltage stabilization time: %d(*20us)\n",
Dave Jones0e64a0c2009-02-04 14:37:50 -0500697 data->vstable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200699 pr_debug("flags2: 0x%x\n", psb->flags2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 data->rvo = psb->flags2 & 3;
701 data->irt = ((psb->flags2) >> 2) & 3;
702 mvs = ((psb->flags2) >> 4) & 3;
703 data->vidmvs = 1 << mvs;
704 data->batps = ((psb->flags2) >> 6) & 3;
705
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200706 pr_debug("ramp voltage offset: %d\n", data->rvo);
707 pr_debug("isochronous relief time: %d\n", data->irt);
708 pr_debug("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200710 pr_debug("numpst: 0x%x\n", psb->num_tables);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 cpst = psb->num_tables;
Dave Jones0e64a0c2009-02-04 14:37:50 -0500712 if ((psb->cpuid == 0x00000fc0) ||
713 (psb->cpuid == 0x00000fe0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
Dave Jones0e64a0c2009-02-04 14:37:50 -0500715 if ((thiscpuid == 0x00000fc0) ||
716 (thiscpuid == 0x00000fe0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 cpst = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719 if (cpst != 1) {
Thomas Renninger2fd47092008-09-01 14:27:03 +0200720 printk(KERN_ERR FW_BUG PFX "numpst must be 1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -ENODEV;
722 }
723
724 data->plllock = psb->plllocktime;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200725 pr_debug("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
726 pr_debug("maxfid: 0x%x\n", psb->maxfid);
727 pr_debug("maxvid: 0x%x\n", psb->maxvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 maxvid = psb->maxvid;
729
730 data->numps = psb->numps;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200731 pr_debug("numpstates: 0x%x\n", data->numps);
Dave Jones0e64a0c2009-02-04 14:37:50 -0500732 return fill_powernow_table(data,
733 (struct pst_s *)(psb+1), maxvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 /*
736 * If you see this message, complain to BIOS manufacturer. If
737 * he tells you "we do not support Linux" or some similar
738 * nonsense, remember that Windows 2000 uses the same legacy
739 * mechanism that the old Linux PSB driver uses. Tell them it
740 * is broken with Windows 2000.
741 *
742 * The reference to the AMD documentation is chapter 9 in the
743 * BIOS and Kernel Developer's Guide, which is available on
744 * www.amd.com
745 */
Thomas Renninger79cc56a2009-02-04 11:56:11 +0100746 printk(KERN_ERR FW_BUG PFX "No PSB or ACPI _PSS objects\n");
Marti Raudsepp298decf2010-01-20 19:19:33 +0200747 printk(KERN_ERR PFX "Make sure that your BIOS is up to date"
748 " and Cool'N'Quiet support is enabled in BIOS setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return -ENODEV;
750}
751
Dave Jones0e64a0c2009-02-04 14:37:50 -0500752static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data,
753 unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Lin Ming439913f2010-01-28 10:53:19 +0800755 u64 control;
Dave Jones0e64a0c2009-02-04 14:37:50 -0500756
Matthew Garrette1f0b8e2012-09-04 08:28:09 +0000757 if (!data->acpi_data.state_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return;
759
Luis Henriques21335d02009-04-23 19:45:04 +0100760 control = data->acpi_data.states[index].control;
761 data->irt = (control >> IRT_SHIFT) & IRT_MASK;
762 data->rvo = (control >> RVO_SHIFT) & RVO_MASK;
763 data->exttype = (control >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
764 data->plllock = (control >> PLL_L_SHIFT) & PLL_L_MASK;
765 data->vidmvs = 1 << ((control >> MVS_SHIFT) & MVS_MASK);
766 data->vstable = (control >> VST_SHIFT) & VST_MASK;
767}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
770{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct cpufreq_frequency_table *powernow_table;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800772 int ret_val = -ENODEV;
Lin Ming439913f2010-01-28 10:53:19 +0800773 u64 control, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700775 if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200776 pr_debug("register performance failed: bad ACPI data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return -EIO;
778 }
779
780 /* verify the data contained in the ACPI structures */
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700781 if (data->acpi_data.state_count <= 1) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200782 pr_debug("No ACPI P-States\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 goto err_out;
784 }
785
Dave Jones2c701b12009-06-05 12:37:07 -0400786 control = data->acpi_data.control_register.space_id;
787 status = data->acpi_data.status_register.space_id;
788
789 if ((control != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
790 (status != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200791 pr_debug("Invalid control/status registers (%llx - %llx)\n",
Dave Jones2c701b12009-06-05 12:37:07 -0400792 control, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto err_out;
794 }
795
796 /* fill in data->powernow_table */
797 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700798 * (data->acpi_data.state_count + 1)), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (!powernow_table) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200800 pr_debug("powernow_table memory alloc failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto err_out;
802 }
803
Mark Langsdorfdb39d552009-08-21 19:15:28 -0500804 /* fill in data */
805 data->numps = data->acpi_data.state_count;
806 powernow_k8_acpi_pst_values(data, 0);
807
Matthew Garrette1f0b8e2012-09-04 08:28:09 +0000808 ret_val = fill_powernow_table_fidvid(data, powernow_table);
Dave Jones1f729e02006-06-04 19:37:58 -0400809 if (ret_val)
810 goto err_out_mem;
811
Dave Jones0e64a0c2009-02-04 14:37:50 -0500812 powernow_table[data->acpi_data.state_count].frequency =
813 CPUFREQ_TABLE_END;
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700814 powernow_table[data->acpi_data.state_count].index = 0;
Dave Jones1f729e02006-06-04 19:37:58 -0400815 data->powernow_table = powernow_table;
816
Rusty Russell7ad728f2009-03-13 14:49:50 +1030817 if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
Mark Langsdorf2e497622007-04-30 14:15:05 -0500818 print_basics(data);
Dave Jones1f729e02006-06-04 19:37:58 -0400819
820 /* notify BIOS that we exist */
821 acpi_processor_notify_smm(THIS_MODULE);
822
Yinghai Lueaa95842009-06-06 14:51:36 -0700823 if (!zalloc_cpumask_var(&data->acpi_data.shared_cpu_map, GFP_KERNEL)) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800824 printk(KERN_ERR PFX
825 "unable to alloc powernow_k8_data cpumask\n");
826 ret_val = -ENOMEM;
827 goto err_out_mem;
828 }
829
Dave Jones1f729e02006-06-04 19:37:58 -0400830 return 0;
831
832err_out_mem:
833 kfree(powernow_table);
834
835err_out:
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700836 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
Dave Jones1f729e02006-06-04 19:37:58 -0400837
Dave Jones0e64a0c2009-02-04 14:37:50 -0500838 /* data->acpi_data.state_count informs us at ->exit()
839 * whether ACPI was used */
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700840 data->acpi_data.state_count = 0;
Dave Jones1f729e02006-06-04 19:37:58 -0400841
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800842 return ret_val;
Dave Jones1f729e02006-06-04 19:37:58 -0400843}
844
Dave Jones0e64a0c2009-02-04 14:37:50 -0500845static int fill_powernow_table_fidvid(struct powernow_k8_data *data,
846 struct cpufreq_frequency_table *powernow_table)
Dave Jones1f729e02006-06-04 19:37:58 -0400847{
848 int i;
Dave Jones0e64a0c2009-02-04 14:37:50 -0500849
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700850 for (i = 0; i < data->acpi_data.state_count; i++) {
Dave Jones094ce7f2005-07-29 12:55:40 -0700851 u32 fid;
852 u32 vid;
Dave Jones0e64a0c2009-02-04 14:37:50 -0500853 u32 freq, index;
Lin Ming439913f2010-01-28 10:53:19 +0800854 u64 status, control;
Dave Jones094ce7f2005-07-29 12:55:40 -0700855
856 if (data->exttype) {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500857 status = data->acpi_data.states[i].status;
858 fid = status & EXT_FID_MASK;
859 vid = (status >> VID_SHIFT) & EXT_VID_MASK;
Dave Jones841e40b2005-07-28 09:40:04 -0700860 } else {
Dave Jones0e64a0c2009-02-04 14:37:50 -0500861 control = data->acpi_data.states[i].control;
862 fid = control & FID_MASK;
863 vid = (control >> VID_SHIFT) & VID_MASK;
Dave Jones841e40b2005-07-28 09:40:04 -0700864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200866 pr_debug(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Dave Jones0e64a0c2009-02-04 14:37:50 -0500868 index = fid | (vid<<8);
869 powernow_table[i].index = index;
870
871 freq = find_khz_freq_from_fid(fid);
872 powernow_table[i].frequency = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /* verify frequency is OK */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500875 if ((freq > (MAX_FREQ * 1000)) || (freq < (MIN_FREQ * 1000))) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200876 pr_debug("invalid freq %u kHz, ignoring\n", freq);
Kurt Roeckxf0adb132009-09-16 11:09:32 -0400877 invalidate_entry(powernow_table, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 continue;
879 }
880
Dave Jones0e64a0c2009-02-04 14:37:50 -0500881 /* verify voltage is OK -
882 * BIOSs are using "off" to indicate invalid */
Dave Jones841e40b2005-07-28 09:40:04 -0700883 if (vid == VID_OFF) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200884 pr_debug("invalid vid %u, ignoring\n", vid);
Kurt Roeckxf0adb132009-09-16 11:09:32 -0400885 invalidate_entry(powernow_table, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 continue;
887 }
888
Dave Jones0e64a0c2009-02-04 14:37:50 -0500889 if (freq != (data->acpi_data.states[i].core_frequency * 1000)) {
890 printk(KERN_INFO PFX "invalid freq entries "
891 "%u kHz vs. %u kHz\n", freq,
892 (unsigned int)
893 (data->acpi_data.states[i].core_frequency
894 * 1000));
Kurt Roeckxf0adb132009-09-16 11:09:32 -0400895 invalidate_entry(powernow_table, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 continue;
897 }
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
903{
Linus Torvaldsf607e3a2008-08-19 13:34:59 -0700904 if (data->acpi_data.state_count)
Dave Jones0e64a0c2009-02-04 14:37:50 -0500905 acpi_processor_unregister_performance(&data->acpi_data,
906 data->cpu);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800907 free_cpumask_var(data->acpi_data.shared_cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Mark Langsdorf732553e2009-02-03 17:46:43 +0100910static int get_transition_latency(struct powernow_k8_data *data)
911{
912 int max_latency = 0;
913 int i;
914 for (i = 0; i < data->acpi_data.state_count; i++) {
915 int cur_latency = data->acpi_data.states[i].transition_latency
916 + data->acpi_data.states[i].bus_master_latency;
917 if (cur_latency > max_latency)
918 max_latency = cur_latency;
919 }
Thomas Renninger86e13682009-04-22 13:48:30 +0200920 if (max_latency == 0) {
Matthew Garrette1f0b8e2012-09-04 08:28:09 +0000921 pr_err(FW_WARN PFX "Invalid zero transition latency\n");
Thomas Renninger86e13682009-04-22 13:48:30 +0200922 max_latency = 1;
923 }
Mark Langsdorf732553e2009-02-03 17:46:43 +0100924 /* value in usecs, needs to be in nanoseconds */
925 return 1000 * max_latency;
926}
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928/* Take a frequency, and issue the fid/vid transition command */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500929static int transition_frequency_fidvid(struct powernow_k8_data *data,
930 unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Dave Jones1f729e02006-06-04 19:37:58 -0400932 u32 fid = 0;
933 u32 vid = 0;
Dave Jones065b8072005-05-31 19:03:46 -0700934 int res, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct cpufreq_freqs freqs;
936
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200937 pr_debug("cpu %d transition to index %u\n", smp_processor_id(), index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Dave Jones1f729e02006-06-04 19:37:58 -0400939 /* fid/vid correctness check for k8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /* fid are the lower 8 bits of the index we stored into
Dave Jones1f729e02006-06-04 19:37:58 -0400941 * the cpufreq frequency table in find_psb_table, vid
942 * are the upper 8 bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 fid = data->powernow_table[index].index & 0xFF;
945 vid = (data->powernow_table[index].index & 0xFF00) >> 8;
946
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200947 pr_debug("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (query_current_values_with_pending_wait(data))
950 return 1;
951
952 if ((data->currvid == vid) && (data->currfid == fid)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200953 pr_debug("target matches current values (fid 0x%x, vid 0x%x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 fid, vid);
955 return 0;
956 }
957
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200958 pr_debug("cpu %d, changing to fid 0x%x, vid 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 smp_processor_id(), fid, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 freqs.old = find_khz_freq_from_fid(data->currfid);
961 freqs.new = find_khz_freq_from_fid(fid);
Dave Jones1f729e02006-06-04 19:37:58 -0400962
Rusty Russell8e7c2592009-06-12 20:58:37 +0930963 for_each_cpu(i, data->available_cores) {
Dave Jones065b8072005-05-31 19:03:46 -0700964 freqs.cpu = i;
965 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 res = transition_fid_vid(data, fid, vid);
Konrad Rzeszutek Wilka9d3d202011-06-16 15:36:39 -0400969 if (res)
970 return res;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 freqs.new = find_khz_freq_from_fid(data->currfid);
Dave Jones1f729e02006-06-04 19:37:58 -0400973
Rusty Russell8e7c2592009-06-12 20:58:37 +0930974 for_each_cpu(i, data->available_cores) {
Dave Jones1f729e02006-06-04 19:37:58 -0400975 freqs.cpu = i;
976 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
977 }
978 return res;
979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981/* Driver entry point to switch to the target frequency */
Dave Jones0e64a0c2009-02-04 14:37:50 -0500982static int powernowk8_target(struct cpufreq_policy *pol,
983 unsigned targfreq, unsigned relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Rusty Russellb8cbe7e2009-11-03 14:57:56 +1030985 cpumask_var_t oldmask;
travis@sgi.com2c6b8c02008-01-30 13:33:11 +0100986 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
Adrian Bunk91800532006-04-19 00:07:28 +0200987 u32 checkfid;
988 u32 checkvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 unsigned int newstate;
990 int ret = -EIO;
991
Jacob Shin4211a302006-04-07 19:49:51 +0200992 if (!data)
993 return -EINVAL;
994
Adrian Bunk91800532006-04-19 00:07:28 +0200995 checkfid = data->currfid;
996 checkvid = data->currvid;
997
Rusty Russellb8cbe7e2009-11-03 14:57:56 +1030998 /* only run on specific CPU from here on. */
999 /* This is poor form: use a workqueue or smp_call_function_single */
1000 if (!alloc_cpumask_var(&oldmask, GFP_KERNEL))
1001 return -ENOMEM;
1002
Rusty Russella4636812009-12-17 11:43:29 -06001003 cpumask_copy(oldmask, tsk_cpus_allowed(current));
Rusty Russellb8cbe7e2009-11-03 14:57:56 +10301004 set_cpus_allowed_ptr(current, cpumask_of(pol->cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 if (smp_processor_id() != pol->cpu) {
Jacob Shin8aae8282005-11-21 07:23:08 -08001007 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto err_out;
1009 }
1010
1011 if (pending_bit_stuck()) {
1012 printk(KERN_ERR PFX "failing targ, change pending bit set\n");
1013 goto err_out;
1014 }
1015
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001016 pr_debug("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 pol->cpu, targfreq, pol->min, pol->max, relation);
1018
Dave Jones83844512006-05-30 17:43:54 -04001019 if (query_current_values_with_pending_wait(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Matthew Garrette1f0b8e2012-09-04 08:28:09 +00001022 pr_debug("targ: curr fid 0x%x, vid 0x%x\n",
1023 data->currfid, data->currvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Matthew Garrette1f0b8e2012-09-04 08:28:09 +00001025 if ((checkvid != data->currvid) ||
1026 (checkfid != data->currfid)) {
1027 pr_info(PFX
1028 "error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
1029 checkfid, data->currfid,
1030 checkvid, data->currvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
Dave Jones0e64a0c2009-02-04 14:37:50 -05001033 if (cpufreq_frequency_table_target(pol, data->powernow_table,
1034 targfreq, relation, &newstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 goto err_out;
1036
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001037 mutex_lock(&fidvid_mutex);
Dave Jones065b8072005-05-31 19:03:46 -07001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 powernow_k8_acpi_pst_values(data, newstate);
1040
Matthew Garrette1f0b8e2012-09-04 08:28:09 +00001041 ret = transition_frequency_fidvid(data, newstate);
1042
Dave Jones1f729e02006-06-04 19:37:58 -04001043 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 printk(KERN_ERR PFX "transition frequency failed\n");
1045 ret = 1;
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001046 mutex_unlock(&fidvid_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto err_out;
1048 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001049 mutex_unlock(&fidvid_mutex);
Dave Jones065b8072005-05-31 19:03:46 -07001050
Matthew Garrette1f0b8e2012-09-04 08:28:09 +00001051 pol->cur = find_khz_freq_from_fid(data->currfid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 ret = 0;
1053
1054err_out:
Rusty Russellb8cbe7e2009-11-03 14:57:56 +10301055 set_cpus_allowed_ptr(current, oldmask);
1056 free_cpumask_var(oldmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return ret;
1058}
1059
1060/* Driver entry point to verify the policy and range of frequencies */
1061static int powernowk8_verify(struct cpufreq_policy *pol)
1062{
travis@sgi.com2c6b8c02008-01-30 13:33:11 +01001063 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Jacob Shin4211a302006-04-07 19:49:51 +02001065 if (!data)
1066 return -EINVAL;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return cpufreq_frequency_table_verify(pol, data->powernow_table);
1069}
1070
Rusty Russell1ff6e972009-06-12 20:55:37 +09301071struct init_on_cpu {
1072 struct powernow_k8_data *data;
1073 int rc;
1074};
1075
1076static void __cpuinit powernowk8_cpu_init_on_cpu(void *_init_on_cpu)
1077{
1078 struct init_on_cpu *init_on_cpu = _init_on_cpu;
1079
1080 if (pending_bit_stuck()) {
1081 printk(KERN_ERR PFX "failing init, change pending bit set\n");
1082 init_on_cpu->rc = -ENODEV;
1083 return;
1084 }
1085
1086 if (query_current_values_with_pending_wait(init_on_cpu->data)) {
1087 init_on_cpu->rc = -ENODEV;
1088 return;
1089 }
1090
Matthew Garrette1f0b8e2012-09-04 08:28:09 +00001091 fidvid_msr_init();
Rusty Russell1ff6e972009-06-12 20:55:37 +09301092
1093 init_on_cpu->rc = 0;
1094}
1095
Borislav Petkov56835e62012-09-05 00:50:26 +02001096static const char missing_pss_msg[] =
1097 KERN_ERR
1098 FW_BUG PFX "No compatible ACPI _PSS objects found.\n"
1099 FW_BUG PFX "First, make sure Cool'N'Quiet is enabled in the BIOS.\n"
1100 FW_BUG PFX "If that doesn't help, try upgrading your BIOS.\n";
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/* per CPU init entry point to the driver */
Andi Kleenaa41eb92006-01-16 01:56:36 +01001103static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 struct powernow_k8_data *data;
Rusty Russell1ff6e972009-06-12 20:55:37 +09301106 struct init_on_cpu init_on_cpu;
Andi Kleend7fa7062006-04-07 19:49:48 +02001107 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Jacob Shin8aae8282005-11-21 07:23:08 -08001109 if (!cpu_online(pol->cpu))
1110 return -ENODEV;
1111
Rusty Russell1ff6e972009-06-12 20:55:37 +09301112 smp_call_function_single(pol->cpu, check_supported_cpu, &rc, 1);
1113 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return -ENODEV;
1115
Dave Jonesbfdc7082005-10-20 15:16:15 -07001116 data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!data) {
1118 printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
1119 return -ENOMEM;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 data->cpu = pol->cpu;
1123
Rusty Russella0abd522009-02-16 17:31:58 -06001124 if (powernow_k8_cpu_init_acpi(data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /*
Lucas De Marchi0d2eb442011-03-17 16:24:16 -03001126 * Use the PSB BIOS structure. This is only available on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 * an UP version, and is deprecated by AMD.
1128 */
Randy Dunlap9ed059e2006-06-20 22:32:56 -07001129 if (num_online_cpus() != 1) {
Borislav Petkov56835e62012-09-05 00:50:26 +02001130 printk_once(missing_pss_msg);
Dave Jones0cb8bc22009-02-18 14:11:00 -05001131 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133 if (pol->cpu != 0) {
Thomas Renninger2fd47092008-09-01 14:27:03 +02001134 printk(KERN_ERR FW_BUG PFX "No ACPI _PSS objects for "
1135 "CPU other than CPU0. Complain to your BIOS "
1136 "vendor.\n");
Dave Jones0cb8bc22009-02-18 14:11:00 -05001137 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139 rc = find_psb_table(data);
Dave Jones0cb8bc22009-02-18 14:11:00 -05001140 if (rc)
1141 goto err_out;
1142
Mark Langsdorf732553e2009-02-03 17:46:43 +01001143 /* Take a crude guess here.
1144 * That guess was in microseconds, so multiply with 1000 */
1145 pol->cpuinfo.transition_latency = (
1146 ((data->rvo + 8) * data->vstable * VST_UNITS_20US) +
1147 ((1 << data->irt) * 30)) * 1000;
1148 } else /* ACPI _PSS objects available */
1149 pol->cpuinfo.transition_latency = get_transition_latency(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /* only run on specific CPU from here on */
Rusty Russell1ff6e972009-06-12 20:55:37 +09301152 init_on_cpu.data = data;
1153 smp_call_function_single(data->cpu, powernowk8_cpu_init_on_cpu,
1154 &init_on_cpu, 1);
1155 rc = init_on_cpu.rc;
1156 if (rc != 0)
1157 goto err_out_exit_acpi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Matthew Garrette1f0b8e2012-09-04 08:28:09 +00001159 cpumask_copy(pol->cpus, cpu_core_mask(pol->cpu));
Rusty Russell835481d2009-01-04 05:18:06 -08001160 data->available_cores = pol->cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Matthew Garrette1f0b8e2012-09-04 08:28:09 +00001162 pol->cur = find_khz_freq_from_fid(data->currfid);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001163 pr_debug("policy current frequency %d kHz\n", pol->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /* min/max the cpu is capable of */
1166 if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
Thomas Renninger2fd47092008-09-01 14:27:03 +02001167 printk(KERN_ERR FW_BUG PFX "invalid powernow_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 powernow_k8_cpu_exit_acpi(data);
1169 kfree(data->powernow_table);
1170 kfree(data);
1171 return -EINVAL;
1172 }
1173
1174 cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
1175
Matthew Garrette1f0b8e2012-09-04 08:28:09 +00001176 pr_debug("cpu_init done, current fid 0x%x, vid 0x%x\n",
1177 data->currfid, data->currvid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
travis@sgi.com2c6b8c02008-01-30 13:33:11 +01001179 per_cpu(powernow_data, pol->cpu) = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 return 0;
1182
Rusty Russell1ff6e972009-06-12 20:55:37 +09301183err_out_exit_acpi:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 powernow_k8_cpu_exit_acpi(data);
1185
Dave Jones0cb8bc22009-02-18 14:11:00 -05001186err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 kfree(data);
1188 return -ENODEV;
1189}
1190
Dave Jones0e64a0c2009-02-04 14:37:50 -05001191static int __devexit powernowk8_cpu_exit(struct cpufreq_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
travis@sgi.com2c6b8c02008-01-30 13:33:11 +01001193 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 if (!data)
1196 return -EINVAL;
1197
1198 powernow_k8_cpu_exit_acpi(data);
1199
1200 cpufreq_frequency_table_put_attr(pol->cpu);
1201
1202 kfree(data->powernow_table);
1203 kfree(data);
Thomas Renninger557a7012009-12-14 11:44:15 +01001204 per_cpu(powernow_data, pol->cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 return 0;
1207}
1208
Rusty Russell1ff6e972009-06-12 20:55:37 +09301209static void query_values_on_cpu(void *_err)
1210{
1211 int *err = _err;
Tejun Heo0a3aee02010-12-18 16:28:55 +01001212 struct powernow_k8_data *data = __this_cpu_read(powernow_data);
Rusty Russell1ff6e972009-06-12 20:55:37 +09301213
1214 *err = query_current_values_with_pending_wait(data);
1215}
1216
Dave Jones0e64a0c2009-02-04 14:37:50 -05001217static unsigned int powernowk8_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Naga Chumbalkare15bc452009-06-11 15:26:54 +00001219 struct powernow_k8_data *data = per_cpu(powernow_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 unsigned int khz = 0;
Rusty Russell1ff6e972009-06-12 20:55:37 +09301221 int err;
shin, jacobeef51672006-03-27 09:57:20 -06001222
1223 if (!data)
Thomas Renninger557a7012009-12-14 11:44:15 +01001224 return 0;
shin, jacobeef51672006-03-27 09:57:20 -06001225
Rusty Russell1ff6e972009-06-12 20:55:37 +09301226 smp_call_function_single(cpu, query_values_on_cpu, &err, true);
1227 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 goto out;
1229
Matthew Garrette1f0b8e2012-09-04 08:28:09 +00001230 khz = find_khz_freq_from_fid(data->currfid);
Joachim Deguara58389a82007-01-30 16:53:54 +01001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Dave Jonesb9111b72005-09-23 11:10:42 -07001233out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return khz;
1235}
1236
Dave Jones0e64a0c2009-02-04 14:37:50 -05001237static struct freq_attr *powernow_k8_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 &cpufreq_freq_attr_scaling_available_freqs,
1239 NULL,
1240};
1241
Linus Torvalds221dee22007-02-26 14:55:48 -08001242static struct cpufreq_driver cpufreq_amd64_driver = {
Thomas Renningere2f74f32009-11-19 12:31:01 +01001243 .verify = powernowk8_verify,
1244 .target = powernowk8_target,
1245 .bios_limit = acpi_processor_get_bios_limit,
1246 .init = powernowk8_cpu_init,
1247 .exit = __devexit_p(powernowk8_cpu_exit),
1248 .get = powernowk8_get,
1249 .name = "powernow-k8",
1250 .owner = THIS_MODULE,
1251 .attr = powernow_k8_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252};
1253
1254/* driver entry point for init */
Andi Kleenaa41eb92006-01-16 01:56:36 +01001255static int __cpuinit powernowk8_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Matthew Garrette1f0b8e2012-09-04 08:28:09 +00001257 unsigned int i, supported_cpus = 0;
Neil Brownac818312010-11-24 11:28:01 +11001258 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Matthew Garrette1f0b8e2012-09-04 08:28:09 +00001260 if (static_cpu_has(X86_FEATURE_HW_PSTATE)) {
1261 pr_warn(PFX "this CPU is not supported anymore, using acpi-cpufreq instead.\n");
1262 request_module("acpi-cpufreq");
1263 return -ENODEV;
1264 }
1265
Andi Kleenfa8031a2012-01-26 00:09:12 +01001266 if (!x86_match_cpu(powernow_k8_ids))
1267 return -ENODEV;
1268
Andrew Mortona7201152006-03-24 03:15:07 -08001269 for_each_online_cpu(i) {
Rusty Russell1ff6e972009-06-12 20:55:37 +09301270 int rc;
1271 smp_call_function_single(i, check_supported_cpu, &rc, 1);
1272 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 supported_cpus++;
1274 }
1275
Borislav Petkov73860c62010-03-31 21:56:42 +02001276 if (supported_cpus != num_online_cpus())
1277 return -ENODEV;
1278
Neil Brownac818312010-11-24 11:28:01 +11001279 rv = cpufreq_register_driver(&cpufreq_amd64_driver);
Andre Przywaraa2060952012-09-04 08:28:05 +00001280
1281 if (!rv)
1282 pr_info(PFX "Found %d %s (%d cpu cores) (" VERSION ")\n",
1283 num_online_nodes(), boot_cpu_data.x86_model_id,
1284 supported_cpus);
1285
Neil Brownac818312010-11-24 11:28:01 +11001286 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289/* driver entry point for term */
1290static void __exit powernowk8_exit(void)
1291{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001292 pr_debug("exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 cpufreq_unregister_driver(&cpufreq_amd64_driver);
1295}
1296
Dave Jones0e64a0c2009-02-04 14:37:50 -05001297MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and "
1298 "Mark Langsdorf <mark.langsdorf@amd.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
1300MODULE_LICENSE("GPL");
1301
1302late_initcall(powernowk8_init);
1303module_exit(powernowk8_exit);