powernow-k8.c 36 KB

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