e_powersaver.c 11 KB

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