amd_freq_sensitivity.c 3.8 KB

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