Rev 582 | Rev 769 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
582 | mauro | 1 | /* |
2 | * (c) 2003 Advanced Micro Devices, Inc. |
||
3 | * 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 | * |
||
7 | * Support : paul.devriendt@amd.com |
||
8 | * |
||
9 | * Based on the powernow-k7.c module written by Dave Jones. |
||
10 | * (C) 2003 Dave Jones <davej@codemonkey.ork.uk> on behalf of SuSE Labs |
||
11 | * Licensed under the terms of the GNU GPL License version 2. |
||
12 | * Based upon datasheets & sample CPUs kindly provided by AMD. |
||
13 | * |
||
14 | * Processor information obtained from Chapter 9 (Power and Thermal Management) |
||
15 | * of the "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD |
||
16 | * Opteron Processors", revision 3.03, available for download from www.amd.com |
||
17 | * |
||
18 | */ |
||
19 | |||
20 | #include <linuxcomp.h> |
||
21 | |||
22 | #include <linux/kernel.h> |
||
23 | #include <linux/smp.h> |
||
24 | #include <linux/module.h> |
||
25 | #include <linux/init.h> |
||
26 | #include <linux/cpufreq.h> |
||
27 | #include <linux/slab.h> |
||
28 | #include <linux/string.h> |
||
29 | |||
30 | #include <asm/msr.h> |
||
31 | #include <asm/io.h> |
||
32 | #include <asm/delay.h> |
||
33 | |||
34 | #define PFX "powernow-k8: " |
||
35 | #define BFX PFX "BIOS error: " |
||
36 | #define VERSION "version 1.00.08 - September 26, 2003" |
||
37 | #include "powernow-k8.h" |
||
38 | |||
39 | #ifdef CONFIG_PREEMPT |
||
40 | #warning this driver has not been tested on a preempt system |
||
41 | #endif |
||
42 | |||
43 | extern struct cpuinfo_x86 new_cpu_data; |
||
44 | |||
45 | static u32 vstable; /* voltage stabalization time, from PSB, units 20 us */ |
||
46 | static u32 plllock; /* pll lock time, from PSB, units 1 us */ |
||
47 | static u32 numps; /* number of p-states, from PSB */ |
||
48 | static u32 rvo; /* ramp voltage offset, from PSB */ |
||
49 | static u32 irt; /* isochronous relief time, from PSB */ |
||
50 | static u32 vidmvs; /* usable value calculated from mvs, from PSB */ |
||
51 | struct pst_s *ppst; /* array of p states, valid for this part */ |
||
52 | static u32 currvid; /* keep track of the current fid / vid */ |
||
53 | static u32 currfid; |
||
54 | |||
55 | /* |
||
56 | The PSB table supplied by BIOS allows for the definition of the number of |
||
57 | p-states that can be used when running on a/c, and the number of p-states |
||
58 | that can be used when running on battery. This allows laptop manufacturers |
||
59 | to force the system to save power when running from battery. The relationship |
||
60 | is : |
||
61 | 1 <= number_of_battery_p_states <= maximum_number_of_p_states |
||
62 | |||
63 | This driver does NOT have the support in it to detect transitions from |
||
64 | a/c power to battery power, and thus trigger the transition to a lower |
||
65 | p-state if required. This is because I need ACPI and the 2.6 kernel to do |
||
66 | this, and this is a 2.4 kernel driver. Check back for a new improved driver |
||
67 | for the 2.6 kernel soon. |
||
68 | |||
69 | This code therefore assumes it is on battery at all times, and thus |
||
70 | restricts performance to number_of_battery_p_states. For desktops, |
||
71 | number_of_battery_p_states == maximum_number_of_pstates, |
||
72 | so this is not actually a restriction. |
||
73 | */ |
||
74 | |||
75 | static u32 batps; /* limit on the number of p states when on battery */ |
||
76 | /* - set by BIOS in the PSB/PST */ |
||
77 | |||
78 | static struct cpufreq_driver cpufreq_amd64_driver = { |
||
79 | .verify = powernowk8_verify, |
||
80 | .target = powernowk8_target, |
||
81 | .init = powernowk8_cpu_init, |
||
82 | .name = "cpufreq-amd64", |
||
83 | .owner = THIS_MODULE, |
||
84 | }; |
||
85 | |||
86 | #define SEARCH_UP 1 |
||
87 | #define SEARCH_DOWN 0 |
||
88 | |||
89 | /* Return a frequency in MHz, given an input fid */ |
||
90 | u32 find_freq_from_fid(u32 fid) |
||
91 | { |
||
92 | return 800 + (fid * 100); |
||
93 | } |
||
94 | |||
95 | /* Return a fid matching an input frequency in MHz */ |
||
96 | static u32 find_fid_from_freq(u32 freq) |
||
97 | { |
||
98 | return (freq - 800) / 100; |
||
99 | } |
||
100 | |||
101 | /* Return the vco fid for an input fid */ |
||
102 | static u32 convert_fid_to_vco_fid(u32 fid) |
||
103 | { |
||
104 | if (fid < HI_FID_TABLE_BOTTOM) { |
||
105 | return 8 + (2 * fid); |
||
106 | } else { |
||
107 | return fid; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /* Sort the fid/vid frequency table into ascending order by fid. The spec */ |
||
112 | /* implies that it will be sorted by BIOS, but, it only implies it, and I */ |
||
113 | /* prefer not to trust when I can check. */ |
||
114 | /* Yes, it is a simple bubble sort, but the PST is really small, so the */ |
||
115 | /* choice of algorithm is pretty irrelevant. */ |
||
116 | static inline void sort_pst(struct pst_s *ppst, u32 numpstates) |
||
117 | { |
||
118 | u32 i; |
||
119 | u8 tempfid; |
||
120 | u8 tempvid; |
||
121 | int swaps = 1; |
||
122 | |||
123 | while (swaps) { |
||
124 | swaps = 0; |
||
125 | for (i = 0; i < (numpstates - 1); i++) { |
||
126 | if (ppst[i].fid > ppst[i + 1].fid) { |
||
127 | swaps = 1; |
||
128 | tempfid = ppst[i].fid; |
||
129 | tempvid = ppst[i].vid; |
||
130 | ppst[i].fid = ppst[i + 1].fid; |
||
131 | ppst[i].vid = ppst[i + 1].vid; |
||
132 | ppst[i + 1].fid = tempfid; |
||
133 | ppst[i + 1].vid = tempvid; |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | return; |
||
139 | } |
||
140 | |||
141 | /* Return 1 if the pending bit is set. Unless we are actually just told the */ |
||
142 | /* processor to transition a state, seeing this bit set is really bad news. */ |
||
143 | static inline int pending_bit_stuck(void) |
||
144 | { |
||
145 | u32 lo; |
||
146 | u32 hi; |
||
147 | |||
148 | rdmsr(MSR_FIDVID_STATUS, lo, hi); |
||
149 | return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0; |
||
150 | } |
||
151 | |||
152 | /* Update the global current fid / vid values from the status msr. Returns 1 */ |
||
153 | /* on error. */ |
||
154 | static int query_current_values_with_pending_wait(void) |
||
155 | { |
||
156 | u32 lo; |
||
157 | u32 hi; |
||
158 | u32 i = 0; |
||
159 | |||
160 | lo = MSR_S_LO_CHANGE_PENDING; |
||
161 | while (lo & MSR_S_LO_CHANGE_PENDING) { |
||
162 | if (i++ > 0x1000000) { |
||
163 | printk(KERN_ERR PFX "detected change pending stuck\n"); |
||
164 | return 1; |
||
165 | } |
||
166 | rdmsr(MSR_FIDVID_STATUS, lo, hi); |
||
167 | } |
||
168 | |||
169 | currvid = hi & MSR_S_HI_CURRENT_VID; |
||
170 | currfid = lo & MSR_S_LO_CURRENT_FID; |
||
171 | |||
172 | return 0; |
||
173 | } |
||
174 | |||
175 | /* the isochronous relief time */ |
||
176 | static inline void count_off_irt(void) |
||
177 | { |
||
178 | udelay((1 << irt) * 10); |
||
179 | return; |
||
180 | } |
||
181 | |||
182 | /* the voltage stabalization time */ |
||
183 | static inline void count_off_vst(void) |
||
184 | { |
||
185 | udelay(vstable * VST_UNITS_20US); |
||
186 | return; |
||
187 | } |
||
188 | |||
189 | /* write the new fid value along with the other control fields to the msr */ |
||
190 | static int write_new_fid(u32 fid) |
||
191 | { |
||
192 | u32 lo; |
||
193 | u32 savevid = currvid; |
||
194 | |||
195 | if ((fid & INVALID_FID_MASK) || (currvid & INVALID_VID_MASK)) { |
||
196 | printk(KERN_ERR PFX "internal error - overflow on fid write\n"); |
||
197 | return 1; |
||
198 | } |
||
199 | |||
200 | lo = fid | (currvid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID; |
||
201 | |||
202 | dprintk(KERN_DEBUG PFX "writing fid %x, lo %x, hi %x\n", |
||
203 | fid, lo, plllock * PLL_LOCK_CONVERSION); |
||
204 | |||
205 | wrmsr(MSR_FIDVID_CTL, lo, plllock * PLL_LOCK_CONVERSION); |
||
206 | |||
207 | if (query_current_values_with_pending_wait()) |
||
208 | return 1; |
||
209 | |||
210 | count_off_irt(); |
||
211 | |||
212 | if (savevid != currvid) { |
||
213 | printk(KERN_ERR PFX |
||
214 | "vid changed on fid transition, save %x, currvid %x\n", |
||
215 | savevid, currvid); |
||
216 | return 1; |
||
217 | } |
||
218 | |||
219 | if (fid != currfid) { |
||
220 | printk(KERN_ERR PFX |
||
221 | "fid transition failed, fid %x, currfid %x\n", |
||
222 | fid, currfid); |
||
223 | return 1; |
||
224 | } |
||
225 | |||
226 | return 0; |
||
227 | } |
||
228 | |||
229 | /* Write a new vid to the hardware */ |
||
230 | static int write_new_vid(u32 vid) |
||
231 | { |
||
232 | u32 lo; |
||
233 | u32 savefid = currfid; |
||
234 | |||
235 | if ((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 | |||
240 | lo = currfid | (vid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID; |
||
241 | |||
242 | dprintk(KERN_DEBUG PFX "writing vid %x, lo %x, hi %x\n", |
||
243 | vid, lo, STOP_GRANT_5NS); |
||
244 | |||
245 | wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS); |
||
246 | |||
247 | if (query_current_values_with_pending_wait()) { |
||
248 | return 1; |
||
249 | } |
||
250 | |||
251 | if (savefid != currfid) { |
||
252 | printk(KERN_ERR PFX |
||
253 | "fid changed on vid transition, save %x currfid %x\n", |
||
254 | savefid, currfid); |
||
255 | return 1; |
||
256 | } |
||
257 | |||
258 | if (vid != currvid) { |
||
259 | printk(KERN_ERR PFX |
||
260 | "vid transition failed, vid %x, currvid %x\n", |
||
261 | vid, currvid); |
||
262 | return 1; |
||
263 | } |
||
264 | |||
265 | return 0; |
||
266 | } |
||
267 | |||
268 | /* Reduce the vid by the max of step or reqvid. */ |
||
269 | /* Decreasing vid codes represent increasing voltages : */ |
||
270 | /* vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of 0x1f is off. */ |
||
271 | static int decrease_vid_code_by_step(u32 reqvid, u32 step) |
||
272 | { |
||
273 | if ((currvid - reqvid) > step) |
||
274 | reqvid = currvid - step; |
||
275 | |||
276 | if (write_new_vid(reqvid)) |
||
277 | return 1; |
||
278 | |||
279 | count_off_vst(); |
||
280 | |||
281 | return 0; |
||
282 | } |
||
283 | |||
284 | /* Change the fid and vid, by the 3 phases. */ |
||
285 | static inline int transition_fid_vid(u32 reqfid, u32 reqvid) |
||
286 | { |
||
287 | if (core_voltage_pre_transition(reqvid)) |
||
288 | return 1; |
||
289 | |||
290 | if (core_frequency_transition(reqfid)) |
||
291 | return 1; |
||
292 | |||
293 | if (core_voltage_post_transition(reqvid)) |
||
294 | return 1; |
||
295 | |||
296 | if (query_current_values_with_pending_wait()) |
||
297 | return 1; |
||
298 | |||
299 | if ((reqfid != currfid) || (reqvid != currvid)) { |
||
300 | printk(KERN_ERR PFX "failed: req 0x%x 0x%x, curr 0x%x 0x%x\n", |
||
301 | reqfid, reqvid, currfid, currvid); |
||
302 | return 1; |
||
303 | } |
||
304 | |||
305 | dprintk(KERN_INFO PFX |
||
306 | "transitioned: new fid 0x%x, vid 0x%x\n", currfid, currvid); |
||
307 | |||
308 | return 0; |
||
309 | } |
||
310 | |||
311 | /* Phase 1 - core voltage transition ... setup appropriate voltage for the */ |
||
312 | /* fid transition. */ |
||
313 | static inline int core_voltage_pre_transition(u32 reqvid) |
||
314 | { |
||
315 | u32 rvosteps = rvo; |
||
316 | u32 savefid = currfid; |
||
317 | |||
318 | dprintk(KERN_DEBUG PFX |
||
319 | "ph1: start, currfid 0x%x, currvid 0x%x, reqvid 0x%x, rvo %x\n", |
||
320 | currfid, currvid, reqvid, rvo); |
||
321 | |||
322 | while (currvid > reqvid) { |
||
323 | dprintk(KERN_DEBUG PFX "ph1: curr 0x%x, requesting vid 0x%x\n", |
||
324 | currvid, reqvid); |
||
325 | if (decrease_vid_code_by_step(reqvid, vidmvs)) |
||
326 | return 1; |
||
327 | } |
||
328 | |||
329 | while (rvosteps > 0) { |
||
330 | if (currvid == 0) { |
||
331 | rvosteps = 0; |
||
332 | } else { |
||
333 | dprintk(KERN_DEBUG PFX |
||
334 | "ph1: changing vid for rvo, requesting 0x%x\n", |
||
335 | currvid - 1); |
||
336 | if (decrease_vid_code_by_step(currvid - 1, 1)) |
||
337 | return 1; |
||
338 | rvosteps--; |
||
339 | } |
||
340 | } |
||
341 | |||
342 | if (query_current_values_with_pending_wait()) |
||
343 | return 1; |
||
344 | |||
345 | if (savefid != currfid) { |
||
346 | printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n", currfid); |
||
347 | return 1; |
||
348 | } |
||
349 | |||
350 | dprintk(KERN_DEBUG PFX "ph1 complete, currfid 0x%x, currvid 0x%x\n", |
||
351 | currfid, currvid); |
||
352 | |||
353 | return 0; |
||
354 | } |
||
355 | |||
356 | /* Phase 2 - core frequency transition */ |
||
357 | static inline int core_frequency_transition(u32 reqfid) |
||
358 | { |
||
359 | u32 vcoreqfid; |
||
360 | u32 vcocurrfid; |
||
361 | u32 vcofiddiff; |
||
362 | u32 savevid = currvid; |
||
363 | |||
364 | if ((reqfid < HI_FID_TABLE_BOTTOM) && (currfid < HI_FID_TABLE_BOTTOM)) { |
||
365 | printk(KERN_ERR PFX "ph2 illegal lo-lo transition 0x%x 0x%x\n", |
||
366 | reqfid, currfid); |
||
367 | return 1; |
||
368 | } |
||
369 | |||
370 | if (currfid == reqfid) { |
||
371 | printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n", currfid); |
||
372 | return 0; |
||
373 | } |
||
374 | |||
375 | dprintk(KERN_DEBUG PFX |
||
376 | "ph2 starting, currfid 0x%x, currvid 0x%x, reqfid 0x%x\n", |
||
377 | currfid, currvid, reqfid); |
||
378 | |||
379 | vcoreqfid = convert_fid_to_vco_fid(reqfid); |
||
380 | vcocurrfid = convert_fid_to_vco_fid(currfid); |
||
381 | vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid |
||
382 | : vcoreqfid - vcocurrfid; |
||
383 | |||
384 | while (vcofiddiff > 2) { |
||
385 | if (reqfid > currfid) { |
||
386 | if (currfid > LO_FID_TABLE_TOP) { |
||
387 | if (write_new_fid(currfid + 2)) { |
||
388 | return 1; |
||
389 | } |
||
390 | } else { |
||
391 | if (write_new_fid |
||
392 | (2 + convert_fid_to_vco_fid(currfid))) { |
||
393 | return 1; |
||
394 | } |
||
395 | } |
||
396 | } else { |
||
397 | if (write_new_fid(currfid - 2)) |
||
398 | return 1; |
||
399 | } |
||
400 | |||
401 | vcocurrfid = convert_fid_to_vco_fid(currfid); |
||
402 | vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid |
||
403 | : vcoreqfid - vcocurrfid; |
||
404 | } |
||
405 | |||
406 | if (write_new_fid(reqfid)) |
||
407 | return 1; |
||
408 | |||
409 | if (query_current_values_with_pending_wait()) |
||
410 | return 1; |
||
411 | |||
412 | if (currfid != reqfid) { |
||
413 | printk(KERN_ERR PFX |
||
414 | "ph2 mismatch, failed fid transition, curr %x, req %x\n", |
||
415 | currfid, reqfid); |
||
416 | return 1; |
||
417 | } |
||
418 | |||
419 | if (savevid != currvid) { |
||
420 | printk(KERN_ERR PFX |
||
421 | "ph2 vid changed, save %x, curr %x\n", savevid, |
||
422 | currvid); |
||
423 | return 1; |
||
424 | } |
||
425 | |||
426 | dprintk(KERN_DEBUG PFX "ph2 complete, currfid 0x%x, currvid 0x%x\n", |
||
427 | currfid, currvid); |
||
428 | |||
429 | return 0; |
||
430 | } |
||
431 | |||
432 | /* Phase 3 - core voltage transition flow ... jump to the final vid. */ |
||
433 | static inline int core_voltage_post_transition(u32 reqvid) |
||
434 | { |
||
435 | u32 savefid = currfid; |
||
436 | u32 savereqvid = reqvid; |
||
437 | |||
438 | dprintk(KERN_DEBUG PFX "ph3 starting, currfid 0x%x, currvid 0x%x\n", |
||
439 | currfid, currvid); |
||
440 | |||
441 | if (reqvid != currvid) { |
||
442 | if (write_new_vid(reqvid)) |
||
443 | return 1; |
||
444 | |||
445 | if (savefid != currfid) { |
||
446 | printk(KERN_ERR PFX |
||
447 | "ph3: bad fid change, save %x, curr %x\n", |
||
448 | savefid, currfid); |
||
449 | return 1; |
||
450 | } |
||
451 | |||
452 | if (currvid != reqvid) { |
||
453 | printk(KERN_ERR PFX |
||
454 | "ph3: failed vid transition\n, req %x, curr %x", |
||
455 | reqvid, currvid); |
||
456 | return 1; |
||
457 | } |
||
458 | } |
||
459 | |||
460 | if (query_current_values_with_pending_wait()) |
||
461 | return 1; |
||
462 | |||
463 | if (savereqvid != currvid) { |
||
464 | dprintk(KERN_ERR PFX "ph3 failed, currvid 0x%x\n", currvid); |
||
465 | return 1; |
||
466 | } |
||
467 | |||
468 | if (savefid != currfid) { |
||
469 | dprintk(KERN_ERR PFX "ph3 failed, currfid changed 0x%x\n", |
||
470 | currfid); |
||
471 | return 1; |
||
472 | } |
||
473 | |||
474 | dprintk(KERN_DEBUG PFX "ph3 complete, currfid 0x%x, currvid 0x%x\n", |
||
475 | currfid, currvid); |
||
476 | |||
477 | return 0; |
||
478 | } |
||
479 | |||
480 | static inline int check_supported_cpu(void) |
||
481 | { |
||
482 | struct cpuinfo_x86 *c = &new_cpu_data; |
||
483 | u32 eax, ebx, ecx, edx; |
||
484 | |||
485 | if (num_online_cpus() != 1) { |
||
486 | dprintk(KERN_INFO PFX "multiprocessor systems not supported\n"); |
||
487 | return 0; |
||
488 | } |
||
489 | |||
490 | if (c->x86_vendor != X86_VENDOR_AMD) { |
||
491 | dprintk(KERN_INFO PFX "Not an AMD processor\n"); |
||
492 | return 0; |
||
493 | } |
||
494 | |||
495 | eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE); |
||
496 | if ((eax & CPUID_XFAM_MOD) == ATHLON64_XFAM_MOD) { |
||
497 | dprintk(KERN_DEBUG PFX "AMD Althon 64 Processor found\n"); |
||
498 | if ((eax & CPUID_F1_STEP) < ATHLON64_REV_C0) { |
||
499 | dprintk(KERN_INFO PFX "Revision C0 or better " |
||
500 | "AMD Athlon 64 processor required\n"); |
||
501 | return 0; |
||
502 | } |
||
503 | } else if ((eax & CPUID_XFAM_MOD) == OPTERON_XFAM_MOD) { |
||
504 | dprintk(KERN_DEBUG PFX "AMD Opteron Processor found\n"); |
||
505 | } else { |
||
506 | dprintk(KERN_INFO PFX |
||
507 | "AMD Athlon 64 or AMD Opteron processor required\n"); |
||
508 | return 0; |
||
509 | } |
||
510 | |||
511 | eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES); |
||
512 | if (eax < CPUID_FREQ_VOLT_CAPABILITIES) { |
||
513 | dprintk(KERN_INFO PFX |
||
514 | "No frequency change capabilities detected\n"); |
||
515 | return 0; |
||
516 | } |
||
517 | |||
518 | cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx); |
||
519 | if ((edx & P_STATE_TRANSITION_CAPABLE) != P_STATE_TRANSITION_CAPABLE) { |
||
520 | dprintk(KERN_INFO PFX "Power state transitions not supported\n"); |
||
521 | return 0; |
||
522 | } |
||
523 | |||
524 | printk(KERN_INFO PFX "Found AMD Athlon 64 / Opteron processor " |
||
525 | "supporting p-state transitions\n"); |
||
526 | |||
527 | return 1; |
||
528 | } |
||
529 | |||
530 | /* Find and validate the PSB/PST table in BIOS. */ |
||
531 | static inline int find_psb_table(void) |
||
532 | { |
||
533 | struct psb_s *psb; |
||
534 | struct pst_s *pst; |
||
535 | unsigned i, j; |
||
536 | u32 lastfid; |
||
537 | u32 mvs; |
||
538 | u8 maxvid; |
||
539 | |||
540 | for (i = 0xc0000; i < 0xffff0; i += 0x10) { |
||
541 | /* Scan BIOS looking for the signature. */ |
||
542 | /* It can not be at ffff0 - it is too big. */ |
||
543 | |||
544 | psb = phys_to_virt(i); |
||
545 | if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0) |
||
546 | continue; |
||
547 | |||
548 | dprintk(KERN_DEBUG PFX "found PSB header at 0x%p\n", psb); |
||
549 | |||
550 | dprintk(KERN_DEBUG PFX "table vers: 0x%x\n", psb->tableversion); |
||
551 | if (psb->tableversion != PSB_VERSION_1_4) { |
||
552 | printk(KERN_INFO BFX "PSB table is not v1.4\n"); |
||
553 | return -ENODEV; |
||
554 | } |
||
555 | |||
556 | dprintk(KERN_DEBUG PFX "flags: 0x%x\n", psb->flags1); |
||
557 | if (psb->flags1) { |
||
558 | printk(KERN_ERR BFX "unknown flags\n"); |
||
559 | return -ENODEV; |
||
560 | } |
||
561 | |||
562 | vstable = psb->voltagestabilizationtime; |
||
563 | printk(KERN_INFO PFX "voltage stable time: %d (units 20us)\n", |
||
564 | vstable); |
||
565 | |||
566 | dprintk(KERN_DEBUG PFX "flags2: 0x%x\n", psb->flags2); |
||
567 | rvo = psb->flags2 & 3; |
||
568 | irt = ((psb->flags2) >> 2) & 3; |
||
569 | mvs = ((psb->flags2) >> 4) & 3; |
||
570 | vidmvs = 1 << mvs; |
||
571 | batps = ((psb->flags2) >> 6) & 3; |
||
572 | printk(KERN_INFO PFX "p states on battery: %d ", batps); |
||
573 | switch (batps) { |
||
574 | case 0: |
||
575 | printk("- all available\n"); |
||
576 | break; |
||
577 | case 1: |
||
578 | printk("- only the minimum\n"); |
||
579 | break; |
||
580 | case 2: |
||
581 | printk("- only the 2 lowest\n"); |
||
582 | break; |
||
583 | case 3: |
||
584 | printk("- only the 3 lowest\n"); |
||
585 | break; |
||
586 | } |
||
587 | printk(KERN_INFO PFX "ramp voltage offset: %d\n", rvo); |
||
588 | printk(KERN_INFO PFX "isochronous relief time: %d\n", irt); |
||
589 | printk(KERN_INFO PFX "maximum voltage step: %d\n", mvs); |
||
590 | |||
591 | dprintk(KERN_DEBUG PFX "numpst: 0x%x\n", psb->numpst); |
||
592 | if (psb->numpst != 1) { |
||
593 | printk(KERN_ERR BFX "numpst must be 1\n"); |
||
594 | return -ENODEV; |
||
595 | } |
||
596 | |||
597 | dprintk(KERN_DEBUG PFX "cpuid: 0x%x\n", psb->cpuid); |
||
598 | |||
599 | plllock = psb->plllocktime; |
||
600 | printk(KERN_INFO PFX "pll lock time: 0x%x\n", plllock); |
||
601 | |||
602 | maxvid = psb->maxvid; |
||
603 | printk(KERN_INFO PFX "maxfid: 0x%x\n", psb->maxfid); |
||
604 | printk(KERN_INFO PFX "maxvid: 0x%x\n", maxvid); |
||
605 | |||
606 | numps = psb->numpstates; |
||
607 | printk(KERN_INFO PFX "numpstates: 0x%x\n", numps); |
||
608 | if (numps < 2) { |
||
609 | printk(KERN_ERR BFX "no p states to transition\n"); |
||
610 | return -ENODEV; |
||
611 | } |
||
612 | |||
613 | if (batps == 0) { |
||
614 | batps = numps; |
||
615 | } else if (batps > numps) { |
||
616 | printk(KERN_ERR BFX "batterypstates > numpstates\n"); |
||
617 | batps = numps; |
||
618 | } else { |
||
619 | printk(KERN_ERR PFX |
||
620 | "Restricting operation to %d p-states\n", batps); |
||
621 | printk(KERN_ERR PFX |
||
622 | "Check for an updated driver to access all " |
||
623 | "%d p-states\n", numps); |
||
624 | } |
||
625 | |||
626 | if ((numps <= 1) || (batps <= 1)) { |
||
627 | printk(KERN_ERR PFX "only 1 p-state to transition\n"); |
||
628 | return -ENODEV; |
||
629 | } |
||
630 | |||
631 | ppst = kmalloc(sizeof (struct pst_s) * numps, GFP_KERNEL); |
||
632 | if (!ppst) { |
||
633 | printk(KERN_ERR PFX "ppst memory alloc failure\n"); |
||
634 | return -ENOMEM; |
||
635 | } |
||
636 | |||
637 | pst = (struct pst_s *) (psb + 1); |
||
638 | for (j = 0; j < numps; j++) { |
||
639 | ppst[j].fid = pst[j].fid; |
||
640 | ppst[j].vid = pst[j].vid; |
||
641 | printk(KERN_INFO PFX |
||
642 | " %d : fid 0x%x, vid 0x%x\n", j, |
||
643 | ppst[j].fid, ppst[j].vid); |
||
644 | } |
||
645 | sort_pst(ppst, numps); |
||
646 | |||
647 | lastfid = ppst[0].fid; |
||
648 | if (lastfid > LO_FID_TABLE_TOP) |
||
649 | printk(KERN_INFO BFX "first fid not in lo freq tbl\n"); |
||
650 | |||
651 | if ((lastfid > MAX_FID) || (lastfid & 1) || (ppst[0].vid > LEAST_VID)) { |
||
652 | printk(KERN_ERR BFX "first fid/vid bad (0x%x - 0x%x)\n", |
||
653 | lastfid, ppst[0].vid); |
||
654 | kfree(ppst); |
||
655 | return -ENODEV; |
||
656 | } |
||
657 | |||
658 | for (j = 1; j < numps; j++) { |
||
659 | if ((lastfid >= ppst[j].fid) |
||
660 | || (ppst[j].fid & 1) |
||
661 | || (ppst[j].fid < HI_FID_TABLE_BOTTOM) |
||
662 | || (ppst[j].fid > MAX_FID) |
||
663 | || (ppst[j].vid > LEAST_VID)) { |
||
664 | printk(KERN_ERR BFX |
||
665 | "invalid fid/vid in pst(%x %x)\n", |
||
666 | ppst[j].fid, ppst[j].vid); |
||
667 | kfree(ppst); |
||
668 | return -ENODEV; |
||
669 | } |
||
670 | lastfid = ppst[j].fid; |
||
671 | } |
||
672 | |||
673 | for (j = 0; j < numps; j++) { |
||
674 | if (ppst[j].vid < rvo) { /* vid+rvo >= 0 */ |
||
675 | printk(KERN_ERR BFX |
||
676 | "0 vid exceeded with pstate %d\n", j); |
||
677 | return -ENODEV; |
||
678 | } |
||
679 | if (ppst[j].vid < maxvid+rvo) { /* vid+rvo >= maxvid */ |
||
680 | printk(KERN_ERR BFX |
||
681 | "maxvid exceeded with pstate %d\n", j); |
||
682 | return -ENODEV; |
||
683 | } |
||
684 | } |
||
685 | |||
686 | if (query_current_values_with_pending_wait()) { |
||
687 | kfree(ppst); |
||
688 | return -EIO; |
||
689 | } |
||
690 | |||
691 | printk(KERN_INFO PFX "currfid 0x%x, currvid 0x%x\n", |
||
692 | currfid, currvid); |
||
693 | |||
694 | for (j = 0; j < numps; j++) |
||
695 | if ((ppst[j].fid==currfid) && (ppst[j].vid==currvid)) |
||
696 | return (0); |
||
697 | |||
698 | printk(KERN_ERR BFX "currfid/vid do not match PST, ignoring\n"); |
||
699 | return 0; |
||
700 | } |
||
701 | |||
702 | printk(KERN_ERR BFX "no PSB\n"); |
||
703 | return -ENODEV; |
||
704 | } |
||
705 | |||
706 | /* Converts a frequency (that might not necessarily be a multiple of 200) */ |
||
707 | /* to a fid. */ |
||
708 | static u32 find_closest_fid(u32 freq, int searchup) |
||
709 | { |
||
710 | if (searchup == SEARCH_UP) |
||
711 | freq += MIN_FREQ_RESOLUTION - 1; |
||
712 | |||
713 | freq = (freq / MIN_FREQ_RESOLUTION) * MIN_FREQ_RESOLUTION; |
||
714 | |||
715 | if (freq < MIN_FREQ) |
||
716 | freq = MIN_FREQ; |
||
717 | else if (freq > MAX_FREQ) |
||
718 | freq = MAX_FREQ; |
||
719 | |||
720 | return find_fid_from_freq(freq); |
||
721 | } |
||
722 | |||
723 | static int find_match(u32 * ptargfreq, u32 * pmin, u32 * pmax, int searchup, u32 * pfid, u32 * pvid) |
||
724 | { |
||
725 | u32 availpstates = batps; |
||
726 | u32 targfid = find_closest_fid(*ptargfreq, searchup); |
||
727 | u32 minfid = find_closest_fid(*pmin, SEARCH_DOWN); |
||
728 | u32 maxfid = find_closest_fid(*pmax, SEARCH_UP); |
||
729 | u32 minidx = 0; |
||
730 | u32 maxidx = availpstates - 1; |
||
731 | u32 targidx = 0xffffffff; |
||
732 | int i; |
||
733 | |||
734 | dprintk(KERN_DEBUG PFX "find match: freq %d MHz, min %d, max %d\n", |
||
735 | *ptargfreq, *pmin, *pmax); |
||
736 | |||
737 | /* Restrict values to the frequency choices in the PST */ |
||
738 | if (minfid < ppst[0].fid) |
||
739 | minfid = ppst[0].fid; |
||
740 | if (maxfid > ppst[maxidx].fid) |
||
741 | maxfid = ppst[maxidx].fid; |
||
742 | |||
743 | /* Find appropriate PST index for the minimim fid */ |
||
744 | for (i = 0; i < (int) availpstates; i++) { |
||
745 | if (minfid >= ppst[i].fid) |
||
746 | minidx = i; |
||
747 | } |
||
748 | |||
749 | /* Find appropriate PST index for the maximum fid */ |
||
750 | for (i = availpstates - 1; i >= 0; i--) { |
||
751 | if (maxfid <= ppst[i].fid) |
||
752 | maxidx = i; |
||
753 | } |
||
754 | |||
755 | if (minidx > maxidx) |
||
756 | maxidx = minidx; |
||
757 | |||
758 | /* Frequency ids are now constrained by limits matching PST entries */ |
||
759 | minfid = ppst[minidx].fid; |
||
760 | maxfid = ppst[maxidx].fid; |
||
761 | |||
762 | /* Limit the target frequency to these limits */ |
||
763 | if (targfid < minfid) |
||
764 | targfid = minfid; |
||
765 | else if (targfid > maxfid) |
||
766 | targfid = maxfid; |
||
767 | |||
768 | /* Find the best target index into the PST, contrained by the range */ |
||
769 | if (searchup == SEARCH_UP) { |
||
770 | for (i = maxidx; i >= (int) minidx; i--) { |
||
771 | if (targfid <= ppst[i].fid) |
||
772 | targidx = i; |
||
773 | } |
||
774 | } else { |
||
775 | for (i = minidx; i <= (int) maxidx; i++) { |
||
776 | if (targfid >= ppst[i].fid) |
||
777 | targidx = i; |
||
778 | } |
||
779 | } |
||
780 | |||
781 | if (targidx == 0xffffffff) { |
||
782 | printk(KERN_ERR PFX "could not find target\n"); |
||
783 | return 1; |
||
784 | } |
||
785 | |||
786 | *pmin = find_freq_from_fid(minfid); |
||
787 | *pmax = find_freq_from_fid(maxfid); |
||
788 | *ptargfreq = find_freq_from_fid(ppst[targidx].fid); |
||
789 | |||
790 | if (pfid) |
||
791 | *pfid = ppst[targidx].fid; |
||
792 | if (pvid) |
||
793 | *pvid = ppst[targidx].vid; |
||
794 | |||
795 | return 0; |
||
796 | } |
||
797 | |||
798 | /* Take a frequency, and issue the fid/vid transition command */ |
||
799 | static inline int transition_frequency(u32 * preq, u32 * pmin, u32 * pmax, u32 searchup) |
||
800 | { |
||
801 | u32 fid; |
||
802 | u32 vid; |
||
803 | int res; |
||
804 | struct cpufreq_freqs freqs; |
||
805 | |||
806 | if (find_match(preq, pmin, pmax, searchup, &fid, &vid)) |
||
807 | return 1; |
||
808 | |||
809 | dprintk(KERN_DEBUG PFX "table matched fid 0x%x, giving vid 0x%x\n", |
||
810 | fid, vid); |
||
811 | |||
812 | if (query_current_values_with_pending_wait()) |
||
813 | return 1; |
||
814 | |||
815 | if ((currvid == vid) && (currfid == fid)) { |
||
816 | dprintk(KERN_DEBUG PFX |
||
817 | "target matches current values (fid 0x%x, vid 0x%x)\n", |
||
818 | fid, vid); |
||
819 | return 0; |
||
820 | } |
||
821 | |||
822 | if ((fid < HI_FID_TABLE_BOTTOM) && (currfid < HI_FID_TABLE_BOTTOM)) { |
||
823 | printk(KERN_ERR PFX |
||
824 | "ignoring illegal change in lo freq table-%x to %x\n", |
||
825 | currfid, fid); |
||
826 | return 1; |
||
827 | } |
||
828 | |||
829 | dprintk(KERN_DEBUG PFX "changing to fid 0x%x, vid 0x%x\n", fid, vid); |
||
830 | |||
831 | freqs.cpu = 0; /* only true because SMP not supported */ |
||
832 | |||
833 | freqs.old = find_freq_from_fid(currfid); |
||
834 | freqs.new = find_freq_from_fid(fid); |
||
600 | mauro | 835 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); |
582 | mauro | 836 | |
837 | res = transition_fid_vid(fid, vid); |
||
838 | |||
839 | freqs.new = find_freq_from_fid(currfid); |
||
600 | mauro | 840 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); |
582 | mauro | 841 | |
842 | return res; |
||
843 | } |
||
844 | |||
845 | /* Driver entry point to switch to the target frequency */ |
||
846 | static int powernowk8_target(struct cpufreq_policy *pol, unsigned targfreq, unsigned relation) |
||
847 | { |
||
848 | u32 checkfid = currfid; |
||
849 | u32 checkvid = currvid; |
||
850 | u32 reqfreq = targfreq / 1000; |
||
851 | u32 minfreq = pol->min / 1000; |
||
852 | u32 maxfreq = pol->max / 1000; |
||
853 | |||
854 | if (ppst == 0) { |
||
855 | printk(KERN_ERR PFX "targ: ppst 0\n"); |
||
856 | return -ENODEV; |
||
857 | } |
||
858 | |||
859 | if (pending_bit_stuck()) { |
||
860 | printk(KERN_ERR PFX "drv targ fail: change pending bit set\n"); |
||
861 | return -EIO; |
||
862 | } |
||
863 | |||
864 | dprintk(KERN_DEBUG PFX "targ: %d kHz, min %d, max %d, relation %d\n", |
||
865 | targfreq, pol->min, pol->max, relation); |
||
866 | |||
867 | if (query_current_values_with_pending_wait()) |
||
868 | return -EIO; |
||
869 | |||
870 | dprintk(KERN_DEBUG PFX "targ: curr fid 0x%x, vid 0x%x\n", |
||
871 | currfid, currvid); |
||
872 | |||
873 | if ((checkvid != currvid) || (checkfid != currfid)) { |
||
874 | printk(KERN_ERR PFX |
||
875 | "error - out of sync, fid 0x%x 0x%x, vid 0x%x 0x%x\n", |
||
876 | checkfid, currfid, checkvid, currvid); |
||
877 | } |
||
878 | |||
879 | if (transition_frequency(&reqfreq, &minfreq, &maxfreq, |
||
880 | relation == |
||
881 | CPUFREQ_RELATION_H ? SEARCH_UP : SEARCH_DOWN)) |
||
882 | { |
||
883 | printk(KERN_ERR PFX "transition frequency failed\n"); |
||
884 | return 1; |
||
885 | } |
||
886 | |||
887 | pol->cur = 1000 * find_freq_from_fid(currfid); |
||
888 | |||
889 | return 0; |
||
890 | } |
||
891 | |||
892 | /* Driver entry point to verify the policy and range of frequencies */ |
||
893 | static int powernowk8_verify(struct cpufreq_policy *pol) |
||
894 | { |
||
895 | u32 min = pol->min / 1000; |
||
896 | u32 max = pol->max / 1000; |
||
897 | u32 targ = min; |
||
898 | int res; |
||
899 | |||
900 | if (ppst == 0) { |
||
901 | printk(KERN_ERR PFX "verify - ppst 0\n"); |
||
902 | return -ENODEV; |
||
903 | } |
||
904 | |||
905 | if (pending_bit_stuck()) { |
||
906 | printk(KERN_ERR PFX "failing verify, change pending bit set\n"); |
||
907 | return -EIO; |
||
908 | } |
||
909 | |||
910 | dprintk(KERN_DEBUG PFX |
||
911 | "ver: cpu%d, min %d, max %d, cur %d, pol %d\n", pol->cpu, |
||
912 | pol->min, pol->max, pol->cur, pol->policy); |
||
913 | |||
914 | if (pol->cpu != 0) { |
||
915 | printk(KERN_ERR PFX "verify - cpu not 0\n"); |
||
916 | return -ENODEV; |
||
917 | } |
||
918 | |||
919 | #warning pol->policy is in undefined state here |
||
920 | res = find_match(&targ, &min, &max, |
||
921 | pol->policy == CPUFREQ_POLICY_POWERSAVE ? |
||
922 | SEARCH_DOWN : SEARCH_UP, 0, 0); |
||
923 | if (!res) { |
||
924 | pol->min = min * 1000; |
||
925 | pol->max = max * 1000; |
||
926 | } |
||
927 | return res; |
||
928 | } |
||
929 | |||
930 | /* per CPU init entry point to the driver */ |
||
931 | static int __init powernowk8_cpu_init(struct cpufreq_policy *pol) |
||
932 | { |
||
933 | if (pol->cpu != 0) { |
||
934 | printk(KERN_ERR PFX "init not cpu 0\n"); |
||
935 | return -ENODEV; |
||
936 | } |
||
937 | |||
938 | pol->governor = 0; //!!!CPUFREQ_DEFAULT_GOVERNOR; |
||
939 | |||
940 | /* Take a crude guess here. */ |
||
941 | pol->cpuinfo.transition_latency = ((rvo + 8) * vstable * VST_UNITS_20US) |
||
942 | + (3 * (1 << irt) * 10); |
||
943 | |||
944 | if (query_current_values_with_pending_wait()) |
||
945 | return -EIO; |
||
946 | |||
947 | pol->cur = 1000 * find_freq_from_fid(currfid); |
||
948 | dprintk(KERN_DEBUG PFX "policy current frequency %d kHz\n", pol->cur); |
||
949 | |||
950 | /* min/max the cpu is capable of */ |
||
951 | pol->cpuinfo.min_freq = 1000 * find_freq_from_fid(ppst[0].fid); |
||
952 | pol->cpuinfo.max_freq = 1000 * find_freq_from_fid(ppst[numps-1].fid); |
||
953 | pol->min = 1000 * find_freq_from_fid(ppst[0].fid); |
||
954 | pol->max = 1000 * find_freq_from_fid(ppst[batps - 1].fid); |
||
955 | |||
956 | printk(KERN_INFO PFX "cpu_init done, current fid 0x%x, vid 0x%x\n", |
||
957 | currfid, currvid); |
||
958 | |||
959 | return 0; |
||
960 | } |
||
961 | |||
962 | /* driver entry point for init */ |
||
963 | /*static*/ int __init powernowk8_init(void) |
||
964 | { |
||
965 | int rc; |
||
966 | |||
967 | dprintk(KERN_INFO PFX VERSION "\n"); |
||
968 | |||
969 | if (check_supported_cpu() == 0) |
||
970 | return -ENODEV; |
||
971 | |||
972 | rc = find_psb_table(); |
||
973 | if (rc) |
||
974 | return rc; |
||
975 | |||
976 | if (pending_bit_stuck()) { |
||
977 | printk(KERN_ERR PFX "powernowk8_init fail, change pending bit set\n"); |
||
978 | kfree(ppst); |
||
979 | return -EIO; |
||
980 | } |
||
981 | |||
982 | return cpufreq_register_driver(&cpufreq_amd64_driver); |
||
983 | } |
||
984 | |||
985 | /* driver entry point for term */ |
||
986 | /*static*/ void __exit powernowk8_exit(void) |
||
987 | { |
||
988 | dprintk(KERN_INFO PFX "powernowk8_exit\n"); |
||
989 | |||
990 | cpufreq_unregister_driver(&cpufreq_amd64_driver); |
||
991 | kfree(ppst); |
||
992 | } |
||
993 | |||
994 | MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com>"); |
||
995 | MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver."); |
||
996 | MODULE_LICENSE("GPL"); |
||
997 | |||
998 | module_init(powernowk8_init); |
||
999 | module_exit(powernowk8_exit); |