powernow-k8.c 41 KB

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