powernow-k8.c 36 KB

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