speedstep-smi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 "speedstep-lib.h"
  21. /* speedstep system management interface port/command.
  22. *
  23. * These parameters are got from IST-SMI BIOS call.
  24. * If user gives it, these are used.
  25. *
  26. */
  27. static int smi_port;
  28. static int smi_cmd;
  29. static unsigned int smi_sig;
  30. /* info about the processor */
  31. static enum speedstep_processor speedstep_processor;
  32. /*
  33. * There are only two frequency states for each processor. Values
  34. * are in kHz for the time being.
  35. */
  36. static struct cpufreq_frequency_table speedstep_freqs[] = {
  37. {SPEEDSTEP_HIGH, 0},
  38. {SPEEDSTEP_LOW, 0},
  39. {0, CPUFREQ_TABLE_END},
  40. };
  41. #define GET_SPEEDSTEP_OWNER 0
  42. #define GET_SPEEDSTEP_STATE 1
  43. #define SET_SPEEDSTEP_STATE 2
  44. #define GET_SPEEDSTEP_FREQS 4
  45. /* how often shall the SMI call be tried if it failed, e.g. because
  46. * of DMA activity going on? */
  47. #define SMI_TRIES 5
  48. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
  49. "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",
  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. dprintk("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. dprintk("bug #1422 -- can't read freqs from BIOS\n");
  92. return -ENODEV;
  93. }
  94. command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
  95. dprintk("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. dprintk("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_get_state - set the SpeedStep state
  121. * @state: processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
  122. *
  123. */
  124. static int speedstep_get_state(void)
  125. {
  126. u32 function = GET_SPEEDSTEP_STATE;
  127. u32 result, state, edi, command, dummy;
  128. command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
  129. dprintk("trying to determine current setting with command %x "
  130. "at port %x\n", command, smi_port);
  131. __asm__ __volatile__(
  132. "push %%ebp\n"
  133. "out %%al, (%%dx)\n"
  134. "pop %%ebp\n"
  135. : "=a" (result),
  136. "=b" (state), "=D" (edi),
  137. "=c" (dummy), "=d" (dummy), "=S" (dummy)
  138. : "a" (command), "b" (function), "c" (0),
  139. "d" (smi_port), "S" (0), "D" (0)
  140. );
  141. dprintk("state is %x, result is %x\n", state, result);
  142. return state & 1;
  143. }
  144. /**
  145. * speedstep_set_state - set the SpeedStep state
  146. * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
  147. *
  148. */
  149. static void speedstep_set_state(unsigned int state)
  150. {
  151. unsigned int result = 0, command, new_state, dummy;
  152. unsigned long flags;
  153. unsigned int function = SET_SPEEDSTEP_STATE;
  154. unsigned int retry = 0;
  155. if (state > 0x1)
  156. return;
  157. /* Disable IRQs */
  158. local_irq_save(flags);
  159. command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
  160. dprintk("trying to set frequency to state %u "
  161. "with command %x at port %x\n",
  162. state, command, smi_port);
  163. do {
  164. if (retry) {
  165. dprintk("retry %u, previous result %u, waiting...\n",
  166. retry, result);
  167. mdelay(retry * 50);
  168. }
  169. retry++;
  170. __asm__ __volatile__(
  171. "push %%ebp\n"
  172. "out %%al, (%%dx)\n"
  173. "pop %%ebp"
  174. : "=b" (new_state), "=D" (result),
  175. "=c" (dummy), "=a" (dummy),
  176. "=d" (dummy), "=S" (dummy)
  177. : "a" (command), "b" (function), "c" (state),
  178. "d" (smi_port), "S" (0), "D" (0)
  179. );
  180. } while ((new_state != state) && (retry <= SMI_TRIES));
  181. /* enable IRQs */
  182. local_irq_restore(flags);
  183. if (new_state == state)
  184. dprintk("change to %u MHz succeeded after %u tries "
  185. "with result %u\n",
  186. (speedstep_freqs[new_state].frequency / 1000),
  187. retry, result);
  188. else
  189. printk(KERN_ERR "cpufreq: change to state %u "
  190. "failed with new_state %u and result %u\n",
  191. state, new_state, result);
  192. return;
  193. }
  194. /**
  195. * speedstep_target - set a new CPUFreq policy
  196. * @policy: new policy
  197. * @target_freq: new freq
  198. * @relation:
  199. *
  200. * Sets a new CPUFreq policy/freq.
  201. */
  202. static int speedstep_target(struct cpufreq_policy *policy,
  203. unsigned int target_freq, unsigned int relation)
  204. {
  205. unsigned int newstate = 0;
  206. struct cpufreq_freqs freqs;
  207. if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0],
  208. target_freq, relation, &newstate))
  209. return -EINVAL;
  210. freqs.old = speedstep_freqs[speedstep_get_state()].frequency;
  211. freqs.new = speedstep_freqs[newstate].frequency;
  212. freqs.cpu = 0; /* speedstep.c is UP only driver */
  213. if (freqs.old == freqs.new)
  214. return 0;
  215. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  216. speedstep_set_state(newstate);
  217. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  218. return 0;
  219. }
  220. /**
  221. * speedstep_verify - verifies a new CPUFreq policy
  222. * @policy: new policy
  223. *
  224. * Limit must be within speedstep_low_freq and speedstep_high_freq, with
  225. * at least one border included.
  226. */
  227. static int speedstep_verify(struct cpufreq_policy *policy)
  228. {
  229. return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
  230. }
  231. static int speedstep_cpu_init(struct cpufreq_policy *policy)
  232. {
  233. int result;
  234. unsigned int speed, state;
  235. unsigned int *low, *high;
  236. /* capability check */
  237. if (policy->cpu != 0)
  238. return -ENODEV;
  239. result = speedstep_smi_ownership();
  240. if (result) {
  241. dprintk("fails in aquiring ownership of a SMI interface.\n");
  242. return -EINVAL;
  243. }
  244. /* detect low and high frequency */
  245. low = &speedstep_freqs[SPEEDSTEP_LOW].frequency;
  246. high = &speedstep_freqs[SPEEDSTEP_HIGH].frequency;
  247. result = speedstep_smi_get_freqs(low, high);
  248. if (result) {
  249. /* fall back to speedstep_lib.c dection mechanism:
  250. * try both states out */
  251. dprintk("could not detect low and high frequencies "
  252. "by SMI call.\n");
  253. result = speedstep_get_freqs(speedstep_processor,
  254. low, high,
  255. NULL,
  256. &speedstep_set_state);
  257. if (result) {
  258. dprintk("could not detect two different speeds"
  259. " -- aborting.\n");
  260. return result;
  261. } else
  262. dprintk("workaround worked.\n");
  263. }
  264. /* get current speed setting */
  265. state = speedstep_get_state();
  266. speed = speedstep_freqs[state].frequency;
  267. dprintk("currently at %s speed setting - %i MHz\n",
  268. (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency)
  269. ? "low" : "high",
  270. (speed / 1000));
  271. /* cpuinfo and default policy values */
  272. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  273. policy->cur = speed;
  274. result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
  275. if (result)
  276. return result;
  277. cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
  278. return 0;
  279. }
  280. static int speedstep_cpu_exit(struct cpufreq_policy *policy)
  281. {
  282. cpufreq_frequency_table_put_attr(policy->cpu);
  283. return 0;
  284. }
  285. static unsigned int speedstep_get(unsigned int cpu)
  286. {
  287. if (cpu)
  288. return -ENODEV;
  289. return speedstep_get_frequency(speedstep_processor);
  290. }
  291. static int speedstep_resume(struct cpufreq_policy *policy)
  292. {
  293. int result = speedstep_smi_ownership();
  294. if (result)
  295. dprintk("fails in re-aquiring ownership of a SMI interface.\n");
  296. return result;
  297. }
  298. static struct freq_attr *speedstep_attr[] = {
  299. &cpufreq_freq_attr_scaling_available_freqs,
  300. NULL,
  301. };
  302. static struct cpufreq_driver speedstep_driver = {
  303. .name = "speedstep-smi",
  304. .verify = speedstep_verify,
  305. .target = speedstep_target,
  306. .init = speedstep_cpu_init,
  307. .exit = speedstep_cpu_exit,
  308. .get = speedstep_get,
  309. .resume = speedstep_resume,
  310. .owner = THIS_MODULE,
  311. .attr = speedstep_attr,
  312. };
  313. /**
  314. * speedstep_init - initializes the SpeedStep CPUFreq driver
  315. *
  316. * Initializes the SpeedStep support. Returns -ENODEV on unsupported
  317. * BIOS, -EINVAL on problems during initiatization, and zero on
  318. * success.
  319. */
  320. static int __init speedstep_init(void)
  321. {
  322. speedstep_processor = speedstep_detect_processor();
  323. switch (speedstep_processor) {
  324. case SPEEDSTEP_CPU_PIII_T:
  325. case SPEEDSTEP_CPU_PIII_C:
  326. case SPEEDSTEP_CPU_PIII_C_EARLY:
  327. break;
  328. default:
  329. speedstep_processor = 0;
  330. }
  331. if (!speedstep_processor) {
  332. dprintk("No supported Intel CPU detected.\n");
  333. return -ENODEV;
  334. }
  335. dprintk("signature:0x%.8lx, command:0x%.8lx, "
  336. "event:0x%.8lx, perf_level:0x%.8lx.\n",
  337. ist_info.signature, ist_info.command,
  338. ist_info.event, ist_info.perf_level);
  339. /* Error if no IST-SMI BIOS or no PARM
  340. sig= 'ISGE' aka 'Intel Speedstep Gate E' */
  341. if ((ist_info.signature != 0x47534943) && (
  342. (smi_port == 0) || (smi_cmd == 0)))
  343. return -ENODEV;
  344. if (smi_sig == 1)
  345. smi_sig = 0x47534943;
  346. else
  347. smi_sig = ist_info.signature;
  348. /* setup smi_port from MODLULE_PARM or BIOS */
  349. if ((smi_port > 0xff) || (smi_port < 0))
  350. return -EINVAL;
  351. else if (smi_port == 0)
  352. smi_port = ist_info.command & 0xff;
  353. if ((smi_cmd > 0xff) || (smi_cmd < 0))
  354. return -EINVAL;
  355. else if (smi_cmd == 0)
  356. smi_cmd = (ist_info.command >> 16) & 0xff;
  357. return cpufreq_register_driver(&speedstep_driver);
  358. }
  359. /**
  360. * speedstep_exit - unregisters SpeedStep support
  361. *
  362. * Unregisters SpeedStep support.
  363. */
  364. static void __exit speedstep_exit(void)
  365. {
  366. cpufreq_unregister_driver(&speedstep_driver);
  367. }
  368. module_param(smi_port, int, 0444);
  369. module_param(smi_cmd, int, 0444);
  370. module_param(smi_sig, uint, 0444);
  371. MODULE_PARM_DESC(smi_port, "Override the BIOS-given IST port with this value "
  372. "-- Intel's default setting is 0xb2");
  373. MODULE_PARM_DESC(smi_cmd, "Override the BIOS-given IST command with this value "
  374. "-- Intel's default setting is 0x82");
  375. MODULE_PARM_DESC(smi_sig, "Set to 1 to fake the IST signature when using the "
  376. "SMI interface.");
  377. MODULE_AUTHOR("Hiroshi Miura");
  378. MODULE_DESCRIPTION("Speedstep driver for IST applet SMI interface.");
  379. MODULE_LICENSE("GPL");
  380. module_init(speedstep_init);
  381. module_exit(speedstep_exit);