speedstep-ich.c 11 KB

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