p4-clockmod.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Pentium 4/Xeon CPU on demand clock modulation/speed scaling
  3. * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
  4. * (C) 2002 Zwane Mwaikambo <zwane@commfireservices.com>
  5. * (C) 2002 Arjan van de Ven <arjanv@redhat.com>
  6. * (C) 2002 Tora T. Engstad
  7. * All Rights Reserved
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * The author(s) of this software shall not be held liable for damages
  15. * of any nature resulting due to the use of this software. This
  16. * software is provided AS-IS with no warranties.
  17. *
  18. * Date Errata Description
  19. * 20020525 N44, O17 12.5% or 25% DC causes lockup
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/smp.h>
  26. #include <linux/cpufreq.h>
  27. #include <linux/slab.h>
  28. #include <linux/cpumask.h>
  29. #include <asm/processor.h>
  30. #include <asm/msr.h>
  31. #include <asm/timex.h>
  32. #include "speedstep-lib.h"
  33. #define PFX "p4-clockmod: "
  34. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "p4-clockmod", msg)
  35. /*
  36. * Duty Cycle (3bits), note DC_DISABLE is not specified in
  37. * intel docs i just use it to mean disable
  38. */
  39. enum {
  40. DC_RESV, DC_DFLT, DC_25PT, DC_38PT, DC_50PT,
  41. DC_64PT, DC_75PT, DC_88PT, DC_DISABLE
  42. };
  43. #define DC_ENTRIES 8
  44. static int has_N44_O17_errata[NR_CPUS];
  45. static unsigned int stock_freq;
  46. static struct cpufreq_driver p4clockmod_driver;
  47. static unsigned int cpufreq_p4_get(unsigned int cpu);
  48. static int cpufreq_p4_setdc(unsigned int cpu, unsigned int newstate)
  49. {
  50. u32 l, h;
  51. if (!cpu_online(cpu) || (newstate > DC_DISABLE) || (newstate == DC_RESV))
  52. return -EINVAL;
  53. rdmsr_on_cpu(cpu, MSR_IA32_THERM_STATUS, &l, &h);
  54. if (l & 0x01)
  55. dprintk("CPU#%d currently thermal throttled\n", cpu);
  56. if (has_N44_O17_errata[cpu] && (newstate == DC_25PT || newstate == DC_DFLT))
  57. newstate = DC_38PT;
  58. rdmsr_on_cpu(cpu, MSR_IA32_THERM_CONTROL, &l, &h);
  59. if (newstate == DC_DISABLE) {
  60. dprintk("CPU#%d disabling modulation\n", cpu);
  61. wrmsr_on_cpu(cpu, MSR_IA32_THERM_CONTROL, l & ~(1<<4), h);
  62. } else {
  63. dprintk("CPU#%d setting duty cycle to %d%%\n",
  64. cpu, ((125 * newstate) / 10));
  65. /* bits 63 - 5 : reserved
  66. * bit 4 : enable/disable
  67. * bits 3-1 : duty cycle
  68. * bit 0 : reserved
  69. */
  70. l = (l & ~14);
  71. l = l | (1<<4) | ((newstate & 0x7)<<1);
  72. wrmsr_on_cpu(cpu, MSR_IA32_THERM_CONTROL, l, h);
  73. }
  74. return 0;
  75. }
  76. static struct cpufreq_frequency_table p4clockmod_table[] = {
  77. {DC_RESV, CPUFREQ_ENTRY_INVALID},
  78. {DC_DFLT, 0},
  79. {DC_25PT, 0},
  80. {DC_38PT, 0},
  81. {DC_50PT, 0},
  82. {DC_64PT, 0},
  83. {DC_75PT, 0},
  84. {DC_88PT, 0},
  85. {DC_DISABLE, 0},
  86. {DC_RESV, CPUFREQ_TABLE_END},
  87. };
  88. static int cpufreq_p4_target(struct cpufreq_policy *policy,
  89. unsigned int target_freq,
  90. unsigned int relation)
  91. {
  92. unsigned int newstate = DC_RESV;
  93. struct cpufreq_freqs freqs;
  94. int i;
  95. if (cpufreq_frequency_table_target(policy, &p4clockmod_table[0], target_freq, relation, &newstate))
  96. return -EINVAL;
  97. freqs.old = cpufreq_p4_get(policy->cpu);
  98. freqs.new = stock_freq * p4clockmod_table[newstate].index / 8;
  99. if (freqs.new == freqs.old)
  100. return 0;
  101. /* notifiers */
  102. for_each_cpu_mask_nr(i, policy->cpus) {
  103. freqs.cpu = i;
  104. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  105. }
  106. /* run on each logical CPU, see section 13.15.3 of IA32 Intel Architecture Software
  107. * Developer's Manual, Volume 3
  108. */
  109. for_each_cpu_mask_nr(i, policy->cpus)
  110. cpufreq_p4_setdc(i, p4clockmod_table[newstate].index);
  111. /* notifiers */
  112. for_each_cpu_mask_nr(i, policy->cpus) {
  113. freqs.cpu = i;
  114. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  115. }
  116. return 0;
  117. }
  118. static int cpufreq_p4_verify(struct cpufreq_policy *policy)
  119. {
  120. return cpufreq_frequency_table_verify(policy, &p4clockmod_table[0]);
  121. }
  122. static unsigned int cpufreq_p4_get_frequency(struct cpuinfo_x86 *c)
  123. {
  124. if (c->x86 == 0x06) {
  125. if (cpu_has(c, X86_FEATURE_EST))
  126. printk(KERN_WARNING PFX "Warning: EST-capable CPU detected. "
  127. "The acpi-cpufreq module offers voltage scaling"
  128. " in addition of frequency scaling. You should use "
  129. "that instead of p4-clockmod, if possible.\n");
  130. switch (c->x86_model) {
  131. case 0x0E: /* Core */
  132. case 0x0F: /* Core Duo */
  133. case 0x16: /* Celeron Core */
  134. p4clockmod_driver.flags |= CPUFREQ_CONST_LOOPS;
  135. return speedstep_get_processor_frequency(SPEEDSTEP_PROCESSOR_PCORE);
  136. case 0x0D: /* Pentium M (Dothan) */
  137. p4clockmod_driver.flags |= CPUFREQ_CONST_LOOPS;
  138. /* fall through */
  139. case 0x09: /* Pentium M (Banias) */
  140. return speedstep_get_processor_frequency(SPEEDSTEP_PROCESSOR_PM);
  141. }
  142. }
  143. if (c->x86 != 0xF) {
  144. if (!cpu_has(c, X86_FEATURE_EST))
  145. printk(KERN_WARNING PFX "Unknown p4-clockmod-capable CPU. "
  146. "Please send an e-mail to <cpufreq@vger.kernel.org>\n");
  147. return 0;
  148. }
  149. /* on P-4s, the TSC runs with constant frequency independent whether
  150. * throttling is active or not. */
  151. p4clockmod_driver.flags |= CPUFREQ_CONST_LOOPS;
  152. if (speedstep_detect_processor() == SPEEDSTEP_PROCESSOR_P4M) {
  153. printk(KERN_WARNING PFX "Warning: Pentium 4-M detected. "
  154. "The speedstep-ich or acpi cpufreq modules offer "
  155. "voltage scaling in addition of frequency scaling. "
  156. "You should use either one instead of p4-clockmod, "
  157. "if possible.\n");
  158. return speedstep_get_processor_frequency(SPEEDSTEP_PROCESSOR_P4M);
  159. }
  160. return speedstep_get_processor_frequency(SPEEDSTEP_PROCESSOR_P4D);
  161. }
  162. static int cpufreq_p4_cpu_init(struct cpufreq_policy *policy)
  163. {
  164. struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
  165. int cpuid = 0;
  166. unsigned int i;
  167. #ifdef CONFIG_SMP
  168. policy->cpus = per_cpu(cpu_sibling_map, policy->cpu);
  169. #endif
  170. /* Errata workaround */
  171. cpuid = (c->x86 << 8) | (c->x86_model << 4) | c->x86_mask;
  172. switch (cpuid) {
  173. case 0x0f07:
  174. case 0x0f0a:
  175. case 0x0f11:
  176. case 0x0f12:
  177. has_N44_O17_errata[policy->cpu] = 1;
  178. dprintk("has errata -- disabling low frequencies\n");
  179. }
  180. /* get max frequency */
  181. stock_freq = cpufreq_p4_get_frequency(c);
  182. if (!stock_freq)
  183. return -EINVAL;
  184. /* table init */
  185. for (i=1; (p4clockmod_table[i].frequency != CPUFREQ_TABLE_END); i++) {
  186. if ((i<2) && (has_N44_O17_errata[policy->cpu]))
  187. p4clockmod_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  188. else
  189. p4clockmod_table[i].frequency = (stock_freq * i)/8;
  190. }
  191. cpufreq_frequency_table_get_attr(p4clockmod_table, policy->cpu);
  192. /* cpuinfo and default policy values */
  193. policy->cpuinfo.transition_latency = 1000000; /* assumed */
  194. policy->cur = stock_freq;
  195. return cpufreq_frequency_table_cpuinfo(policy, &p4clockmod_table[0]);
  196. }
  197. static int cpufreq_p4_cpu_exit(struct cpufreq_policy *policy)
  198. {
  199. cpufreq_frequency_table_put_attr(policy->cpu);
  200. return 0;
  201. }
  202. static unsigned int cpufreq_p4_get(unsigned int cpu)
  203. {
  204. u32 l, h;
  205. rdmsr_on_cpu(cpu, MSR_IA32_THERM_CONTROL, &l, &h);
  206. if (l & 0x10) {
  207. l = l >> 1;
  208. l &= 0x7;
  209. } else
  210. l = DC_DISABLE;
  211. if (l != DC_DISABLE)
  212. return (stock_freq * l / 8);
  213. return stock_freq;
  214. }
  215. static struct freq_attr* p4clockmod_attr[] = {
  216. &cpufreq_freq_attr_scaling_available_freqs,
  217. NULL,
  218. };
  219. static struct cpufreq_driver p4clockmod_driver = {
  220. .verify = cpufreq_p4_verify,
  221. .target = cpufreq_p4_target,
  222. .init = cpufreq_p4_cpu_init,
  223. .exit = cpufreq_p4_cpu_exit,
  224. .get = cpufreq_p4_get,
  225. .name = "p4-clockmod",
  226. .owner = THIS_MODULE,
  227. .attr = p4clockmod_attr,
  228. .hide_interface = 1,
  229. };
  230. static int __init cpufreq_p4_init(void)
  231. {
  232. struct cpuinfo_x86 *c = &cpu_data(0);
  233. int ret;
  234. /*
  235. * THERM_CONTROL is architectural for IA32 now, so
  236. * we can rely on the capability checks
  237. */
  238. if (c->x86_vendor != X86_VENDOR_INTEL)
  239. return -ENODEV;
  240. if (!test_cpu_cap(c, X86_FEATURE_ACPI) ||
  241. !test_cpu_cap(c, X86_FEATURE_ACC))
  242. return -ENODEV;
  243. ret = cpufreq_register_driver(&p4clockmod_driver);
  244. if (!ret)
  245. printk(KERN_INFO PFX "P4/Xeon(TM) CPU On-Demand Clock Modulation available\n");
  246. return (ret);
  247. }
  248. static void __exit cpufreq_p4_exit(void)
  249. {
  250. cpufreq_unregister_driver(&p4clockmod_driver);
  251. }
  252. MODULE_AUTHOR ("Zwane Mwaikambo <zwane@commfireservices.com>");
  253. MODULE_DESCRIPTION ("cpufreq driver for Pentium(TM) 4/Xeon(TM)");
  254. MODULE_LICENSE ("GPL");
  255. late_initcall(cpufreq_p4_init);
  256. module_exit(cpufreq_p4_exit);