powernow-k8.c 37 KB

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