speedstep-ich.c 11 KB

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