Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jacob Shin | 9c5320c | 2013-04-04 16:19:04 +0000 | [diff] [blame] | 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> |
Jacob Shin | 9c5320c | 2013-04-04 16:19:04 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/types.h> |
Akshu Agrawal | 59a3b3a | 2018-01-18 15:51:30 +0530 | [diff] [blame] | 14 | #include <linux/pci.h> |
Jacob Shin | 9c5320c | 2013-04-04 16:19:04 +0000 | [diff] [blame] | 15 | #include <linux/percpu-defs.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/mod_devicetable.h> |
| 18 | |
| 19 | #include <asm/msr.h> |
| 20 | #include <asm/cpufeature.h> |
| 21 | |
Rafael J. Wysocki | 7d5a995 | 2016-02-18 18:40:14 +0100 | [diff] [blame] | 22 | #include "cpufreq_ondemand.h" |
Jacob Shin | 9c5320c | 2013-04-04 16:19:04 +0000 | [diff] [blame] | 23 | |
| 24 | #define MSR_AMD64_FREQ_SENSITIVITY_ACTUAL 0xc0010080 |
| 25 | #define MSR_AMD64_FREQ_SENSITIVITY_REFERENCE 0xc0010081 |
| 26 | #define CLASS_CODE_SHIFT 56 |
| 27 | #define POWERSAVE_BIAS_MAX 1000 |
| 28 | #define POWERSAVE_BIAS_DEF 400 |
| 29 | |
| 30 | struct cpu_data_t { |
| 31 | u64 actual; |
| 32 | u64 reference; |
| 33 | unsigned int freq_prev; |
| 34 | }; |
| 35 | |
| 36 | static DEFINE_PER_CPU(struct cpu_data_t, cpu_data); |
| 37 | |
| 38 | static unsigned int amd_powersave_bias_target(struct cpufreq_policy *policy, |
| 39 | unsigned int freq_next, |
| 40 | unsigned int relation) |
| 41 | { |
| 42 | int sensitivity; |
| 43 | long d_actual, d_reference; |
| 44 | struct msr actual, reference; |
| 45 | struct cpu_data_t *data = &per_cpu(cpu_data, policy->cpu); |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 46 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
| 47 | struct dbs_data *od_data = policy_dbs->dbs_data; |
Jacob Shin | 9c5320c | 2013-04-04 16:19:04 +0000 | [diff] [blame] | 48 | struct od_dbs_tuners *od_tuners = od_data->tuners; |
Jacob Shin | 9c5320c | 2013-04-04 16:19:04 +0000 | [diff] [blame] | 49 | |
Viresh Kumar | 34ac5d7 | 2016-06-03 10:58:48 +0530 | [diff] [blame] | 50 | if (!policy->freq_table) |
Jacob Shin | 9c5320c | 2013-04-04 16:19:04 +0000 | [diff] [blame] | 51 | return freq_next; |
| 52 | |
| 53 | rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, |
| 54 | &actual.l, &actual.h); |
| 55 | rdmsr_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_REFERENCE, |
| 56 | &reference.l, &reference.h); |
| 57 | actual.h &= 0x00ffffff; |
| 58 | reference.h &= 0x00ffffff; |
| 59 | |
| 60 | /* counter wrapped around, so stay on current frequency */ |
| 61 | if (actual.q < data->actual || reference.q < data->reference) { |
| 62 | freq_next = policy->cur; |
| 63 | goto out; |
| 64 | } |
| 65 | |
| 66 | d_actual = actual.q - data->actual; |
| 67 | d_reference = reference.q - data->reference; |
| 68 | |
| 69 | /* divide by 0, so stay on current frequency as well */ |
| 70 | if (d_reference == 0) { |
| 71 | freq_next = policy->cur; |
| 72 | goto out; |
| 73 | } |
| 74 | |
| 75 | sensitivity = POWERSAVE_BIAS_MAX - |
| 76 | (POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference); |
| 77 | |
| 78 | clamp(sensitivity, 0, POWERSAVE_BIAS_MAX); |
| 79 | |
| 80 | /* this workload is not CPU bound, so choose a lower freq */ |
| 81 | if (sensitivity < od_tuners->powersave_bias) { |
| 82 | if (data->freq_prev == policy->cur) |
| 83 | freq_next = policy->cur; |
| 84 | |
| 85 | if (freq_next > policy->cur) |
| 86 | freq_next = policy->cur; |
| 87 | else if (freq_next < policy->cur) |
| 88 | freq_next = policy->min; |
| 89 | else { |
| 90 | unsigned int index; |
| 91 | |
Viresh Kumar | 8257736 | 2016-06-27 09:59:34 +0530 | [diff] [blame] | 92 | index = cpufreq_table_find_index_h(policy, |
| 93 | policy->cur - 1); |
Viresh Kumar | 34ac5d7 | 2016-06-03 10:58:48 +0530 | [diff] [blame] | 94 | freq_next = policy->freq_table[index].frequency; |
Jacob Shin | 9c5320c | 2013-04-04 16:19:04 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | data->freq_prev = freq_next; |
| 98 | } else |
| 99 | data->freq_prev = 0; |
| 100 | |
| 101 | out: |
| 102 | data->actual = actual.q; |
| 103 | data->reference = reference.q; |
| 104 | return freq_next; |
| 105 | } |
| 106 | |
| 107 | static int __init amd_freq_sensitivity_init(void) |
| 108 | { |
| 109 | u64 val; |
Akshu Agrawal | 59a3b3a | 2018-01-18 15:51:30 +0530 | [diff] [blame] | 110 | struct pci_dev *pcidev; |
Pu Wen | cc9690c | 2018-09-23 17:37:38 +0800 | [diff] [blame] | 111 | unsigned int pci_vendor; |
Jacob Shin | 9c5320c | 2013-04-04 16:19:04 +0000 | [diff] [blame] | 112 | |
Pu Wen | cc9690c | 2018-09-23 17:37:38 +0800 | [diff] [blame] | 113 | if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) |
| 114 | pci_vendor = PCI_VENDOR_ID_AMD; |
| 115 | else if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) |
| 116 | pci_vendor = PCI_VENDOR_ID_HYGON; |
| 117 | else |
Jacob Shin | 9c5320c | 2013-04-04 16:19:04 +0000 | [diff] [blame] | 118 | return -ENODEV; |
| 119 | |
Pu Wen | cc9690c | 2018-09-23 17:37:38 +0800 | [diff] [blame] | 120 | pcidev = pci_get_device(pci_vendor, |
Akshu Agrawal | 59a3b3a | 2018-01-18 15:51:30 +0530 | [diff] [blame] | 121 | PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL); |
| 122 | |
| 123 | if (!pcidev) { |
Borislav Petkov | 108ec36 | 2019-03-30 12:20:22 +0100 | [diff] [blame] | 124 | if (!boot_cpu_has(X86_FEATURE_PROC_FEEDBACK)) |
Akshu Agrawal | 59a3b3a | 2018-01-18 15:51:30 +0530 | [diff] [blame] | 125 | return -ENODEV; |
| 126 | } |
Jacob Shin | 9c5320c | 2013-04-04 16:19:04 +0000 | [diff] [blame] | 127 | |
| 128 | if (rdmsrl_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val)) |
| 129 | return -ENODEV; |
| 130 | |
| 131 | if (!(val >> CLASS_CODE_SHIFT)) |
| 132 | return -ENODEV; |
| 133 | |
| 134 | od_register_powersave_bias_handler(amd_powersave_bias_target, |
| 135 | POWERSAVE_BIAS_DEF); |
| 136 | return 0; |
| 137 | } |
| 138 | late_initcall(amd_freq_sensitivity_init); |
| 139 | |
| 140 | static void __exit amd_freq_sensitivity_exit(void) |
| 141 | { |
| 142 | od_unregister_powersave_bias_handler(); |
| 143 | } |
| 144 | module_exit(amd_freq_sensitivity_exit); |
| 145 | |
| 146 | static const struct x86_cpu_id amd_freq_sensitivity_ids[] = { |
| 147 | X86_FEATURE_MATCH(X86_FEATURE_PROC_FEEDBACK), |
| 148 | {} |
| 149 | }; |
| 150 | MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids); |
| 151 | |
| 152 | MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>"); |
| 153 | MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for " |
| 154 | "the ondemand governor."); |
| 155 | MODULE_LICENSE("GPL"); |