Path: blob/master/arch/x86/kernel/cpu/resctrl/monitor.c
29271 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Resource Director Technology(RDT)3* - Monitoring code4*5* Copyright (C) 2017 Intel Corporation6*7* Author:8* Vikas Shivappa <[email protected]>9*10* This replaces the cqm.c based on perf but we reuse a lot of11* code and datastructures originally from Peter Zijlstra and Matt Fleming.12*13* More information about RDT be found in the Intel (R) x86 Architecture14* Software Developer Manual June 2016, volume 3, section 17.17.15*/1617#define pr_fmt(fmt) "resctrl: " fmt1819#include <linux/cpu.h>20#include <linux/resctrl.h>2122#include <asm/cpu_device_id.h>23#include <asm/msr.h>2425#include "internal.h"2627/*28* Global boolean for rdt_monitor which is true if any29* resource monitoring is enabled.30*/31bool rdt_mon_capable;3233#define CF(cf) ((unsigned long)(1048576 * (cf) + 0.5))3435static int snc_nodes_per_l3_cache = 1;3637/*38* The correction factor table is documented in Documentation/filesystems/resctrl.rst.39* If rmid > rmid threshold, MBM total and local values should be multiplied40* by the correction factor.41*42* The original table is modified for better code:43*44* 1. The threshold 0 is changed to rmid count - 1 so don't do correction45* for the case.46* 2. MBM total and local correction table indexed by core counter which is47* equal to (x86_cache_max_rmid + 1) / 8 - 1 and is from 0 up to 27.48* 3. The correction factor is normalized to 2^20 (1048576) so it's faster49* to calculate corrected value by shifting:50* corrected_value = (original_value * correction_factor) >> 2051*/52static const struct mbm_correction_factor_table {53u32 rmidthreshold;54u64 cf;55} mbm_cf_table[] __initconst = {56{7, CF(1.000000)},57{15, CF(1.000000)},58{15, CF(0.969650)},59{31, CF(1.000000)},60{31, CF(1.066667)},61{31, CF(0.969650)},62{47, CF(1.142857)},63{63, CF(1.000000)},64{63, CF(1.185115)},65{63, CF(1.066553)},66{79, CF(1.454545)},67{95, CF(1.000000)},68{95, CF(1.230769)},69{95, CF(1.142857)},70{95, CF(1.066667)},71{127, CF(1.000000)},72{127, CF(1.254863)},73{127, CF(1.185255)},74{151, CF(1.000000)},75{127, CF(1.066667)},76{167, CF(1.000000)},77{159, CF(1.454334)},78{183, CF(1.000000)},79{127, CF(0.969744)},80{191, CF(1.280246)},81{191, CF(1.230921)},82{215, CF(1.000000)},83{191, CF(1.143118)},84};8586static u32 mbm_cf_rmidthreshold __read_mostly = UINT_MAX;8788static u64 mbm_cf __read_mostly;8990static inline u64 get_corrected_mbm_count(u32 rmid, unsigned long val)91{92/* Correct MBM value. */93if (rmid > mbm_cf_rmidthreshold)94val = (val * mbm_cf) >> 20;9596return val;97}9899/*100* When Sub-NUMA Cluster (SNC) mode is not enabled (as indicated by101* "snc_nodes_per_l3_cache == 1") no translation of the RMID value is102* needed. The physical RMID is the same as the logical RMID.103*104* On a platform with SNC mode enabled, Linux enables RMID sharing mode105* via MSR 0xCA0 (see the "RMID Sharing Mode" section in the "Intel106* Resource Director Technology Architecture Specification" for a full107* description of RMID sharing mode).108*109* In RMID sharing mode there are fewer "logical RMID" values available110* to accumulate data ("physical RMIDs" are divided evenly between SNC111* nodes that share an L3 cache). Linux creates an rdt_mon_domain for112* each SNC node.113*114* The value loaded into IA32_PQR_ASSOC is the "logical RMID".115*116* Data is collected independently on each SNC node and can be retrieved117* using the "physical RMID" value computed by this function and loaded118* into IA32_QM_EVTSEL. @cpu can be any CPU in the SNC node.119*120* The scope of the IA32_QM_EVTSEL and IA32_QM_CTR MSRs is at the L3121* cache. So a "physical RMID" may be read from any CPU that shares122* the L3 cache with the desired SNC node, not just from a CPU in123* the specific SNC node.124*/125static int logical_rmid_to_physical_rmid(int cpu, int lrmid)126{127struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;128129if (snc_nodes_per_l3_cache == 1)130return lrmid;131132return lrmid + (cpu_to_node(cpu) % snc_nodes_per_l3_cache) * r->mon.num_rmid;133}134135static int __rmid_read_phys(u32 prmid, enum resctrl_event_id eventid, u64 *val)136{137u64 msr_val;138139/*140* As per the SDM, when IA32_QM_EVTSEL.EvtID (bits 7:0) is configured141* with a valid event code for supported resource type and the bits142* IA32_QM_EVTSEL.RMID (bits 41:32) are configured with valid RMID,143* IA32_QM_CTR.data (bits 61:0) reports the monitored data.144* IA32_QM_CTR.Error (bit 63) and IA32_QM_CTR.Unavailable (bit 62)145* are error bits.146*/147wrmsr(MSR_IA32_QM_EVTSEL, eventid, prmid);148rdmsrq(MSR_IA32_QM_CTR, msr_val);149150if (msr_val & RMID_VAL_ERROR)151return -EIO;152if (msr_val & RMID_VAL_UNAVAIL)153return -EINVAL;154155*val = msr_val;156return 0;157}158159static struct arch_mbm_state *get_arch_mbm_state(struct rdt_hw_mon_domain *hw_dom,160u32 rmid,161enum resctrl_event_id eventid)162{163struct arch_mbm_state *state;164165if (!resctrl_is_mbm_event(eventid))166return NULL;167168state = hw_dom->arch_mbm_states[MBM_STATE_IDX(eventid)];169170return state ? &state[rmid] : NULL;171}172173void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_mon_domain *d,174u32 unused, u32 rmid,175enum resctrl_event_id eventid)176{177struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);178int cpu = cpumask_any(&d->hdr.cpu_mask);179struct arch_mbm_state *am;180u32 prmid;181182am = get_arch_mbm_state(hw_dom, rmid, eventid);183if (am) {184memset(am, 0, sizeof(*am));185186prmid = logical_rmid_to_physical_rmid(cpu, rmid);187/* Record any initial, non-zero count value. */188__rmid_read_phys(prmid, eventid, &am->prev_msr);189}190}191192/*193* Assumes that hardware counters are also reset and thus that there is194* no need to record initial non-zero counts.195*/196void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_mon_domain *d)197{198struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);199enum resctrl_event_id eventid;200int idx;201202for_each_mbm_event_id(eventid) {203if (!resctrl_is_mon_event_enabled(eventid))204continue;205idx = MBM_STATE_IDX(eventid);206memset(hw_dom->arch_mbm_states[idx], 0,207sizeof(*hw_dom->arch_mbm_states[0]) * r->mon.num_rmid);208}209}210211static u64 mbm_overflow_count(u64 prev_msr, u64 cur_msr, unsigned int width)212{213u64 shift = 64 - width, chunks;214215chunks = (cur_msr << shift) - (prev_msr << shift);216return chunks >> shift;217}218219static u64 get_corrected_val(struct rdt_resource *r, struct rdt_mon_domain *d,220u32 rmid, enum resctrl_event_id eventid, u64 msr_val)221{222struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);223struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);224struct arch_mbm_state *am;225u64 chunks;226227am = get_arch_mbm_state(hw_dom, rmid, eventid);228if (am) {229am->chunks += mbm_overflow_count(am->prev_msr, msr_val,230hw_res->mbm_width);231chunks = get_corrected_mbm_count(rmid, am->chunks);232am->prev_msr = msr_val;233} else {234chunks = msr_val;235}236237return chunks * hw_res->mon_scale;238}239240int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d,241u32 unused, u32 rmid, enum resctrl_event_id eventid,242u64 *val, void *ignored)243{244int cpu = cpumask_any(&d->hdr.cpu_mask);245u64 msr_val;246u32 prmid;247int ret;248249resctrl_arch_rmid_read_context_check();250251prmid = logical_rmid_to_physical_rmid(cpu, rmid);252ret = __rmid_read_phys(prmid, eventid, &msr_val);253if (ret)254return ret;255256*val = get_corrected_val(r, d, rmid, eventid, msr_val);257258return 0;259}260261static int __cntr_id_read(u32 cntr_id, u64 *val)262{263u64 msr_val;264265/*266* QM_EVTSEL Register definition:267* =======================================================268* Bits Mnemonic Description269* =======================================================270* 63:44 -- Reserved271* 43:32 RMID RMID or counter ID in ABMC mode272* when reading an MBM event273* 31 ExtendedEvtID Extended Event Identifier274* 30:8 -- Reserved275* 7:0 EvtID Event Identifier276* =======================================================277* The contents of a specific counter can be read by setting the278* following fields in QM_EVTSEL.ExtendedEvtID(=1) and279* QM_EVTSEL.EvtID = L3CacheABMC (=1) and setting QM_EVTSEL.RMID280* to the desired counter ID. Reading the QM_CTR then returns the281* contents of the specified counter. The RMID_VAL_ERROR bit is set282* if the counter configuration is invalid, or if an invalid counter283* ID is set in the QM_EVTSEL.RMID field. The RMID_VAL_UNAVAIL bit284* is set if the counter data is unavailable.285*/286wrmsr(MSR_IA32_QM_EVTSEL, ABMC_EXTENDED_EVT_ID | ABMC_EVT_ID, cntr_id);287rdmsrl(MSR_IA32_QM_CTR, msr_val);288289if (msr_val & RMID_VAL_ERROR)290return -EIO;291if (msr_val & RMID_VAL_UNAVAIL)292return -EINVAL;293294*val = msr_val;295return 0;296}297298void resctrl_arch_reset_cntr(struct rdt_resource *r, struct rdt_mon_domain *d,299u32 unused, u32 rmid, int cntr_id,300enum resctrl_event_id eventid)301{302struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);303struct arch_mbm_state *am;304305am = get_arch_mbm_state(hw_dom, rmid, eventid);306if (am) {307memset(am, 0, sizeof(*am));308309/* Record any initial, non-zero count value. */310__cntr_id_read(cntr_id, &am->prev_msr);311}312}313314int resctrl_arch_cntr_read(struct rdt_resource *r, struct rdt_mon_domain *d,315u32 unused, u32 rmid, int cntr_id,316enum resctrl_event_id eventid, u64 *val)317{318u64 msr_val;319int ret;320321ret = __cntr_id_read(cntr_id, &msr_val);322if (ret)323return ret;324325*val = get_corrected_val(r, d, rmid, eventid, msr_val);326327return 0;328}329330/*331* The power-on reset value of MSR_RMID_SNC_CONFIG is 0x1332* which indicates that RMIDs are configured in legacy mode.333* This mode is incompatible with Linux resctrl semantics334* as RMIDs are partitioned between SNC nodes, which requires335* a user to know which RMID is allocated to a task.336* Clearing bit 0 reconfigures the RMID counters for use337* in RMID sharing mode. This mode is better for Linux.338* The RMID space is divided between all SNC nodes with the339* RMIDs renumbered to start from zero in each node when340* counting operations from tasks. Code to read the counters341* must adjust RMID counter numbers based on SNC node. See342* logical_rmid_to_physical_rmid() for code that does this.343*/344void arch_mon_domain_online(struct rdt_resource *r, struct rdt_mon_domain *d)345{346if (snc_nodes_per_l3_cache > 1)347msr_clear_bit(MSR_RMID_SNC_CONFIG, 0);348}349350/* CPU models that support MSR_RMID_SNC_CONFIG */351static const struct x86_cpu_id snc_cpu_ids[] __initconst = {352X86_MATCH_VFM(INTEL_ICELAKE_X, 0),353X86_MATCH_VFM(INTEL_SAPPHIRERAPIDS_X, 0),354X86_MATCH_VFM(INTEL_EMERALDRAPIDS_X, 0),355X86_MATCH_VFM(INTEL_GRANITERAPIDS_X, 0),356X86_MATCH_VFM(INTEL_ATOM_CRESTMONT_X, 0),357{}358};359360/*361* There isn't a simple hardware bit that indicates whether a CPU is running362* in Sub-NUMA Cluster (SNC) mode. Infer the state by comparing the363* number of CPUs sharing the L3 cache with CPU0 to the number of CPUs in364* the same NUMA node as CPU0.365* It is not possible to accurately determine SNC state if the system is366* booted with a maxcpus=N parameter. That distorts the ratio of SNC nodes367* to L3 caches. It will be OK if system is booted with hyperthreading368* disabled (since this doesn't affect the ratio).369*/370static __init int snc_get_config(void)371{372struct cacheinfo *ci = get_cpu_cacheinfo_level(0, RESCTRL_L3_CACHE);373const cpumask_t *node0_cpumask;374int cpus_per_node, cpus_per_l3;375int ret;376377if (!x86_match_cpu(snc_cpu_ids) || !ci)378return 1;379380cpus_read_lock();381if (num_online_cpus() != num_present_cpus())382pr_warn("Some CPUs offline, SNC detection may be incorrect\n");383cpus_read_unlock();384385node0_cpumask = cpumask_of_node(cpu_to_node(0));386387cpus_per_node = cpumask_weight(node0_cpumask);388cpus_per_l3 = cpumask_weight(&ci->shared_cpu_map);389390if (!cpus_per_node || !cpus_per_l3)391return 1;392393ret = cpus_per_l3 / cpus_per_node;394395/* sanity check: Only valid results are 1, 2, 3, 4, 6 */396switch (ret) {397case 1:398break;399case 2 ... 4:400case 6:401pr_info("Sub-NUMA Cluster mode detected with %d nodes per L3 cache\n", ret);402rdt_resources_all[RDT_RESOURCE_L3].r_resctrl.mon_scope = RESCTRL_L3_NODE;403break;404default:405pr_warn("Ignore improbable SNC node count %d\n", ret);406ret = 1;407break;408}409410return ret;411}412413int __init rdt_get_mon_l3_config(struct rdt_resource *r)414{415unsigned int mbm_offset = boot_cpu_data.x86_cache_mbm_width_offset;416struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);417unsigned int threshold;418u32 eax, ebx, ecx, edx;419420snc_nodes_per_l3_cache = snc_get_config();421422resctrl_rmid_realloc_limit = boot_cpu_data.x86_cache_size * 1024;423hw_res->mon_scale = boot_cpu_data.x86_cache_occ_scale / snc_nodes_per_l3_cache;424r->mon.num_rmid = (boot_cpu_data.x86_cache_max_rmid + 1) / snc_nodes_per_l3_cache;425hw_res->mbm_width = MBM_CNTR_WIDTH_BASE;426427if (mbm_offset > 0 && mbm_offset <= MBM_CNTR_WIDTH_OFFSET_MAX)428hw_res->mbm_width += mbm_offset;429else if (mbm_offset > MBM_CNTR_WIDTH_OFFSET_MAX)430pr_warn("Ignoring impossible MBM counter offset\n");431432/*433* A reasonable upper limit on the max threshold is the number434* of lines tagged per RMID if all RMIDs have the same number of435* lines tagged in the LLC.436*437* For a 35MB LLC and 56 RMIDs, this is ~1.8% of the LLC.438*/439threshold = resctrl_rmid_realloc_limit / r->mon.num_rmid;440441/*442* Because num_rmid may not be a power of two, round the value443* to the nearest multiple of hw_res->mon_scale so it matches a444* value the hardware will measure. mon_scale may not be a power of 2.445*/446resctrl_rmid_realloc_threshold = resctrl_arch_round_mon_val(threshold);447448if (rdt_cpu_has(X86_FEATURE_BMEC) || rdt_cpu_has(X86_FEATURE_ABMC)) {449/* Detect list of bandwidth sources that can be tracked */450cpuid_count(0x80000020, 3, &eax, &ebx, &ecx, &edx);451r->mon.mbm_cfg_mask = ecx & MAX_EVT_CONFIG_BITS;452}453454if (rdt_cpu_has(X86_FEATURE_ABMC)) {455r->mon.mbm_cntr_assignable = true;456cpuid_count(0x80000020, 5, &eax, &ebx, &ecx, &edx);457r->mon.num_mbm_cntrs = (ebx & GENMASK(15, 0)) + 1;458hw_res->mbm_cntr_assign_enabled = true;459}460461r->mon_capable = true;462463return 0;464}465466void __init intel_rdt_mbm_apply_quirk(void)467{468int cf_index;469470cf_index = (boot_cpu_data.x86_cache_max_rmid + 1) / 8 - 1;471if (cf_index >= ARRAY_SIZE(mbm_cf_table)) {472pr_info("No MBM correction factor available\n");473return;474}475476mbm_cf_rmidthreshold = mbm_cf_table[cf_index].rmidthreshold;477mbm_cf = mbm_cf_table[cf_index].cf;478}479480static void resctrl_abmc_set_one_amd(void *arg)481{482bool *enable = arg;483484if (*enable)485msr_set_bit(MSR_IA32_L3_QOS_EXT_CFG, ABMC_ENABLE_BIT);486else487msr_clear_bit(MSR_IA32_L3_QOS_EXT_CFG, ABMC_ENABLE_BIT);488}489490/*491* ABMC enable/disable requires update of L3_QOS_EXT_CFG MSR on all the CPUs492* associated with all monitor domains.493*/494static void _resctrl_abmc_enable(struct rdt_resource *r, bool enable)495{496struct rdt_mon_domain *d;497498lockdep_assert_cpus_held();499500list_for_each_entry(d, &r->mon_domains, hdr.list) {501on_each_cpu_mask(&d->hdr.cpu_mask, resctrl_abmc_set_one_amd,502&enable, 1);503resctrl_arch_reset_rmid_all(r, d);504}505}506507int resctrl_arch_mbm_cntr_assign_set(struct rdt_resource *r, bool enable)508{509struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);510511if (r->mon.mbm_cntr_assignable &&512hw_res->mbm_cntr_assign_enabled != enable) {513_resctrl_abmc_enable(r, enable);514hw_res->mbm_cntr_assign_enabled = enable;515}516517return 0;518}519520bool resctrl_arch_mbm_cntr_assign_enabled(struct rdt_resource *r)521{522return resctrl_to_arch_res(r)->mbm_cntr_assign_enabled;523}524525static void resctrl_abmc_config_one_amd(void *info)526{527union l3_qos_abmc_cfg *abmc_cfg = info;528529wrmsrl(MSR_IA32_L3_QOS_ABMC_CFG, abmc_cfg->full);530}531532/*533* Send an IPI to the domain to assign the counter to RMID, event pair.534*/535void resctrl_arch_config_cntr(struct rdt_resource *r, struct rdt_mon_domain *d,536enum resctrl_event_id evtid, u32 rmid, u32 closid,537u32 cntr_id, bool assign)538{539struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);540union l3_qos_abmc_cfg abmc_cfg = { 0 };541struct arch_mbm_state *am;542543abmc_cfg.split.cfg_en = 1;544abmc_cfg.split.cntr_en = assign ? 1 : 0;545abmc_cfg.split.cntr_id = cntr_id;546abmc_cfg.split.bw_src = rmid;547if (assign)548abmc_cfg.split.bw_type = resctrl_get_mon_evt_cfg(evtid);549550smp_call_function_any(&d->hdr.cpu_mask, resctrl_abmc_config_one_amd, &abmc_cfg, 1);551552/*553* The hardware counter is reset (because cfg_en == 1) so there is no554* need to record initial non-zero counts.555*/556am = get_arch_mbm_state(hw_dom, rmid, evtid);557if (am)558memset(am, 0, sizeof(*am));559}560561void resctrl_arch_mbm_cntr_assign_set_one(struct rdt_resource *r)562{563struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);564565resctrl_abmc_set_one_amd(&hw_res->mbm_cntr_assign_enabled);566}567568569