18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Intel Running Average Power Limit (RAPL) Driver via MSR interface 48c2ecf20Sopenharmony_ci * Copyright (c) 2019, Intel Corporation. 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/list.h> 118c2ecf20Sopenharmony_ci#include <linux/types.h> 128c2ecf20Sopenharmony_ci#include <linux/device.h> 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci#include <linux/log2.h> 158c2ecf20Sopenharmony_ci#include <linux/bitmap.h> 168c2ecf20Sopenharmony_ci#include <linux/delay.h> 178c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 188c2ecf20Sopenharmony_ci#include <linux/cpu.h> 198c2ecf20Sopenharmony_ci#include <linux/powercap.h> 208c2ecf20Sopenharmony_ci#include <linux/suspend.h> 218c2ecf20Sopenharmony_ci#include <linux/intel_rapl.h> 228c2ecf20Sopenharmony_ci#include <linux/processor.h> 238c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#include <asm/cpu_device_id.h> 268c2ecf20Sopenharmony_ci#include <asm/intel-family.h> 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/* Local defines */ 298c2ecf20Sopenharmony_ci#define MSR_PLATFORM_POWER_LIMIT 0x0000065C 308c2ecf20Sopenharmony_ci#define MSR_VR_CURRENT_CONFIG 0x00000601 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* private data for RAPL MSR Interface */ 338c2ecf20Sopenharmony_cistatic struct rapl_if_priv rapl_msr_priv = { 348c2ecf20Sopenharmony_ci .reg_unit = MSR_RAPL_POWER_UNIT, 358c2ecf20Sopenharmony_ci .regs[RAPL_DOMAIN_PACKAGE] = { 368c2ecf20Sopenharmony_ci MSR_PKG_POWER_LIMIT, MSR_PKG_ENERGY_STATUS, MSR_PKG_PERF_STATUS, 0, MSR_PKG_POWER_INFO }, 378c2ecf20Sopenharmony_ci .regs[RAPL_DOMAIN_PP0] = { 388c2ecf20Sopenharmony_ci MSR_PP0_POWER_LIMIT, MSR_PP0_ENERGY_STATUS, 0, MSR_PP0_POLICY, 0 }, 398c2ecf20Sopenharmony_ci .regs[RAPL_DOMAIN_PP1] = { 408c2ecf20Sopenharmony_ci MSR_PP1_POWER_LIMIT, MSR_PP1_ENERGY_STATUS, 0, MSR_PP1_POLICY, 0 }, 418c2ecf20Sopenharmony_ci .regs[RAPL_DOMAIN_DRAM] = { 428c2ecf20Sopenharmony_ci MSR_DRAM_POWER_LIMIT, MSR_DRAM_ENERGY_STATUS, MSR_DRAM_PERF_STATUS, 0, MSR_DRAM_POWER_INFO }, 438c2ecf20Sopenharmony_ci .regs[RAPL_DOMAIN_PLATFORM] = { 448c2ecf20Sopenharmony_ci MSR_PLATFORM_POWER_LIMIT, MSR_PLATFORM_ENERGY_STATUS, 0, 0, 0}, 458c2ecf20Sopenharmony_ci .limits[RAPL_DOMAIN_PACKAGE] = 2, 468c2ecf20Sopenharmony_ci .limits[RAPL_DOMAIN_PLATFORM] = 2, 478c2ecf20Sopenharmony_ci}; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci/* Handles CPU hotplug on multi-socket systems. 508c2ecf20Sopenharmony_ci * If a CPU goes online as the first CPU of the physical package 518c2ecf20Sopenharmony_ci * we add the RAPL package to the system. Similarly, when the last 528c2ecf20Sopenharmony_ci * CPU of the package is removed, we remove the RAPL package and its 538c2ecf20Sopenharmony_ci * associated domains. Cooling devices are handled accordingly at 548c2ecf20Sopenharmony_ci * per-domain level. 558c2ecf20Sopenharmony_ci */ 568c2ecf20Sopenharmony_cistatic int rapl_cpu_online(unsigned int cpu) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct rapl_package *rp; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci rp = rapl_find_package_domain(cpu, &rapl_msr_priv); 618c2ecf20Sopenharmony_ci if (!rp) { 628c2ecf20Sopenharmony_ci rp = rapl_add_package(cpu, &rapl_msr_priv); 638c2ecf20Sopenharmony_ci if (IS_ERR(rp)) 648c2ecf20Sopenharmony_ci return PTR_ERR(rp); 658c2ecf20Sopenharmony_ci } 668c2ecf20Sopenharmony_ci cpumask_set_cpu(cpu, &rp->cpumask); 678c2ecf20Sopenharmony_ci return 0; 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistatic int rapl_cpu_down_prep(unsigned int cpu) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci struct rapl_package *rp; 738c2ecf20Sopenharmony_ci int lead_cpu; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci rp = rapl_find_package_domain(cpu, &rapl_msr_priv); 768c2ecf20Sopenharmony_ci if (!rp) 778c2ecf20Sopenharmony_ci return 0; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci cpumask_clear_cpu(cpu, &rp->cpumask); 808c2ecf20Sopenharmony_ci lead_cpu = cpumask_first(&rp->cpumask); 818c2ecf20Sopenharmony_ci if (lead_cpu >= nr_cpu_ids) 828c2ecf20Sopenharmony_ci rapl_remove_package(rp); 838c2ecf20Sopenharmony_ci else if (rp->lead_cpu == cpu) 848c2ecf20Sopenharmony_ci rp->lead_cpu = lead_cpu; 858c2ecf20Sopenharmony_ci return 0; 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistatic int rapl_msr_read_raw(int cpu, struct reg_action *ra) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci u32 msr = (u32)ra->reg; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci if (rdmsrl_safe_on_cpu(cpu, msr, &ra->value)) { 938c2ecf20Sopenharmony_ci pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu); 948c2ecf20Sopenharmony_ci return -EIO; 958c2ecf20Sopenharmony_ci } 968c2ecf20Sopenharmony_ci ra->value &= ra->mask; 978c2ecf20Sopenharmony_ci return 0; 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic void rapl_msr_update_func(void *info) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci struct reg_action *ra = info; 1038c2ecf20Sopenharmony_ci u32 msr = (u32)ra->reg; 1048c2ecf20Sopenharmony_ci u64 val; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci ra->err = rdmsrl_safe(msr, &val); 1078c2ecf20Sopenharmony_ci if (ra->err) 1088c2ecf20Sopenharmony_ci return; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci val &= ~ra->mask; 1118c2ecf20Sopenharmony_ci val |= ra->value; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci ra->err = wrmsrl_safe(msr, val); 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic int rapl_msr_write_raw(int cpu, struct reg_action *ra) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci int ret; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci ret = smp_call_function_single(cpu, rapl_msr_update_func, ra, 1); 1218c2ecf20Sopenharmony_ci if (WARN_ON_ONCE(ret)) 1228c2ecf20Sopenharmony_ci return ret; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci return ra->err; 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci/* List of verified CPUs. */ 1288c2ecf20Sopenharmony_cistatic const struct x86_cpu_id pl4_support_ids[] = { 1298c2ecf20Sopenharmony_ci { X86_VENDOR_INTEL, 6, INTEL_FAM6_TIGERLAKE_L, X86_FEATURE_ANY }, 1308c2ecf20Sopenharmony_ci {} 1318c2ecf20Sopenharmony_ci}; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic int rapl_msr_probe(struct platform_device *pdev) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci const struct x86_cpu_id *id = x86_match_cpu(pl4_support_ids); 1368c2ecf20Sopenharmony_ci int ret; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci rapl_msr_priv.read_raw = rapl_msr_read_raw; 1398c2ecf20Sopenharmony_ci rapl_msr_priv.write_raw = rapl_msr_write_raw; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci if (id) { 1428c2ecf20Sopenharmony_ci rapl_msr_priv.limits[RAPL_DOMAIN_PACKAGE] = 3; 1438c2ecf20Sopenharmony_ci rapl_msr_priv.regs[RAPL_DOMAIN_PACKAGE][RAPL_DOMAIN_REG_PL4] = 1448c2ecf20Sopenharmony_ci MSR_VR_CURRENT_CONFIG; 1458c2ecf20Sopenharmony_ci pr_info("PL4 support detected.\n"); 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci rapl_msr_priv.control_type = powercap_register_control_type(NULL, "intel-rapl", NULL); 1498c2ecf20Sopenharmony_ci if (IS_ERR(rapl_msr_priv.control_type)) { 1508c2ecf20Sopenharmony_ci pr_debug("failed to register powercap control_type.\n"); 1518c2ecf20Sopenharmony_ci return PTR_ERR(rapl_msr_priv.control_type); 1528c2ecf20Sopenharmony_ci } 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online", 1558c2ecf20Sopenharmony_ci rapl_cpu_online, rapl_cpu_down_prep); 1568c2ecf20Sopenharmony_ci if (ret < 0) 1578c2ecf20Sopenharmony_ci goto out; 1588c2ecf20Sopenharmony_ci rapl_msr_priv.pcap_rapl_online = ret; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci return 0; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ciout: 1638c2ecf20Sopenharmony_ci if (ret) 1648c2ecf20Sopenharmony_ci powercap_unregister_control_type(rapl_msr_priv.control_type); 1658c2ecf20Sopenharmony_ci return ret; 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic int rapl_msr_remove(struct platform_device *pdev) 1698c2ecf20Sopenharmony_ci{ 1708c2ecf20Sopenharmony_ci cpuhp_remove_state(rapl_msr_priv.pcap_rapl_online); 1718c2ecf20Sopenharmony_ci powercap_unregister_control_type(rapl_msr_priv.control_type); 1728c2ecf20Sopenharmony_ci return 0; 1738c2ecf20Sopenharmony_ci} 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic const struct platform_device_id rapl_msr_ids[] = { 1768c2ecf20Sopenharmony_ci { .name = "intel_rapl_msr", }, 1778c2ecf20Sopenharmony_ci {} 1788c2ecf20Sopenharmony_ci}; 1798c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, rapl_msr_ids); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_cistatic struct platform_driver intel_rapl_msr_driver = { 1828c2ecf20Sopenharmony_ci .probe = rapl_msr_probe, 1838c2ecf20Sopenharmony_ci .remove = rapl_msr_remove, 1848c2ecf20Sopenharmony_ci .id_table = rapl_msr_ids, 1858c2ecf20Sopenharmony_ci .driver = { 1868c2ecf20Sopenharmony_ci .name = "intel_rapl_msr", 1878c2ecf20Sopenharmony_ci }, 1888c2ecf20Sopenharmony_ci}; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cimodule_platform_driver(intel_rapl_msr_driver); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit) control via MSR interface"); 1938c2ecf20Sopenharmony_ciMODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>"); 1948c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 195