amd_freq_sensitivity.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * amd_freq_sensitivity.c: AMD frequency sensitivity feedback powersave bias
  4. * for the ondemand governor.
  5. *
  6. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  7. *
  8. * Author: Jacob Shin <[email protected]>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/pci.h>
  14. #include <linux/percpu-defs.h>
  15. #include <linux/init.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <asm/msr.h>
  18. #include <asm/cpufeature.h>
  19. #include <asm/cpu_device_id.h>
  20. #include "cpufreq_ondemand.h"
  21. #define MSR_AMD64_FREQ_SENSITIVITY_ACTUAL 0xc0010080
  22. #define MSR_AMD64_FREQ_SENSITIVITY_REFERENCE 0xc0010081
  23. #define CLASS_CODE_SHIFT 56
  24. #define POWERSAVE_BIAS_MAX 1000
  25. #define POWERSAVE_BIAS_DEF 400
  26. struct cpu_data_t {
  27. u64 actual;
  28. u64 reference;
  29. unsigned int freq_prev;
  30. };
  31. static DEFINE_PER_CPU(struct cpu_data_t, cpu_data);
  32. static unsigned int amd_powersave_bias_target(struct cpufreq_policy *policy,
  33. unsigned int freq_next,
  34. unsigned int relation)
  35. {
  36. int sensitivity;
  37. long d_actual, d_reference;
  38. struct msr actual, reference;
  39. struct cpu_data_t *data = &per_cpu(cpu_data, policy->cpu);
  40. struct policy_dbs_info *policy_dbs = policy->governor_data;
  41. struct dbs_data *od_data = policy_dbs->dbs_data;
  42. struct od_dbs_tuners *od_tuners = od_data->tuners;
  43. if (!policy->freq_table)
  44. return freq_next;
  45. rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_ACTUAL,
  46. &actual.l, &actual.h);
  47. rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_REFERENCE,
  48. &reference.l, &reference.h);
  49. actual.h &= 0x00ffffff;
  50. reference.h &= 0x00ffffff;
  51. /* counter wrapped around, so stay on current frequency */
  52. if (actual.q < data->actual || reference.q < data->reference) {
  53. freq_next = policy->cur;
  54. goto out;
  55. }
  56. d_actual = actual.q - data->actual;
  57. d_reference = reference.q - data->reference;
  58. /* divide by 0, so stay on current frequency as well */
  59. if (d_reference == 0) {
  60. freq_next = policy->cur;
  61. goto out;
  62. }
  63. sensitivity = POWERSAVE_BIAS_MAX -
  64. (POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
  65. clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
  66. /* this workload is not CPU bound, so choose a lower freq */
  67. if (sensitivity < od_tuners->powersave_bias) {
  68. if (data->freq_prev == policy->cur)
  69. freq_next = policy->cur;
  70. if (freq_next > policy->cur)
  71. freq_next = policy->cur;
  72. else if (freq_next < policy->cur)
  73. freq_next = policy->min;
  74. else {
  75. unsigned int index;
  76. index = cpufreq_table_find_index_h(policy,
  77. policy->cur - 1,
  78. relation & CPUFREQ_RELATION_E);
  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. struct pci_dev *pcidev;
  93. unsigned int pci_vendor;
  94. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  95. pci_vendor = PCI_VENDOR_ID_AMD;
  96. else if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
  97. pci_vendor = PCI_VENDOR_ID_HYGON;
  98. else
  99. return -ENODEV;
  100. pcidev = pci_get_device(pci_vendor,
  101. PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
  102. if (!pcidev) {
  103. if (!boot_cpu_has(X86_FEATURE_PROC_FEEDBACK))
  104. return -ENODEV;
  105. } else {
  106. pci_dev_put(pcidev);
  107. }
  108. if (rdmsrl_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
  109. return -ENODEV;
  110. if (!(val >> CLASS_CODE_SHIFT))
  111. return -ENODEV;
  112. od_register_powersave_bias_handler(amd_powersave_bias_target,
  113. POWERSAVE_BIAS_DEF);
  114. return 0;
  115. }
  116. late_initcall(amd_freq_sensitivity_init);
  117. static void __exit amd_freq_sensitivity_exit(void)
  118. {
  119. od_unregister_powersave_bias_handler();
  120. }
  121. module_exit(amd_freq_sensitivity_exit);
  122. static const struct x86_cpu_id __maybe_unused amd_freq_sensitivity_ids[] = {
  123. X86_MATCH_FEATURE(X86_FEATURE_PROC_FEEDBACK, NULL),
  124. {}
  125. };
  126. MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
  127. MODULE_AUTHOR("Jacob Shin <[email protected]>");
  128. MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
  129. "the ondemand governor.");
  130. MODULE_LICENSE("GPL");