Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Resource Director Technology(RDT)
   4 * - Cache Allocation code.
   5 *
   6 * Copyright (C) 2016 Intel Corporation
   7 *
   8 * Authors:
   9 *    Fenghua Yu <fenghua.yu@intel.com>
  10 *    Tony Luck <tony.luck@intel.com>
  11 *    Vikas Shivappa <vikas.shivappa@intel.com>
  12 *
  13 * More information about RDT be found in the Intel (R) x86 Architecture
  14 * Software Developer Manual June 2016, volume 3, section 17.17.
  15 */
  16
  17#define pr_fmt(fmt)	"resctrl: " fmt
  18
  19#include <linux/cpu.h>
  20#include <linux/slab.h>
  21#include <linux/err.h>
 
  22#include <linux/cpuhotplug.h>
  23
  24#include <asm/cpu_device_id.h>
  25#include <asm/resctrl.h>
  26#include "internal.h"
  27
  28/*
  29 * rdt_domain structures are kfree()d when their last CPU goes offline,
  30 * and allocated when the first CPU in a new domain comes online.
  31 * The rdt_resource's domain list is updated when this happens. Readers of
  32 * the domain list must either take cpus_read_lock(), or rely on an RCU
  33 * read-side critical section, to avoid observing concurrent modification.
  34 * All writers take this mutex:
  35 */
  36static DEFINE_MUTEX(domain_list_lock);
  37
  38/*
  39 * The cached resctrl_pqr_state is strictly per CPU and can never be
  40 * updated from a remote CPU. Functions which modify the state
  41 * are called with interrupts disabled and no preemption, which
  42 * is sufficient for the protection.
  43 */
  44DEFINE_PER_CPU(struct resctrl_pqr_state, pqr_state);
  45
  46/*
  47 * Used to store the max resource name width and max resource data width
  48 * to display the schemata in a tabular format
  49 */
  50int max_name_width, max_data_width;
  51
  52/*
  53 * Global boolean for rdt_alloc which is true if any
  54 * resource allocation is enabled.
  55 */
  56bool rdt_alloc_capable;
  57
  58static void mba_wrmsr_intel(struct msr_param *m);
  59static void cat_wrmsr(struct msr_param *m);
  60static void mba_wrmsr_amd(struct msr_param *m);
 
 
 
 
 
  61
  62#define ctrl_domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].r_resctrl.ctrl_domains)
  63#define mon_domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].r_resctrl.mon_domains)
  64
  65struct rdt_hw_resource rdt_resources_all[] = {
  66	[RDT_RESOURCE_L3] =
  67	{
  68		.r_resctrl = {
  69			.rid			= RDT_RESOURCE_L3,
  70			.name			= "L3",
  71			.ctrl_scope		= RESCTRL_L3_CACHE,
  72			.mon_scope		= RESCTRL_L3_CACHE,
  73			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_L3),
  74			.mon_domains		= mon_domain_init(RDT_RESOURCE_L3),
  75			.parse_ctrlval		= parse_cbm,
  76			.format_str		= "%d=%0*x",
  77			.fflags			= RFTYPE_RES_CACHE,
  78		},
  79		.msr_base		= MSR_IA32_L3_CBM_BASE,
  80		.msr_update		= cat_wrmsr,
  81	},
  82	[RDT_RESOURCE_L2] =
  83	{
  84		.r_resctrl = {
  85			.rid			= RDT_RESOURCE_L2,
  86			.name			= "L2",
  87			.ctrl_scope		= RESCTRL_L2_CACHE,
  88			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_L2),
  89			.parse_ctrlval		= parse_cbm,
  90			.format_str		= "%d=%0*x",
  91			.fflags			= RFTYPE_RES_CACHE,
  92		},
  93		.msr_base		= MSR_IA32_L2_CBM_BASE,
  94		.msr_update		= cat_wrmsr,
  95	},
  96	[RDT_RESOURCE_MBA] =
  97	{
  98		.r_resctrl = {
  99			.rid			= RDT_RESOURCE_MBA,
 100			.name			= "MB",
 101			.ctrl_scope		= RESCTRL_L3_CACHE,
 102			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_MBA),
 103			.parse_ctrlval		= parse_bw,
 104			.format_str		= "%d=%*u",
 105			.fflags			= RFTYPE_RES_MB,
 106		},
 107	},
 108	[RDT_RESOURCE_SMBA] =
 109	{
 110		.r_resctrl = {
 111			.rid			= RDT_RESOURCE_SMBA,
 112			.name			= "SMBA",
 113			.ctrl_scope		= RESCTRL_L3_CACHE,
 114			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_SMBA),
 115			.parse_ctrlval		= parse_bw,
 116			.format_str		= "%d=%*u",
 117			.fflags			= RFTYPE_RES_MB,
 118		},
 119	},
 120};
 121
 122u32 resctrl_arch_system_num_rmid_idx(void)
 123{
 124	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
 125
 126	/* RMID are independent numbers for x86. num_rmid_idx == num_rmid */
 127	return r->num_rmid;
 128}
 129
 130/*
 131 * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
 132 * as they do not have CPUID enumeration support for Cache allocation.
 133 * The check for Vendor/Family/Model is not enough to guarantee that
 134 * the MSRs won't #GP fault because only the following SKUs support
 135 * CAT:
 136 *	Intel(R) Xeon(R)  CPU E5-2658  v3  @  2.20GHz
 137 *	Intel(R) Xeon(R)  CPU E5-2648L v3  @  1.80GHz
 138 *	Intel(R) Xeon(R)  CPU E5-2628L v3  @  2.00GHz
 139 *	Intel(R) Xeon(R)  CPU E5-2618L v3  @  2.30GHz
 140 *	Intel(R) Xeon(R)  CPU E5-2608L v3  @  2.00GHz
 141 *	Intel(R) Xeon(R)  CPU E5-2658A v3  @  2.20GHz
 142 *
 143 * Probe by trying to write the first of the L3 cache mask registers
 144 * and checking that the bits stick. Max CLOSids is always 4 and max cbm length
 145 * is always 20 on hsw server parts. The minimum cache bitmask length
 146 * allowed for HSW server is always 2 bits. Hardcode all of them.
 147 */
 148static inline void cache_alloc_hsw_probe(void)
 149{
 150	struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_L3];
 151	struct rdt_resource *r  = &hw_res->r_resctrl;
 152	u64 max_cbm = BIT_ULL_MASK(20) - 1, l3_cbm_0;
 153
 154	if (wrmsrl_safe(MSR_IA32_L3_CBM_BASE, max_cbm))
 155		return;
 156
 157	rdmsrl(MSR_IA32_L3_CBM_BASE, l3_cbm_0);
 158
 159	/* If all the bits were set in MSR, return success */
 160	if (l3_cbm_0 != max_cbm)
 161		return;
 162
 163	hw_res->num_closid = 4;
 164	r->default_ctrl = max_cbm;
 165	r->cache.cbm_len = 20;
 166	r->cache.shareable_bits = 0xc0000;
 167	r->cache.min_cbm_bits = 2;
 168	r->cache.arch_has_sparse_bitmasks = false;
 169	r->alloc_capable = true;
 170
 171	rdt_alloc_capable = true;
 172}
 173
 174bool is_mba_sc(struct rdt_resource *r)
 175{
 176	if (!r)
 177		return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.mba_sc;
 178
 179	/*
 180	 * The software controller support is only applicable to MBA resource.
 181	 * Make sure to check for resource type.
 182	 */
 183	if (r->rid != RDT_RESOURCE_MBA)
 184		return false;
 185
 186	return r->membw.mba_sc;
 187}
 188
 189/*
 190 * rdt_get_mb_table() - get a mapping of bandwidth(b/w) percentage values
 191 * exposed to user interface and the h/w understandable delay values.
 192 *
 193 * The non-linear delay values have the granularity of power of two
 194 * and also the h/w does not guarantee a curve for configured delay
 195 * values vs. actual b/w enforced.
 196 * Hence we need a mapping that is pre calibrated so the user can
 197 * express the memory b/w as a percentage value.
 198 */
 199static inline bool rdt_get_mb_table(struct rdt_resource *r)
 200{
 201	/*
 202	 * There are no Intel SKUs as of now to support non-linear delay.
 203	 */
 204	pr_info("MBA b/w map not implemented for cpu:%d, model:%d",
 205		boot_cpu_data.x86, boot_cpu_data.x86_model);
 206
 207	return false;
 208}
 209
 210static __init bool __get_mem_config_intel(struct rdt_resource *r)
 211{
 212	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
 213	union cpuid_0x10_3_eax eax;
 214	union cpuid_0x10_x_edx edx;
 215	u32 ebx, ecx, max_delay;
 216
 217	cpuid_count(0x00000010, 3, &eax.full, &ebx, &ecx, &edx.full);
 218	hw_res->num_closid = edx.split.cos_max + 1;
 219	max_delay = eax.split.max_delay + 1;
 220	r->default_ctrl = MAX_MBA_BW;
 221	r->membw.arch_needs_linear = true;
 222	if (ecx & MBA_IS_LINEAR) {
 223		r->membw.delay_linear = true;
 224		r->membw.min_bw = MAX_MBA_BW - max_delay;
 225		r->membw.bw_gran = MAX_MBA_BW - max_delay;
 226	} else {
 227		if (!rdt_get_mb_table(r))
 228			return false;
 229		r->membw.arch_needs_linear = false;
 230	}
 231	r->data_width = 3;
 232
 233	if (boot_cpu_has(X86_FEATURE_PER_THREAD_MBA))
 234		r->membw.throttle_mode = THREAD_THROTTLE_PER_THREAD;
 235	else
 236		r->membw.throttle_mode = THREAD_THROTTLE_MAX;
 237	thread_throttle_mode_init();
 238
 239	r->alloc_capable = true;
 240
 241	return true;
 242}
 243
 244static __init bool __rdt_get_mem_config_amd(struct rdt_resource *r)
 245{
 246	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
 247	u32 eax, ebx, ecx, edx, subleaf;
 248
 249	/*
 250	 * Query CPUID_Fn80000020_EDX_x01 for MBA and
 251	 * CPUID_Fn80000020_EDX_x02 for SMBA
 252	 */
 253	subleaf = (r->rid == RDT_RESOURCE_SMBA) ? 2 :  1;
 254
 255	cpuid_count(0x80000020, subleaf, &eax, &ebx, &ecx, &edx);
 256	hw_res->num_closid = edx + 1;
 257	r->default_ctrl = 1 << eax;
 258
 259	/* AMD does not use delay */
 260	r->membw.delay_linear = false;
 261	r->membw.arch_needs_linear = false;
 262
 263	/*
 264	 * AMD does not use memory delay throttle model to control
 265	 * the allocation like Intel does.
 266	 */
 267	r->membw.throttle_mode = THREAD_THROTTLE_UNDEFINED;
 268	r->membw.min_bw = 0;
 269	r->membw.bw_gran = 1;
 270	/* Max value is 2048, Data width should be 4 in decimal */
 271	r->data_width = 4;
 272
 273	r->alloc_capable = true;
 274
 275	return true;
 276}
 277
 278static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r)
 279{
 280	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
 281	union cpuid_0x10_1_eax eax;
 282	union cpuid_0x10_x_ecx ecx;
 283	union cpuid_0x10_x_edx edx;
 284	u32 ebx;
 285
 286	cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx.full, &edx.full);
 287	hw_res->num_closid = edx.split.cos_max + 1;
 288	r->cache.cbm_len = eax.split.cbm_len + 1;
 289	r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
 290	r->cache.shareable_bits = ebx & r->default_ctrl;
 291	r->data_width = (r->cache.cbm_len + 3) / 4;
 292	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
 293		r->cache.arch_has_sparse_bitmasks = ecx.split.noncont;
 294	r->alloc_capable = true;
 295}
 296
 297static void rdt_get_cdp_config(int level)
 298{
 299	/*
 300	 * By default, CDP is disabled. CDP can be enabled by mount parameter
 301	 * "cdp" during resctrl file system mount time.
 302	 */
 303	rdt_resources_all[level].cdp_enabled = false;
 304	rdt_resources_all[level].r_resctrl.cdp_capable = true;
 305}
 306
 307static void rdt_get_cdp_l3_config(void)
 308{
 309	rdt_get_cdp_config(RDT_RESOURCE_L3);
 310}
 311
 312static void rdt_get_cdp_l2_config(void)
 313{
 314	rdt_get_cdp_config(RDT_RESOURCE_L2);
 315}
 316
 317static void mba_wrmsr_amd(struct msr_param *m)
 
 318{
 319	struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(m->dom);
 320	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(m->res);
 321	unsigned int i;
 
 
 322
 323	for (i = m->low; i < m->high; i++)
 324		wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
 325}
 326
 327/*
 328 * Map the memory b/w percentage value to delay values
 329 * that can be written to QOS_MSRs.
 330 * There are currently no SKUs which support non linear delay values.
 331 */
 332static u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)
 333{
 334	if (r->membw.delay_linear)
 335		return MAX_MBA_BW - bw;
 336
 337	pr_warn_once("Non Linear delay-bw map not supported but queried\n");
 338	return r->default_ctrl;
 339}
 340
 341static void mba_wrmsr_intel(struct msr_param *m)
 
 
 342{
 343	struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(m->dom);
 344	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(m->res);
 345	unsigned int i;
 
 
 346
 347	/*  Write the delay values for mba. */
 348	for (i = m->low; i < m->high; i++)
 349		wrmsrl(hw_res->msr_base + i, delay_bw_map(hw_dom->ctrl_val[i], m->res));
 350}
 351
 352static void cat_wrmsr(struct msr_param *m)
 
 353{
 354	struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(m->dom);
 355	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(m->res);
 356	unsigned int i;
 
 
 357
 358	for (i = m->low; i < m->high; i++)
 359		wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
 360}
 361
 362struct rdt_ctrl_domain *get_ctrl_domain_from_cpu(int cpu, struct rdt_resource *r)
 363{
 364	struct rdt_ctrl_domain *d;
 365
 366	lockdep_assert_cpus_held();
 367
 368	list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
 369		/* Find the domain that contains this CPU */
 370		if (cpumask_test_cpu(cpu, &d->hdr.cpu_mask))
 371			return d;
 372	}
 373
 374	return NULL;
 375}
 376
 377struct rdt_mon_domain *get_mon_domain_from_cpu(int cpu, struct rdt_resource *r)
 378{
 379	struct rdt_mon_domain *d;
 380
 381	lockdep_assert_cpus_held();
 382
 383	list_for_each_entry(d, &r->mon_domains, hdr.list) {
 384		/* Find the domain that contains this CPU */
 385		if (cpumask_test_cpu(cpu, &d->hdr.cpu_mask))
 386			return d;
 387	}
 388
 389	return NULL;
 390}
 391
 392u32 resctrl_arch_get_num_closid(struct rdt_resource *r)
 393{
 394	return resctrl_to_arch_res(r)->num_closid;
 395}
 396
 397void rdt_ctrl_update(void *arg)
 398{
 399	struct rdt_hw_resource *hw_res;
 400	struct msr_param *m = arg;
 
 
 
 
 401
 402	hw_res = resctrl_to_arch_res(m->res);
 403	hw_res->msr_update(m);
 
 
 
 
 
 404}
 405
 406/*
 407 * rdt_find_domain - Search for a domain id in a resource domain list.
 408 *
 409 * Search the domain list to find the domain id. If the domain id is
 410 * found, return the domain. NULL otherwise.  If the domain id is not
 411 * found (and NULL returned) then the first domain with id bigger than
 412 * the input id can be returned to the caller via @pos.
 413 */
 414struct rdt_domain_hdr *rdt_find_domain(struct list_head *h, int id,
 415				       struct list_head **pos)
 416{
 417	struct rdt_domain_hdr *d;
 418	struct list_head *l;
 419
 420	list_for_each(l, h) {
 421		d = list_entry(l, struct rdt_domain_hdr, list);
 
 
 
 422		/* When id is found, return its domain. */
 423		if (id == d->id)
 424			return d;
 425		/* Stop searching when finding id's position in sorted list. */
 426		if (id < d->id)
 427			break;
 428	}
 429
 430	if (pos)
 431		*pos = l;
 432
 433	return NULL;
 434}
 435
 436static void setup_default_ctrlval(struct rdt_resource *r, u32 *dc)
 437{
 438	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
 439	int i;
 440
 441	/*
 442	 * Initialize the Control MSRs to having no control.
 443	 * For Cache Allocation: Set all bits in cbm
 444	 * For Memory Allocation: Set b/w requested to 100%
 445	 */
 446	for (i = 0; i < hw_res->num_closid; i++, dc++)
 447		*dc = r->default_ctrl;
 448}
 449
 450static void ctrl_domain_free(struct rdt_hw_ctrl_domain *hw_dom)
 451{
 452	kfree(hw_dom->ctrl_val);
 453	kfree(hw_dom);
 454}
 455
 456static void mon_domain_free(struct rdt_hw_mon_domain *hw_dom)
 457{
 458	kfree(hw_dom->arch_mbm_total);
 459	kfree(hw_dom->arch_mbm_local);
 
 460	kfree(hw_dom);
 461}
 462
 463static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_ctrl_domain *d)
 464{
 465	struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(d);
 466	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
 
 467	struct msr_param m;
 468	u32 *dc;
 469
 470	dc = kmalloc_array(hw_res->num_closid, sizeof(*hw_dom->ctrl_val),
 471			   GFP_KERNEL);
 472	if (!dc)
 473		return -ENOMEM;
 474
 475	hw_dom->ctrl_val = dc;
 476	setup_default_ctrlval(r, dc);
 477
 478	m.res = r;
 479	m.dom = d;
 480	m.low = 0;
 481	m.high = hw_res->num_closid;
 482	hw_res->msr_update(&m);
 483	return 0;
 484}
 485
 486/**
 487 * arch_domain_mbm_alloc() - Allocate arch private storage for the MBM counters
 488 * @num_rmid:	The size of the MBM counter array
 489 * @hw_dom:	The domain that owns the allocated arrays
 490 */
 491static int arch_domain_mbm_alloc(u32 num_rmid, struct rdt_hw_mon_domain *hw_dom)
 492{
 493	size_t tsize;
 494
 495	if (is_mbm_total_enabled()) {
 496		tsize = sizeof(*hw_dom->arch_mbm_total);
 497		hw_dom->arch_mbm_total = kcalloc(num_rmid, tsize, GFP_KERNEL);
 498		if (!hw_dom->arch_mbm_total)
 499			return -ENOMEM;
 500	}
 501	if (is_mbm_local_enabled()) {
 502		tsize = sizeof(*hw_dom->arch_mbm_local);
 503		hw_dom->arch_mbm_local = kcalloc(num_rmid, tsize, GFP_KERNEL);
 504		if (!hw_dom->arch_mbm_local) {
 505			kfree(hw_dom->arch_mbm_total);
 506			hw_dom->arch_mbm_total = NULL;
 507			return -ENOMEM;
 508		}
 509	}
 510
 511	return 0;
 512}
 513
 514static int get_domain_id_from_scope(int cpu, enum resctrl_scope scope)
 515{
 516	switch (scope) {
 517	case RESCTRL_L2_CACHE:
 518	case RESCTRL_L3_CACHE:
 519		return get_cpu_cacheinfo_id(cpu, scope);
 520	case RESCTRL_L3_NODE:
 521		return cpu_to_node(cpu);
 522	default:
 523		break;
 524	}
 525
 526	return -EINVAL;
 527}
 528
 529static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)
 530{
 531	int id = get_domain_id_from_scope(cpu, r->ctrl_scope);
 532	struct rdt_hw_ctrl_domain *hw_dom;
 533	struct list_head *add_pos = NULL;
 534	struct rdt_domain_hdr *hdr;
 535	struct rdt_ctrl_domain *d;
 536	int err;
 537
 538	lockdep_assert_held(&domain_list_lock);
 539
 540	if (id < 0) {
 541		pr_warn_once("Can't find control domain id for CPU:%d scope:%d for resource %s\n",
 542			     cpu, r->ctrl_scope, r->name);
 543		return;
 544	}
 545
 546	hdr = rdt_find_domain(&r->ctrl_domains, id, &add_pos);
 547	if (hdr) {
 548		if (WARN_ON_ONCE(hdr->type != RESCTRL_CTRL_DOMAIN))
 549			return;
 550		d = container_of(hdr, struct rdt_ctrl_domain, hdr);
 551
 552		cpumask_set_cpu(cpu, &d->hdr.cpu_mask);
 553		if (r->cache.arch_has_per_cpu_cfg)
 554			rdt_domain_reconfigure_cdp(r);
 555		return;
 556	}
 557
 558	hw_dom = kzalloc_node(sizeof(*hw_dom), GFP_KERNEL, cpu_to_node(cpu));
 559	if (!hw_dom)
 560		return;
 561
 562	d = &hw_dom->d_resctrl;
 563	d->hdr.id = id;
 564	d->hdr.type = RESCTRL_CTRL_DOMAIN;
 565	cpumask_set_cpu(cpu, &d->hdr.cpu_mask);
 566
 567	rdt_domain_reconfigure_cdp(r);
 568
 569	if (domain_setup_ctrlval(r, d)) {
 570		ctrl_domain_free(hw_dom);
 571		return;
 572	}
 573
 574	list_add_tail_rcu(&d->hdr.list, add_pos);
 575
 576	err = resctrl_online_ctrl_domain(r, d);
 577	if (err) {
 578		list_del_rcu(&d->hdr.list);
 579		synchronize_rcu();
 580		ctrl_domain_free(hw_dom);
 581	}
 582}
 583
 584static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)
 585{
 586	int id = get_domain_id_from_scope(cpu, r->mon_scope);
 587	struct list_head *add_pos = NULL;
 588	struct rdt_hw_mon_domain *hw_dom;
 589	struct rdt_domain_hdr *hdr;
 590	struct rdt_mon_domain *d;
 591	int err;
 592
 593	lockdep_assert_held(&domain_list_lock);
 594
 595	if (id < 0) {
 596		pr_warn_once("Can't find monitor domain id for CPU:%d scope:%d for resource %s\n",
 597			     cpu, r->mon_scope, r->name);
 598		return;
 599	}
 600
 601	hdr = rdt_find_domain(&r->mon_domains, id, &add_pos);
 602	if (hdr) {
 603		if (WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN))
 604			return;
 605		d = container_of(hdr, struct rdt_mon_domain, hdr);
 606
 607		cpumask_set_cpu(cpu, &d->hdr.cpu_mask);
 608		return;
 609	}
 610
 611	hw_dom = kzalloc_node(sizeof(*hw_dom), GFP_KERNEL, cpu_to_node(cpu));
 612	if (!hw_dom)
 613		return;
 614
 615	d = &hw_dom->d_resctrl;
 616	d->hdr.id = id;
 617	d->hdr.type = RESCTRL_MON_DOMAIN;
 618	d->ci = get_cpu_cacheinfo_level(cpu, RESCTRL_L3_CACHE);
 619	if (!d->ci) {
 620		pr_warn_once("Can't find L3 cache for CPU:%d resource %s\n", cpu, r->name);
 621		mon_domain_free(hw_dom);
 622		return;
 623	}
 624	cpumask_set_cpu(cpu, &d->hdr.cpu_mask);
 625
 626	arch_mon_domain_online(r, d);
 627
 628	if (arch_domain_mbm_alloc(r->num_rmid, hw_dom)) {
 629		mon_domain_free(hw_dom);
 630		return;
 631	}
 632
 633	list_add_tail_rcu(&d->hdr.list, add_pos);
 634
 635	err = resctrl_online_mon_domain(r, d);
 636	if (err) {
 637		list_del_rcu(&d->hdr.list);
 638		synchronize_rcu();
 639		mon_domain_free(hw_dom);
 640	}
 641}
 642
 643static void domain_add_cpu(int cpu, struct rdt_resource *r)
 644{
 645	if (r->alloc_capable)
 646		domain_add_cpu_ctrl(cpu, r);
 647	if (r->mon_capable)
 648		domain_add_cpu_mon(cpu, r);
 649}
 650
 651static void domain_remove_cpu_ctrl(int cpu, struct rdt_resource *r)
 652{
 653	int id = get_domain_id_from_scope(cpu, r->ctrl_scope);
 654	struct rdt_hw_ctrl_domain *hw_dom;
 655	struct rdt_domain_hdr *hdr;
 656	struct rdt_ctrl_domain *d;
 657
 658	lockdep_assert_held(&domain_list_lock);
 659
 660	if (id < 0) {
 661		pr_warn_once("Can't find control domain id for CPU:%d scope:%d for resource %s\n",
 662			     cpu, r->ctrl_scope, r->name);
 663		return;
 664	}
 665
 666	hdr = rdt_find_domain(&r->ctrl_domains, id, NULL);
 667	if (!hdr) {
 668		pr_warn("Can't find control domain for id=%d for CPU %d for resource %s\n",
 669			id, cpu, r->name);
 670		return;
 671	}
 
 672
 673	if (WARN_ON_ONCE(hdr->type != RESCTRL_CTRL_DOMAIN))
 674		return;
 675
 676	d = container_of(hdr, struct rdt_ctrl_domain, hdr);
 677	hw_dom = resctrl_to_arch_ctrl_dom(d);
 678
 679	cpumask_clear_cpu(cpu, &d->hdr.cpu_mask);
 680	if (cpumask_empty(&d->hdr.cpu_mask)) {
 681		resctrl_offline_ctrl_domain(r, d);
 682		list_del_rcu(&d->hdr.list);
 683		synchronize_rcu();
 684
 685		/*
 686		 * rdt_ctrl_domain "d" is going to be freed below, so clear
 687		 * its pointer from pseudo_lock_region struct.
 688		 */
 689		if (d->plr)
 690			d->plr->d = NULL;
 691		ctrl_domain_free(hw_dom);
 692
 693		return;
 694	}
 695}
 696
 697static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
 698{
 699	int id = get_domain_id_from_scope(cpu, r->mon_scope);
 700	struct rdt_hw_mon_domain *hw_dom;
 701	struct rdt_domain_hdr *hdr;
 702	struct rdt_mon_domain *d;
 703
 704	lockdep_assert_held(&domain_list_lock);
 705
 706	if (id < 0) {
 707		pr_warn_once("Can't find monitor domain id for CPU:%d scope:%d for resource %s\n",
 708			     cpu, r->mon_scope, r->name);
 709		return;
 710	}
 711
 712	hdr = rdt_find_domain(&r->mon_domains, id, NULL);
 713	if (!hdr) {
 714		pr_warn("Can't find monitor domain for id=%d for CPU %d for resource %s\n",
 715			id, cpu, r->name);
 716		return;
 717	}
 718
 719	if (WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN))
 720		return;
 721
 722	d = container_of(hdr, struct rdt_mon_domain, hdr);
 723	hw_dom = resctrl_to_arch_mon_dom(d);
 724
 725	cpumask_clear_cpu(cpu, &d->hdr.cpu_mask);
 726	if (cpumask_empty(&d->hdr.cpu_mask)) {
 727		resctrl_offline_mon_domain(r, d);
 728		list_del_rcu(&d->hdr.list);
 729		synchronize_rcu();
 730		mon_domain_free(hw_dom);
 731
 732		return;
 733	}
 734}
 735
 736static void domain_remove_cpu(int cpu, struct rdt_resource *r)
 737{
 738	if (r->alloc_capable)
 739		domain_remove_cpu_ctrl(cpu, r);
 740	if (r->mon_capable)
 741		domain_remove_cpu_mon(cpu, r);
 742}
 743
 744static void clear_closid_rmid(int cpu)
 745{
 746	struct resctrl_pqr_state *state = this_cpu_ptr(&pqr_state);
 747
 748	state->default_closid = RESCTRL_RESERVED_CLOSID;
 749	state->default_rmid = RESCTRL_RESERVED_RMID;
 750	state->cur_closid = RESCTRL_RESERVED_CLOSID;
 751	state->cur_rmid = RESCTRL_RESERVED_RMID;
 752	wrmsr(MSR_IA32_PQR_ASSOC, RESCTRL_RESERVED_RMID,
 753	      RESCTRL_RESERVED_CLOSID);
 754}
 755
 756static int resctrl_arch_online_cpu(unsigned int cpu)
 757{
 758	struct rdt_resource *r;
 759
 760	mutex_lock(&domain_list_lock);
 761	for_each_capable_rdt_resource(r)
 762		domain_add_cpu(cpu, r);
 763	mutex_unlock(&domain_list_lock);
 764
 765	clear_closid_rmid(cpu);
 766	resctrl_online_cpu(cpu);
 767
 768	return 0;
 769}
 770
 771static int resctrl_arch_offline_cpu(unsigned int cpu)
 772{
 773	struct rdt_resource *r;
 774
 775	resctrl_offline_cpu(cpu);
 
 
 
 
 
 776
 777	mutex_lock(&domain_list_lock);
 
 
 
 
 
 778	for_each_capable_rdt_resource(r)
 779		domain_remove_cpu(cpu, r);
 780	mutex_unlock(&domain_list_lock);
 781
 
 
 
 
 782	clear_closid_rmid(cpu);
 
 783
 784	return 0;
 785}
 786
 787/*
 788 * Choose a width for the resource name and resource data based on the
 789 * resource that has widest name and cbm.
 790 */
 791static __init void rdt_init_padding(void)
 792{
 793	struct rdt_resource *r;
 794
 795	for_each_alloc_capable_rdt_resource(r) {
 796		if (r->data_width > max_data_width)
 797			max_data_width = r->data_width;
 798	}
 799}
 800
 801enum {
 802	RDT_FLAG_CMT,
 803	RDT_FLAG_MBM_TOTAL,
 804	RDT_FLAG_MBM_LOCAL,
 805	RDT_FLAG_L3_CAT,
 806	RDT_FLAG_L3_CDP,
 807	RDT_FLAG_L2_CAT,
 808	RDT_FLAG_L2_CDP,
 809	RDT_FLAG_MBA,
 810	RDT_FLAG_SMBA,
 811	RDT_FLAG_BMEC,
 812};
 813
 814#define RDT_OPT(idx, n, f)	\
 815[idx] = {			\
 816	.name = n,		\
 817	.flag = f		\
 818}
 819
 820struct rdt_options {
 821	char	*name;
 822	int	flag;
 823	bool	force_off, force_on;
 824};
 825
 826static struct rdt_options rdt_options[]  __initdata = {
 827	RDT_OPT(RDT_FLAG_CMT,	    "cmt",	X86_FEATURE_CQM_OCCUP_LLC),
 828	RDT_OPT(RDT_FLAG_MBM_TOTAL, "mbmtotal", X86_FEATURE_CQM_MBM_TOTAL),
 829	RDT_OPT(RDT_FLAG_MBM_LOCAL, "mbmlocal", X86_FEATURE_CQM_MBM_LOCAL),
 830	RDT_OPT(RDT_FLAG_L3_CAT,    "l3cat",	X86_FEATURE_CAT_L3),
 831	RDT_OPT(RDT_FLAG_L3_CDP,    "l3cdp",	X86_FEATURE_CDP_L3),
 832	RDT_OPT(RDT_FLAG_L2_CAT,    "l2cat",	X86_FEATURE_CAT_L2),
 833	RDT_OPT(RDT_FLAG_L2_CDP,    "l2cdp",	X86_FEATURE_CDP_L2),
 834	RDT_OPT(RDT_FLAG_MBA,	    "mba",	X86_FEATURE_MBA),
 835	RDT_OPT(RDT_FLAG_SMBA,	    "smba",	X86_FEATURE_SMBA),
 836	RDT_OPT(RDT_FLAG_BMEC,	    "bmec",	X86_FEATURE_BMEC),
 837};
 838#define NUM_RDT_OPTIONS ARRAY_SIZE(rdt_options)
 839
 840static int __init set_rdt_options(char *str)
 841{
 842	struct rdt_options *o;
 843	bool force_off;
 844	char *tok;
 845
 846	if (*str == '=')
 847		str++;
 848	while ((tok = strsep(&str, ",")) != NULL) {
 849		force_off = *tok == '!';
 850		if (force_off)
 851			tok++;
 852		for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
 853			if (strcmp(tok, o->name) == 0) {
 854				if (force_off)
 855					o->force_off = true;
 856				else
 857					o->force_on = true;
 858				break;
 859			}
 860		}
 861	}
 862	return 1;
 863}
 864__setup("rdt", set_rdt_options);
 865
 866bool __init rdt_cpu_has(int flag)
 867{
 868	bool ret = boot_cpu_has(flag);
 869	struct rdt_options *o;
 870
 871	if (!ret)
 872		return ret;
 873
 874	for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
 875		if (flag == o->flag) {
 876			if (o->force_off)
 877				ret = false;
 878			if (o->force_on)
 879				ret = true;
 880			break;
 881		}
 882	}
 883	return ret;
 884}
 885
 886static __init bool get_mem_config(void)
 887{
 888	struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_MBA];
 889
 890	if (!rdt_cpu_has(X86_FEATURE_MBA))
 891		return false;
 892
 893	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
 894		return __get_mem_config_intel(&hw_res->r_resctrl);
 895	else if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
 896		return __rdt_get_mem_config_amd(&hw_res->r_resctrl);
 897
 898	return false;
 899}
 900
 901static __init bool get_slow_mem_config(void)
 902{
 903	struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_SMBA];
 904
 905	if (!rdt_cpu_has(X86_FEATURE_SMBA))
 906		return false;
 907
 908	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
 909		return __rdt_get_mem_config_amd(&hw_res->r_resctrl);
 910
 911	return false;
 912}
 913
 914static __init bool get_rdt_alloc_resources(void)
 915{
 916	struct rdt_resource *r;
 917	bool ret = false;
 918
 919	if (rdt_alloc_capable)
 920		return true;
 921
 922	if (!boot_cpu_has(X86_FEATURE_RDT_A))
 923		return false;
 924
 925	if (rdt_cpu_has(X86_FEATURE_CAT_L3)) {
 926		r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
 927		rdt_get_cache_alloc_cfg(1, r);
 928		if (rdt_cpu_has(X86_FEATURE_CDP_L3))
 929			rdt_get_cdp_l3_config();
 930		ret = true;
 931	}
 932	if (rdt_cpu_has(X86_FEATURE_CAT_L2)) {
 933		/* CPUID 0x10.2 fields are same format at 0x10.1 */
 934		r = &rdt_resources_all[RDT_RESOURCE_L2].r_resctrl;
 935		rdt_get_cache_alloc_cfg(2, r);
 936		if (rdt_cpu_has(X86_FEATURE_CDP_L2))
 937			rdt_get_cdp_l2_config();
 938		ret = true;
 939	}
 940
 941	if (get_mem_config())
 942		ret = true;
 943
 944	if (get_slow_mem_config())
 945		ret = true;
 946
 947	return ret;
 948}
 949
 950static __init bool get_rdt_mon_resources(void)
 951{
 952	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
 953
 954	if (rdt_cpu_has(X86_FEATURE_CQM_OCCUP_LLC))
 955		rdt_mon_features |= (1 << QOS_L3_OCCUP_EVENT_ID);
 956	if (rdt_cpu_has(X86_FEATURE_CQM_MBM_TOTAL))
 957		rdt_mon_features |= (1 << QOS_L3_MBM_TOTAL_EVENT_ID);
 958	if (rdt_cpu_has(X86_FEATURE_CQM_MBM_LOCAL))
 959		rdt_mon_features |= (1 << QOS_L3_MBM_LOCAL_EVENT_ID);
 960
 961	if (!rdt_mon_features)
 962		return false;
 963
 964	return !rdt_get_mon_l3_config(r);
 965}
 966
 967static __init void __check_quirks_intel(void)
 968{
 969	switch (boot_cpu_data.x86_vfm) {
 970	case INTEL_HASWELL_X:
 971		if (!rdt_options[RDT_FLAG_L3_CAT].force_off)
 972			cache_alloc_hsw_probe();
 973		break;
 974	case INTEL_SKYLAKE_X:
 975		if (boot_cpu_data.x86_stepping <= 4)
 976			set_rdt_options("!cmt,!mbmtotal,!mbmlocal,!l3cat");
 977		else
 978			set_rdt_options("!l3cat");
 979		fallthrough;
 980	case INTEL_BROADWELL_X:
 981		intel_rdt_mbm_apply_quirk();
 982		break;
 983	}
 984}
 985
 986static __init void check_quirks(void)
 987{
 988	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
 989		__check_quirks_intel();
 990}
 991
 992static __init bool get_rdt_resources(void)
 993{
 994	rdt_alloc_capable = get_rdt_alloc_resources();
 995	rdt_mon_capable = get_rdt_mon_resources();
 996
 997	return (rdt_mon_capable || rdt_alloc_capable);
 998}
 999
