e_powersaver.c 11 KB

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