speedstep-smi.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel SpeedStep SMI driver.
  4. *
  5. * (C) 2003 Hiroshi Miura <miura@da-cha.org>
  6. */
  7. /*********************************************************************
  8. * SPEEDSTEP - DEFINITIONS *
  9. *********************************************************************/
  10. #define pr_fmt(fmt) "cpufreq: " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/delay.h>
  17. #include <linux/io.h>
  18. #include <asm/ist.h>
  19. #include <asm/cpu_device_id.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. {0, SPEEDSTEP_HIGH, 0},
  38. {0, SPEEDSTEP_LOW, 0},
  39. {0, 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. /**
  49. * speedstep_smi_ownership
  50. */
  51. static int speedstep_smi_ownership(void)
  52. {
  53. u32 command, result, magic, dummy;
  54. u32 function = GET_SPEEDSTEP_OWNER;
  55. unsigned char magic_data[] = "Copyright (c) 1999 Intel Corporation";
  56. command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
  57. magic = virt_to_phys(magic_data);
  58. pr_debug("trying to obtain ownership with command %x at port %x\n",
  59. command, smi_port);
  60. __asm__ __volatile__(
  61. "push %%ebp\n"
  62. "out %%al, (%%dx)\n"
  63. "pop %%ebp\n"
  64. : "=D" (result),
  65. "=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. pr_debug("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. pr_debug("bug #1422 -- can't read freqs from BIOS\n");
  90. return -ENODEV;
  91. }
  92. command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
  93. pr_debug("trying to determine frequencies with command %x at port %x\n",
  94. command, smi_port);
  95. __asm__ __volatile__(
  96. "push %%ebp\n"
  97. "out %%al, (%%dx)\n"
  98. "pop %%ebp"
  99. : "=a" (result),
  100. "=b" (high_mhz),
  101. "=c" (low_mhz),
  102. "=d" (state), "=D" (edi), "=S" (dummy)
  103. : "a" (command),
  104. "b" (function),
  105. "c" (state),
  106. "d" (smi_port), "S" (0), "D" (0)
  107. );
  108. pr_debug("result %x, low_freq %u, high_freq %u\n",
  109. result, low_mhz, high_mhz);
  110. /* abort if results are obviously incorrect... */
  111. if ((high_mhz + low_mhz) < 600)
  112. return -EINVAL;
  113. *high = high_mhz * 1000;
  114. *low = low_mhz * 1000;
  115. return result;
  116. }
  117. /**
  118. * speedstep_set_state - set the SpeedStep state
  119. * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
  120. *
  121. */
  122. static void speedstep_set_state(unsigned int state)
  123. {
  124. unsigned int result = 0, command, new_state, dummy;
  125. unsigned long flags;
  126. unsigned int function = SET_SPEEDSTEP_STATE;
  127. unsigned int retry = 0;
  128. if (state > 0x1)
  129. return;
  130. /* Disable IRQs */
  131. preempt_disable();
  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. /*
  140. * We need to enable interrupts, otherwise the blockage
  141. * won't resolve.
  142. *
  143. * We disable preemption so that other processes don't
  144. * run. If other processes were running, they could
  145. * submit more DMA requests, making the blockage worse.
  146. */
  147. pr_debug("retry %u, previous result %u, waiting...\n",
  148. retry, result);
  149. local_irq_enable();
  150. mdelay(retry * 50);
  151. local_irq_disable();
  152. }
  153. retry++;
  154. __asm__ __volatile__(
  155. "push %%ebp\n"
  156. "out %%al, (%%dx)\n"
  157. "pop %%ebp"
  158. : "=b" (new_state), "=D" (result),
  159. "=c" (dummy), "=a" (dummy),
  160. "=d" (dummy), "=S" (dummy)
  161. : "a" (command), "b" (function), "c" (state),
  162. "d" (smi_port), "S" (0), "D" (0)
  163. );
  164. } while ((new_state != state) && (retry <= SMI_TRIES));
  165. /* enable IRQs */
  166. local_irq_restore(flags);
  167. preempt_enable();
  168. if (new_state == state)
  169. pr_debug("change to %u MHz succeeded after %u tries "
  170. "with result %u\n",
  171. (speedstep_freqs[new_state].frequency / 1000),
  172. retry, result);
  173. else
  174. pr_err("change to state %u failed with new_state %u and result %u\n",
  175. state, new_state, result);
  176. return;
  177. }
  178. /**
  179. * speedstep_target - set a new CPUFreq policy
  180. * @policy: new policy
  181. * @index: index of new freq
  182. *
  183. * Sets a new CPUFreq policy/freq.
  184. */
  185. static int speedstep_target(struct cpufreq_policy *policy, unsigned int index)
  186. {
  187. speedstep_set_state(index);
  188. return 0;
  189. }
  190. static int speedstep_cpu_init(struct cpufreq_policy *policy)
  191. {
  192. int result;
  193. unsigned int *low, *high;
  194. /* capability check */
  195. if (policy->cpu != 0)
  196. return -ENODEV;
  197. result = speedstep_smi_ownership();
  198. if (result) {
  199. pr_debug("fails in acquiring ownership of a SMI interface.\n");
  200. return -EINVAL;
  201. }
  202. /* detect low and high frequency */
  203. low = &speedstep_freqs[SPEEDSTEP_LOW].frequency;
  204. high = &speedstep_freqs[SPEEDSTEP_HIGH].frequency;
  205. result = speedstep_smi_get_freqs(low, high);
  206. if (result) {
  207. /* fall back to speedstep_lib.c dection mechanism:
  208. * try both states out */
  209. pr_debug("could not detect low and high frequencies "
  210. "by SMI call.\n");
  211. result = speedstep_get_freqs(speedstep_processor,
  212. low, high,
  213. NULL,
  214. &speedstep_set_state);
  215. if (result) {
  216. pr_debug("could not detect two different speeds"
  217. " -- aborting.\n");
  218. return result;
  219. } else
  220. pr_debug("workaround worked.\n");
  221. }
  222. policy->freq_table = speedstep_freqs;
  223. return 0;
  224. }
  225. static unsigned int speedstep_get(unsigned int cpu)
  226. {
  227. if (cpu)
  228. return -ENODEV;
  229. return speedstep_get_frequency(speedstep_processor);
  230. }
  231. static int speedstep_resume(struct cpufreq_policy *policy)
  232. {
  233. int result = speedstep_smi_ownership();
  234. if (result)
  235. pr_debug("fails in re-acquiring ownership of a SMI interface.\n");
  236. return result;
  237. }
  238. static struct cpufreq_driver speedstep_driver = {
  239. .name = "speedstep-smi",
  240. .flags = CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING,
  241. .verify = cpufreq_generic_frequency_table_verify,
  242. .target_index = speedstep_target,
  243. .init = speedstep_cpu_init,
  244. .get = speedstep_get,
  245. .resume = speedstep_resume,
  246. .attr = cpufreq_generic_attr,
  247. };
  248. static const struct x86_cpu_id ss_smi_ids[] = {
  249. X86_MATCH_VENDOR_FAM_MODEL(INTEL, 6, 0x8, 0),
  250. X86_MATCH_VENDOR_FAM_MODEL(INTEL, 6, 0xb, 0),
  251. X86_MATCH_VENDOR_FAM_MODEL(INTEL, 15, 0x2, 0),
  252. {}
  253. };
  254. /**
  255. * speedstep_init - initializes the SpeedStep CPUFreq driver
  256. *
  257. * Initializes the SpeedStep support. Returns -ENODEV on unsupported
  258. * BIOS, -EINVAL on problems during initiatization, and zero on
  259. * success.
  260. */
  261. static int __init speedstep_init(void)
  262. {
  263. if (!x86_match_cpu(ss_smi_ids))
  264. return -ENODEV;
  265. speedstep_processor = speedstep_detect_processor();
  266. switch (speedstep_processor) {
  267. case SPEEDSTEP_CPU_PIII_T:
  268. case SPEEDSTEP_CPU_PIII_C:
  269. case SPEEDSTEP_CPU_PIII_C_EARLY:
  270. break;
  271. default:
  272. speedstep_processor = 0;
  273. }
  274. if (!speedstep_processor) {
  275. pr_debug("No supported Intel CPU detected.\n");
  276. return -ENODEV;
  277. }
  278. pr_debug("signature:0x%.8x, command:0x%.8x, "
  279. "event:0x%.8x, perf_level:0x%.8x.\n",
  280. ist_info.signature, ist_info.command,
  281. ist_info.event, ist_info.perf_level);
  282. /* Error if no IST-SMI BIOS or no PARM
  283. sig= 'ISGE' aka 'Intel Speedstep Gate E' */
  284. if ((ist_info.signature != 0x47534943) && (
  285. (smi_port == 0) || (smi_cmd == 0)))
  286. return -ENODEV;
  287. if (smi_sig == 1)
  288. smi_sig = 0x47534943;
  289. else
  290. smi_sig = ist_info.signature;
  291. /* setup smi_port from MODLULE_PARM or BIOS */
  292. if ((smi_port > 0xff) || (smi_port < 0))
  293. return -EINVAL;
  294. else if (smi_port == 0)
  295. smi_port = ist_info.command & 0xff;
  296. if ((smi_cmd > 0xff) || (smi_cmd < 0))
  297. return -EINVAL;
  298. else if (smi_cmd == 0)
  299. smi_cmd = (ist_info.command >> 16) & 0xff;
  300. return cpufreq_register_driver(&speedstep_driver);
  301. }
  302. /**
  303. * speedstep_exit - unregisters SpeedStep support
  304. *
  305. * Unregisters SpeedStep support.
  306. */
  307. static void __exit speedstep_exit(void)
  308. {
  309. cpufreq_unregister_driver(&speedstep_driver);
  310. }
  311. module_param_hw(smi_port, int, ioport, 0444);
  312. module_param(smi_cmd, int, 0444);
  313. module_param(smi_sig, uint, 0444);
  314. MODULE_PARM_DESC(smi_port, "Override the BIOS-given IST port with this value "
  315. "-- Intel's default setting is 0xb2");
  316. MODULE_PARM_DESC(smi_cmd, "Override the BIOS-given IST command with this value "
  317. "-- Intel's default setting is 0x82");
  318. MODULE_PARM_DESC(smi_sig, "Set to 1 to fake the IST signature when using the "
  319. "SMI interface.");
  320. MODULE_AUTHOR("Hiroshi Miura");
  321. MODULE_DESCRIPTION("Speedstep driver for IST applet SMI interface.");
  322. MODULE_LICENSE("GPL");
  323. module_init(speedstep_init);
  324. module_exit(speedstep_exit);