1000static __init void rdt_init_res_defs_intel(void)
1001{
1002	struct rdt_hw_resource *hw_res;
1003	struct rdt_resource *r;
1004
1005	for_each_rdt_resource(r) {
1006		hw_res = resctrl_to_arch_res(r);
1007
1008		if (r->rid == RDT_RESOURCE_L3 ||
1009		    r->rid == RDT_RESOURCE_L2) {
 
1010			r->cache.arch_has_per_cpu_cfg = false;
1011			r->cache.min_cbm_bits = 1;
1012		} else if (r->rid == RDT_RESOURCE_MBA) {
1013			hw_res->msr_base = MSR_IA32_MBA_THRTL_BASE;
1014			hw_res->msr_update = mba_wrmsr_intel;
1015		}
1016	}
1017}
1018
1019static __init void rdt_init_res_defs_amd(void)
1020{
1021	struct rdt_hw_resource *hw_res;
1022	struct rdt_resource *r;
1023
1024	for_each_rdt_resource(r) {
1025		hw_res = resctrl_to_arch_res(r);
1026
1027		if (r->rid == RDT_RESOURCE_L3 ||
1028		    r->rid == RDT_RESOURCE_L2) {
1029			r->cache.arch_has_sparse_bitmasks = true;
1030			r->cache.arch_has_per_cpu_cfg = true;
1031			r->cache.min_cbm_bits = 0;
1032		} else if (r->rid == RDT_RESOURCE_MBA) {
1033			hw_res->msr_base = MSR_IA32_MBA_BW_BASE;
1034			hw_res->msr_update = mba_wrmsr_amd;
1035		} else if (r->rid == RDT_RESOURCE_SMBA) {
1036			hw_res->msr_base = MSR_IA32_SMBA_BW_BASE;
1037			hw_res->msr_update = mba_wrmsr_amd;
1038		}
1039	}
1040}
1041
1042static __init void rdt_init_res_defs(void)
1043{
1044	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1045		rdt_init_res_defs_intel();
1046	else if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1047		rdt_init_res_defs_amd();
1048}
1049
1050static enum cpuhp_state rdt_online;
1051
1052/* Runs once on the BSP during boot. */
1053void resctrl_cpu_detect(struct cpuinfo_x86 *c)
1054{
1055	if (!cpu_has(c, X86_FEATURE_CQM_LLC)) {
1056		c->x86_cache_max_rmid  = -1;
1057		c->x86_cache_occ_scale = -1;
1058		c->x86_cache_mbm_width_offset = -1;
1059		return;
1060	}
1061
1062	/* will be overridden if occupancy monitoring exists */
1063	c->x86_cache_max_rmid = cpuid_ebx(0xf);
1064
1065	if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC) ||
1066	    cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL) ||
1067	    cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)) {
1068		u32 eax, ebx, ecx, edx;
1069
1070		/* QoS sub-leaf, EAX=0Fh, ECX=1 */
1071		cpuid_count(0xf, 1, &eax, &ebx, &ecx, &edx);
1072
1073		c->x86_cache_max_rmid  = ecx;
1074		c->x86_cache_occ_scale = ebx;
1075		c->x86_cache_mbm_width_offset = eax & 0xff;
1076
1077		if (c->x86_vendor == X86_VENDOR_AMD && !c->x86_cache_mbm_width_offset)
1078			c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_AMD;
1079	}
1080}
1081
1082static int __init resctrl_late_init(void)
1083{
1084	struct rdt_resource *r;
1085	int state, ret;
1086
1087	/*
1088	 * Initialize functions(or definitions) that are different
1089	 * between vendors here.
1090	 */
1091	rdt_init_res_defs();
1092
1093	check_quirks();
1094
1095	if (!get_rdt_resources())
1096		return -ENODEV;
1097
1098	rdt_init_padding();
1099
1100	state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
1101				  "x86/resctrl/cat:online:",
1102				  resctrl_arch_online_cpu,
1103				  resctrl_arch_offline_cpu);
1104	if (state < 0)
1105		return state;
1106
1107	ret = rdtgroup_init();
1108	if (ret) {
1109		cpuhp_remove_state(state);
1110		return ret;
1111	}
1112	rdt_online = state;
1113
1114	for_each_alloc_capable_rdt_resource(r)
1115		pr_info("%s allocation detected\n", r->name);
1116
1117	for_each_mon_capable_rdt_resource(r)
1118		pr_info("%s monitoring detected\n", r->name);
1119
1120	return 0;
1121}
1122
1123late_initcall(resctrl_late_init);
1124
1125static void __exit resctrl_exit(void)
1126{
1127	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
1128
1129	cpuhp_remove_state(rdt_online);
1130
1131	rdtgroup_exit();
1132
1133	if (r->mon_capable)
1134		rdt_put_mon_l3_config();
1135}
1136
1137__exitcall(resctrl_exit);
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Resource Director Technology(RDT)
  4 * - Cache Allocation code.
  5 *
  6 * Copyright (C) 2016 Intel Corporation
  7 *
  8 * Authors:
  9 *    Fenghua Yu <fenghua.yu@intel.com>
 10 *    Tony Luck <tony.luck@intel.com>
 11 *    Vikas Shivappa <vikas.shivappa@intel.com>
 12 *
 13 * More information about RDT be found in the Intel (R) x86 Architecture
 14 * Software Developer Manual June 2016, volume 3, section 17.17.
 15 */
 16
 17#define pr_fmt(fmt)	"resctrl: " fmt
 18
 
 19#include <linux/slab.h>
 20#include <linux/err.h>
 21#include <linux/cacheinfo.h>
 22#include <linux/cpuhotplug.h>
 23
 24#include <asm/intel-family.h>
 25#include <asm/resctrl.h>
 26#include "internal.h"
 27
 28/* Mutex to protect rdtgroup access. */
 29DEFINE_MUTEX(rdtgroup_mutex);
 
 
 
 
 
 
 
 30
 31/*
 32 * The cached resctrl_pqr_state is strictly per CPU and can never be
 33 * updated from a remote CPU. Functions which modify the state
 34 * are called with interrupts disabled and no preemption, which
 35 * is sufficient for the protection.
 36 */
 37DEFINE_PER_CPU(struct resctrl_pqr_state, pqr_state);
 38
 39/*
 40 * Used to store the max resource name width and max resource data width
 41 * to display the schemata in a tabular format
 42 */
 43int max_name_width, max_data_width;
 44
 45/*
 46 * Global boolean for rdt_alloc which is true if any
 47 * resource allocation is enabled.
 48 */
 49bool rdt_alloc_capable;
 50
 51static void
 52mba_wrmsr_intel(struct rdt_domain *d, struct msr_param *m,
 53		struct rdt_resource *r);
 54static void
 55cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r);
 56static void
 57mba_wrmsr_amd(struct rdt_domain *d, struct msr_param *m,
 58	      struct rdt_resource *r);
 59
 60#define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].r_resctrl.domains)
 
 61
 62struct rdt_hw_resource rdt_resources_all[] = {
 63	[RDT_RESOURCE_L3] =
 64	{
 65		.r_resctrl = {
 66			.rid			= RDT_RESOURCE_L3,
 67			.name			= "L3",
 68			.cache_level		= 3,
 69			.domains		= domain_init(RDT_RESOURCE_L3),
 
 
 70			.parse_ctrlval		= parse_cbm,
 71			.format_str		= "%d=%0*x",
 72			.fflags			= RFTYPE_RES_CACHE,
 73		},
 74		.msr_base		= MSR_IA32_L3_CBM_BASE,
 75		.msr_update		= cat_wrmsr,
 76	},
 77	[RDT_RESOURCE_L2] =
 78	{
 79		.r_resctrl = {
 80			.rid			= RDT_RESOURCE_L2,
 81			.name			= "L2",
 82			.cache_level		= 2,
 83			.domains		= domain_init(RDT_RESOURCE_L2),
 84			.parse_ctrlval		= parse_cbm,
 85			.format_str		= "%d=%0*x",
 86			.fflags			= RFTYPE_RES_CACHE,
 87		},
 88		.msr_base		= MSR_IA32_L2_CBM_BASE,
 89		.msr_update		= cat_wrmsr,
 90	},
 91	[RDT_RESOURCE_MBA] =
 92	{
 93		.r_resctrl = {
 94			.rid			= RDT_RESOURCE_MBA,
 95			.name			= "MB",
 96			.cache_level		= 3,
 97			.domains		= domain_init(RDT_RESOURCE_MBA),
 
 
 
 
 
 
 
 
 
 
 
 
 98			.parse_ctrlval		= parse_bw,
 99			.format_str		= "%d=%*u",
100			.fflags			= RFTYPE_RES_MB,
101		},
102	},
103};
104
 
 
 
 
 
 
 
 
105/*
106 * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
107 * as they do not have CPUID enumeration support for Cache allocation.
108 * The check for Vendor/Family/Model is not enough to guarantee that
109 * the MSRs won't #GP fault because only the following SKUs support
110 * CAT:
111 *	Intel(R) Xeon(R)  CPU E5-2658  v3  @  2.20GHz
112 *	Intel(R) Xeon(R)  CPU E5-2648L v3  @  1.80GHz
113 *	Intel(R) Xeon(R)  CPU E5-2628L v3  @  2.00GHz
114 *	Intel(R) Xeon(R)  CPU E5-2618L v3  @  2.30GHz
115 *	Intel(R) Xeon(R)  CPU E5-2608L v3  @  2.00GHz
116 *	Intel(R) Xeon(R)  CPU E5-2658A v3  @  2.20GHz
117 *
118 * Probe by trying to write the first of the L3 cache mask registers
119 * and checking that the bits stick. Max CLOSids is always 4 and max cbm length
120 * is always 20 on hsw server parts. The minimum cache bitmask length
121 * allowed for HSW server is always 2 bits. Hardcode all of them.
122 */
123static inline void cache_alloc_hsw_probe(void)
124{
125	struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_L3];
126	struct rdt_resource *r  = &hw_res->r_resctrl;
127	u32 l, h, max_cbm = BIT_MASK(20) - 1;
128
129	if (wrmsr_safe(MSR_IA32_L3_CBM_BASE, max_cbm, 0))
130		return;
131
132	rdmsr(MSR_IA32_L3_CBM_BASE, l, h);
133
134	/* If all the bits were set in MSR, return success */
135	if (l != max_cbm)
136		return;
137
138	hw_res->num_closid = 4;
139	r->default_ctrl = max_cbm;
140	r->cache.cbm_len = 20;
141	r->cache.shareable_bits = 0xc0000;
142	r->cache.min_cbm_bits = 2;
 
