powernow-k8.c 37 KB

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