powernow-k8.c 37 KB

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