• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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. */
29 DEFINE_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  */
37 DEFINE_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  */
43 int 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  */
49 bool rdt_alloc_capable;
50 
51 static void
52 mba_wrmsr_intel(struct rdt_domain *d, struct msr_param *m,
53 		struct rdt_resource *r);
54 static void
55 cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r);
56 static void
57 mba_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 
62 struct 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  */
cache_alloc_hsw_probe(void)123 static 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 	r->alloc_enabled = true;
145 
146 	rdt_alloc_capable = true;
147 }
148 
is_mba_sc(struct rdt_resource * r)149 bool is_mba_sc(struct rdt_resource *r)
150 {
151 	if (!r)
152 		return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.mba_sc;
153 
154 	return r->membw.mba_sc;
155 }
156 
157 /*
158  * rdt_get_mb_table() - get a mapping of bandwidth(b/w) percentage values
159  * exposed to user interface and the h/w understandable delay values.
160  *
161  * The non-linear delay values have the granularity of power of two
162  * and also the h/w does not guarantee a curve for configured delay
163  * values vs. actual b/w enforced.
164  * Hence we need a mapping that is pre calibrated so the user can
165  * express the memory b/w as a percentage value.
166  */
rdt_get_mb_table(struct rdt_resource * r)167 static inline bool rdt_get_mb_table(struct rdt_resource *r)
168 {
169 	/*
170 	 * There are no Intel SKUs as of now to support non-linear delay.
171 	 */
172 	pr_info("MBA b/w map not implemented for cpu:%d, model:%d",
173 		boot_cpu_data.x86, boot_cpu_data.x86_model);
174 
175 	return false;
176 }
177 
__get_mem_config_intel(struct rdt_resource * r)178 static bool __get_mem_config_intel(struct rdt_resource *r)
179 {
180 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
181 	union cpuid_0x10_3_eax eax;
182 	union cpuid_0x10_x_edx edx;
183 	u32 ebx, ecx, max_delay;
184 
185 	cpuid_count(0x00000010, 3, &eax.full, &ebx, &ecx, &edx.full);
186 	hw_res->num_closid = edx.split.cos_max + 1;
187 	max_delay = eax.split.max_delay + 1;
188 	r->default_ctrl = MAX_MBA_BW;
189 	r->membw.arch_needs_linear = true;
190 	if (ecx & MBA_IS_LINEAR) {
191 		r->membw.delay_linear = true;
192 		r->membw.min_bw = MAX_MBA_BW - max_delay;
193 		r->membw.bw_gran = MAX_MBA_BW - max_delay;
194 	} else {
195 		if (!rdt_get_mb_table(r))
196 			return false;
197 		r->membw.arch_needs_linear = false;
198 	}
199 	r->data_width = 3;
200 
201 	if (boot_cpu_has(X86_FEATURE_PER_THREAD_MBA))
202 		r->membw.throttle_mode = THREAD_THROTTLE_PER_THREAD;
203 	else
204 		r->membw.throttle_mode = THREAD_THROTTLE_MAX;
205 	thread_throttle_mode_init();
206 
207 	r->alloc_capable = true;
208 	r->alloc_enabled = true;
209 
210 	return true;
211 }
212 
__rdt_get_mem_config_amd(struct rdt_resource * r)213 static bool __rdt_get_mem_config_amd(struct rdt_resource *r)
214 {
215 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
216 	union cpuid_0x10_3_eax eax;
217 	union cpuid_0x10_x_edx edx;
218 	u32 ebx, ecx;
219 
220 	cpuid_count(0x80000020, 1, &eax.full, &ebx, &ecx, &edx.full);
221 	hw_res->num_closid = edx.split.cos_max + 1;
222 	r->default_ctrl = MAX_MBA_BW_AMD;
223 
224 	/* AMD does not use delay */
225 	r->membw.delay_linear = false;
226 	r->membw.arch_needs_linear = false;
227 
228 	/*
229 	 * AMD does not use memory delay throttle model to control
230 	 * the allocation like Intel does.
231 	 */
232 	r->membw.throttle_mode = THREAD_THROTTLE_UNDEFINED;
233 	r->membw.min_bw = 0;
234 	r->membw.bw_gran = 1;
235 	/* Max value is 2048, Data width should be 4 in decimal */
236 	r->data_width = 4;
237 
238 	r->alloc_capable = true;
239 	r->alloc_enabled = true;
240 
241 	return true;
242 }
243 
rdt_get_cache_alloc_cfg(int idx,struct rdt_resource * r)244 static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r)
245 {
246 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
247 	union cpuid_0x10_1_eax eax;
248 	union cpuid_0x10_x_edx edx;
249 	u32 ebx, ecx;
250 
251 	cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full);
252 	hw_res->num_closid = edx.split.cos_max + 1;
253 	r->cache.cbm_len = eax.split.cbm_len + 1;
254 	r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
255 	r->cache.shareable_bits = ebx & r->default_ctrl;
256 	r->data_width = (r->cache.cbm_len + 3) / 4;
257 	r->alloc_capable = true;
258 	r->alloc_enabled = true;
259 }
260 
rdt_get_cdp_config(int level)261 static void rdt_get_cdp_config(int level)
262 {
263 	/*
264 	 * By default, CDP is disabled. CDP can be enabled by mount parameter
265 	 * "cdp" during resctrl file system mount time.
266 	 */
267 	rdt_resources_all[level].cdp_enabled = false;
268 	rdt_resources_all[level].r_resctrl.cdp_capable = true;
269 }
270 
rdt_get_cdp_l3_config(void)271 static void rdt_get_cdp_l3_config(void)
272 {
273 	rdt_get_cdp_config(RDT_RESOURCE_L3);
274 }
275 
rdt_get_cdp_l2_config(void)276 static void rdt_get_cdp_l2_config(void)
277 {
278 	rdt_get_cdp_config(RDT_RESOURCE_L2);
279 }
280 
281 static void
mba_wrmsr_amd(struct rdt_domain * d,struct msr_param * m,struct rdt_resource * r)282 mba_wrmsr_amd(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
283 {
284 	unsigned int i;
285 	struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
286 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
287 
288 	for (i = m->low; i < m->high; i++)
289 		wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
290 }
291 
292 /*
293  * Map the memory b/w percentage value to delay values
294  * that can be written to QOS_MSRs.
295  * There are currently no SKUs which support non linear delay values.
296  */
delay_bw_map(unsigned long bw,struct rdt_resource * r)297 u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)
298 {
299 	if (r->membw.delay_linear)
300 		return MAX_MBA_BW - bw;
301 
302 	pr_warn_once("Non Linear delay-bw map not supported but queried\n");
303 	return r->default_ctrl;
304 }
305 
306 static void
mba_wrmsr_intel(struct rdt_domain * d,struct msr_param * m,struct rdt_resource * r)307 mba_wrmsr_intel(struct rdt_domain *d, struct msr_param *m,
308 		struct rdt_resource *r)
309 {
310 	unsigned int i;
311 	struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
312 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
313 
314 	/*  Write the delay values for mba. */
315 	for (i = m->low; i < m->high; i++)
316 		wrmsrl(hw_res->msr_base + i, delay_bw_map(hw_dom->ctrl_val[i], r));
317 }
318 
319 static void
cat_wrmsr(struct rdt_domain * d,struct msr_param * m,struct rdt_resource * r)320 cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
321 {
322 	unsigned int i;
323 	struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
324 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
325 
326 	for (i = m->low; i < m->high; i++)
327 		wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
328 }
329 
get_domain_from_cpu(int cpu,struct rdt_resource * r)330 struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r)
331 {
332 	struct rdt_domain *d;
333 
334 	list_for_each_entry(d, &r->domains, list) {
335 		/* Find the domain that contains this CPU */
336 		if (cpumask_test_cpu(cpu, &d->cpu_mask))
337 			return d;
338 	}
339 
340 	return NULL;
341 }
342 
resctrl_arch_get_num_closid(struct rdt_resource * r)343 u32 resctrl_arch_get_num_closid(struct rdt_resource *r)
344 {
345 	return resctrl_to_arch_res(r)->num_closid;
346 }
347 
rdt_ctrl_update(void * arg)348 void rdt_ctrl_update(void *arg)
349 {
350 	struct msr_param *m = arg;
351 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(m->res);
352 	struct rdt_resource *r = m->res;
353 	int cpu = smp_processor_id();
354 	struct rdt_domain *d;
355 
356 	d = get_domain_from_cpu(cpu, r);
357 	if (d) {
358 		hw_res->msr_update(d, m, r);
359 		return;
360 	}
361 	pr_warn_once("cpu %d not found in any domain for resource %s\n",
362 		     cpu, r->name);
363 }
364 
365 /*
366  * rdt_find_domain - Find a domain in a resource that matches input resource id
367  *
368  * Search resource r's domain list to find the resource id. If the resource
369  * id is found in a domain, return the domain. Otherwise, if requested by
370  * caller, return the first domain whose id is bigger than the input id.
371  * The domain list is sorted by id in ascending order.
372  */
rdt_find_domain(struct rdt_resource * r,int id,struct list_head ** pos)373 struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
374 				   struct list_head **pos)
375 {
376 	struct rdt_domain *d;
377 	struct list_head *l;
378 
379 	if (id < 0)
380 		return ERR_PTR(-ENODEV);
381 
382 	list_for_each(l, &r->domains) {
383 		d = list_entry(l, struct rdt_domain, list);
384 		/* When id is found, return its domain. */
385 		if (id == d->id)
386 			return d;
387 		/* Stop searching when finding id's position in sorted list. */
388 		if (id < d->id)
389 			break;
390 	}
391 
392 	if (pos)
393 		*pos = l;
394 
395 	return NULL;
396 }
397 
setup_default_ctrlval(struct rdt_resource * r,u32 * dc,u32 * dm)398 void setup_default_ctrlval(struct rdt_resource *r, u32 *dc, u32 *dm)
399 {
400 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
401 	int i;
402 
403 	/*
404 	 * Initialize the Control MSRs to having no control.
405 	 * For Cache Allocation: Set all bits in cbm
406 	 * For Memory Allocation: Set b/w requested to 100%
407 	 * and the bandwidth in MBps to U32_MAX
408 	 */
409 	for (i = 0; i < hw_res->num_closid; i++, dc++, dm++) {
410 		*dc = r->default_ctrl;
411 		*dm = MBA_MAX_MBPS;
412 	}
413 }
414 
domain_setup_ctrlval(struct rdt_resource * r,struct rdt_domain * d)415 static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
416 {
417 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
418 	struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
419 	struct msr_param m;
420 	u32 *dc, *dm;
421 
422 	dc = kmalloc_array(hw_res->num_closid, sizeof(*hw_dom->ctrl_val),
423 			   GFP_KERNEL);
424 	if (!dc)
425 		return -ENOMEM;
426 
427 	dm = kmalloc_array(hw_res->num_closid, sizeof(*hw_dom->mbps_val),
428 			   GFP_KERNEL);
429 	if (!dm) {
430 		kfree(dc);
431 		return -ENOMEM;
432 	}
433 
434 	hw_dom->ctrl_val = dc;
435 	hw_dom->mbps_val = dm;
436 	setup_default_ctrlval(r, dc, dm);
437 
438 	m.low = 0;
439 	m.high = hw_res->num_closid;
440 	hw_res->msr_update(d, &m, r);
441 	return 0;
442 }
443 
domain_setup_mon_state(struct rdt_resource * r,struct rdt_domain * d)444 static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
445 {
446 	size_t tsize;
447 
448 	if (is_llc_occupancy_enabled()) {
449 		d->rmid_busy_llc = bitmap_zalloc(r->num_rmid, GFP_KERNEL);
450 		if (!d->rmid_busy_llc)
451 			return -ENOMEM;
452 		INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo);
453 	}
454 	if (is_mbm_total_enabled()) {
455 		tsize = sizeof(*d->mbm_total);
456 		d->mbm_total = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
457 		if (!d->mbm_total) {
458 			bitmap_free(d->rmid_busy_llc);
459 			return -ENOMEM;
460 		}
461 	}
462 	if (is_mbm_local_enabled()) {
463 		tsize = sizeof(*d->mbm_local);
464 		d->mbm_local = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
465 		if (!d->mbm_local) {
466 			bitmap_free(d->rmid_busy_llc);
467 			kfree(d->mbm_total);
468 			return -ENOMEM;
469 		}
470 	}
471 
472 	if (is_mbm_enabled()) {
473 		INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow);
474 		mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL);
475 	}
476 
477 	return 0;
478 }
479 
480 /*
481  * domain_add_cpu - Add a cpu to a resource's domain list.
482  *
483  * If an existing domain in the resource r's domain list matches the cpu's
484  * resource id, add the cpu in the domain.
485  *
486  * Otherwise, a new domain is allocated and inserted into the right position
487  * in the domain list sorted by id in ascending order.
488  *
489  * The order in the domain list is visible to users when we print entries
490  * in the schemata file and schemata input is validated to have the same order
491  * as this list.
492  */
domain_add_cpu(int cpu,struct rdt_resource * r)493 static void domain_add_cpu(int cpu, struct rdt_resource *r)
494 {
495 	int id = get_cpu_cacheinfo_id(cpu, r->cache_level);
496 	struct list_head *add_pos = NULL;
497 	struct rdt_hw_domain *hw_dom;
498 	struct rdt_domain *d;
499 
500 	d = rdt_find_domain(r, id, &add_pos);
501 	if (IS_ERR(d)) {
502 		pr_warn("Couldn't find cache id for CPU %d\n", cpu);
503 		return;
504 	}
505 
506 	if (d) {
507 		cpumask_set_cpu(cpu, &d->cpu_mask);
508 		if (r->cache.arch_has_per_cpu_cfg)
509 			rdt_domain_reconfigure_cdp(r);
510 		return;
511 	}
512 
513 	hw_dom = kzalloc_node(sizeof(*hw_dom), GFP_KERNEL, cpu_to_node(cpu));
514 	if (!hw_dom)
515 		return;
516 
517 	d = &hw_dom->d_resctrl;
518 	d->id = id;
519 	cpumask_set_cpu(cpu, &d->cpu_mask);
520 
521 	rdt_domain_reconfigure_cdp(r);
522 
523 	if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
524 		kfree(hw_dom);
525 		return;
526 	}
527 
528 	if (r->mon_capable && domain_setup_mon_state(r, d)) {
529 		kfree(hw_dom->ctrl_val);
530 		kfree(hw_dom->mbps_val);
531 		kfree(hw_dom);
532 		return;
533 	}
534 
535 	list_add_tail(&d->list, add_pos);
536 
537 	/*
538 	 * If resctrl is mounted, add
539 	 * per domain monitor data directories.
540 	 */
541 	if (static_branch_unlikely(&rdt_mon_enable_key))
542 		mkdir_mondata_subdir_allrdtgrp(r, d);
543 }
544 
domain_remove_cpu(int cpu,struct rdt_resource * r)545 static void domain_remove_cpu(int cpu, struct rdt_resource *r)
546 {
547 	int id = get_cpu_cacheinfo_id(cpu, r->cache_level);
548 	struct rdt_hw_domain *hw_dom;
549 	struct rdt_domain *d;
550 
551 	d = rdt_find_domain(r, id, NULL);
552 	if (IS_ERR_OR_NULL(d)) {
553 		pr_warn("Couldn't find cache id for CPU %d\n", cpu);
554 		return;
555 	}
556 	hw_dom = resctrl_to_arch_dom(d);
557 
558 	cpumask_clear_cpu(cpu, &d->cpu_mask);
559 	if (cpumask_empty(&d->cpu_mask)) {
560 		/*
561 		 * If resctrl is mounted, remove all the
562 		 * per domain monitor data directories.
563 		 */
564 		if (static_branch_unlikely(&rdt_mon_enable_key))
565 			rmdir_mondata_subdir_allrdtgrp(r, d->id);
566 		list_del(&d->list);
567 		if (r->mon_capable && is_mbm_enabled())
568 			cancel_delayed_work(&d->mbm_over);
569 		if (is_llc_occupancy_enabled() &&  has_busy_rmid(r, d)) {
570 			/*
571 			 * When a package is going down, forcefully
572 			 * decrement rmid->ebusy. There is no way to know
573 			 * that the L3 was flushed and hence may lead to
574 			 * incorrect counts in rare scenarios, but leaving
575 			 * the RMID as busy creates RMID leaks if the
576 			 * package never comes back.
577 			 */
578 			__check_limbo(d, true);
579 			cancel_delayed_work(&d->cqm_limbo);
580 		}
581 
582 		/*
583 		 * rdt_domain "d" is going to be freed below, so clear
584 		 * its pointer from pseudo_lock_region struct.
585 		 */
586 		if (d->plr)
587 			d->plr->d = NULL;
588 
589 		kfree(hw_dom->ctrl_val);
590 		kfree(hw_dom->mbps_val);
591 		bitmap_free(d->rmid_busy_llc);
592 		kfree(d->mbm_total);
593 		kfree(d->mbm_local);
594 		kfree(hw_dom);
595 		return;
596 	}
597 
598 	if (r == &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl) {
599 		if (is_mbm_enabled() && cpu == d->mbm_work_cpu) {
600 			cancel_delayed_work(&d->mbm_over);
601 			mbm_setup_overflow_handler(d, 0);
602 		}
603 		if (is_llc_occupancy_enabled() && cpu == d->cqm_work_cpu &&
604 		    has_busy_rmid(r, d)) {
605 			cancel_delayed_work(&d->cqm_limbo);
606 			cqm_setup_limbo_handler(d, 0);
607 		}
608 	}
609 }
610 
clear_closid_rmid(int cpu)611 static void clear_closid_rmid(int cpu)
612 {
613 	struct resctrl_pqr_state *state = this_cpu_ptr(&pqr_state);
614 
615 	state->default_closid = 0;
616 	state->default_rmid = 0;
617 	state->cur_closid = 0;
618 	state->cur_rmid = 0;
619 	wrmsr(IA32_PQR_ASSOC, 0, 0);
620 }
621 
resctrl_online_cpu(unsigned int cpu)622 static int resctrl_online_cpu(unsigned int cpu)
623 {
624 	struct rdt_resource *r;
625 
626 	mutex_lock(&rdtgroup_mutex);
627 	for_each_capable_rdt_resource(r)
628 		domain_add_cpu(cpu, r);
629 	/* The cpu is set in default rdtgroup after online. */
630 	cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
631 	clear_closid_rmid(cpu);
632 	mutex_unlock(&rdtgroup_mutex);
633 
634 	return 0;
635 }
636 
clear_childcpus(struct rdtgroup * r,unsigned int cpu)637 static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)
638 {
639 	struct rdtgroup *cr;
640 
641 	list_for_each_entry(cr, &r->mon.crdtgrp_list, mon.crdtgrp_list) {
642 		if (cpumask_test_and_clear_cpu(cpu, &cr->cpu_mask)) {
643 			break;
644 		}
645 	}
646 }
647 
resctrl_offline_cpu(unsigned int cpu)648 static int resctrl_offline_cpu(unsigned int cpu)
649 {
650 	struct rdtgroup *rdtgrp;
651 	struct rdt_resource *r;
652 
653 	mutex_lock(&rdtgroup_mutex);
654 	for_each_capable_rdt_resource(r)
655 		domain_remove_cpu(cpu, r);
656 	list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
657 		if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask)) {
658 			clear_childcpus(rdtgrp, cpu);
659 			break;
660 		}
661 	}
662 	clear_closid_rmid(cpu);
663 	mutex_unlock(&rdtgroup_mutex);
664 
665 	return 0;
666 }
667 
668 /*
669  * Choose a width for the resource name and resource data based on the
670  * resource that has widest name and cbm.
671  */
rdt_init_padding(void)672 static __init void rdt_init_padding(void)
673 {
674 	struct rdt_resource *r;
675 
676 	for_each_alloc_capable_rdt_resource(r) {
677 		if (r->data_width > max_data_width)
678 			max_data_width = r->data_width;
679 	}
680 }
681 
682 enum {
683 	RDT_FLAG_CMT,
684 	RDT_FLAG_MBM_TOTAL,
685 	RDT_FLAG_MBM_LOCAL,
686 	RDT_FLAG_L3_CAT,
687 	RDT_FLAG_L3_CDP,
688 	RDT_FLAG_L2_CAT,
689 	RDT_FLAG_L2_CDP,
690 	RDT_FLAG_MBA,
691 };
692 
693 #define RDT_OPT(idx, n, f)	\
694 [idx] = {			\
695 	.name = n,		\
696 	.flag = f		\
697 }
698 
699 struct rdt_options {
700 	char	*name;
701 	int	flag;
702 	bool	force_off, force_on;
703 };
704 
705 static struct rdt_options rdt_options[]  __initdata = {
706 	RDT_OPT(RDT_FLAG_CMT,	    "cmt",	X86_FEATURE_CQM_OCCUP_LLC),
707 	RDT_OPT(RDT_FLAG_MBM_TOTAL, "mbmtotal", X86_FEATURE_CQM_MBM_TOTAL),
708 	RDT_OPT(RDT_FLAG_MBM_LOCAL, "mbmlocal", X86_FEATURE_CQM_MBM_LOCAL),
709 	RDT_OPT(RDT_FLAG_L3_CAT,    "l3cat",	X86_FEATURE_CAT_L3),
710 	RDT_OPT(RDT_FLAG_L3_CDP,    "l3cdp",	X86_FEATURE_CDP_L3),
711 	RDT_OPT(RDT_FLAG_L2_CAT,    "l2cat",	X86_FEATURE_CAT_L2),
712 	RDT_OPT(RDT_FLAG_L2_CDP,    "l2cdp",	X86_FEATURE_CDP_L2),
713 	RDT_OPT(RDT_FLAG_MBA,	    "mba",	X86_FEATURE_MBA),
714 };
715 #define NUM_RDT_OPTIONS ARRAY_SIZE(rdt_options)
716 
set_rdt_options(char * str)717 static int __init set_rdt_options(char *str)
718 {
719 	struct rdt_options *o;
720 	bool force_off;
721 	char *tok;
722 
723 	if (*str == '=')
724 		str++;
725 	while ((tok = strsep(&str, ",")) != NULL) {
726 		force_off = *tok == '!';
727 		if (force_off)
728 			tok++;
729 		for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
730 			if (strcmp(tok, o->name) == 0) {
731 				if (force_off)
732 					o->force_off = true;
733 				else
734 					o->force_on = true;
735 				break;
736 			}
737 		}
738 	}
739 	return 1;
740 }
741 __setup("rdt", set_rdt_options);
742 
rdt_cpu_has(int flag)743 static bool __init rdt_cpu_has(int flag)
744 {
745 	bool ret = boot_cpu_has(flag);
746 	struct rdt_options *o;
747 
748 	if (!ret)
749 		return ret;
750 
751 	for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
752 		if (flag == o->flag) {
753 			if (o->force_off)
754 				ret = false;
755 			if (o->force_on)
756 				ret = true;
757 			break;
758 		}
759 	}
760 	return ret;
761 }
762 
get_mem_config(void)763 static __init bool get_mem_config(void)
764 {
765 	struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_MBA];
766 
767 	if (!rdt_cpu_has(X86_FEATURE_MBA))
768 		return false;
769 
770 	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
771 		return __get_mem_config_intel(&hw_res->r_resctrl);
772 	else if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
773 		return __rdt_get_mem_config_amd(&hw_res->r_resctrl);
774 
775 	return false;
776 }
777 
get_rdt_alloc_resources(void)778 static __init bool get_rdt_alloc_resources(void)
779 {
780 	struct rdt_resource *r;
781 	bool ret = false;
782 
783 	if (rdt_alloc_capable)
784 		return true;
785 
786 	if (!boot_cpu_has(X86_FEATURE_RDT_A))
787 		return false;
788 
789 	if (rdt_cpu_has(X86_FEATURE_CAT_L3)) {
790 		r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
791 		rdt_get_cache_alloc_cfg(1, r);
792 		if (rdt_cpu_has(X86_FEATURE_CDP_L3))
793 			rdt_get_cdp_l3_config();
794 		ret = true;
795 	}
796 	if (rdt_cpu_has(X86_FEATURE_CAT_L2)) {
797 		/* CPUID 0x10.2 fields are same format at 0x10.1 */
798 		r = &rdt_resources_all[RDT_RESOURCE_L2].r_resctrl;
799 		rdt_get_cache_alloc_cfg(2, r);
800 		if (rdt_cpu_has(X86_FEATURE_CDP_L2))
801 			rdt_get_cdp_l2_config();
802 		ret = true;
803 	}
804 
805 	if (get_mem_config())
806 		ret = true;
807 
808 	return ret;
809 }
810 
get_rdt_mon_resources(void)811 static __init bool get_rdt_mon_resources(void)
812 {
813 	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
814 
815 	if (rdt_cpu_has(X86_FEATURE_CQM_OCCUP_LLC))
816 		rdt_mon_features |= (1 << QOS_L3_OCCUP_EVENT_ID);
817 	if (rdt_cpu_has(X86_FEATURE_CQM_MBM_TOTAL))
818 		rdt_mon_features |= (1 << QOS_L3_MBM_TOTAL_EVENT_ID);
819 	if (rdt_cpu_has(X86_FEATURE_CQM_MBM_LOCAL))
820 		rdt_mon_features |= (1 << QOS_L3_MBM_LOCAL_EVENT_ID);
821 
822 	if (!rdt_mon_features)
823 		return false;
824 
825 	return !rdt_get_mon_l3_config(r);
826 }
827 
__check_quirks_intel(void)828 static __init void __check_quirks_intel(void)
829 {
830 	switch (boot_cpu_data.x86_model) {
831 	case INTEL_FAM6_HASWELL_X:
832 		if (!rdt_options[RDT_FLAG_L3_CAT].force_off)
833 			cache_alloc_hsw_probe();
834 		break;
835 	case INTEL_FAM6_SKYLAKE_X:
836 		if (boot_cpu_data.x86_stepping <= 4)
837 			set_rdt_options("!cmt,!mbmtotal,!mbmlocal,!l3cat");
838 		else
839 			set_rdt_options("!l3cat");
840 		fallthrough;
841 	case INTEL_FAM6_BROADWELL_X:
842 		intel_rdt_mbm_apply_quirk();
843 		break;
844 	}
845 }
846 
check_quirks(void)847 static __init void check_quirks(void)
848 {
849 	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
850 		__check_quirks_intel();
851 }
852 
get_rdt_resources(void)853 static __init bool get_rdt_resources(void)
854 {
855 	rdt_alloc_capable = get_rdt_alloc_resources();
856 	rdt_mon_capable = get_rdt_mon_resources();
857 
858 	return (rdt_mon_capable || rdt_alloc_capable);
859 }
860 
rdt_init_res_defs_intel(void)861 static __init void rdt_init_res_defs_intel(void)
862 {
863 	struct rdt_hw_resource *hw_res;
864 	struct rdt_resource *r;
865 
866 	for_each_rdt_resource(r) {
867 		hw_res = resctrl_to_arch_res(r);
868 
869 		if (r->rid == RDT_RESOURCE_L3 ||
870 		    r->rid == RDT_RESOURCE_L2) {
871 			r->cache.arch_has_sparse_bitmaps = false;
872 			r->cache.arch_has_empty_bitmaps = false;
873 			r->cache.arch_has_per_cpu_cfg = false;
874 			r->cache.min_cbm_bits = 1;
875 		} else if (r->rid == RDT_RESOURCE_MBA) {
876 			hw_res->msr_base = MSR_IA32_MBA_THRTL_BASE;
877 			hw_res->msr_update = mba_wrmsr_intel;
878 		}
879 	}
880 }
881 
rdt_init_res_defs_amd(void)882 static __init void rdt_init_res_defs_amd(void)
883 {
884 	struct rdt_hw_resource *hw_res;
885 	struct rdt_resource *r;
886 
887 	for_each_rdt_resource(r) {
888 		hw_res = resctrl_to_arch_res(r);
889 
890 		if (r->rid == RDT_RESOURCE_L3 ||
891 		    r->rid == RDT_RESOURCE_L2) {
892 			r->cache.arch_has_sparse_bitmaps = true;
893 			r->cache.arch_has_empty_bitmaps = true;
894 			r->cache.arch_has_per_cpu_cfg = true;
895 			r->cache.min_cbm_bits = 0;
896 		} else if (r->rid == RDT_RESOURCE_MBA) {
897 			hw_res->msr_base = MSR_IA32_MBA_BW_BASE;
898 			hw_res->msr_update = mba_wrmsr_amd;
899 		}
900 	}
901 }
902 
rdt_init_res_defs(void)903 static __init void rdt_init_res_defs(void)
904 {
905 	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
906 		rdt_init_res_defs_intel();
907 	else if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
908 		rdt_init_res_defs_amd();
909 }
910 
911 static enum cpuhp_state rdt_online;
912 
913 /* Runs once on the BSP during boot. */
resctrl_cpu_detect(struct cpuinfo_x86 * c)914 void resctrl_cpu_detect(struct cpuinfo_x86 *c)
915 {
916 	if (!cpu_has(c, X86_FEATURE_CQM_LLC)) {
917 		c->x86_cache_max_rmid  = -1;
918 		c->x86_cache_occ_scale = -1;
919 		c->x86_cache_mbm_width_offset = -1;
920 		return;
921 	}
922 
923 	/* will be overridden if occupancy monitoring exists */
924 	c->x86_cache_max_rmid = cpuid_ebx(0xf);
925 
926 	if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC) ||
927 	    cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL) ||
928 	    cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)) {
929 		u32 eax, ebx, ecx, edx;
930 
931 		/* QoS sub-leaf, EAX=0Fh, ECX=1 */
932 		cpuid_count(0xf, 1, &eax, &ebx, &ecx, &edx);
933 
934 		c->x86_cache_max_rmid  = ecx;
935 		c->x86_cache_occ_scale = ebx;
936 		c->x86_cache_mbm_width_offset = eax & 0xff;
937 
938 		if (c->x86_vendor == X86_VENDOR_AMD && !c->x86_cache_mbm_width_offset)
939 			c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_AMD;
940 	}
941 }
942 
resctrl_late_init(void)943 static int __init resctrl_late_init(void)
944 {
945 	struct rdt_resource *r;
946 	int state, ret;
947 
948 	/*
949 	 * Initialize functions(or definitions) that are different
950 	 * between vendors here.
951 	 */
952 	rdt_init_res_defs();
953 
954 	check_quirks();
955 
956 	if (!get_rdt_resources())
957 		return -ENODEV;
958 
959 	rdt_init_padding();
960 
961 	state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
962 				  "x86/resctrl/cat:online:",
963 				  resctrl_online_cpu, resctrl_offline_cpu);
964 	if (state < 0)
965 		return state;
966 
967 	ret = rdtgroup_init();
968 	if (ret) {
969 		cpuhp_remove_state(state);
970 		return ret;
971 	}
972 	rdt_online = state;
973 
974 	for_each_alloc_capable_rdt_resource(r)
975 		pr_info("%s allocation detected\n", r->name);
976 
977 	for_each_mon_capable_rdt_resource(r)
978 		pr_info("%s monitoring detected\n", r->name);
979 
980 	return 0;
981 }
982 
983 late_initcall(resctrl_late_init);
984 
resctrl_exit(void)985 static void __exit resctrl_exit(void)
986 {
987 	cpuhp_remove_state(rdt_online);
988 	rdtgroup_exit();
989 }
990 
991 __exitcall(resctrl_exit);
992