powernow-k8.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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->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->cpu);
  720. free_cpumask_var(data->acpi_data.shared_cpu_map);
  721. }
  722. static int get_transition_latency(struct powernow_k8_data *data)
  723. {
  724. int max_latency = 0;
  725. int i;
  726. for (i = 0; i < data->acpi_data.state_count; i++) {
  727. int cur_latency = data->acpi_data.states[i].transition_latency
  728. + data->acpi_data.states[i].bus_master_latency;
  729. if (cur_latency > max_latency)
  730. max_latency = cur_latency;
  731. }
  732. if (max_latency == 0) {
  733. pr_err(FW_WARN "Invalid zero transition latency\n");
  734. max_latency = 1;
  735. }
  736. /* value in usecs, needs to be in nanoseconds */
  737. return 1000 * max_latency;
  738. }
  739. /* Take a frequency, and issue the fid/vid transition command */
  740. static int transition_frequency_fidvid(struct powernow_k8_data *data,
  741. unsigned int index)
  742. {
  743. struct cpufreq_policy *policy;
  744. u32 fid = 0;
  745. u32 vid = 0;
  746. int res;
  747. struct cpufreq_freqs freqs;
  748. pr_debug("cpu %d transition to index %u\n", smp_processor_id(), index);
  749. /* fid/vid correctness check for k8 */
  750. /* fid are the lower 8 bits of the index we stored into
  751. * the cpufreq frequency table in find_psb_table, vid
  752. * are the upper 8 bits.
  753. */
  754. fid = data->powernow_table[index].driver_data & 0xFF;
  755. vid = (data->powernow_table[index].driver_data & 0xFF00) >> 8;
  756. pr_debug("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
  757. if (query_current_values_with_pending_wait(data))
  758. return 1;
  759. if ((data->currvid == vid) && (data->currfid == fid)) {
  760. pr_debug("target matches current values (fid 0x%x, vid 0x%x)\n",
  761. fid, vid);
  762. return 0;
  763. }
  764. pr_debug("cpu %d, changing to fid 0x%x, vid 0x%x\n",
  765. smp_processor_id(), fid, vid);
  766. freqs.old = find_khz_freq_from_fid(data->currfid);
  767. freqs.new = find_khz_freq_from_fid(fid);
  768. policy = cpufreq_cpu_get(smp_processor_id());
  769. cpufreq_cpu_put(policy);
  770. cpufreq_freq_transition_begin(policy, &freqs);
  771. res = transition_fid_vid(data, fid, vid);
  772. cpufreq_freq_transition_end(policy, &freqs, res);
  773. return res;
  774. }
  775. struct powernowk8_target_arg {
  776. struct cpufreq_policy *pol;
  777. unsigned newstate;
  778. };
  779. static long powernowk8_target_fn(void *arg)
  780. {
  781. struct powernowk8_target_arg *pta = arg;
  782. struct cpufreq_policy *pol = pta->pol;
  783. unsigned newstate = pta->newstate;
  784. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  785. u32 checkfid;
  786. u32 checkvid;
  787. int ret;
  788. if (!data)
  789. return -EINVAL;
  790. checkfid = data->currfid;
  791. checkvid = data->currvid;
  792. if (pending_bit_stuck()) {
  793. pr_err("failing targ, change pending bit set\n");
  794. return -EIO;
  795. }
  796. pr_debug("targ: cpu %d, %d kHz, min %d, max %d\n",
  797. pol->cpu, data->powernow_table[newstate].frequency, pol->min,
  798. pol->max);
  799. if (query_current_values_with_pending_wait(data))
  800. return -EIO;
  801. pr_debug("targ: curr fid 0x%x, vid 0x%x\n",
  802. data->currfid, data->currvid);
  803. if ((checkvid != data->currvid) ||
  804. (checkfid != data->currfid)) {
  805. pr_info("error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
  806. checkfid, data->currfid,
  807. checkvid, data->currvid);
  808. }
  809. mutex_lock(&fidvid_mutex);
  810. powernow_k8_acpi_pst_values(data, newstate);
  811. ret = transition_frequency_fidvid(data, newstate);
  812. if (ret) {
  813. pr_err("transition frequency failed\n");
  814. mutex_unlock(&fidvid_mutex);
  815. return 1;
  816. }
  817. mutex_unlock(&fidvid_mutex);
  818. pol->cur = find_khz_freq_from_fid(data->currfid);
  819. return 0;
  820. }
  821. /* Driver entry point to switch to the target frequency */
  822. static int powernowk8_target(struct cpufreq_policy *pol, unsigned index)
  823. {
  824. struct powernowk8_target_arg pta = { .pol = pol, .newstate = index };
  825. return work_on_cpu(pol->cpu, powernowk8_target_fn, &pta);
  826. }
  827. struct init_on_cpu {
  828. struct powernow_k8_data *data;
  829. int rc;
  830. };
  831. static void powernowk8_cpu_init_on_cpu(void *_init_on_cpu)
  832. {
  833. struct init_on_cpu *init_on_cpu = _init_on_cpu;
  834. if (pending_bit_stuck()) {
  835. pr_err("failing init, change pending bit set\n");
  836. init_on_cpu->rc = -ENODEV;
  837. return;
  838. }
  839. if (query_current_values_with_pending_wait(init_on_cpu->data)) {
  840. init_on_cpu->rc = -ENODEV;
  841. return;
  842. }
  843. fidvid_msr_init();
  844. init_on_cpu->rc = 0;
  845. }
  846. #define MISSING_PSS_MSG \
  847. FW_BUG "No compatible ACPI _PSS objects found.\n" \
  848. FW_BUG "First, make sure Cool'N'Quiet is enabled in the BIOS.\n" \
  849. FW_BUG "If that doesn't help, try upgrading your BIOS.\n"
  850. /* per CPU init entry point to the driver */
  851. static int powernowk8_cpu_init(struct cpufreq_policy *pol)
  852. {
  853. struct powernow_k8_data *data;
  854. struct init_on_cpu init_on_cpu;
  855. int rc, cpu;
  856. smp_call_function_single(pol->cpu, check_supported_cpu, &rc, 1);
  857. if (rc)
  858. return -ENODEV;
  859. data = kzalloc(sizeof(*data), GFP_KERNEL);
  860. if (!data) {
  861. pr_err("unable to alloc powernow_k8_data");
  862. return -ENOMEM;
  863. }
  864. data->cpu = pol->cpu;
  865. if (powernow_k8_cpu_init_acpi(data)) {
  866. /*
  867. * Use the PSB BIOS structure. This is only available on
  868. * an UP version, and is deprecated by AMD.
  869. */
  870. if (num_online_cpus() != 1) {
  871. pr_err_once(MISSING_PSS_MSG);
  872. goto err_out;
  873. }
  874. if (pol->cpu != 0) {
  875. pr_err(FW_BUG "No ACPI _PSS objects for CPU other than CPU0. Complain to your BIOS vendor.\n");
  876. goto err_out;
  877. }
  878. rc = find_psb_table(data);
  879. if (rc)
  880. goto err_out;
  881. /* Take a crude guess here.
  882. * That guess was in microseconds, so multiply with 1000 */
  883. pol->cpuinfo.transition_latency = (
  884. ((data->rvo + 8) * data->vstable * VST_UNITS_20US) +
  885. ((1 << data->irt) * 30)) * 1000;
  886. } else /* ACPI _PSS objects available */
  887. pol->cpuinfo.transition_latency = get_transition_latency(data);
  888. /* only run on specific CPU from here on */
  889. init_on_cpu.data = data;
  890. smp_call_function_single(data->cpu, powernowk8_cpu_init_on_cpu,
  891. &init_on_cpu, 1);
  892. rc = init_on_cpu.rc;
  893. if (rc != 0)
  894. goto err_out_exit_acpi;
  895. cpumask_copy(pol->cpus, topology_core_cpumask(pol->cpu));
  896. data->available_cores = pol->cpus;
  897. /* min/max the cpu is capable of */
  898. if (cpufreq_table_validate_and_show(pol, data->powernow_table)) {
  899. pr_err(FW_BUG "invalid powernow_table\n");
  900. powernow_k8_cpu_exit_acpi(data);
  901. kfree(data->powernow_table);
  902. kfree(data);
  903. return -EINVAL;
  904. }
  905. pr_debug("cpu_init done, current fid 0x%x, vid 0x%x\n",
  906. data->currfid, data->currvid);
  907. /* Point all the CPUs in this policy to the same data */
  908. for_each_cpu(cpu, pol->cpus)
  909. per_cpu(powernow_data, cpu) = data;
  910. return 0;
  911. err_out_exit_acpi:
  912. powernow_k8_cpu_exit_acpi(data);
  913. err_out:
  914. kfree(data);
  915. return -ENODEV;
  916. }
  917. static int powernowk8_cpu_exit(struct cpufreq_policy *pol)
  918. {
  919. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  920. int cpu;
  921. if (!data)
  922. return -EINVAL;
  923. powernow_k8_cpu_exit_acpi(data);
  924. kfree(data->powernow_table);
  925. kfree(data);
  926. for_each_cpu(cpu, pol->cpus)
  927. per_cpu(powernow_data, cpu) = NULL;
  928. return 0;
  929. }
  930. static void query_values_on_cpu(void *_err)
  931. {
  932. int *err = _err;
  933. struct powernow_k8_data *data = __this_cpu_read(powernow_data);
  934. *err = query_current_values_with_pending_wait(data);
  935. }
  936. static unsigned int powernowk8_get(unsigned int cpu)
  937. {
  938. struct powernow_k8_data *data = per_cpu(powernow_data, cpu);
  939. unsigned int khz = 0;
  940. int err;
  941. if (!data)
  942. return 0;
  943. smp_call_function_single(cpu, query_values_on_cpu, &err, true);
  944. if (err)
  945. goto out;
  946. khz = find_khz_freq_from_fid(data->currfid);
  947. out:
  948. return khz;
  949. }
  950. static struct cpufreq_driver cpufreq_amd64_driver = {
  951. .flags = CPUFREQ_ASYNC_NOTIFICATION,
  952. .verify = cpufreq_generic_frequency_table_verify,
  953. .target_index = powernowk8_target,
  954. .bios_limit = acpi_processor_get_bios_limit,
  955. .init = powernowk8_cpu_init,
  956. .exit = powernowk8_cpu_exit,
  957. .get = powernowk8_get,
  958. .name = "powernow-k8",
  959. .attr = cpufreq_generic_attr,
  960. };
  961. static void __request_acpi_cpufreq(void)
  962. {
  963. const char *cur_drv, *drv = "acpi-cpufreq";
  964. cur_drv = cpufreq_get_current_driver();
  965. if (!cur_drv)
  966. goto request;
  967. if (strncmp(cur_drv, drv, min_t(size_t, strlen(cur_drv), strlen(drv))))
  968. pr_warn("WTF driver: %s\n", cur_drv);
  969. return;
  970. request:
  971. pr_warn("This CPU is not supported anymore, using acpi-cpufreq instead.\n");
  972. request_module(drv);
  973. }
  974. /* driver entry point for init */
  975. static int powernowk8_init(void)
  976. {
  977. unsigned int i, supported_cpus = 0;
  978. int ret;
  979. if (static_cpu_has(X86_FEATURE_HW_PSTATE)) {
  980. __request_acpi_cpufreq();
  981. return -ENODEV;
  982. }
  983. if (!x86_match_cpu(powernow_k8_ids))
  984. return -ENODEV;
  985. get_online_cpus();
  986. for_each_online_cpu(i) {
  987. smp_call_function_single(i, check_supported_cpu, &ret, 1);
  988. if (!ret)
  989. supported_cpus++;
  990. }
  991. if (supported_cpus != num_online_cpus()) {
  992. put_online_cpus();
  993. return -ENODEV;
  994. }
  995. put_online_cpus();
  996. ret = cpufreq_register_driver(&cpufreq_amd64_driver);
  997. if (ret)
  998. return ret;
  999. pr_info("Found %d %s (%d cpu cores) (" VERSION ")\n",
  1000. num_online_nodes(), boot_cpu_data.x86_model_id, supported_cpus);
  1001. return ret;
  1002. }
  1003. /* driver entry point for term */
  1004. static void __exit powernowk8_exit(void)
  1005. {
  1006. pr_debug("exit\n");
  1007. cpufreq_unregister_driver(&cpufreq_amd64_driver);
  1008. }
  1009. MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com>");
  1010. MODULE_AUTHOR("Mark Langsdorf <mark.langsdorf@amd.com>");
  1011. MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
  1012. MODULE_LICENSE("GPL");
  1013. late_initcall(powernowk8_init);
  1014. module_exit(powernowk8_exit);