powernow-k8.c 37 KB

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