powernow-k8.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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. #include <linux/acpi.h>
  39. #include <linux/mutex.h>
  40. #include <acpi/processor.h>
  41. #define PFX "powernow-k8: "
  42. #define VERSION "version 2.20.00"
  43. #include "powernow-k8.h"
  44. /* serialize freq changes */
  45. static DEFINE_MUTEX(fidvid_mutex);
  46. static DEFINE_PER_CPU(struct powernow_k8_data *, powernow_data);
  47. static int cpu_family = CPU_OPTERON;
  48. #ifndef CONFIG_SMP
  49. static inline const struct cpumask *cpu_core_mask(int cpu)
  50. {
  51. return cpumask_of(0);
  52. }
  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,
  65. u32 pstate)
  66. {
  67. return data[pstate].frequency;
  68. }
  69. /* Return the vco fid for an input fid
  70. *
  71. * Each "low" fid has corresponding "high" fid, and you can get to "low" fids
  72. * only from corresponding high fids. This returns "high" fid corresponding to
  73. * "low" one.
  74. */
  75. static u32 convert_fid_to_vco_fid(u32 fid)
  76. {
  77. if (fid < HI_FID_TABLE_BOTTOM)
  78. return 8 + (2 * fid);
  79. else
  80. return fid;
  81. }
  82. /*
  83. * Return 1 if the pending bit is set. Unless we just instructed the processor
  84. * to transition to a new state, seeing this bit set is really bad news.
  85. */
  86. static int pending_bit_stuck(void)
  87. {
  88. u32 lo, hi;
  89. if (cpu_family == CPU_HW_PSTATE)
  90. return 0;
  91. rdmsr(MSR_FIDVID_STATUS, lo, hi);
  92. return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0;
  93. }
  94. /*
  95. * Update the global current fid / vid values from the status msr.
  96. * Returns 1 on error.
  97. */
  98. static int query_current_values_with_pending_wait(struct powernow_k8_data *data)
  99. {
  100. u32 lo, hi;
  101. u32 i = 0;
  102. if (cpu_family == CPU_HW_PSTATE) {
  103. if (data->currpstate == HW_PSTATE_INVALID) {
  104. /* read (initial) hw pstate if not yet set */
  105. rdmsr(MSR_PSTATE_STATUS, lo, hi);
  106. i = lo & HW_PSTATE_MASK;
  107. /*
  108. * a workaround for family 11h erratum 311 might cause
  109. * an "out-of-range Pstate if the core is in Pstate-0
  110. */
  111. if (i >= data->numps)
  112. data->currpstate = HW_PSTATE_0;
  113. else
  114. data->currpstate = i;
  115. }
  116. return 0;
  117. }
  118. do {
  119. if (i++ > 10000) {
  120. dprintk("detected change pending stuck\n");
  121. return 1;
  122. }
  123. rdmsr(MSR_FIDVID_STATUS, lo, hi);
  124. } while (lo & MSR_S_LO_CHANGE_PENDING);
  125. data->currvid = hi & MSR_S_HI_CURRENT_VID;
  126. data->currfid = lo & MSR_S_LO_CURRENT_FID;
  127. return 0;
  128. }
  129. /* the isochronous relief time */
  130. static void count_off_irt(struct powernow_k8_data *data)
  131. {
  132. udelay((1 << data->irt) * 10);
  133. return;
  134. }
  135. /* the voltage stabilization time */
  136. static void count_off_vst(struct powernow_k8_data *data)
  137. {
  138. udelay(data->vstable * VST_UNITS_20US);
  139. return;
  140. }
  141. /* need to init the control msr to a safe value (for each cpu) */
  142. static void fidvid_msr_init(void)
  143. {
  144. u32 lo, hi;
  145. u8 fid, vid;
  146. rdmsr(MSR_FIDVID_STATUS, lo, hi);
  147. vid = hi & MSR_S_HI_CURRENT_VID;
  148. fid = lo & MSR_S_LO_CURRENT_FID;
  149. lo = fid | (vid << MSR_C_LO_VID_SHIFT);
  150. hi = MSR_C_HI_STP_GNT_BENIGN;
  151. dprintk("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi);
  152. wrmsr(MSR_FIDVID_CTL, lo, hi);
  153. }
  154. /* write the new fid value along with the other control fields to the msr */
  155. static int write_new_fid(struct powernow_k8_data *data, u32 fid)
  156. {
  157. u32 lo;
  158. u32 savevid = data->currvid;
  159. u32 i = 0;
  160. if ((fid & INVALID_FID_MASK) || (data->currvid & INVALID_VID_MASK)) {
  161. printk(KERN_ERR PFX "internal error - overflow on fid write\n");
  162. return 1;
  163. }
  164. lo = fid;
  165. lo |= (data->currvid << MSR_C_LO_VID_SHIFT);
  166. lo |= MSR_C_LO_INIT_FID_VID;
  167. dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
  168. fid, lo, data->plllock * PLL_LOCK_CONVERSION);
  169. do {
  170. wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
  171. if (i++ > 100) {
  172. printk(KERN_ERR PFX
  173. "Hardware error - pending bit very stuck - "
  174. "no further pstate changes possible\n");
  175. return 1;
  176. }
  177. } while (query_current_values_with_pending_wait(data));
  178. count_off_irt(data);
  179. if (savevid != data->currvid) {
  180. printk(KERN_ERR PFX
  181. "vid change on fid trans, old 0x%x, new 0x%x\n",
  182. savevid, data->currvid);
  183. return 1;
  184. }
  185. if (fid != data->currfid) {
  186. printk(KERN_ERR PFX
  187. "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
  188. data->currfid);
  189. return 1;
  190. }
  191. return 0;
  192. }
  193. /* Write a new vid to the hardware */
  194. static int write_new_vid(struct powernow_k8_data *data, u32 vid)
  195. {
  196. u32 lo;
  197. u32 savefid = data->currfid;
  198. int i = 0;
  199. if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
  200. printk(KERN_ERR PFX "internal error - overflow on vid write\n");
  201. return 1;
  202. }
  203. lo = data->currfid;
  204. lo |= (vid << MSR_C_LO_VID_SHIFT);
  205. lo |= MSR_C_LO_INIT_FID_VID;
  206. dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
  207. vid, lo, STOP_GRANT_5NS);
  208. do {
  209. wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
  210. if (i++ > 100) {
  211. printk(KERN_ERR PFX "internal error - pending bit "
  212. "very stuck - no further pstate "
  213. "changes possible\n");
  214. return 1;
  215. }
  216. } while (query_current_values_with_pending_wait(data));
  217. if (savefid != data->currfid) {
  218. printk(KERN_ERR PFX "fid changed on vid trans, old "
  219. "0x%x new 0x%x\n",
  220. savefid, data->currfid);
  221. return 1;
  222. }
  223. if (vid != data->currvid) {
  224. printk(KERN_ERR PFX "vid trans failed, vid 0x%x, "
  225. "curr 0x%x\n",
  226. vid, data->currvid);
  227. return 1;
  228. }
  229. return 0;
  230. }
  231. /*
  232. * Reduce the vid by the max of step or reqvid.
  233. * Decreasing vid codes represent increasing voltages:
  234. * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off.
  235. */
  236. static int decrease_vid_code_by_step(struct powernow_k8_data *data,
  237. u32 reqvid, u32 step)
  238. {
  239. if ((data->currvid - reqvid) > step)
  240. reqvid = data->currvid - step;
  241. if (write_new_vid(data, reqvid))
  242. return 1;
  243. count_off_vst(data);
  244. return 0;
  245. }
  246. /* Change hardware pstate by single MSR write */
  247. static int transition_pstate(struct powernow_k8_data *data, u32 pstate)
  248. {
  249. wrmsr(MSR_PSTATE_CTRL, pstate, 0);
  250. data->currpstate = pstate;
  251. return 0;
  252. }
  253. /* Change Opteron/Athlon64 fid and vid, by the 3 phases. */
  254. static int transition_fid_vid(struct powernow_k8_data *data,
  255. u32 reqfid, u32 reqvid)
  256. {
  257. if (core_voltage_pre_transition(data, reqvid))
  258. return 1;
  259. if (core_frequency_transition(data, reqfid))
  260. return 1;
  261. if (core_voltage_post_transition(data, reqvid))
  262. return 1;
  263. if (query_current_values_with_pending_wait(data))
  264. return 1;
  265. if ((reqfid != data->currfid) || (reqvid != data->currvid)) {
  266. printk(KERN_ERR PFX "failed (cpu%d): req 0x%x 0x%x, "
  267. "curr 0x%x 0x%x\n",
  268. smp_processor_id(),
  269. reqfid, reqvid, data->currfid, data->currvid);
  270. return 1;
  271. }
  272. dprintk("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
  273. smp_processor_id(), data->currfid, data->currvid);
  274. return 0;
  275. }
  276. /* Phase 1 - core voltage transition ... setup voltage */
  277. static int core_voltage_pre_transition(struct powernow_k8_data *data,
  278. u32 reqvid)
  279. {
  280. u32 rvosteps = data->rvo;
  281. u32 savefid = data->currfid;
  282. u32 maxvid, lo;
  283. dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, "
  284. "reqvid 0x%x, rvo 0x%x\n",
  285. smp_processor_id(),
  286. data->currfid, data->currvid, reqvid, data->rvo);
  287. rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
  288. maxvid = 0x1f & (maxvid >> 16);
  289. dprintk("ph1 maxvid=0x%x\n", maxvid);
  290. if (reqvid < maxvid) /* lower numbers are higher voltages */
  291. reqvid = maxvid;
  292. while (data->currvid > reqvid) {
  293. dprintk("ph1: curr 0x%x, req vid 0x%x\n",
  294. data->currvid, reqvid);
  295. if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
  296. return 1;
  297. }
  298. while ((rvosteps > 0) && ((data->rvo + data->currvid) > reqvid)) {
  299. if (data->currvid == maxvid) {
  300. rvosteps = 0;
  301. } else {
  302. dprintk("ph1: changing vid for rvo, req 0x%x\n",
  303. data->currvid - 1);
  304. if (decrease_vid_code_by_step(data, data->currvid-1, 1))
  305. return 1;
  306. rvosteps--;
  307. }
  308. }
  309. if (query_current_values_with_pending_wait(data))
  310. return 1;
  311. if (savefid != data->currfid) {
  312. printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n",
  313. data->currfid);
  314. return 1;
  315. }
  316. dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
  317. data->currfid, data->currvid);
  318. return 0;
  319. }
  320. /* Phase 2 - core frequency transition */
  321. static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
  322. {
  323. u32 vcoreqfid, vcocurrfid, vcofiddiff;
  324. u32 fid_interval, savevid = data->currvid;
  325. if ((reqfid < HI_FID_TABLE_BOTTOM) &&
  326. (data->currfid < HI_FID_TABLE_BOTTOM)) {
  327. printk(KERN_ERR PFX "ph2: illegal lo-lo transition "
  328. "0x%x 0x%x\n", reqfid, data->currfid);
  329. return 1;
  330. }
  331. if (data->currfid == reqfid) {
  332. printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n",
  333. data->currfid);
  334. return 0;
  335. }
  336. dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, "
  337. "reqfid 0x%x\n",
  338. smp_processor_id(),
  339. data->currfid, data->currvid, reqfid);
  340. vcoreqfid = convert_fid_to_vco_fid(reqfid);
  341. vcocurrfid = convert_fid_to_vco_fid(data->currfid);
  342. vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
  343. : vcoreqfid - vcocurrfid;
  344. while (vcofiddiff > 2) {
  345. (data->currfid & 1) ? (fid_interval = 1) : (fid_interval = 2);
  346. if (reqfid > data->currfid) {
  347. if (data->currfid > LO_FID_TABLE_TOP) {
  348. if (write_new_fid(data,
  349. data->currfid + fid_interval))
  350. return 1;
  351. } else {
  352. if (write_new_fid
  353. (data,
  354. 2 + convert_fid_to_vco_fid(data->currfid)))
  355. return 1;
  356. }
  357. } else {
  358. if (write_new_fid(data, data->currfid - fid_interval))
  359. return 1;
  360. }
  361. vcocurrfid = convert_fid_to_vco_fid(data->currfid);
  362. vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
  363. : vcoreqfid - vcocurrfid;
  364. }
  365. if (write_new_fid(data, reqfid))
  366. return 1;
  367. if (query_current_values_with_pending_wait(data))
  368. return 1;
  369. if (data->currfid != reqfid) {
  370. printk(KERN_ERR PFX
  371. "ph2: mismatch, failed fid transition, "
  372. "curr 0x%x, req 0x%x\n",
  373. data->currfid, reqfid);
  374. return 1;
  375. }
  376. if (savevid != data->currvid) {
  377. printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
  378. savevid, data->currvid);
  379. return 1;
  380. }
  381. dprintk("ph2 complete, currfid 0x%x, currvid 0x%x\n",
  382. data->currfid, data->currvid);
  383. return 0;
  384. }
  385. /* Phase 3 - core voltage transition flow ... jump to the final vid. */
  386. static int core_voltage_post_transition(struct powernow_k8_data *data,
  387. u32 reqvid)
  388. {
  389. u32 savefid = data->currfid;
  390. u32 savereqvid = reqvid;
  391. dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
  392. smp_processor_id(),
  393. data->currfid, data->currvid);
  394. if (reqvid != data->currvid) {
  395. if (write_new_vid(data, reqvid))
  396. return 1;
  397. if (savefid != data->currfid) {
  398. printk(KERN_ERR PFX
  399. "ph3: bad fid change, save 0x%x, curr 0x%x\n",
  400. savefid, data->currfid);
  401. return 1;
  402. }
  403. if (data->currvid != reqvid) {
  404. printk(KERN_ERR PFX
  405. "ph3: failed vid transition\n, "
  406. "req 0x%x, curr 0x%x",
  407. reqvid, data->currvid);
  408. return 1;
  409. }
  410. }
  411. if (query_current_values_with_pending_wait(data))
  412. return 1;
  413. if (savereqvid != data->currvid) {
  414. dprintk("ph3 failed, currvid 0x%x\n", data->currvid);
  415. return 1;
  416. }
  417. if (savefid != data->currfid) {
  418. dprintk("ph3 failed, currfid changed 0x%x\n",
  419. data->currfid);
  420. return 1;
  421. }
  422. dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
  423. data->currfid, data->currvid);
  424. return 0;
  425. }
  426. static int check_supported_cpu(unsigned int cpu)
  427. {
  428. cpumask_t oldmask;
  429. u32 eax, ebx, ecx, edx;
  430. unsigned int rc = 0;
  431. oldmask = current->cpus_allowed;
  432. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  433. if (smp_processor_id() != cpu) {
  434. printk(KERN_ERR PFX "limiting to cpu %u failed\n", cpu);
  435. goto out;
  436. }
  437. if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
  438. goto out;
  439. eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
  440. if (((eax & CPUID_XFAM) != CPUID_XFAM_K8) &&
  441. ((eax & CPUID_XFAM) < CPUID_XFAM_10H))
  442. goto out;
  443. if ((eax & CPUID_XFAM) == CPUID_XFAM_K8) {
  444. if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
  445. ((eax & CPUID_XMOD) > CPUID_XMOD_REV_MASK)) {
  446. printk(KERN_INFO PFX
  447. "Processor cpuid %x not supported\n", eax);
  448. goto out;
  449. }
  450. eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
  451. if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
  452. printk(KERN_INFO PFX
  453. "No frequency change capabilities detected\n");
  454. goto out;
  455. }
  456. cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
  457. if ((edx & P_STATE_TRANSITION_CAPABLE)
  458. != P_STATE_TRANSITION_CAPABLE) {
  459. printk(KERN_INFO PFX
  460. "Power state transitions not supported\n");
  461. goto out;
  462. }
  463. } else { /* must be a HW Pstate capable processor */
  464. cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
  465. if ((edx & USE_HW_PSTATE) == USE_HW_PSTATE)
  466. cpu_family = CPU_HW_PSTATE;
  467. else
  468. goto out;
  469. }
  470. rc = 1;
  471. out:
  472. set_cpus_allowed_ptr(current, &oldmask);
  473. return rc;
  474. }
  475. static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst,
  476. u8 maxvid)
  477. {
  478. unsigned int j;
  479. u8 lastfid = 0xff;
  480. for (j = 0; j < data->numps; j++) {
  481. if (pst[j].vid > LEAST_VID) {
  482. printk(KERN_ERR FW_BUG PFX "vid %d invalid : 0x%x\n",
  483. j, pst[j].vid);
  484. return -EINVAL;
  485. }
  486. if (pst[j].vid < data->rvo) {
  487. /* vid + rvo >= 0 */
  488. printk(KERN_ERR FW_BUG PFX "0 vid exceeded with pstate"
  489. " %d\n", j);
  490. return -ENODEV;
  491. }
  492. if (pst[j].vid < maxvid + data->rvo) {
  493. /* vid + rvo >= maxvid */
  494. printk(KERN_ERR FW_BUG PFX "maxvid exceeded with pstate"
  495. " %d\n", j);
  496. return -ENODEV;
  497. }
  498. if (pst[j].fid > MAX_FID) {
  499. printk(KERN_ERR FW_BUG PFX "maxfid exceeded with pstate"
  500. " %d\n", j);
  501. return -ENODEV;
  502. }
  503. if (j && (pst[j].fid < HI_FID_TABLE_BOTTOM)) {
  504. /* Only first fid is allowed to be in "low" range */
  505. printk(KERN_ERR FW_BUG PFX "two low fids - %d : "
  506. "0x%x\n", j, pst[j].fid);
  507. return -EINVAL;
  508. }
  509. if (pst[j].fid < lastfid)
  510. lastfid = pst[j].fid;
  511. }
  512. if (lastfid & 1) {
  513. printk(KERN_ERR FW_BUG PFX "lastfid invalid\n");
  514. return -EINVAL;
  515. }
  516. if (lastfid > LO_FID_TABLE_TOP)
  517. printk(KERN_INFO FW_BUG PFX
  518. "first fid not from lo freq table\n");
  519. return 0;
  520. }
  521. static void invalidate_entry(struct powernow_k8_data *data, unsigned int entry)
  522. {
  523. data->powernow_table[entry].frequency = CPUFREQ_ENTRY_INVALID;
  524. }
  525. static void print_basics(struct powernow_k8_data *data)
  526. {
  527. int j;
  528. for (j = 0; j < data->numps; j++) {
  529. if (data->powernow_table[j].frequency !=
  530. CPUFREQ_ENTRY_INVALID) {
  531. if (cpu_family == CPU_HW_PSTATE) {
  532. printk(KERN_INFO PFX
  533. " %d : pstate %d (%d MHz)\n", j,
  534. data->powernow_table[j].index,
  535. data->powernow_table[j].frequency/1000);
  536. } else {
  537. printk(KERN_INFO PFX
  538. " %d : fid 0x%x (%d MHz), vid 0x%x\n",
  539. j,
  540. data->powernow_table[j].index & 0xff,
  541. data->powernow_table[j].frequency/1000,
  542. data->powernow_table[j].index >> 8);
  543. }
  544. }
  545. }
  546. if (data->batps)
  547. printk(KERN_INFO PFX "Only %d pstates on battery\n",
  548. data->batps);
  549. }
  550. static int fill_powernow_table(struct powernow_k8_data *data,
  551. struct pst_s *pst, u8 maxvid)
  552. {
  553. struct cpufreq_frequency_table *powernow_table;
  554. unsigned int j;
  555. if (data->batps) {
  556. /* use ACPI support to get full speed on mains power */
  557. printk(KERN_WARNING PFX
  558. "Only %d pstates usable (use ACPI driver for full "
  559. "range\n", data->batps);
  560. data->numps = data->batps;
  561. }
  562. for (j = 1; j < data->numps; j++) {
  563. if (pst[j-1].fid >= pst[j].fid) {
  564. printk(KERN_ERR PFX "PST out of sequence\n");
  565. return -EINVAL;
  566. }
  567. }
  568. if (data->numps < 2) {
  569. printk(KERN_ERR PFX "no p states to transition\n");
  570. return -ENODEV;
  571. }
  572. if (check_pst_table(data, pst, maxvid))
  573. return -EINVAL;
  574. powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
  575. * (data->numps + 1)), GFP_KERNEL);
  576. if (!powernow_table) {
  577. printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
  578. return -ENOMEM;
  579. }
  580. for (j = 0; j < data->numps; j++) {
  581. int freq;
  582. powernow_table[j].index = pst[j].fid; /* lower 8 bits */
  583. powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
  584. freq = find_khz_freq_from_fid(pst[j].fid);
  585. powernow_table[j].frequency = freq;
  586. }
  587. powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
  588. powernow_table[data->numps].index = 0;
  589. if (query_current_values_with_pending_wait(data)) {
  590. kfree(powernow_table);
  591. return -EIO;
  592. }
  593. dprintk("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
  594. data->powernow_table = powernow_table;
  595. if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
  596. print_basics(data);
  597. for (j = 0; j < data->numps; j++)
  598. if ((pst[j].fid == data->currfid) &&
  599. (pst[j].vid == data->currvid))
  600. return 0;
  601. dprintk("currfid/vid do not match PST, ignoring\n");
  602. return 0;
  603. }
  604. /* Find and validate the PSB/PST table in BIOS. */
  605. static int find_psb_table(struct powernow_k8_data *data)
  606. {
  607. struct psb_s *psb;
  608. unsigned int i;
  609. u32 mvs;
  610. u8 maxvid;
  611. u32 cpst = 0;
  612. u32 thiscpuid;
  613. for (i = 0xc0000; i < 0xffff0; i += 0x10) {
  614. /* Scan BIOS looking for the signature. */
  615. /* It can not be at ffff0 - it is too big. */
  616. psb = phys_to_virt(i);
  617. if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
  618. continue;
  619. dprintk("found PSB header at 0x%p\n", psb);
  620. dprintk("table vers: 0x%x\n", psb->tableversion);
  621. if (psb->tableversion != PSB_VERSION_1_4) {
  622. printk(KERN_ERR FW_BUG PFX "PSB table is not v1.4\n");
  623. return -ENODEV;
  624. }
  625. dprintk("flags: 0x%x\n", psb->flags1);
  626. if (psb->flags1) {
  627. printk(KERN_ERR FW_BUG PFX "unknown flags\n");
  628. return -ENODEV;
  629. }
  630. data->vstable = psb->vstable;
  631. dprintk("voltage stabilization time: %d(*20us)\n",
  632. data->vstable);
  633. dprintk("flags2: 0x%x\n", psb->flags2);
  634. data->rvo = psb->flags2 & 3;
  635. data->irt = ((psb->flags2) >> 2) & 3;
  636. mvs = ((psb->flags2) >> 4) & 3;
  637. data->vidmvs = 1 << mvs;
  638. data->batps = ((psb->flags2) >> 6) & 3;
  639. dprintk("ramp voltage offset: %d\n", data->rvo);
  640. dprintk("isochronous relief time: %d\n", data->irt);
  641. dprintk("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
  642. dprintk("numpst: 0x%x\n", psb->num_tables);
  643. cpst = psb->num_tables;
  644. if ((psb->cpuid == 0x00000fc0) ||
  645. (psb->cpuid == 0x00000fe0)) {
  646. thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
  647. if ((thiscpuid == 0x00000fc0) ||
  648. (thiscpuid == 0x00000fe0))
  649. cpst = 1;
  650. }
  651. if (cpst != 1) {
  652. printk(KERN_ERR FW_BUG PFX "numpst must be 1\n");
  653. return -ENODEV;
  654. }
  655. data->plllock = psb->plllocktime;
  656. dprintk("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
  657. dprintk("maxfid: 0x%x\n", psb->maxfid);
  658. dprintk("maxvid: 0x%x\n", psb->maxvid);
  659. maxvid = psb->maxvid;
  660. data->numps = psb->numps;
  661. dprintk("numpstates: 0x%x\n", data->numps);
  662. return fill_powernow_table(data,
  663. (struct pst_s *)(psb+1), maxvid);
  664. }
  665. /*
  666. * If you see this message, complain to BIOS manufacturer. If
  667. * he tells you "we do not support Linux" or some similar
  668. * nonsense, remember that Windows 2000 uses the same legacy
  669. * mechanism that the old Linux PSB driver uses. Tell them it
  670. * is broken with Windows 2000.
  671. *
  672. * The reference to the AMD documentation is chapter 9 in the
  673. * BIOS and Kernel Developer's Guide, which is available on
  674. * www.amd.com
  675. */
  676. printk(KERN_ERR FW_BUG PFX "No PSB or ACPI _PSS objects\n");
  677. return -ENODEV;
  678. }
  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 (cpumask_first(cpu_core_mask(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. /* Take a frequency, and issue the fid/vid transition command */
  873. static int transition_frequency_fidvid(struct powernow_k8_data *data,
  874. unsigned int index)
  875. {
  876. u32 fid = 0;
  877. u32 vid = 0;
  878. int res, i;
  879. struct cpufreq_freqs freqs;
  880. dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
  881. /* fid/vid correctness check for k8 */
  882. /* fid are the lower 8 bits of the index we stored into
  883. * the cpufreq frequency table in find_psb_table, vid
  884. * are the upper 8 bits.
  885. */
  886. fid = data->powernow_table[index].index & 0xFF;
  887. vid = (data->powernow_table[index].index & 0xFF00) >> 8;
  888. dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
  889. if (query_current_values_with_pending_wait(data))
  890. return 1;
  891. if ((data->currvid == vid) && (data->currfid == fid)) {
  892. dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
  893. fid, vid);
  894. return 0;
  895. }
  896. if ((fid < HI_FID_TABLE_BOTTOM) &&
  897. (data->currfid < HI_FID_TABLE_BOTTOM)) {
  898. printk(KERN_ERR PFX
  899. "ignoring illegal change in lo freq table-%x to 0x%x\n",
  900. data->currfid, fid);
  901. return 1;
  902. }
  903. dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
  904. smp_processor_id(), fid, vid);
  905. freqs.old = find_khz_freq_from_fid(data->currfid);
  906. freqs.new = find_khz_freq_from_fid(fid);
  907. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  908. freqs.cpu = i;
  909. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  910. }
  911. res = transition_fid_vid(data, fid, vid);
  912. freqs.new = find_khz_freq_from_fid(data->currfid);
  913. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  914. freqs.cpu = i;
  915. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  916. }
  917. return res;
  918. }
  919. /* Take a frequency, and issue the hardware pstate transition command */
  920. static int transition_frequency_pstate(struct powernow_k8_data *data,
  921. unsigned int index)
  922. {
  923. u32 pstate = 0;
  924. int res, i;
  925. struct cpufreq_freqs freqs;
  926. dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
  927. /* get MSR index for hardware pstate transition */
  928. pstate = index & HW_PSTATE_MASK;
  929. if (pstate > data->max_hw_pstate)
  930. return 0;
  931. freqs.old = find_khz_freq_from_pstate(data->powernow_table,
  932. data->currpstate);
  933. freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
  934. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  935. freqs.cpu = i;
  936. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  937. }
  938. res = transition_pstate(data, pstate);
  939. freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
  940. for_each_cpu_mask_nr(i, *(data->available_cores)) {
  941. freqs.cpu = i;
  942. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  943. }
  944. return res;
  945. }
  946. /* Driver entry point to switch to the target frequency */
  947. static int powernowk8_target(struct cpufreq_policy *pol,
  948. unsigned targfreq, unsigned relation)
  949. {
  950. cpumask_t oldmask;
  951. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  952. u32 checkfid;
  953. u32 checkvid;
  954. unsigned int newstate;
  955. int ret = -EIO;
  956. if (!data)
  957. return -EINVAL;
  958. checkfid = data->currfid;
  959. checkvid = data->currvid;
  960. /* only run on specific CPU from here on */
  961. oldmask = current->cpus_allowed;
  962. set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
  963. if (smp_processor_id() != pol->cpu) {
  964. printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
  965. goto err_out;
  966. }
  967. if (pending_bit_stuck()) {
  968. printk(KERN_ERR PFX "failing targ, change pending bit set\n");
  969. goto err_out;
  970. }
  971. dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
  972. pol->cpu, targfreq, pol->min, pol->max, relation);
  973. if (query_current_values_with_pending_wait(data))
  974. goto err_out;
  975. if (cpu_family != CPU_HW_PSTATE) {
  976. dprintk("targ: curr fid 0x%x, vid 0x%x\n",
  977. data->currfid, data->currvid);
  978. if ((checkvid != data->currvid) ||
  979. (checkfid != data->currfid)) {
  980. printk(KERN_INFO PFX
  981. "error - out of sync, fix 0x%x 0x%x, "
  982. "vid 0x%x 0x%x\n",
  983. checkfid, data->currfid,
  984. checkvid, data->currvid);
  985. }
  986. }
  987. if (cpufreq_frequency_table_target(pol, data->powernow_table,
  988. targfreq, relation, &newstate))
  989. goto err_out;
  990. mutex_lock(&fidvid_mutex);
  991. powernow_k8_acpi_pst_values(data, newstate);
  992. if (cpu_family == CPU_HW_PSTATE)
  993. ret = transition_frequency_pstate(data, newstate);
  994. else
  995. ret = transition_frequency_fidvid(data, newstate);
  996. if (ret) {
  997. printk(KERN_ERR PFX "transition frequency failed\n");
  998. ret = 1;
  999. mutex_unlock(&fidvid_mutex);
  1000. goto err_out;
  1001. }
  1002. mutex_unlock(&fidvid_mutex);
  1003. if (cpu_family == CPU_HW_PSTATE)
  1004. pol->cur = find_khz_freq_from_pstate(data->powernow_table,
  1005. newstate);
  1006. else
  1007. pol->cur = find_khz_freq_from_fid(data->currfid);
  1008. ret = 0;
  1009. err_out:
  1010. set_cpus_allowed_ptr(current, &oldmask);
  1011. return ret;
  1012. }
  1013. /* Driver entry point to verify the policy and range of frequencies */
  1014. static int powernowk8_verify(struct cpufreq_policy *pol)
  1015. {
  1016. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  1017. if (!data)
  1018. return -EINVAL;
  1019. return cpufreq_frequency_table_verify(pol, data->powernow_table);
  1020. }
  1021. /* per CPU init entry point to the driver */
  1022. static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
  1023. {
  1024. struct powernow_k8_data *data;
  1025. cpumask_t oldmask;
  1026. int rc;
  1027. static int print_once;
  1028. if (!cpu_online(pol->cpu))
  1029. return -ENODEV;
  1030. if (!check_supported_cpu(pol->cpu))
  1031. return -ENODEV;
  1032. data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
  1033. if (!data) {
  1034. printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
  1035. return -ENOMEM;
  1036. }
  1037. data->cpu = pol->cpu;
  1038. data->currpstate = HW_PSTATE_INVALID;
  1039. if (powernow_k8_cpu_init_acpi(data)) {
  1040. /*
  1041. * Use the PSB BIOS structure. This is only availabe on
  1042. * an UP version, and is deprecated by AMD.
  1043. */
  1044. if (num_online_cpus() != 1) {
  1045. /*
  1046. * Replace this one with print_once as soon as such a
  1047. * thing gets introduced
  1048. */
  1049. if (!print_once) {
  1050. WARN_ONCE(1, KERN_ERR FW_BUG PFX "Your BIOS "
  1051. "does not provide ACPI _PSS objects "
  1052. "in a way that Linux understands. "
  1053. "Please report this to the Linux ACPI"
  1054. " maintainers and complain to your "
  1055. "BIOS vendor.\n");
  1056. print_once++;
  1057. }
  1058. goto err_out;
  1059. }
  1060. if (pol->cpu != 0) {
  1061. printk(KERN_ERR FW_BUG PFX "No ACPI _PSS objects for "
  1062. "CPU other than CPU0. Complain to your BIOS "
  1063. "vendor.\n");
  1064. goto err_out;
  1065. }
  1066. rc = find_psb_table(data);
  1067. if (rc)
  1068. goto err_out;
  1069. /* Take a crude guess here.
  1070. * That guess was in microseconds, so multiply with 1000 */
  1071. pol->cpuinfo.transition_latency = (
  1072. ((data->rvo + 8) * data->vstable * VST_UNITS_20US) +
  1073. ((1 << data->irt) * 30)) * 1000;
  1074. } else /* ACPI _PSS objects available */
  1075. pol->cpuinfo.transition_latency = get_transition_latency(data);
  1076. /* only run on specific CPU from here on */
  1077. oldmask = current->cpus_allowed;
  1078. set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
  1079. if (smp_processor_id() != pol->cpu) {
  1080. printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
  1081. goto err_out_unmask;
  1082. }
  1083. if (pending_bit_stuck()) {
  1084. printk(KERN_ERR PFX "failing init, change pending bit set\n");
  1085. goto err_out_unmask;
  1086. }
  1087. if (query_current_values_with_pending_wait(data))
  1088. goto err_out_unmask;
  1089. if (cpu_family == CPU_OPTERON)
  1090. fidvid_msr_init();
  1091. /* run on any CPU again */
  1092. set_cpus_allowed_ptr(current, &oldmask);
  1093. if (cpu_family == CPU_HW_PSTATE)
  1094. cpumask_copy(pol->cpus, cpumask_of(pol->cpu));
  1095. else
  1096. cpumask_copy(pol->cpus, cpu_core_mask(pol->cpu));
  1097. data->available_cores = pol->cpus;
  1098. if (cpu_family == CPU_HW_PSTATE)
  1099. pol->cur = find_khz_freq_from_pstate(data->powernow_table,
  1100. data->currpstate);
  1101. else
  1102. pol->cur = find_khz_freq_from_fid(data->currfid);
  1103. dprintk("policy current frequency %d kHz\n", pol->cur);
  1104. /* min/max the cpu is capable of */
  1105. if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
  1106. printk(KERN_ERR FW_BUG PFX "invalid powernow_table\n");
  1107. powernow_k8_cpu_exit_acpi(data);
  1108. kfree(data->powernow_table);
  1109. kfree(data);
  1110. return -EINVAL;
  1111. }
  1112. cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
  1113. if (cpu_family == CPU_HW_PSTATE)
  1114. dprintk("cpu_init done, current pstate 0x%x\n",
  1115. data->currpstate);
  1116. else
  1117. dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
  1118. data->currfid, data->currvid);
  1119. per_cpu(powernow_data, pol->cpu) = data;
  1120. return 0;
  1121. err_out_unmask:
  1122. set_cpus_allowed_ptr(current, &oldmask);
  1123. powernow_k8_cpu_exit_acpi(data);
  1124. err_out:
  1125. kfree(data);
  1126. return -ENODEV;
  1127. }
  1128. static int __devexit powernowk8_cpu_exit(struct cpufreq_policy *pol)
  1129. {
  1130. struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
  1131. if (!data)
  1132. return -EINVAL;
  1133. powernow_k8_cpu_exit_acpi(data);
  1134. cpufreq_frequency_table_put_attr(pol->cpu);
  1135. kfree(data->powernow_table);
  1136. kfree(data);
  1137. return 0;
  1138. }
  1139. static unsigned int powernowk8_get(unsigned int cpu)
  1140. {
  1141. struct powernow_k8_data *data;
  1142. cpumask_t oldmask = current->cpus_allowed;
  1143. unsigned int khz = 0;
  1144. unsigned int first;
  1145. first = cpumask_first(cpu_core_mask(cpu));
  1146. data = per_cpu(powernow_data, first);
  1147. if (!data)
  1148. return -EINVAL;
  1149. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  1150. if (smp_processor_id() != cpu) {
  1151. printk(KERN_ERR PFX
  1152. "limiting to CPU %d failed in powernowk8_get\n", cpu);
  1153. set_cpus_allowed_ptr(current, &oldmask);
  1154. return 0;
  1155. }
  1156. if (query_current_values_with_pending_wait(data))
  1157. goto out;
  1158. if (cpu_family == CPU_HW_PSTATE)
  1159. khz = find_khz_freq_from_pstate(data->powernow_table,
  1160. data->currpstate);
  1161. else
  1162. khz = find_khz_freq_from_fid(data->currfid);
  1163. out:
  1164. set_cpus_allowed_ptr(current, &oldmask);
  1165. return khz;
  1166. }
  1167. static struct freq_attr *powernow_k8_attr[] = {
  1168. &cpufreq_freq_attr_scaling_available_freqs,
  1169. NULL,
  1170. };
  1171. static struct cpufreq_driver cpufreq_amd64_driver = {
  1172. .verify = powernowk8_verify,
  1173. .target = powernowk8_target,
  1174. .init = powernowk8_cpu_init,
  1175. .exit = __devexit_p(powernowk8_cpu_exit),
  1176. .get = powernowk8_get,
  1177. .name = "powernow-k8",
  1178. .owner = THIS_MODULE,
  1179. .attr = powernow_k8_attr,
  1180. };
  1181. /* driver entry point for init */
  1182. static int __cpuinit powernowk8_init(void)
  1183. {
  1184. unsigned int i, supported_cpus = 0;
  1185. for_each_online_cpu(i) {
  1186. if (check_supported_cpu(i))
  1187. supported_cpus++;
  1188. }
  1189. if (supported_cpus == num_online_cpus()) {
  1190. printk(KERN_INFO PFX "Found %d %s "
  1191. "processors (%d cpu cores) (" VERSION ")\n",
  1192. num_online_nodes(),
  1193. boot_cpu_data.x86_model_id, supported_cpus);
  1194. return cpufreq_register_driver(&cpufreq_amd64_driver);
  1195. }
  1196. return -ENODEV;
  1197. }
  1198. /* driver entry point for term */
  1199. static void __exit powernowk8_exit(void)
  1200. {
  1201. dprintk("exit\n");
  1202. cpufreq_unregister_driver(&cpufreq_amd64_driver);
  1203. }
  1204. MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and "
  1205. "Mark Langsdorf <mark.langsdorf@amd.com>");
  1206. MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
  1207. MODULE_LICENSE("GPL");
  1208. late_initcall(powernowk8_init);
  1209. module_exit(powernowk8_exit);