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