p4-clockmod.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/cpumask.h>
  28. #include <linux/timex.h>
  29. #include <asm/processor.h>
  30. #include <asm/msr.h>
  31. #include <asm/timer.h>
  32. #include <asm/cpu_device_id.h>
  33. #include "speedstep-lib.h"
  34. #define PFX "p4-clockmod: "
  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 ((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. pr_debug("CPU#%d currently thermal throttled\n", cpu);
  56. if (has_N44_O17_errata[cpu] &&
  57. (newstate == DC_25PT || newstate == DC_DFLT))
  58. newstate = DC_38PT;
  59. rdmsr_on_cpu(cpu, MSR_IA32_THERM_CONTROL, &l, &h);
  60. if (newstate == DC_DISABLE) {
  61. pr_debug("CPU#%d disabling modulation\n", cpu);
  62. wrmsr_on_cpu(cpu, MSR_IA32_THERM_CONTROL, l & ~(1<<4), h);
  63. } else {
  64. pr_debug("CPU#%d setting duty cycle to %d%%\n",
  65. cpu, ((125 * newstate) / 10));
  66. /* bits 63 - 5 : reserved
  67. * bit 4 : enable/disable
  68. * bits 3-1 : duty cycle
  69. * bit 0 : reserved
  70. */
  71. l = (l & ~14);
  72. l = l | (1<<4) | ((newstate & 0x7)<<1);
  73. wrmsr_on_cpu(cpu, MSR_IA32_THERM_CONTROL, l, h);
  74. }
  75. return 0;
  76. }
  77. static struct cpufreq_frequency_table p4clockmod_table[] = {
  78. {0, DC_RESV, CPUFREQ_ENTRY_INVALID},
  79. {0, DC_DFLT, 0},
  80. {0, DC_25PT, 0},
  81. {0, DC_38PT, 0},
  82. {0, DC_50PT, 0},
  83. {0, DC_64PT, 0},
  84. {0, DC_75PT, 0},
  85. {0, DC_88PT, 0},
  86. {0, DC_DISABLE, 0},
  87. {0, DC_RESV, CPUFREQ_TABLE_END},
  88. };
  89. static int cpufreq_p4_target(struct cpufreq_policy *policy, unsigned int index)
  90. {
  91. int i;
  92. /* run on each logical CPU,
  93. * see section 13.15.3 of IA32 Intel Architecture Software
  94. * Developer's Manual, Volume 3
  95. */
  96. for_each_cpu(i, policy->cpus)
  97. cpufreq_p4_setdc(i, p4clockmod_table[index].driver_data);
  98. return 0;
  99. }
  100. static unsigned int cpufreq_p4_get_frequency(struct cpuinfo_x86 *c)
  101. {
  102. if (c->x86 == 0x06) {
  103. if (cpu_has(c, X86_FEATURE_EST))
  104. printk_once(KERN_WARNING PFX "Warning: EST-capable "
  105. "CPU detected. The acpi-cpufreq module offers "
  106. "voltage scaling in addition to frequency "
  107. "scaling. You should use that instead of "
  108. "p4-clockmod, if possible.\n");
  109. switch (c->x86_model) {
  110. case 0x0E: /* Core */
  111. case 0x0F: /* Core Duo */
  112. case 0x16: /* Celeron Core */
  113. case 0x1C: /* Atom */
  114. p4clockmod_driver.flags |= CPUFREQ_CONST_LOOPS;
  115. return speedstep_get_frequency(SPEEDSTEP_CPU_PCORE);
  116. case 0x0D: /* Pentium M (Dothan) */
  117. p4clockmod_driver.flags |= CPUFREQ_CONST_LOOPS;
  118. /* fall through */
  119. case 0x09: /* Pentium M (Banias) */
  120. return speedstep_get_frequency(SPEEDSTEP_CPU_PM);
  121. }
  122. }
  123. if (c->x86 != 0xF)
  124. return 0;
  125. /* on P-4s, the TSC runs with constant frequency independent whether
  126. * throttling is active or not. */
  127. p4clockmod_driver.flags |= CPUFREQ_CONST_LOOPS;
  128. if (speedstep_detect_processor() == SPEEDSTEP_CPU_P4M) {
  129. printk(KERN_WARNING PFX "Warning: Pentium 4-M detected. "
  130. "The speedstep-ich or acpi cpufreq modules offer "
  131. "voltage scaling in addition of frequency scaling. "
  132. "You should use either one instead of p4-clockmod, "
  133. "if possible.\n");
  134. return speedstep_get_frequency(SPEEDSTEP_CPU_P4M);
  135. }
  136. return speedstep_get_frequency(SPEEDSTEP_CPU_P4D);
  137. }
  138. static int cpufreq_p4_cpu_init(struct cpufreq_policy *policy)
  139. {
  140. struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
  141. int cpuid = 0;
  142. unsigned int i;
  143. #ifdef CONFIG_SMP
  144. cpumask_copy(policy->cpus, topology_sibling_cpumask(policy->cpu));
  145. #endif
  146. /* Errata workaround */
  147. cpuid = (c->x86 << 8) | (c->x86_model << 4) | c->x86_mask;
  148. switch (cpuid) {
  149. case 0x0f07:
  150. case 0x0f0a:
  151. case 0x0f11:
  152. case 0x0f12:
  153. has_N44_O17_errata[policy->cpu] = 1;
  154. pr_debug("has errata -- disabling low frequencies\n");
  155. }
  156. if (speedstep_detect_processor() == SPEEDSTEP_CPU_P4D &&
  157. c->x86_model < 2) {
  158. /* switch to maximum frequency and measure result */
  159. cpufreq_p4_setdc(policy->cpu, DC_DISABLE);
  160. recalibrate_cpu_khz();
  161. }
  162. /* get max frequency */
  163. stock_freq = cpufreq_p4_get_frequency(c);
  164. if (!stock_freq)
  165. return -EINVAL;
  166. /* table init */
  167. for (i = 1; (p4clockmod_table[i].frequency != CPUFREQ_TABLE_END); i++) {
  168. if ((i < 2) && (has_N44_O17_errata[policy->cpu]))
  169. p4clockmod_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  170. else
  171. p4clockmod_table[i].frequency = (stock_freq * i)/8;
  172. }
  173. /* cpuinfo and default policy values */
  174. /* the transition latency is set to be 1 higher than the maximum
  175. * transition latency of the ondemand governor */
  176. policy->cpuinfo.transition_latency = 10000001;
  177. return cpufreq_table_validate_and_show(policy, &p4clockmod_table[0]);
  178. }
  179. static unsigned int cpufreq_p4_get(unsigned int cpu)
  180. {
  181. u32 l, h;
  182. rdmsr_on_cpu(cpu, MSR_IA32_THERM_CONTROL, &l, &h);
  183. if (l & 0x10) {
  184. l = l >> 1;
  185. l &= 0x7;
  186. } else
  187. l = DC_DISABLE;
  188. if (l != DC_DISABLE)
  189. return stock_freq * l / 8;
  190. return stock_freq;
  191. }
  192. static struct cpufreq_driver p4clockmod_driver = {
  193. .verify = cpufreq_generic_frequency_table_verify,
  194. .target_index = cpufreq_p4_target,
  195. .init = cpufreq_p4_cpu_init,
  196. .get = cpufreq_p4_get,
  197. .name = "p4-clockmod",
  198. .attr = cpufreq_generic_attr,
  199. };
  200. static const struct x86_cpu_id cpufreq_p4_id[] = {
  201. { X86_VENDOR_INTEL, X86_FAMILY_ANY, X86_MODEL_ANY, X86_FEATURE_ACC },
  202. {}
  203. };
  204. /*
  205. * Intentionally no MODULE_DEVICE_TABLE here: this driver should not
  206. * be auto loaded. Please don't add one.
  207. */
  208. static int __init cpufreq_p4_init(void)
  209. {
  210. int ret;
  211. /*
  212. * THERM_CONTROL is architectural for IA32 now, so
  213. * we can rely on the capability checks
  214. */
  215. if (!x86_match_cpu(cpufreq_p4_id) || !boot_cpu_has(X86_FEATURE_ACPI))
  216. return -ENODEV;
  217. ret = cpufreq_register_driver(&p4clockmod_driver);
  218. if (!ret)
  219. printk(KERN_INFO PFX "P4/Xeon(TM) CPU On-Demand Clock "
  220. "Modulation available\n");
  221. return ret;
  222. }
  223. static void __exit cpufreq_p4_exit(void)
  224. {
  225. cpufreq_unregister_driver(&p4clockmod_driver);
  226. }
  227. MODULE_AUTHOR("Zwane Mwaikambo <zwane@commfireservices.com>");
  228. MODULE_DESCRIPTION("cpufreq driver for Pentium(TM) 4/Xeon(TM)");
  229. MODULE_LICENSE("GPL");
  230. late_initcall(cpufreq_p4_init);
  231. module_exit(cpufreq_p4_exit);