speedstep-smi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. {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. /**
  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_get_state - set the SpeedStep state
  120. * @state: processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
  121. *
  122. */
  123. static int speedstep_get_state(void)
  124. {
  125. u32 function = GET_SPEEDSTEP_STATE;
  126. u32 result, state, edi, command, dummy;
  127. command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
  128. pr_debug("trying to determine current setting with command %x "
  129. "at port %x\n", command, smi_port);
  130. __asm__ __volatile__(
  131. "push %%ebp\n"
  132. "out %%al, (%%dx)\n"
  133. "pop %%ebp\n"
  134. : "=a" (result),
  135. "=b" (state), "=D" (edi),
  136. "=c" (dummy), "=d" (dummy), "=S" (dummy)
  137. : "a" (command), "b" (function), "c" (0),
  138. "d" (smi_port), "S" (0), "D" (0)
  139. );
  140. pr_debug("state is %x, result is %x\n", state, result);
  141. return state & 1;
  142. }
  143. /**
  144. * speedstep_set_state - set the SpeedStep state
  145. * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
  146. *
  147. */
  148. static void speedstep_set_state(unsigned int state)
  149. {
  150. unsigned int result = 0, command, new_state, dummy;
  151. unsigned long flags;
  152. unsigned int function = SET_SPEEDSTEP_STATE;
  153. unsigned int retry = 0;
  154. if (state > 0x1)
  155. return;
  156. /* Disable IRQs */
  157. preempt_disable();
  158. local_irq_save(flags);
  159. command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
  160. pr_debug("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. /*
  166. * We need to enable interrupts, otherwise the blockage
  167. * won't resolve.
  168. *
  169. * We disable preemption so that other processes don't
  170. * run. If other processes were running, they could
  171. * submit more DMA requests, making the blockage worse.
  172. */
  173. pr_debug("retry %u, previous result %u, waiting...\n",
  174. retry, result);
  175. local_irq_enable();
  176. mdelay(retry * 50);
  177. local_irq_disable();
  178. }
  179. retry++;
  180. __asm__ __volatile__(
  181. "push %%ebp\n"
  182. "out %%al, (%%dx)\n"
  183. "pop %%ebp"
  184. : "=b" (new_state), "=D" (result),
  185. "=c" (dummy), "=a" (dummy),
  186. "=d" (dummy), "=S" (dummy)
  187. : "a" (command), "b" (function), "c" (state),
  188. "d" (smi_port), "S" (0), "D" (0)
  189. );
  190. } while ((new_state != state) && (retry <= SMI_TRIES));
  191. /* enable IRQs */
  192. local_irq_restore(flags);
  193. preempt_enable();
  194. if (new_state == state)
  195. pr_debug("change to %u MHz succeeded after %u tries "
  196. "with result %u\n",
  197. (speedstep_freqs[new_state].frequency / 1000),
  198. retry, result);
  199. else
  200. printk(KERN_ERR "cpufreq: change to state %u "
  201. "failed with new_state %u and result %u\n",
  202. state, new_state, result);
  203. return;
  204. }
  205. /**
  206. * speedstep_target - set a new CPUFreq policy
  207. * @policy: new policy
  208. * @target_freq: new freq
  209. * @relation:
  210. *
  211. * Sets a new CPUFreq policy/freq.
  212. */
  213. static int speedstep_target(struct cpufreq_policy *policy,
  214. unsigned int target_freq, unsigned int relation)
  215. {
  216. unsigned int newstate = 0;
  217. struct cpufreq_freqs freqs;
  218. if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0],
  219. target_freq, relation, &newstate))
  220. return -EINVAL;
  221. freqs.old = speedstep_freqs[speedstep_get_state()].frequency;
  222. freqs.new = speedstep_freqs[newstate].frequency;
  223. freqs.cpu = 0; /* speedstep.c is UP only driver */
  224. if (freqs.old == freqs.new)
  225. return 0;
  226. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  227. speedstep_set_state(newstate);
  228. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  229. return 0;
  230. }
  231. /**
  232. * speedstep_verify - verifies a new CPUFreq policy
  233. * @policy: new policy
  234. *
  235. * Limit must be within speedstep_low_freq and speedstep_high_freq, with
  236. * at least one border included.
  237. */
  238. static int speedstep_verify(struct cpufreq_policy *policy)
  239. {
  240. return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
  241. }
  242. static int speedstep_cpu_init(struct cpufreq_policy *policy)
  243. {
  244. int result;
  245. unsigned int speed, state;
  246. unsigned int *low, *high;
  247. /* capability check */
  248. if (policy->cpu != 0)
  249. return -ENODEV;
  250. result = speedstep_smi_ownership();
  251. if (result) {
  252. pr_debug("fails in acquiring ownership of a SMI interface.\n");
  253. return -EINVAL;
  254. }
  255. /* detect low and high frequency */
  256. low = &speedstep_freqs[SPEEDSTEP_LOW].frequency;
  257. high = &speedstep_freqs[SPEEDSTEP_HIGH].frequency;
  258. result = speedstep_smi_get_freqs(low, high);
  259. if (result) {
  260. /* fall back to speedstep_lib.c dection mechanism:
  261. * try both states out */
  262. pr_debug("could not detect low and high frequencies "
  263. "by SMI call.\n");
  264. result = speedstep_get_freqs(speedstep_processor,
  265. low, high,
  266. NULL,
  267. &speedstep_set_state);
  268. if (result) {
  269. pr_debug("could not detect two different speeds"
  270. " -- aborting.\n");
  271. return result;
  272. } else
  273. pr_debug("workaround worked.\n");
  274. }
  275. /* get current speed setting */
  276. state = speedstep_get_state();
  277. speed = speedstep_freqs[state].frequency;
  278. pr_debug("currently at %s speed setting - %i MHz\n",
  279. (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency)
  280. ? "low" : "high",
  281. (speed / 1000));
  282. /* cpuinfo and default policy values */
  283. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  284. policy->cur = speed;
  285. result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
  286. if (result)
  287. return result;
  288. cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
  289. return 0;
  290. }
  291. static int speedstep_cpu_exit(struct cpufreq_policy *policy)
  292. {
  293. cpufreq_frequency_table_put_attr(policy->cpu);
  294. return 0;
  295. }
  296. static unsigned int speedstep_get(unsigned int cpu)
  297. {
  298. if (cpu)
  299. return -ENODEV;
  300. return speedstep_get_frequency(speedstep_processor);
  301. }
  302. static int speedstep_resume(struct cpufreq_policy *policy)
  303. {
  304. int result = speedstep_smi_ownership();
  305. if (result)
  306. pr_debug("fails in re-acquiring ownership of a SMI interface.\n");
  307. return result;
  308. }
  309. static struct freq_attr *speedstep_attr[] = {
  310. &cpufreq_freq_attr_scaling_available_freqs,
  311. NULL,
  312. };
  313. static struct cpufreq_driver speedstep_driver = {
  314. .name = "speedstep-smi",
  315. .verify = speedstep_verify,
  316. .target = speedstep_target,
  317. .init = speedstep_cpu_init,
  318. .exit = speedstep_cpu_exit,
  319. .get = speedstep_get,
  320. .resume = speedstep_resume,
  321. .owner = THIS_MODULE,
  322. .attr = speedstep_attr,
  323. };
  324. static const struct x86_cpu_id ss_smi_ids[] = {
  325. { X86_VENDOR_INTEL, 6, 0xb, },
  326. { X86_VENDOR_INTEL, 6, 0x8, },
  327. { X86_VENDOR_INTEL, 15, 2 },
  328. {}
  329. };
  330. #if 0
  331. /* Not auto loaded currently */
  332. MODULE_DEVICE_TABLE(x86cpu, ss_smi_ids);
  333. #endif
  334. /**
  335. * speedstep_init - initializes the SpeedStep CPUFreq driver
  336. *
  337. * Initializes the SpeedStep support. Returns -ENODEV on unsupported
  338. * BIOS, -EINVAL on problems during initiatization, and zero on
  339. * success.
  340. */
  341. static int __init speedstep_init(void)
  342. {
  343. if (!x86_match_cpu(ss_smi_ids))
  344. return -ENODEV;
  345. speedstep_processor = speedstep_detect_processor();
  346. switch (speedstep_processor) {
  347. case SPEEDSTEP_CPU_PIII_T:
  348. case SPEEDSTEP_CPU_PIII_C:
  349. case SPEEDSTEP_CPU_PIII_C_EARLY:
  350. break;
  351. default:
  352. speedstep_processor = 0;
  353. }
  354. if (!speedstep_processor) {
  355. pr_debug("No supported Intel CPU detected.\n");
  356. return -ENODEV;
  357. }
  358. pr_debug("signature:0x%.8ulx, command:0x%.8ulx, "
  359. "event:0x%.8ulx, perf_level:0x%.8ulx.\n",
  360. ist_info.signature, ist_info.command,
  361. ist_info.event, ist_info.perf_level);
  362. /* Error if no IST-SMI BIOS or no PARM
  363. sig= 'ISGE' aka 'Intel Speedstep Gate E' */
  364. if ((ist_info.signature != 0x47534943) && (
  365. (smi_port == 0) || (smi_cmd == 0)))
  366. return -ENODEV;
  367. if (smi_sig == 1)
  368. smi_sig = 0x47534943;
  369. else
  370. smi_sig = ist_info.signature;
  371. /* setup smi_port from MODLULE_PARM or BIOS */
  372. if ((smi_port > 0xff) || (smi_port < 0))
  373. return -EINVAL;
  374. else if (smi_port == 0)
  375. smi_port = ist_info.command & 0xff;
  376. if ((smi_cmd > 0xff) || (smi_cmd < 0))
  377. return -EINVAL;
  378. else if (smi_cmd == 0)
  379. smi_cmd = (ist_info.command >> 16) & 0xff;
  380. return cpufreq_register_driver(&speedstep_driver);
  381. }
  382. /**
  383. * speedstep_exit - unregisters SpeedStep support
  384. *
  385. * Unregisters SpeedStep support.
  386. */
  387. static void __exit speedstep_exit(void)
  388. {
  389. cpufreq_unregister_driver(&speedstep_driver);
  390. }
  391. module_param(smi_port, int, 0444);
  392. module_param(smi_cmd, int, 0444);
  393. module_param(smi_sig, uint, 0444);
  394. MODULE_PARM_DESC(smi_port, "Override the BIOS-given IST port with this value "
  395. "-- Intel's default setting is 0xb2");
  396. MODULE_PARM_DESC(smi_cmd, "Override the BIOS-given IST command with this value "
  397. "-- Intel's default setting is 0x82");
  398. MODULE_PARM_DESC(smi_sig, "Set to 1 to fake the IST signature when using the "
  399. "SMI interface.");
  400. MODULE_AUTHOR("Hiroshi Miura");
  401. MODULE_DESCRIPTION("Speedstep driver for IST applet SMI interface.");
  402. MODULE_LICENSE("GPL");
  403. module_init(speedstep_init);
  404. module_exit(speedstep_exit);