powernow-k8.c 40 KB

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