speedstep-ich.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * (C) 2001 Dave Jones, Arjan van de ven.
  3. * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
  4. *
  5. * Licensed under the terms of the GNU GPL License version 2.
  6. * Based upon reverse engineered information, and on Intel documentation
  7. * for chipsets ICH2-M and ICH3-M.
  8. *
  9. * Many thanks to Ducrot Bruno for finding and fixing the last
  10. * "missing link" for ICH2-M/ICH3-M support, and to Thomas Winkler
  11. * for extensive testing.
  12. *
  13. * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
  14. */
  15. /*********************************************************************
  16. * SPEEDSTEP - DEFINITIONS *
  17. *********************************************************************/
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/cpufreq.h>
  22. #include <linux/pci.h>
  23. #include <linux/sched.h>
  24. #include <asm/cpu_device_id.h>
  25. #include "speedstep-lib.h"
  26. /* speedstep_chipset:
  27. * It is necessary to know which chipset is used. As accesses to
  28. * this device occur at various places in this module, we need a
  29. * static struct pci_dev * pointing to that device.
  30. */
  31. static struct pci_dev *speedstep_chipset_dev;
  32. /* speedstep_processor
  33. */
  34. static enum speedstep_processor speedstep_processor;
  35. static u32 pmbase;
  36. /*
  37. * There are only two frequency states for each processor. Values
  38. * are in kHz for the time being.
  39. */
  40. static struct cpufreq_frequency_table speedstep_freqs[] = {
  41. {0, SPEEDSTEP_HIGH, 0},
  42. {0, SPEEDSTEP_LOW, 0},
  43. {0, 0, CPUFREQ_TABLE_END},
  44. };
  45. /**
  46. * speedstep_find_register - read the PMBASE address
  47. *
  48. * Returns: -ENODEV if no register could be found
  49. */
  50. static int speedstep_find_register(void)
  51. {
  52. if (!speedstep_chipset_dev)
  53. return -ENODEV;
  54. /* get PMBASE */
  55. pci_read_config_dword(speedstep_chipset_dev, 0x40, &pmbase);
  56. if (!(pmbase & 0x01)) {
  57. printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
  58. return -ENODEV;
  59. }
  60. pmbase &= 0xFFFFFFFE;
  61. if (!pmbase) {
  62. printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
  63. return -ENODEV;
  64. }
  65. pr_debug("pmbase is 0x%x\n", pmbase);
  66. return 0;
  67. }
  68. /**
  69. * speedstep_set_state - set the SpeedStep state
  70. * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
  71. *
  72. * Tries to change the SpeedStep state. Can be called from
  73. * smp_call_function_single.
  74. */
  75. static void speedstep_set_state(unsigned int state)
  76. {
  77. u8 pm2_blk;
  78. u8 value;
  79. unsigned long flags;
  80. if (state > 0x1)
  81. return;
  82. /* Disable IRQs */
  83. local_irq_save(flags);
  84. /* read state */
  85. value = inb(pmbase + 0x50);
  86. pr_debug("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
  87. /* write new state */
  88. value &= 0xFE;
  89. value |= state;
  90. pr_debug("writing 0x%x to pmbase 0x%x + 0x50\n", value, pmbase);
  91. /* Disable bus master arbitration */
  92. pm2_blk = inb(pmbase + 0x20);
  93. pm2_blk |= 0x01;
  94. outb(pm2_blk, (pmbase + 0x20));
  95. /* Actual transition */
  96. outb(value, (pmbase + 0x50));
  97. /* Restore bus master arbitration */
  98. pm2_blk &= 0xfe;
  99. outb(pm2_blk, (pmbase + 0x20));
  100. /* check if transition was successful */
  101. value = inb(pmbase + 0x50);
  102. /* Enable IRQs */
  103. local_irq_restore(flags);
  104. pr_debug("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
  105. if (state == (value & 0x1))
  106. pr_debug("change to %u MHz succeeded\n",
  107. speedstep_get_frequency(speedstep_processor) / 1000);
  108. else
  109. printk(KERN_ERR "cpufreq: change failed - I/O error\n");
  110. return;
  111. }
  112. /* Wrapper for smp_call_function_single. */
  113. static void _speedstep_set_state(void *_state)
  114. {
  115. speedstep_set_state(*(unsigned int *)_state);
  116. }
  117. /**
  118. * speedstep_activate - activate SpeedStep control in the chipset
  119. *
  120. * Tries to activate the SpeedStep status and control registers.
  121. * Returns -EINVAL on an unsupported chipset, and zero on success.
  122. */
  123. static int speedstep_activate(void)
  124. {
  125. u16 value = 0;
  126. if (!speedstep_chipset_dev)
  127. return -EINVAL;
  128. pci_read_config_word(speedstep_chipset_dev, 0x00A0, &value);
  129. if (!(value & 0x08)) {
  130. value |= 0x08;
  131. pr_debug("activating SpeedStep (TM) registers\n");
  132. pci_write_config_word(speedstep_chipset_dev, 0x00A0, value);
  133. }
  134. return 0;
  135. }
  136. /**
  137. * speedstep_detect_chipset - detect the Southbridge which contains SpeedStep logic
  138. *
  139. * Detects ICH2-M, ICH3-M and ICH4-M so far. The pci_dev points to
  140. * the LPC bridge / PM module which contains all power-management
  141. * functions. Returns the SPEEDSTEP_CHIPSET_-number for the detected
  142. * chipset, or zero on failure.
  143. */
  144. static unsigned int speedstep_detect_chipset(void)
  145. {
  146. speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  147. PCI_DEVICE_ID_INTEL_82801DB_12,
  148. PCI_ANY_ID, PCI_ANY_ID,
  149. NULL);
  150. if (speedstep_chipset_dev)
  151. return 4; /* 4-M */
  152. speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  153. PCI_DEVICE_ID_INTEL_82801CA_12,
  154. PCI_ANY_ID, PCI_ANY_ID,
  155. NULL);
  156. if (speedstep_chipset_dev)
  157. return 3; /* 3-M */
  158. speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  159. PCI_DEVICE_ID_INTEL_82801BA_10,
  160. PCI_ANY_ID, PCI_ANY_ID,
  161. NULL);
  162. if (speedstep_chipset_dev) {
  163. /* speedstep.c causes lockups on Dell Inspirons 8000 and
  164. * 8100 which use a pretty old revision of the 82815
  165. * host bridge. Abort on these systems.
  166. */
  167. static struct pci_dev *hostbridge;
  168. hostbridge = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  169. PCI_DEVICE_ID_INTEL_82815_MC,
  170. PCI_ANY_ID, PCI_ANY_ID,
  171. NULL);
  172. if (!hostbridge)
  173. return 2; /* 2-M */
  174. if (hostbridge->revision < 5) {
  175. pr_debug("hostbridge does not support speedstep\n");
  176. speedstep_chipset_dev = NULL;
  177. pci_dev_put(hostbridge);
  178. return 0;
  179. }
  180. pci_dev_put(hostbridge);
  181. return 2; /* 2-M */
  182. }
  183. return 0;
  184. }
  185. static void get_freq_data(void *_speed)
  186. {
  187. unsigned int *speed = _speed;
  188. *speed = speedstep_get_frequency(speedstep_processor);
  189. }
  190. static unsigned int speedstep_get(unsigned int cpu)
  191. {
  192. unsigned int speed;
  193. /* You're supposed to ensure CPU is online. */
  194. if (smp_call_function_single(cpu, get_freq_data, &speed, 1) != 0)
  195. BUG();
  196. pr_debug("detected %u kHz as current frequency\n", speed);
  197. return speed;
  198. }
  199. /**
  200. * speedstep_target - set a new CPUFreq policy
  201. * @policy: new policy
  202. * @index: index of target frequency
  203. *
  204. * Sets a new CPUFreq policy.
  205. */
  206. static int speedstep_target(struct cpufreq_policy *policy, unsigned int index)
  207. {
  208. unsigned int policy_cpu;
  209. policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
  210. smp_call_function_single(policy_cpu, _speedstep_set_state, &index,
  211. true);
  212. return 0;
  213. }
  214. struct get_freqs {
  215. struct cpufreq_policy *policy;
  216. int ret;
  217. };
  218. static void get_freqs_on_cpu(void *_get_freqs)
  219. {
  220. struct get_freqs *get_freqs = _get_freqs;
  221. get_freqs->ret =
  222. speedstep_get_freqs(speedstep_processor,
  223. &speedstep_freqs[SPEEDSTEP_LOW].frequency,
  224. &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
  225. &get_freqs->policy->cpuinfo.transition_latency,
  226. &speedstep_set_state);
  227. }
  228. static int speedstep_cpu_init(struct cpufreq_policy *policy)
  229. {
  230. unsigned int policy_cpu;
  231. struct get_freqs gf;
  232. /* only run on CPU to be set, or on its sibling */
  233. #ifdef CONFIG_SMP
  234. cpumask_copy(policy->cpus, topology_sibling_cpumask(policy->cpu));
  235. #endif
  236. policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
  237. /* detect low and high frequency and transition latency */
  238. gf.policy = policy;
  239. smp_call_function_single(policy_cpu, get_freqs_on_cpu, &gf, 1);
  240. if (gf.ret)
  241. return gf.ret;
  242. return cpufreq_table_validate_and_show(policy, speedstep_freqs);
  243. }
  244. static struct cpufreq_driver speedstep_driver = {
  245. .name = "speedstep-ich",
  246. .verify = cpufreq_generic_frequency_table_verify,
  247. .target_index = speedstep_target,
  248. .init = speedstep_cpu_init,
  249. .get = speedstep_get,
  250. .attr = cpufreq_generic_attr,
  251. };
  252. static const struct x86_cpu_id ss_smi_ids[] = {
  253. { X86_VENDOR_INTEL, 6, 0xb, },
  254. { X86_VENDOR_INTEL, 6, 0x8, },
  255. { X86_VENDOR_INTEL, 15, 2 },
  256. {}
  257. };
  258. #if 0
  259. /* Autoload or not? Do not for now. */
  260. MODULE_DEVICE_TABLE(x86cpu, ss_smi_ids);
  261. #endif
  262. /**
  263. * speedstep_init - initializes the SpeedStep CPUFreq driver
  264. *
  265. * Initializes the SpeedStep support. Returns -ENODEV on unsupported
  266. * devices, -EINVAL on problems during initiatization, and zero on
  267. * success.
  268. */
  269. static int __init speedstep_init(void)
  270. {
  271. if (!x86_match_cpu(ss_smi_ids))
  272. return -ENODEV;
  273. /* detect processor */
  274. speedstep_processor = speedstep_detect_processor();
  275. if (!speedstep_processor) {
  276. pr_debug("Intel(R) SpeedStep(TM) capable processor "
  277. "not found\n");
  278. return -ENODEV;
  279. }
  280. /* detect chipset */
  281. if (!speedstep_detect_chipset()) {
  282. pr_debug("Intel(R) SpeedStep(TM) for this chipset not "
  283. "(yet) available.\n");
  284. return -ENODEV;
  285. }
  286. /* activate speedstep support */
  287. if (speedstep_activate()) {
  288. pci_dev_put(speedstep_chipset_dev);
  289. return -EINVAL;
  290. }
  291. if (speedstep_find_register())
  292. return -ENODEV;
  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. pci_dev_put(speedstep_chipset_dev);
  303. cpufreq_unregister_driver(&speedstep_driver);
  304. }
  305. MODULE_AUTHOR("Dave Jones, Dominik Brodowski <linux@brodo.de>");
  306. MODULE_DESCRIPTION("Speedstep driver for Intel mobile processors on chipsets "
  307. "with ICH-M southbridges.");
  308. MODULE_LICENSE("GPL");
  309. module_init(speedstep_init);
  310. module_exit(speedstep_exit);