powernow-k8.c 37 KB

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