amd_freq_sensitivity.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * amd_freq_sensitivity.c: AMD frequency sensitivity feedback powersave bias
  3. * for the ondemand governor.
  4. *
  5. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Jacob Shin <jacob.shin@amd.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/percpu-defs.h>
  17. #include <linux/init.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <asm/msr.h>
  20. #include <asm/cpufeature.h>
  21. #include "cpufreq_governor.h"
  22. #define MSR_AMD64_FREQ_SENSITIVITY_ACTUAL 0xc0010080
  23. #define MSR_AMD64_FREQ_SENSITIVITY_REFERENCE 0xc0010081
  24. #define CLASS_CODE_SHIFT 56
  25. #define POWERSAVE_BIAS_MAX 1000
  26. #define POWERSAVE_BIAS_DEF 400
  27. struct cpu_data_t {
  28. u64 actual;
  29. u64 reference;
  30. unsigned int freq_prev;
  31. };
  32. static DEFINE_PER_CPU(struct cpu_data_t, cpu_data);
  33. static unsigned int amd_powersave_bias_target(struct cpufreq_policy *policy,
  34. unsigned int freq_next,
  35. unsigned int relation)
  36. {
  37. int sensitivity;
  38. long d_actual, d_reference;
  39. struct msr actual, reference;
  40. struct cpu_data_t *data = &per_cpu(cpu_data, policy->cpu);
  41. struct dbs_data *od_data = policy->governor_data;
  42. struct od_dbs_tuners *od_tuners = od_data->tuners;
  43. struct od_cpu_dbs_info_s *od_info =
  44. od_data->cdata->get_cpu_dbs_info_s(policy->cpu);
  45. if (!od_info->freq_table)
  46. return freq_next;
  47. rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_ACTUAL,
  48. &actual.l, &actual.h);
  49. rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_REFERENCE,
  50. &reference.l, &reference.h);
  51. actual.h &= 0x00ffffff;
  52. reference.h &= 0x00ffffff;
  53. /* counter wrapped around, so stay on current frequency */
  54. if (actual.q < data->actual || reference.q < data->reference) {
  55. freq_next = policy->cur;
  56. goto out;
  57. }
  58. d_actual = actual.q - data->actual;
  59. d_reference = reference.q - data->reference;
  60. /* divide by 0, so stay on current frequency as well */
  61. if (d_reference == 0) {
  62. freq_next = policy->cur;
  63. goto out;
  64. }
  65. sensitivity = POWERSAVE_BIAS_MAX -
  66. (POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
  67. clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
  68. /* this workload is not CPU bound, so choose a lower freq */
  69. if (sensitivity < od_tuners->powersave_bias) {
  70. if (data->freq_prev == policy->cur)
  71. freq_next = policy->cur;
  72. if (freq_next > policy->cur)
  73. freq_next = policy->cur;
  74. else if (freq_next < policy->cur)
  75. freq_next = policy->min;
  76. else {
  77. unsigned int index;
  78. cpufreq_frequency_table_target(policy,
  79. od_info->freq_table, policy->cur - 1,
  80. CPUFREQ_RELATION_H, &index);
  81. freq_next = od_info->freq_table[index].frequency;
  82. }
  83. data->freq_prev = freq_next;
  84. } else
  85. data->freq_prev = 0;
  86. out:
  87. data->actual = actual.q;
  88. data->reference = reference.q;
  89. return freq_next;
  90. }
  91. static int __init amd_freq_sensitivity_init(void)
  92. {
  93. u64 val;
  94. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  95. return -ENODEV;
  96. if (!static_cpu_has(X86_FEATURE_PROC_FEEDBACK))
  97. return -ENODEV;
  98. if (rdmsrl_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
  99. return -ENODEV;
  100. if (!(val >> CLASS_CODE_SHIFT))
  101. return -ENODEV;
  102. od_register_powersave_bias_handler(amd_powersave_bias_target,
  103. POWERSAVE_BIAS_DEF);
  104. return 0;
  105. }
  106. late_initcall(amd_freq_sensitivity_init);
  107. static void __exit amd_freq_sensitivity_exit(void)
  108. {
  109. od_unregister_powersave_bias_handler();
  110. }
  111. module_exit(amd_freq_sensitivity_exit);
  112. static const struct x86_cpu_id amd_freq_sensitivity_ids[] = {
  113. X86_FEATURE_MATCH(X86_FEATURE_PROC_FEEDBACK),
  114. {}
  115. };
  116. MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
  117. MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>");
  118. MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
  119. "the ondemand governor.");
  120. MODULE_LICENSE("GPL");