amd_freq_sensitivity.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 policy_dbs_info *policy_dbs = policy->governor_data;
  42. struct dbs_data *od_data = policy_dbs->dbs_data;
  43. struct od_dbs_tuners *od_tuners = od_data->tuners;
  44. struct od_cpu_dbs_info_s *od_info =
  45. dbs_governor_of(policy)->get_cpu_dbs_info_s(policy->cpu);
  46. if (!od_info->freq_table)
  47. return freq_next;
  48. rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_ACTUAL,
  49. &actual.l, &actual.h);
  50. rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_REFERENCE,
  51. &reference.l, &reference.h);
  52. actual.h &= 0x00ffffff;
  53. reference.h &= 0x00ffffff;
  54. /* counter wrapped around, so stay on current frequency */
  55. if (actual.q < data->actual || reference.q < data->reference) {
  56. freq_next = policy->cur;
  57. goto out;
  58. }
  59. d_actual = actual.q - data->actual;
  60. d_reference = reference.q - data->reference;
  61. /* divide by 0, so stay on current frequency as well */
  62. if (d_reference == 0) {
  63. freq_next = policy->cur;
  64. goto out;
  65. }
  66. sensitivity = POWERSAVE_BIAS_MAX -
  67. (POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
  68. clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
  69. /* this workload is not CPU bound, so choose a lower freq */
  70. if (sensitivity < od_tuners->powersave_bias) {
  71. if (data->freq_prev == policy->cur)
  72. freq_next = policy->cur;
  73. if (freq_next > policy->cur)
  74. freq_next = policy->cur;
  75. else if (freq_next < policy->cur)
  76. freq_next = policy->min;
  77. else {
  78. unsigned int index;
  79. cpufreq_frequency_table_target(policy,
  80. od_info->freq_table, policy->cur - 1,
  81. CPUFREQ_RELATION_H, &index);
  82. freq_next = od_info->freq_table[index].frequency;
  83. }
  84. data->freq_prev = freq_next;
  85. } else
  86. data->freq_prev = 0;
  87. out:
  88. data->actual = actual.q;
  89. data->reference = reference.q;
  90. return freq_next;
  91. }
  92. static int __init amd_freq_sensitivity_init(void)
  93. {
  94. u64 val;
  95. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  96. return -ENODEV;
  97. if (!static_cpu_has(X86_FEATURE_PROC_FEEDBACK))
  98. return -ENODEV;
  99. if (rdmsrl_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
  100. return -ENODEV;
  101. if (!(val >> CLASS_CODE_SHIFT))
  102. return -ENODEV;
  103. od_register_powersave_bias_handler(amd_powersave_bias_target,
  104. POWERSAVE_BIAS_DEF);
  105. return 0;
  106. }
  107. late_initcall(amd_freq_sensitivity_init);
  108. static void __exit amd_freq_sensitivity_exit(void)
  109. {
  110. od_unregister_powersave_bias_handler();
  111. }
  112. module_exit(amd_freq_sensitivity_exit);
  113. static const struct x86_cpu_id amd_freq_sensitivity_ids[] = {
  114. X86_FEATURE_MATCH(X86_FEATURE_PROC_FEEDBACK),
  115. {}
  116. };
  117. MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
  118. MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>");
  119. MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
  120. "the ondemand governor.");
  121. MODULE_LICENSE("GPL");