1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Uncore Frequency Setting
4 * Copyright (c) 2022, Intel Corporation.
7 * Provide interface to set MSR 620 at a granularity of per die. On CPU online,
8 * one control CPU is identified per die to read/write limit. This control CPU
9 * is changed, if the CPU state is changed to offline. When the last CPU is
10 * offline in a die then remove the sysfs object for that die.
11 * The majority of actual code is related to sysfs create and read/write
17 #include <linux/cpu.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/suspend.h>
21 #include <asm/cpu_device_id.h>
22 #include <asm/intel-family.h>
24 #include "uncore-frequency-common.h"
26 /* Max instances for uncore data, one for each die */
27 static int uncore_max_entries __read_mostly;
28 /* Storage for uncore data for all instances */
29 static struct uncore_data *uncore_instances;
30 /* Stores the CPU mask of the target CPUs to use during uncore read/write */
31 static cpumask_t uncore_cpu_mask;
32 /* CPU online callback register instance */
33 static enum cpuhp_state uncore_hp_state __read_mostly;
35 #define MSR_UNCORE_RATIO_LIMIT 0x620
36 #define MSR_UNCORE_PERF_STATUS 0x621
37 #define UNCORE_FREQ_KHZ_MULTIPLIER 100000
39 static int uncore_read_control_freq(struct uncore_data *data, unsigned int *min,
45 if (data->control_cpu < 0)
48 ret = rdmsrl_on_cpu(data->control_cpu, MSR_UNCORE_RATIO_LIMIT, &cap);
52 *max = (cap & 0x7F) * UNCORE_FREQ_KHZ_MULTIPLIER;
53 *min = ((cap & GENMASK(14, 8)) >> 8) * UNCORE_FREQ_KHZ_MULTIPLIER;
58 static int uncore_write_control_freq(struct uncore_data *data, unsigned int input,
64 input /= UNCORE_FREQ_KHZ_MULTIPLIER;
65 if (!input || input > 0x7F)
68 if (data->control_cpu < 0)
71 ret = rdmsrl_on_cpu(data->control_cpu, MSR_UNCORE_RATIO_LIMIT, &cap);
79 cap &= ~GENMASK(14, 8);
83 ret = wrmsrl_on_cpu(data->control_cpu, MSR_UNCORE_RATIO_LIMIT, cap);
87 data->stored_uncore_data = cap;
92 static int uncore_read_freq(struct uncore_data *data, unsigned int *freq)
97 if (data->control_cpu < 0)
100 ret = rdmsrl_on_cpu(data->control_cpu, MSR_UNCORE_PERF_STATUS, &ratio);
104 *freq = (ratio & 0x7F) * UNCORE_FREQ_KHZ_MULTIPLIER;
109 /* Caller provides protection */
110 static struct uncore_data *uncore_get_instance(unsigned int cpu)
112 int id = topology_logical_die_id(cpu);
114 if (id >= 0 && id < uncore_max_entries)
115 return &uncore_instances[id];
120 static int uncore_event_cpu_online(unsigned int cpu)
122 struct uncore_data *data;
125 /* Check if there is an online cpu in the package for uncore MSR */
126 target = cpumask_any_and(&uncore_cpu_mask, topology_die_cpumask(cpu));
127 if (target < nr_cpu_ids)
130 /* Use this CPU on this die as a control CPU */
131 cpumask_set_cpu(cpu, &uncore_cpu_mask);
133 data = uncore_get_instance(cpu);
137 data->package_id = topology_physical_package_id(cpu);
138 data->die_id = topology_die_id(cpu);
140 return uncore_freq_add_entry(data, cpu);
143 static int uncore_event_cpu_offline(unsigned int cpu)
145 struct uncore_data *data;
148 data = uncore_get_instance(cpu);
152 /* Check if existing cpu is used for uncore MSRs */
153 if (!cpumask_test_and_clear_cpu(cpu, &uncore_cpu_mask))
156 /* Find a new cpu to set uncore MSR */
157 target = cpumask_any_but(topology_die_cpumask(cpu), cpu);
159 if (target < nr_cpu_ids) {
160 cpumask_set_cpu(target, &uncore_cpu_mask);
161 uncore_freq_add_entry(data, target);
163 uncore_freq_remove_die_entry(data);
169 static int uncore_pm_notify(struct notifier_block *nb, unsigned long mode,
175 case PM_POST_HIBERNATION:
176 case PM_POST_RESTORE:
177 case PM_POST_SUSPEND:
178 for (i = 0; i < uncore_max_entries; ++i) {
179 struct uncore_data *data = &uncore_instances[i];
181 if (!data || !data->valid || !data->stored_uncore_data)
184 wrmsrl_on_cpu(data->control_cpu, MSR_UNCORE_RATIO_LIMIT,
185 data->stored_uncore_data);
194 static struct notifier_block uncore_pm_nb = {
195 .notifier_call = uncore_pm_notify,
198 static const struct x86_cpu_id intel_uncore_cpu_ids[] = {
199 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G, NULL),
200 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, NULL),
201 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, NULL),
202 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, NULL),
203 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, NULL),
204 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, NULL),
205 X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, NULL),
206 X86_MATCH_INTEL_FAM6_MODEL(EMERALDRAPIDS_X, NULL),
207 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE, NULL),
208 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_L, NULL),
209 X86_MATCH_INTEL_FAM6_MODEL(RAPTORLAKE, NULL),
210 X86_MATCH_INTEL_FAM6_MODEL(RAPTORLAKE_P, NULL),
211 X86_MATCH_INTEL_FAM6_MODEL(RAPTORLAKE_S, NULL),
212 X86_MATCH_INTEL_FAM6_MODEL(METEORLAKE, NULL),
213 X86_MATCH_INTEL_FAM6_MODEL(METEORLAKE_L, NULL),
216 MODULE_DEVICE_TABLE(x86cpu, intel_uncore_cpu_ids);
218 static int __init intel_uncore_init(void)
220 const struct x86_cpu_id *id;
223 if (cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
226 id = x86_match_cpu(intel_uncore_cpu_ids);
230 uncore_max_entries = topology_max_packages() *
231 topology_max_die_per_package();
232 uncore_instances = kcalloc(uncore_max_entries,
233 sizeof(*uncore_instances), GFP_KERNEL);
234 if (!uncore_instances)
237 ret = uncore_freq_common_init(uncore_read_control_freq, uncore_write_control_freq,
242 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
243 "platform/x86/uncore-freq:online",
244 uncore_event_cpu_online,
245 uncore_event_cpu_offline);
249 uncore_hp_state = ret;
251 ret = register_pm_notifier(&uncore_pm_nb);
258 cpuhp_remove_state(uncore_hp_state);
260 uncore_freq_common_exit();
262 kfree(uncore_instances);
266 module_init(intel_uncore_init)
268 static void __exit intel_uncore_exit(void)
272 unregister_pm_notifier(&uncore_pm_nb);
273 cpuhp_remove_state(uncore_hp_state);
274 for (i = 0; i < uncore_max_entries; ++i)
275 uncore_freq_remove_die_entry(&uncore_instances[i]);
276 uncore_freq_common_exit();
277 kfree(uncore_instances);
279 module_exit(intel_uncore_exit)
281 MODULE_IMPORT_NS(INTEL_UNCORE_FREQUENCY);
282 MODULE_LICENSE("GPL v2");
283 MODULE_DESCRIPTION("Intel Uncore Frequency Limits Driver");