Linux Audio

Check our new training course

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