powernow-k8.c 31 KB

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