sfi-cpufreq.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * SFI Performance States Driver
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * Author: Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com>
  14. * Author: Srinidhi Kasagar <srinidhi.kasagar@intel.com>
  15. */
  16. #include <linux/cpufreq.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/sfi.h>
  21. #include <linux/slab.h>
  22. #include <linux/smp.h>
  23. #include <asm/msr.h>
  24. struct cpufreq_frequency_table *freq_table;
  25. static struct sfi_freq_table_entry *sfi_cpufreq_array;
  26. static int num_freq_table_entries;
  27. static int sfi_parse_freq(struct sfi_table_header *table)
  28. {
  29. struct sfi_table_simple *sb;
  30. struct sfi_freq_table_entry *pentry;
  31. int totallen;
  32. sb = (struct sfi_table_simple *)table;
  33. num_freq_table_entries = SFI_GET_NUM_ENTRIES(sb,
  34. struct sfi_freq_table_entry);
  35. if (num_freq_table_entries <= 1) {
  36. pr_err("No p-states discovered\n");
  37. return -ENODEV;
  38. }
  39. pentry = (struct sfi_freq_table_entry *)sb->pentry;
  40. totallen = num_freq_table_entries * sizeof(*pentry);
  41. sfi_cpufreq_array = kzalloc(totallen, GFP_KERNEL);
  42. if (!sfi_cpufreq_array)
  43. return -ENOMEM;
  44. memcpy(sfi_cpufreq_array, pentry, totallen);
  45. return 0;
  46. }
  47. static int sfi_cpufreq_target(struct cpufreq_policy *policy, unsigned int index)
  48. {
  49. unsigned int next_perf_state = 0; /* Index into perf table */
  50. u32 lo, hi;
  51. next_perf_state = policy->freq_table[index].driver_data;
  52. rdmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, &lo, &hi);
  53. lo = (lo & ~INTEL_PERF_CTL_MASK) |
  54. ((u32) sfi_cpufreq_array[next_perf_state].ctrl_val &
  55. INTEL_PERF_CTL_MASK);
  56. wrmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, lo, hi);
  57. return 0;
  58. }
  59. static int sfi_cpufreq_cpu_init(struct cpufreq_policy *policy)
  60. {
  61. policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
  62. policy->cpuinfo.transition_latency = 100000; /* 100us */
  63. return cpufreq_table_validate_and_show(policy, freq_table);
  64. }
  65. static struct cpufreq_driver sfi_cpufreq_driver = {
  66. .flags = CPUFREQ_CONST_LOOPS,
  67. .verify = cpufreq_generic_frequency_table_verify,
  68. .target_index = sfi_cpufreq_target,
  69. .init = sfi_cpufreq_cpu_init,
  70. .name = "sfi-cpufreq",
  71. .attr = cpufreq_generic_attr,
  72. };
  73. static int __init sfi_cpufreq_init(void)
  74. {
  75. int ret, i;
  76. /* parse the freq table from SFI */
  77. ret = sfi_table_parse(SFI_SIG_FREQ, NULL, NULL, sfi_parse_freq);
  78. if (ret)
  79. return ret;
  80. freq_table = kzalloc(sizeof(*freq_table) *
  81. (num_freq_table_entries + 1), GFP_KERNEL);
  82. if (!freq_table) {
  83. ret = -ENOMEM;
  84. goto err_free_array;
  85. }
  86. for (i = 0; i < num_freq_table_entries; i++) {
  87. freq_table[i].driver_data = i;
  88. freq_table[i].frequency = sfi_cpufreq_array[i].freq_mhz * 1000;
  89. }
  90. freq_table[i].frequency = CPUFREQ_TABLE_END;
  91. ret = cpufreq_register_driver(&sfi_cpufreq_driver);
  92. if (ret)
  93. goto err_free_tbl;
  94. return ret;
  95. err_free_tbl:
  96. kfree(freq_table);
  97. err_free_array:
  98. kfree(sfi_cpufreq_array);
  99. return ret;
  100. }
  101. late_initcall(sfi_cpufreq_init);
  102. static void __exit sfi_cpufreq_exit(void)
  103. {
  104. cpufreq_unregister_driver(&sfi_cpufreq_driver);
  105. kfree(freq_table);
  106. kfree(sfi_cpufreq_array);
  107. }
  108. module_exit(sfi_cpufreq_exit);
  109. MODULE_AUTHOR("Vishwesh M Rudramuni <vishwesh.m.rudramuni@intel.com>");
  110. MODULE_DESCRIPTION("SFI Performance-States Driver");
  111. MODULE_LICENSE("GPL");