xref: /linux/drivers/cpufreq/amd_freq_sensitivity.c (revision edbafe65eef2b58625db1e113fbbfb1fe10c0291)
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 
18 #include <asm/msr.h>
19 #include <asm/cpufeature.h>
20 #include <asm/cpu_device_id.h>
21 
22 #include "cpufreq_ondemand.h"
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);
46 	struct policy_dbs_info *policy_dbs = policy->governor_data;
47 	struct dbs_data *od_data = policy_dbs->dbs_data;
48 	struct od_dbs_tuners *od_tuners = od_data->tuners;
49 
50 	if (!policy->freq_table)
51 		return freq_next;
52 
53 	rdmsrq_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &actual.q);
54 	rdmsrq_on_cpu(policy->cpu, MSR_AMD64_FREQ_SENSITIVITY_REFERENCE, &reference.q);
55 	actual.h &= 0x00ffffff;
56 	reference.h &= 0x00ffffff;
57 
58 	/* counter wrapped around, so stay on current frequency */
59 	if (actual.q < data->actual || reference.q < data->reference) {
60 		freq_next = policy->cur;
61 		goto out;
62 	}
63 
64 	d_actual = actual.q - data->actual;
65 	d_reference = reference.q - data->reference;
66 
67 	/* divide by 0, so stay on current frequency as well */
68 	if (d_reference == 0) {
69 		freq_next = policy->cur;
70 		goto out;
71 	}
72 
73 	sensitivity = POWERSAVE_BIAS_MAX -
74 		(POWERSAVE_BIAS_MAX * (d_reference - d_actual) / d_reference);
75 
76 	clamp(sensitivity, 0, POWERSAVE_BIAS_MAX);
77 
78 	/* this workload is not CPU bound, so choose a lower freq */
79 	if (sensitivity < od_tuners->powersave_bias) {
80 		if (data->freq_prev == policy->cur)
81 			freq_next = policy->cur;
82 
83 		if (freq_next > policy->cur)
84 			freq_next = policy->cur;
85 		else if (freq_next < policy->cur)
86 			freq_next = policy->min;
87 		else {
88 			unsigned int index;
89 
90 			index = cpufreq_table_find_index_h(policy,
91 							   policy->cur - 1,
92 							   relation & CPUFREQ_RELATION_E);
93 			freq_next = policy->freq_table[index].frequency;
94 		}
95 
96 		data->freq_prev = freq_next;
97 	} else
98 		data->freq_prev = 0;
99 
100 out:
101 	data->actual = actual.q;
102 	data->reference = reference.q;
103 	return freq_next;
104 }
105 
106 static int __init amd_freq_sensitivity_init(void)
107 {
108 	u64 val;
109 	struct pci_dev *pcidev;
110 	unsigned int pci_vendor;
111 
112 	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
113 		pci_vendor = PCI_VENDOR_ID_AMD;
114 	else if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
115 		pci_vendor = PCI_VENDOR_ID_HYGON;
116 	else
117 		return -ENODEV;
118 
119 	pcidev = pci_get_device(pci_vendor,
120 			PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, NULL);
121 
122 	if (!pcidev) {
123 		if (!boot_cpu_has(X86_FEATURE_PROC_FEEDBACK))
124 			return -ENODEV;
125 	} else {
126 		pci_dev_put(pcidev);
127 	}
128 
129 	if (rdmsrq_safe(MSR_AMD64_FREQ_SENSITIVITY_ACTUAL, &val))
130 		return -ENODEV;
131 
132 	if (!(val >> CLASS_CODE_SHIFT))
133 		return -ENODEV;
134 
135 	od_register_powersave_bias_handler(amd_powersave_bias_target,
136 			POWERSAVE_BIAS_DEF);
137 	return 0;
138 }
139 late_initcall(amd_freq_sensitivity_init);
140 
141 static void __exit amd_freq_sensitivity_exit(void)
142 {
143 	od_unregister_powersave_bias_handler();
144 }
145 module_exit(amd_freq_sensitivity_exit);
146 
147 static const struct x86_cpu_id __maybe_unused amd_freq_sensitivity_ids[] = {
148 	X86_MATCH_FEATURE(X86_FEATURE_PROC_FEEDBACK, NULL),
149 	{}
150 };
151 MODULE_DEVICE_TABLE(x86cpu, amd_freq_sensitivity_ids);
152 
153 MODULE_AUTHOR("Jacob Shin <jacob.shin@amd.com>");
154 MODULE_DESCRIPTION("AMD frequency sensitivity feedback powersave bias for "
155 		"the ondemand governor.");
156 MODULE_LICENSE("GPL");
157