powernow-k8.c 40 KB

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