powernow-k8.c 40 KB

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