e_powersaver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on documentation provided by Dave Jones. Thanks!
  4. *
  5. * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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 IS_ENABLED(CONFIG_ACPI_PROCESSOR)
  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 IS_ENABLED(CONFIG_ACPI_PROCESSOR)
  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 IS_ENABLED(CONFIG_ACPI_PROCESSOR)
  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. pr_info("Current voltage = %dmV\n", current_voltage * 16 + 700);
  125. current_multiplier = (lo >> 8) & 0xff;
  126. pr_info("Current multiplier = %d\n", current_multiplier);
  127. }
  128. #endif
  129. return 0;
  130. }
  131. static int eps_target(struct cpufreq_policy *policy, unsigned int index)
  132. {
  133. struct eps_cpu_data *centaur;
  134. unsigned int cpu = policy->cpu;
  135. unsigned int dest_state;
  136. int ret;
  137. if (unlikely(eps_cpu[cpu] == NULL))
  138. return -ENODEV;
  139. centaur = eps_cpu[cpu];
  140. /* Make frequency transition */
  141. dest_state = centaur->freq_table[index].driver_data & 0xffff;
  142. ret = eps_set_state(centaur, policy, dest_state);
  143. if (ret)
  144. pr_err("Timeout!\n");
  145. return ret;
  146. }
  147. static int eps_cpu_init(struct cpufreq_policy *policy)
  148. {
  149. unsigned int i;
  150. u32 lo, hi;
  151. u64 val;
  152. u8 current_multiplier, current_voltage;
  153. u8 max_multiplier, max_voltage;
  154. u8 min_multiplier, min_voltage;
  155. u8 brand = 0;
  156. u32 fsb;
  157. struct eps_cpu_data *centaur;
  158. struct cpuinfo_x86 *c = &cpu_data(0);
  159. struct cpufreq_frequency_table *f_table;
  160. int k, step, voltage;
  161. int states;
  162. #if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
  163. unsigned int limit;
  164. #endif
  165. if (policy->cpu != 0)
  166. return -ENODEV;
  167. /* Check brand */
  168. pr_info("Detected VIA ");
  169. switch (c->x86_model) {
  170. case 10:
  171. rdmsr(0x1153, lo, hi);
  172. brand = (((lo >> 2) ^ lo) >> 18) & 3;
  173. pr_cont("Model A ");
  174. break;
  175. case 13:
  176. rdmsr(0x1154, lo, hi);
  177. brand = (((lo >> 4) ^ (lo >> 2))) & 0x000000ff;
  178. pr_cont("Model D ");
  179. break;
  180. }
  181. switch (brand) {
  182. case EPS_BRAND_C7M:
  183. pr_cont("C7-M\n");
  184. break;
  185. case EPS_BRAND_C7:
  186. pr_cont("C7\n");
  187. break;
  188. case EPS_BRAND_EDEN:
  189. pr_cont("Eden\n");
  190. break;
  191. case EPS_BRAND_C7D:
  192. pr_cont("C7-D\n");
  193. break;
  194. case EPS_BRAND_C3:
  195. pr_cont("C3\n");
  196. return -ENODEV;
  197. }
  198. /* Enable Enhanced PowerSaver */
  199. rdmsrl(MSR_IA32_MISC_ENABLE, val);
  200. if (!(val & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
  201. val |= MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP;
  202. wrmsrl(MSR_IA32_MISC_ENABLE, val);
  203. /* Can be locked at 0 */
  204. rdmsrl(MSR_IA32_MISC_ENABLE, val);
  205. if (!(val & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
  206. pr_info("Can't enable Enhanced PowerSaver\n");
  207. return -ENODEV;
  208. }
  209. }
  210. /* Print voltage and multiplier */
  211. rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
  212. current_voltage = lo & 0xff;
  213. pr_info("Current voltage = %dmV\n", current_voltage * 16 + 700);
  214. current_multiplier = (lo >> 8) & 0xff;
  215. pr_info("Current multiplier = %d\n", current_multiplier);
  216. /* Print limits */
  217. max_voltage = hi & 0xff;
  218. pr_info("Highest voltage = %dmV\n", max_voltage * 16 + 700);
  219. max_multiplier = (hi >> 8) & 0xff;
  220. pr_info("Highest multiplier = %d\n", max_multiplier);
  221. min_voltage = (hi >> 16) & 0xff;
  222. pr_info("Lowest voltage = %dmV\n", min_voltage * 16 + 700);
  223. min_multiplier = (hi >> 24) & 0xff;
  224. pr_info("Lowest multiplier = %d\n", min_multiplier);
  225. /* Sanity checks */
  226. if (current_multiplier == 0 || max_multiplier == 0
  227. || min_multiplier == 0)
  228. return -EINVAL;
  229. if (current_multiplier > max_multiplier
  230. || max_multiplier <= min_multiplier)
  231. return -EINVAL;
  232. if (current_voltage > 0x1f || max_voltage > 0x1f)
  233. return -EINVAL;
  234. if (max_voltage < min_voltage
  235. || current_voltage < min_voltage
  236. || current_voltage > max_voltage)
  237. return -EINVAL;
  238. /* Check for systems using underclocked CPU */
  239. if (!freq_failsafe_off && max_multiplier != current_multiplier) {
  240. pr_info("Your processor is running at different frequency then its maximum. Aborting.\n");
  241. pr_info("You can use freq_failsafe_off option to disable this check.\n");
  242. return -EINVAL;
  243. }
  244. if (!voltage_failsafe_off && max_voltage != current_voltage) {
  245. pr_info("Your processor is running at different voltage then its maximum. Aborting.\n");
  246. pr_info("You can use voltage_failsafe_off option to disable this check.\n");
  247. return -EINVAL;
  248. }
  249. /* Calc FSB speed */
  250. fsb = cpu_khz / current_multiplier;
  251. #if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
  252. /* Check for ACPI processor speed limit */
  253. if (!ignore_acpi_limit && !eps_acpi_init()) {
  254. if (!acpi_processor_get_bios_limit(policy->cpu, &limit)) {
  255. pr_info("ACPI limit %u.%uGHz\n",
  256. limit/1000000,
  257. (limit%1000000)/10000);
  258. eps_acpi_exit(policy);
  259. /* Check if max_multiplier is in BIOS limits */
  260. if (limit && max_multiplier * fsb > limit) {
  261. pr_info("Aborting\n");
  262. return -EINVAL;
  263. }
  264. }
  265. }
  266. #endif
  267. /* Allow user to set lower maximum voltage then that reported
  268. * by processor */
  269. if (brand == EPS_BRAND_C7M && set_max_voltage) {
  270. u32 v;
  271. /* Change mV to something hardware can use */
  272. v = (set_max_voltage - 700) / 16;
  273. /* Check if voltage is within limits */
  274. if (v >= min_voltage && v <= max_voltage) {
  275. pr_info("Setting %dmV as maximum\n", v * 16 + 700);
  276. max_voltage = v;
  277. }
  278. }
  279. /* Calc number of p-states supported */
  280. if (brand == EPS_BRAND_C7M)
  281. states = max_multiplier - min_multiplier + 1;
  282. else
  283. states = 2;
  284. /* Allocate private data and frequency table for current cpu */
  285. centaur = kzalloc(struct_size(centaur, freq_table, states + 1),
  286. GFP_KERNEL);
  287. if (!centaur)
  288. return -ENOMEM;
  289. eps_cpu[0] = centaur;
  290. /* Copy basic values */
  291. centaur->fsb = fsb;
  292. #if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
  293. centaur->bios_limit = limit;
  294. #endif
  295. /* Fill frequency and MSR value table */
  296. f_table = &centaur->freq_table[0];
  297. if (brand != EPS_BRAND_C7M) {
  298. f_table[0].frequency = fsb * min_multiplier;
  299. f_table[0].driver_data = (min_multiplier << 8) | min_voltage;
  300. f_table[1].frequency = fsb * max_multiplier;
  301. f_table[1].driver_data = (max_multiplier << 8) | max_voltage;
  302. f_table[2].frequency = CPUFREQ_TABLE_END;
  303. } else {
  304. k = 0;
  305. step = ((max_voltage - min_voltage) * 256)
  306. / (max_multiplier - min_multiplier);
  307. for (i = min_multiplier; i <= max_multiplier; i++) {
  308. voltage = (k * step) / 256 + min_voltage;
  309. f_table[k].frequency = fsb * i;
  310. f_table[k].driver_data = (i << 8) | voltage;
  311. k++;
  312. }
  313. f_table[k].frequency = CPUFREQ_TABLE_END;
  314. }
  315. policy->cpuinfo.transition_latency = 140000; /* 844mV -> 700mV in ns */
  316. policy->freq_table = &centaur->freq_table[0];
  317. return 0;
  318. }
  319. static int eps_cpu_exit(struct cpufreq_policy *policy)
  320. {
  321. unsigned int cpu = policy->cpu;
  322. /* Bye */
  323. kfree(eps_cpu[cpu]);
  324. eps_cpu[cpu] = NULL;
  325. return 0;
  326. }
  327. static struct cpufreq_driver eps_driver = {
  328. .verify = cpufreq_generic_frequency_table_verify,
  329. .target_index = eps_target,
  330. .init = eps_cpu_init,
  331. .exit = eps_cpu_exit,
  332. .get = eps_get,
  333. .name = "e_powersaver",
  334. .attr = cpufreq_generic_attr,
  335. };
  336. /* This driver will work only on Centaur C7 processors with
  337. * Enhanced SpeedStep/PowerSaver registers */
  338. static const struct x86_cpu_id eps_cpu_id[] = {
  339. X86_MATCH_VENDOR_FAM_FEATURE(CENTAUR, 6, X86_FEATURE_EST, NULL),
  340. {}
  341. };
  342. MODULE_DEVICE_TABLE(x86cpu, eps_cpu_id);
  343. static int __init eps_init(void)
  344. {
  345. if (!x86_match_cpu(eps_cpu_id) || boot_cpu_data.x86_model < 10)
  346. return -ENODEV;
  347. if (cpufreq_register_driver(&eps_driver))
  348. return -EINVAL;
  349. return 0;
  350. }
  351. static void __exit eps_exit(void)
  352. {
  353. cpufreq_unregister_driver(&eps_driver);
  354. }
  355. /* Allow user to overclock his machine or to change frequency to higher after
  356. * unloading module */
  357. module_param(freq_failsafe_off, int, 0644);
  358. MODULE_PARM_DESC(freq_failsafe_off, "Disable current vs max frequency check");
  359. module_param(voltage_failsafe_off, int, 0644);
  360. MODULE_PARM_DESC(voltage_failsafe_off, "Disable current vs max voltage check");
  361. #if IS_ENABLED(CONFIG_ACPI_PROCESSOR)
  362. module_param(ignore_acpi_limit, int, 0644);
  363. MODULE_PARM_DESC(ignore_acpi_limit, "Don't check ACPI's processor speed limit");
  364. #endif
  365. module_param(set_max_voltage, int, 0644);
  366. MODULE_PARM_DESC(set_max_voltage, "Set maximum CPU voltage (mV) C7-M only");
  367. MODULE_AUTHOR("Rafal Bilski <rafalbilski@interia.pl>");
  368. MODULE_DESCRIPTION("Enhanced PowerSaver driver for VIA C7 CPU's.");
  369. MODULE_LICENSE("GPL");
  370. module_init(eps_init);
  371. module_exit(eps_exit);