powernow-k8.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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 struct acpi_processor_performance *acpi_perf_data;
  627. static int preregister_valid;
  628. static int powernow_k8_cpu_preinit_acpi(void)
  629. {
  630. acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
  631. if (!acpi_perf_data)
  632. return -ENODEV;
  633. if (acpi_processor_preregister_performance(acpi_perf_data))
  634. return -ENODEV;
  635. else
  636. preregister_valid = 1;
  637. return 0;
  638. }
  639. static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
  640. {
  641. struct cpufreq_frequency_table *powernow_table;
  642. int ret_val;
  643. int cpu = 0;
  644. data->acpi_data = percpu_ptr(acpi_perf_data, cpu);
  645. if (acpi_processor_register_performance(data->acpi_data, data->cpu)) {
  646. dprintk("register performance failed: bad ACPI data\n");
  647. return -EIO;
  648. }
  649. /* verify the data contained in the ACPI structures */
  650. if (data->acpi_data->state_count <= 1) {
  651. dprintk("No ACPI P-States\n");
  652. goto err_out;
  653. }
  654. if ((data->acpi_data->control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
  655. (data->acpi_data->status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
  656. dprintk("Invalid control/status registers (%x - %x)\n",
  657. data->acpi_data->control_register.space_id,
  658. data->acpi_data->status_register.space_id);
  659. goto err_out;
  660. }
  661. /* fill in data->powernow_table */
  662. powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
  663. * (data->acpi_data->state_count + 1)), GFP_KERNEL);
  664. if (!powernow_table) {
  665. dprintk("powernow_table memory alloc failure\n");
  666. goto err_out;
  667. }
  668. if (cpu_family == CPU_HW_PSTATE)
  669. ret_val = fill_powernow_table_pstate(data, powernow_table);
  670. else
  671. ret_val = fill_powernow_table_fidvid(data, powernow_table);
  672. if (ret_val)
  673. goto err_out_mem;
  674. powernow_table[data->acpi_data->state_count].frequency = CPUFREQ_TABLE_END;
  675. powernow_table[data->acpi_data->state_count].index = 0;
  676. data->powernow_table = powernow_table;
  677. /* fill in data */
  678. data->numps = data->acpi_data->state_count;
  679. if (first_cpu(per_cpu(cpu_core_map, data->cpu)) == data->cpu)
  680. print_basics(data);
  681. powernow_k8_acpi_pst_values(data, 0);
  682. /* notify BIOS that we exist */
  683. acpi_processor_notify_smm(THIS_MODULE);
  684. /* determine affinity, from ACPI if available */
  685. if (preregister_valid) {
  686. if ((data->acpi_data->shared_type == CPUFREQ_SHARED_TYPE_ALL) ||
  687. (data->acpi_data->shared_type == CPUFREQ_SHARED_TYPE_ANY))
  688. data->starting_core_affinity = data->acpi_data->shared_cpu_map;
  689. else
  690. data->starting_core_affinity = cpumask_of_cpu(data->cpu);
  691. } else {
  692. /* best guess from family if not */
  693. if (cpu_family == CPU_HW_PSTATE)
  694. data->starting_core_affinity = cpumask_of_cpu(data->cpu);
  695. else
  696. data->starting_core_affinity = per_cpu(cpu_core_map, data->cpu);
  697. }
  698. return 0;
  699. err_out_mem:
  700. kfree(powernow_table);
  701. err_out:
  702. acpi_processor_unregister_performance(data->acpi_data, data->cpu);
  703. /* data->acpi_data.state_count informs us at ->exit() whether ACPI was used */
  704. data->acpi_data->state_count = 0;
  705. return -ENODEV;
  706. }
  707. static int fill_powernow_table_pstate(struct powernow_k8_data *data, struct cpufreq_frequency_table *powernow_table)
  708. {
  709. int i;
  710. u32 hi = 0, lo = 0;
  711. rdmsr(MSR_PSTATE_CUR_LIMIT, hi, lo);
  712. data->max_hw_pstate = (hi & HW_PSTATE_MAX_MASK) >> HW_PSTATE_MAX_SHIFT;
  713. for (i = 0; i < data->acpi_data->state_count; i++) {
  714. u32 index;
  715. index = data->acpi_data->states[i].control & HW_PSTATE_MASK;
  716. if (index > data->max_hw_pstate) {
  717. printk(KERN_ERR PFX "invalid pstate %d - bad value %d.\n", i, index);
  718. printk(KERN_ERR PFX "Please report to BIOS manufacturer\n");
  719. powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  720. continue;
  721. }
  722. rdmsr(MSR_PSTATE_DEF_BASE + index, lo, hi);
  723. if (!(hi & HW_PSTATE_VALID_MASK)) {
  724. dprintk("invalid pstate %d, ignoring\n", index);
  725. powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  726. continue;
  727. }
  728. powernow_table[i].index = index;
  729. powernow_table[i].frequency = data->acpi_data->states[i].core_frequency * 1000;
  730. }
  731. return 0;
  732. }
  733. static int fill_powernow_table_fidvid(struct powernow_k8_data *data, struct cpufreq_frequency_table *powernow_table)
  734. {
  735. int i;
  736. int cntlofreq = 0;
  737. for (i = 0; i < data->acpi_data->state_count; i++) {
  738. u32 fid;
  739. u32 vid;
  740. if (data->exttype) {
  741. fid = data->acpi_data->states[i].status & EXT_FID_MASK;
  742. vid = (data->acpi_data->states[i].status >> VID_SHIFT) & EXT_VID_MASK;
  743. } else {
  744. fid = data->acpi_data->states[i].control & FID_MASK;
  745. vid = (data->acpi_data->states[i].control >> VID_SHIFT) & VID_MASK;
  746. }
  747. dprintk(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
  748. powernow_table[i].index = fid; /* lower 8 bits */
  749. powernow_table[i].index |= (vid << 8); /* upper 8 bits */
  750. powernow_table[i].frequency = find_khz_freq_from_fid(fid);
  751. /* verify frequency is OK */
  752. if ((powernow_table[i].frequency > (MAX_FREQ * 1000)) ||
  753. (powernow_table[i].frequency < (MIN_FREQ * 1000))) {
  754. dprintk("invalid freq %u kHz, ignoring\n", powernow_table[i].frequency);
  755. powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  756. continue;
  757. }
  758. /* verify voltage is OK - BIOSs are using "off" to indicate invalid */
  759. if (vid == VID_OFF) {
  760. dprintk("invalid vid %u, ignoring\n", vid);
  761. powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  762. continue;
  763. }
  764. /* verify only 1 entry from the lo frequency table */
  765. if (fid < HI_FID_TABLE_BOTTOM) {
  766. if (cntlofreq) {
  767. /* if both entries are the same, ignore this one ... */
  768. if ((powernow_table[i].frequency != powernow_table[cntlofreq].frequency) ||
  769. (powernow_table[i].index != powernow_table[cntlofreq].index)) {
  770. printk(KERN_ERR PFX "Too many lo freq table entries\n");
  771. return 1;
  772. }
  773. dprintk("double low frequency table entry, ignoring it.\n");
  774. powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  775. continue;
  776. } else
  777. cntlofreq = i;
  778. }
  779. if (powernow_table[i].frequency != (data->acpi_data->states[i].core_frequency * 1000)) {
  780. printk(KERN_INFO PFX "invalid freq entries %u kHz vs. %u kHz\n",
  781. powernow_table[i].frequency,
  782. (unsigned int) (data->acpi_data->states[i].core_frequency * 1000));
  783. powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  784. continue;
  785. }
  786. }
  787. return 0;
  788. }
  789. static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
  790. {
  791. if (data->acpi_data->state_count)
  792. acpi_processor_unregister_performance(data->acpi_data, data->cpu);
  793. }
  794. #else
  795. static int powernow_k8_cpu_preinit_acpi(void) { return -ENODEV; }
  796. static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data) { return -ENODEV; }
  797. static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data) { return; }
  798. static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index) { return; }
  799. #endif /* CONFIG_X86_POWERNOW_K8_ACPI */
  800. /* Take a frequency, and issue the fid/vid transition command */
  801. static int transition_frequency_fidvid(struct powernow_k8_data *data, unsigned int index)
  802. {
  803. u32 fid = 0;
  804. u32 vid = 0;
  805. int res, i;
  806. struct cpufreq_freqs freqs;
  807. dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
  808. /* fid/vid correctness check for k8 */
  809. /* fid are the lower 8 bits of the index we stored into
  810. * the cpufreq frequency table in find_psb_table, vid
  811. * are the upper 8 bits.
  812. */
  813. fid = data->powernow_table[index].index & 0xFF;
  814. vid = (data->powernow_table[index].index & 0xFF00) >> 8;
  815. dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
  816. if (query_current_values_with_pending_wait(data))
  817. return 1;
  818. if ((data->currvid == vid) && (data->currfid == fid)) {
  819. dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
  820. fid, vid);
  821. return 0;
  822. }
  823. if ((fid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
  824. printk(KERN_ERR PFX
  825. "ignoring illegal change in lo freq table-%x to 0x%x\n",
  826. data->currfid, fid);
  827. return 1;
  828. }
  829. dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
  830. smp_processor_id(), fid, vid);
  831. freqs.old = find_khz_freq_from_fid(data->currfid);
  832. freqs.new = find_khz_freq_from_fid(fid);
  833. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  834. freqs.cpu = i;
  835. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  836. }
  837. res = transition_fid_vid(data, fid, vid);
  838. freqs.new = find_khz_freq_from_fid(data->currfid);
  839. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  840. freqs.cpu = i;
  841. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  842. }
  843. return res;
  844. }
  845. /* Take a frequency, and issue the hardware pstate transition command */
  846. static int transition_frequency_pstate(struct powernow_k8_data *data, unsigned int index)
  847. {
  848. u32 pstate = 0;
  849. int res, i;
  850. struct cpufreq_freqs freqs;
  851. dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
  852. /* get MSR index for hardware pstate transition */
  853. pstate = index & HW_PSTATE_MASK;
  854. if (pstate > data->max_hw_pstate)
  855. return 0;
  856. freqs.old = find_khz_freq_from_pstate(data->powernow_table, data->currpstate);
  857. freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
  858. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  859. freqs.cpu = i;
  860. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  861. }
  862. res = transition_pstate(data, pstate);
  863. freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
  864. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  865. freqs.cpu = i;
  866. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  867. }
  868. return res;
  869. }
  870. /* Driver entry point to switch to the target frequency */
  871. static int powernowk8_target(struct cpufreq_policy *pol, unsigned targfreq, unsigned relation)
  872. {
  873. cpumask_t oldmask;
  874. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  875. u32 checkfid;
  876. u32 checkvid;
  877. unsigned int newstate;
  878. int ret = -EIO;
  879. if (!data)
  880. return -EINVAL;
  881. checkfid = data->currfid;
  882. checkvid = data->currvid;
  883. /* only run on specific CPU from here on */
  884. oldmask = current->cpus_allowed;
  885. set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
  886. if (smp_processor_id() != pol->cpu) {
  887. printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
  888. goto err_out;
  889. }
  890. if (pending_bit_stuck()) {
  891. printk(KERN_ERR PFX "failing targ, change pending bit set\n");
  892. goto err_out;
  893. }
  894. dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
  895. pol->cpu, targfreq, pol->min, pol->max, relation);
  896. if (query_current_values_with_pending_wait(data))
  897. goto err_out;
  898. if (cpu_family != CPU_HW_PSTATE) {
  899. dprintk("targ: curr fid 0x%x, vid 0x%x\n",
  900. data->currfid, data->currvid);
  901. if ((checkvid != data->currvid) || (checkfid != data->currfid)) {
  902. printk(KERN_INFO PFX
  903. "error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
  904. checkfid, data->currfid, checkvid, data->currvid);
  905. }
  906. }
  907. if (cpufreq_frequency_table_target(pol, data->powernow_table, targfreq, relation, &newstate))
  908. goto err_out;
  909. mutex_lock(&fidvid_mutex);
  910. powernow_k8_acpi_pst_values(data, newstate);
  911. if (cpu_family == CPU_HW_PSTATE)
  912. ret = transition_frequency_pstate(data, newstate);
  913. else
  914. ret = transition_frequency_fidvid(data, newstate);
  915. if (ret) {
  916. printk(KERN_ERR PFX "transition frequency failed\n");
  917. ret = 1;
  918. mutex_unlock(&fidvid_mutex);
  919. goto err_out;
  920. }
  921. mutex_unlock(&fidvid_mutex);
  922. if (cpu_family == CPU_HW_PSTATE)
  923. pol->cur = find_khz_freq_from_pstate(data->powernow_table, newstate);
  924. else
  925. pol->cur = find_khz_freq_from_fid(data->currfid);
  926. ret = 0;
  927. err_out:
  928. set_cpus_allowed_ptr(current, &oldmask);
  929. return ret;
  930. }
  931. /* Driver entry point to verify the policy and range of frequencies */
  932. static int powernowk8_verify(struct cpufreq_policy *pol)
  933. {
  934. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  935. if (!data)
  936. return -EINVAL;
  937. return cpufreq_frequency_table_verify(pol, data->powernow_table);
  938. }
  939. /* per CPU init entry point to the driver */
  940. static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
  941. {
  942. struct powernow_k8_data *data;
  943. cpumask_t oldmask = CPU_MASK_ALL;
  944. int rc;
  945. if (!cpu_online(pol->cpu))
  946. return -ENODEV;
  947. if (!check_supported_cpu(pol->cpu))
  948. return -ENODEV;
  949. data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
  950. if (!data) {
  951. printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
  952. return -ENOMEM;
  953. }
  954. data->cpu = pol->cpu;
  955. if (powernow_k8_cpu_init_acpi(data)) {
  956. /*
  957. * Use the PSB BIOS structure. This is only availabe on
  958. * an UP version, and is deprecated by AMD.
  959. */
  960. if (num_online_cpus() != 1) {
  961. #ifndef CONFIG_ACPI_PROCESSOR
  962. printk(KERN_ERR PFX "ACPI Processor support is required "
  963. "for SMP systems but is absent. Please load the "
  964. "ACPI Processor module before starting this "
  965. "driver.\n");
  966. #else
  967. printk(KERN_ERR PFX "Your BIOS does not provide ACPI "
  968. "_PSS objects in a way that Linux understands. "
  969. "Please report this to the Linux ACPI maintainers"
  970. " and complain to your BIOS vendor.\n");
  971. #endif
  972. kfree(data);
  973. return -ENODEV;
  974. }
  975. if (pol->cpu != 0) {
  976. printk(KERN_ERR PFX "No ACPI _PSS objects for CPU other than "
  977. "CPU0. Complain to your BIOS vendor.\n");
  978. kfree(data);
  979. return -ENODEV;
  980. }
  981. rc = find_psb_table(data);
  982. if (rc) {
  983. kfree(data);
  984. return -ENODEV;
  985. }
  986. }
  987. /* only run on specific CPU from here on */
  988. oldmask = current->cpus_allowed;
  989. set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
  990. if (smp_processor_id() != pol->cpu) {
  991. printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
  992. goto err_out;
  993. }
  994. if (pending_bit_stuck()) {
  995. printk(KERN_ERR PFX "failing init, change pending bit set\n");
  996. goto err_out;
  997. }
  998. if (query_current_values_with_pending_wait(data))
  999. goto err_out;
  1000. if (cpu_family == CPU_OPTERON)
  1001. fidvid_msr_init();
  1002. /* run on any CPU again */
  1003. set_cpus_allowed_ptr(current, &oldmask);
  1004. pol->cpus = data->starting_core_affinity;
  1005. data->available_cores = &(pol->cpus);
  1006. /* Take a crude guess here.
  1007. * That guess was in microseconds, so multiply with 1000 */
  1008. pol->cpuinfo.transition_latency = (((data->rvo + 8) * data->vstable * VST_UNITS_20US)
  1009. + (3 * (1 << data->irt) * 10)) * 1000;
  1010. if (cpu_family == CPU_HW_PSTATE)
  1011. pol->cur = find_khz_freq_from_pstate(data->powernow_table, data->currpstate);
  1012. else
  1013. pol->cur = find_khz_freq_from_fid(data->currfid);
  1014. dprintk("policy current frequency %d kHz\n", pol->cur);
  1015. /* min/max the cpu is capable of */
  1016. if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
  1017. printk(KERN_ERR PFX "invalid powernow_table\n");
  1018. powernow_k8_cpu_exit_acpi(data);
  1019. kfree(data->powernow_table);
  1020. kfree(data);
  1021. return -EINVAL;
  1022. }
  1023. cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
  1024. if (cpu_family == CPU_HW_PSTATE)
  1025. dprintk("cpu_init done, current pstate 0x%x\n", data->currpstate);
  1026. else
  1027. dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
  1028. data->currfid, data->currvid);
  1029. per_cpu(powernow_data, pol->cpu) = data;
  1030. return 0;
  1031. err_out:
  1032. set_cpus_allowed_ptr(current, &oldmask);
  1033. powernow_k8_cpu_exit_acpi(data);
  1034. kfree(data);
  1035. return -ENODEV;
  1036. }
  1037. static int __devexit powernowk8_cpu_exit (struct cpufreq_policy *pol)
  1038. {
  1039. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  1040. if (!data)
  1041. return -EINVAL;
  1042. powernow_k8_cpu_exit_acpi(data);
  1043. cpufreq_frequency_table_put_attr(pol->cpu);
  1044. kfree(data->powernow_table);
  1045. kfree(data);
  1046. return 0;
  1047. }
  1048. static unsigned int powernowk8_get (unsigned int cpu)
  1049. {
  1050. struct powernow_k8_data *data;
  1051. cpumask_t oldmask = current->cpus_allowed;
  1052. unsigned int khz = 0;
  1053. unsigned int first;
  1054. first = first_cpu(per_cpu(cpu_core_map, cpu));
  1055. data = per_cpu(powernow_data, first);
  1056. if (!data)
  1057. return -EINVAL;
  1058. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  1059. if (smp_processor_id() != cpu) {
  1060. printk(KERN_ERR PFX
  1061. "limiting to CPU %d failed in powernowk8_get\n", cpu);
  1062. set_cpus_allowed_ptr(current, &oldmask);
  1063. return 0;
  1064. }
  1065. if (query_current_values_with_pending_wait(data))
  1066. goto out;
  1067. if (cpu_family == CPU_HW_PSTATE)
  1068. khz = find_khz_freq_from_pstate(data->powernow_table,
  1069. data->currpstate);
  1070. else
  1071. khz = find_khz_freq_from_fid(data->currfid);
  1072. out:
  1073. set_cpus_allowed_ptr(current, &oldmask);
  1074. return khz;
  1075. }
  1076. static struct freq_attr* powernow_k8_attr[] = {
  1077. &cpufreq_freq_attr_scaling_available_freqs,
  1078. NULL,
  1079. };
  1080. static struct cpufreq_driver cpufreq_amd64_driver = {
  1081. .verify = powernowk8_verify,
  1082. .target = powernowk8_target,
  1083. .init = powernowk8_cpu_init,
  1084. .exit = __devexit_p(powernowk8_cpu_exit),
  1085. .get = powernowk8_get,
  1086. .name = "powernow-k8",
  1087. .owner = THIS_MODULE,
  1088. .attr = powernow_k8_attr,
  1089. };
  1090. /* driver entry point for init */
  1091. static int __cpuinit powernowk8_init(void)
  1092. {
  1093. unsigned int i, supported_cpus = 0;
  1094. for_each_online_cpu(i) {
  1095. if (check_supported_cpu(i))
  1096. supported_cpus++;
  1097. }
  1098. if (supported_cpus == num_online_cpus()) {
  1099. powernow_k8_cpu_preinit_acpi();
  1100. printk(KERN_INFO PFX "Found %d %s "
  1101. "processors (%d cpu cores) (" VERSION ")\n",
  1102. num_online_nodes(),
  1103. boot_cpu_data.x86_model_id, supported_cpus);
  1104. return cpufreq_register_driver(&cpufreq_amd64_driver);
  1105. }
  1106. return -ENODEV;
  1107. }
  1108. /* driver entry point for term */
  1109. static void __exit powernowk8_exit(void)
  1110. {
  1111. dprintk("exit\n");
  1112. cpufreq_unregister_driver(&cpufreq_amd64_driver);
  1113. #ifdef CONFIG_X86_POWERNOW_K8_ACPI
  1114. free_percpu(acpi_perf_data);
  1115. #endif
  1116. }
  1117. MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and Mark Langsdorf <mark.langsdorf@amd.com>");
  1118. MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
  1119. MODULE_LICENSE("GPL");
  1120. late_initcall(powernowk8_init);
  1121. module_exit(powernowk8_exit);