speedstep-smi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Intel SpeedStep SMI driver.
  3. *
  4. * (C) 2003 Hiroshi Miura <miura@da-cha.org>
  5. *
  6. * Licensed under the terms of the GNU GPL License version 2.
  7. *
  8. */
  9. /*********************************************************************
  10. * SPEEDSTEP - DEFINITIONS *
  11. *********************************************************************/
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/cpufreq.h>
  17. #include <linux/pci.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <asm/ist.h>
  21. #include "speedstep-lib.h"
  22. /* speedstep system management interface port/command.
  23. *
  24. * These parameters are got from IST-SMI BIOS call.
  25. * If user gives it, these are used.
  26. *
  27. */
  28. static int smi_port = 0;
  29. static int smi_cmd = 0;
  30. static unsigned int smi_sig = 0;
  31. /* info about the processor */
  32. static unsigned int speedstep_processor = 0;
  33. /*
  34. * There are only two frequency states for each processor. Values
  35. * are in kHz for the time being.
  36. */
  37. static struct cpufreq_frequency_table speedstep_freqs[] = {
  38. {SPEEDSTEP_HIGH, 0},
  39. {SPEEDSTEP_LOW, 0},
  40. {0, CPUFREQ_TABLE_END},
  41. };
  42. #define GET_SPEEDSTEP_OWNER 0
  43. #define GET_SPEEDSTEP_STATE 1
  44. #define SET_SPEEDSTEP_STATE 2
  45. #define GET_SPEEDSTEP_FREQS 4
  46. /* how often shall the SMI call be tried if it failed, e.g. because
  47. * of DMA activity going on? */
  48. #define SMI_TRIES 5
  49. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "speedstep-smi", msg)
  50. /**
  51. * speedstep_smi_ownership
  52. */
  53. static int speedstep_smi_ownership (void)
  54. {
  55. u32 command, result, magic;
  56. u32 function = GET_SPEEDSTEP_OWNER;
  57. unsigned char magic_data[] = "Copyright (c) 1999 Intel Corporation";
  58. command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
  59. magic = virt_to_phys(magic_data);
  60. dprintk("trying to obtain ownership with command %x at port %x\n", command, smi_port);
  61. __asm__ __volatile__(
  62. "out %%al, (%%dx)\n"
  63. : "=D" (result)
  64. : "a" (command), "b" (function), "c" (0), "d" (smi_port), "D" (0), "S" (magic)
  65. );
  66. dprintk("result is %x\n", result);
  67. return result;
  68. }
  69. /**
  70. * speedstep_smi_get_freqs - get SpeedStep preferred & current freq.
  71. * @low: the low frequency value is placed here
  72. * @high: the high frequency value is placed here
  73. *
  74. * Only available on later SpeedStep-enabled systems, returns false results or
  75. * even hangs [cf. bugme.osdl.org # 1422] on earlier systems. Empirical testing
  76. * shows that the latter occurs if !(ist_info.event & 0xFFFF).
  77. */
  78. static int speedstep_smi_get_freqs (unsigned int *low, unsigned int *high)
  79. {
  80. u32 command, result = 0, edi, high_mhz, low_mhz;
  81. u32 state=0;
  82. u32 function = GET_SPEEDSTEP_FREQS;
  83. if (!(ist_info.event & 0xFFFF)) {
  84. dprintk("bug #1422 -- can't read freqs from BIOS\n");
  85. return -ENODEV;
  86. }
  87. command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
  88. dprintk("trying to determine frequencies with command %x at port %x\n", command, smi_port);
  89. __asm__ __volatile__("movl $0, %%edi\n"
  90. "out %%al, (%%dx)\n"
  91. : "=a" (result), "=b" (high_mhz), "=c" (low_mhz), "=d" (state), "=D" (edi)
  92. : "a" (command), "b" (function), "c" (state), "d" (smi_port), "S" (0)
  93. );
  94. dprintk("result %x, low_freq %u, high_freq %u\n", result, low_mhz, high_mhz);
  95. /* abort if results are obviously incorrect... */
  96. if ((high_mhz + low_mhz) < 600)
  97. return -EINVAL;
  98. *high = high_mhz * 1000;
  99. *low = low_mhz * 1000;
  100. return result;
  101. }
  102. /**
  103. * speedstep_get_state - set the SpeedStep state
  104. * @state: processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
  105. *
  106. */
  107. static int speedstep_get_state (void)
  108. {
  109. u32 function=GET_SPEEDSTEP_STATE;
  110. u32 result, state, edi, command;
  111. command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
  112. dprintk("trying to determine current setting with command %x at port %x\n", command, smi_port);
  113. __asm__ __volatile__("movl $0, %%edi\n"
  114. "out %%al, (%%dx)\n"
  115. : "=a" (result), "=b" (state), "=D" (edi)
  116. : "a" (command), "b" (function), "c" (0), "d" (smi_port), "S" (0)
  117. );
  118. dprintk("state is %x, result is %x\n", state, result);
  119. return (state & 1);
  120. }
  121. /**
  122. * speedstep_set_state - set the SpeedStep state
  123. * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
  124. *
  125. */
  126. static void speedstep_set_state (unsigned int state)
  127. {
  128. unsigned int result = 0, command, new_state;
  129. unsigned long flags;
  130. unsigned int function=SET_SPEEDSTEP_STATE;
  131. unsigned int retry = 0;
  132. if (state > 0x1)
  133. return;
  134. /* Disable IRQs */
  135. local_irq_save(flags);
  136. command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
  137. dprintk("trying to set frequency to state %u with command %x at port %x\n", state, command, smi_port);
  138. do {
  139. if (retry) {
  140. dprintk("retry %u, previous result %u, waiting...\n", retry, result);
  141. mdelay(retry * 50);
  142. }
  143. retry++;
  144. __asm__ __volatile__(
  145. "movl $0, %%edi\n"
  146. "out %%al, (%%dx)\n"
  147. : "=b" (new_state), "=D" (result)
  148. : "a" (command), "b" (function), "c" (state), "d" (smi_port), "S" (0)
  149. );
  150. } while ((new_state != state) && (retry <= SMI_TRIES));
  151. /* enable IRQs */
  152. local_irq_restore(flags);
  153. if (new_state == state) {
  154. dprintk("change to %u MHz succeeded after %u tries with result %u\n", (speedstep_freqs[new_state].frequency / 1000), retry, result);
  155. } else {
  156. printk(KERN_ERR "cpufreq: change failed with new_state %u and result %u\n", new_state, result);
  157. }
  158. return;
  159. }
  160. /**
  161. * speedstep_target - set a new CPUFreq policy
  162. * @policy: new policy
  163. * @target_freq: new freq
  164. * @relation:
  165. *
  166. * Sets a new CPUFreq policy/freq.
  167. */
  168. static int speedstep_target (struct cpufreq_policy *policy,
  169. unsigned int target_freq, unsigned int relation)
  170. {
  171. unsigned int newstate = 0;
  172. struct cpufreq_freqs freqs;
  173. if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0], target_freq, relation, &newstate))
  174. return -EINVAL;
  175. freqs.old = speedstep_freqs[speedstep_get_state()].frequency;
  176. freqs.new = speedstep_freqs[newstate].frequency;
  177. freqs.cpu = 0; /* speedstep.c is UP only driver */
  178. if (freqs.old == freqs.new)
  179. return 0;
  180. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  181. speedstep_set_state(newstate);
  182. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  183. return 0;
  184. }
  185. /**
  186. * speedstep_verify - verifies a new CPUFreq policy
  187. * @policy: new policy
  188. *
  189. * Limit must be within speedstep_low_freq and speedstep_high_freq, with
  190. * at least one border included.
  191. */
  192. static int speedstep_verify (struct cpufreq_policy *policy)
  193. {
  194. return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
  195. }
  196. static int speedstep_cpu_init(struct cpufreq_policy *policy)
  197. {
  198. int result;
  199. unsigned int speed,state;
  200. /* capability check */
  201. if (policy->cpu != 0)
  202. return -ENODEV;
  203. result = speedstep_smi_ownership();
  204. if (result) {
  205. dprintk("fails in aquiring ownership of a SMI interface.\n");
  206. return -EINVAL;
  207. }
  208. /* detect low and high frequency */
  209. result = speedstep_smi_get_freqs(&speedstep_freqs[SPEEDSTEP_LOW].frequency,
  210. &speedstep_freqs[SPEEDSTEP_HIGH].frequency);
  211. if (result) {
  212. /* fall back to speedstep_lib.c dection mechanism: try both states out */
  213. dprintk("could not detect low and high frequencies by SMI call.\n");
  214. result = speedstep_get_freqs(speedstep_processor,
  215. &speedstep_freqs[SPEEDSTEP_LOW].frequency,
  216. &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
  217. &speedstep_set_state);
  218. if (result) {
  219. dprintk("could not detect two different speeds -- aborting.\n");
  220. return result;
  221. } else
  222. dprintk("workaround worked.\n");
  223. }
  224. /* get current speed setting */
  225. state = speedstep_get_state();
  226. speed = speedstep_freqs[state].frequency;
  227. dprintk("currently at %s speed setting - %i MHz\n",
  228. (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency) ? "low" : "high",
  229. (speed / 1000));
  230. /* cpuinfo and default policy values */
  231. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  232. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  233. policy->cur = speed;
  234. result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
  235. if (result)
  236. return (result);
  237. cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
  238. return 0;
  239. }
  240. static int speedstep_cpu_exit(struct cpufreq_policy *policy)
  241. {
  242. cpufreq_frequency_table_put_attr(policy->cpu);
  243. return 0;
  244. }
  245. static unsigned int speedstep_get(unsigned int cpu)
  246. {
  247. if (cpu)
  248. return -ENODEV;
  249. return speedstep_get_processor_frequency(speedstep_processor);
  250. }
  251. static int speedstep_resume(struct cpufreq_policy *policy)
  252. {
  253. int result = speedstep_smi_ownership();
  254. if (result)
  255. dprintk("fails in re-aquiring ownership of a SMI interface.\n");
  256. return result;
  257. }
  258. static struct freq_attr* speedstep_attr[] = {
  259. &cpufreq_freq_attr_scaling_available_freqs,
  260. NULL,
  261. };
  262. static struct cpufreq_driver speedstep_driver = {
  263. .name = "speedstep-smi",
  264. .verify = speedstep_verify,
  265. .target = speedstep_target,
  266. .init = speedstep_cpu_init,
  267. .exit = speedstep_cpu_exit,
  268. .get = speedstep_get,
  269. .resume = speedstep_resume,
  270. .owner = THIS_MODULE,
  271. .attr = speedstep_attr,
  272. };
  273. /**
  274. * speedstep_init - initializes the SpeedStep CPUFreq driver
  275. *
  276. * Initializes the SpeedStep support. Returns -ENODEV on unsupported
  277. * BIOS, -EINVAL on problems during initiatization, and zero on
  278. * success.
  279. */
  280. static int __init speedstep_init(void)
  281. {
  282. speedstep_processor = speedstep_detect_processor();
  283. switch (speedstep_processor) {
  284. case SPEEDSTEP_PROCESSOR_PIII_T:
  285. case SPEEDSTEP_PROCESSOR_PIII_C:
  286. case SPEEDSTEP_PROCESSOR_PIII_C_EARLY:
  287. break;
  288. case SPEEDSTEP_PROCESSOR_P4M:
  289. printk(KERN_INFO "speedstep-smi: you're trying to use this cpufreq driver on a Pentium 4-based CPU. Most likely it will not work.\n");
  290. break;
  291. default:
  292. speedstep_processor = 0;
  293. }
  294. if (!speedstep_processor) {
  295. dprintk ("No supported Intel CPU detected.\n");
  296. return -ENODEV;
  297. }
  298. dprintk("signature:0x%.8lx, command:0x%.8lx, event:0x%.8lx, perf_level:0x%.8lx.\n",
  299. ist_info.signature, ist_info.command, ist_info.event, ist_info.perf_level);
  300. /* Error if no IST-SMI BIOS or no PARM
  301. sig= 'ISGE' aka 'Intel Speedstep Gate E' */
  302. if ((ist_info.signature != 0x47534943) && (
  303. (smi_port == 0) || (smi_cmd == 0)))
  304. return -ENODEV;
  305. if (smi_sig == 1)
  306. smi_sig = 0x47534943;
  307. else
  308. smi_sig = ist_info.signature;
  309. /* setup smi_port from MODLULE_PARM or BIOS */
  310. if ((smi_port > 0xff) || (smi_port < 0)) {
  311. return -EINVAL;
  312. } else if (smi_port == 0) {
  313. smi_port = ist_info.command & 0xff;
  314. }
  315. if ((smi_cmd > 0xff) || (smi_cmd < 0)) {
  316. return -EINVAL;
  317. } else if (smi_cmd == 0) {
  318. smi_cmd = (ist_info.command >> 16) & 0xff;
  319. }
  320. return cpufreq_register_driver(&speedstep_driver);
  321. }
  322. /**
  323. * speedstep_exit - unregisters SpeedStep support
  324. *
  325. * Unregisters SpeedStep support.
  326. */
  327. static void __exit speedstep_exit(void)
  328. {
  329. cpufreq_unregister_driver(&speedstep_driver);
  330. }
  331. module_param(smi_port, int, 0444);
  332. module_param(smi_cmd, int, 0444);
  333. module_param(smi_sig, uint, 0444);
  334. MODULE_PARM_DESC(smi_port, "Override the BIOS-given IST port with this value -- Intel's default setting is 0xb2");
  335. MODULE_PARM_DESC(smi_cmd, "Override the BIOS-given IST command with this value -- Intel's default setting is 0x82");
  336. MODULE_PARM_DESC(smi_sig, "Set to 1 to fake the IST signature when using the SMI interface.");
  337. MODULE_AUTHOR ("Hiroshi Miura");
  338. MODULE_DESCRIPTION ("Speedstep driver for IST applet SMI interface.");
  339. MODULE_LICENSE ("GPL");
  340. module_init(speedstep_init);
  341. module_exit(speedstep_exit);