powernow-k8.c 37 KB

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