PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/cpufreq/e_powersaver.c

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