speedstep-smi.c 9.6 KB

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