speedstep-smi.c 11 KB

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