143	r->alloc_capable = true;
144
145	rdt_alloc_capable = true;
146}
147
148bool is_mba_sc(struct rdt_resource *r)
149{
150	if (!r)
151		return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.mba_sc;
152
 
 
 
 
 
 
 
153	return r->membw.mba_sc;
154}
155
156/*
157 * rdt_get_mb_table() - get a mapping of bandwidth(b/w) percentage values
158 * exposed to user interface and the h/w understandable delay values.
159 *
160 * The non-linear delay values have the granularity of power of two
161 * and also the h/w does not guarantee a curve for configured delay
162 * values vs. actual b/w enforced.
163 * Hence we need a mapping that is pre calibrated so the user can
164 * express the memory b/w as a percentage value.
165 */
166static inline bool rdt_get_mb_table(struct rdt_resource *r)
167{
168	/*
169	 * There are no Intel SKUs as of now to support non-linear delay.
170	 */
171	pr_info("MBA b/w map not implemented for cpu:%d, model:%d",
172		boot_cpu_data.x86, boot_cpu_data.x86_model);
173
174	return false;
175}
176
177static bool __get_mem_config_intel(struct rdt_resource *r)
178{
179	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
180	union cpuid_0x10_3_eax eax;
181	union cpuid_0x10_x_edx edx;
182	u32 ebx, ecx, max_delay;
183
184	cpuid_count(0x00000010, 3, &eax.full, &ebx, &ecx, &edx.full);
185	hw_res->num_closid = edx.split.cos_max + 1;
186	max_delay = eax.split.max_delay + 1;
187	r->default_ctrl = MAX_MBA_BW;
188	r->membw.arch_needs_linear = true;
189	if (ecx & MBA_IS_LINEAR) {
190		r->membw.delay_linear = true;
191		r->membw.min_bw = MAX_MBA_BW - max_delay;
192		r->membw.bw_gran = MAX_MBA_BW - max_delay;
193	} else {
194		if (!rdt_get_mb_table(r))
195			return false;
196		r->membw.arch_needs_linear = false;
197	}
198	r->data_width = 3;
199
200	if (boot_cpu_has(X86_FEATURE_PER_THREAD_MBA))
201		r->membw.throttle_mode = THREAD_THROTTLE_PER_THREAD;
202	else
203		r->membw.throttle_mode = THREAD_THROTTLE_MAX;
204	thread_throttle_mode_init();
205
206	r->alloc_capable = true;
207
208	return true;
209}
210
211static bool __rdt_get_mem_config_amd(struct rdt_resource *r)
212{
213	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
214	union cpuid_0x10_3_eax eax;
215	union cpuid_0x10_x_edx edx;
216	u32 ebx, ecx;
 
 
 
 
217
218	cpuid_count(0x80000020, 1, &eax.full, &ebx, &ecx, &edx.full);
219	hw_res->num_closid = edx.split.cos_max + 1;
220	r->default_ctrl = MAX_MBA_BW_AMD;
221
222	/* AMD does not use delay */
223	r->membw.delay_linear = false;
224	r->membw.arch_needs_linear = false;
225
226	/*
227	 * AMD does not use memory delay throttle model to control
228	 * the allocation like Intel does.
229	 */
230	r->membw.throttle_mode = THREAD_THROTTLE_UNDEFINED;
231	r->membw.min_bw = 0;
232	r->membw.bw_gran = 1;
233	/* Max value is 2048, Data width should be 4 in decimal */
234	r->data_width = 4;
235
236	r->alloc_capable = true;
237
238	return true;
239}
240
241static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r)
242{
243	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
244	union cpuid_0x10_1_eax eax;
 
245	union cpuid_0x10_x_edx edx;
246	u32 ebx, ecx;
247
248	cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full);
249	hw_res->num_closid = edx.split.cos_max + 1;
250	r->cache.cbm_len = eax.split.cbm_len + 1;
251	r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
252	r->cache.shareable_bits = ebx & r->default_ctrl;
253	r->data_width = (r->cache.cbm_len + 3) / 4;
 
 
254	r->alloc_capable = true;
255}
256
257static void rdt_get_cdp_config(int level)
258{
259	/*
260	 * By default, CDP is disabled. CDP can be enabled by mount parameter
261	 * "cdp" during resctrl file system mount time.
262	 */
263	rdt_resources_all[level].cdp_enabled = false;
264	rdt_resources_all[level].r_resctrl.cdp_capable = true;
265}
266
267static void rdt_get_cdp_l3_config(void)
268{
269	rdt_get_cdp_config(RDT_RESOURCE_L3);
270}
271
272static void rdt_get_cdp_l2_config(void)
273{
274	rdt_get_cdp_config(RDT_RESOURCE_L2);
275}
276
277static void
278mba_wrmsr_amd(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
279{
 
 
280	unsigned int i;
281	struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
282	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
283
284	for (i = m->low; i < m->high; i++)
285		wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
286}
287
288/*
289 * Map the memory b/w percentage value to delay values
290 * that can be written to QOS_MSRs.
291 * There are currently no SKUs which support non linear delay values.
292 */
293static u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)
294{
295	if (r->membw.delay_linear)
296		return MAX_MBA_BW - bw;
297
298	pr_warn_once("Non Linear delay-bw map not supported but queried\n");
299	return r->default_ctrl;
300}
301
302static void
303mba_wrmsr_intel(struct rdt_domain *d, struct msr_param *m,
304		struct rdt_resource *r)
305{
 
 
306	unsigned int i;
307	struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
308	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
309
310	/*  Write the delay values for mba. */
311	for (i = m->low; i < m->high; i++)
312		wrmsrl(hw_res->msr_base + i, delay_bw_map(hw_dom->ctrl_val[i], r));
313}
314
315static void
316cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
317{
 
 
318	unsigned int i;
319	struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
320	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
321
322	for (i = m->low; i < m->high; i++)
323		wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
324}
325
326struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r)
327{
328	struct rdt_domain *d;
 
 
329
330	list_for_each_entry(d, &r->domains, list) {
331		/* Find the domain that contains this CPU */
332		if (cpumask_test_cpu(cpu, &d->cpu_mask))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333			return d;
334	}
335
336	return NULL;
337}
338
339u32 resctrl_arch_get_num_closid(struct rdt_resource *r)
340{
341	return resctrl_to_arch_res(r)->num_closid;
342}
343
344void rdt_ctrl_update(void *arg)
345{
 
346	struct msr_param *m = arg;
347	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(m->res);
348	struct rdt_resource *r = m->res;
349	int cpu = smp_processor_id();
350	struct rdt_domain *d;
351
352	d = get_domain_from_cpu(cpu, r);
353	if (d) {
354		hw_res->msr_update(d, m, r);
355		return;
356	}
357	pr_warn_once("cpu %d not found in any domain for resource %s\n",
358		     cpu, r->name);
359}
360
361/*
362 * rdt_find_domain - Find a domain in a resource that matches input resource id
363 *
364 * Search resource r's domain list to find the resource id. If the resource
365 * id is found in a domain, return the domain. Otherwise, if requested by
366 * caller, return the first domain whose id is bigger than the input id.
367 * The domain list is sorted by id in ascending order.
368 */
369struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
370				   struct list_head **pos)
371{
372	struct rdt_domain *d;
373	struct list_head *l;
374
375	if (id < 0)
376		return ERR_PTR(-ENODEV);
377
378	list_for_each(l, &r->domains) {
379		d = list_entry(l, struct rdt_domain, list);
380		/* When id is found, return its domain. */
381		if (id == d->id)
382			return d;
383		/* Stop searching when finding id's position in sorted list. */
384		if (id < d->id)
385			break;
386	}
387
388	if (pos)
389		*pos = l;
390
391	return NULL;
392}
393
394static void setup_default_ctrlval(struct rdt_resource *r, u32 *dc)
395{
396	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
397	int i;
398
399	/*
400	 * Initialize the Control MSRs to having no control.
401	 * For Cache Allocation: Set all bits in cbm
402	 * For Memory Allocation: Set b/w requested to 100%
403	 */
404	for (i = 0; i < hw_res->num_closid; i++, dc++)
405		*dc = r->default_ctrl;
406}
407
408static void domain_free(struct rdt_hw_domain *hw_dom)
 
 
 
 
 
 
409{
410	kfree(hw_dom->arch_mbm_total);
411	kfree(hw_dom->arch_mbm_local);
412	kfree(hw_dom->ctrl_val);
413	kfree(hw_dom);
414}
415
416static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
417{
 
418	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
419	struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
420	struct msr_param m;
421	u32 *dc;
422
423	dc = kmalloc_array(hw_res->num_closid, sizeof(*hw_dom->ctrl_val),
424			   GFP_KERNEL);
425	if (!dc)
426		return -ENOMEM;
427
428	hw_dom->ctrl_val = dc;
429	setup_default_ctrlval(r, dc);
430
 
 
431	m.low = 0;
432	m.high = hw_res->num_closid;
433	hw_res->msr_update(d, &m, r);
434	return 0;
435}
436
437/**
438 * arch_domain_mbm_alloc() - Allocate arch private storage for the MBM counters
439 * @num_rmid:	The size of the MBM counter array
440 * @hw_dom:	The domain that owns the allocated arrays
441 */
442static int arch_domain_mbm_alloc(u32 num_rmid, struct rdt_hw_domain *hw_dom)
443{
444	size_t tsize;
445
446	if (is_mbm_total_enabled()) {
447		tsize = sizeof(*hw_dom->arch_mbm_total);
448		hw_dom->arch_mbm_total = kcalloc(num_rmid, tsize, GFP_KERNEL);
449		if (!hw_dom->arch_mbm_total)
450			return -ENOMEM;
451	}
452	if (is_mbm_local_enabled()) {
453		tsize = sizeof(*hw_dom->arch_mbm_local);
454		hw_dom->arch_mbm_local = kcalloc(num_rmid, tsize, GFP_KERNEL);
455		if (!hw_dom->arch_mbm_local) {
456			kfree(hw_dom->arch_mbm_total);
457			hw_dom->arch_mbm_total = NULL;
458			return -ENOMEM;
459		}
460	}
461
462	return 0;
463}
464
465/*
466 * domain_add_cpu - Add a cpu to a resource's domain list.
467 *
468 * If an existing domain in the resource r's domain list matches the cpu's
469 * resource id, add the cpu in the domain.
470 *
471 * Otherwise, a new domain is allocated and inserted into the right position
472 * in the domain list sorted by id in ascending order.
473 *
474 * The order in the domain list is visible to users when we print entries
475 * in the schemata file and schemata input is validated to have the same order
476 * as this list.
477 */
478static void domain_add_cpu(int cpu, struct rdt_resource *r)
 
 
479{
480	int id = get_cpu_cacheinfo_id(cpu, r->cache_level);
 
481	struct list_head *add_pos = NULL;
482	struct rdt_hw_domain *hw_dom;
483	struct rdt_domain *d;
484	int err;
485
486	d = rdt_find_domain(r, id, &add_pos);
487	if (IS_ERR(d)) {
488		pr_warn("Couldn't find cache id for CPU %d\n", cpu);
 
 
489		return;
490	}
491
492	if (d) {
493		cpumask_set_cpu(cpu, &d->cpu_mask);
 
 
 
 
 
494		if (r->cache.arch_has_per_cpu_cfg)
495			rdt_domain_reconfigure_cdp(r);
496		return;
497	}
498
499	hw_dom = kzalloc_node(sizeof(*hw_dom), GFP_KERNEL, cpu_to_node(cpu));
500	if (!hw_dom)
501		return;
502
503	d = &hw_dom->d_resctrl;
504	d->id = id;
505	cpumask_set_cpu(cpu, &d->cpu_mask);
 
506
507	rdt_domain_reconfigure_cdp(r);
508
509	if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
510		domain_free(hw_dom);
511		return;
512	}
513
514	if (r->mon_capable && arch_domain_mbm_alloc(r->num_rmid, hw_dom)) {
515		domain_free(hw_dom);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
516		return;
517	}
518
519	list_add_tail(&d->list, add_pos);
 
 
 
 
520
521	err = resctrl_online_domain(r, d);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
522	if (err) {
523		list_del(&d->list);
524		domain_free(hw_dom);
 
525	}
526}
527
528static void domain_remove_cpu(int cpu, struct rdt_resource *r)
 
 
 
 
 
 
 
 
529{
530	int id = get_cpu_cacheinfo_id(cpu, r->cache_level);
531	struct rdt_hw_domain *hw_dom;
532	struct rdt_domain *d;
 
 
 
 
 
 
 
 
 
533
534	d = rdt_find_domain(r, id, NULL);
535	if (IS_ERR_OR_NULL(d)) {
536		pr_warn("Couldn't find cache id for CPU %d\n", cpu);
 
537		return;
538	}
539	hw_dom = resctrl_to_arch_dom(d);
540
541	cpumask_clear_cpu(cpu, &d->cpu_mask);
542	if (cpumask_empty(&d->cpu_mask)) {
543		resctrl_offline_domain(r, d);
544		list_del(&d->list);
 
 
 
 
 
 
 
545
546		/*
547		 * rdt_domain "d" is going to be freed below, so clear
548		 * its pointer from pseudo_lock_region struct.
549		 */
550		if (d->plr)
551			d->plr->d = NULL;
552		domain_free(hw_dom);
553
554		return;
555	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
556
557	if (r == &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl) {
558		if (is_mbm_enabled() && cpu == d->mbm_work_cpu) {
559			cancel_delayed_work(&d->mbm_over);
560			mbm_setup_overflow_handler(d, 0);
561		}
562		if (is_llc_occupancy_enabled() && cpu == d->cqm_work_cpu &&
563		    has_busy_rmid(r, d)) {
564			cancel_delayed_work(&d->cqm_limbo);
565			cqm_setup_limbo_handler(d, 0);
566		}
 
567	}
568}
569
 
 
 
 
 
 
 
 
570static void clear_closid_rmid(int cpu)
571{
572	struct resctrl_pqr_state *state = this_cpu_ptr(&pqr_state);
573
574	state->default_closid = 0;
575	state->default_rmid = 0;
576	state->cur_closid = 0;
577	state->cur_rmid = 0;
578	wrmsr(MSR_IA32_PQR_ASSOC, 0, 0);
 
579}
580
581static int resctrl_online_cpu(unsigned int cpu)
582{
583	struct rdt_resource *r;
584
585	mutex_lock(&rdtgroup_mutex);
586	for_each_capable_rdt_resource(r)
587		domain_add_cpu(cpu, r);
588	/* The cpu is set in default rdtgroup after online. */
589	cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
590	clear_closid_rmid(cpu);
591	mutex_unlock(&rdtgroup_mutex);
592
593	return 0;
594}
595
596static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)
597{
598	struct rdtgroup *cr;
599
600	list_for_each_entry(cr, &r->mon.crdtgrp_list, mon.crdtgrp_list) {
601		if (cpumask_test_and_clear_cpu(cpu, &cr->cpu_mask)) {
602			break;
603		}
604	}
605}
606
607static int resctrl_offline_cpu(unsigned int cpu)
608{
609	struct rdtgroup *rdtgrp;
610	struct rdt_resource *r;
611
612	mutex_lock(&rdtgroup_mutex);
613	for_each_capable_rdt_resource(r)
614		domain_remove_cpu(cpu, r);
615	list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
616		if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask)) {
617			clear_childcpus(rdtgrp, cpu);
618			break;
619		}
620	}
621	clear_closid_rmid(cpu);
622	mutex_unlock(&rdtgroup_mutex);
623
624	return 0;
625}
626
627/*
628 * Choose a width for the resource name and resource data based on the
629 * resource that has widest name and cbm.
630 */
631static __init void rdt_init_padding(void)
632{
633	struct rdt_resource *r;
634
635	for_each_alloc_capable_rdt_resource(r) {
636		if (r->data_width > max_data_width)
637			max_data_width = r->data_width;
638	}
639}
640
641enum {
642	RDT_FLAG_CMT,
643	RDT_FLAG_MBM_TOTAL,
644	RDT_FLAG_MBM_LOCAL,
645	RDT_FLAG_L3_CAT,
646	RDT_FLAG_L3_CDP,
647	RDT_FLAG_L2_CAT,
648	RDT_FLAG_L2_CDP,
649	RDT_FLAG_MBA,
 
 
650};
651
652#define RDT_OPT(idx, n, f)	\
653[idx] = {			\
654	.name = n,		\
655	.flag = f		\
656}
657
658struct rdt_options {
659	char	*name;
660	int	flag;
661	bool	force_off, force_on;
662};
663
664static struct rdt_options rdt_options[]  __initdata = {
665	RDT_OPT(RDT_FLAG_CMT,	    "cmt",	X86_FEATURE_CQM_OCCUP_LLC),
666	RDT_OPT(RDT_FLAG_MBM_TOTAL, "mbmtotal", X86_FEATURE_CQM_MBM_TOTAL),
667	RDT_OPT(RDT_FLAG_MBM_LOCAL, "mbmlocal", X86_FEATURE_CQM_MBM_LOCAL),
668	RDT_OPT(RDT_FLAG_L3_CAT,    "l3cat",	X86_FEATURE_CAT_L3),
669	RDT_OPT(RDT_FLAG_L3_CDP,    "l3cdp",	X86_FEATURE_CDP_L3),
670	RDT_OPT(RDT_FLAG_L2_CAT,    "l2cat",	X86_FEATURE_CAT_L2),
671	RDT_OPT(RDT_FLAG_L2_CDP,    "l2cdp",	X86_FEATURE_CDP_L2),
672	RDT_OPT(RDT_FLAG_MBA,	    "mba",	X86_FEATURE_MBA),
 
 
673};
674#define NUM_RDT_OPTIONS ARRAY_SIZE(rdt_options)
675
676static int __init set_rdt_options(char *str)
677{
678	struct rdt_options *o;
679	bool force_off;
680	char *tok;
681
682	if (*str == '=')
683		str++;
684	while ((tok = strsep(&str, ",")) != NULL) {
685		force_off = *tok == '!';
686		if (force_off)
687			tok++;
688		for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
689			if (strcmp(tok, o->name) == 0) {
690				if (force_off)
691					o->force_off = true;
692				else
693					o->force_on = true;
694				break;
695			}
696		}
697	}
698	return 1;
699}
700__setup("rdt", set_rdt_options);
701
702static bool __init rdt_cpu_has(int flag)
703{
704	bool ret = boot_cpu_has(flag);
705	struct rdt_options *o;
706
707	if (!ret)
708		return ret;
709
710	for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
711		if (flag == o->flag) {
712			if (o->force_off)
713				ret = false;
714			if (o->force_on)
715				ret = true;
716			break;
717		}
718	}
719	return ret;
720}
721
722static __init bool get_mem_config(void)
723{
724	struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_MBA];
725
726	if (!rdt_cpu_has(X86_FEATURE_MBA))
727		return false;
728
729	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
730		return __get_mem_config_intel(&hw_res->r_resctrl);
731	else if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
732		return __rdt_get_mem_config_amd(&hw_res->r_resctrl);
733
734	return false;
735}
736
 
 
 
 
 
 
 
 
 
 
 
 
 
