powernow-k8.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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, reqfid))
  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, u32 reqfid)
  275. {
  276. u32 rvosteps = data->rvo;
  277. u32 savefid = data->currfid;
  278. u32 maxvid, lo, rvomult = 1;
  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. if ((savefid < LO_FID_TABLE_TOP) && (reqfid < LO_FID_TABLE_TOP))
  284. rvomult = 2;
  285. rvosteps *= rvomult;
  286. rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
  287. maxvid = 0x1f & (maxvid >> 16);
  288. dprintk("ph1 maxvid=0x%x\n", maxvid);
  289. if (reqvid < maxvid) /* lower numbers are higher voltages */
  290. reqvid = maxvid;
  291. while (data->currvid > reqvid) {
  292. dprintk("ph1: curr 0x%x, req vid 0x%x\n",
  293. data->currvid, reqvid);
  294. if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
  295. return 1;
  296. }
  297. while ((rvosteps > 0) &&
  298. ((rvomult * data->rvo + data->currvid) > reqvid)) {
  299. if (data->currvid == maxvid) {
  300. rvosteps = 0;
  301. } else {
  302. dprintk("ph1: changing vid for rvo, req 0x%x\n",
  303. data->currvid - 1);
  304. if (decrease_vid_code_by_step(data, data->currvid-1, 1))
  305. return 1;
  306. rvosteps--;
  307. }
  308. }
  309. if (query_current_values_with_pending_wait(data))
  310. return 1;
  311. if (savefid != data->currfid) {
  312. printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n",
  313. data->currfid);
  314. return 1;
  315. }
  316. dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
  317. data->currfid, data->currvid);
  318. return 0;
  319. }
  320. /* Phase 2 - core frequency transition */
  321. static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
  322. {
  323. u32 vcoreqfid, vcocurrfid, vcofiddiff;
  324. u32 fid_interval, savevid = data->currvid;
  325. if (data->currfid == reqfid) {
  326. printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n",
  327. data->currfid);
  328. return 0;
  329. }
  330. dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, "
  331. "reqfid 0x%x\n",
  332. smp_processor_id(),
  333. data->currfid, data->currvid, reqfid);
  334. vcoreqfid = convert_fid_to_vco_fid(reqfid);
  335. vcocurrfid = convert_fid_to_vco_fid(data->currfid);
  336. vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
  337. : vcoreqfid - vcocurrfid;
  338. if ((reqfid <= LO_FID_TABLE_TOP) && (data->currfid <= LO_FID_TABLE_TOP))
  339. vcofiddiff = 0;
  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 void check_supported_cpu(void *_rc)
  423. {
  424. u32 eax, ebx, ecx, edx;
  425. int *rc = _rc;
  426. *rc = -ENODEV;
  427. if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
  428. return;
  429. eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
  430. if (((eax & CPUID_XFAM) != CPUID_XFAM_K8) &&
  431. ((eax & CPUID_XFAM) < CPUID_XFAM_10H))
  432. return;
  433. if ((eax & CPUID_XFAM) == CPUID_XFAM_K8) {
  434. if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
  435. ((eax & CPUID_XMOD) > CPUID_XMOD_REV_MASK)) {
  436. printk(KERN_INFO PFX
  437. "Processor cpuid %x not supported\n", eax);
  438. return;
  439. }
  440. eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
  441. if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
  442. printk(KERN_INFO PFX
  443. "No frequency change capabilities detected\n");
  444. return;
  445. }
  446. cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
  447. if ((edx & P_STATE_TRANSITION_CAPABLE)
  448. != P_STATE_TRANSITION_CAPABLE) {
  449. printk(KERN_INFO PFX
  450. "Power state transitions not supported\n");
  451. return;
  452. }
  453. } else { /* must be a HW Pstate capable processor */
  454. cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
  455. if ((edx & USE_HW_PSTATE) == USE_HW_PSTATE)
  456. cpu_family = CPU_HW_PSTATE;
  457. else
  458. return;
  459. }
  460. *rc = 0;
  461. }
  462. static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst,
  463. u8 maxvid)
  464. {
  465. unsigned int j;
  466. u8 lastfid = 0xff;
  467. for (j = 0; j < data->numps; j++) {
  468. if (pst[j].vid > LEAST_VID) {
  469. printk(KERN_ERR FW_BUG PFX "vid %d invalid : 0x%x\n",
  470. j, pst[j].vid);
  471. return -EINVAL;
  472. }
  473. if (pst[j].vid < data->rvo) {
  474. /* vid + rvo >= 0 */
  475. printk(KERN_ERR FW_BUG PFX "0 vid exceeded with pstate"
  476. " %d\n", j);
  477. return -ENODEV;
  478. }
  479. if (pst[j].vid < maxvid + data->rvo) {
  480. /* vid + rvo >= maxvid */
  481. printk(KERN_ERR FW_BUG PFX "maxvid exceeded with pstate"
  482. " %d\n", j);
  483. return -ENODEV;
  484. }
  485. if (pst[j].fid > MAX_FID) {
  486. printk(KERN_ERR FW_BUG PFX "maxfid exceeded with pstate"
  487. " %d\n", j);
  488. return -ENODEV;
  489. }
  490. if (j && (pst[j].fid < HI_FID_TABLE_BOTTOM)) {
  491. /* Only first fid is allowed to be in "low" range */
  492. printk(KERN_ERR FW_BUG PFX "two low fids - %d : "
  493. "0x%x\n", j, pst[j].fid);
  494. return -EINVAL;
  495. }
  496. if (pst[j].fid < lastfid)
  497. lastfid = pst[j].fid;
  498. }
  499. if (lastfid & 1) {
  500. printk(KERN_ERR FW_BUG PFX "lastfid invalid\n");
  501. return -EINVAL;
  502. }
  503. if (lastfid > LO_FID_TABLE_TOP)
  504. printk(KERN_INFO FW_BUG PFX
  505. "first fid not from lo freq table\n");
  506. return 0;
  507. }
  508. static void invalidate_entry(struct cpufreq_frequency_table *powernow_table,
  509. unsigned int entry)
  510. {
  511. powernow_table[entry].frequency = CPUFREQ_ENTRY_INVALID;
  512. }
  513. static void print_basics(struct powernow_k8_data *data)
  514. {
  515. int j;
  516. for (j = 0; j < data->numps; j++) {
  517. if (data->powernow_table[j].frequency !=
  518. CPUFREQ_ENTRY_INVALID) {
  519. if (cpu_family == CPU_HW_PSTATE) {
  520. printk(KERN_INFO PFX
  521. " %d : pstate %d (%d MHz)\n", j,
  522. data->powernow_table[j].index,
  523. data->powernow_table[j].frequency/1000);
  524. } else {
  525. printk(KERN_INFO PFX
  526. " %d : fid 0x%x (%d MHz), vid 0x%x\n",
  527. j,
  528. data->powernow_table[j].index & 0xff,
  529. data->powernow_table[j].frequency/1000,
  530. data->powernow_table[j].index >> 8);
  531. }
  532. }
  533. }
  534. if (data->batps)
  535. printk(KERN_INFO PFX "Only %d pstates on battery\n",
  536. data->batps);
  537. }
  538. static u32 freq_from_fid_did(u32 fid, u32 did)
  539. {
  540. u32 mhz = 0;
  541. if (boot_cpu_data.x86 == 0x10)
  542. mhz = (100 * (fid + 0x10)) >> did;
  543. else if (boot_cpu_data.x86 == 0x11)
  544. mhz = (100 * (fid + 8)) >> did;
  545. else
  546. BUG();
  547. return mhz * 1000;
  548. }
  549. static int fill_powernow_table(struct powernow_k8_data *data,
  550. struct pst_s *pst, u8 maxvid)
  551. {
  552. struct cpufreq_frequency_table *powernow_table;
  553. unsigned int j;
  554. if (data->batps) {
  555. /* use ACPI support to get full speed on mains power */
  556. printk(KERN_WARNING PFX
  557. "Only %d pstates usable (use ACPI driver for full "
  558. "range\n", data->batps);
  559. data->numps = data->batps;
  560. }
  561. for (j = 1; j < data->numps; j++) {
  562. if (pst[j-1].fid >= pst[j].fid) {
  563. printk(KERN_ERR PFX "PST out of sequence\n");
  564. return -EINVAL;
  565. }
  566. }
  567. if (data->numps < 2) {
  568. printk(KERN_ERR PFX "no p states to transition\n");
  569. return -ENODEV;
  570. }
  571. if (check_pst_table(data, pst, maxvid))
  572. return -EINVAL;
  573. powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
  574. * (data->numps + 1)), GFP_KERNEL);
  575. if (!powernow_table) {
  576. printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
  577. return -ENOMEM;
  578. }
  579. for (j = 0; j < data->numps; j++) {
  580. int freq;
  581. powernow_table[j].index = pst[j].fid; /* lower 8 bits */
  582. powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
  583. freq = find_khz_freq_from_fid(pst[j].fid);
  584. powernow_table[j].frequency = freq;
  585. }
  586. powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
  587. powernow_table[data->numps].index = 0;
  588. if (query_current_values_with_pending_wait(data)) {
  589. kfree(powernow_table);
  590. return -EIO;
  591. }
  592. dprintk("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
  593. data->powernow_table = powernow_table;
  594. if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
  595. print_basics(data);
  596. for (j = 0; j < data->numps; j++)
  597. if ((pst[j].fid == data->currfid) &&
  598. (pst[j].vid == data->currvid))
  599. return 0;
  600. dprintk("currfid/vid do not match PST, ignoring\n");
  601. return 0;
  602. }
  603. /* Find and validate the PSB/PST table in BIOS. */
  604. static int find_psb_table(struct powernow_k8_data *data)
  605. {
  606. struct psb_s *psb;
  607. unsigned int i;
  608. u32 mvs;
  609. u8 maxvid;
  610. u32 cpst = 0;
  611. u32 thiscpuid;
  612. for (i = 0xc0000; i < 0xffff0; i += 0x10) {
  613. /* Scan BIOS looking for the signature. */
  614. /* It can not be at ffff0 - it is too big. */
  615. psb = phys_to_virt(i);
  616. if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
  617. continue;
  618. dprintk("found PSB header at 0x%p\n", psb);
  619. dprintk("table vers: 0x%x\n", psb->tableversion);
  620. if (psb->tableversion != PSB_VERSION_1_4) {
  621. printk(KERN_ERR FW_BUG PFX "PSB table is not v1.4\n");
  622. return -ENODEV;
  623. }
  624. dprintk("flags: 0x%x\n", psb->flags1);
  625. if (psb->flags1) {
  626. printk(KERN_ERR FW_BUG PFX "unknown flags\n");
  627. return -ENODEV;
  628. }
  629. data->vstable = psb->vstable;
  630. dprintk("voltage stabilization time: %d(*20us)\n",
  631. data->vstable);
  632. dprintk("flags2: 0x%x\n", psb->flags2);
  633. data->rvo = psb->flags2 & 3;
  634. data->irt = ((psb->flags2) >> 2) & 3;
  635. mvs = ((psb->flags2) >> 4) & 3;
  636. data->vidmvs = 1 << mvs;
  637. data->batps = ((psb->flags2) >> 6) & 3;
  638. dprintk("ramp voltage offset: %d\n", data->rvo);
  639. dprintk("isochronous relief time: %d\n", data->irt);
  640. dprintk("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
  641. dprintk("numpst: 0x%x\n", psb->num_tables);
  642. cpst = psb->num_tables;
  643. if ((psb->cpuid == 0x00000fc0) ||
  644. (psb->cpuid == 0x00000fe0)) {
  645. thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
  646. if ((thiscpuid == 0x00000fc0) ||
  647. (thiscpuid == 0x00000fe0))
  648. cpst = 1;
  649. }
  650. if (cpst != 1) {
  651. printk(KERN_ERR FW_BUG PFX "numpst must be 1\n");
  652. return -ENODEV;
  653. }
  654. data->plllock = psb->plllocktime;
  655. dprintk("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
  656. dprintk("maxfid: 0x%x\n", psb->maxfid);
  657. dprintk("maxvid: 0x%x\n", psb->maxvid);
  658. maxvid = psb->maxvid;
  659. data->numps = psb->numps;
  660. dprintk("numpstates: 0x%x\n", data->numps);
  661. return fill_powernow_table(data,
  662. (struct pst_s *)(psb+1), maxvid);
  663. }
  664. /*
  665. * If you see this message, complain to BIOS manufacturer. If
  666. * he tells you "we do not support Linux" or some similar
  667. * nonsense, remember that Windows 2000 uses the same legacy
  668. * mechanism that the old Linux PSB driver uses. Tell them it
  669. * is broken with Windows 2000.
  670. *
  671. * The reference to the AMD documentation is chapter 9 in the
  672. * BIOS and Kernel Developer's Guide, which is available on
  673. * www.amd.com
  674. */
  675. printk(KERN_ERR FW_BUG PFX "No PSB or ACPI _PSS objects\n");
  676. return -ENODEV;
  677. }
  678. static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data,
  679. unsigned int index)
  680. {
  681. acpi_integer control;
  682. if (!data->acpi_data.state_count || (cpu_family == CPU_HW_PSTATE))
  683. return;
  684. control = data->acpi_data.states[index].control;
  685. data->irt = (control >> IRT_SHIFT) & IRT_MASK;
  686. data->rvo = (control >> RVO_SHIFT) & RVO_MASK;
  687. data->exttype = (control >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
  688. data->plllock = (control >> PLL_L_SHIFT) & PLL_L_MASK;
  689. data->vidmvs = 1 << ((control >> MVS_SHIFT) & MVS_MASK);
  690. data->vstable = (control >> VST_SHIFT) & VST_MASK;
  691. }
  692. static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
  693. {
  694. struct cpufreq_frequency_table *powernow_table;
  695. int ret_val = -ENODEV;
  696. acpi_integer control, status;
  697. if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
  698. dprintk("register performance failed: bad ACPI data\n");
  699. return -EIO;
  700. }
  701. /* verify the data contained in the ACPI structures */
  702. if (data->acpi_data.state_count <= 1) {
  703. dprintk("No ACPI P-States\n");
  704. goto err_out;
  705. }
  706. control = data->acpi_data.control_register.space_id;
  707. status = data->acpi_data.status_register.space_id;
  708. if ((control != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
  709. (status != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
  710. dprintk("Invalid control/status registers (%x - %x)\n",
  711. control, status);
  712. goto err_out;
  713. }
  714. /* fill in data->powernow_table */
  715. powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
  716. * (data->acpi_data.state_count + 1)), GFP_KERNEL);
  717. if (!powernow_table) {
  718. dprintk("powernow_table memory alloc failure\n");
  719. goto err_out;
  720. }
  721. /* fill in data */
  722. data->numps = data->acpi_data.state_count;
  723. powernow_k8_acpi_pst_values(data, 0);
  724. if (cpu_family == CPU_HW_PSTATE)
  725. ret_val = fill_powernow_table_pstate(data, powernow_table);
  726. else
  727. ret_val = fill_powernow_table_fidvid(data, powernow_table);
  728. if (ret_val)
  729. goto err_out_mem;
  730. powernow_table[data->acpi_data.state_count].frequency =
  731. CPUFREQ_TABLE_END;
  732. powernow_table[data->acpi_data.state_count].index = 0;
  733. data->powernow_table = powernow_table;
  734. if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
  735. print_basics(data);
  736. /* notify BIOS that we exist */
  737. acpi_processor_notify_smm(THIS_MODULE);
  738. if (!zalloc_cpumask_var(&data->acpi_data.shared_cpu_map, GFP_KERNEL)) {
  739. printk(KERN_ERR PFX
  740. "unable to alloc powernow_k8_data cpumask\n");
  741. ret_val = -ENOMEM;
  742. goto err_out_mem;
  743. }
  744. return 0;
  745. err_out_mem:
  746. kfree(powernow_table);
  747. err_out:
  748. acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
  749. /* data->acpi_data.state_count informs us at ->exit()
  750. * whether ACPI was used */
  751. data->acpi_data.state_count = 0;
  752. return ret_val;
  753. }
  754. static int fill_powernow_table_pstate(struct powernow_k8_data *data,
  755. struct cpufreq_frequency_table *powernow_table)
  756. {
  757. int i;
  758. u32 hi = 0, lo = 0;
  759. rdmsr(MSR_PSTATE_CUR_LIMIT, hi, lo);
  760. data->max_hw_pstate = (hi & HW_PSTATE_MAX_MASK) >> HW_PSTATE_MAX_SHIFT;
  761. for (i = 0; i < data->acpi_data.state_count; i++) {
  762. u32 index;
  763. index = data->acpi_data.states[i].control & HW_PSTATE_MASK;
  764. if (index > data->max_hw_pstate) {
  765. printk(KERN_ERR PFX "invalid pstate %d - "
  766. "bad value %d.\n", i, index);
  767. printk(KERN_ERR PFX "Please report to BIOS "
  768. "manufacturer\n");
  769. invalidate_entry(powernow_table, i);
  770. continue;
  771. }
  772. rdmsr(MSR_PSTATE_DEF_BASE + index, lo, hi);
  773. if (!(hi & HW_PSTATE_VALID_MASK)) {
  774. dprintk("invalid pstate %d, ignoring\n", index);
  775. invalidate_entry(powernow_table, i);
  776. continue;
  777. }
  778. powernow_table[i].index = index;
  779. /* Frequency may be rounded for these */
  780. if (boot_cpu_data.x86 == 0x10 || boot_cpu_data.x86 == 0x11) {
  781. powernow_table[i].frequency =
  782. freq_from_fid_did(lo & 0x3f, (lo >> 6) & 7);
  783. } else
  784. powernow_table[i].frequency =
  785. data->acpi_data.states[i].core_frequency * 1000;
  786. }
  787. return 0;
  788. }
  789. static int fill_powernow_table_fidvid(struct powernow_k8_data *data,
  790. struct cpufreq_frequency_table *powernow_table)
  791. {
  792. int i;
  793. for (i = 0; i < data->acpi_data.state_count; i++) {
  794. u32 fid;
  795. u32 vid;
  796. u32 freq, index;
  797. acpi_integer status, control;
  798. if (data->exttype) {
  799. status = data->acpi_data.states[i].status;
  800. fid = status & EXT_FID_MASK;
  801. vid = (status >> VID_SHIFT) & EXT_VID_MASK;
  802. } else {
  803. control = data->acpi_data.states[i].control;
  804. fid = control & FID_MASK;
  805. vid = (control >> VID_SHIFT) & VID_MASK;
  806. }
  807. dprintk(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
  808. index = fid | (vid<<8);
  809. powernow_table[i].index = index;
  810. freq = find_khz_freq_from_fid(fid);
  811. powernow_table[i].frequency = freq;
  812. /* verify frequency is OK */
  813. if ((freq > (MAX_FREQ * 1000)) || (freq < (MIN_FREQ * 1000))) {
  814. dprintk("invalid freq %u kHz, ignoring\n", freq);
  815. invalidate_entry(powernow_table, i);
  816. continue;
  817. }
  818. /* verify voltage is OK -
  819. * BIOSs are using "off" to indicate invalid */
  820. if (vid == VID_OFF) {
  821. dprintk("invalid vid %u, ignoring\n", vid);
  822. invalidate_entry(powernow_table, i);
  823. continue;
  824. }
  825. if (freq != (data->acpi_data.states[i].core_frequency * 1000)) {
  826. printk(KERN_INFO PFX "invalid freq entries "
  827. "%u kHz vs. %u kHz\n", freq,
  828. (unsigned int)
  829. (data->acpi_data.states[i].core_frequency
  830. * 1000));
  831. invalidate_entry(powernow_table, i);
  832. continue;
  833. }
  834. }
  835. return 0;
  836. }
  837. static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
  838. {
  839. if (data->acpi_data.state_count)
  840. acpi_processor_unregister_performance(&data->acpi_data,
  841. data->cpu);
  842. free_cpumask_var(data->acpi_data.shared_cpu_map);
  843. }
  844. static int get_transition_latency(struct powernow_k8_data *data)
  845. {
  846. int max_latency = 0;
  847. int i;
  848. for (i = 0; i < data->acpi_data.state_count; i++) {
  849. int cur_latency = data->acpi_data.states[i].transition_latency
  850. + data->acpi_data.states[i].bus_master_latency;
  851. if (cur_latency > max_latency)
  852. max_latency = cur_latency;
  853. }
  854. if (max_latency == 0) {
  855. /*
  856. * Fam 11h always returns 0 as transition latency.
  857. * This is intended and means "very fast". While cpufreq core
  858. * and governors currently can handle that gracefully, better
  859. * set it to 1 to avoid problems in the future.
  860. * For all others it's a BIOS bug.
  861. */
  862. if (boot_cpu_data.x86 != 0x11)
  863. printk(KERN_ERR FW_WARN PFX "Invalid zero transition "
  864. "latency\n");
  865. max_latency = 1;
  866. }
  867. /* value in usecs, needs to be in nanoseconds */
  868. return 1000 * max_latency;
  869. }
  870. /* Take a frequency, and issue the fid/vid transition command */
  871. static int transition_frequency_fidvid(struct powernow_k8_data *data,
  872. unsigned int index)
  873. {
  874. u32 fid = 0;
  875. u32 vid = 0;
  876. int res, i;
  877. struct cpufreq_freqs freqs;
  878. dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
  879. /* fid/vid correctness check for k8 */
  880. /* fid are the lower 8 bits of the index we stored into
  881. * the cpufreq frequency table in find_psb_table, vid
  882. * are the upper 8 bits.
  883. */
  884. fid = data->powernow_table[index].index & 0xFF;
  885. vid = (data->powernow_table[index].index & 0xFF00) >> 8;
  886. dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
  887. if (query_current_values_with_pending_wait(data))
  888. return 1;
  889. if ((data->currvid == vid) && (data->currfid == fid)) {
  890. dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
  891. fid, vid);
  892. return 0;
  893. }
  894. dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
  895. smp_processor_id(), fid, vid);
  896. freqs.old = find_khz_freq_from_fid(data->currfid);
  897. freqs.new = find_khz_freq_from_fid(fid);
  898. for_each_cpu(i, data->available_cores) {
  899. freqs.cpu = i;
  900. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  901. }
  902. res = transition_fid_vid(data, fid, vid);
  903. freqs.new = find_khz_freq_from_fid(data->currfid);
  904. for_each_cpu(i, data->available_cores) {
  905. freqs.cpu = i;
  906. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  907. }
  908. return res;
  909. }
  910. /* Take a frequency, and issue the hardware pstate transition command */
  911. static int transition_frequency_pstate(struct powernow_k8_data *data,
  912. unsigned int index)
  913. {
  914. u32 pstate = 0;
  915. int res, i;
  916. struct cpufreq_freqs freqs;
  917. dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
  918. /* get MSR index for hardware pstate transition */
  919. pstate = index & HW_PSTATE_MASK;
  920. if (pstate > data->max_hw_pstate)
  921. return 0;
  922. freqs.old = find_khz_freq_from_pstate(data->powernow_table,
  923. data->currpstate);
  924. freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
  925. for_each_cpu(i, data->available_cores) {
  926. freqs.cpu = i;
  927. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  928. }
  929. res = transition_pstate(data, pstate);
  930. freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
  931. for_each_cpu(i, data->available_cores) {
  932. freqs.cpu = i;
  933. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  934. }
  935. return res;
  936. }
  937. /* Driver entry point to switch to the target frequency */
  938. static int powernowk8_target(struct cpufreq_policy *pol,
  939. unsigned targfreq, unsigned relation)
  940. {
  941. cpumask_t oldmask;
  942. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  943. u32 checkfid;
  944. u32 checkvid;
  945. unsigned int newstate;
  946. int ret = -EIO;
  947. if (!data)
  948. return -EINVAL;
  949. checkfid = data->currfid;
  950. checkvid = data->currvid;
  951. /* only run on specific CPU from here on */
  952. oldmask = current->cpus_allowed;
  953. set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
  954. if (smp_processor_id() != pol->cpu) {
  955. printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
  956. goto err_out;
  957. }
  958. if (pending_bit_stuck()) {
  959. printk(KERN_ERR PFX "failing targ, change pending bit set\n");
  960. goto err_out;
  961. }
  962. dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
  963. pol->cpu, targfreq, pol->min, pol->max, relation);
  964. if (query_current_values_with_pending_wait(data))
  965. goto err_out;
  966. if (cpu_family != CPU_HW_PSTATE) {
  967. dprintk("targ: curr fid 0x%x, vid 0x%x\n",
  968. data->currfid, data->currvid);
  969. if ((checkvid != data->currvid) ||
  970. (checkfid != data->currfid)) {
  971. printk(KERN_INFO PFX
  972. "error - out of sync, fix 0x%x 0x%x, "
  973. "vid 0x%x 0x%x\n",
  974. checkfid, data->currfid,
  975. checkvid, data->currvid);
  976. }
  977. }
  978. if (cpufreq_frequency_table_target(pol, data->powernow_table,
  979. targfreq, relation, &newstate))
  980. goto err_out;
  981. mutex_lock(&fidvid_mutex);
  982. powernow_k8_acpi_pst_values(data, newstate);
  983. if (cpu_family == CPU_HW_PSTATE)
  984. ret = transition_frequency_pstate(data, newstate);
  985. else
  986. ret = transition_frequency_fidvid(data, newstate);
  987. if (ret) {
  988. printk(KERN_ERR PFX "transition frequency failed\n");
  989. ret = 1;
  990. mutex_unlock(&fidvid_mutex);
  991. goto err_out;
  992. }
  993. mutex_unlock(&fidvid_mutex);
  994. if (cpu_family == CPU_HW_PSTATE)
  995. pol->cur = find_khz_freq_from_pstate(data->powernow_table,
  996. newstate);
  997. else
  998. pol->cur = find_khz_freq_from_fid(data->currfid);
  999. ret = 0;
  1000. err_out:
  1001. set_cpus_allowed_ptr(current, &oldmask);
  1002. return ret;
  1003. }
  1004. /* Driver entry point to verify the policy and range of frequencies */
  1005. static int powernowk8_verify(struct cpufreq_policy *pol)
  1006. {
  1007. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  1008. if (!data)
  1009. return -EINVAL;
  1010. return cpufreq_frequency_table_verify(pol, data->powernow_table);
  1011. }
  1012. struct init_on_cpu {
  1013. struct powernow_k8_data *data;
  1014. int rc;
  1015. };
  1016. static void __cpuinit powernowk8_cpu_init_on_cpu(void *_init_on_cpu)
  1017. {
  1018. struct init_on_cpu *init_on_cpu = _init_on_cpu;
  1019. if (pending_bit_stuck()) {
  1020. printk(KERN_ERR PFX "failing init, change pending bit set\n");
  1021. init_on_cpu->rc = -ENODEV;
  1022. return;
  1023. }
  1024. if (query_current_values_with_pending_wait(init_on_cpu->data)) {
  1025. init_on_cpu->rc = -ENODEV;
  1026. return;
  1027. }
  1028. if (cpu_family == CPU_OPTERON)
  1029. fidvid_msr_init();
  1030. init_on_cpu->rc = 0;
  1031. }
  1032. /* per CPU init entry point to the driver */
  1033. static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
  1034. {
  1035. static const char ACPI_PSS_BIOS_BUG_MSG[] =
  1036. KERN_ERR FW_BUG PFX "No compatible ACPI _PSS objects found.\n"
  1037. FW_BUG PFX "Try again with latest BIOS.\n";
  1038. struct powernow_k8_data *data;
  1039. struct init_on_cpu init_on_cpu;
  1040. int rc;
  1041. if (!cpu_online(pol->cpu))
  1042. return -ENODEV;
  1043. smp_call_function_single(pol->cpu, check_supported_cpu, &rc, 1);
  1044. if (rc)
  1045. return -ENODEV;
  1046. data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
  1047. if (!data) {
  1048. printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
  1049. return -ENOMEM;
  1050. }
  1051. data->cpu = pol->cpu;
  1052. data->currpstate = HW_PSTATE_INVALID;
  1053. if (powernow_k8_cpu_init_acpi(data)) {
  1054. /*
  1055. * Use the PSB BIOS structure. This is only availabe on
  1056. * an UP version, and is deprecated by AMD.
  1057. */
  1058. if (num_online_cpus() != 1) {
  1059. printk_once(ACPI_PSS_BIOS_BUG_MSG);
  1060. goto err_out;
  1061. }
  1062. if (pol->cpu != 0) {
  1063. printk(KERN_ERR FW_BUG PFX "No ACPI _PSS objects for "
  1064. "CPU other than CPU0. Complain to your BIOS "
  1065. "vendor.\n");
  1066. goto err_out;
  1067. }
  1068. rc = find_psb_table(data);
  1069. if (rc)
  1070. goto err_out;
  1071. /* Take a crude guess here.
  1072. * That guess was in microseconds, so multiply with 1000 */
  1073. pol->cpuinfo.transition_latency = (
  1074. ((data->rvo + 8) * data->vstable * VST_UNITS_20US) +
  1075. ((1 << data->irt) * 30)) * 1000;
  1076. } else /* ACPI _PSS objects available */
  1077. pol->cpuinfo.transition_latency = get_transition_latency(data);
  1078. /* only run on specific CPU from here on */
  1079. init_on_cpu.data = data;
  1080. smp_call_function_single(data->cpu, powernowk8_cpu_init_on_cpu,
  1081. &init_on_cpu, 1);
  1082. rc = init_on_cpu.rc;
  1083. if (rc != 0)
  1084. goto err_out_exit_acpi;
  1085. if (cpu_family == CPU_HW_PSTATE)
  1086. cpumask_copy(pol->cpus, cpumask_of(pol->cpu));
  1087. else
  1088. cpumask_copy(pol->cpus, cpu_core_mask(pol->cpu));
  1089. data->available_cores = pol->cpus;
  1090. if (cpu_family == CPU_HW_PSTATE)
  1091. pol->cur = find_khz_freq_from_pstate(data->powernow_table,
  1092. data->currpstate);
  1093. else
  1094. pol->cur = find_khz_freq_from_fid(data->currfid);
  1095. dprintk("policy current frequency %d kHz\n", pol->cur);
  1096. /* min/max the cpu is capable of */
  1097. if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
  1098. printk(KERN_ERR FW_BUG PFX "invalid powernow_table\n");
  1099. powernow_k8_cpu_exit_acpi(data);
  1100. kfree(data->powernow_table);
  1101. kfree(data);
  1102. return -EINVAL;
  1103. }
  1104. cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
  1105. if (cpu_family == CPU_HW_PSTATE)
  1106. dprintk("cpu_init done, current pstate 0x%x\n",
  1107. data->currpstate);
  1108. else
  1109. dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
  1110. data->currfid, data->currvid);
  1111. per_cpu(powernow_data, pol->cpu) = data;
  1112. return 0;
  1113. err_out_exit_acpi:
  1114. powernow_k8_cpu_exit_acpi(data);
  1115. err_out:
  1116. kfree(data);
  1117. return -ENODEV;
  1118. }
  1119. static int __devexit powernowk8_cpu_exit(struct cpufreq_policy *pol)
  1120. {
  1121. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  1122. if (!data)
  1123. return -EINVAL;
  1124. powernow_k8_cpu_exit_acpi(data);
  1125. cpufreq_frequency_table_put_attr(pol->cpu);
  1126. kfree(data->powernow_table);
  1127. kfree(data);
  1128. return 0;
  1129. }
  1130. static void query_values_on_cpu(void *_err)
  1131. {
  1132. int *err = _err;
  1133. struct powernow_k8_data *data = __get_cpu_var(powernow_data);
  1134. *err = query_current_values_with_pending_wait(data);
  1135. }
  1136. static unsigned int powernowk8_get(unsigned int cpu)
  1137. {
  1138. struct powernow_k8_data *data = per_cpu(powernow_data, cpu);
  1139. unsigned int khz = 0;
  1140. int err;
  1141. if (!data)
  1142. return -EINVAL;
  1143. smp_call_function_single(cpu, query_values_on_cpu, &err, true);
  1144. if (err)
  1145. goto out;
  1146. if (cpu_family == CPU_HW_PSTATE)
  1147. khz = find_khz_freq_from_pstate(data->powernow_table,
  1148. data->currpstate);
  1149. else
  1150. khz = find_khz_freq_from_fid(data->currfid);
  1151. out:
  1152. return khz;
  1153. }
  1154. static struct freq_attr *powernow_k8_attr[] = {
  1155. &cpufreq_freq_attr_scaling_available_freqs,
  1156. NULL,
  1157. };
  1158. static struct cpufreq_driver cpufreq_amd64_driver = {
  1159. .verify = powernowk8_verify,
  1160. .target = powernowk8_target,
  1161. .init = powernowk8_cpu_init,
  1162. .exit = __devexit_p(powernowk8_cpu_exit),
  1163. .get = powernowk8_get,
  1164. .name = "powernow-k8",
  1165. .owner = THIS_MODULE,
  1166. .attr = powernow_k8_attr,
  1167. };
  1168. /* driver entry point for init */
  1169. static int __cpuinit powernowk8_init(void)
  1170. {
  1171. unsigned int i, supported_cpus = 0;
  1172. for_each_online_cpu(i) {
  1173. int rc;
  1174. smp_call_function_single(i, check_supported_cpu, &rc, 1);
  1175. if (rc == 0)
  1176. supported_cpus++;
  1177. }
  1178. if (supported_cpus == num_online_cpus()) {
  1179. printk(KERN_INFO PFX "Found %d %s "
  1180. "processors (%d cpu cores) (" VERSION ")\n",
  1181. num_online_nodes(),
  1182. boot_cpu_data.x86_model_id, supported_cpus);
  1183. return cpufreq_register_driver(&cpufreq_amd64_driver);
  1184. }
  1185. return -ENODEV;
  1186. }
  1187. /* driver entry point for term */
  1188. static void __exit powernowk8_exit(void)
  1189. {
  1190. dprintk("exit\n");
  1191. cpufreq_unregister_driver(&cpufreq_amd64_driver);
  1192. }
  1193. MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and "
  1194. "Mark Langsdorf <mark.langsdorf@amd.com>");
  1195. MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
  1196. MODULE_LICENSE("GPL");
  1197. late_initcall(powernowk8_init);
  1198. module_exit(powernowk8_exit);