powernow-k8.c 40 KB

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