powernow-k8.c 36 KB

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