737static __init bool get_rdt_alloc_resources(void)
738{
739	struct rdt_resource *r;
740	bool ret = false;
741
742	if (rdt_alloc_capable)
743		return true;
744
745	if (!boot_cpu_has(X86_FEATURE_RDT_A))
746		return false;
747
748	if (rdt_cpu_has(X86_FEATURE_CAT_L3)) {
749		r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
750		rdt_get_cache_alloc_cfg(1, r);
751		if (rdt_cpu_has(X86_FEATURE_CDP_L3))
752			rdt_get_cdp_l3_config();
753		ret = true;
754	}
755	if (rdt_cpu_has(X86_FEATURE_CAT_L2)) {
756		/* CPUID 0x10.2 fields are same format at 0x10.1 */
757		r = &rdt_resources_all[RDT_RESOURCE_L2].r_resctrl;
758		rdt_get_cache_alloc_cfg(2, r);
759		if (rdt_cpu_has(X86_FEATURE_CDP_L2))
760			rdt_get_cdp_l2_config();
761		ret = true;
762	}
763
764	if (get_mem_config())
765		ret = true;
766
 
 
 
767	return ret;
768}
769
770static __init bool get_rdt_mon_resources(void)
771{
772	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
773
774	if (rdt_cpu_has(X86_FEATURE_CQM_OCCUP_LLC))
775		rdt_mon_features |= (1 << QOS_L3_OCCUP_EVENT_ID);
776	if (rdt_cpu_has(X86_FEATURE_CQM_MBM_TOTAL))
777		rdt_mon_features |= (1 << QOS_L3_MBM_TOTAL_EVENT_ID);
778	if (rdt_cpu_has(X86_FEATURE_CQM_MBM_LOCAL))
779		rdt_mon_features |= (1 << QOS_L3_MBM_LOCAL_EVENT_ID);
780
781	if (!rdt_mon_features)
782		return false;
783
784	return !rdt_get_mon_l3_config(r);
785}
786
787static __init void __check_quirks_intel(void)
788{
789	switch (boot_cpu_data.x86_model) {
790	case INTEL_FAM6_HASWELL_X:
791		if (!rdt_options[RDT_FLAG_L3_CAT].force_off)
792			cache_alloc_hsw_probe();
793		break;
794	case INTEL_FAM6_SKYLAKE_X:
795		if (boot_cpu_data.x86_stepping <= 4)
796			set_rdt_options("!cmt,!mbmtotal,!mbmlocal,!l3cat");
797		else
798			set_rdt_options("!l3cat");
799		fallthrough;
800	case INTEL_FAM6_BROADWELL_X:
801		intel_rdt_mbm_apply_quirk();
802		break;
803	}
804}
805
806static __init void check_quirks(void)
807{
808	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
809		__check_quirks_intel();
810}
811
812static __init bool get_rdt_resources(void)
813{
814	rdt_alloc_capable = get_rdt_alloc_resources();
815	rdt_mon_capable = get_rdt_mon_resources();
816
817	return (rdt_mon_capable || rdt_alloc_capable);
818}
819
820static __init void rdt_init_res_defs_intel(void)
821{
822	struct rdt_hw_resource *hw_res;
823	struct rdt_resource *r;
824
825	for_each_rdt_resource(r) {
826		hw_res = resctrl_to_arch_res(r);
827
828		if (r->rid == RDT_RESOURCE_L3 ||
829		    r->rid == RDT_RESOURCE_L2) {
830			r->cache.arch_has_sparse_bitmaps = false;
831			r->cache.arch_has_per_cpu_cfg = false;
832			r->cache.min_cbm_bits = 1;
833		} else if (r->rid == RDT_RESOURCE_MBA) {
834			hw_res->msr_base = MSR_IA32_MBA_THRTL_BASE;
835			hw_res->msr_update = mba_wrmsr_intel;
836		}
837	}
838}
839
840static __init void rdt_init_res_defs_amd(void)
841{
842	struct rdt_hw_resource *hw_res;
843	struct rdt_resource *r;
844
845	for_each_rdt_resource(r) {
846		hw_res = resctrl_to_arch_res(r);
847
848		if (r->rid == RDT_RESOURCE_L3 ||
849		    r->rid == RDT_RESOURCE_L2) {
850			r->cache.arch_has_sparse_bitmaps = true;
851			r->cache.arch_has_per_cpu_cfg = true;
852			r->cache.min_cbm_bits = 0;
853		} else if (r->rid == RDT_RESOURCE_MBA) {
854			hw_res->msr_base = MSR_IA32_MBA_BW_BASE;
855			hw_res->msr_update = mba_wrmsr_amd;
 
 
 
856		}
857	}
858}
859
860static __init void rdt_init_res_defs(void)
861{
862	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
863		rdt_init_res_defs_intel();
864	else if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
865		rdt_init_res_defs_amd();
866}
867
868static enum cpuhp_state rdt_online;
869
870/* Runs once on the BSP during boot. */
871void resctrl_cpu_detect(struct cpuinfo_x86 *c)
872{
873	if (!cpu_has(c, X86_FEATURE_CQM_LLC)) {
874		c->x86_cache_max_rmid  = -1;
875		c->x86_cache_occ_scale = -1;
876		c->x86_cache_mbm_width_offset = -1;
877		return;
878	}
879
880	/* will be overridden if occupancy monitoring exists */
881	c->x86_cache_max_rmid = cpuid_ebx(0xf);
882
883	if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC) ||
884	    cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL) ||
885	    cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)) {
886		u32 eax, ebx, ecx, edx;
887
888		/* QoS sub-leaf, EAX=0Fh, ECX=1 */
889		cpuid_count(0xf, 1, &eax, &ebx, &ecx, &edx);
890
891		c->x86_cache_max_rmid  = ecx;
892		c->x86_cache_occ_scale = ebx;
893		c->x86_cache_mbm_width_offset = eax & 0xff;
894
895		if (c->x86_vendor == X86_VENDOR_AMD && !c->x86_cache_mbm_width_offset)
896			c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_AMD;
897	}
898}
899
900static int __init resctrl_late_init(void)
901{
902	struct rdt_resource *r;
903	int state, ret;
904
905	/*
906	 * Initialize functions(or definitions) that are different
907	 * between vendors here.
908	 */
909	rdt_init_res_defs();
910
911	check_quirks();
912
913	if (!get_rdt_resources())
914		return -ENODEV;
915
916	rdt_init_padding();
917
918	state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
919				  "x86/resctrl/cat:online:",
920				  resctrl_online_cpu, resctrl_offline_cpu);
 
921	if (state < 0)
922		return state;
923
924	ret = rdtgroup_init();
925	if (ret) {
926		cpuhp_remove_state(state);
927		return ret;
928	}
929	rdt_online = state;
930
931	for_each_alloc_capable_rdt_resource(r)
932		pr_info("%s allocation detected\n", r->name);
933
934	for_each_mon_capable_rdt_resource(r)
935		pr_info("%s monitoring detected\n", r->name);
936
937	return 0;
938}
939
940late_initcall(resctrl_late_init);
941
942static void __exit resctrl_exit(void)
943{
 
 
944	cpuhp_remove_state(rdt_online);
 
945	rdtgroup_exit();
 
 
 
946}
947
948__exitcall(resctrl_exit);