speedstep-ich.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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/slab.h>
  24. #include <linux/sched.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 unsigned int speedstep_processor = 0;
  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. {SPEEDSTEP_HIGH, 0},
  42. {SPEEDSTEP_LOW, 0},
  43. {0, CPUFREQ_TABLE_END},
  44. };
  45. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "speedstep-ich", msg)
  46. /**
  47. * speedstep_find_register - read the PMBASE address
  48. *
  49. * Returns: -ENODEV if no register could be found
  50. */
  51. static int speedstep_find_register (void)
  52. {
  53. if (!speedstep_chipset_dev)
  54. return -ENODEV;
  55. /* get PMBASE */
  56. pci_read_config_dword(speedstep_chipset_dev, 0x40, &pmbase);
  57. if (!(pmbase & 0x01)) {
  58. printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
  59. return -ENODEV;
  60. }
  61. pmbase &= 0xFFFFFFFE;
  62. if (!pmbase) {
  63. printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
  64. return -ENODEV;
  65. }
  66. dprintk("pmbase is 0x%x\n", pmbase);
  67. return 0;
  68. }
  69. /**
  70. * speedstep_set_state - set the SpeedStep state
  71. * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
  72. *
  73. * Tries to change the SpeedStep state.
  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. dprintk("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
  87. /* write new state */
  88. value &= 0xFE;
  89. value |= state;
  90. dprintk("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. dprintk("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
  105. if (state == (value & 0x1)) {
  106. dprintk("change to %u MHz succeeded\n", (speedstep_get_processor_frequency(speedstep_processor) / 1000));
  107. } else {
  108. printk (KERN_ERR "cpufreq: change failed - I/O error\n");
  109. }
  110. return;
  111. }
  112. /**
  113. * speedstep_activate - activate SpeedStep control in the chipset
  114. *
  115. * Tries to activate the SpeedStep status and control registers.
  116. * Returns -EINVAL on an unsupported chipset, and zero on success.
  117. */
  118. static int speedstep_activate (void)
  119. {
  120. u16 value = 0;
  121. if (!speedstep_chipset_dev)
  122. return -EINVAL;
  123. pci_read_config_word(speedstep_chipset_dev, 0x00A0, &value);
  124. if (!(value & 0x08)) {
  125. value |= 0x08;
  126. dprintk("activating SpeedStep (TM) registers\n");
  127. pci_write_config_word(speedstep_chipset_dev, 0x00A0, value);
  128. }
  129. return 0;
  130. }
  131. /**
  132. * speedstep_detect_chipset - detect the Southbridge which contains SpeedStep logic
  133. *
  134. * Detects ICH2-M, ICH3-M and ICH4-M so far. The pci_dev points to
  135. * the LPC bridge / PM module which contains all power-management
  136. * functions. Returns the SPEEDSTEP_CHIPSET_-number for the detected
  137. * chipset, or zero on failure.
  138. */
  139. static unsigned int speedstep_detect_chipset (void)
  140. {
  141. speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  142. PCI_DEVICE_ID_INTEL_82801DB_12,
  143. PCI_ANY_ID,
  144. PCI_ANY_ID,
  145. NULL);
  146. if (speedstep_chipset_dev)
  147. return 4; /* 4-M */
  148. speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  149. PCI_DEVICE_ID_INTEL_82801CA_12,
  150. PCI_ANY_ID,
  151. PCI_ANY_ID,
  152. NULL);
  153. if (speedstep_chipset_dev)
  154. return 3; /* 3-M */
  155. speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  156. PCI_DEVICE_ID_INTEL_82801BA_10,
  157. PCI_ANY_ID,
  158. PCI_ANY_ID,
  159. NULL);
  160. if (speedstep_chipset_dev) {
  161. /* speedstep.c causes lockups on Dell Inspirons 8000 and
  162. * 8100 which use a pretty old revision of the 82815
  163. * host brige. Abort on these systems.
  164. */
  165. static struct pci_dev *hostbridge;
  166. u8 rev = 0;
  167. hostbridge = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  168. PCI_DEVICE_ID_INTEL_82815_MC,
  169. PCI_ANY_ID,
  170. PCI_ANY_ID,
  171. NULL);
  172. if (!hostbridge)
  173. return 2; /* 2-M */
  174. pci_read_config_byte(hostbridge, PCI_REVISION_ID, &rev);
  175. if (rev < 5) {
  176. dprintk("hostbridge does not support speedstep\n");
  177. speedstep_chipset_dev = NULL;
  178. pci_dev_put(hostbridge);
  179. return 0;
  180. }
  181. pci_dev_put(hostbridge);
  182. return 2; /* 2-M */
  183. }
  184. return 0;
  185. }
  186. static unsigned int _speedstep_get(cpumask_t cpus)
  187. {
  188. unsigned int speed;
  189. cpumask_t cpus_allowed;
  190. cpus_allowed = current->cpus_allowed;
  191. set_cpus_allowed(current, cpus);
  192. speed = speedstep_get_processor_frequency(speedstep_processor);
  193. set_cpus_allowed(current, cpus_allowed);
  194. dprintk("detected %u kHz as current frequency\n", speed);
  195. return speed;
  196. }
  197. static unsigned int speedstep_get(unsigned int cpu)
  198. {
  199. return _speedstep_get(cpumask_of_cpu(cpu));
  200. }
  201. /**
  202. * speedstep_target - set a new CPUFreq policy
  203. * @policy: new policy
  204. * @target_freq: the target frequency
  205. * @relation: how that frequency relates to achieved frequency (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
  206. *
  207. * Sets a new CPUFreq policy.
  208. */
  209. static int speedstep_target (struct cpufreq_policy *policy,
  210. unsigned int target_freq,
  211. unsigned int relation)
  212. {
  213. unsigned int newstate = 0;
  214. struct cpufreq_freqs freqs;
  215. cpumask_t cpus_allowed;
  216. int i;
  217. if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0], target_freq, relation, &newstate))
  218. return -EINVAL;
  219. freqs.old = _speedstep_get(policy->cpus);
  220. freqs.new = speedstep_freqs[newstate].frequency;
  221. freqs.cpu = policy->cpu;
  222. dprintk("transiting from %u to %u kHz\n", freqs.old, freqs.new);
  223. /* no transition necessary */
  224. if (freqs.old == freqs.new)
  225. return 0;
  226. cpus_allowed = current->cpus_allowed;
  227. for_each_cpu_mask(i, policy->cpus) {
  228. freqs.cpu = i;
  229. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  230. }
  231. /* switch to physical CPU where state is to be changed */
  232. set_cpus_allowed(current, policy->cpus);
  233. speedstep_set_state(newstate);
  234. /* allow to be run on all CPUs */
  235. set_cpus_allowed(current, cpus_allowed);
  236. for_each_cpu_mask(i, policy->cpus) {
  237. freqs.cpu = i;
  238. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  239. }
  240. return 0;
  241. }
  242. /**
  243. * speedstep_verify - verifies a new CPUFreq policy
  244. * @policy: new policy
  245. *
  246. * Limit must be within speedstep_low_freq and speedstep_high_freq, with
  247. * at least one border included.
  248. */
  249. static int speedstep_verify (struct cpufreq_policy *policy)
  250. {
  251. return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
  252. }
  253. static int speedstep_cpu_init(struct cpufreq_policy *policy)
  254. {
  255. int result = 0;
  256. unsigned int speed;
  257. cpumask_t cpus_allowed;
  258. /* only run on CPU to be set, or on its sibling */
  259. #ifdef CONFIG_SMP
  260. policy->cpus = cpu_sibling_map[policy->cpu];
  261. #endif
  262. cpus_allowed = current->cpus_allowed;
  263. set_cpus_allowed(current, policy->cpus);
  264. /* detect low and high frequency and transition latency */
  265. result = speedstep_get_freqs(speedstep_processor,
  266. &speedstep_freqs[SPEEDSTEP_LOW].frequency,
  267. &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
  268. &policy->cpuinfo.transition_latency,
  269. &speedstep_set_state);
  270. set_cpus_allowed(current, cpus_allowed);
  271. if (result)
  272. return result;
  273. /* get current speed setting */
  274. speed = _speedstep_get(policy->cpus);
  275. if (!speed)
  276. return -EIO;
  277. dprintk("currently at %s speed setting - %i MHz\n",
  278. (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency) ? "low" : "high",
  279. (speed / 1000));
  280. /* cpuinfo and default policy values */
  281. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  282. policy->cur = speed;
  283. result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
  284. if (result)
  285. return (result);
  286. cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
  287. return 0;
  288. }
  289. static int speedstep_cpu_exit(struct cpufreq_policy *policy)
  290. {
  291. cpufreq_frequency_table_put_attr(policy->cpu);
  292. return 0;
  293. }
  294. static struct freq_attr* speedstep_attr[] = {
  295. &cpufreq_freq_attr_scaling_available_freqs,
  296. NULL,
  297. };
  298. static struct cpufreq_driver speedstep_driver = {
  299. .name = "speedstep-ich",
  300. .verify = speedstep_verify,
  301. .target = speedstep_target,
  302. .init = speedstep_cpu_init,
  303. .exit = speedstep_cpu_exit,
  304. .get = speedstep_get,
  305. .owner = THIS_MODULE,
  306. .attr = speedstep_attr,
  307. };
  308. /**
  309. * speedstep_init - initializes the SpeedStep CPUFreq driver
  310. *
  311. * Initializes the SpeedStep support. Returns -ENODEV on unsupported
  312. * devices, -EINVAL on problems during initiatization, and zero on
  313. * success.
  314. */
  315. static int __init speedstep_init(void)
  316. {
  317. /* detect processor */
  318. speedstep_processor = speedstep_detect_processor();
  319. if (!speedstep_processor) {
  320. dprintk("Intel(R) SpeedStep(TM) capable processor not found\n");
  321. return -ENODEV;
  322. }
  323. /* detect chipset */
  324. if (!speedstep_detect_chipset()) {
  325. dprintk("Intel(R) SpeedStep(TM) for this chipset not (yet) available.\n");
  326. return -ENODEV;
  327. }
  328. /* activate speedstep support */
  329. if (speedstep_activate()) {
  330. pci_dev_put(speedstep_chipset_dev);
  331. return -EINVAL;
  332. }
  333. if (speedstep_find_register())
  334. return -ENODEV;
  335. return cpufreq_register_driver(&speedstep_driver);
  336. }
  337. /**
  338. * speedstep_exit - unregisters SpeedStep support
  339. *
  340. * Unregisters SpeedStep support.
  341. */
  342. static void __exit speedstep_exit(void)
  343. {
  344. pci_dev_put(speedstep_chipset_dev);
  345. cpufreq_unregister_driver(&speedstep_driver);
  346. }
  347. MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>, Dominik Brodowski <linux@brodo.de>");
  348. MODULE_DESCRIPTION ("Speedstep driver for Intel mobile processors on chipsets with ICH-M southbridges.");
  349. MODULE_LICENSE ("GPL");
  350. module_init(speedstep_init);
  351. module_exit(speedstep_exit);