powernow-k8.c 37 KB

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