ec3a9e335aa3f339bdc8276acebf0b40cd947708
[deliverable/linux.git] / arch / i386 / kernel / cpu / cpufreq / powernow-k8.c
1 /*
2 * (c) 2003, 2004, 2005 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 : mark.langsdorf@amd.com
8 *
9 * Based on the powernow-k7.c module written by Dave Jones.
10 * (C) 2003 Dave Jones <davej@codemonkey.org.uk> on behalf of SuSE Labs
11 * (C) 2004 Dominik Brodowski <linux@brodo.de>
12 * (C) 2004 Pavel Machek <pavel@suse.cz>
13 * Licensed under the terms of the GNU GPL License version 2.
14 * Based upon datasheets & sample CPUs kindly provided by AMD.
15 *
16 * Valuable input gratefully received from Dave Jones, Pavel Machek,
17 * Dominik Brodowski, and others.
18 * Originally developed by Paul Devriendt.
19 * Processor information obtained from Chapter 9 (Power and Thermal Management)
20 * of the "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
21 * Opteron Processors" available for download from www.amd.com
22 *
23 * Tables for specific CPUs can be infrerred from
24 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/30430.pdf
25 */
26
27 #include <linux/kernel.h>
28 #include <linux/smp.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/cpufreq.h>
32 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/cpumask.h>
35
36 #include <asm/msr.h>
37 #include <asm/io.h>
38 #include <asm/delay.h>
39
40 #ifdef CONFIG_X86_POWERNOW_K8_ACPI
41 #include <linux/acpi.h>
42 #include <acpi/processor.h>
43 #endif
44
45 #define PFX "powernow-k8: "
46 #define BFX PFX "BIOS error: "
47 #define VERSION "version 1.50.4"
48 #include "powernow-k8.h"
49
50 /* serialize freq changes */
51 static DECLARE_MUTEX(fidvid_sem);
52
53 static struct powernow_k8_data *powernow_data[NR_CPUS];
54
55 #ifndef CONFIG_SMP
56 static cpumask_t cpu_core_map[1];
57 #endif
58
59 /* Return a frequency in MHz, given an input fid */
60 static u32 find_freq_from_fid(u32 fid)
61 {
62 return 800 + (fid * 100);
63 }
64
65 /* Return a frequency in KHz, given an input fid */
66 static u32 find_khz_freq_from_fid(u32 fid)
67 {
68 return 1000 * find_freq_from_fid(fid);
69 }
70
71 /* Return a voltage in miliVolts, given an input vid */
72 static u32 find_millivolts_from_vid(struct powernow_k8_data *data, u32 vid)
73 {
74 return 1550-vid*25;
75 }
76
77 /* Return the vco fid for an input fid
78 *
79 * Each "low" fid has corresponding "high" fid, and you can get to "low" fids
80 * only from corresponding high fids. This returns "high" fid corresponding to
81 * "low" one.
82 */
83 static u32 convert_fid_to_vco_fid(u32 fid)
84 {
85 if (fid < HI_FID_TABLE_BOTTOM) {
86 return 8 + (2 * fid);
87 } else {
88 return fid;
89 }
90 }
91
92 /*
93 * Return 1 if the pending bit is set. Unless we just instructed the processor
94 * to transition to a new state, seeing this bit set is really bad news.
95 */
96 static int pending_bit_stuck(void)
97 {
98 u32 lo, hi;
99
100 rdmsr(MSR_FIDVID_STATUS, lo, hi);
101 return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0;
102 }
103
104 /*
105 * Update the global current fid / vid values from the status msr.
106 * Returns 1 on error.
107 */
108 static int query_current_values_with_pending_wait(struct powernow_k8_data *data)
109 {
110 u32 lo, hi;
111 u32 i = 0;
112
113 do {
114 if (i++ > 10000) {
115 dprintk("detected change pending stuck\n");
116 return 1;
117 }
118 rdmsr(MSR_FIDVID_STATUS, lo, hi);
119 } while (lo & MSR_S_LO_CHANGE_PENDING);
120
121 data->currvid = hi & MSR_S_HI_CURRENT_VID;
122 data->currfid = lo & MSR_S_LO_CURRENT_FID;
123
124 return 0;
125 }
126
127 /* the isochronous relief time */
128 static void count_off_irt(struct powernow_k8_data *data)
129 {
130 udelay((1 << data->irt) * 10);
131 return;
132 }
133
134 /* the voltage stabalization time */
135 static void count_off_vst(struct powernow_k8_data *data)
136 {
137 udelay(data->vstable * VST_UNITS_20US);
138 return;
139 }
140
141 /* need to init the control msr to a safe value (for each cpu) */
142 static void fidvid_msr_init(void)
143 {
144 u32 lo, hi;
145 u8 fid, vid;
146
147 rdmsr(MSR_FIDVID_STATUS, lo, hi);
148 vid = hi & MSR_S_HI_CURRENT_VID;
149 fid = lo & MSR_S_LO_CURRENT_FID;
150 lo = fid | (vid << MSR_C_LO_VID_SHIFT);
151 hi = MSR_C_HI_STP_GNT_BENIGN;
152 dprintk("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi);
153 wrmsr(MSR_FIDVID_CTL, lo, hi);
154 }
155
156
157 /* write the new fid value along with the other control fields to the msr */
158 static int write_new_fid(struct powernow_k8_data *data, u32 fid)
159 {
160 u32 lo;
161 u32 savevid = data->currvid;
162 u32 i = 0;
163
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
169 lo = fid | (data->currvid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
170
171 dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
172 fid, lo, data->plllock * PLL_LOCK_CONVERSION);
173
174 do {
175 wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
176 if (i++ > 100) {
177 printk(KERN_ERR PFX "internal error - pending bit very stuck - no further pstate changes possible\n");
178 retrun 1;
179 }
180 } while (query_current_values_with_pending_wait(data));
181
182 count_off_irt(data);
183
184 if (savevid != data->currvid) {
185 printk(KERN_ERR PFX "vid change on fid trans, old 0x%x, new 0x%x\n",
186 savevid, data->currvid);
187 return 1;
188 }
189
190 if (fid != data->currfid) {
191 printk(KERN_ERR PFX "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
192 data->currfid);
193 return 1;
194 }
195
196 return 0;
197 }
198
199 /* Write a new vid to the hardware */
200 static int write_new_vid(struct powernow_k8_data *data, u32 vid)
201 {
202 u32 lo;
203 u32 savefid = data->currfid;
204 int i = 0;
205
206 if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
207 printk(KERN_ERR PFX "internal error - overflow on vid write\n");
208 return 1;
209 }
210
211 lo = data->currfid | (vid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
212
213 dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
214 vid, lo, STOP_GRANT_5NS);
215
216 do {
217 wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
218 if (i++ > 100) {
219 printk(KERN_ERR PFX "internal error - pending bit very stuck - no further pstate changes possible\n");
220 return 1;
221 }
222 } while (query_current_values_with_pending_wait(data));
223
224 if (savefid != data->currfid) {
225 printk(KERN_ERR PFX "fid changed on vid trans, old 0x%x new 0x%x\n",
226 savefid, data->currfid);
227 return 1;
228 }
229
230 if (vid != data->currvid) {
231 printk(KERN_ERR PFX "vid trans failed, vid 0x%x, curr 0x%x\n", vid,
232 data->currvid);
233 return 1;
234 }
235
236 return 0;
237 }
238
239 /*
240 * Reduce the vid by the max of step or reqvid.
241 * Decreasing vid codes represent increasing voltages:
242 * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off.
243 */
244 static int decrease_vid_code_by_step(struct powernow_k8_data *data, u32 reqvid, u32 step)
245 {
246 if ((data->currvid - reqvid) > step)
247 reqvid = data->currvid - step;
248
249 if (write_new_vid(data, reqvid))
250 return 1;
251
252 count_off_vst(data);
253
254 return 0;
255 }
256
257 /* Change the fid and vid, by the 3 phases. */
258 static int transition_fid_vid(struct powernow_k8_data *data, u32 reqfid, u32 reqvid)
259 {
260 if (core_voltage_pre_transition(data, reqvid))
261 return 1;
262
263 if (core_frequency_transition(data, reqfid))
264 return 1;
265
266 if (core_voltage_post_transition(data, reqvid))
267 return 1;
268
269 if (query_current_values_with_pending_wait(data))
270 return 1;
271
272 if ((reqfid != data->currfid) || (reqvid != data->currvid)) {
273 printk(KERN_ERR PFX "failed (cpu%d): req 0x%x 0x%x, curr 0x%x 0x%x\n",
274 smp_processor_id(),
275 reqfid, reqvid, data->currfid, data->currvid);
276 return 1;
277 }
278
279 dprintk("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
280 smp_processor_id(), data->currfid, data->currvid);
281
282 return 0;
283 }
284
285 /* Phase 1 - core voltage transition ... setup voltage */
286 static int core_voltage_pre_transition(struct powernow_k8_data *data, u32 reqvid)
287 {
288 u32 rvosteps = data->rvo;
289 u32 savefid = data->currfid;
290 u32 maxvid, lo;
291
292 dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, reqvid 0x%x, rvo 0x%x\n",
293 smp_processor_id(),
294 data->currfid, data->currvid, reqvid, data->rvo);
295
296 rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
297 maxvid = 0x1f & (maxvid >> 16);
298 dprintk("ph1 maxvid=0x%x\n", maxvid);
299 if (reqvid < maxvid) /* lower numbers are higher voltages */
300 reqvid = maxvid;
301
302 while (data->currvid > reqvid) {
303 dprintk("ph1: curr 0x%x, req vid 0x%x\n",
304 data->currvid, reqvid);
305 if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
306 return 1;
307 }
308
309 while ((rvosteps > 0) && ((data->rvo + data->currvid) > reqvid)) {
310 if (data->currvid == maxvid) {
311 rvosteps = 0;
312 } else {
313 dprintk("ph1: changing vid for rvo, req 0x%x\n",
314 data->currvid - 1);
315 if (decrease_vid_code_by_step(data, data->currvid - 1, 1))
316 return 1;
317 rvosteps--;
318 }
319 }
320
321 if (query_current_values_with_pending_wait(data))
322 return 1;
323
324 if (savefid != data->currfid) {
325 printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n", data->currfid);
326 return 1;
327 }
328
329 dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
330 data->currfid, data->currvid);
331
332 return 0;
333 }
334
335 /* Phase 2 - core frequency transition */
336 static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
337 {
338 u32 vcoreqfid, vcocurrfid, vcofiddiff, savevid = data->currvid;
339
340 if ((reqfid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
341 printk(KERN_ERR PFX "ph2: illegal lo-lo transition 0x%x 0x%x\n",
342 reqfid, data->currfid);
343 return 1;
344 }
345
346 if (data->currfid == reqfid) {
347 printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n", data->currfid);
348 return 0;
349 }
350
351 dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, reqfid 0x%x\n",
352 smp_processor_id(),
353 data->currfid, data->currvid, reqfid);
354
355 vcoreqfid = convert_fid_to_vco_fid(reqfid);
356 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
357 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
358 : vcoreqfid - vcocurrfid;
359
360 while (vcofiddiff > 2) {
361 if (reqfid > data->currfid) {
362 if (data->currfid > LO_FID_TABLE_TOP) {
363 if (write_new_fid(data, data->currfid + 2)) {
364 return 1;
365 }
366 } else {
367 if (write_new_fid
368 (data, 2 + convert_fid_to_vco_fid(data->currfid))) {
369 return 1;
370 }
371 }
372 } else {
373 if (write_new_fid(data, data->currfid - 2))
374 return 1;
375 }
376
377 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
378 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
379 : vcoreqfid - vcocurrfid;
380 }
381
382 if (write_new_fid(data, reqfid))
383 return 1;
384
385 if (query_current_values_with_pending_wait(data))
386 return 1;
387
388 if (data->currfid != reqfid) {
389 printk(KERN_ERR PFX
390 "ph2: mismatch, failed fid transition, curr 0x%x, req 0x%x\n",
391 data->currfid, reqfid);
392 return 1;
393 }
394
395 if (savevid != data->currvid) {
396 printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
397 savevid, data->currvid);
398 return 1;
399 }
400
401 dprintk("ph2 complete, currfid 0x%x, currvid 0x%x\n",
402 data->currfid, data->currvid);
403
404 return 0;
405 }
406
407 /* Phase 3 - core voltage transition flow ... jump to the final vid. */
408 static int core_voltage_post_transition(struct powernow_k8_data *data, u32 reqvid)
409 {
410 u32 savefid = data->currfid;
411 u32 savereqvid = reqvid;
412
413 dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
414 smp_processor_id(),
415 data->currfid, data->currvid);
416
417 if (reqvid != data->currvid) {
418 if (write_new_vid(data, reqvid))
419 return 1;
420
421 if (savefid != data->currfid) {
422 printk(KERN_ERR PFX
423 "ph3: bad fid change, save 0x%x, curr 0x%x\n",
424 savefid, data->currfid);
425 return 1;
426 }
427
428 if (data->currvid != reqvid) {
429 printk(KERN_ERR PFX
430 "ph3: failed vid transition\n, req 0x%x, curr 0x%x",
431 reqvid, data->currvid);
432 return 1;
433 }
434 }
435
436 if (query_current_values_with_pending_wait(data))
437 return 1;
438
439 if (savereqvid != data->currvid) {
440 dprintk("ph3 failed, currvid 0x%x\n", data->currvid);
441 return 1;
442 }
443
444 if (savefid != data->currfid) {
445 dprintk("ph3 failed, currfid changed 0x%x\n",
446 data->currfid);
447 return 1;
448 }
449
450 dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
451 data->currfid, data->currvid);
452
453 return 0;
454 }
455
456 static int check_supported_cpu(unsigned int cpu)
457 {
458 cpumask_t oldmask = CPU_MASK_ALL;
459 u32 eax, ebx, ecx, edx;
460 unsigned int rc = 0;
461
462 oldmask = current->cpus_allowed;
463 set_cpus_allowed(current, cpumask_of_cpu(cpu));
464 schedule();
465
466 if (smp_processor_id() != cpu) {
467 printk(KERN_ERR "limiting to cpu %u failed\n", cpu);
468 goto out;
469 }
470
471 if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
472 goto out;
473
474 eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
475 if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
476 ((eax & CPUID_XFAM) != CPUID_XFAM_K8) ||
477 ((eax & CPUID_XMOD) > CPUID_XMOD_REV_F)) {
478 printk(KERN_INFO PFX "Processor cpuid %x not supported\n", eax);
479 goto out;
480 }
481
482 eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
483 if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
484 printk(KERN_INFO PFX
485 "No frequency change capabilities detected\n");
486 goto out;
487 }
488
489 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
490 if ((edx & P_STATE_TRANSITION_CAPABLE) != P_STATE_TRANSITION_CAPABLE) {
491 printk(KERN_INFO PFX "Power state transitions not supported\n");
492 goto out;
493 }
494
495 rc = 1;
496
497 out:
498 set_cpus_allowed(current, oldmask);
499 schedule();
500 return rc;
501
502 }
503
504 static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
505 {
506 unsigned int j;
507 u8 lastfid = 0xff;
508
509 for (j = 0; j < data->numps; j++) {
510 if (pst[j].vid > LEAST_VID) {
511 printk(KERN_ERR PFX "vid %d invalid : 0x%x\n", j, pst[j].vid);
512 return -EINVAL;
513 }
514 if (pst[j].vid < data->rvo) { /* vid + rvo >= 0 */
515 printk(KERN_ERR BFX "0 vid exceeded with pstate %d\n", j);
516 return -ENODEV;
517 }
518 if (pst[j].vid < maxvid + data->rvo) { /* vid + rvo >= maxvid */
519 printk(KERN_ERR BFX "maxvid exceeded with pstate %d\n", j);
520 return -ENODEV;
521 }
522 if ((pst[j].fid > MAX_FID)
523 || (pst[j].fid & 1)
524 || (j && (pst[j].fid < HI_FID_TABLE_BOTTOM))) {
525 /* Only first fid is allowed to be in "low" range */
526 printk(KERN_ERR PFX "two low fids - %d : 0x%x\n", j, pst[j].fid);
527 return -EINVAL;
528 }
529 if (pst[j].fid < lastfid)
530 lastfid = pst[j].fid;
531 }
532 if (lastfid & 1) {
533 printk(KERN_ERR PFX "lastfid invalid\n");
534 return -EINVAL;
535 }
536 if (lastfid > LO_FID_TABLE_TOP)
537 printk(KERN_INFO PFX "first fid not from lo freq table\n");
538
539 return 0;
540 }
541
542 static void print_basics(struct powernow_k8_data *data)
543 {
544 int j;
545 for (j = 0; j < data->numps; j++) {
546 if (data->powernow_table[j].frequency != CPUFREQ_ENTRY_INVALID)
547 printk(KERN_INFO PFX " %d : fid 0x%x (%d MHz), vid 0x%x (%d mV)\n", j,
548 data->powernow_table[j].index & 0xff,
549 data->powernow_table[j].frequency/1000,
550 data->powernow_table[j].index >> 8,
551 find_millivolts_from_vid(data, data->powernow_table[j].index >> 8));
552 }
553 if (data->batps)
554 printk(KERN_INFO PFX "Only %d pstates on battery\n", data->batps);
555 }
556
557 static int fill_powernow_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
558 {
559 struct cpufreq_frequency_table *powernow_table;
560 unsigned int j;
561
562 if (data->batps) { /* use ACPI support to get full speed on mains power */
563 printk(KERN_WARNING PFX "Only %d pstates usable (use ACPI driver for full range\n", data->batps);
564 data->numps = data->batps;
565 }
566
567 for ( j=1; j<data->numps; j++ ) {
568 if (pst[j-1].fid >= pst[j].fid) {
569 printk(KERN_ERR PFX "PST out of sequence\n");
570 return -EINVAL;
571 }
572 }
573
574 if (data->numps < 2) {
575 printk(KERN_ERR PFX "no p states to transition\n");
576 return -ENODEV;
577 }
578
579 if (check_pst_table(data, pst, maxvid))
580 return -EINVAL;
581
582 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
583 * (data->numps + 1)), GFP_KERNEL);
584 if (!powernow_table) {
585 printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
586 return -ENOMEM;
587 }
588
589 for (j = 0; j < data->numps; j++) {
590 powernow_table[j].index = pst[j].fid; /* lower 8 bits */
591 powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
592 powernow_table[j].frequency = find_khz_freq_from_fid(pst[j].fid);
593 }
594 powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
595 powernow_table[data->numps].index = 0;
596
597 if (query_current_values_with_pending_wait(data)) {
598 kfree(powernow_table);
599 return -EIO;
600 }
601
602 dprintk("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
603 data->powernow_table = powernow_table;
604 print_basics(data);
605
606 for (j = 0; j < data->numps; j++)
607 if ((pst[j].fid==data->currfid) && (pst[j].vid==data->currvid))
608 return 0;
609
610 dprintk("currfid/vid do not match PST, ignoring\n");
611 return 0;
612 }
613
614 /* Find and validate the PSB/PST table in BIOS. */
615 static int find_psb_table(struct powernow_k8_data *data)
616 {
617 struct psb_s *psb;
618 unsigned int i;
619 u32 mvs;
620 u8 maxvid;
621 u32 cpst = 0;
622 u32 thiscpuid;
623
624 for (i = 0xc0000; i < 0xffff0; i += 0x10) {
625 /* Scan BIOS looking for the signature. */
626 /* It can not be at ffff0 - it is too big. */
627
628 psb = phys_to_virt(i);
629 if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
630 continue;
631
632 dprintk("found PSB header at 0x%p\n", psb);
633
634 dprintk("table vers: 0x%x\n", psb->tableversion);
635 if (psb->tableversion != PSB_VERSION_1_4) {
636 printk(KERN_INFO BFX "PSB table is not v1.4\n");
637 return -ENODEV;
638 }
639
640 dprintk("flags: 0x%x\n", psb->flags1);
641 if (psb->flags1) {
642 printk(KERN_ERR BFX "unknown flags\n");
643 return -ENODEV;
644 }
645
646 data->vstable = psb->vstable;
647 dprintk("voltage stabilization time: %d(*20us)\n", data->vstable);
648
649 dprintk("flags2: 0x%x\n", psb->flags2);
650 data->rvo = psb->flags2 & 3;
651 data->irt = ((psb->flags2) >> 2) & 3;
652 mvs = ((psb->flags2) >> 4) & 3;
653 data->vidmvs = 1 << mvs;
654 data->batps = ((psb->flags2) >> 6) & 3;
655
656 dprintk("ramp voltage offset: %d\n", data->rvo);
657 dprintk("isochronous relief time: %d\n", data->irt);
658 dprintk("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
659
660 dprintk("numpst: 0x%x\n", psb->num_tables);
661 cpst = psb->num_tables;
662 if ((psb->cpuid == 0x00000fc0) || (psb->cpuid == 0x00000fe0) ){
663 thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
664 if ((thiscpuid == 0x00000fc0) || (thiscpuid == 0x00000fe0) ) {
665 cpst = 1;
666 }
667 }
668 if (cpst != 1) {
669 printk(KERN_ERR BFX "numpst must be 1\n");
670 return -ENODEV;
671 }
672
673 data->plllock = psb->plllocktime;
674 dprintk("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
675 dprintk("maxfid: 0x%x\n", psb->maxfid);
676 dprintk("maxvid: 0x%x\n", psb->maxvid);
677 maxvid = psb->maxvid;
678
679 data->numps = psb->numps;
680 dprintk("numpstates: 0x%x\n", data->numps);
681 return fill_powernow_table(data, (struct pst_s *)(psb+1), maxvid);
682 }
683 /*
684 * If you see this message, complain to BIOS manufacturer. If
685 * he tells you "we do not support Linux" or some similar
686 * nonsense, remember that Windows 2000 uses the same legacy
687 * mechanism that the old Linux PSB driver uses. Tell them it
688 * is broken with Windows 2000.
689 *
690 * The reference to the AMD documentation is chapter 9 in the
691 * BIOS and Kernel Developer's Guide, which is available on
692 * www.amd.com
693 */
694 printk(KERN_INFO PFX "BIOS error - no PSB or ACPI _PSS objects\n");
695 return -ENODEV;
696 }
697
698 #ifdef CONFIG_X86_POWERNOW_K8_ACPI
699 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index)
700 {
701 if (!data->acpi_data.state_count)
702 return;
703
704 data->irt = (data->acpi_data.states[index].control >> IRT_SHIFT) & IRT_MASK;
705 data->rvo = (data->acpi_data.states[index].control >> RVO_SHIFT) & RVO_MASK;
706 data->exttype = (data->acpi_data.states[index].control >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
707 data->plllock = (data->acpi_data.states[index].control >> PLL_L_SHIFT) & PLL_L_MASK;
708 data->vidmvs = 1 << ((data->acpi_data.states[index].control >> MVS_SHIFT) & MVS_MASK);
709 data->vstable = (data->acpi_data.states[index].control >> VST_SHIFT) & VST_MASK;
710 }
711
712 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
713 {
714 int i;
715 int cntlofreq = 0;
716 struct cpufreq_frequency_table *powernow_table;
717
718 if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
719 dprintk("register performance failed: bad ACPI data\n");
720 return -EIO;
721 }
722
723 /* verify the data contained in the ACPI structures */
724 if (data->acpi_data.state_count <= 1) {
725 dprintk("No ACPI P-States\n");
726 goto err_out;
727 }
728
729 if ((data->acpi_data.control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
730 (data->acpi_data.status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
731 dprintk("Invalid control/status registers (%x - %x)\n",
732 data->acpi_data.control_register.space_id,
733 data->acpi_data.status_register.space_id);
734 goto err_out;
735 }
736
737 /* fill in data->powernow_table */
738 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
739 * (data->acpi_data.state_count + 1)), GFP_KERNEL);
740 if (!powernow_table) {
741 dprintk("powernow_table memory alloc failure\n");
742 goto err_out;
743 }
744
745 for (i = 0; i < data->acpi_data.state_count; i++) {
746 u32 fid;
747 u32 vid;
748
749 if (data->exttype) {
750 fid = data->acpi_data.states[i].status & FID_MASK;
751 vid = (data->acpi_data.states[i].status >> VID_SHIFT) & VID_MASK;
752 } else {
753 fid = data->acpi_data.states[i].control & FID_MASK;
754 vid = (data->acpi_data.states[i].control >> VID_SHIFT) & VID_MASK;
755 }
756
757 dprintk(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
758
759 powernow_table[i].index = fid; /* lower 8 bits */
760 powernow_table[i].index |= (vid << 8); /* upper 8 bits */
761 powernow_table[i].frequency = find_khz_freq_from_fid(fid);
762
763 /* verify frequency is OK */
764 if ((powernow_table[i].frequency > (MAX_FREQ * 1000)) ||
765 (powernow_table[i].frequency < (MIN_FREQ * 1000))) {
766 dprintk("invalid freq %u kHz, ignoring\n", powernow_table[i].frequency);
767 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
768 continue;
769 }
770
771 /* verify voltage is OK - BIOSs are using "off" to indicate invalid */
772 if (vid == VID_OFF) {
773 dprintk("invalid vid %u, ignoring\n", vid);
774 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
775 continue;
776 }
777
778 /* verify only 1 entry from the lo frequency table */
779 if (fid < HI_FID_TABLE_BOTTOM) {
780 if (cntlofreq) {
781 /* if both entries are the same, ignore this
782 * one...
783 */
784 if ((powernow_table[i].frequency != powernow_table[cntlofreq].frequency) ||
785 (powernow_table[i].index != powernow_table[cntlofreq].index)) {
786 printk(KERN_ERR PFX "Too many lo freq table entries\n");
787 goto err_out_mem;
788 }
789
790 dprintk("double low frequency table entry, ignoring it.\n");
791 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
792 continue;
793 } else
794 cntlofreq = i;
795 }
796
797 if (powernow_table[i].frequency != (data->acpi_data.states[i].core_frequency * 1000)) {
798 printk(KERN_INFO PFX "invalid freq entries %u kHz vs. %u kHz\n",
799 powernow_table[i].frequency,
800 (unsigned int) (data->acpi_data.states[i].core_frequency * 1000));
801 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
802 continue;
803 }
804 }
805
806 powernow_table[data->acpi_data.state_count].frequency = CPUFREQ_TABLE_END;
807 powernow_table[data->acpi_data.state_count].index = 0;
808 data->powernow_table = powernow_table;
809
810 /* fill in data */
811 data->numps = data->acpi_data.state_count;
812 print_basics(data);
813 powernow_k8_acpi_pst_values(data, 0);
814
815 /* notify BIOS that we exist */
816 acpi_processor_notify_smm(THIS_MODULE);
817
818 return 0;
819
820 err_out_mem:
821 kfree(powernow_table);
822
823 err_out:
824 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
825
826 /* data->acpi_data.state_count informs us at ->exit() whether ACPI was used */
827 data->acpi_data.state_count = 0;
828
829 return -ENODEV;
830 }
831
832 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
833 {
834 if (data->acpi_data.state_count)
835 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
836 }
837
838 #else
839 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data) { return -ENODEV; }
840 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data) { return; }
841 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index) { return; }
842 #endif /* CONFIG_X86_POWERNOW_K8_ACPI */
843
844 /* Take a frequency, and issue the fid/vid transition command */
845 static int transition_frequency(struct powernow_k8_data *data, unsigned int index)
846 {
847 u32 fid;
848 u32 vid;
849 int res, i;
850 struct cpufreq_freqs freqs;
851
852 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
853
854 /* fid are the lower 8 bits of the index we stored into
855 * the cpufreq frequency table in find_psb_table, vid are
856 * the upper 8 bits.
857 */
858
859 fid = data->powernow_table[index].index & 0xFF;
860 vid = (data->powernow_table[index].index & 0xFF00) >> 8;
861
862 dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
863
864 if (query_current_values_with_pending_wait(data))
865 return 1;
866
867 if ((data->currvid == vid) && (data->currfid == fid)) {
868 dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
869 fid, vid);
870 return 0;
871 }
872
873 if ((fid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
874 printk(KERN_ERR PFX
875 "ignoring illegal change in lo freq table-%x to 0x%x\n",
876 data->currfid, fid);
877 return 1;
878 }
879
880 dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
881 smp_processor_id(), fid, vid);
882
883 freqs.cpu = data->cpu;
884 freqs.old = find_khz_freq_from_fid(data->currfid);
885 freqs.new = find_khz_freq_from_fid(fid);
886 for_each_cpu_mask(i, cpu_core_map[data->cpu]) {
887 freqs.cpu = i;
888 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
889 }
890
891 res = transition_fid_vid(data, fid, vid);
892
893 freqs.new = find_khz_freq_from_fid(data->currfid);
894 for_each_cpu_mask(i, cpu_core_map[data->cpu]) {
895 freqs.cpu = i;
896 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
897 }
898 return res;
899 }
900
901 /* Driver entry point to switch to the target frequency */
902 static int powernowk8_target(struct cpufreq_policy *pol, unsigned targfreq, unsigned relation)
903 {
904 cpumask_t oldmask = CPU_MASK_ALL;
905 struct powernow_k8_data *data = powernow_data[pol->cpu];
906 u32 checkfid = data->currfid;
907 u32 checkvid = data->currvid;
908 unsigned int newstate;
909 int ret = -EIO;
910 int i;
911
912 /* only run on specific CPU from here on */
913 oldmask = current->cpus_allowed;
914 set_cpus_allowed(current, cpumask_of_cpu(pol->cpu));
915 schedule();
916
917 if (smp_processor_id() != pol->cpu) {
918 printk(KERN_ERR "limiting to cpu %u failed\n", pol->cpu);
919 goto err_out;
920 }
921
922 if (pending_bit_stuck()) {
923 printk(KERN_ERR PFX "failing targ, change pending bit set\n");
924 goto err_out;
925 }
926
927 dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
928 pol->cpu, targfreq, pol->min, pol->max, relation);
929
930 if (query_current_values_with_pending_wait(data)) {
931 ret = -EIO;
932 goto err_out;
933 }
934
935 dprintk("targ: curr fid 0x%x, vid 0x%x\n",
936 data->currfid, data->currvid);
937
938 if ((checkvid != data->currvid) || (checkfid != data->currfid)) {
939 printk(KERN_INFO PFX
940 "error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
941 checkfid, data->currfid, checkvid, data->currvid);
942 }
943
944 if (cpufreq_frequency_table_target(pol, data->powernow_table, targfreq, relation, &newstate))
945 goto err_out;
946
947 down(&fidvid_sem);
948
949 powernow_k8_acpi_pst_values(data, newstate);
950
951 if (transition_frequency(data, newstate)) {
952 printk(KERN_ERR PFX "transition frequency failed\n");
953 ret = 1;
954 up(&fidvid_sem);
955 goto err_out;
956 }
957
958 /* Update all the fid/vids of our siblings */
959 for_each_cpu_mask(i, cpu_core_map[pol->cpu]) {
960 powernow_data[i]->currvid = data->currvid;
961 powernow_data[i]->currfid = data->currfid;
962 }
963 up(&fidvid_sem);
964
965 pol->cur = find_khz_freq_from_fid(data->currfid);
966 ret = 0;
967
968 err_out:
969 set_cpus_allowed(current, oldmask);
970 schedule();
971
972 return ret;
973 }
974
975 /* Driver entry point to verify the policy and range of frequencies */
976 static int powernowk8_verify(struct cpufreq_policy *pol)
977 {
978 struct powernow_k8_data *data = powernow_data[pol->cpu];
979
980 return cpufreq_frequency_table_verify(pol, data->powernow_table);
981 }
982
983 /* per CPU init entry point to the driver */
984 static int __init powernowk8_cpu_init(struct cpufreq_policy *pol)
985 {
986 struct powernow_k8_data *data;
987 cpumask_t oldmask = CPU_MASK_ALL;
988 int rc, i;
989
990 if (!check_supported_cpu(pol->cpu))
991 return -ENODEV;
992
993 data = kmalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
994 if (!data) {
995 printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
996 return -ENOMEM;
997 }
998 memset(data,0,sizeof(struct powernow_k8_data));
999
1000 data->cpu = pol->cpu;
1001
1002 if (powernow_k8_cpu_init_acpi(data)) {
1003 /*
1004 * Use the PSB BIOS structure. This is only availabe on
1005 * an UP version, and is deprecated by AMD.
1006 */
1007
1008 if ((num_online_cpus() != 1) || (num_possible_cpus() != 1)) {
1009 printk(KERN_ERR PFX "MP systems not supported by PSB BIOS structure\n");
1010 kfree(data);
1011 return -ENODEV;
1012 }
1013 if (pol->cpu != 0) {
1014 printk(KERN_ERR PFX "init not cpu 0\n");
1015 kfree(data);
1016 return -ENODEV;
1017 }
1018 rc = find_psb_table(data);
1019 if (rc) {
1020 kfree(data);
1021 return -ENODEV;
1022 }
1023 }
1024
1025 /* only run on specific CPU from here on */
1026 oldmask = current->cpus_allowed;
1027 set_cpus_allowed(current, cpumask_of_cpu(pol->cpu));
1028 schedule();
1029
1030 if (smp_processor_id() != pol->cpu) {
1031 printk(KERN_ERR "limiting to cpu %u failed\n", pol->cpu);
1032 goto err_out;
1033 }
1034
1035 if (pending_bit_stuck()) {
1036 printk(KERN_ERR PFX "failing init, change pending bit set\n");
1037 goto err_out;
1038 }
1039
1040 if (query_current_values_with_pending_wait(data))
1041 goto err_out;
1042
1043 fidvid_msr_init();
1044
1045 /* run on any CPU again */
1046 set_cpus_allowed(current, oldmask);
1047 schedule();
1048
1049 pol->governor = CPUFREQ_DEFAULT_GOVERNOR;
1050 pol->cpus = cpu_core_map[pol->cpu];
1051
1052 /* Take a crude guess here.
1053 * That guess was in microseconds, so multiply with 1000 */
1054 pol->cpuinfo.transition_latency = (((data->rvo + 8) * data->vstable * VST_UNITS_20US)
1055 + (3 * (1 << data->irt) * 10)) * 1000;
1056
1057 pol->cur = find_khz_freq_from_fid(data->currfid);
1058 dprintk("policy current frequency %d kHz\n", pol->cur);
1059
1060 /* min/max the cpu is capable of */
1061 if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
1062 printk(KERN_ERR PFX "invalid powernow_table\n");
1063 powernow_k8_cpu_exit_acpi(data);
1064 kfree(data->powernow_table);
1065 kfree(data);
1066 return -EINVAL;
1067 }
1068
1069 cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
1070
1071 printk("cpu_init done, current fid 0x%x, vid 0x%x\n",
1072 data->currfid, data->currvid);
1073
1074 for_each_cpu_mask(i, cpu_core_map[pol->cpu]) {
1075 powernow_data[i] = data;
1076 }
1077
1078 return 0;
1079
1080 err_out:
1081 set_cpus_allowed(current, oldmask);
1082 schedule();
1083 powernow_k8_cpu_exit_acpi(data);
1084
1085 kfree(data);
1086 return -ENODEV;
1087 }
1088
1089 static int __devexit powernowk8_cpu_exit (struct cpufreq_policy *pol)
1090 {
1091 struct powernow_k8_data *data = powernow_data[pol->cpu];
1092
1093 if (!data)
1094 return -EINVAL;
1095
1096 powernow_k8_cpu_exit_acpi(data);
1097
1098 cpufreq_frequency_table_put_attr(pol->cpu);
1099
1100 kfree(data->powernow_table);
1101 kfree(data);
1102
1103 return 0;
1104 }
1105
1106 static unsigned int powernowk8_get (unsigned int cpu)
1107 {
1108 struct powernow_k8_data *data = powernow_data[cpu];
1109 cpumask_t oldmask = current->cpus_allowed;
1110 unsigned int khz = 0;
1111
1112 set_cpus_allowed(current, cpumask_of_cpu(cpu));
1113 if (smp_processor_id() != cpu) {
1114 printk(KERN_ERR PFX "limiting to CPU %d failed in powernowk8_get\n", cpu);
1115 set_cpus_allowed(current, oldmask);
1116 return 0;
1117 }
1118 preempt_disable();
1119
1120 if (query_current_values_with_pending_wait(data))
1121 goto out;
1122
1123 khz = find_khz_freq_from_fid(data->currfid);
1124
1125 out:
1126 preempt_enable_no_resched();
1127 set_cpus_allowed(current, oldmask);
1128
1129 return khz;
1130 }
1131
1132 static struct freq_attr* powernow_k8_attr[] = {
1133 &cpufreq_freq_attr_scaling_available_freqs,
1134 NULL,
1135 };
1136
1137 static struct cpufreq_driver cpufreq_amd64_driver = {
1138 .verify = powernowk8_verify,
1139 .target = powernowk8_target,
1140 .init = powernowk8_cpu_init,
1141 .exit = __devexit_p(powernowk8_cpu_exit),
1142 .get = powernowk8_get,
1143 .name = "powernow-k8",
1144 .owner = THIS_MODULE,
1145 .attr = powernow_k8_attr,
1146 };
1147
1148 /* driver entry point for init */
1149 static int __init powernowk8_init(void)
1150 {
1151 unsigned int i, supported_cpus = 0;
1152
1153 for (i=0; i<NR_CPUS; i++) {
1154 if (!cpu_online(i))
1155 continue;
1156 if (check_supported_cpu(i))
1157 supported_cpus++;
1158 }
1159
1160 if (supported_cpus == num_online_cpus()) {
1161 printk(KERN_INFO PFX "Found %d AMD Athlon 64 / Opteron processors (" VERSION ")\n",
1162 supported_cpus);
1163 return cpufreq_register_driver(&cpufreq_amd64_driver);
1164 }
1165
1166 return -ENODEV;
1167 }
1168
1169 /* driver entry point for term */
1170 static void __exit powernowk8_exit(void)
1171 {
1172 dprintk("exit\n");
1173
1174 cpufreq_unregister_driver(&cpufreq_amd64_driver);
1175 }
1176
1177 MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and Mark Langsdorf <mark.langsdorf@amd.com.");
1178 MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
1179 MODULE_LICENSE("GPL");
1180
1181 late_initcall(powernowk8_init);
1182 module_exit(powernowk8_exit);
1183
This page took 0.071376 seconds and 4 git commands to generate.