powernow-k8.c 31 KB

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