powernow-k8.c 40 KB

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