xref: /linux/drivers/cpufreq/amd_freq_sensitivity.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/pci.h>
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 #include <asm/cpu_device_id.h>
22 
23 #include "cpufreq_ondemand.h"
24 
25 #define MSR_AMD64_FREQ_SENSITIVITY_ACTUAL	0xc0010080
26 #define MSR_AMD64_FREQ_SENSITIVITY_REFERENCE	0xc0010081
27 #define CLASS_CODE_SHIFT			56
28 #define POWERSAVE_BIAS_MAX			1000
29 #define POWERSAVE_BIAS_DEF			400
30 
31 struct cpu_data_t {
32 	u64 actual;
33 	u64 reference;
34 	unsigned int freq_prev;
35 };
36 
37 static DEFINE_PER_CPU(struct cpu_data_t, cpu_data);
38 
39 static unsigned int amd_powersave_bias_target(struct cpufreq_policy *policy,
40 					      unsigned int freq_next,
41 					      unsigned int relation)
42 {
43 	int sensitivity;
44 	long d_actual, d_reference;
45 	struct msr actual, reference;
46 	struct cpu_data_t *data = &per_cpu(cpu_data, policy->cpu);
47 	struct policy_dbs_info *policy_dbs = policy->governor_data;
48 	struct dbs_data *od_data = policy_dbs->dbs_data;
49 	struct od_dbs_tuners *od_tuners = od_data->tuners;
50 
51 	if (!policy->freq_table)
52 		return freq_next;
53 
54 	rdmsrq_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &actual.q);
55 	rdmsrq_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_REFERENCE, &reference.q);
56 	actual.h &= 0x00ffffff;
57 	reference.h &= 0x00ffffff;
58 
59 	/* counter wrapped around, so stay on current frequency */
60 	if (actual.q < data->actual || reference.q < data->reference) {
61 		freq_next = policy->cur;
62 		goto out;
63 	}
64 
65 	d_actual = actual.q - data->actual;
66 	d_reference = reference.q - data->reference;
67 
68 	/* divide by 0, so stay on current frequency as well */
69 	if (d_reference == 0) {
70 		freq_next = policy->cur;
71 		goto out;
72 	}
73 
74 	sensitivity = POWERSAVE_BIAS_MAX -
75 		(POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
76 
77 	clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
78 
79 	/* this workload is not CPU bound, so choose a lower freq */
80 	if (sensitivity < od_tuners->powersave_bias) {
81 		if (data->freq_prev == policy->cur)
82 			freq_next = policy->cur;
83 
84 		if (freq_next > policy->cur)
85 			freq_next = policy->cur;
86 		else if (freq_next < policy->cur)
87 			freq_next = policy->min;
88 		else {
89 			unsigned int index;
90 
91 			index = cpufreq_table_find_index_h(policy,
92 							   policy->cur - 1,
93 							   relation & CPUFREQ_RELATION_E);
94 			freq_next = policy->freq_table[index].frequency;
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;
110 	struct pci_dev *pcidev;
111 	unsigned int pci_vendor;
112 
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
118 		return -ENODEV;
119 
120 	pcidev = pci_get_device(pci_vendor,
121 			PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
122 
123 	if (!pcidev) {
124 		if (!boot_cpu_has(X86_FEATURE_PROC_FEEDBACK))
125 			return -ENODEV;
126 	} else {
127 		pci_dev_put(pcidev);
128 	}
129 
130 	if (rdmsrq_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
131 		return -ENODEV;
132 
133 	if (!(val >> CLASS_CODE_SHIFT))
134 		return -ENODEV;
135 
136 	od_register_powersave_bias_handler(amd_powersave_bias_target,
137 			POWERSAVE_BIAS_DEF);
138 	return 0;
139 }
140 late_initcall(amd_freq_sensitivity_init);
141 
142 static void __exit amd_freq_sensitivity_exit(void)
143 {
144 	od_unregister_powersave_bias_handler();
145 }
146 module_exit(amd_freq_sensitivity_exit);
147 
148 static const struct x86_cpu_id __maybe_unused amd_freq_sensitivity_ids[] = {
149 	X86_MATCH_FEATURE(X86_FEATURE_PROC_FEEDBACK, NULL),
150 	{}
151 };
152 MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
153 
154 MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>");
155 MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
156 		"the ondemand governor.");
157 MODULE_LICENSE("GPL");
158