Blame view

drivers/powercap/intel_rapl_msr.c 4.99 KB
3382388d7   Zhang Rui   intel_rapl: abstr...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  // SPDX-License-Identifier: GPL-2.0-only
  /*
   * Intel Running Average Power Limit (RAPL) Driver via MSR interface
   * Copyright (c) 2019, Intel Corporation.
   */
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/list.h>
  #include <linux/types.h>
  #include <linux/device.h>
  #include <linux/slab.h>
  #include <linux/log2.h>
  #include <linux/bitmap.h>
  #include <linux/delay.h>
  #include <linux/sysfs.h>
  #include <linux/cpu.h>
  #include <linux/powercap.h>
  #include <linux/suspend.h>
  #include <linux/intel_rapl.h>
  #include <linux/processor.h>
abcfaeb3f   Zhang Rui   intel_rapl: Fix m...
23
  #include <linux/platform_device.h>
3382388d7   Zhang Rui   intel_rapl: abstr...
24
25
26
27
28
29
30
  
  #include <asm/iosf_mbi.h>
  #include <asm/cpu_device_id.h>
  #include <asm/intel-family.h>
  
  /* Local defines */
  #define MSR_PLATFORM_POWER_LIMIT	0x0000065C
8365a898f   Sumeet Pawnikar   powercap: Add Pow...
31
  #define MSR_VR_CURRENT_CONFIG		0x00000601
