powernow-k8.c 31 KB

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