powernow-k8.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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; data->irt = (control
  697. >> IRT_SHIFT) & IRT_MASK; data->rvo = (control >>
  698. RVO_SHIFT) & RVO_MASK; data->exttype = (control
  699. >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
  700. data->plllock = (control >> PLL_L_SHIFT) & PLL_L_MASK; data->vidmvs = 1
  701. << ((control >> MVS_SHIFT) & MVS_MASK); data->vstable =
  702. (control >> VST_SHIFT) & VST_MASK; }
  703. static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
  704. {
  705. struct cpufreq_frequency_table *powernow_table;
  706. int ret_val = -ENODEV;
  707. acpi_integer control, status;
  708. if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
  709. dprintk("register performance failed: bad ACPI data\n");
  710. return -EIO;
  711. }
  712. /* verify the data contained in the ACPI structures */
  713. if (data->acpi_data.state_count <= 1) {
  714. dprintk("No ACPI P-States\n");
  715. goto err_out;
  716. }
  717. control = data->acpi_data.control_register.space_id;
  718. status = data->acpi_data.status_register.space_id;
  719. if ((control != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
  720. (status != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
  721. dprintk("Invalid control/status registers (%x - %x)\n",
  722. control, status);
  723. goto err_out;
  724. }
  725. /* fill in data->powernow_table */
  726. powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
  727. * (data->acpi_data.state_count + 1)), GFP_KERNEL);
  728. if (!powernow_table) {
  729. dprintk("powernow_table memory alloc failure\n");
  730. goto err_out;
  731. }
  732. if (cpu_family == CPU_HW_PSTATE)
  733. ret_val = fill_powernow_table_pstate(data, powernow_table);
  734. else
  735. ret_val = fill_powernow_table_fidvid(data, powernow_table);
  736. if (ret_val)
  737. goto err_out_mem;
  738. powernow_table[data->acpi_data.state_count].frequency =
  739. CPUFREQ_TABLE_END;
  740. powernow_table[data->acpi_data.state_count].index = 0;
  741. data->powernow_table = powernow_table;
  742. /* fill in data */
  743. data->numps = data->acpi_data.state_count;
  744. if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
  745. print_basics(data);
  746. powernow_k8_acpi_pst_values(data, 0);
  747. /* notify BIOS that we exist */
  748. acpi_processor_notify_smm(THIS_MODULE);
  749. if (!zalloc_cpumask_var(&data->acpi_data.shared_cpu_map, GFP_KERNEL)) {
  750. printk(KERN_ERR PFX
  751. "unable to alloc powernow_k8_data cpumask\n");
  752. ret_val = -ENOMEM;
  753. goto err_out_mem;
  754. }
  755. return 0;
  756. err_out_mem:
  757. kfree(powernow_table);
  758. err_out:
  759. acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
  760. /* data->acpi_data.state_count informs us at ->exit()
  761. * whether ACPI was used */
  762. data->acpi_data.state_count = 0;
  763. return ret_val;
  764. }
  765. static int fill_powernow_table_pstate(struct powernow_k8_data *data,
  766. struct cpufreq_frequency_table *powernow_table)
  767. {
  768. int i;
  769. u32 hi = 0, lo = 0;
  770. rdmsr(MSR_PSTATE_CUR_LIMIT, hi, lo);
  771. data->max_hw_pstate = (hi & HW_PSTATE_MAX_MASK) >> HW_PSTATE_MAX_SHIFT;
  772. for (i = 0; i < data->acpi_data.state_count; i++) {
  773. u32 index;
  774. index = data->acpi_data.states[i].control & HW_PSTATE_MASK;
  775. if (index > data->max_hw_pstate) {
  776. printk(KERN_ERR PFX "invalid pstate %d - "
  777. "bad value %d.\n", i, index);
  778. printk(KERN_ERR PFX "Please report to BIOS "
  779. "manufacturer\n");
  780. invalidate_entry(data, i);
  781. continue;
  782. }
  783. rdmsr(MSR_PSTATE_DEF_BASE + index, lo, hi);
  784. if (!(hi & HW_PSTATE_VALID_MASK)) {
  785. dprintk("invalid pstate %d, ignoring\n", index);
  786. invalidate_entry(data, i);
  787. continue;
  788. }
  789. powernow_table[i].index = index;
  790. /* Frequency may be rounded for these */
  791. if (boot_cpu_data.x86 == 0x10 || boot_cpu_data.x86 == 0x11) {
  792. powernow_table[i].frequency =
  793. freq_from_fid_did(lo & 0x3f, (lo >> 6) & 7);
  794. } else
  795. powernow_table[i].frequency =
  796. data->acpi_data.states[i].core_frequency * 1000;
  797. }
  798. return 0;
  799. }
  800. static int fill_powernow_table_fidvid(struct powernow_k8_data *data,
  801. struct cpufreq_frequency_table *powernow_table)
  802. {
  803. int i;
  804. int cntlofreq = 0;
  805. for (i = 0; i < data->acpi_data.state_count; i++) {
  806. u32 fid;
  807. u32 vid;
  808. u32 freq, index;
  809. acpi_integer status, control;
  810. if (data->exttype) {
  811. status = data->acpi_data.states[i].status;
  812. fid = status & EXT_FID_MASK;
  813. vid = (status >> VID_SHIFT) & EXT_VID_MASK;
  814. } else {
  815. control = data->acpi_data.states[i].control;
  816. fid = control & FID_MASK;
  817. vid = (control >> VID_SHIFT) & VID_MASK;
  818. }
  819. dprintk(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
  820. index = fid | (vid<<8);
  821. powernow_table[i].index = index;
  822. freq = find_khz_freq_from_fid(fid);
  823. powernow_table[i].frequency = freq;
  824. /* verify frequency is OK */
  825. if ((freq > (MAX_FREQ * 1000)) || (freq < (MIN_FREQ * 1000))) {
  826. dprintk("invalid freq %u kHz, ignoring\n", freq);
  827. invalidate_entry(data, i);
  828. continue;
  829. }
  830. /* verify voltage is OK -
  831. * BIOSs are using "off" to indicate invalid */
  832. if (vid == VID_OFF) {
  833. dprintk("invalid vid %u, ignoring\n", vid);
  834. invalidate_entry(data, i);
  835. continue;
  836. }
  837. /* verify only 1 entry from the lo frequency table */
  838. if (fid < HI_FID_TABLE_BOTTOM) {
  839. if (cntlofreq) {
  840. /* if both entries are the same,
  841. * ignore this one ... */
  842. if ((freq != powernow_table[cntlofreq].frequency) ||
  843. (index != powernow_table[cntlofreq].index)) {
  844. printk(KERN_ERR PFX
  845. "Too many lo freq table "
  846. "entries\n");
  847. return 1;
  848. }
  849. dprintk("double low frequency table entry, "
  850. "ignoring it.\n");
  851. invalidate_entry(data, i);
  852. continue;
  853. } else
  854. cntlofreq = i;
  855. }
  856. if (freq != (data->acpi_data.states[i].core_frequency * 1000)) {
  857. printk(KERN_INFO PFX "invalid freq entries "
  858. "%u kHz vs. %u kHz\n", freq,
  859. (unsigned int)
  860. (data->acpi_data.states[i].core_frequency
  861. * 1000));
  862. invalidate_entry(data, i);
  863. continue;
  864. }
  865. }
  866. return 0;
  867. }
  868. static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
  869. {
  870. if (data->acpi_data.state_count)
  871. acpi_processor_unregister_performance(&data->acpi_data,
  872. data->cpu);
  873. free_cpumask_var(data->acpi_data.shared_cpu_map);
  874. }
  875. static int get_transition_latency(struct powernow_k8_data *data)
  876. {
  877. int max_latency = 0;
  878. int i;
  879. for (i = 0; i < data->acpi_data.state_count; i++) {
  880. int cur_latency = data->acpi_data.states[i].transition_latency
  881. + data->acpi_data.states[i].bus_master_latency;
  882. if (cur_latency > max_latency)
  883. max_latency = cur_latency;
  884. }
  885. /* value in usecs, needs to be in nanoseconds */
  886. return 1000 * max_latency;
  887. }
  888. /* Take a frequency, and issue the fid/vid transition command */
  889. static int transition_frequency_fidvid(struct powernow_k8_data *data,
  890. unsigned int index)
  891. {
  892. u32 fid = 0;
  893. u32 vid = 0;
  894. int res, i;
  895. struct cpufreq_freqs freqs;
  896. dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
  897. /* fid/vid correctness check for k8 */
  898. /* fid are the lower 8 bits of the index we stored into
  899. * the cpufreq frequency table in find_psb_table, vid
  900. * are the upper 8 bits.
  901. */
  902. fid = data->powernow_table[index].index & 0xFF;
  903. vid = (data->powernow_table[index].index & 0xFF00) >> 8;
  904. dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
  905. if (query_current_values_with_pending_wait(data))
  906. return 1;
  907. if ((data->currvid == vid) && (data->currfid == fid)) {
  908. dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
  909. fid, vid);
  910. return 0;
  911. }
  912. if ((fid < HI_FID_TABLE_BOTTOM) &&
  913. (data->currfid < HI_FID_TABLE_BOTTOM)) {
  914. printk(KERN_ERR PFX
  915. "ignoring illegal change in lo freq table-%x to 0x%x\n",
  916. data->currfid, fid);
  917. return 1;
  918. }
  919. dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
  920. smp_processor_id(), fid, vid);
  921. freqs.old = find_khz_freq_from_fid(data->currfid);
  922. freqs.new = find_khz_freq_from_fid(fid);
  923. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  924. freqs.cpu = i;
  925. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  926. }
  927. res = transition_fid_vid(data, fid, vid);
  928. freqs.new = find_khz_freq_from_fid(data->currfid);
  929. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  930. freqs.cpu = i;
  931. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  932. }
  933. return res;
  934. }
  935. /* Take a frequency, and issue the hardware pstate transition command */
  936. static int transition_frequency_pstate(struct powernow_k8_data *data,
  937. unsigned int index)
  938. {
  939. u32 pstate = 0;
  940. int res, i;
  941. struct cpufreq_freqs freqs;
  942. dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
  943. /* get MSR index for hardware pstate transition */
  944. pstate = index & HW_PSTATE_MASK;
  945. if (pstate > data->max_hw_pstate)
  946. return 0;
  947. freqs.old = find_khz_freq_from_pstate(data->powernow_table,
  948. data->currpstate);
  949. freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
  950. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  951. freqs.cpu = i;
  952. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  953. }
  954. res = transition_pstate(data, pstate);
  955. freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
  956. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  957. freqs.cpu = i;
  958. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  959. }
  960. return res;
  961. }
  962. /* Driver entry point to switch to the target frequency */
  963. static int powernowk8_target(struct cpufreq_policy *pol,
  964. unsigned targfreq, unsigned relation)
  965. {
  966. cpumask_t oldmask;
  967. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  968. u32 checkfid;
  969. u32 checkvid;
  970. unsigned int newstate;
  971. int ret = -EIO;
  972. if (!data)
  973. return -EINVAL;
  974. checkfid = data->currfid;
  975. checkvid = data->currvid;
  976. /* only run on specific CPU from here on */
  977. oldmask = current->cpus_allowed;
  978. set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
  979. if (smp_processor_id() != pol->cpu) {
  980. printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
  981. goto err_out;
  982. }
  983. if (pending_bit_stuck()) {
  984. printk(KERN_ERR PFX "failing targ, change pending bit set\n");
  985. goto err_out;
  986. }
  987. dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
  988. pol->cpu, targfreq, pol->min, pol->max, relation);
  989. if (query_current_values_with_pending_wait(data))
  990. goto err_out;
  991. if (cpu_family != CPU_HW_PSTATE) {
  992. dprintk("targ: curr fid 0x%x, vid 0x%x\n",
  993. data->currfid, data->currvid);
  994. if ((checkvid != data->currvid) ||
  995. (checkfid != data->currfid)) {
  996. printk(KERN_INFO PFX
  997. "error - out of sync, fix 0x%x 0x%x, "
  998. "vid 0x%x 0x%x\n",
  999. checkfid, data->currfid,
  1000. checkvid, data->currvid);
  1001. }
  1002. }
  1003. if (cpufreq_frequency_table_target(pol, data->powernow_table,
  1004. targfreq, relation, &newstate))
  1005. goto err_out;
  1006. mutex_lock(&fidvid_mutex);
  1007. powernow_k8_acpi_pst_values(data, newstate);
  1008. if (cpu_family == CPU_HW_PSTATE)
  1009. ret = transition_frequency_pstate(data, newstate);
  1010. else
  1011. ret = transition_frequency_fidvid(data, newstate);
  1012. if (ret) {
  1013. printk(KERN_ERR PFX "transition frequency failed\n");
  1014. ret = 1;
  1015. mutex_unlock(&fidvid_mutex);
  1016. goto err_out;
  1017. }
  1018. mutex_unlock(&fidvid_mutex);
  1019. if (cpu_family == CPU_HW_PSTATE)
  1020. pol->cur = find_khz_freq_from_pstate(data->powernow_table,
  1021. newstate);
  1022. else
  1023. pol->cur = find_khz_freq_from_fid(data->currfid);
  1024. ret = 0;
  1025. err_out:
  1026. set_cpus_allowed_ptr(current, &oldmask);
  1027. return ret;
  1028. }
  1029. /* Driver entry point to verify the policy and range of frequencies */
  1030. static int powernowk8_verify(struct cpufreq_policy *pol)
  1031. {
  1032. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  1033. if (!data)
  1034. return -EINVAL;
  1035. return cpufreq_frequency_table_verify(pol, data->powernow_table);
  1036. }
  1037. static const char ACPI_PSS_BIOS_BUG_MSG[] =
  1038. KERN_ERR FW_BUG PFX "No compatible ACPI _PSS objects found.\n"
  1039. KERN_ERR FW_BUG PFX "Try again with latest BIOS.\n";
  1040. /* per CPU init entry point to the driver */
  1041. static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
  1042. {
  1043. struct powernow_k8_data *data;
  1044. cpumask_t oldmask;
  1045. int rc;
  1046. if (!cpu_online(pol->cpu))
  1047. return -ENODEV;
  1048. if (!check_supported_cpu(pol->cpu))
  1049. return -ENODEV;
  1050. data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
  1051. if (!data) {
  1052. printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
  1053. return -ENOMEM;
  1054. }
  1055. data->cpu = pol->cpu;
  1056. data->currpstate = HW_PSTATE_INVALID;
  1057. if (powernow_k8_cpu_init_acpi(data)) {
  1058. /*
  1059. * Use the PSB BIOS structure. This is only availabe on
  1060. * an UP version, and is deprecated by AMD.
  1061. */
  1062. if (num_online_cpus() != 1) {
  1063. printk_once(ACPI_PSS_BIOS_BUG_MSG);
  1064. goto err_out;
  1065. }
  1066. if (pol->cpu != 0) {
  1067. printk(KERN_ERR FW_BUG PFX "No ACPI _PSS objects for "
  1068. "CPU other than CPU0. Complain to your BIOS "
  1069. "vendor.\n");
  1070. goto err_out;
  1071. }
  1072. rc = find_psb_table(data);
  1073. if (rc)
  1074. goto err_out;
  1075. /* Take a crude guess here.
  1076. * That guess was in microseconds, so multiply with 1000 */
  1077. pol->cpuinfo.transition_latency = (
  1078. ((data->rvo + 8) * data->vstable * VST_UNITS_20US) +
  1079. ((1 << data->irt) * 30)) * 1000;
  1080. } else /* ACPI _PSS objects available */
  1081. pol->cpuinfo.transition_latency = get_transition_latency(data);
  1082. /* only run on specific CPU from here on */
  1083. oldmask = current->cpus_allowed;
  1084. set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
  1085. if (smp_processor_id() != pol->cpu) {
  1086. printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
  1087. goto err_out_unmask;
  1088. }
  1089. if (pending_bit_stuck()) {
  1090. printk(KERN_ERR PFX "failing init, change pending bit set\n");
  1091. goto err_out_unmask;
  1092. }
  1093. if (query_current_values_with_pending_wait(data))
  1094. goto err_out_unmask;
  1095. if (cpu_family == CPU_OPTERON)
  1096. fidvid_msr_init();
  1097. /* run on any CPU again */
  1098. set_cpus_allowed_ptr(current, &oldmask);
  1099. if (cpu_family == CPU_HW_PSTATE)
  1100. cpumask_copy(pol->cpus, cpumask_of(pol->cpu));
  1101. else
  1102. cpumask_copy(pol->cpus, cpu_core_mask(pol->cpu));
  1103. data->available_cores = pol->cpus;
  1104. if (cpu_family == CPU_HW_PSTATE)
  1105. pol->cur = find_khz_freq_from_pstate(data->powernow_table,
  1106. data->currpstate);
  1107. else
  1108. pol->cur = find_khz_freq_from_fid(data->currfid);
  1109. dprintk("policy current frequency %d kHz\n", pol->cur);
  1110. /* min/max the cpu is capable of */
  1111. if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
  1112. printk(KERN_ERR FW_BUG PFX "invalid powernow_table\n");
  1113. powernow_k8_cpu_exit_acpi(data);
  1114. kfree(data->powernow_table);
  1115. kfree(data);
  1116. return -EINVAL;
  1117. }
  1118. cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
  1119. if (cpu_family == CPU_HW_PSTATE)
  1120. dprintk("cpu_init done, current pstate 0x%x\n",
  1121. data->currpstate);
  1122. else
  1123. dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
  1124. data->currfid, data->currvid);
  1125. per_cpu(powernow_data, pol->cpu) = data;
  1126. return 0;
  1127. err_out_unmask:
  1128. set_cpus_allowed_ptr(current, &oldmask);
  1129. powernow_k8_cpu_exit_acpi(data);
  1130. err_out:
  1131. kfree(data);
  1132. return -ENODEV;
  1133. }
  1134. static int __devexit powernowk8_cpu_exit(struct cpufreq_policy *pol)
  1135. {
  1136. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  1137. if (!data)
  1138. return -EINVAL;
  1139. powernow_k8_cpu_exit_acpi(data);
  1140. cpufreq_frequency_table_put_attr(pol->cpu);
  1141. kfree(data->powernow_table);
  1142. kfree(data);
  1143. return 0;
  1144. }
  1145. static unsigned int powernowk8_get(unsigned int cpu)
  1146. {
  1147. struct powernow_k8_data *data;
  1148. cpumask_t oldmask = current->cpus_allowed;
  1149. unsigned int khz = 0;
  1150. unsigned int first;
  1151. first = cpumask_first(cpu_core_mask(cpu));
  1152. data = per_cpu(powernow_data, first);
  1153. if (!data)
  1154. return -EINVAL;
  1155. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  1156. if (smp_processor_id() != cpu) {
  1157. printk(KERN_ERR PFX
  1158. "limiting to CPU %d failed in powernowk8_get\n", cpu);
  1159. set_cpus_allowed_ptr(current, &oldmask);
  1160. return 0;
  1161. }
  1162. if (query_current_values_with_pending_wait(data))
  1163. goto out;
  1164. if (cpu_family == CPU_HW_PSTATE)
  1165. khz = find_khz_freq_from_pstate(data->powernow_table,
  1166. data->currpstate);
  1167. else
  1168. khz = find_khz_freq_from_fid(data->currfid);
  1169. out:
  1170. set_cpus_allowed_ptr(current, &oldmask);
  1171. return khz;
  1172. }
  1173. static struct freq_attr *powernow_k8_attr[] = {
  1174. &cpufreq_freq_attr_scaling_available_freqs,
  1175. NULL,
  1176. };
  1177. static struct cpufreq_driver cpufreq_amd64_driver = {
  1178. .verify = powernowk8_verify,
  1179. .target = powernowk8_target,
  1180. .init = powernowk8_cpu_init,
  1181. .exit = __devexit_p(powernowk8_cpu_exit),
  1182. .get = powernowk8_get,
  1183. .name = "powernow-k8",
  1184. .owner = THIS_MODULE,
  1185. .attr = powernow_k8_attr,
  1186. };
  1187. /* driver entry point for init */
  1188. static int __cpuinit powernowk8_init(void)
  1189. {
  1190. unsigned int i, supported_cpus = 0;
  1191. for_each_online_cpu(i) {
  1192. if (check_supported_cpu(i))
  1193. supported_cpus++;
  1194. }
  1195. if (supported_cpus == num_online_cpus()) {
  1196. printk(KERN_INFO PFX "Found %d %s "
  1197. "processors (%d cpu cores) (" VERSION ")\n",
  1198. num_online_nodes(),
  1199. boot_cpu_data.x86_model_id, supported_cpus);
  1200. return cpufreq_register_driver(&cpufreq_amd64_driver);
  1201. }
  1202. return -ENODEV;
  1203. }
  1204. /* driver entry point for term */
  1205. static void __exit powernowk8_exit(void)
  1206. {
  1207. dprintk("exit\n");
  1208. cpufreq_unregister_driver(&cpufreq_amd64_driver);
  1209. }
  1210. MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and "
  1211. "Mark Langsdorf <mark.langsdorf@amd.com>");
  1212. MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
  1213. MODULE_LICENSE("GPL");
  1214. late_initcall(powernowk8_init);
  1215. module_exit(powernowk8_exit);