p4-clockmod.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/config.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/smp.h>
  27. #include <linux/cpufreq.h>
  28. #include <linux/slab.h>
  29. #include <linux/cpumask.h>
  30. #include <linux/sched.h> /* current / set_cpus_allowed() */
  31. #include <asm/processor.h>
  32. #include <asm/msr.h>
  33. #include <asm/timex.h>
  34. #include "speedstep-lib.h"
  35. #define PFX "p4-clockmod: "
  36. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "p4-clockmod", msg)
  37. /*
  38. * Duty Cycle (3bits), note DC_DISABLE is not specified in
  39. * intel docs i just use it to mean disable
  40. */
  41. enum {
  42. DC_RESV, DC_DFLT, DC_25PT, DC_38PT, DC_50PT,
  43. DC_64PT, DC_75PT, DC_88PT, DC_DISABLE
  44. };
  45. #define DC_ENTRIES 8
  46. static int has_N44_O17_errata[NR_CPUS];
  47. static int has_N60_errata[NR_CPUS];
  48. static unsigned int stock_freq;
  49. static struct cpufreq_driver p4clockmod_driver;
  50. static unsigned int cpufreq_p4_get(unsigned int cpu);
  51. static int cpufreq_p4_setdc(unsigned int cpu, unsigned int newstate)
  52. {
  53. u32 l, h;
  54. if (!cpu_online(cpu) || (newstate > DC_DISABLE) || (newstate == DC_RESV))
  55. return -EINVAL;
  56. rdmsr(MSR_IA32_THERM_STATUS, l, h);
  57. if (l & 0x01)
  58. dprintk("CPU#%d currently thermal throttled\n", cpu);
  59. if (has_N44_O17_errata[cpu] && (newstate == DC_25PT || newstate == DC_DFLT))
  60. newstate = DC_38PT;
  61. rdmsr(MSR_IA32_THERM_CONTROL, l, h);
  62. if (newstate == DC_DISABLE) {
  63. dprintk("CPU#%d disabling modulation\n", cpu);
  64. wrmsr(MSR_IA32_THERM_CONTROL, l & ~(1<<4), h);
  65. } else {
  66. dprintk("CPU#%d setting duty cycle to %d%%\n",
  67. cpu, ((125 * newstate) / 10));
  68. /* bits 63 - 5 : reserved
  69. * bit 4 : enable/disable
  70. * bits 3-1 : duty cycle
  71. * bit 0 : reserved
  72. */
  73. l = (l & ~14);
  74. l = l | (1<<4) | ((newstate & 0x7)<<1);
  75. wrmsr(MSR_IA32_THERM_CONTROL, l, h);
  76. }
  77. return 0;
  78. }
  79. static struct cpufreq_frequency_table p4clockmod_table[] = {
  80. {DC_RESV, CPUFREQ_ENTRY_INVALID},
  81. {DC_DFLT, 0},
  82. {DC_25PT, 0},
  83. {DC_38PT, 0},
  84. {DC_50PT, 0},
  85. {DC_64PT, 0},
  86. {DC_75PT, 0},
  87. {DC_88PT, 0},
  88. {DC_DISABLE, 0},
  89. {DC_RESV, CPUFREQ_TABLE_END},
  90. };
  91. static int cpufreq_p4_target(struct cpufreq_policy *policy,
  92. unsigned int target_freq,
  93. unsigned int relation)
  94. {
  95. unsigned int newstate = DC_RESV;
  96. struct cpufreq_freqs freqs;
  97. cpumask_t cpus_allowed;
  98. int i;
  99. if (cpufreq_frequency_table_target(policy, &p4clockmod_table[0], target_freq, relation, &newstate))
  100. return -EINVAL;
  101. freqs.old = cpufreq_p4_get(policy->cpu);
  102. freqs.new = stock_freq * p4clockmod_table[newstate].index / 8;
  103. if (freqs.new == freqs.old)
  104. return 0;
  105. /* notifiers */
  106. for_each_cpu_mask(i, policy->cpus) {
  107. freqs.cpu = i;
  108. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  109. }
  110. /* run on each logical CPU, see section 13.15.3 of IA32 Intel Architecture Software
  111. * Developer's Manual, Volume 3
  112. */
  113. cpus_allowed = current->cpus_allowed;
  114. for_each_cpu_mask(i, policy->cpus) {
  115. cpumask_t this_cpu = cpumask_of_cpu(i);
  116. set_cpus_allowed(current, this_cpu);
  117. BUG_ON(smp_processor_id() != i);
  118. cpufreq_p4_setdc(i, p4clockmod_table[newstate].index);
  119. }
  120. set_cpus_allowed(current, cpus_allowed);
  121. /* notifiers */
  122. for_each_cpu_mask(i, policy->cpus) {
  123. freqs.cpu = i;
  124. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  125. }
  126. return 0;
  127. }
  128. static int cpufreq_p4_verify(struct cpufreq_policy *policy)
  129. {
  130. return cpufreq_frequency_table_verify(policy, &p4clockmod_table[0]);
  131. }
  132. static unsigned int cpufreq_p4_get_frequency(struct cpuinfo_x86 *c)
  133. {
  134. if ((c->x86 == 0x06) && (c->x86_model == 0x09)) {
  135. /* Pentium M (Banias) */
  136. printk(KERN_WARNING PFX "Warning: Pentium M detected. "
  137. "The speedstep_centrino module offers voltage scaling"
  138. " in addition of frequency scaling. You should use "
  139. "that instead of p4-clockmod, if possible.\n");
  140. return speedstep_get_processor_frequency(SPEEDSTEP_PROCESSOR_PM);
  141. }
  142. if ((c->x86 == 0x06) && (c->x86_model == 0x0D)) {
  143. /* Pentium M (Dothan) */
  144. printk(KERN_WARNING PFX "Warning: Pentium M detected. "
  145. "The speedstep_centrino module offers voltage scaling"
  146. " in addition of frequency scaling. You should use "
  147. "that instead of p4-clockmod, if possible.\n");
  148. /* on P-4s, the TSC runs with constant frequency independent whether
  149. * throttling is active or not. */
  150. p4clockmod_driver.flags |= CPUFREQ_CONST_LOOPS;
  151. return speedstep_get_processor_frequency(SPEEDSTEP_PROCESSOR_PM);
  152. }
  153. if (c->x86 != 0xF) {
  154. printk(KERN_WARNING PFX "Unknown p4-clockmod-capable CPU. Please send an e-mail to <linux@brodo.de>\n");
  155. return 0;
  156. }
  157. /* on P-4s, the TSC runs with constant frequency independent whether
  158. * throttling is active or not. */
  159. p4clockmod_driver.flags |= CPUFREQ_CONST_LOOPS;
  160. if (speedstep_detect_processor() == SPEEDSTEP_PROCESSOR_P4M) {
  161. printk(KERN_WARNING PFX "Warning: Pentium 4-M detected. "
  162. "The speedstep-ich or acpi cpufreq modules offer "
  163. "voltage scaling in addition of frequency scaling. "
  164. "You should use either one instead of p4-clockmod, "
  165. "if possible.\n");
  166. return speedstep_get_processor_frequency(SPEEDSTEP_PROCESSOR_P4M);
  167. }
  168. return speedstep_get_processor_frequency(SPEEDSTEP_PROCESSOR_P4D);
  169. }
  170. static int cpufreq_p4_cpu_init(struct cpufreq_policy *policy)
  171. {
  172. struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
  173. int cpuid = 0;
  174. unsigned int i;
  175. #ifdef CONFIG_SMP
  176. policy->cpus = cpu_sibling_map[policy->cpu];
  177. #endif
  178. /* Errata workaround */
  179. cpuid = (c->x86 << 8) | (c->x86_model << 4) | c->x86_mask;
  180. switch (cpuid) {
  181. case 0x0f07:
  182. case 0x0f0a:
  183. case 0x0f11:
  184. case 0x0f12:
  185. has_N44_O17_errata[policy->cpu] = 1;
  186. dprintk("has errata -- disabling low frequencies\n");
  187. break;
  188. case 0x0f29:
  189. has_N60_errata[policy->cpu] = 1;
  190. dprintk("has errata -- disabling frequencies lower than 2ghz\n");
  191. break;
  192. }
  193. /* get max frequency */
  194. stock_freq = cpufreq_p4_get_frequency(c);
  195. if (!stock_freq)
  196. return -EINVAL;
  197. /* table init */
  198. for (i=1; (p4clockmod_table[i].frequency != CPUFREQ_TABLE_END); i++) {
  199. if ((i<2) && (has_N44_O17_errata[policy->cpu]))
  200. p4clockmod_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  201. else if (has_N60_errata[policy->cpu] && ((stock_freq * i)/8) < 2000000)
  202. p4clockmod_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  203. else
  204. p4clockmod_table[i].frequency = (stock_freq * i)/8;
  205. }
  206. cpufreq_frequency_table_get_attr(p4clockmod_table, policy->cpu);
  207. /* cpuinfo and default policy values */
  208. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  209. policy->cpuinfo.transition_latency = 1000000; /* assumed */
  210. policy->cur = stock_freq;
  211. return cpufreq_frequency_table_cpuinfo(policy, &p4clockmod_table[0]);
  212. }
  213. static int cpufreq_p4_cpu_exit(struct cpufreq_policy *policy)
  214. {
  215. cpufreq_frequency_table_put_attr(policy->cpu);
  216. return 0;
  217. }
  218. static unsigned int cpufreq_p4_get(unsigned int cpu)
  219. {
  220. cpumask_t cpus_allowed;
  221. u32 l, h;
  222. cpus_allowed = current->cpus_allowed;
  223. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  224. BUG_ON(smp_processor_id() != cpu);
  225. rdmsr(MSR_IA32_THERM_CONTROL, l, h);
  226. set_cpus_allowed(current, cpus_allowed);
  227. if (l & 0x10) {
  228. l = l >> 1;
  229. l &= 0x7;
  230. } else
  231. l = DC_DISABLE;
  232. if (l != DC_DISABLE)
  233. return (stock_freq * l / 8);
  234. return stock_freq;
  235. }
  236. static struct freq_attr* p4clockmod_attr[] = {
  237. &cpufreq_freq_attr_scaling_available_freqs,
  238. NULL,
  239. };
  240. static struct cpufreq_driver p4clockmod_driver = {
  241. .verify = cpufreq_p4_verify,
  242. .target = cpufreq_p4_target,
  243. .init = cpufreq_p4_cpu_init,
  244. .exit = cpufreq_p4_cpu_exit,
  245. .get = cpufreq_p4_get,
  246. .name = "p4-clockmod",
  247. .owner = THIS_MODULE,
  248. .attr = p4clockmod_attr,
  249. };
  250. static int __init cpufreq_p4_init(void)
  251. {
  252. struct cpuinfo_x86 *c = cpu_data;
  253. int ret;
  254. /*
  255. * THERM_CONTROL is architectural for IA32 now, so
  256. * we can rely on the capability checks
  257. */
  258. if (c->x86_vendor != X86_VENDOR_INTEL)
  259. return -ENODEV;
  260. if (!test_bit(X86_FEATURE_ACPI, c->x86_capability) ||
  261. !test_bit(X86_FEATURE_ACC, c->x86_capability))
  262. return -ENODEV;
  263. ret = cpufreq_register_driver(&p4clockmod_driver);
  264. if (!ret)
  265. printk(KERN_INFO PFX "P4/Xeon(TM) CPU On-Demand Clock Modulation available\n");
  266. return (ret);
  267. }
  268. static void __exit cpufreq_p4_exit(void)
  269. {
  270. cpufreq_unregister_driver(&p4clockmod_driver);
  271. }
  272. MODULE_AUTHOR ("Zwane Mwaikambo <zwane@commfireservices.com>");
  273. MODULE_DESCRIPTION ("cpufreq driver for Pentium(TM) 4/Xeon(TM)");
  274. MODULE_LICENSE ("GPL");
  275. late_initcall(cpufreq_p4_init);
  276. module_exit(cpufreq_p4_exit);