amd_freq_sensitivity.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <jacob.shin@amd.com>
  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. freq_next = policy->freq_table[index].frequency;
  79. }
  80. data->freq_prev = freq_next;
  81. } else
  82. data->freq_prev = 0;
  83. out:
  84. data->actual = actual.q;
  85. data->reference = reference.q;
  86. return freq_next;
  87. }
  88. static int __init amd_freq_sensitivity_init(void)
  89. {
  90. u64 val;
  91. struct pci_dev *pcidev;
  92. unsigned int pci_vendor;
  93. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  94. pci_vendor = PCI_VENDOR_ID_AMD;
  95. else if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
  96. pci_vendor = PCI_VENDOR_ID_HYGON;
  97. else
  98. return -ENODEV;
  99. pcidev = pci_get_device(pci_vendor,
  100. PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
  101. if (!pcidev) {
  102. if (!boot_cpu_has(X86_FEATURE_PROC_FEEDBACK))
  103. return -ENODEV;
  104. }
  105. if (rdmsrl_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
  106. return -ENODEV;
  107. if (!(val >> CLASS_CODE_SHIFT))
  108. return -ENODEV;
  109. od_register_powersave_bias_handler(amd_powersave_bias_target,
  110. POWERSAVE_BIAS_DEF);
  111. return 0;
  112. }
  113. late_initcall(amd_freq_sensitivity_init);
  114. static void __exit amd_freq_sensitivity_exit(void)
  115. {
  116. od_unregister_powersave_bias_handler();
  117. }
  118. module_exit(amd_freq_sensitivity_exit);
  119. static const struct x86_cpu_id __maybe_unused amd_freq_sensitivity_ids[] = {
  120. X86_MATCH_FEATURE(X86_FEATURE_PROC_FEEDBACK, NULL),
  121. {}
  122. };
  123. MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
  124. MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>");
  125. MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
  126. "the ondemand governor.");
  127. MODULE_LICENSE("GPL");