powernow-k8.c 37 KB

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