powernow-k8.c 31 KB

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