speedstep-ich.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 "speedstep-lib.h"
  25. /* speedstep_chipset:
  26. * It is necessary to know which chipset is used. As accesses to
  27. * this device occur at various places in this module, we need a
  28. * static struct pci_dev * pointing to that device.
  29. */
  30. static struct pci_dev *speedstep_chipset_dev;
  31. /* speedstep_processor
  32. */
  33. static enum speedstep_processor speedstep_processor;
  34. static u32 pmbase;
  35. /*
  36. * There are only two frequency states for each processor. Values
  37. * are in kHz for the time being.
  38. */
  39. static struct cpufreq_frequency_table speedstep_freqs[] = {
  40. {SPEEDSTEP_HIGH, 0},
  41. {SPEEDSTEP_LOW, 0},
  42. {0, CPUFREQ_TABLE_END},
  43. };
  44. /**
  45. * speedstep_find_register - read the PMBASE address
  46. *
  47. * Returns: -ENODEV if no register could be found
  48. */
  49. static int speedstep_find_register(void)
  50. {
  51. if (!speedstep_chipset_dev)
  52. return -ENODEV;
  53. /* get PMBASE */
  54. pci_read_config_dword(speedstep_chipset_dev, 0x40, &pmbase);
  55. if (!(pmbase & 0x01)) {
  56. printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
  57. return -ENODEV;
  58. }
  59. pmbase &= 0xFFFFFFFE;
  60. if (!pmbase) {
  61. printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
  62. return -ENODEV;
  63. }
  64. pr_debug("pmbase is 0x%x\n", pmbase);
  65. return 0;
  66. }
  67. /**
  68. * speedstep_set_state - set the SpeedStep state
  69. * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
  70. *
  71. * Tries to change the SpeedStep state. Can be called from
  72. * smp_call_function_single.
  73. */
  74. static void speedstep_set_state(unsigned int state)
  75. {
  76. u8 pm2_blk;
  77. u8 value;
  78. unsigned long flags;
  79. if (state > 0x1)
  80. return;
  81. /* Disable IRQs */
  82. local_irq_save(flags);
  83. /* read state */
  84. value = inb(pmbase + 0x50);
  85. pr_debug("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
  86. /* write new state */
  87. value &= 0xFE;
  88. value |= state;
  89. pr_debug("writing 0x%x to pmbase 0x%x + 0x50\n", value, pmbase);
  90. /* Disable bus master arbitration */
  91. pm2_blk = inb(pmbase + 0x20);
  92. pm2_blk |= 0x01;
  93. outb(pm2_blk, (pmbase + 0x20));
  94. /* Actual transition */
  95. outb(value, (pmbase + 0x50));
  96. /* Restore bus master arbitration */
  97. pm2_blk &= 0xfe;
  98. outb(pm2_blk, (pmbase + 0x20));
  99. /* check if transition was successful */
  100. value = inb(pmbase + 0x50);
  101. /* Enable IRQs */
  102. local_irq_restore(flags);
  103. pr_debug("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
  104. if (state == (value & 0x1))
  105. pr_debug("change to %u MHz succeeded\n",
  106. speedstep_get_frequency(speedstep_processor) / 1000);
  107. else
  108. printk(KERN_ERR "cpufreq: change failed - I/O error\n");
  109. return;
  110. }
  111. /* Wrapper for smp_call_function_single. */
  112. static void _speedstep_set_state(void *_state)
  113. {
  114. speedstep_set_state(*(unsigned int *)_state);
  115. }
  116. /**
  117. * speedstep_activate - activate SpeedStep control in the chipset
  118. *
  119. * Tries to activate the SpeedStep status and control registers.
  120. * Returns -EINVAL on an unsupported chipset, and zero on success.
  121. */
  122. static int speedstep_activate(void)
  123. {
  124. u16 value = 0;
  125. if (!speedstep_chipset_dev)
  126. return -EINVAL;
  127. pci_read_config_word(speedstep_chipset_dev, 0x00A0, &value);
  128. if (!(value & 0x08)) {
  129. value |= 0x08;
  130. pr_debug("activating SpeedStep (TM) registers\n");
  131. pci_write_config_word(speedstep_chipset_dev, 0x00A0, value);
  132. }
  133. return 0;
  134. }
  135. /**
  136. * speedstep_detect_chipset - detect the Southbridge which contains SpeedStep logic
  137. *
  138. * Detects ICH2-M, ICH3-M and ICH4-M so far. The pci_dev points to
  139. * the LPC bridge / PM module which contains all power-management
  140. * functions. Returns the SPEEDSTEP_CHIPSET_-number for the detected
  141. * chipset, or zero on failure.
  142. */
  143. static unsigned int speedstep_detect_chipset(void)
  144. {
  145. speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  146. PCI_DEVICE_ID_INTEL_82801DB_12,
  147. PCI_ANY_ID, PCI_ANY_ID,
  148. NULL);
  149. if (speedstep_chipset_dev)
  150. return 4; /* 4-M */
  151. speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  152. PCI_DEVICE_ID_INTEL_82801CA_12,
  153. PCI_ANY_ID, PCI_ANY_ID,
  154. NULL);
  155. if (speedstep_chipset_dev)
  156. return 3; /* 3-M */
  157. speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  158. PCI_DEVICE_ID_INTEL_82801BA_10,
  159. PCI_ANY_ID, PCI_ANY_ID,
  160. NULL);
  161. if (speedstep_chipset_dev) {
  162. /* speedstep.c causes lockups on Dell Inspirons 8000 and
  163. * 8100 which use a pretty old revision of the 82815
  164. * host brige. Abort on these systems.
  165. */
  166. static struct pci_dev *hostbridge;
  167. hostbridge = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  168. PCI_DEVICE_ID_INTEL_82815_MC,
  169. PCI_ANY_ID, PCI_ANY_ID,
  170. NULL);
  171. if (!hostbridge)
  172. return 2; /* 2-M */
  173. if (hostbridge->revision < 5) {
  174. pr_debug("hostbridge does not support speedstep\n");
  175. speedstep_chipset_dev = NULL;
  176. pci_dev_put(hostbridge);
  177. return 0;
  178. }
  179. pci_dev_put(hostbridge);
  180. return 2; /* 2-M */
  181. }
  182. return 0;
  183. }
  184. static void get_freq_data(void *_speed)
  185. {
  186. unsigned int *speed = _speed;
  187. *speed = speedstep_get_frequency(speedstep_processor);
  188. }
  189. static unsigned int speedstep_get(unsigned int cpu)
  190. {
  191. unsigned int speed;
  192. /* You're supposed to ensure CPU is online. */
  193. if (smp_call_function_single(cpu, get_freq_data, &speed, 1) != 0)
  194. BUG();
  195. pr_debug("detected %u kHz as current frequency\n", speed);
  196. return speed;
  197. }
  198. /**
  199. * speedstep_target - set a new CPUFreq policy
  200. * @policy: new policy
  201. * @target_freq: the target frequency
  202. * @relation: how that frequency relates to achieved frequency
  203. * (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
  204. *
  205. * Sets a new CPUFreq policy.
  206. */
  207. static int speedstep_target(struct cpufreq_policy *policy,
  208. unsigned int target_freq,
  209. unsigned int relation)
  210. {
  211. unsigned int newstate = 0, policy_cpu;
  212. struct cpufreq_freqs freqs;
  213. int i;
  214. if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0],
  215. target_freq, relation, &newstate))
  216. return -EINVAL;
  217. policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
  218. freqs.old = speedstep_get(policy_cpu);
  219. freqs.new = speedstep_freqs[newstate].frequency;
  220. freqs.cpu = policy->cpu;
  221. pr_debug("transiting from %u to %u kHz\n", freqs.old, freqs.new);
  222. /* no transition necessary */
  223. if (freqs.old == freqs.new)
  224. return 0;
  225. for_each_cpu(i, policy->cpus) {
  226. freqs.cpu = i;
  227. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  228. }
  229. smp_call_function_single(policy_cpu, _speedstep_set_state, &newstate,
  230. true);
  231. for_each_cpu(i, policy->cpus) {
  232. freqs.cpu = i;
  233. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  234. }
  235. return 0;
  236. }
  237. /**
  238. * speedstep_verify - verifies a new CPUFreq policy
  239. * @policy: new policy
  240. *
  241. * Limit must be within speedstep_low_freq and speedstep_high_freq, with
  242. * at least one border included.
  243. */
  244. static int speedstep_verify(struct cpufreq_policy *policy)
  245. {
  246. return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
  247. }
  248. struct get_freqs {
  249. struct cpufreq_policy *policy;
  250. int ret;
  251. };
  252. static void get_freqs_on_cpu(void *_get_freqs)
  253. {
  254. struct get_freqs *get_freqs = _get_freqs;
  255. get_freqs->ret =
  256. speedstep_get_freqs(speedstep_processor,
  257. &speedstep_freqs[SPEEDSTEP_LOW].frequency,
  258. &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
  259. &get_freqs->policy->cpuinfo.transition_latency,
  260. &speedstep_set_state);
  261. }
  262. static int speedstep_cpu_init(struct cpufreq_policy *policy)
  263. {
  264. int result;
  265. unsigned int policy_cpu, speed;
  266. struct get_freqs gf;
  267. /* only run on CPU to be set, or on its sibling */
  268. #ifdef CONFIG_SMP
  269. cpumask_copy(policy->cpus, cpu_sibling_mask(policy->cpu));
  270. #endif
  271. policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
  272. /* detect low and high frequency and transition latency */
  273. gf.policy = policy;
  274. smp_call_function_single(policy_cpu, get_freqs_on_cpu, &gf, 1);
  275. if (gf.ret)
  276. return gf.ret;
  277. /* get current speed setting */
  278. speed = speedstep_get(policy_cpu);
  279. if (!speed)
  280. return -EIO;
  281. pr_debug("currently at %s speed setting - %i MHz\n",
  282. (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency)
  283. ? "low" : "high",
  284. (speed / 1000));
  285. /* cpuinfo and default policy values */
  286. policy->cur = speed;
  287. result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
  288. if (result)
  289. return result;
  290. cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
  291. return 0;
  292. }
  293. static int speedstep_cpu_exit(struct cpufreq_policy *policy)
  294. {
  295. cpufreq_frequency_table_put_attr(policy->cpu);
  296. return 0;
  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-ich",
  304. .verify = speedstep_verify,
  305. .target = speedstep_target,
  306. .init = speedstep_cpu_init,
  307. .exit = speedstep_cpu_exit,
  308. .get = speedstep_get,
  309. .owner = THIS_MODULE,
  310. .attr = speedstep_attr,
  311. };
  312. /**
  313. * speedstep_init - initializes the SpeedStep CPUFreq driver
  314. *
  315. * Initializes the SpeedStep support. Returns -ENODEV on unsupported
  316. * devices, -EINVAL on problems during initiatization, and zero on
  317. * success.
  318. */
  319. static int __init speedstep_init(void)
  320. {
  321. /* detect processor */
  322. speedstep_processor = speedstep_detect_processor();
  323. if (!speedstep_processor) {
  324. pr_debug("Intel(R) SpeedStep(TM) capable processor "
  325. "not found\n");
  326. return -ENODEV;
  327. }
  328. /* detect chipset */
  329. if (!speedstep_detect_chipset()) {
  330. pr_debug("Intel(R) SpeedStep(TM) for this chipset not "
  331. "(yet) available.\n");
  332. return -ENODEV;
  333. }
  334. /* activate speedstep support */
  335. if (speedstep_activate()) {
  336. pci_dev_put(speedstep_chipset_dev);
  337. return -EINVAL;
  338. }
  339. if (speedstep_find_register())
  340. return -ENODEV;
  341. return cpufreq_register_driver(&speedstep_driver);
  342. }
  343. /**
  344. * speedstep_exit - unregisters SpeedStep support
  345. *
  346. * Unregisters SpeedStep support.
  347. */
  348. static void __exit speedstep_exit(void)
  349. {
  350. pci_dev_put(speedstep_chipset_dev);
  351. cpufreq_unregister_driver(&speedstep_driver);
  352. }
  353. MODULE_AUTHOR("Dave Jones <davej@redhat.com>, "
  354. "Dominik Brodowski <linux@brodo.de>");
  355. MODULE_DESCRIPTION("Speedstep driver for Intel mobile processors on chipsets "
  356. "with ICH-M southbridges.");
  357. MODULE_LICENSE("GPL");
  358. module_init(speedstep_init);
  359. module_exit(speedstep_exit);