powernow-k8.c 31 KB

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