3382388d7   Zhang Rui   intel_rapl: abstr...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  
  /* private data for RAPL MSR Interface */
  static struct rapl_if_priv rapl_msr_priv = {
  	.reg_unit = MSR_RAPL_POWER_UNIT,
  	.regs[RAPL_DOMAIN_PACKAGE] = {
  		MSR_PKG_POWER_LIMIT, MSR_PKG_ENERGY_STATUS, MSR_PKG_PERF_STATUS, 0, MSR_PKG_POWER_INFO },
  	.regs[RAPL_DOMAIN_PP0] = {
  		MSR_PP0_POWER_LIMIT, MSR_PP0_ENERGY_STATUS, 0, MSR_PP0_POLICY, 0 },
  	.regs[RAPL_DOMAIN_PP1] = {
  		MSR_PP1_POWER_LIMIT, MSR_PP1_ENERGY_STATUS, 0, MSR_PP1_POLICY, 0 },
  	.regs[RAPL_DOMAIN_DRAM] = {
  		MSR_DRAM_POWER_LIMIT, MSR_DRAM_ENERGY_STATUS, MSR_DRAM_PERF_STATUS, 0, MSR_DRAM_POWER_INFO },
  	.regs[RAPL_DOMAIN_PLATFORM] = {
  		MSR_PLATFORM_POWER_LIMIT, MSR_PLATFORM_ENERGY_STATUS, 0, 0, 0},
0c2ddedd8   Zhang Rui   intel_rapl: suppo...
46
  	.limits[RAPL_DOMAIN_PACKAGE] = 2,
f1e8d7560   Zhang Rui   powercap/intel_ra...
47
  	.limits[RAPL_DOMAIN_PLATFORM] = 2,
3382388d7   Zhang Rui   intel_rapl: abstr...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  };
  
  /* Handles CPU hotplug on multi-socket systems.
   * If a CPU goes online as the first CPU of the physical package
   * we add the RAPL package to the system. Similarly, when the last
   * CPU of the package is removed, we remove the RAPL package and its
   * associated domains. Cooling devices are handled accordingly at
   * per-domain level.
   */
  static int rapl_cpu_online(unsigned int cpu)
  {
  	struct rapl_package *rp;
  
  	rp = rapl_find_package_domain(cpu, &rapl_msr_priv);
  	if (!rp) {
  		rp = rapl_add_package(cpu, &rapl_msr_priv);
  		if (IS_ERR(rp))
  			return PTR_ERR(rp);
  	}
  	cpumask_set_cpu(cpu, &rp->cpumask);
  	return 0;
  }
  
  static int rapl_cpu_down_prep(unsigned int cpu)
  {
  	struct rapl_package *rp;
  	int lead_cpu;
  
  	rp = rapl_find_package_domain(cpu, &rapl_msr_priv);
  	if (!rp)
  		return 0;
  
  	cpumask_clear_cpu(cpu, &rp->cpumask);
  	lead_cpu = cpumask_first(&rp->cpumask);
  	if (lead_cpu >= nr_cpu_ids)
  		rapl_remove_package(rp);
  	else if (rp->lead_cpu == cpu)
  		rp->lead_cpu = lead_cpu;
  	return 0;
  }
  
  static int rapl_msr_read_raw(int cpu, struct reg_action *ra)
  {
d978e755a   Zhang Rui   intel_rapl: suppo...
91
92
93
94
95
  	u32 msr = (u32)ra->reg;
  
  	if (rdmsrl_safe_on_cpu(cpu, msr, &ra->value)) {
  		pr_debug("failed to read msr 0x%x on cpu %d
  ", msr, cpu);
3382388d7   Zhang Rui   intel_rapl: abstr...
96
97
98
99
100
101
102
103
104
  		return -EIO;
  	}
  	ra->value &= ra->mask;
  	return 0;
  }
  
  static void rapl_msr_update_func(void *info)
  {
  	struct reg_action *ra = info;
d978e755a   Zhang Rui   intel_rapl: suppo...
105
  	u32 msr = (u32)ra->reg;
3382388d7   Zhang Rui   intel_rapl: abstr...
106
  	u64 val;
d978e755a   Zhang Rui   intel_rapl: suppo...
107
  	ra->err = rdmsrl_safe(msr, &val);
3382388d7   Zhang Rui   intel_rapl: abstr...
108
109
110
111
112
  	if (ra->err)
  		return;
  
  	val &= ~ra->mask;
  	val |= ra->value;
d978e755a   Zhang Rui   intel_rapl: suppo...
113
  	ra->err = wrmsrl_safe(msr, val);
3382388d7   Zhang Rui   intel_rapl: abstr...
114
115
116
117
118
119
120
121
122
123
124
125
  }
  
  static int rapl_msr_write_raw(int cpu, struct reg_action *ra)
  {
  	int ret;
  
  	ret = smp_call_function_single(cpu, rapl_msr_update_func, ra, 1);
  	if (WARN_ON_ONCE(ret))
  		return ret;
  
  	return ra->err;
  }
8365a898f   Sumeet Pawnikar   powercap: Add Pow...
126
127
128
129
130
  /* List of verified CPUs. */
  static const struct x86_cpu_id pl4_support_ids[] = {
  	{ X86_VENDOR_INTEL, 6, INTEL_FAM6_TIGERLAKE_L, X86_FEATURE_ANY },
  	{}
  };
abcfaeb3f   Zhang Rui   intel_rapl: Fix m...
131
  static int rapl_msr_probe(struct platform_device *pdev)
3382388d7   Zhang Rui   intel_rapl: abstr...
132
  {
8365a898f   Sumeet Pawnikar   powercap: Add Pow...
133
  	const struct x86_cpu_id *id = x86_match_cpu(pl4_support_ids);
3382388d7   Zhang Rui   intel_rapl: abstr...
134
135
136
137
  	int ret;
  
  	rapl_msr_priv.read_raw = rapl_msr_read_raw;
  	rapl_msr_priv.write_raw = rapl_msr_write_raw;
8365a898f   Sumeet Pawnikar   powercap: Add Pow...
138
139
140
141
142
143
144
  	if (id) {
  		rapl_msr_priv.limits[RAPL_DOMAIN_PACKAGE] = 3;
  		rapl_msr_priv.regs[RAPL_DOMAIN_PACKAGE][RAPL_DOMAIN_REG_PL4] =
  			MSR_VR_CURRENT_CONFIG;
  		pr_info("PL4 support detected.
  ");
  	}
3382388d7   Zhang Rui   intel_rapl: abstr...
145
146
147
148
149
150
151
152
153
154
155
156
  	rapl_msr_priv.control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
  	if (IS_ERR(rapl_msr_priv.control_type)) {
  		pr_debug("failed to register powercap control_type.
  ");
  		return PTR_ERR(rapl_msr_priv.control_type);
  	}
  
  	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
  				rapl_cpu_online, rapl_cpu_down_prep);
  	if (ret < 0)
  		goto out;
  	rapl_msr_priv.pcap_rapl_online = ret;
3382388d7   Zhang Rui   intel_rapl: abstr...
157
158
159
160
161
162
163
  	return 0;
  
  out:
  	if (ret)
  		powercap_unregister_control_type(rapl_msr_priv.control_type);
  	return ret;
  }
abcfaeb3f   Zhang Rui   intel_rapl: Fix m...
164
  static int rapl_msr_remove(struct platform_device *pdev)
3382388d7   Zhang Rui   intel_rapl: abstr...
165
166
  {
  	cpuhp_remove_state(rapl_msr_priv.pcap_rapl_online);
3382388d7   Zhang Rui   intel_rapl: abstr...
167
  	powercap_unregister_control_type(rapl_msr_priv.control_type);
abcfaeb3f   Zhang Rui   intel_rapl: Fix m...
168
  	return 0;
3382388d7   Zhang Rui   intel_rapl: abstr...
169
  }
abcfaeb3f   Zhang Rui   intel_rapl: Fix m...
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  static const struct platform_device_id rapl_msr_ids[] = {
  	{ .name = "intel_rapl_msr", },
  	{}
  };
  MODULE_DEVICE_TABLE(platform, rapl_msr_ids);
  
  static struct platform_driver intel_rapl_msr_driver = {
  	.probe = rapl_msr_probe,
  	.remove = rapl_msr_remove,
  	.id_table = rapl_msr_ids,
  	.driver = {
  		.name = "intel_rapl_msr",
  	},
  };
  
  module_platform_driver(intel_rapl_msr_driver);
3382388d7   Zhang Rui   intel_rapl: abstr...
186
187
188
189
  
  MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit) control via MSR interface");
  MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
  MODULE_LICENSE("GPL v2");