1 // SPDX-License-Identifier: GPL-2.0-only
3 * Resource Director Technology(RDT)
4 * - Cache Allocation code.
6 * Copyright (C) 2016 Intel Corporation
13 * More information about RDT be found in the Intel (R) x86 Architecture
14 * Software Developer Manual June 2016, volume 3, section 17.17.
17 #define pr_fmt(fmt) "resctrl: " fmt
19 #include <linux/cpu.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/cacheinfo.h>
23 #include <linux/cpuhotplug.h>
25 #include <asm/intel-family.h>
26 #include <asm/resctrl.h>
30 * rdt_domain structures are kfree()d when their last CPU goes offline,
31 * and allocated when the first CPU in a new domain comes online.
32 * The rdt_resource's domain list is updated when this happens. Readers of
33 * the domain list must either take cpus_read_lock(), or rely on an RCU
34 * read-side critical section, to avoid observing concurrent modification.
35 * All writers take this mutex:
37 static DEFINE_MUTEX(domain_list_lock);
40 * The cached resctrl_pqr_state is strictly per CPU and can never be
41 * updated from a remote CPU. Functions which modify the state
42 * are called with interrupts disabled and no preemption, which
43 * is sufficient for the protection.
45 DEFINE_PER_CPU(struct resctrl_pqr_state, pqr_state);
48 * Used to store the max resource name width and max resource data width
49 * to display the schemata in a tabular format
51 int max_name_width, max_data_width;
54 * Global boolean for rdt_alloc which is true if any
55 * resource allocation is enabled.
57 bool rdt_alloc_capable;
60 mba_wrmsr_intel(struct rdt_domain *d, struct msr_param *m,
61 struct rdt_resource *r);
63 cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r);
65 mba_wrmsr_amd(struct rdt_domain *d, struct msr_param *m,
66 struct rdt_resource *r);
68 #define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].r_resctrl.domains)
70 struct rdt_hw_resource rdt_resources_all[] = {
74 .rid = RDT_RESOURCE_L3,
77 .domains = domain_init(RDT_RESOURCE_L3),
78 .parse_ctrlval = parse_cbm,
79 .format_str = "%d=%0*x",
80 .fflags = RFTYPE_RES_CACHE,
82 .msr_base = MSR_IA32_L3_CBM_BASE,
83 .msr_update = cat_wrmsr,
88 .rid = RDT_RESOURCE_L2,
91 .domains = domain_init(RDT_RESOURCE_L2),
92 .parse_ctrlval = parse_cbm,
93 .format_str = "%d=%0*x",
94 .fflags = RFTYPE_RES_CACHE,
96 .msr_base = MSR_IA32_L2_CBM_BASE,
97 .msr_update = cat_wrmsr,
102 .rid = RDT_RESOURCE_MBA,
105 .domains = domain_init(RDT_RESOURCE_MBA),
106 .parse_ctrlval = parse_bw,
107 .format_str = "%d=%*u",
108 .fflags = RFTYPE_RES_MB,
111 [RDT_RESOURCE_SMBA] =
114 .rid = RDT_RESOURCE_SMBA,
117 .domains = domain_init(RDT_RESOURCE_SMBA),
118 .parse_ctrlval = parse_bw,
119 .format_str = "%d=%*u",
120 .fflags = RFTYPE_RES_MB,
126 * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
127 * as they do not have CPUID enumeration support for Cache allocation.
128 * The check for Vendor/Family/Model is not enough to guarantee that
129 * the MSRs won't #GP fault because only the following SKUs support
131 * Intel(R) Xeon(R) CPU E5-2658 v3 @ 2.20GHz
132 * Intel(R) Xeon(R) CPU E5-2648L v3 @ 1.80GHz
133 * Intel(R) Xeon(R) CPU E5-2628L v3 @ 2.00GHz
134 * Intel(R) Xeon(R) CPU E5-2618L v3 @ 2.30GHz
135 * Intel(R) Xeon(R) CPU E5-2608L v3 @ 2.00GHz
136 * Intel(R) Xeon(R) CPU E5-2658A v3 @ 2.20GHz
138 * Probe by trying to write the first of the L3 cache mask registers
139 * and checking that the bits stick. Max CLOSids is always 4 and max cbm length
140 * is always 20 on hsw server parts. The minimum cache bitmask length
141 * allowed for HSW server is always 2 bits. Hardcode all of them.
143 static inline void cache_alloc_hsw_probe(void)
145 struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_L3];
146 struct rdt_resource *r = &hw_res->r_resctrl;
147 u64 max_cbm = BIT_ULL_MASK(20) - 1, l3_cbm_0;
149 if (wrmsrl_safe(MSR_IA32_L3_CBM_BASE, max_cbm))
152 rdmsrl(MSR_IA32_L3_CBM_BASE, l3_cbm_0);
154 /* If all the bits were set in MSR, return success */
155 if (l3_cbm_0 != max_cbm)
158 hw_res->num_closid = 4;
159 r->default_ctrl = max_cbm;
160 r->cache.cbm_len = 20;
161 r->cache.shareable_bits = 0xc0000;
162 r->cache.min_cbm_bits = 2;
163 r->cache.arch_has_sparse_bitmasks = false;
164 r->alloc_capable = true;
166 rdt_alloc_capable = true;
169 bool is_mba_sc(struct rdt_resource *r)
172 return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.mba_sc;
175 * The software controller support is only applicable to MBA resource.
176 * Make sure to check for resource type.
178 if (r->rid != RDT_RESOURCE_MBA)
181 return r->membw.mba_sc;
185 * rdt_get_mb_table() - get a mapping of bandwidth(b/w) percentage values
186 * exposed to user interface and the h/w understandable delay values.
188 * The non-linear delay values have the granularity of power of two
189 * and also the h/w does not guarantee a curve for configured delay
190 * values vs. actual b/w enforced.
191 * Hence we need a mapping that is pre calibrated so the user can
192 * express the memory b/w as a percentage value.
194 static inline bool rdt_get_mb_table(struct rdt_resource *r)
197 * There are no Intel SKUs as of now to support non-linear delay.
199 pr_info("MBA b/w map not implemented for cpu:%d, model:%d",
200 boot_cpu_data.x86, boot_cpu_data.x86_model);
205 static bool __get_mem_config_intel(struct rdt_resource *r)
207 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
208 union cpuid_0x10_3_eax eax;
209 union cpuid_0x10_x_edx edx;
210 u32 ebx, ecx, max_delay;
212 cpuid_count(0x00000010, 3, &eax.full, &ebx, &ecx, &edx.full);
213 hw_res->num_closid = edx.split.cos_max + 1;
214 max_delay = eax.split.max_delay + 1;
215 r->default_ctrl = MAX_MBA_BW;
216 r->membw.arch_needs_linear = true;
217 if (ecx & MBA_IS_LINEAR) {
218 r->membw.delay_linear = true;
219 r->membw.min_bw = MAX_MBA_BW - max_delay;
220 r->membw.bw_gran = MAX_MBA_BW - max_delay;
222 if (!rdt_get_mb_table(r))
224 r->membw.arch_needs_linear = false;
228 if (boot_cpu_has(X86_FEATURE_PER_THREAD_MBA))
229 r->membw.throttle_mode = THREAD_THROTTLE_PER_THREAD;
231 r->membw.throttle_mode = THREAD_THROTTLE_MAX;
232 thread_throttle_mode_init();
234 r->alloc_capable = true;
239 static bool __rdt_get_mem_config_amd(struct rdt_resource *r)
241 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
242 u32 eax, ebx, ecx, edx, subleaf;
245 * Query CPUID_Fn80000020_EDX_x01 for MBA and
246 * CPUID_Fn80000020_EDX_x02 for SMBA
248 subleaf = (r->rid == RDT_RESOURCE_SMBA) ? 2 : 1;
250 cpuid_count(0x80000020, subleaf, &eax, &ebx, &ecx, &edx);
251 hw_res->num_closid = edx + 1;
252 r->default_ctrl = 1 << eax;
254 /* AMD does not use delay */
255 r->membw.delay_linear = false;
256 r->membw.arch_needs_linear = false;
259 * AMD does not use memory delay throttle model to control
260 * the allocation like Intel does.
262 r->membw.throttle_mode = THREAD_THROTTLE_UNDEFINED;
264 r->membw.bw_gran = 1;
265 /* Max value is 2048, Data width should be 4 in decimal */
268 r->alloc_capable = true;
273 static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r)
275 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
276 union cpuid_0x10_1_eax eax;
277 union cpuid_0x10_x_ecx ecx;
278 union cpuid_0x10_x_edx edx;
281 cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx.full, &edx.full);
282 hw_res->num_closid = edx.split.cos_max + 1;
283 r->cache.cbm_len = eax.split.cbm_len + 1;
284 r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
285 r->cache.shareable_bits = ebx & r->default_ctrl;
286 r->data_width = (r->cache.cbm_len + 3) / 4;
287 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
288 r->cache.arch_has_sparse_bitmasks = ecx.split.noncont;
289 r->alloc_capable = true;
292 static void rdt_get_cdp_config(int level)
295 * By default, CDP is disabled. CDP can be enabled by mount parameter
296 * "cdp" during resctrl file system mount time.
298 rdt_resources_all[level].cdp_enabled = false;
299 rdt_resources_all[level].r_resctrl.cdp_capable = true;
302 static void rdt_get_cdp_l3_config(void)
304 rdt_get_cdp_config(RDT_RESOURCE_L3);
307 static void rdt_get_cdp_l2_config(void)
309 rdt_get_cdp_config(RDT_RESOURCE_L2);
313 mba_wrmsr_amd(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
316 struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
317 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
319 for (i = m->low; i < m->high; i++)
320 wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
324 * Map the memory b/w percentage value to delay values
325 * that can be written to QOS_MSRs.
326 * There are currently no SKUs which support non linear delay values.
328 static u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)
330 if (r->membw.delay_linear)
331 return MAX_MBA_BW - bw;
333 pr_warn_once("Non Linear delay-bw map not supported but queried\n");
334 return r->default_ctrl;
338 mba_wrmsr_intel(struct rdt_domain *d, struct msr_param *m,
339 struct rdt_resource *r)
342 struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
343 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
345 /* Write the delay values for mba. */
346 for (i = m->low; i < m->high; i++)
347 wrmsrl(hw_res->msr_base + i, delay_bw_map(hw_dom->ctrl_val[i], r));
351 cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
354 struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
355 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
357 for (i = m->low; i < m->high; i++)
358 wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
361 struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r)
363 struct rdt_domain *d;
365 list_for_each_entry(d, &r->domains, list) {
366 /* Find the domain that contains this CPU */
367 if (cpumask_test_cpu(cpu, &d->cpu_mask))
374 u32 resctrl_arch_get_num_closid(struct rdt_resource *r)
376 return resctrl_to_arch_res(r)->num_closid;
379 void rdt_ctrl_update(void *arg)
381 struct msr_param *m = arg;
382 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(m->res);
383 struct rdt_resource *r = m->res;
384 int cpu = smp_processor_id();
385 struct rdt_domain *d;
387 d = get_domain_from_cpu(cpu, r);
389 hw_res->msr_update(d, m, r);
392 pr_warn_once("cpu %d not found in any domain for resource %s\n",
397 * rdt_find_domain - Find a domain in a resource that matches input resource id
399 * Search resource r's domain list to find the resource id. If the resource
400 * id is found in a domain, return the domain. Otherwise, if requested by
401 * caller, return the first domain whose id is bigger than the input id.
402 * The domain list is sorted by id in ascending order.
404 struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
405 struct list_head **pos)
407 struct rdt_domain *d;
411 return ERR_PTR(-ENODEV);
413 list_for_each(l, &r->domains) {
414 d = list_entry(l, struct rdt_domain, list);
415 /* When id is found, return its domain. */
418 /* Stop searching when finding id's position in sorted list. */
429 static void setup_default_ctrlval(struct rdt_resource *r, u32 *dc)
431 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
435 * Initialize the Control MSRs to having no control.
436 * For Cache Allocation: Set all bits in cbm
437 * For Memory Allocation: Set b/w requested to 100%
439 for (i = 0; i < hw_res->num_closid; i++, dc++)
440 *dc = r->default_ctrl;
443 static void domain_free(struct rdt_hw_domain *hw_dom)
445 kfree(hw_dom->arch_mbm_total);
446 kfree(hw_dom->arch_mbm_local);
447 kfree(hw_dom->ctrl_val);
451 static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
453 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
454 struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
458 dc = kmalloc_array(hw_res->num_closid, sizeof(*hw_dom->ctrl_val),
463 hw_dom->ctrl_val = dc;
464 setup_default_ctrlval(r, dc);
467 m.high = hw_res->num_closid;
468 hw_res->msr_update(d, &m, r);
473 * arch_domain_mbm_alloc() - Allocate arch private storage for the MBM counters
474 * @num_rmid: The size of the MBM counter array
475 * @hw_dom: The domain that owns the allocated arrays
477 static int arch_domain_mbm_alloc(u32 num_rmid, struct rdt_hw_domain *hw_dom)
481 if (is_mbm_total_enabled()) {
482 tsize = sizeof(*hw_dom->arch_mbm_total);
483 hw_dom->arch_mbm_total = kcalloc(num_rmid, tsize, GFP_KERNEL);
484 if (!hw_dom->arch_mbm_total)
487 if (is_mbm_local_enabled()) {
488 tsize = sizeof(*hw_dom->arch_mbm_local);
489 hw_dom->arch_mbm_local = kcalloc(num_rmid, tsize, GFP_KERNEL);
490 if (!hw_dom->arch_mbm_local) {
491 kfree(hw_dom->arch_mbm_total);
492 hw_dom->arch_mbm_total = NULL;
501 * domain_add_cpu - Add a cpu to a resource's domain list.
503 * If an existing domain in the resource r's domain list matches the cpu's
504 * resource id, add the cpu in the domain.
506 * Otherwise, a new domain is allocated and inserted into the right position
507 * in the domain list sorted by id in ascending order.
509 * The order in the domain list is visible to users when we print entries
510 * in the schemata file and schemata input is validated to have the same order
513 static void domain_add_cpu(int cpu, struct rdt_resource *r)
515 int id = get_cpu_cacheinfo_id(cpu, r->cache_level);
516 struct list_head *add_pos = NULL;
517 struct rdt_hw_domain *hw_dom;
518 struct rdt_domain *d;
521 lockdep_assert_held(&domain_list_lock);
523 d = rdt_find_domain(r, id, &add_pos);
525 pr_warn("Couldn't find cache id for CPU %d\n", cpu);
530 cpumask_set_cpu(cpu, &d->cpu_mask);
531 if (r->cache.arch_has_per_cpu_cfg)
532 rdt_domain_reconfigure_cdp(r);
536 hw_dom = kzalloc_node(sizeof(*hw_dom), GFP_KERNEL, cpu_to_node(cpu));
540 d = &hw_dom->d_resctrl;
542 cpumask_set_cpu(cpu, &d->cpu_mask);
544 rdt_domain_reconfigure_cdp(r);
546 if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
551 if (r->mon_capable && arch_domain_mbm_alloc(r->num_rmid, hw_dom)) {
556 list_add_tail_rcu(&d->list, add_pos);
558 err = resctrl_online_domain(r, d);
560 list_del_rcu(&d->list);
566 static void domain_remove_cpu(int cpu, struct rdt_resource *r)
568 int id = get_cpu_cacheinfo_id(cpu, r->cache_level);
569 struct rdt_hw_domain *hw_dom;
570 struct rdt_domain *d;
572 lockdep_assert_held(&domain_list_lock);
574 d = rdt_find_domain(r, id, NULL);
575 if (IS_ERR_OR_NULL(d)) {
576 pr_warn("Couldn't find cache id for CPU %d\n", cpu);
579 hw_dom = resctrl_to_arch_dom(d);
581 cpumask_clear_cpu(cpu, &d->cpu_mask);
582 if (cpumask_empty(&d->cpu_mask)) {
583 resctrl_offline_domain(r, d);
584 list_del_rcu(&d->list);
588 * rdt_domain "d" is going to be freed below, so clear
589 * its pointer from pseudo_lock_region struct.
599 static void clear_closid_rmid(int cpu)
601 struct resctrl_pqr_state *state = this_cpu_ptr(&pqr_state);
603 state->default_closid = RESCTRL_RESERVED_CLOSID;
604 state->default_rmid = RESCTRL_RESERVED_RMID;
605 state->cur_closid = RESCTRL_RESERVED_CLOSID;
606 state->cur_rmid = RESCTRL_RESERVED_RMID;
607 wrmsr(MSR_IA32_PQR_ASSOC, RESCTRL_RESERVED_RMID,
608 RESCTRL_RESERVED_CLOSID);
611 static int resctrl_arch_online_cpu(unsigned int cpu)
613 struct rdt_resource *r;
615 mutex_lock(&domain_list_lock);
616 for_each_capable_rdt_resource(r)
617 domain_add_cpu(cpu, r);
618 mutex_unlock(&domain_list_lock);
620 clear_closid_rmid(cpu);
621 resctrl_online_cpu(cpu);
626 static int resctrl_arch_offline_cpu(unsigned int cpu)
628 struct rdt_resource *r;
630 resctrl_offline_cpu(cpu);
632 mutex_lock(&domain_list_lock);
633 for_each_capable_rdt_resource(r)
634 domain_remove_cpu(cpu, r);
635 mutex_unlock(&domain_list_lock);
637 clear_closid_rmid(cpu);
643 * Choose a width for the resource name and resource data based on the
644 * resource that has widest name and cbm.
646 static __init void rdt_init_padding(void)
648 struct rdt_resource *r;
650 for_each_alloc_capable_rdt_resource(r) {
651 if (r->data_width > max_data_width)
652 max_data_width = r->data_width;
669 #define RDT_OPT(idx, n, f) \
678 bool force_off, force_on;
681 static struct rdt_options rdt_options[] __initdata = {
682 RDT_OPT(RDT_FLAG_CMT, "cmt", X86_FEATURE_CQM_OCCUP_LLC),
683 RDT_OPT(RDT_FLAG_MBM_TOTAL, "mbmtotal", X86_FEATURE_CQM_MBM_TOTAL),
684 RDT_OPT(RDT_FLAG_MBM_LOCAL, "mbmlocal", X86_FEATURE_CQM_MBM_LOCAL),
685 RDT_OPT(RDT_FLAG_L3_CAT, "l3cat", X86_FEATURE_CAT_L3),
686 RDT_OPT(RDT_FLAG_L3_CDP, "l3cdp", X86_FEATURE_CDP_L3),
687 RDT_OPT(RDT_FLAG_L2_CAT, "l2cat", X86_FEATURE_CAT_L2),
688 RDT_OPT(RDT_FLAG_L2_CDP, "l2cdp", X86_FEATURE_CDP_L2),
689 RDT_OPT(RDT_FLAG_MBA, "mba", X86_FEATURE_MBA),
690 RDT_OPT(RDT_FLAG_SMBA, "smba", X86_FEATURE_SMBA),
691 RDT_OPT(RDT_FLAG_BMEC, "bmec", X86_FEATURE_BMEC),
693 #define NUM_RDT_OPTIONS ARRAY_SIZE(rdt_options)
695 static int __init set_rdt_options(char *str)
697 struct rdt_options *o;
703 while ((tok = strsep(&str, ",")) != NULL) {
704 force_off = *tok == '!';
707 for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
708 if (strcmp(tok, o->name) == 0) {
719 __setup("rdt", set_rdt_options);
721 bool __init rdt_cpu_has(int flag)
723 bool ret = boot_cpu_has(flag);
724 struct rdt_options *o;
729 for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
730 if (flag == o->flag) {
741 static __init bool get_mem_config(void)
743 struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_MBA];
745 if (!rdt_cpu_has(X86_FEATURE_MBA))
748 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
749 return __get_mem_config_intel(&hw_res->r_resctrl);
750 else if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
751 return __rdt_get_mem_config_amd(&hw_res->r_resctrl);
756 static __init bool get_slow_mem_config(void)
758 struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_SMBA];
760 if (!rdt_cpu_has(X86_FEATURE_SMBA))
763 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
764 return __rdt_get_mem_config_amd(&hw_res->r_resctrl);
769 static __init bool get_rdt_alloc_resources(void)
771 struct rdt_resource *r;
774 if (rdt_alloc_capable)
777 if (!boot_cpu_has(X86_FEATURE_RDT_A))
780 if (rdt_cpu_has(X86_FEATURE_CAT_L3)) {
781 r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
782 rdt_get_cache_alloc_cfg(1, r);
783 if (rdt_cpu_has(X86_FEATURE_CDP_L3))
784 rdt_get_cdp_l3_config();
787 if (rdt_cpu_has(X86_FEATURE_CAT_L2)) {
788 /* CPUID 0x10.2 fields are same format at 0x10.1 */
789 r = &rdt_resources_all[RDT_RESOURCE_L2].r_resctrl;
790 rdt_get_cache_alloc_cfg(2, r);
791 if (rdt_cpu_has(X86_FEATURE_CDP_L2))
792 rdt_get_cdp_l2_config();
796 if (get_mem_config())
799 if (get_slow_mem_config())
805 static __init bool get_rdt_mon_resources(void)
807 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
809 if (rdt_cpu_has(X86_FEATURE_CQM_OCCUP_LLC))
810 rdt_mon_features |= (1 << QOS_L3_OCCUP_EVENT_ID);
811 if (rdt_cpu_has(X86_FEATURE_CQM_MBM_TOTAL))
812 rdt_mon_features |= (1 << QOS_L3_MBM_TOTAL_EVENT_ID);
813 if (rdt_cpu_has(X86_FEATURE_CQM_MBM_LOCAL))
814 rdt_mon_features |= (1 << QOS_L3_MBM_LOCAL_EVENT_ID);
816 if (!rdt_mon_features)
819 return !rdt_get_mon_l3_config(r);
822 static __init void __check_quirks_intel(void)
824 switch (boot_cpu_data.x86_model) {
825 case INTEL_FAM6_HASWELL_X:
826 if (!rdt_options[RDT_FLAG_L3_CAT].force_off)
827 cache_alloc_hsw_probe();
829 case INTEL_FAM6_SKYLAKE_X:
830 if (boot_cpu_data.x86_stepping <= 4)
831 set_rdt_options("!cmt,!mbmtotal,!mbmlocal,!l3cat");
833 set_rdt_options("!l3cat");
835 case INTEL_FAM6_BROADWELL_X:
836 intel_rdt_mbm_apply_quirk();
841 static __init void check_quirks(void)
843 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
844 __check_quirks_intel();
847 static __init bool get_rdt_resources(void)
849 rdt_alloc_capable = get_rdt_alloc_resources();
850 rdt_mon_capable = get_rdt_mon_resources();
852 return (rdt_mon_capable || rdt_alloc_capable);
855 static __init void rdt_init_res_defs_intel(void)
857 struct rdt_hw_resource *hw_res;
858 struct rdt_resource *r;
860 for_each_rdt_resource(r) {
861 hw_res = resctrl_to_arch_res(r);
863 if (r->rid == RDT_RESOURCE_L3 ||
864 r->rid == RDT_RESOURCE_L2) {
865 r->cache.arch_has_per_cpu_cfg = false;
866 r->cache.min_cbm_bits = 1;
867 } else if (r->rid == RDT_RESOURCE_MBA) {
868 hw_res->msr_base = MSR_IA32_MBA_THRTL_BASE;
869 hw_res->msr_update = mba_wrmsr_intel;
874 static __init void rdt_init_res_defs_amd(void)
876 struct rdt_hw_resource *hw_res;
877 struct rdt_resource *r;
879 for_each_rdt_resource(r) {
880 hw_res = resctrl_to_arch_res(r);
882 if (r->rid == RDT_RESOURCE_L3 ||
883 r->rid == RDT_RESOURCE_L2) {
884 r->cache.arch_has_sparse_bitmasks = true;
885 r->cache.arch_has_per_cpu_cfg = true;
886 r->cache.min_cbm_bits = 0;
887 } else if (r->rid == RDT_RESOURCE_MBA) {
888 hw_res->msr_base = MSR_IA32_MBA_BW_BASE;
889 hw_res->msr_update = mba_wrmsr_amd;
890 } else if (r->rid == RDT_RESOURCE_SMBA) {
891 hw_res->msr_base = MSR_IA32_SMBA_BW_BASE;
892 hw_res->msr_update = mba_wrmsr_amd;
897 static __init void rdt_init_res_defs(void)
899 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
900 rdt_init_res_defs_intel();
901 else if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
902 rdt_init_res_defs_amd();
905 static enum cpuhp_state rdt_online;
907 /* Runs once on the BSP during boot. */
908 void resctrl_cpu_detect(struct cpuinfo_x86 *c)
910 if (!cpu_has(c, X86_FEATURE_CQM_LLC)) {
911 c->x86_cache_max_rmid = -1;
912 c->x86_cache_occ_scale = -1;
913 c->x86_cache_mbm_width_offset = -1;
917 /* will be overridden if occupancy monitoring exists */
918 c->x86_cache_max_rmid = cpuid_ebx(0xf);
920 if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC) ||
921 cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL) ||
922 cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)) {
923 u32 eax, ebx, ecx, edx;
925 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
926 cpuid_count(0xf, 1, &eax, &ebx, &ecx, &edx);
928 c->x86_cache_max_rmid = ecx;
929 c->x86_cache_occ_scale = ebx;
930 c->x86_cache_mbm_width_offset = eax & 0xff;
932 if (c->x86_vendor == X86_VENDOR_AMD && !c->x86_cache_mbm_width_offset)
933 c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_AMD;
937 static int __init resctrl_late_init(void)
939 struct rdt_resource *r;
943 * Initialize functions(or definitions) that are different
944 * between vendors here.
950 if (!get_rdt_resources())
955 state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
956 "x86/resctrl/cat:online:",
957 resctrl_arch_online_cpu,
958 resctrl_arch_offline_cpu);
962 ret = rdtgroup_init();
964 cpuhp_remove_state(state);
969 for_each_alloc_capable_rdt_resource(r)
970 pr_info("%s allocation detected\n", r->name);
972 for_each_mon_capable_rdt_resource(r)
973 pr_info("%s monitoring detected\n", r->name);
978 late_initcall(resctrl_late_init);
980 static void __exit resctrl_exit(void)
982 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
984 cpuhp_remove_state(rdt_online);
989 rdt_put_mon_l3_config();
992 __exitcall(resctrl_exit);