amd_freq_sensitivity.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/pci.h>
  17. #include <linux/percpu-defs.h>
  18. #include <linux/init.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <asm/msr.h>
  21. #include <asm/cpufeature.h>
  22. #include "cpufreq_ondemand.h"
  23. #define MSR_AMD64_FREQ_SENSITIVITY_ACTUAL 0xc0010080
  24. #define MSR_AMD64_FREQ_SENSITIVITY_REFERENCE 0xc0010081
  25. #define CLASS_CODE_SHIFT 56
  26. #define POWERSAVE_BIAS_MAX 1000
  27. #define POWERSAVE_BIAS_DEF 400
  28. struct cpu_data_t {
  29. u64 actual;
  30. u64 reference;
  31. unsigned int freq_prev;
  32. };
  33. static DEFINE_PER_CPU(struct cpu_data_t, cpu_data);
  34. static unsigned int amd_powersave_bias_target(struct cpufreq_policy *policy,
  35. unsigned int freq_next,
  36. unsigned int relation)
  37. {
  38. int sensitivity;
  39. long d_actual, d_reference;
  40. struct msr actual, reference;
  41. struct cpu_data_t *data = &per_cpu(cpu_data, policy->cpu);
  42. struct policy_dbs_info *policy_dbs = policy->governor_data;
  43. struct dbs_data *od_data = policy_dbs->dbs_data;
  44. struct od_dbs_tuners *od_tuners = od_data->tuners;
  45. if (!policy->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. index = cpufreq_table_find_index_h(policy,
  79. policy->cur - 1);
  80. freq_next = policy->freq_table[index].frequency;
  81. }
  82. data->freq_prev = freq_next;
  83. } else
  84. data->freq_prev = 0;
  85. out:
  86. data->actual = actual.q;
  87. data->reference = reference.q;
  88. return freq_next;
  89. }
  90. static int __init amd_freq_sensitivity_init(void)
  91. {
  92. u64 val;
  93. struct pci_dev *pcidev;
  94. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  95. return -ENODEV;
  96. pcidev = pci_get_device(PCI_VENDOR_ID_AMD,
  97. PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
  98. if (!pcidev) {
  99. if (!static_cpu_has(X86_FEATURE_PROC_FEEDBACK))
  100. return -ENODEV;
  101. }
  102. if (rdmsrl_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
  103. return -ENODEV;
  104. if (!(val >> CLASS_CODE_SHIFT))
  105. return -ENODEV;
  106. od_register_powersave_bias_handler(amd_powersave_bias_target,
  107. POWERSAVE_BIAS_DEF);
  108. return 0;
  109. }
  110. late_initcall(amd_freq_sensitivity_init);
  111. static void __exit amd_freq_sensitivity_exit(void)
  112. {
  113. od_unregister_powersave_bias_handler();
  114. }
  115. module_exit(amd_freq_sensitivity_exit);
  116. static const struct x86_cpu_id amd_freq_sensitivity_ids[] = {
  117. X86_FEATURE_MATCH(X86_FEATURE_PROC_FEEDBACK),
  118. {}
  119. };
  120. MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
  121. MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>");
  122. MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
  123. "the ondemand governor.");
  124. MODULE_LICENSE("GPL");