speedstep-smi.c 9.2 KB

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