e_powersaver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Based on documentation provided by Dave Jones. Thanks!
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2.
  5. *
  6. * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/cpufreq.h>
  12. #include <linux/ioport.h>
  13. #include <linux/slab.h>
  14. #include <linux/timex.h>
  15. #include <linux/io.h>
  16. #include <linux/delay.h>
  17. #include <asm/cpu_device_id.h>
  18. #include <asm/msr.h>
  19. #include <asm/tsc.h>
  20. #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
  21. #include <linux/acpi.h>
  22. #include <acpi/processor.h>
  23. #endif
  24. #define EPS_BRAND_C7M 0
  25. #define EPS_BRAND_C7 1
  26. #define EPS_BRAND_EDEN 2
  27. #define EPS_BRAND_C3 3
  28. #define EPS_BRAND_C7D 4
  29. struct eps_cpu_data {
  30. u32 fsb;
  31. #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
  32. u32 bios_limit;
  33. #endif
  34. struct cpufreq_frequency_table freq_table[];
  35. };
  36. static struct eps_cpu_data *eps_cpu[NR_CPUS];
  37. /* Module parameters */
  38. static int freq_failsafe_off;
  39. static int voltage_failsafe_off;
  40. static int set_max_voltage;
  41. #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
  42. static int ignore_acpi_limit;
  43. static struct acpi_processor_performance *eps_acpi_cpu_perf;
  44. /* Minimum necessary to get acpi_processor_get_bios_limit() working */
  45. static int eps_acpi_init(void)
  46. {
  47. eps_acpi_cpu_perf = kzalloc(sizeof(*eps_acpi_cpu_perf),
  48. GFP_KERNEL);
  49. if (!eps_acpi_cpu_perf)
  50. return -ENOMEM;
  51. if (!zalloc_cpumask_var(&eps_acpi_cpu_perf->shared_cpu_map,
  52. GFP_KERNEL)) {
  53. kfree(eps_acpi_cpu_perf);
  54. eps_acpi_cpu_perf = NULL;
  55. return -ENOMEM;
  56. }
  57. if (acpi_processor_register_performance(eps_acpi_cpu_perf, 0)) {
  58. free_cpumask_var(eps_acpi_cpu_perf->shared_cpu_map);
  59. kfree(eps_acpi_cpu_perf);
  60. eps_acpi_cpu_perf = NULL;
  61. return -EIO;
  62. }
  63. return 0;
  64. }
  65. static int eps_acpi_exit(struct cpufreq_policy *policy)
  66. {
  67. if (eps_acpi_cpu_perf) {
  68. acpi_processor_unregister_performance(0);
  69. free_cpumask_var(eps_acpi_cpu_perf->shared_cpu_map);
  70. kfree(eps_acpi_cpu_perf);
  71. eps_acpi_cpu_perf = NULL;
  72. }
  73. return 0;
  74. }
  75. #endif
  76. static unsigned int eps_get(unsigned int cpu)
  77. {
  78. struct eps_cpu_data *centaur;
  79. u32 lo, hi;
  80. if (cpu)
  81. return 0;
  82. centaur = eps_cpu[cpu];
  83. if (centaur == NULL)
  84. return 0;
  85. /* Return current frequency */
  86. rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
  87. return centaur->fsb * ((lo >> 8) & 0xff);
  88. }
  89. static int eps_set_state(struct eps_cpu_data *centaur,
  90. struct cpufreq_policy *policy,
  91. u32 dest_state)
  92. {
  93. u32 lo, hi;
  94. int i;
  95. /* Wait while CPU is busy */
  96. rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
  97. i = 0;
  98. while (lo & ((1 << 16) | (1 << 17))) {
  99. udelay(16);
  100. rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
  101. i++;
  102. if (unlikely(i > 64)) {
  103. return -ENODEV;
  104. }
  105. }
  106. /* Set new multiplier and voltage */
  107. wrmsr(MSR_IA32_PERF_CTL, dest_state & 0xffff, 0);
  108. /* Wait until transition end */
  109. i = 0;
  110. do {
  111. udelay(16);
  112. rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
  113. i++;
  114. if (unlikely(i > 64)) {
  115. return -ENODEV;
  116. }
  117. } while (lo & ((1 << 16) | (1 << 17)));
  118. #ifdef DEBUG
  119. {
  120. u8 current_multiplier, current_voltage;
  121. /* Print voltage and multiplier */
  122. rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
  123. current_voltage = lo & 0xff;
  124. printk(KERN_INFO "eps: Current voltage = %dmV\n",
  125. current_voltage * 16 + 700);
  126. current_multiplier = (lo >> 8) & 0xff;
  127. printk(KERN_INFO "eps: Current multiplier = %d\n",
  128. current_multiplier);
  129. }
  130. #endif
  131. return 0;
  132. }
  133. static int eps_target(struct cpufreq_policy *policy, unsigned int index)
  134. {
  135. struct eps_cpu_data *centaur;
  136. unsigned int cpu = policy->cpu;
  137. unsigned int dest_state;
  138. int ret;
  139. if (unlikely(eps_cpu[cpu] == NULL))
  140. return -ENODEV;
  141. centaur = eps_cpu[cpu];
  142. /* Make frequency transition */
  143. dest_state = centaur->freq_table[index].driver_data & 0xffff;
  144. ret = eps_set_state(centaur, policy, dest_state);
  145. if (ret)
  146. printk(KERN_ERR "eps: Timeout!\n");
  147. return ret;
  148. }
  149. static int eps_cpu_init(struct cpufreq_policy *policy)
  150. {
  151. unsigned int i;
  152. u32 lo, hi;
  153. u64 val;
  154. u8 current_multiplier, current_voltage;
  155. u8 max_multiplier, max_voltage;
  156. u8 min_multiplier, min_voltage;
  157. u8 brand = 0;
  158. u32 fsb;
  159. struct eps_cpu_data *centaur;
  160. struct cpuinfo_x86 *c = &cpu_data(0);
  161. struct cpufreq_frequency_table *f_table;
  162. int k, step, voltage;
  163. int ret;
  164. int states;
  165. #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
  166. unsigned int limit;
  167. #endif
  168. if (policy->cpu != 0)
  169. return -ENODEV;
  170. /* Check brand */
  171. printk(KERN_INFO "eps: Detected VIA ");
  172. switch (c->x86_model) {
  173. case 10:
  174. rdmsr(0x1153, lo, hi);
  175. brand = (((lo >> 2) ^ lo) >> 18) & 3;
  176. printk(KERN_CONT "Model A ");
  177. break;
  178. case 13:
  179. rdmsr(0x1154, lo, hi);
  180. brand = (((lo >> 4) ^ (lo >> 2))) & 0x000000ff;
  181. printk(KERN_CONT "Model D ");
  182. break;
  183. }
  184. switch (brand) {
  185. case EPS_BRAND_C7M:
  186. printk(KERN_CONT "C7-M\n");
  187. break;
  188. case EPS_BRAND_C7:
  189. printk(KERN_CONT "C7\n");
  190. break;
  191. case EPS_BRAND_EDEN:
  192. printk(KERN_CONT "Eden\n");
  193. break;
  194. case EPS_BRAND_C7D:
  195. printk(KERN_CONT "C7-D\n");
  196. break;
  197. case EPS_BRAND_C3:
  198. printk(KERN_CONT "C3\n");
  199. return -ENODEV;
  200. break;
  201. }
  202. /* Enable Enhanced PowerSaver */
  203. rdmsrl(MSR_IA32_MISC_ENABLE, val);
  204. if (!(val & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
  205. val |= MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP;
  206. wrmsrl(MSR_IA32_MISC_ENABLE, val);
  207. /* Can be locked at 0 */
  208. rdmsrl(MSR_IA32_MISC_ENABLE, val);
  209. if (!(val & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
  210. printk(KERN_INFO "eps: Can't enable Enhanced PowerSaver\n");
  211. return -ENODEV;
  212. }
  213. }
  214. /* Print voltage and multiplier */
  215. rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
  216. current_voltage = lo & 0xff;
  217. printk(KERN_INFO "eps: Current voltage = %dmV\n",
  218. current_voltage * 16 + 700);
  219. current_multiplier = (lo >> 8) & 0xff;
  220. printk(KERN_INFO "eps: Current multiplier = %d\n", current_multiplier);
  221. /* Print limits */
  222. max_voltage = hi & 0xff;
  223. printk(KERN_INFO "eps: Highest voltage = %dmV\n",
  224. max_voltage * 16 + 700);
  225. max_multiplier = (hi >> 8) & 0xff;
  226. printk(KERN_INFO "eps: Highest multiplier = %d\n", max_multiplier);
  227. min_voltage = (hi >> 16) & 0xff;
  228. printk(KERN_INFO "eps: Lowest voltage = %dmV\n",
  229. min_voltage * 16 + 700);
  230. min_multiplier = (hi >> 24) & 0xff;
  231. printk(KERN_INFO "eps: Lowest multiplier = %d\n", min_multiplier);
  232. /* Sanity checks */
  233. if (current_multiplier == 0 || max_multiplier == 0
  234. || min_multiplier == 0)
  235. return -EINVAL;
  236. if (current_multiplier > max_multiplier
  237. || max_multiplier <= min_multiplier)
  238. return -EINVAL;
  239. if (current_voltage > 0x1f || max_voltage > 0x1f)
  240. return -EINVAL;
  241. if (max_voltage < min_voltage
  242. || current_voltage < min_voltage
  243. || current_voltage > max_voltage)
  244. return -EINVAL;
  245. /* Check for systems using underclocked CPU */
  246. if (!freq_failsafe_off && max_multiplier != current_multiplier) {
  247. printk(KERN_INFO "eps: Your processor is running at different "
  248. "frequency then its maximum. Aborting.\n");
  249. printk(KERN_INFO "eps: You can use freq_failsafe_off option "
  250. "to disable this check.\n");
  251. return -EINVAL;
  252. }
  253. if (!voltage_failsafe_off && max_voltage != current_voltage) {
  254. printk(KERN_INFO "eps: Your processor is running at different "
  255. "voltage then its maximum. Aborting.\n");
  256. printk(KERN_INFO "eps: You can use voltage_failsafe_off "
  257. "option to disable this check.\n");
  258. return -EINVAL;
  259. }
  260. /* Calc FSB speed */
  261. fsb = cpu_khz / current_multiplier;
  262. #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
  263. /* Check for ACPI processor speed limit */
  264. if (!ignore_acpi_limit && !eps_acpi_init()) {
  265. if (!acpi_processor_get_bios_limit(policy->cpu, &limit)) {
  266. printk(KERN_INFO "eps: ACPI limit %u.%uGHz\n",
  267. limit/1000000,
  268. (limit%1000000)/10000);
  269. eps_acpi_exit(policy);
  270. /* Check if max_multiplier is in BIOS limits */
  271. if (limit && max_multiplier * fsb > limit) {
  272. printk(KERN_INFO "eps: Aborting.\n");
  273. return -EINVAL;
  274. }
  275. }
  276. }
  277. #endif
  278. /* Allow user to set lower maximum voltage then that reported
  279. * by processor */
  280. if (brand == EPS_BRAND_C7M && set_max_voltage) {
  281. u32 v;
  282. /* Change mV to something hardware can use */
  283. v = (set_max_voltage - 700) / 16;
  284. /* Check if voltage is within limits */
  285. if (v >= min_voltage && v <= max_voltage) {
  286. printk(KERN_INFO "eps: Setting %dmV as maximum.\n",
  287. v * 16 + 700);
  288. max_voltage = v;
  289. }
  290. }
  291. /* Calc number of p-states supported */
  292. if (brand == EPS_BRAND_C7M)
  293. states = max_multiplier - min_multiplier + 1;
  294. else
  295. states = 2;
  296. /* Allocate private data and frequency table for current cpu */
  297. centaur = kzalloc(sizeof(*centaur)
  298. + (states + 1) * sizeof(struct cpufreq_frequency_table),
  299. GFP_KERNEL);
  300. if (!centaur)
  301. return -ENOMEM;
  302. eps_cpu[0] = centaur;
  303. /* Copy basic values */
  304. centaur->fsb = fsb;
  305. #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
  306. centaur->bios_limit = limit;
  307. #endif
  308. /* Fill frequency and MSR value table */
  309. f_table = &centaur->freq_table[0];
  310. if (brand != EPS_BRAND_C7M) {
  311. f_table[0].frequency = fsb * min_multiplier;
  312. f_table[0].driver_data = (min_multiplier << 8) | min_voltage;
  313. f_table[1].frequency = fsb * max_multiplier;
  314. f_table[1].driver_data = (max_multiplier << 8) | max_voltage;
  315. f_table[2].frequency = CPUFREQ_TABLE_END;
  316. } else {
  317. k = 0;
  318. step = ((max_voltage - min_voltage) * 256)
  319. / (max_multiplier - min_multiplier);
  320. for (i = min_multiplier; i <= max_multiplier; i++) {
  321. voltage = (k * step) / 256 + min_voltage;
  322. f_table[k].frequency = fsb * i;
  323. f_table[k].driver_data = (i << 8) | voltage;
  324. k++;
  325. }
  326. f_table[k].frequency = CPUFREQ_TABLE_END;
  327. }
  328. policy->cpuinfo.transition_latency = 140000; /* 844mV -> 700mV in ns */
  329. ret = cpufreq_table_validate_and_show(policy, &centaur->freq_table[0]);
  330. if (ret) {
  331. kfree(centaur);
  332. return ret;
  333. }
  334. return 0;
  335. }
  336. static int eps_cpu_exit(struct cpufreq_policy *policy)
  337. {
  338. unsigned int cpu = policy->cpu;
  339. /* Bye */
  340. kfree(eps_cpu[cpu]);
  341. eps_cpu[cpu] = NULL;
  342. return 0;
  343. }
  344. static struct cpufreq_driver eps_driver = {
  345. .verify = cpufreq_generic_frequency_table_verify,
  346. .target_index = eps_target,
  347. .init = eps_cpu_init,
  348. .exit = eps_cpu_exit,
  349. .get = eps_get,
  350. .name = "e_powersaver",
  351. .attr = cpufreq_generic_attr,
  352. };
  353. /* This driver will work only on Centaur C7 processors with
  354. * Enhanced SpeedStep/PowerSaver registers */
  355. static const struct x86_cpu_id eps_cpu_id[] = {
  356. { X86_VENDOR_CENTAUR, 6, X86_MODEL_ANY, X86_FEATURE_EST },
  357. {}
  358. };
  359. MODULE_DEVICE_TABLE(x86cpu, eps_cpu_id);
  360. static int __init eps_init(void)
  361. {
  362. if (!x86_match_cpu(eps_cpu_id) || boot_cpu_data.x86_model < 10)
  363. return -ENODEV;
  364. if (cpufreq_register_driver(&eps_driver))
  365. return -EINVAL;
  366. return 0;
  367. }
  368. static void __exit eps_exit(void)
  369. {
  370. cpufreq_unregister_driver(&eps_driver);
  371. }
  372. /* Allow user to overclock his machine or to change frequency to higher after
  373. * unloading module */
  374. module_param(freq_failsafe_off, int, 0644);
  375. MODULE_PARM_DESC(freq_failsafe_off, "Disable current vs max frequency check");
  376. module_param(voltage_failsafe_off, int, 0644);
  377. MODULE_PARM_DESC(voltage_failsafe_off, "Disable current vs max voltage check");
  378. #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
  379. module_param(ignore_acpi_limit, int, 0644);
  380. MODULE_PARM_DESC(ignore_acpi_limit, "Don't check ACPI's processor speed limit");
  381. #endif
  382. module_param(set_max_voltage, int, 0644);
  383. MODULE_PARM_DESC(set_max_voltage, "Set maximum CPU voltage (mV) C7-M only");
  384. MODULE_AUTHOR("Rafal Bilski <rafalbilski@interia.pl>");
  385. MODULE_DESCRIPTION("Enhanced PowerSaver driver for VIA C7 CPU's.");
  386. MODULE_LICENSE("GPL");
  387. module_init(eps_init);
  388. module_exit(eps_exit);