powernow-k8.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /*
  2. * (c) 2003-2010 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 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 <linux/io.h>
  36. #include <linux/delay.h>
  37. #include <asm/msr.h>
  38. #include <linux/acpi.h>
  39. #include <linux/mutex.h>
  40. #include <acpi/processor.h>
  41. #define PFX "powernow-k8: "
  42. #define VERSION "version 2.20.00"
  43. #include "powernow-k8.h"
  44. /* serialize freq changes */
  45. static DEFINE_MUTEX(fidvid_mutex);
  46. static DEFINE_PER_CPU(struct powernow_k8_data *, powernow_data);
  47. static int cpu_family = CPU_OPTERON;
  48. /* core performance boost */
  49. static bool cpb_capable, cpb_enabled;
  50. static struct msr __percpu *msrs;
  51. #ifndef CONFIG_SMP
  52. static inline const struct cpumask *cpu_core_mask(int cpu)
  53. {
  54. return cpumask_of(0);
  55. }
  56. #endif
  57. /* Return a frequency in MHz, given an input fid */
  58. static u32 find_freq_from_fid(u32 fid)
  59. {
  60. return 800 + (fid * 100);
  61. }
  62. /* Return a frequency in KHz, given an input fid */
  63. static u32 find_khz_freq_from_fid(u32 fid)
  64. {
  65. return 1000 * find_freq_from_fid(fid);
  66. }
  67. static u32 find_khz_freq_from_pstate(struct cpufreq_frequency_table *data,
  68. u32 pstate)
  69. {
  70. return data[pstate].frequency;
  71. }
  72. /* Return the vco fid for an input fid
  73. *
  74. * Each "low" fid has corresponding "high" fid, and you can get to "low" fids
  75. * only from corresponding high fids. This returns "high" fid corresponding to
  76. * "low" one.
  77. */
  78. static u32 convert_fid_to_vco_fid(u32 fid)
  79. {
  80. if (fid < HI_FID_TABLE_BOTTOM)
  81. return 8 + (2 * fid);
  82. else
  83. return fid;
  84. }
  85. /*
  86. * Return 1 if the pending bit is set. Unless we just instructed the processor
  87. * to transition to a new state, seeing this bit set is really bad news.
  88. */
  89. static int pending_bit_stuck(void)
  90. {
  91. u32 lo, hi;
  92. if (cpu_family == CPU_HW_PSTATE)
  93. return 0;
  94. rdmsr(MSR_FIDVID_STATUS, lo, hi);
  95. return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0;
  96. }
  97. /*
  98. * Update the global current fid / vid values from the status msr.
  99. * Returns 1 on error.
  100. */
  101. static int query_current_values_with_pending_wait(struct powernow_k8_data *data)
  102. {
  103. u32 lo, hi;
  104. u32 i = 0;
  105. if (cpu_family == CPU_HW_PSTATE) {
  106. rdmsr(MSR_PSTATE_STATUS, lo, hi);
  107. i = lo & HW_PSTATE_MASK;
  108. data->currpstate = i;
  109. /*
  110. * a workaround for family 11h erratum 311 might cause
  111. * an "out-of-range Pstate if the core is in Pstate-0
  112. */
  113. if ((boot_cpu_data.x86 == 0x11) && (i >= data->numps))
  114. data->currpstate = HW_PSTATE_0;
  115. return 0;
  116. }
  117. do {
  118. if (i++ > 10000) {
  119. dprintk("detected change pending stuck\n");
  120. return 1;
  121. }
  122. rdmsr(MSR_FIDVID_STATUS, lo, hi);
  123. } while (lo & MSR_S_LO_CHANGE_PENDING);
  124. data->currvid = hi & MSR_S_HI_CURRENT_VID;
  125. data->currfid = lo & MSR_S_LO_CURRENT_FID;
  126. return 0;
  127. }
  128. /* the isochronous relief time */
  129. static void count_off_irt(struct powernow_k8_data *data)
  130. {
  131. udelay((1 << data->irt) * 10);
  132. return;
  133. }
  134. /* the voltage stabilization time */
  135. static void count_off_vst(struct powernow_k8_data *data)
  136. {
  137. udelay(data->vstable * VST_UNITS_20US);
  138. return;
  139. }
  140. /* need to init the control msr to a safe value (for each cpu) */
  141. static void fidvid_msr_init(void)
  142. {
  143. u32 lo, hi;
  144. u8 fid, vid;
  145. rdmsr(MSR_FIDVID_STATUS, lo, hi);
  146. vid = hi & MSR_S_HI_CURRENT_VID;
  147. fid = lo & MSR_S_LO_CURRENT_FID;
  148. lo = fid | (vid << MSR_C_LO_VID_SHIFT);
  149. hi = MSR_C_HI_STP_GNT_BENIGN;
  150. dprintk("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi);
  151. wrmsr(MSR_FIDVID_CTL, lo, hi);
  152. }
  153. /* write the new fid value along with the other control fields to the msr */
  154. static int write_new_fid(struct powernow_k8_data *data, u32 fid)
  155. {
  156. u32 lo;
  157. u32 savevid = data->currvid;
  158. u32 i = 0;
  159. if ((fid & INVALID_FID_MASK) || (data->currvid & INVALID_VID_MASK)) {
  160. printk(KERN_ERR PFX "internal error - overflow on fid write\n");
  161. return 1;
  162. }
  163. lo = fid;
  164. lo |= (data->currvid << MSR_C_LO_VID_SHIFT);
  165. lo |= MSR_C_LO_INIT_FID_VID;
  166. dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
  167. fid, lo, data->plllock * PLL_LOCK_CONVERSION);
  168. do {
  169. wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
  170. if (i++ > 100) {
  171. printk(KERN_ERR PFX
  172. "Hardware error - pending bit very stuck - "
  173. "no further pstate changes possible\n");
  174. return 1;
  175. }
  176. } while (query_current_values_with_pending_wait(data));
  177. count_off_irt(data);
  178. if (savevid != data->currvid) {
  179. printk(KERN_ERR PFX
  180. "vid change on fid trans, old 0x%x, new 0x%x\n",
  181. savevid, data->currvid);
  182. return 1;
  183. }
  184. if (fid != data->currfid) {
  185. printk(KERN_ERR PFX
  186. "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
  187. data->currfid);
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. /* Write a new vid to the hardware */
  193. static int write_new_vid(struct powernow_k8_data *data, u32 vid)
  194. {
  195. u32 lo;
  196. u32 savefid = data->currfid;
  197. int i = 0;
  198. if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
  199. printk(KERN_ERR PFX "internal error - overflow on vid write\n");
  200. return 1;
  201. }
  202. lo = data->currfid;
  203. lo |= (vid << MSR_C_LO_VID_SHIFT);
  204. lo |= MSR_C_LO_INIT_FID_VID;
  205. dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
  206. vid, lo, STOP_GRANT_5NS);
  207. do {
  208. wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
  209. if (i++ > 100) {
  210. printk(KERN_ERR PFX "internal error - pending bit "
  211. "very stuck - no further pstate "
  212. "changes possible\n");
  213. return 1;
  214. }
  215. } while (query_current_values_with_pending_wait(data));
  216. if (savefid != data->currfid) {
  217. printk(KERN_ERR PFX "fid changed on vid trans, old "
  218. "0x%x new 0x%x\n",
  219. savefid, data->currfid);
  220. return 1;
  221. }
  222. if (vid != data->currvid) {
  223. printk(KERN_ERR PFX "vid trans failed, vid 0x%x, "
  224. "curr 0x%x\n",
  225. vid, 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,
  236. u32 reqvid, u32 step)
  237. {
  238. if ((data->currvid - reqvid) > step)
  239. reqvid = data->currvid - step;
  240. if (write_new_vid(data, reqvid))
  241. return 1;
  242. count_off_vst(data);
  243. return 0;
  244. }
  245. /* Change hardware pstate by single MSR write */
  246. static int transition_pstate(struct powernow_k8_data *data, u32 pstate)
  247. {
  248. wrmsr(MSR_PSTATE_CTRL, pstate, 0);
  249. data->currpstate = pstate;
  250. return 0;
  251. }
  252. /* Change Opteron/Athlon64 fid and vid, by the 3 phases. */
  253. static int transition_fid_vid(struct powernow_k8_data *data,
  254. u32 reqfid, u32 reqvid)
  255. {
  256. if (core_voltage_pre_transition(data, reqvid, reqfid))
  257. return 1;
  258. if (core_frequency_transition(data, reqfid))
  259. return 1;
  260. if (core_voltage_post_transition(data, reqvid))
  261. return 1;
  262. if (query_current_values_with_pending_wait(data))
  263. return 1;
  264. if ((reqfid != data->currfid) || (reqvid != data->currvid)) {
  265. printk(KERN_ERR PFX "failed (cpu%d): req 0x%x 0x%x, "
  266. "curr 0x%x 0x%x\n",
  267. smp_processor_id(),
  268. reqfid, reqvid, data->currfid, data->currvid);
  269. return 1;
  270. }
  271. dprintk("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
  272. smp_processor_id(), data->currfid, data->currvid);
  273. return 0;
  274. }
  275. /* Phase 1 - core voltage transition ... setup voltage */
  276. static int core_voltage_pre_transition(struct powernow_k8_data *data,
  277. u32 reqvid, u32 reqfid)
  278. {
  279. u32 rvosteps = data->rvo;
  280. u32 savefid = data->currfid;
  281. u32 maxvid, lo, rvomult = 1;
  282. dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, "
  283. "reqvid 0x%x, rvo 0x%x\n",
  284. smp_processor_id(),
  285. data->currfid, data->currvid, reqvid, data->rvo);
  286. if ((savefid < LO_FID_TABLE_TOP) && (reqfid < LO_FID_TABLE_TOP))
  287. rvomult = 2;
  288. rvosteps *= rvomult;
  289. rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
  290. maxvid = 0x1f & (maxvid >> 16);
  291. dprintk("ph1 maxvid=0x%x\n", maxvid);
  292. if (reqvid < maxvid) /* lower numbers are higher voltages */
  293. reqvid = maxvid;
  294. while (data->currvid > reqvid) {
  295. dprintk("ph1: curr 0x%x, req vid 0x%x\n",
  296. data->currvid, reqvid);
  297. if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
  298. return 1;
  299. }
  300. while ((rvosteps > 0) &&
  301. ((rvomult * data->rvo + data->currvid) > reqvid)) {
  302. if (data->currvid == maxvid) {
  303. rvosteps = 0;
  304. } else {
  305. dprintk("ph1: changing vid for rvo, req 0x%x\n",
  306. data->currvid - 1);
  307. if (decrease_vid_code_by_step(data, data->currvid-1, 1))
  308. return 1;
  309. rvosteps--;
  310. }
  311. }
  312. if (query_current_values_with_pending_wait(data))
  313. return 1;
  314. if (savefid != data->currfid) {
  315. printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n",
  316. data->currfid);
  317. return 1;
  318. }
  319. dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
  320. data->currfid, data->currvid);
  321. return 0;
  322. }
  323. /* Phase 2 - core frequency transition */
  324. static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
  325. {
  326. u32 vcoreqfid, vcocurrfid, vcofiddiff;
  327. u32 fid_interval, savevid = data->currvid;
  328. if (data->currfid == reqfid) {
  329. printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n",
  330. data->currfid);
  331. return 0;
  332. }
  333. dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, "
  334. "reqfid 0x%x\n",
  335. smp_processor_id(),
  336. data->currfid, data->currvid, reqfid);
  337. vcoreqfid = convert_fid_to_vco_fid(reqfid);
  338. vcocurrfid = convert_fid_to_vco_fid(data->currfid);
  339. vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
  340. : vcoreqfid - vcocurrfid;
  341. if ((reqfid <= LO_FID_TABLE_TOP) && (data->currfid <= LO_FID_TABLE_TOP))
  342. vcofiddiff = 0;
  343. while (vcofiddiff > 2) {
  344. (data->currfid & 1) ? (fid_interval = 1) : (fid_interval = 2);
  345. if (reqfid > data->currfid) {
  346. if (data->currfid > LO_FID_TABLE_TOP) {
  347. if (write_new_fid(data,
  348. data->currfid + fid_interval))
  349. return 1;
  350. } else {
  351. if (write_new_fid
  352. (data,
  353. 2 + convert_fid_to_vco_fid(data->currfid)))
  354. return 1;
  355. }
  356. } else {
  357. if (write_new_fid(data, data->currfid - fid_interval))
  358. return 1;
  359. }
  360. vcocurrfid = convert_fid_to_vco_fid(data->currfid);
  361. vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
  362. : vcoreqfid - vcocurrfid;
  363. }
  364. if (write_new_fid(data, reqfid))
  365. return 1;
  366. if (query_current_values_with_pending_wait(data))
  367. return 1;
  368. if (data->currfid != reqfid) {
  369. printk(KERN_ERR PFX
  370. "ph2: mismatch, failed fid transition, "
  371. "curr 0x%x, req 0x%x\n",
  372. data->currfid, reqfid);
  373. return 1;
  374. }
  375. if (savevid != data->currvid) {
  376. printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
  377. savevid, data->currvid);
  378. return 1;
  379. }
  380. dprintk("ph2 complete, currfid 0x%x, currvid 0x%x\n",
  381. data->currfid, data->currvid);
  382. return 0;
  383. }
  384. /* Phase 3 - core voltage transition flow ... jump to the final vid. */
  385. static int core_voltage_post_transition(struct powernow_k8_data *data,
  386. u32 reqvid)
  387. {
  388. u32 savefid = data->currfid;
  389. u32 savereqvid = reqvid;
  390. dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
  391. smp_processor_id(),
  392. data->currfid, data->currvid);
  393. if (reqvid != data->currvid) {
  394. if (write_new_vid(data, reqvid))
  395. return 1;
  396. if (savefid != data->currfid) {
  397. printk(KERN_ERR PFX
  398. "ph3: bad fid change, save 0x%x, curr 0x%x\n",
  399. savefid, data->currfid);
  400. return 1;
  401. }
  402. if (data->currvid != reqvid) {
  403. printk(KERN_ERR PFX
  404. "ph3: failed vid transition\n, "
  405. "req 0x%x, curr 0x%x",
  406. reqvid, data->currvid);
  407. return 1;
  408. }
  409. }
  410. if (query_current_values_with_pending_wait(data))
  411. return 1;
  412. if (savereqvid != data->currvid) {
  413. dprintk("ph3 failed, currvid 0x%x\n", data->currvid);
  414. return 1;
  415. }
  416. if (savefid != data->currfid) {
  417. dprintk("ph3 failed, currfid changed 0x%x\n",
  418. data->currfid);
  419. return 1;
  420. }
  421. dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
  422. data->currfid, data->currvid);
  423. return 0;
  424. }
  425. static void check_supported_cpu(void *_rc)
  426. {
  427. u32 eax, ebx, ecx, edx;
  428. int *rc = _rc;
  429. *rc = -ENODEV;
  430. if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
  431. return;
  432. eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
  433. if (((eax & CPUID_XFAM) != CPUID_XFAM_K8) &&
  434. ((eax & CPUID_XFAM) < CPUID_XFAM_10H))
  435. return;
  436. if ((eax & CPUID_XFAM) == CPUID_XFAM_K8) {
  437. if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
  438. ((eax & CPUID_XMOD) > CPUID_XMOD_REV_MASK)) {
  439. printk(KERN_INFO PFX
  440. "Processor cpuid %x not supported\n", eax);
  441. return;
  442. }
  443. eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
  444. if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
  445. printk(KERN_INFO PFX
  446. "No frequency change capabilities detected\n");
  447. return;
  448. }
  449. cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
  450. if ((edx & P_STATE_TRANSITION_CAPABLE)
  451. != P_STATE_TRANSITION_CAPABLE) {
  452. printk(KERN_INFO PFX
  453. "Power state transitions not supported\n");
  454. return;
  455. }
  456. } else { /* must be a HW Pstate capable processor */
  457. cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
  458. if ((edx & USE_HW_PSTATE) == USE_HW_PSTATE)
  459. cpu_family = CPU_HW_PSTATE;
  460. else
  461. return;
  462. }
  463. *rc = 0;
  464. }
  465. static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst,
  466. u8 maxvid)
  467. {
  468. unsigned int j;
  469. u8 lastfid = 0xff;
  470. for (j = 0; j < data->numps; j++) {
  471. if (pst[j].vid > LEAST_VID) {
  472. printk(KERN_ERR FW_BUG PFX "vid %d invalid : 0x%x\n",
  473. j, pst[j].vid);
  474. return -EINVAL;
  475. }
  476. if (pst[j].vid < data->rvo) {
  477. /* vid + rvo >= 0 */
  478. printk(KERN_ERR FW_BUG PFX "0 vid exceeded with pstate"
  479. " %d\n", j);
  480. return -ENODEV;
  481. }
  482. if (pst[j].vid < maxvid + data->rvo) {
  483. /* vid + rvo >= maxvid */
  484. printk(KERN_ERR FW_BUG PFX "maxvid exceeded with pstate"
  485. " %d\n", j);
  486. return -ENODEV;
  487. }
  488. if (pst[j].fid > MAX_FID) {
  489. printk(KERN_ERR FW_BUG PFX "maxfid exceeded with pstate"
  490. " %d\n", j);
  491. return -ENODEV;
  492. }
  493. if (j && (pst[j].fid < HI_FID_TABLE_BOTTOM)) {
  494. /* Only first fid is allowed to be in "low" range */
  495. printk(KERN_ERR FW_BUG PFX "two low fids - %d : "
  496. "0x%x\n", j, pst[j].fid);
  497. return -EINVAL;
  498. }
  499. if (pst[j].fid < lastfid)
  500. lastfid = pst[j].fid;
  501. }
  502. if (lastfid & 1) {
  503. printk(KERN_ERR FW_BUG PFX "lastfid invalid\n");
  504. return -EINVAL;
  505. }
  506. if (lastfid > LO_FID_TABLE_TOP)
  507. printk(KERN_INFO FW_BUG PFX
  508. "first fid not from lo freq table\n");
  509. return 0;
  510. }
  511. static void invalidate_entry(struct cpufreq_frequency_table *powernow_table,
  512. unsigned int entry)
  513. {
  514. powernow_table[entry].frequency = CPUFREQ_ENTRY_INVALID;
  515. }
  516. static void print_basics(struct powernow_k8_data *data)
  517. {
  518. int j;
  519. for (j = 0; j < data->numps; j++) {
  520. if (data->powernow_table[j].frequency !=
  521. CPUFREQ_ENTRY_INVALID) {
  522. if (cpu_family == CPU_HW_PSTATE) {
  523. printk(KERN_INFO PFX
  524. " %d : pstate %d (%d MHz)\n", j,
  525. data->powernow_table[j].index,
  526. data->powernow_table[j].frequency/1000);
  527. } else {
  528. printk(KERN_INFO PFX
  529. " %d : fid 0x%x (%d MHz), vid 0x%x\n",
  530. j,
  531. data->powernow_table[j].index & 0xff,
  532. data->powernow_table[j].frequency/1000,
  533. data->powernow_table[j].index >> 8);
  534. }
  535. }
  536. }
  537. if (data->batps)
  538. printk(KERN_INFO PFX "Only %d pstates on battery\n",
  539. data->batps);
  540. }
  541. static u32 freq_from_fid_did(u32 fid, u32 did)
  542. {
  543. u32 mhz = 0;
  544. if (boot_cpu_data.x86 == 0x10)
  545. mhz = (100 * (fid + 0x10)) >> did;
  546. else if (boot_cpu_data.x86 == 0x11)
  547. mhz = (100 * (fid + 8)) >> did;
  548. else
  549. BUG();
  550. return mhz * 1000;
  551. }
  552. static int fill_powernow_table(struct powernow_k8_data *data,
  553. struct pst_s *pst, u8 maxvid)
  554. {
  555. struct cpufreq_frequency_table *powernow_table;
  556. unsigned int j;
  557. if (data->batps) {
  558. /* use ACPI support to get full speed on mains power */
  559. printk(KERN_WARNING PFX
  560. "Only %d pstates usable (use ACPI driver for full "
  561. "range\n", data->batps);
  562. data->numps = data->batps;
  563. }
  564. for (j = 1; j < data->numps; j++) {
  565. if (pst[j-1].fid >= pst[j].fid) {
  566. printk(KERN_ERR PFX "PST out of sequence\n");
  567. return -EINVAL;
  568. }
  569. }
  570. if (data->numps < 2) {
  571. printk(KERN_ERR PFX "no p states to transition\n");
  572. return -ENODEV;
  573. }
  574. if (check_pst_table(data, pst, maxvid))
  575. return -EINVAL;
  576. powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
  577. * (data->numps + 1)), GFP_KERNEL);
  578. if (!powernow_table) {
  579. printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
  580. return -ENOMEM;
  581. }
  582. for (j = 0; j < data->numps; j++) {
  583. int freq;
  584. powernow_table[j].index = pst[j].fid; /* lower 8 bits */
  585. powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
  586. freq = find_khz_freq_from_fid(pst[j].fid);
  587. powernow_table[j].frequency = freq;
  588. }
  589. powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
  590. powernow_table[data->numps].index = 0;
  591. if (query_current_values_with_pending_wait(data)) {
  592. kfree(powernow_table);
  593. return -EIO;
  594. }
  595. dprintk("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
  596. data->powernow_table = powernow_table;
  597. if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
  598. print_basics(data);
  599. for (j = 0; j < data->numps; j++)
  600. if ((pst[j].fid == data->currfid) &&
  601. (pst[j].vid == data->currvid))
  602. return 0;
  603. dprintk("currfid/vid do not match PST, ignoring\n");
  604. return 0;
  605. }
  606. /* Find and validate the PSB/PST table in BIOS. */
  607. static int find_psb_table(struct powernow_k8_data *data)
  608. {
  609. struct psb_s *psb;
  610. unsigned int i;
  611. u32 mvs;
  612. u8 maxvid;
  613. u32 cpst = 0;
  614. u32 thiscpuid;
  615. for (i = 0xc0000; i < 0xffff0; i += 0x10) {
  616. /* Scan BIOS looking for the signature. */
  617. /* It can not be at ffff0 - it is too big. */
  618. psb = phys_to_virt(i);
  619. if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
  620. continue;
  621. dprintk("found PSB header at 0x%p\n", psb);
  622. dprintk("table vers: 0x%x\n", psb->tableversion);
  623. if (psb->tableversion != PSB_VERSION_1_4) {
  624. printk(KERN_ERR FW_BUG PFX "PSB table is not v1.4\n");
  625. return -ENODEV;
  626. }
  627. dprintk("flags: 0x%x\n", psb->flags1);
  628. if (psb->flags1) {
  629. printk(KERN_ERR FW_BUG PFX "unknown flags\n");
  630. return -ENODEV;
  631. }
  632. data->vstable = psb->vstable;
  633. dprintk("voltage stabilization time: %d(*20us)\n",
  634. data->vstable);
  635. dprintk("flags2: 0x%x\n", psb->flags2);
  636. data->rvo = psb->flags2 & 3;
  637. data->irt = ((psb->flags2) >> 2) & 3;
  638. mvs = ((psb->flags2) >> 4) & 3;
  639. data->vidmvs = 1 << mvs;
  640. data->batps = ((psb->flags2) >> 6) & 3;
  641. dprintk("ramp voltage offset: %d\n", data->rvo);
  642. dprintk("isochronous relief time: %d\n", data->irt);
  643. dprintk("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
  644. dprintk("numpst: 0x%x\n", psb->num_tables);
  645. cpst = psb->num_tables;
  646. if ((psb->cpuid == 0x00000fc0) ||
  647. (psb->cpuid == 0x00000fe0)) {
  648. thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
  649. if ((thiscpuid == 0x00000fc0) ||
  650. (thiscpuid == 0x00000fe0))
  651. cpst = 1;
  652. }
  653. if (cpst != 1) {
  654. printk(KERN_ERR FW_BUG PFX "numpst must be 1\n");
  655. return -ENODEV;
  656. }
  657. data->plllock = psb->plllocktime;
  658. dprintk("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
  659. dprintk("maxfid: 0x%x\n", psb->maxfid);
  660. dprintk("maxvid: 0x%x\n", psb->maxvid);
  661. maxvid = psb->maxvid;
  662. data->numps = psb->numps;
  663. dprintk("numpstates: 0x%x\n", data->numps);
  664. return fill_powernow_table(data,
  665. (struct pst_s *)(psb+1), maxvid);
  666. }
  667. /*
  668. * If you see this message, complain to BIOS manufacturer. If
  669. * he tells you "we do not support Linux" or some similar
  670. * nonsense, remember that Windows 2000 uses the same legacy
  671. * mechanism that the old Linux PSB driver uses. Tell them it
  672. * is broken with Windows 2000.
  673. *
  674. * The reference to the AMD documentation is chapter 9 in the
  675. * BIOS and Kernel Developer's Guide, which is available on
  676. * www.amd.com
  677. */
  678. printk(KERN_ERR FW_BUG PFX "No PSB or ACPI _PSS objects\n");
  679. return -ENODEV;
  680. }
  681. static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data,
  682. unsigned int index)
  683. {
  684. u64 control;
  685. if (!data->acpi_data.state_count || (cpu_family == CPU_HW_PSTATE))
  686. return;
  687. control = data->acpi_data.states[index].control;
  688. data->irt = (control >> IRT_SHIFT) & IRT_MASK;
  689. data->rvo = (control >> RVO_SHIFT) & RVO_MASK;
  690. data->exttype = (control >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
  691. data->plllock = (control >> PLL_L_SHIFT) & PLL_L_MASK;
  692. data->vidmvs = 1 << ((control >> MVS_SHIFT) & MVS_MASK);
  693. data->vstable = (control >> VST_SHIFT) & VST_MASK;
  694. }
  695. static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
  696. {
  697. struct cpufreq_frequency_table *powernow_table;
  698. int ret_val = -ENODEV;
  699. u64 control, status;
  700. if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
  701. dprintk("register performance failed: bad ACPI data\n");
  702. return -EIO;
  703. }
  704. /* verify the data contained in the ACPI structures */
  705. if (data->acpi_data.state_count <= 1) {
  706. dprintk("No ACPI P-States\n");
  707. goto err_out;
  708. }
  709. control = data->acpi_data.control_register.space_id;
  710. status = data->acpi_data.status_register.space_id;
  711. if ((control != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
  712. (status != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
  713. dprintk("Invalid control/status registers (%x - %x)\n",
  714. control, status);
  715. goto err_out;
  716. }
  717. /* fill in data->powernow_table */
  718. powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
  719. * (data->acpi_data.state_count + 1)), GFP_KERNEL);
  720. if (!powernow_table) {
  721. dprintk("powernow_table memory alloc failure\n");
  722. goto err_out;
  723. }
  724. /* fill in data */
  725. data->numps = data->acpi_data.state_count;
  726. powernow_k8_acpi_pst_values(data, 0);
  727. if (cpu_family == CPU_HW_PSTATE)
  728. ret_val = fill_powernow_table_pstate(data, powernow_table);
  729. else
  730. ret_val = fill_powernow_table_fidvid(data, powernow_table);
  731. if (ret_val)
  732. goto err_out_mem;
  733. powernow_table[data->acpi_data.state_count].frequency =
  734. CPUFREQ_TABLE_END;
  735. powernow_table[data->acpi_data.state_count].index = 0;
  736. data->powernow_table = powernow_table;
  737. if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
  738. print_basics(data);
  739. /* notify BIOS that we exist */
  740. acpi_processor_notify_smm(THIS_MODULE);
  741. if (!zalloc_cpumask_var(&data->acpi_data.shared_cpu_map, GFP_KERNEL)) {
  742. printk(KERN_ERR PFX
  743. "unable to alloc powernow_k8_data cpumask\n");
  744. ret_val = -ENOMEM;
  745. goto err_out_mem;
  746. }
  747. return 0;
  748. err_out_mem:
  749. kfree(powernow_table);
  750. err_out:
  751. acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
  752. /* data->acpi_data.state_count informs us at ->exit()
  753. * whether ACPI was used */
  754. data->acpi_data.state_count = 0;
  755. return ret_val;
  756. }
  757. static int fill_powernow_table_pstate(struct powernow_k8_data *data,
  758. struct cpufreq_frequency_table *powernow_table)
  759. {
  760. int i;
  761. u32 hi = 0, lo = 0;
  762. rdmsr(MSR_PSTATE_CUR_LIMIT, hi, lo);
  763. data->max_hw_pstate = (hi & HW_PSTATE_MAX_MASK) >> HW_PSTATE_MAX_SHIFT;
  764. for (i = 0; i < data->acpi_data.state_count; i++) {
  765. u32 index;
  766. index = data->acpi_data.states[i].control & HW_PSTATE_MASK;
  767. if (index > data->max_hw_pstate) {
  768. printk(KERN_ERR PFX "invalid pstate %d - "
  769. "bad value %d.\n", i, index);
  770. printk(KERN_ERR PFX "Please report to BIOS "
  771. "manufacturer\n");
  772. invalidate_entry(powernow_table, i);
  773. continue;
  774. }
  775. rdmsr(MSR_PSTATE_DEF_BASE + index, lo, hi);
  776. if (!(hi & HW_PSTATE_VALID_MASK)) {
  777. dprintk("invalid pstate %d, ignoring\n", index);
  778. invalidate_entry(powernow_table, i);
  779. continue;
  780. }
  781. powernow_table[i].index = index;
  782. /* Frequency may be rounded for these */
  783. if (boot_cpu_data.x86 == 0x10 || boot_cpu_data.x86 == 0x11) {
  784. powernow_table[i].frequency =
  785. freq_from_fid_did(lo & 0x3f, (lo >> 6) & 7);
  786. } else
  787. powernow_table[i].frequency =
  788. data->acpi_data.states[i].core_frequency * 1000;
  789. }
  790. return 0;
  791. }
  792. static int fill_powernow_table_fidvid(struct powernow_k8_data *data,
  793. struct cpufreq_frequency_table *powernow_table)
  794. {
  795. int i;
  796. for (i = 0; i < data->acpi_data.state_count; i++) {
  797. u32 fid;
  798. u32 vid;
  799. u32 freq, index;
  800. u64 status, control;
  801. if (data->exttype) {
  802. status = data->acpi_data.states[i].status;
  803. fid = status & EXT_FID_MASK;
  804. vid = (status >> VID_SHIFT) & EXT_VID_MASK;
  805. } else {
  806. control = data->acpi_data.states[i].control;
  807. fid = control & FID_MASK;
  808. vid = (control >> VID_SHIFT) & VID_MASK;
  809. }
  810. dprintk(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
  811. index = fid | (vid<<8);
  812. powernow_table[i].index = index;
  813. freq = find_khz_freq_from_fid(fid);
  814. powernow_table[i].frequency = freq;
  815. /* verify frequency is OK */
  816. if ((freq > (MAX_FREQ * 1000)) || (freq < (MIN_FREQ * 1000))) {
  817. dprintk("invalid freq %u kHz, ignoring\n", freq);
  818. invalidate_entry(powernow_table, i);
  819. continue;
  820. }
  821. /* verify voltage is OK -
  822. * BIOSs are using "off" to indicate invalid */
  823. if (vid == VID_OFF) {
  824. dprintk("invalid vid %u, ignoring\n", vid);
  825. invalidate_entry(powernow_table, i);
  826. continue;
  827. }
  828. if (freq != (data->acpi_data.states[i].core_frequency * 1000)) {
  829. printk(KERN_INFO PFX "invalid freq entries "
  830. "%u kHz vs. %u kHz\n", freq,
  831. (unsigned int)
  832. (data->acpi_data.states[i].core_frequency
  833. * 1000));
  834. invalidate_entry(powernow_table, i);
  835. continue;
  836. }
  837. }
  838. return 0;
  839. }
  840. static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
  841. {
  842. if (data->acpi_data.state_count)
  843. acpi_processor_unregister_performance(&data->acpi_data,
  844. data->cpu);
  845. free_cpumask_var(data->acpi_data.shared_cpu_map);
  846. }
  847. static int get_transition_latency(struct powernow_k8_data *data)
  848. {
  849. int max_latency = 0;
  850. int i;
  851. for (i = 0; i < data->acpi_data.state_count; i++) {
  852. int cur_latency = data->acpi_data.states[i].transition_latency
  853. + data->acpi_data.states[i].bus_master_latency;
  854. if (cur_latency > max_latency)
  855. max_latency = cur_latency;
  856. }
  857. if (max_latency == 0) {
  858. /*
  859. * Fam 11h always returns 0 as transition latency.
  860. * This is intended and means "very fast". While cpufreq core
  861. * and governors currently can handle that gracefully, better
  862. * set it to 1 to avoid problems in the future.
  863. * For all others it's a BIOS bug.
  864. */
  865. if (boot_cpu_data.x86 != 0x11)
  866. printk(KERN_ERR FW_WARN PFX "Invalid zero transition "
  867. "latency\n");
  868. max_latency = 1;
  869. }
  870. /* value in usecs, needs to be in nanoseconds */
  871. return 1000 * max_latency;
  872. }
  873. /* Take a frequency, and issue the fid/vid transition command */
  874. static int transition_frequency_fidvid(struct powernow_k8_data *data,
  875. unsigned int index)
  876. {
  877. u32 fid = 0;
  878. u32 vid = 0;
  879. int res, i;
  880. struct cpufreq_freqs freqs;
  881. dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
  882. /* fid/vid correctness check for k8 */
  883. /* fid are the lower 8 bits of the index we stored into
  884. * the cpufreq frequency table in find_psb_table, vid
  885. * are the upper 8 bits.
  886. */
  887. fid = data->powernow_table[index].index & 0xFF;
  888. vid = (data->powernow_table[index].index & 0xFF00) >> 8;
  889. dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
  890. if (query_current_values_with_pending_wait(data))
  891. return 1;
  892. if ((data->currvid == vid) && (data->currfid == fid)) {
  893. dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
  894. fid, vid);
  895. return 0;
  896. }
  897. dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
  898. smp_processor_id(), fid, vid);
  899. freqs.old = find_khz_freq_from_fid(data->currfid);
  900. freqs.new = find_khz_freq_from_fid(fid);
  901. for_each_cpu(i, data->available_cores) {
  902. freqs.cpu = i;
  903. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  904. }
  905. res = transition_fid_vid(data, fid, vid);
  906. freqs.new = find_khz_freq_from_fid(data->currfid);
  907. for_each_cpu(i, data->available_cores) {
  908. freqs.cpu = i;
  909. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  910. }
  911. return res;
  912. }
  913. /* Take a frequency, and issue the hardware pstate transition command */
  914. static int transition_frequency_pstate(struct powernow_k8_data *data,
  915. unsigned int index)
  916. {
  917. u32 pstate = 0;
  918. int res, i;
  919. struct cpufreq_freqs freqs;
  920. dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
  921. /* get MSR index for hardware pstate transition */
  922. pstate = index & HW_PSTATE_MASK;
  923. if (pstate > data->max_hw_pstate)
  924. return 0;
  925. freqs.old = find_khz_freq_from_pstate(data->powernow_table,
  926. data->currpstate);
  927. freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
  928. for_each_cpu(i, data->available_cores) {
  929. freqs.cpu = i;
  930. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  931. }
  932. res = transition_pstate(data, pstate);
  933. freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
  934. for_each_cpu(i, data->available_cores) {
  935. freqs.cpu = i;
  936. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  937. }
  938. return res;
  939. }
  940. /* Driver entry point to switch to the target frequency */
  941. static int powernowk8_target(struct cpufreq_policy *pol,
  942. unsigned targfreq, unsigned relation)
  943. {
  944. cpumask_var_t oldmask;
  945. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  946. u32 checkfid;
  947. u32 checkvid;
  948. unsigned int newstate;
  949. int ret = -EIO;
  950. if (!data)
  951. return -EINVAL;
  952. checkfid = data->currfid;
  953. checkvid = data->currvid;
  954. /* only run on specific CPU from here on. */
  955. /* This is poor form: use a workqueue or smp_call_function_single */
  956. if (!alloc_cpumask_var(&oldmask, GFP_KERNEL))
  957. return -ENOMEM;
  958. cpumask_copy(oldmask, tsk_cpus_allowed(current));
  959. set_cpus_allowed_ptr(current, cpumask_of(pol->cpu));
  960. if (smp_processor_id() != pol->cpu) {
  961. printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
  962. goto err_out;
  963. }
  964. if (pending_bit_stuck()) {
  965. printk(KERN_ERR PFX "failing targ, change pending bit set\n");
  966. goto err_out;
  967. }
  968. dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
  969. pol->cpu, targfreq, pol->min, pol->max, relation);
  970. if (query_current_values_with_pending_wait(data))
  971. goto err_out;
  972. if (cpu_family != CPU_HW_PSTATE) {
  973. dprintk("targ: curr fid 0x%x, vid 0x%x\n",
  974. data->currfid, data->currvid);
  975. if ((checkvid != data->currvid) ||
  976. (checkfid != data->currfid)) {
  977. printk(KERN_INFO PFX
  978. "error - out of sync, fix 0x%x 0x%x, "
  979. "vid 0x%x 0x%x\n",
  980. checkfid, data->currfid,
  981. checkvid, data->currvid);
  982. }
  983. }
  984. if (cpufreq_frequency_table_target(pol, data->powernow_table,
  985. targfreq, relation, &newstate))
  986. goto err_out;
  987. mutex_lock(&fidvid_mutex);
  988. powernow_k8_acpi_pst_values(data, newstate);
  989. if (cpu_family == CPU_HW_PSTATE)
  990. ret = transition_frequency_pstate(data, newstate);
  991. else
  992. ret = transition_frequency_fidvid(data, newstate);
  993. if (ret) {
  994. printk(KERN_ERR PFX "transition frequency failed\n");
  995. ret = 1;
  996. mutex_unlock(&fidvid_mutex);
  997. goto err_out;
  998. }
  999. mutex_unlock(&fidvid_mutex);
  1000. if (cpu_family == CPU_HW_PSTATE)
  1001. pol->cur = find_khz_freq_from_pstate(data->powernow_table,
  1002. newstate);
  1003. else
  1004. pol->cur = find_khz_freq_from_fid(data->currfid);
  1005. ret = 0;
  1006. err_out:
  1007. set_cpus_allowed_ptr(current, oldmask);
  1008. free_cpumask_var(oldmask);
  1009. return ret;
  1010. }
  1011. /* Driver entry point to verify the policy and range of frequencies */
  1012. static int powernowk8_verify(struct cpufreq_policy *pol)
  1013. {
  1014. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  1015. if (!data)
  1016. return -EINVAL;
  1017. return cpufreq_frequency_table_verify(pol, data->powernow_table);
  1018. }
  1019. struct init_on_cpu {
  1020. struct powernow_k8_data *data;
  1021. int rc;
  1022. };
  1023. static void __cpuinit powernowk8_cpu_init_on_cpu(void *_init_on_cpu)
  1024. {
  1025. struct init_on_cpu *init_on_cpu = _init_on_cpu;
  1026. if (pending_bit_stuck()) {
  1027. printk(KERN_ERR PFX "failing init, change pending bit set\n");
  1028. init_on_cpu->rc = -ENODEV;
  1029. return;
  1030. }
  1031. if (query_current_values_with_pending_wait(init_on_cpu->data)) {
  1032. init_on_cpu->rc = -ENODEV;
  1033. return;
  1034. }
  1035. if (cpu_family == CPU_OPTERON)
  1036. fidvid_msr_init();
  1037. init_on_cpu->rc = 0;
  1038. }
  1039. /* per CPU init entry point to the driver */
  1040. static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
  1041. {
  1042. static const char ACPI_PSS_BIOS_BUG_MSG[] =
  1043. KERN_ERR FW_BUG PFX "No compatible ACPI _PSS objects found.\n"
  1044. FW_BUG PFX "Try again with latest BIOS.\n";
  1045. struct powernow_k8_data *data;
  1046. struct init_on_cpu init_on_cpu;
  1047. int rc;
  1048. if (!cpu_online(pol->cpu))
  1049. return -ENODEV;
  1050. smp_call_function_single(pol->cpu, check_supported_cpu, &rc, 1);
  1051. if (rc)
  1052. return -ENODEV;
  1053. data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
  1054. if (!data) {
  1055. printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
  1056. return -ENOMEM;
  1057. }
  1058. data->cpu = pol->cpu;
  1059. data->currpstate = HW_PSTATE_INVALID;
  1060. if (powernow_k8_cpu_init_acpi(data)) {
  1061. /*
  1062. * Use the PSB BIOS structure. This is only availabe on
  1063. * an UP version, and is deprecated by AMD.
  1064. */
  1065. if (num_online_cpus() != 1) {
  1066. printk_once(ACPI_PSS_BIOS_BUG_MSG);
  1067. goto err_out;
  1068. }
  1069. if (pol->cpu != 0) {
  1070. printk(KERN_ERR FW_BUG PFX "No ACPI _PSS objects for "
  1071. "CPU other than CPU0. Complain to your BIOS "
  1072. "vendor.\n");
  1073. goto err_out;
  1074. }
  1075. rc = find_psb_table(data);
  1076. if (rc)
  1077. goto err_out;
  1078. /* Take a crude guess here.
  1079. * That guess was in microseconds, so multiply with 1000 */
  1080. pol->cpuinfo.transition_latency = (
  1081. ((data->rvo + 8) * data->vstable * VST_UNITS_20US) +
  1082. ((1 << data->irt) * 30)) * 1000;
  1083. } else /* ACPI _PSS objects available */
  1084. pol->cpuinfo.transition_latency = get_transition_latency(data);
  1085. /* only run on specific CPU from here on */
  1086. init_on_cpu.data = data;
  1087. smp_call_function_single(data->cpu, powernowk8_cpu_init_on_cpu,
  1088. &init_on_cpu, 1);
  1089. rc = init_on_cpu.rc;
  1090. if (rc != 0)
  1091. goto err_out_exit_acpi;
  1092. if (cpu_family == CPU_HW_PSTATE)
  1093. cpumask_copy(pol->cpus, cpumask_of(pol->cpu));
  1094. else
  1095. cpumask_copy(pol->cpus, cpu_core_mask(pol->cpu));
  1096. data->available_cores = pol->cpus;
  1097. if (cpu_family == CPU_HW_PSTATE)
  1098. pol->cur = find_khz_freq_from_pstate(data->powernow_table,
  1099. data->currpstate);
  1100. else
  1101. pol->cur = find_khz_freq_from_fid(data->currfid);
  1102. dprintk("policy current frequency %d kHz\n", pol->cur);
  1103. /* min/max the cpu is capable of */
  1104. if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
  1105. printk(KERN_ERR FW_BUG PFX "invalid powernow_table\n");
  1106. powernow_k8_cpu_exit_acpi(data);
  1107. kfree(data->powernow_table);
  1108. kfree(data);
  1109. return -EINVAL;
  1110. }
  1111. cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
  1112. if (cpu_family == CPU_HW_PSTATE)
  1113. dprintk("cpu_init done, current pstate 0x%x\n",
  1114. data->currpstate);
  1115. else
  1116. dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
  1117. data->currfid, data->currvid);
  1118. per_cpu(powernow_data, pol->cpu) = data;
  1119. return 0;
  1120. err_out_exit_acpi:
  1121. powernow_k8_cpu_exit_acpi(data);
  1122. err_out:
  1123. kfree(data);
  1124. return -ENODEV;
  1125. }
  1126. static int __devexit powernowk8_cpu_exit(struct cpufreq_policy *pol)
  1127. {
  1128. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  1129. if (!data)
  1130. return -EINVAL;
  1131. powernow_k8_cpu_exit_acpi(data);
  1132. cpufreq_frequency_table_put_attr(pol->cpu);
  1133. kfree(data->powernow_table);
  1134. kfree(data);
  1135. per_cpu(powernow_data, pol->cpu) = NULL;
  1136. return 0;
  1137. }
  1138. static void query_values_on_cpu(void *_err)
  1139. {
  1140. int *err = _err;
  1141. struct powernow_k8_data *data = __get_cpu_var(powernow_data);
  1142. *err = query_current_values_with_pending_wait(data);
  1143. }
  1144. static unsigned int powernowk8_get(unsigned int cpu)
  1145. {
  1146. struct powernow_k8_data *data = per_cpu(powernow_data, cpu);
  1147. unsigned int khz = 0;
  1148. int err;
  1149. if (!data)
  1150. return 0;
  1151. smp_call_function_single(cpu, query_values_on_cpu, &err, true);
  1152. if (err)
  1153. goto out;
  1154. if (cpu_family == CPU_HW_PSTATE)
  1155. khz = find_khz_freq_from_pstate(data->powernow_table,
  1156. data->currpstate);
  1157. else
  1158. khz = find_khz_freq_from_fid(data->currfid);
  1159. out:
  1160. return khz;
  1161. }
  1162. static void _cpb_toggle_msrs(bool t)
  1163. {
  1164. int cpu;
  1165. get_online_cpus();
  1166. rdmsr_on_cpus(cpu_online_mask, MSR_K7_HWCR, msrs);
  1167. for_each_cpu(cpu, cpu_online_mask) {
  1168. struct msr *reg = per_cpu_ptr(msrs, cpu);
  1169. if (t)
  1170. reg->l &= ~BIT(25);
  1171. else
  1172. reg->l |= BIT(25);
  1173. }
  1174. wrmsr_on_cpus(cpu_online_mask, MSR_K7_HWCR, msrs);
  1175. put_online_cpus();
  1176. }
  1177. /*
  1178. * Switch on/off core performance boosting.
  1179. *
  1180. * 0=disable
  1181. * 1=enable.
  1182. */
  1183. static void cpb_toggle(bool t)
  1184. {
  1185. if (!cpb_capable)
  1186. return;
  1187. if (t && !cpb_enabled) {
  1188. cpb_enabled = true;
  1189. _cpb_toggle_msrs(t);
  1190. printk(KERN_INFO PFX "Core Boosting enabled.\n");
  1191. } else if (!t && cpb_enabled) {
  1192. cpb_enabled = false;
  1193. _cpb_toggle_msrs(t);
  1194. printk(KERN_INFO PFX "Core Boosting disabled.\n");
  1195. }
  1196. }
  1197. static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf,
  1198. size_t count)
  1199. {
  1200. int ret = -EINVAL;
  1201. unsigned long val = 0;
  1202. ret = strict_strtoul(buf, 10, &val);
  1203. if (!ret && (val == 0 || val == 1) && cpb_capable)
  1204. cpb_toggle(val);
  1205. else
  1206. return -EINVAL;
  1207. return count;
  1208. }
  1209. static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf)
  1210. {
  1211. return sprintf(buf, "%u\n", cpb_enabled);
  1212. }
  1213. #define define_one_rw(_name) \
  1214. static struct freq_attr _name = \
  1215. __ATTR(_name, 0644, show_##_name, store_##_name)
  1216. define_one_rw(cpb);
  1217. static struct freq_attr *powernow_k8_attr[] = {
  1218. &cpufreq_freq_attr_scaling_available_freqs,
  1219. &cpb,
  1220. NULL,
  1221. };
  1222. static struct cpufreq_driver cpufreq_amd64_driver = {
  1223. .verify = powernowk8_verify,
  1224. .target = powernowk8_target,
  1225. .bios_limit = acpi_processor_get_bios_limit,
  1226. .init = powernowk8_cpu_init,
  1227. .exit = __devexit_p(powernowk8_cpu_exit),
  1228. .get = powernowk8_get,
  1229. .name = "powernow-k8",
  1230. .owner = THIS_MODULE,
  1231. .attr = powernow_k8_attr,
  1232. };
  1233. /*
  1234. * Clear the boost-disable flag on the CPU_DOWN path so that this cpu
  1235. * cannot block the remaining ones from boosting. On the CPU_UP path we
  1236. * simply keep the boost-disable flag in sync with the current global
  1237. * state.
  1238. */
  1239. static int __cpuinit cpb_notify(struct notifier_block *nb, unsigned long action,
  1240. void *hcpu)
  1241. {
  1242. unsigned cpu = (long)hcpu;
  1243. u32 lo, hi;
  1244. switch (action) {
  1245. case CPU_UP_PREPARE:
  1246. case CPU_UP_PREPARE_FROZEN:
  1247. if (!cpb_enabled) {
  1248. rdmsr_on_cpu(cpu, MSR_K7_HWCR, &lo, &hi);
  1249. lo |= BIT(25);
  1250. wrmsr_on_cpu(cpu, MSR_K7_HWCR, lo, hi);
  1251. }
  1252. break;
  1253. case CPU_DOWN_PREPARE:
  1254. case CPU_DOWN_PREPARE_FROZEN:
  1255. rdmsr_on_cpu(cpu, MSR_K7_HWCR, &lo, &hi);
  1256. lo &= ~BIT(25);
  1257. wrmsr_on_cpu(cpu, MSR_K7_HWCR, lo, hi);
  1258. break;
  1259. default:
  1260. break;
  1261. }
  1262. return NOTIFY_OK;
  1263. }
  1264. static struct notifier_block __cpuinitdata cpb_nb = {
  1265. .notifier_call = cpb_notify,
  1266. };
  1267. /* driver entry point for init */
  1268. static int __cpuinit powernowk8_init(void)
  1269. {
  1270. unsigned int i, supported_cpus = 0, cpu;
  1271. for_each_online_cpu(i) {
  1272. int rc;
  1273. smp_call_function_single(i, check_supported_cpu, &rc, 1);
  1274. if (rc == 0)
  1275. supported_cpus++;
  1276. }
  1277. if (supported_cpus != num_online_cpus())
  1278. return -ENODEV;
  1279. printk(KERN_INFO PFX "Found %d %s (%d cpu cores) (" VERSION ")\n",
  1280. num_online_nodes(), boot_cpu_data.x86_model_id, supported_cpus);
  1281. if (boot_cpu_has(X86_FEATURE_CPB)) {
  1282. cpb_capable = true;
  1283. register_cpu_notifier(&cpb_nb);
  1284. msrs = msrs_alloc();
  1285. if (!msrs) {
  1286. printk(KERN_ERR "%s: Error allocating msrs!\n", __func__);
  1287. return -ENOMEM;
  1288. }
  1289. rdmsr_on_cpus(cpu_online_mask, MSR_K7_HWCR, msrs);
  1290. for_each_cpu(cpu, cpu_online_mask) {
  1291. struct msr *reg = per_cpu_ptr(msrs, cpu);
  1292. cpb_enabled |= !(!!(reg->l & BIT(25)));
  1293. }
  1294. printk(KERN_INFO PFX "Core Performance Boosting: %s.\n",
  1295. (cpb_enabled ? "on" : "off"));
  1296. }
  1297. return cpufreq_register_driver(&cpufreq_amd64_driver);
  1298. }
  1299. /* driver entry point for term */
  1300. static void __exit powernowk8_exit(void)
  1301. {
  1302. dprintk("exit\n");
  1303. if (boot_cpu_has(X86_FEATURE_CPB)) {
  1304. msrs_free(msrs);
  1305. msrs = NULL;
  1306. unregister_cpu_notifier(&cpb_nb);
  1307. }
  1308. cpufreq_unregister_driver(&cpufreq_amd64_driver);
  1309. }
  1310. MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and "
  1311. "Mark Langsdorf <mark.langsdorf@amd.com>");
  1312. MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
  1313. MODULE_LICENSE("GPL");
  1314. late_initcall(powernowk8_init);
  1315. module_exit(powernowk8_exit);