powernow-k8.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /*
  2. * (c) 2003, 2004, 2005 Advanced Micro Devices, Inc.
  3. * Your use of this code is subject to the terms and conditions of the
  4. * GNU general public license version 2. See "COPYING" or
  5. * http://www.gnu.org/licenses/gpl.html
  6. *
  7. * Support : mark.langsdorf@amd.com
  8. *
  9. * Based on the powernow-k7.c module written by Dave Jones.
  10. * (C) 2003 Dave Jones <davej@codemonkey.org.uk> on behalf of SuSE Labs
  11. * (C) 2004 Dominik Brodowski <linux@brodo.de>
  12. * (C) 2004 Pavel Machek <pavel@suse.cz>
  13. * Licensed under the terms of the GNU GPL License version 2.
  14. * Based upon datasheets & sample CPUs kindly provided by AMD.
  15. *
  16. * Valuable input gratefully received from Dave Jones, Pavel Machek,
  17. * Dominik Brodowski, and others.
  18. * Originally developed by Paul Devriendt.
  19. * Processor information obtained from Chapter 9 (Power and Thermal Management)
  20. * of the "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
  21. * Opteron Processors" available for download from www.amd.com
  22. *
  23. * Tables for specific CPUs can be infrerred from
  24. * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/30430.pdf
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/smp.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/cpufreq.h>
  31. #include <linux/slab.h>
  32. #include <linux/string.h>
  33. #include <linux/cpumask.h>
  34. #include <linux/sched.h> /* for current / set_cpus_allowed() */
  35. #include <asm/msr.h>
  36. #include <asm/io.h>
  37. #include <asm/delay.h>
  38. #ifdef CONFIG_X86_POWERNOW_K8_ACPI
  39. #include <linux/acpi.h>
  40. #include <acpi/processor.h>
  41. #endif
  42. #define PFX "powernow-k8: "
  43. #define BFX PFX "BIOS error: "
  44. #define VERSION "version 1.60.1"
  45. #include "powernow-k8.h"
  46. /* serialize freq changes */
  47. static DECLARE_MUTEX(fidvid_sem);
  48. static struct powernow_k8_data *powernow_data[NR_CPUS];
  49. #ifndef CONFIG_SMP
  50. static cpumask_t cpu_core_map[1] = { CPU_MASK_ALL };
  51. #endif
  52. /* Return a frequency in MHz, given an input fid */
  53. static u32 find_freq_from_fid(u32 fid)
  54. {
  55. return 800 + (fid * 100);
  56. }
  57. /* Return a frequency in KHz, given an input fid */
  58. static u32 find_khz_freq_from_fid(u32 fid)
  59. {
  60. return 1000 * find_freq_from_fid(fid);
  61. }
  62. /* Return a voltage in miliVolts, given an input vid */
  63. static u32 find_millivolts_from_vid(struct powernow_k8_data *data, u32 vid)
  64. {
  65. return 1550-vid*25;
  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. dprintk("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 stabalization 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. dprintk("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. printk(KERN_ERR PFX "internal error - overflow on fid write\n");
  142. return 1;
  143. }
  144. lo = fid | (data->currvid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
  145. dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
  146. fid, lo, data->plllock * PLL_LOCK_CONVERSION);
  147. do {
  148. wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
  149. if (i++ > 100) {
  150. printk(KERN_ERR PFX "internal error - pending bit very stuck - no further pstate changes possible\n");
  151. return 1;
  152. }
  153. } while (query_current_values_with_pending_wait(data));
  154. count_off_irt(data);
  155. if (savevid != data->currvid) {
  156. printk(KERN_ERR PFX "vid change on fid trans, old 0x%x, new 0x%x\n",
  157. savevid, data->currvid);
  158. return 1;
  159. }
  160. if (fid != data->currfid) {
  161. printk(KERN_ERR PFX "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
  162. data->currfid);
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. /* Write a new vid to the hardware */
  168. static int write_new_vid(struct powernow_k8_data *data, u32 vid)
  169. {
  170. u32 lo;
  171. u32 savefid = data->currfid;
  172. int i = 0;
  173. if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
  174. printk(KERN_ERR PFX "internal error - overflow on vid write\n");
  175. return 1;
  176. }
  177. lo = data->currfid | (vid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
  178. dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
  179. vid, lo, STOP_GRANT_5NS);
  180. do {
  181. wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
  182. if (i++ > 100) {
  183. printk(KERN_ERR PFX "internal error - pending bit very stuck - no further pstate changes possible\n");
  184. return 1;
  185. }
  186. } while (query_current_values_with_pending_wait(data));
  187. if (savefid != data->currfid) {
  188. printk(KERN_ERR PFX "fid changed on vid trans, old 0x%x new 0x%x\n",
  189. savefid, data->currfid);
  190. return 1;
  191. }
  192. if (vid != data->currvid) {
  193. printk(KERN_ERR PFX "vid trans failed, vid 0x%x, curr 0x%x\n", vid,
  194. data->currvid);
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. /*
  200. * Reduce the vid by the max of step or reqvid.
  201. * Decreasing vid codes represent increasing voltages:
  202. * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off.
  203. */
  204. static int decrease_vid_code_by_step(struct powernow_k8_data *data, u32 reqvid, u32 step)
  205. {
  206. if ((data->currvid - reqvid) > step)
  207. reqvid = data->currvid - step;
  208. if (write_new_vid(data, reqvid))
  209. return 1;
  210. count_off_vst(data);
  211. return 0;
  212. }
  213. /* Change the fid and vid, by the 3 phases. */
  214. static int transition_fid_vid(struct powernow_k8_data *data, u32 reqfid, u32 reqvid)
  215. {
  216. if (core_voltage_pre_transition(data, reqvid))
  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. printk(KERN_ERR PFX "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. dprintk("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, u32 reqvid)
  236. {
  237. u32 rvosteps = data->rvo;
  238. u32 savefid = data->currfid;
  239. u32 maxvid, lo;
  240. dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, reqvid 0x%x, rvo 0x%x\n",
  241. smp_processor_id(),
  242. data->currfid, data->currvid, reqvid, data->rvo);
  243. rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
  244. maxvid = 0x1f & (maxvid >> 16);
  245. dprintk("ph1 maxvid=0x%x\n", maxvid);
  246. if (reqvid < maxvid) /* lower numbers are higher voltages */
  247. reqvid = maxvid;
  248. while (data->currvid > reqvid) {
  249. dprintk("ph1: curr 0x%x, req vid 0x%x\n",
  250. data->currvid, reqvid);
  251. if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
  252. return 1;
  253. }
  254. while ((rvosteps > 0) && ((data->rvo + data->currvid) > reqvid)) {
  255. if (data->currvid == maxvid) {
  256. rvosteps = 0;
  257. } else {
  258. dprintk("ph1: changing vid for rvo, req 0x%x\n",
  259. data->currvid - 1);
  260. if (decrease_vid_code_by_step(data, data->currvid - 1, 1))
  261. return 1;
  262. rvosteps--;
  263. }
  264. }
  265. if (query_current_values_with_pending_wait(data))
  266. return 1;
  267. if (savefid != data->currfid) {
  268. printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n", data->currfid);
  269. return 1;
  270. }
  271. dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
  272. data->currfid, data->currvid);
  273. return 0;
  274. }
  275. /* Phase 2 - core frequency transition */
  276. static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
  277. {
  278. u32 vcoreqfid, vcocurrfid, vcofiddiff, fid_interval, savevid = data->currvid;
  279. if ((reqfid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
  280. printk(KERN_ERR PFX "ph2: illegal lo-lo transition 0x%x 0x%x\n",
  281. reqfid, data->currfid);
  282. return 1;
  283. }
  284. if (data->currfid == reqfid) {
  285. printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n", data->currfid);
  286. return 0;
  287. }
  288. dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, reqfid 0x%x\n",
  289. smp_processor_id(),
  290. data->currfid, data->currvid, reqfid);
  291. vcoreqfid = convert_fid_to_vco_fid(reqfid);
  292. vcocurrfid = convert_fid_to_vco_fid(data->currfid);
  293. vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
  294. : vcoreqfid - vcocurrfid;
  295. while (vcofiddiff > 2) {
  296. (data->currfid & 1) ? (fid_interval = 1) : (fid_interval = 2);
  297. if (reqfid > data->currfid) {
  298. if (data->currfid > LO_FID_TABLE_TOP) {
  299. if (write_new_fid(data, data->currfid + fid_interval)) {
  300. return 1;
  301. }
  302. } else {
  303. if (write_new_fid
  304. (data, 2 + convert_fid_to_vco_fid(data->currfid))) {
  305. return 1;
  306. }
  307. }
  308. } else {
  309. if (write_new_fid(data, data->currfid - fid_interval))
  310. return 1;
  311. }
  312. vcocurrfid = convert_fid_to_vco_fid(data->currfid);
  313. vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
  314. : vcoreqfid - vcocurrfid;
  315. }
  316. if (write_new_fid(data, reqfid))
  317. return 1;
  318. if (query_current_values_with_pending_wait(data))
  319. return 1;
  320. if (data->currfid != reqfid) {
  321. printk(KERN_ERR PFX
  322. "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. printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
  328. savevid, data->currvid);
  329. return 1;
  330. }
  331. dprintk("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, u32 reqvid)
  337. {
  338. u32 savefid = data->currfid;
  339. u32 savereqvid = reqvid;
  340. dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
  341. smp_processor_id(),
  342. data->currfid, data->currvid);
  343. if (reqvid != data->currvid) {
  344. if (write_new_vid(data, reqvid))
  345. return 1;
  346. if (savefid != data->currfid) {
  347. printk(KERN_ERR PFX
  348. "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. printk(KERN_ERR PFX
  354. "ph3: failed vid transition\n, req 0x%x, curr 0x%x",
  355. reqvid, data->currvid);
  356. return 1;
  357. }
  358. }
  359. if (query_current_values_with_pending_wait(data))
  360. return 1;
  361. if (savereqvid != data->currvid) {
  362. dprintk("ph3 failed, currvid 0x%x\n", data->currvid);
  363. return 1;
  364. }
  365. if (savefid != data->currfid) {
  366. dprintk("ph3 failed, currfid changed 0x%x\n",
  367. data->currfid);
  368. return 1;
  369. }
  370. dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
  371. data->currfid, data->currvid);
  372. return 0;
  373. }
  374. static int check_supported_cpu(unsigned int cpu)
  375. {
  376. cpumask_t oldmask = CPU_MASK_ALL;
  377. u32 eax, ebx, ecx, edx;
  378. unsigned int rc = 0;
  379. oldmask = current->cpus_allowed;
  380. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  381. if (smp_processor_id() != cpu) {
  382. printk(KERN_ERR PFX "limiting to cpu %u failed\n", cpu);
  383. goto out;
  384. }
  385. if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
  386. goto out;
  387. eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
  388. if ((eax & CPUID_XFAM) != CPUID_XFAM_K8)
  389. goto out;
  390. if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
  391. ((eax & CPUID_XMOD) > CPUID_XMOD_REV_G)) {
  392. printk(KERN_INFO PFX "Processor cpuid %x not supported\n", eax);
  393. goto out;
  394. }
  395. eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
  396. if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
  397. printk(KERN_INFO PFX
  398. "No frequency change capabilities detected\n");
  399. goto out;
  400. }
  401. cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
  402. if ((edx & P_STATE_TRANSITION_CAPABLE) != P_STATE_TRANSITION_CAPABLE) {
  403. printk(KERN_INFO PFX "Power state transitions not supported\n");
  404. goto out;
  405. }
  406. rc = 1;
  407. out:
  408. set_cpus_allowed(current, oldmask);
  409. return rc;
  410. }
  411. static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
  412. {
  413. unsigned int j;
  414. u8 lastfid = 0xff;
  415. for (j = 0; j < data->numps; j++) {
  416. if (pst[j].vid > LEAST_VID) {
  417. printk(KERN_ERR PFX "vid %d invalid : 0x%x\n", j, pst[j].vid);
  418. return -EINVAL;
  419. }
  420. if (pst[j].vid < data->rvo) { /* vid + rvo >= 0 */
  421. printk(KERN_ERR BFX "0 vid exceeded with pstate %d\n", j);
  422. return -ENODEV;
  423. }
  424. if (pst[j].vid < maxvid + data->rvo) { /* vid + rvo >= maxvid */
  425. printk(KERN_ERR BFX "maxvid exceeded with pstate %d\n", j);
  426. return -ENODEV;
  427. }
  428. if (pst[j].fid > MAX_FID) {
  429. printk(KERN_ERR BFX "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. printk(KERN_ERR BFX "two low fids - %d : 0x%x\n", j, pst[j].fid);
  435. return -EINVAL;
  436. }
  437. if (pst[j].fid < lastfid)
  438. lastfid = pst[j].fid;
  439. }
  440. if (lastfid & 1) {
  441. printk(KERN_ERR BFX "lastfid invalid\n");
  442. return -EINVAL;
  443. }
  444. if (lastfid > LO_FID_TABLE_TOP)
  445. printk(KERN_INFO BFX "first fid not from lo freq table\n");
  446. return 0;
  447. }
  448. static void print_basics(struct powernow_k8_data *data)
  449. {
  450. int j;
  451. for (j = 0; j < data->numps; j++) {
  452. if (data->powernow_table[j].frequency != CPUFREQ_ENTRY_INVALID)
  453. printk(KERN_INFO PFX " %d : fid 0x%x (%d MHz), vid 0x%x (%d mV)\n", j,
  454. data->powernow_table[j].index & 0xff,
  455. data->powernow_table[j].frequency/1000,
  456. data->powernow_table[j].index >> 8,
  457. find_millivolts_from_vid(data, data->powernow_table[j].index >> 8));
  458. }
  459. if (data->batps)
  460. printk(KERN_INFO PFX "Only %d pstates on battery\n", data->batps);
  461. }
  462. static int fill_powernow_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
  463. {
  464. struct cpufreq_frequency_table *powernow_table;
  465. unsigned int j;
  466. if (data->batps) { /* use ACPI support to get full speed on mains power */
  467. printk(KERN_WARNING PFX "Only %d pstates usable (use ACPI driver for full range\n", data->batps);
  468. data->numps = data->batps;
  469. }
  470. for ( j=1; j<data->numps; j++ ) {
  471. if (pst[j-1].fid >= pst[j].fid) {
  472. printk(KERN_ERR PFX "PST out of sequence\n");
  473. return -EINVAL;
  474. }
  475. }
  476. if (data->numps < 2) {
  477. printk(KERN_ERR PFX "no p states to transition\n");
  478. return -ENODEV;
  479. }
  480. if (check_pst_table(data, pst, maxvid))
  481. return -EINVAL;
  482. powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
  483. * (data->numps + 1)), GFP_KERNEL);
  484. if (!powernow_table) {
  485. printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
  486. return -ENOMEM;
  487. }
  488. for (j = 0; j < data->numps; j++) {
  489. powernow_table[j].index = pst[j].fid; /* lower 8 bits */
  490. powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
  491. powernow_table[j].frequency = find_khz_freq_from_fid(pst[j].fid);
  492. }
  493. powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
  494. powernow_table[data->numps].index = 0;
  495. if (query_current_values_with_pending_wait(data)) {
  496. kfree(powernow_table);
  497. return -EIO;
  498. }
  499. dprintk("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
  500. data->powernow_table = powernow_table;
  501. print_basics(data);
  502. for (j = 0; j < data->numps; j++)
  503. if ((pst[j].fid==data->currfid) && (pst[j].vid==data->currvid))
  504. return 0;
  505. dprintk("currfid/vid do not match PST, ignoring\n");
  506. return 0;
  507. }
  508. /* Find and validate the PSB/PST table in BIOS. */
  509. static int find_psb_table(struct powernow_k8_data *data)
  510. {
  511. struct psb_s *psb;
  512. unsigned int i;
  513. u32 mvs;
  514. u8 maxvid;
  515. u32 cpst = 0;
  516. u32 thiscpuid;
  517. for (i = 0xc0000; i < 0xffff0; i += 0x10) {
  518. /* Scan BIOS looking for the signature. */
  519. /* It can not be at ffff0 - it is too big. */
  520. psb = phys_to_virt(i);
  521. if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
  522. continue;
  523. dprintk("found PSB header at 0x%p\n", psb);
  524. dprintk("table vers: 0x%x\n", psb->tableversion);
  525. if (psb->tableversion != PSB_VERSION_1_4) {
  526. printk(KERN_ERR BFX "PSB table is not v1.4\n");
  527. return -ENODEV;
  528. }
  529. dprintk("flags: 0x%x\n", psb->flags1);
  530. if (psb->flags1) {
  531. printk(KERN_ERR BFX "unknown flags\n");
  532. return -ENODEV;
  533. }
  534. data->vstable = psb->vstable;
  535. dprintk("voltage stabilization time: %d(*20us)\n", data->vstable);
  536. dprintk("flags2: 0x%x\n", psb->flags2);
  537. data->rvo = psb->flags2 & 3;
  538. data->irt = ((psb->flags2) >> 2) & 3;
  539. mvs = ((psb->flags2) >> 4) & 3;
  540. data->vidmvs = 1 << mvs;
  541. data->batps = ((psb->flags2) >> 6) & 3;
  542. dprintk("ramp voltage offset: %d\n", data->rvo);
  543. dprintk("isochronous relief time: %d\n", data->irt);
  544. dprintk("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
  545. dprintk("numpst: 0x%x\n", psb->num_tables);
  546. cpst = psb->num_tables;
  547. if ((psb->cpuid == 0x00000fc0) || (psb->cpuid == 0x00000fe0) ){
  548. thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
  549. if ((thiscpuid == 0x00000fc0) || (thiscpuid == 0x00000fe0) ) {
  550. cpst = 1;
  551. }
  552. }
  553. if (cpst != 1) {
  554. printk(KERN_ERR BFX "numpst must be 1\n");
  555. return -ENODEV;
  556. }
  557. data->plllock = psb->plllocktime;
  558. dprintk("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
  559. dprintk("maxfid: 0x%x\n", psb->maxfid);
  560. dprintk("maxvid: 0x%x\n", psb->maxvid);
  561. maxvid = psb->maxvid;
  562. data->numps = psb->numps;
  563. dprintk("numpstates: 0x%x\n", data->numps);
  564. return fill_powernow_table(data, (struct pst_s *)(psb+1), maxvid);
  565. }
  566. /*
  567. * If you see this message, complain to BIOS manufacturer. If
  568. * he tells you "we do not support Linux" or some similar
  569. * nonsense, remember that Windows 2000 uses the same legacy
  570. * mechanism that the old Linux PSB driver uses. Tell them it
  571. * is broken with Windows 2000.
  572. *
  573. * The reference to the AMD documentation is chapter 9 in the
  574. * BIOS and Kernel Developer's Guide, which is available on
  575. * www.amd.com
  576. */
  577. printk(KERN_ERR PFX "BIOS error - no PSB or ACPI _PSS objects\n");
  578. return -ENODEV;
  579. }
  580. #ifdef CONFIG_X86_POWERNOW_K8_ACPI
  581. static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index)
  582. {
  583. if (!data->acpi_data.state_count)
  584. return;
  585. data->irt = (data->acpi_data.states[index].control >> IRT_SHIFT) & IRT_MASK;
  586. data->rvo = (data->acpi_data.states[index].control >> RVO_SHIFT) & RVO_MASK;
  587. data->exttype = (data->acpi_data.states[index].control >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
  588. data->plllock = (data->acpi_data.states[index].control >> PLL_L_SHIFT) & PLL_L_MASK;
  589. data->vidmvs = 1 << ((data->acpi_data.states[index].control >> MVS_SHIFT) & MVS_MASK);
  590. data->vstable = (data->acpi_data.states[index].control >> VST_SHIFT) & VST_MASK;
  591. }
  592. static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
  593. {
  594. int i;
  595. int cntlofreq = 0;
  596. struct cpufreq_frequency_table *powernow_table;
  597. if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
  598. dprintk("register performance failed: bad ACPI data\n");
  599. return -EIO;
  600. }
  601. /* verify the data contained in the ACPI structures */
  602. if (data->acpi_data.state_count <= 1) {
  603. dprintk("No ACPI P-States\n");
  604. goto err_out;
  605. }
  606. if ((data->acpi_data.control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
  607. (data->acpi_data.status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
  608. dprintk("Invalid control/status registers (%x - %x)\n",
  609. data->acpi_data.control_register.space_id,
  610. data->acpi_data.status_register.space_id);
  611. goto err_out;
  612. }
  613. /* fill in data->powernow_table */
  614. powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
  615. * (data->acpi_data.state_count + 1)), GFP_KERNEL);
  616. if (!powernow_table) {
  617. dprintk("powernow_table memory alloc failure\n");
  618. goto err_out;
  619. }
  620. for (i = 0; i < data->acpi_data.state_count; i++) {
  621. u32 fid;
  622. u32 vid;
  623. if (data->exttype) {
  624. fid = data->acpi_data.states[i].status & FID_MASK;
  625. vid = (data->acpi_data.states[i].status >> VID_SHIFT) & VID_MASK;
  626. } else {
  627. fid = data->acpi_data.states[i].control & FID_MASK;
  628. vid = (data->acpi_data.states[i].control >> VID_SHIFT) & VID_MASK;
  629. }
  630. dprintk(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
  631. powernow_table[i].index = fid; /* lower 8 bits */
  632. powernow_table[i].index |= (vid << 8); /* upper 8 bits */
  633. powernow_table[i].frequency = find_khz_freq_from_fid(fid);
  634. /* verify frequency is OK */
  635. if ((powernow_table[i].frequency > (MAX_FREQ * 1000)) ||
  636. (powernow_table[i].frequency < (MIN_FREQ * 1000))) {
  637. dprintk("invalid freq %u kHz, ignoring\n", powernow_table[i].frequency);
  638. powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  639. continue;
  640. }
  641. /* verify voltage is OK - BIOSs are using "off" to indicate invalid */
  642. if (vid == VID_OFF) {
  643. dprintk("invalid vid %u, ignoring\n", vid);
  644. powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  645. continue;
  646. }
  647. /* verify only 1 entry from the lo frequency table */
  648. if (fid < HI_FID_TABLE_BOTTOM) {
  649. if (cntlofreq) {
  650. /* if both entries are the same, ignore this one ... */
  651. if ((powernow_table[i].frequency != powernow_table[cntlofreq].frequency) ||
  652. (powernow_table[i].index != powernow_table[cntlofreq].index)) {
  653. printk(KERN_ERR PFX "Too many lo freq table entries\n");
  654. goto err_out_mem;
  655. }
  656. dprintk("double low frequency table entry, ignoring it.\n");
  657. powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  658. continue;
  659. } else
  660. cntlofreq = i;
  661. }
  662. if (powernow_table[i].frequency != (data->acpi_data.states[i].core_frequency * 1000)) {
  663. printk(KERN_INFO PFX "invalid freq entries %u kHz vs. %u kHz\n",
  664. powernow_table[i].frequency,
  665. (unsigned int) (data->acpi_data.states[i].core_frequency * 1000));
  666. powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  667. continue;
  668. }
  669. }
  670. powernow_table[data->acpi_data.state_count].frequency = CPUFREQ_TABLE_END;
  671. powernow_table[data->acpi_data.state_count].index = 0;
  672. data->powernow_table = powernow_table;
  673. /* fill in data */
  674. data->numps = data->acpi_data.state_count;
  675. print_basics(data);
  676. powernow_k8_acpi_pst_values(data, 0);
  677. /* notify BIOS that we exist */
  678. acpi_processor_notify_smm(THIS_MODULE);
  679. return 0;
  680. err_out_mem:
  681. kfree(powernow_table);
  682. err_out:
  683. acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
  684. /* data->acpi_data.state_count informs us at ->exit() whether ACPI was used */
  685. data->acpi_data.state_count = 0;
  686. return -ENODEV;
  687. }
  688. static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
  689. {
  690. if (data->acpi_data.state_count)
  691. acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
  692. }
  693. #else
  694. static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data) { return -ENODEV; }
  695. static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data) { return; }
  696. static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index) { return; }
  697. #endif /* CONFIG_X86_POWERNOW_K8_ACPI */
  698. /* Take a frequency, and issue the fid/vid transition command */
  699. static int transition_frequency(struct powernow_k8_data *data, unsigned int index)
  700. {
  701. u32 fid;
  702. u32 vid;
  703. int res, i;
  704. struct cpufreq_freqs freqs;
  705. dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
  706. /* fid are the lower 8 bits of the index we stored into
  707. * the cpufreq frequency table in find_psb_table, vid are
  708. * the upper 8 bits.
  709. */
  710. fid = data->powernow_table[index].index & 0xFF;
  711. vid = (data->powernow_table[index].index & 0xFF00) >> 8;
  712. dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
  713. if (query_current_values_with_pending_wait(data))
  714. return 1;
  715. if ((data->currvid == vid) && (data->currfid == fid)) {
  716. dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
  717. fid, vid);
  718. return 0;
  719. }
  720. if ((fid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
  721. printk(KERN_ERR PFX
  722. "ignoring illegal change in lo freq table-%x to 0x%x\n",
  723. data->currfid, fid);
  724. return 1;
  725. }
  726. dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
  727. smp_processor_id(), fid, vid);
  728. freqs.cpu = data->cpu;
  729. freqs.old = find_khz_freq_from_fid(data->currfid);
  730. freqs.new = find_khz_freq_from_fid(fid);
  731. for_each_cpu_mask(i, cpu_core_map[data->cpu]) {
  732. freqs.cpu = i;
  733. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  734. }
  735. res = transition_fid_vid(data, fid, vid);
  736. freqs.new = find_khz_freq_from_fid(data->currfid);
  737. for_each_cpu_mask(i, cpu_core_map[data->cpu]) {
  738. freqs.cpu = i;
  739. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  740. }
  741. return res;
  742. }
  743. /* Driver entry point to switch to the target frequency */
  744. static int powernowk8_target(struct cpufreq_policy *pol, unsigned targfreq, unsigned relation)
  745. {
  746. cpumask_t oldmask = CPU_MASK_ALL;
  747. struct powernow_k8_data *data = powernow_data[pol->cpu];
  748. u32 checkfid = data->currfid;
  749. u32 checkvid = data->currvid;
  750. unsigned int newstate;
  751. int ret = -EIO;
  752. /* only run on specific CPU from here on */
  753. oldmask = current->cpus_allowed;
  754. set_cpus_allowed(current, cpumask_of_cpu(pol->cpu));
  755. if (smp_processor_id() != pol->cpu) {
  756. printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
  757. goto err_out;
  758. }
  759. if (pending_bit_stuck()) {
  760. printk(KERN_ERR PFX "failing targ, change pending bit set\n");
  761. goto err_out;
  762. }
  763. dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
  764. pol->cpu, targfreq, pol->min, pol->max, relation);
  765. if (query_current_values_with_pending_wait(data)) {
  766. ret = -EIO;
  767. goto err_out;
  768. }
  769. dprintk("targ: curr fid 0x%x, vid 0x%x\n",
  770. data->currfid, data->currvid);
  771. if ((checkvid != data->currvid) || (checkfid != data->currfid)) {
  772. printk(KERN_INFO PFX
  773. "error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
  774. checkfid, data->currfid, checkvid, data->currvid);
  775. }
  776. if (cpufreq_frequency_table_target(pol, data->powernow_table, targfreq, relation, &newstate))
  777. goto err_out;
  778. down(&fidvid_sem);
  779. powernow_k8_acpi_pst_values(data, newstate);
  780. if (transition_frequency(data, newstate)) {
  781. printk(KERN_ERR PFX "transition frequency failed\n");
  782. ret = 1;
  783. up(&fidvid_sem);
  784. goto err_out;
  785. }
  786. up(&fidvid_sem);
  787. pol->cur = find_khz_freq_from_fid(data->currfid);
  788. ret = 0;
  789. err_out:
  790. set_cpus_allowed(current, oldmask);
  791. return ret;
  792. }
  793. /* Driver entry point to verify the policy and range of frequencies */
  794. static int powernowk8_verify(struct cpufreq_policy *pol)
  795. {
  796. struct powernow_k8_data *data = powernow_data[pol->cpu];
  797. return cpufreq_frequency_table_verify(pol, data->powernow_table);
  798. }
  799. /* per CPU init entry point to the driver */
  800. static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
  801. {
  802. struct powernow_k8_data *data;
  803. cpumask_t oldmask = CPU_MASK_ALL;
  804. int rc, i;
  805. if (!cpu_online(pol->cpu))
  806. return -ENODEV;
  807. if (!check_supported_cpu(pol->cpu))
  808. return -ENODEV;
  809. data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
  810. if (!data) {
  811. printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
  812. return -ENOMEM;
  813. }
  814. data->cpu = pol->cpu;
  815. if (powernow_k8_cpu_init_acpi(data)) {
  816. /*
  817. * Use the PSB BIOS structure. This is only availabe on
  818. * an UP version, and is deprecated by AMD.
  819. */
  820. if ((num_online_cpus() != 1) || (num_possible_cpus() != 1)) {
  821. printk(KERN_ERR PFX "MP systems not supported by PSB BIOS structure\n");
  822. kfree(data);
  823. return -ENODEV;
  824. }
  825. if (pol->cpu != 0) {
  826. printk(KERN_ERR PFX "init not cpu 0\n");
  827. kfree(data);
  828. return -ENODEV;
  829. }
  830. rc = find_psb_table(data);
  831. if (rc) {
  832. kfree(data);
  833. return -ENODEV;
  834. }
  835. }
  836. /* only run on specific CPU from here on */
  837. oldmask = current->cpus_allowed;
  838. set_cpus_allowed(current, cpumask_of_cpu(pol->cpu));
  839. if (smp_processor_id() != pol->cpu) {
  840. printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
  841. goto err_out;
  842. }
  843. if (pending_bit_stuck()) {
  844. printk(KERN_ERR PFX "failing init, change pending bit set\n");
  845. goto err_out;
  846. }
  847. if (query_current_values_with_pending_wait(data))
  848. goto err_out;
  849. fidvid_msr_init();
  850. /* run on any CPU again */
  851. set_cpus_allowed(current, oldmask);
  852. pol->governor = CPUFREQ_DEFAULT_GOVERNOR;
  853. pol->cpus = cpu_core_map[pol->cpu];
  854. /* Take a crude guess here.
  855. * That guess was in microseconds, so multiply with 1000 */
  856. pol->cpuinfo.transition_latency = (((data->rvo + 8) * data->vstable * VST_UNITS_20US)
  857. + (3 * (1 << data->irt) * 10)) * 1000;
  858. pol->cur = find_khz_freq_from_fid(data->currfid);
  859. dprintk("policy current frequency %d kHz\n", pol->cur);
  860. /* min/max the cpu is capable of */
  861. if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
  862. printk(KERN_ERR PFX "invalid powernow_table\n");
  863. powernow_k8_cpu_exit_acpi(data);
  864. kfree(data->powernow_table);
  865. kfree(data);
  866. return -EINVAL;
  867. }
  868. cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
  869. printk("cpu_init done, current fid 0x%x, vid 0x%x\n",
  870. data->currfid, data->currvid);
  871. for_each_cpu_mask(i, cpu_core_map[pol->cpu])
  872. powernow_data[i] = data;
  873. return 0;
  874. err_out:
  875. set_cpus_allowed(current, oldmask);
  876. powernow_k8_cpu_exit_acpi(data);
  877. kfree(data);
  878. return -ENODEV;
  879. }
  880. static int __devexit powernowk8_cpu_exit (struct cpufreq_policy *pol)
  881. {
  882. struct powernow_k8_data *data = powernow_data[pol->cpu];
  883. if (!data)
  884. return -EINVAL;
  885. powernow_k8_cpu_exit_acpi(data);
  886. cpufreq_frequency_table_put_attr(pol->cpu);
  887. kfree(data->powernow_table);
  888. kfree(data);
  889. return 0;
  890. }
  891. static unsigned int powernowk8_get (unsigned int cpu)
  892. {
  893. struct powernow_k8_data *data = powernow_data[cpu];
  894. cpumask_t oldmask = current->cpus_allowed;
  895. unsigned int khz = 0;
  896. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  897. if (smp_processor_id() != cpu) {
  898. printk(KERN_ERR PFX "limiting to CPU %d failed in powernowk8_get\n", cpu);
  899. set_cpus_allowed(current, oldmask);
  900. return 0;
  901. }
  902. if (query_current_values_with_pending_wait(data))
  903. goto out;
  904. khz = find_khz_freq_from_fid(data->currfid);
  905. out:
  906. set_cpus_allowed(current, oldmask);
  907. return khz;
  908. }
  909. static struct freq_attr* powernow_k8_attr[] = {
  910. &cpufreq_freq_attr_scaling_available_freqs,
  911. NULL,
  912. };
  913. static struct cpufreq_driver cpufreq_amd64_driver = {
  914. .verify = powernowk8_verify,
  915. .target = powernowk8_target,
  916. .init = powernowk8_cpu_init,
  917. .exit = __devexit_p(powernowk8_cpu_exit),
  918. .get = powernowk8_get,
  919. .name = "powernow-k8",
  920. .owner = THIS_MODULE,
  921. .attr = powernow_k8_attr,
  922. };
  923. /* driver entry point for init */
  924. static int __cpuinit powernowk8_init(void)
  925. {
  926. unsigned int i, supported_cpus = 0;
  927. for_each_online_cpu(i) {
  928. if (check_supported_cpu(i))
  929. supported_cpus++;
  930. }
  931. if (supported_cpus == num_online_cpus()) {
  932. printk(KERN_INFO PFX "Found %d AMD Athlon 64 / Opteron "
  933. "processors (" VERSION ")\n", supported_cpus);
  934. return cpufreq_register_driver(&cpufreq_amd64_driver);
  935. }
  936. return -ENODEV;
  937. }
  938. /* driver entry point for term */
  939. static void __exit powernowk8_exit(void)
  940. {
  941. dprintk("exit\n");
  942. cpufreq_unregister_driver(&cpufreq_amd64_driver);
  943. }
  944. MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and Mark Langsdorf <mark.langsdorf@amd.com>");
  945. MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
  946. MODULE_LICENSE("GPL");
  947. late_initcall(powernowk8_init);
  948. module_exit(powernowk8_exit);