1 // SPDX-License-Identifier: GPL-2.0-only
3 * Resource Director Technology(RDT)
4 * - Cache Allocation code.
6 * Copyright (C) 2016 Intel Corporation
9 * Fenghua Yu <fenghua.yu@intel.com>
10 * Tony Luck <tony.luck@intel.com>
11 * Vikas Shivappa <vikas.shivappa@intel.com>
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/slab.h>
20 #include <linux/err.h>
21 #include <linux/cacheinfo.h>
22 #include <linux/cpuhotplug.h>
24 #include <asm/intel-family.h>
25 #include <asm/resctrl.h>
28 /* Mutex to protect rdtgroup access. */
29 DEFINE_MUTEX(rdtgroup_mutex);
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.
37 DEFINE_PER_CPU(struct resctrl_pqr_state, pqr_state);
40 * Used to store the max resource name width and max resource data width
41 * to display the schemata in a tabular format
43 int max_name_width, max_data_width;
46 * Global boolean for rdt_alloc which is true if any
47 * resource allocation is enabled.
49 bool rdt_alloc_capable;
52 mba_wrmsr_intel(struct rdt_domain *d, struct msr_param *m,
53 struct rdt_resource *r);
55 cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r);
57 mba_wrmsr_amd(struct rdt_domain *d, struct msr_param *m,
58 struct rdt_resource *r);
60 #define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].r_resctrl.domains)
62 struct rdt_hw_resource rdt_resources_all[] = {
66 .rid = RDT_RESOURCE_L3,
74 .domains = domain_init(RDT_RESOURCE_L3),
75 .parse_ctrlval = parse_cbm,
76 .format_str = "%d=%0*x",
77 .fflags = RFTYPE_RES_CACHE,
79 .msr_base = MSR_IA32_L3_CBM_BASE,
80 .msr_update = cat_wrmsr,
82 [RDT_RESOURCE_L3DATA] =
85 .rid = RDT_RESOURCE_L3DATA,
93 .domains = domain_init(RDT_RESOURCE_L3DATA),
94 .parse_ctrlval = parse_cbm,
95 .format_str = "%d=%0*x",
96 .fflags = RFTYPE_RES_CACHE,
98 .msr_base = MSR_IA32_L3_CBM_BASE,
99 .msr_update = cat_wrmsr,
101 [RDT_RESOURCE_L3CODE] =
104 .rid = RDT_RESOURCE_L3CODE,
112 .domains = domain_init(RDT_RESOURCE_L3CODE),
113 .parse_ctrlval = parse_cbm,
114 .format_str = "%d=%0*x",
115 .fflags = RFTYPE_RES_CACHE,
117 .msr_base = MSR_IA32_L3_CBM_BASE,
118 .msr_update = cat_wrmsr,
123 .rid = RDT_RESOURCE_L2,
131 .domains = domain_init(RDT_RESOURCE_L2),
132 .parse_ctrlval = parse_cbm,
133 .format_str = "%d=%0*x",
134 .fflags = RFTYPE_RES_CACHE,
136 .msr_base = MSR_IA32_L2_CBM_BASE,
137 .msr_update = cat_wrmsr,
139 [RDT_RESOURCE_L2DATA] =
142 .rid = RDT_RESOURCE_L2DATA,
150 .domains = domain_init(RDT_RESOURCE_L2DATA),
151 .parse_ctrlval = parse_cbm,
152 .format_str = "%d=%0*x",
153 .fflags = RFTYPE_RES_CACHE,
155 .msr_base = MSR_IA32_L2_CBM_BASE,
156 .msr_update = cat_wrmsr,
158 [RDT_RESOURCE_L2CODE] =
161 .rid = RDT_RESOURCE_L2CODE,
169 .domains = domain_init(RDT_RESOURCE_L2CODE),
170 .parse_ctrlval = parse_cbm,
171 .format_str = "%d=%0*x",
172 .fflags = RFTYPE_RES_CACHE,
174 .msr_base = MSR_IA32_L2_CBM_BASE,
175 .msr_update = cat_wrmsr,
180 .rid = RDT_RESOURCE_MBA,
183 .domains = domain_init(RDT_RESOURCE_MBA),
184 .parse_ctrlval = parse_bw,
185 .format_str = "%d=%*u",
186 .fflags = RFTYPE_RES_MB,
191 static unsigned int cbm_idx(struct rdt_resource *r, unsigned int closid)
193 return closid * r->cache.cbm_idx_mult + r->cache.cbm_idx_offset;
197 * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
198 * as they do not have CPUID enumeration support for Cache allocation.
199 * The check for Vendor/Family/Model is not enough to guarantee that
200 * the MSRs won't #GP fault because only the following SKUs support
202 * Intel(R) Xeon(R) CPU E5-2658 v3 @ 2.20GHz
203 * Intel(R) Xeon(R) CPU E5-2648L v3 @ 1.80GHz
204 * Intel(R) Xeon(R) CPU E5-2628L v3 @ 2.00GHz
205 * Intel(R) Xeon(R) CPU E5-2618L v3 @ 2.30GHz
206 * Intel(R) Xeon(R) CPU E5-2608L v3 @ 2.00GHz
207 * Intel(R) Xeon(R) CPU E5-2658A v3 @ 2.20GHz
209 * Probe by trying to write the first of the L3 cache mask registers
210 * and checking that the bits stick. Max CLOSids is always 4 and max cbm length
211 * is always 20 on hsw server parts. The minimum cache bitmask length
212 * allowed for HSW server is always 2 bits. Hardcode all of them.
214 static inline void cache_alloc_hsw_probe(void)
216 struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_L3];
217 struct rdt_resource *r = &hw_res->r_resctrl;
218 u32 l, h, max_cbm = BIT_MASK(20) - 1;
220 if (wrmsr_safe(MSR_IA32_L3_CBM_BASE, max_cbm, 0))
223 rdmsr(MSR_IA32_L3_CBM_BASE, l, h);
225 /* If all the bits were set in MSR, return success */
229 hw_res->num_closid = 4;
230 r->default_ctrl = max_cbm;
231 r->cache.cbm_len = 20;
232 r->cache.shareable_bits = 0xc0000;
233 r->cache.min_cbm_bits = 2;
234 r->alloc_capable = true;
235 r->alloc_enabled = true;
237 rdt_alloc_capable = true;
240 bool is_mba_sc(struct rdt_resource *r)
243 return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.mba_sc;
245 return r->membw.mba_sc;
249 * rdt_get_mb_table() - get a mapping of bandwidth(b/w) percentage values
250 * exposed to user interface and the h/w understandable delay values.
252 * The non-linear delay values have the granularity of power of two
253 * and also the h/w does not guarantee a curve for configured delay
254 * values vs. actual b/w enforced.
255 * Hence we need a mapping that is pre calibrated so the user can
256 * express the memory b/w as a percentage value.
258 static inline bool rdt_get_mb_table(struct rdt_resource *r)
261 * There are no Intel SKUs as of now to support non-linear delay.
263 pr_info("MBA b/w map not implemented for cpu:%d, model:%d",
264 boot_cpu_data.x86, boot_cpu_data.x86_model);
269 static bool __get_mem_config_intel(struct rdt_resource *r)
271 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
272 union cpuid_0x10_3_eax eax;
273 union cpuid_0x10_x_edx edx;
274 u32 ebx, ecx, max_delay;
276 cpuid_count(0x00000010, 3, &eax.full, &ebx, &ecx, &edx.full);
277 hw_res->num_closid = edx.split.cos_max + 1;
278 max_delay = eax.split.max_delay + 1;
279 r->default_ctrl = MAX_MBA_BW;
280 r->membw.arch_needs_linear = true;
281 if (ecx & MBA_IS_LINEAR) {
282 r->membw.delay_linear = true;
283 r->membw.min_bw = MAX_MBA_BW - max_delay;
284 r->membw.bw_gran = MAX_MBA_BW - max_delay;
286 if (!rdt_get_mb_table(r))
288 r->membw.arch_needs_linear = false;
292 if (boot_cpu_has(X86_FEATURE_PER_THREAD_MBA))
293 r->membw.throttle_mode = THREAD_THROTTLE_PER_THREAD;
295 r->membw.throttle_mode = THREAD_THROTTLE_MAX;
296 thread_throttle_mode_init();
298 r->alloc_capable = true;
299 r->alloc_enabled = true;
304 static bool __rdt_get_mem_config_amd(struct rdt_resource *r)
306 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
307 union cpuid_0x10_3_eax eax;
308 union cpuid_0x10_x_edx edx;
311 cpuid_count(0x80000020, 1, &eax.full, &ebx, &ecx, &edx.full);
312 hw_res->num_closid = edx.split.cos_max + 1;
313 r->default_ctrl = MAX_MBA_BW_AMD;
315 /* AMD does not use delay */
316 r->membw.delay_linear = false;
317 r->membw.arch_needs_linear = false;
320 * AMD does not use memory delay throttle model to control
321 * the allocation like Intel does.
323 r->membw.throttle_mode = THREAD_THROTTLE_UNDEFINED;
325 r->membw.bw_gran = 1;
326 /* Max value is 2048, Data width should be 4 in decimal */
329 r->alloc_capable = true;
330 r->alloc_enabled = true;
335 static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r)
337 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
338 union cpuid_0x10_1_eax eax;
339 union cpuid_0x10_x_edx edx;
342 cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full);
343 hw_res->num_closid = edx.split.cos_max + 1;
344 r->cache.cbm_len = eax.split.cbm_len + 1;
345 r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
346 r->cache.shareable_bits = ebx & r->default_ctrl;
347 r->data_width = (r->cache.cbm_len + 3) / 4;
348 r->alloc_capable = true;
349 r->alloc_enabled = true;
352 static void rdt_get_cdp_config(int level, int type)
354 struct rdt_resource *r_l = &rdt_resources_all[level].r_resctrl;
355 struct rdt_hw_resource *hw_res_l = resctrl_to_arch_res(r_l);
356 struct rdt_resource *r = &rdt_resources_all[type].r_resctrl;
357 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
359 hw_res->num_closid = hw_res_l->num_closid / 2;
360 r->cache.cbm_len = r_l->cache.cbm_len;
361 r->default_ctrl = r_l->default_ctrl;
362 r->cache.shareable_bits = r_l->cache.shareable_bits;
363 r->data_width = (r->cache.cbm_len + 3) / 4;
364 r->alloc_capable = true;
366 * By default, CDP is disabled. CDP can be enabled by mount parameter
367 * "cdp" during resctrl file system mount time.
369 r->alloc_enabled = false;
372 static void rdt_get_cdp_l3_config(void)
374 rdt_get_cdp_config(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA);
375 rdt_get_cdp_config(RDT_RESOURCE_L3, RDT_RESOURCE_L3CODE);
378 static void rdt_get_cdp_l2_config(void)
380 rdt_get_cdp_config(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA);
381 rdt_get_cdp_config(RDT_RESOURCE_L2, RDT_RESOURCE_L2CODE);
385 mba_wrmsr_amd(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
388 struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
389 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
391 for (i = m->low; i < m->high; i++)
392 wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
396 * Map the memory b/w percentage value to delay values
397 * that can be written to QOS_MSRs.
398 * There are currently no SKUs which support non linear delay values.
400 u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)
402 if (r->membw.delay_linear)
403 return MAX_MBA_BW - bw;
405 pr_warn_once("Non Linear delay-bw map not supported but queried\n");
406 return r->default_ctrl;
410 mba_wrmsr_intel(struct rdt_domain *d, struct msr_param *m,
411 struct rdt_resource *r)
414 struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
415 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
417 /* Write the delay values for mba. */
418 for (i = m->low; i < m->high; i++)
419 wrmsrl(hw_res->msr_base + i, delay_bw_map(hw_dom->ctrl_val[i], r));
423 cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
426 struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
427 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
429 for (i = m->low; i < m->high; i++)
430 wrmsrl(hw_res->msr_base + cbm_idx(r, i), hw_dom->ctrl_val[i]);
433 struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r)
435 struct rdt_domain *d;
437 list_for_each_entry(d, &r->domains, list) {
438 /* Find the domain that contains this CPU */
439 if (cpumask_test_cpu(cpu, &d->cpu_mask))
446 void rdt_ctrl_update(void *arg)
448 struct msr_param *m = arg;
449 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(m->res);
450 struct rdt_resource *r = m->res;
451 int cpu = smp_processor_id();
452 struct rdt_domain *d;
454 d = get_domain_from_cpu(cpu, r);
456 hw_res->msr_update(d, m, r);
459 pr_warn_once("cpu %d not found in any domain for resource %s\n",
464 * rdt_find_domain - Find a domain in a resource that matches input resource id
466 * Search resource r's domain list to find the resource id. If the resource
467 * id is found in a domain, return the domain. Otherwise, if requested by
468 * caller, return the first domain whose id is bigger than the input id.
469 * The domain list is sorted by id in ascending order.
471 struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
472 struct list_head **pos)
474 struct rdt_domain *d;
478 return ERR_PTR(-ENODEV);
480 list_for_each(l, &r->domains) {
481 d = list_entry(l, struct rdt_domain, list);
482 /* When id is found, return its domain. */
485 /* Stop searching when finding id's position in sorted list. */
496 void setup_default_ctrlval(struct rdt_resource *r, u32 *dc, u32 *dm)
498 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
502 * Initialize the Control MSRs to having no control.
503 * For Cache Allocation: Set all bits in cbm
504 * For Memory Allocation: Set b/w requested to 100%
505 * and the bandwidth in MBps to U32_MAX
507 for (i = 0; i < hw_res->num_closid; i++, dc++, dm++) {
508 *dc = r->default_ctrl;
513 static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
515 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
516 struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
520 dc = kmalloc_array(hw_res->num_closid, sizeof(*hw_dom->ctrl_val), GFP_KERNEL);
524 dm = kmalloc_array(hw_res->num_closid, sizeof(*hw_dom->mbps_val), GFP_KERNEL);
530 hw_dom->ctrl_val = dc;
531 hw_dom->mbps_val = dm;
532 setup_default_ctrlval(r, dc, dm);
535 m.high = hw_res->num_closid;
536 hw_res->msr_update(d, &m, r);
540 static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
544 if (is_llc_occupancy_enabled()) {
545 d->rmid_busy_llc = bitmap_zalloc(r->num_rmid, GFP_KERNEL);
546 if (!d->rmid_busy_llc)
548 INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo);
550 if (is_mbm_total_enabled()) {
551 tsize = sizeof(*d->mbm_total);
552 d->mbm_total = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
554 bitmap_free(d->rmid_busy_llc);
558 if (is_mbm_local_enabled()) {
559 tsize = sizeof(*d->mbm_local);
560 d->mbm_local = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
562 bitmap_free(d->rmid_busy_llc);
568 if (is_mbm_enabled()) {
569 INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow);
570 mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL);
577 * domain_add_cpu - Add a cpu to a resource's domain list.
579 * If an existing domain in the resource r's domain list matches the cpu's
580 * resource id, add the cpu in the domain.
582 * Otherwise, a new domain is allocated and inserted into the right position
583 * in the domain list sorted by id in ascending order.
585 * The order in the domain list is visible to users when we print entries
586 * in the schemata file and schemata input is validated to have the same order
589 static void domain_add_cpu(int cpu, struct rdt_resource *r)
591 int id = get_cpu_cacheinfo_id(cpu, r->cache_level);
592 struct list_head *add_pos = NULL;
593 struct rdt_hw_domain *hw_dom;
594 struct rdt_domain *d;
596 d = rdt_find_domain(r, id, &add_pos);
598 pr_warn("Couldn't find cache id for CPU %d\n", cpu);
603 cpumask_set_cpu(cpu, &d->cpu_mask);
604 if (r->cache.arch_has_per_cpu_cfg)
605 rdt_domain_reconfigure_cdp(r);
609 hw_dom = kzalloc_node(sizeof(*hw_dom), GFP_KERNEL, cpu_to_node(cpu));
613 d = &hw_dom->d_resctrl;
615 cpumask_set_cpu(cpu, &d->cpu_mask);
617 rdt_domain_reconfigure_cdp(r);
619 if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
624 if (r->mon_capable && domain_setup_mon_state(r, d)) {
629 list_add_tail(&d->list, add_pos);
632 * If resctrl is mounted, add
633 * per domain monitor data directories.
635 if (static_branch_unlikely(&rdt_mon_enable_key))
636 mkdir_mondata_subdir_allrdtgrp(r, d);
639 static void domain_remove_cpu(int cpu, struct rdt_resource *r)
641 int id = get_cpu_cacheinfo_id(cpu, r->cache_level);
642 struct rdt_hw_domain *hw_dom;
643 struct rdt_domain *d;
645 d = rdt_find_domain(r, id, NULL);
646 if (IS_ERR_OR_NULL(d)) {
647 pr_warn("Couldn't find cache id for CPU %d\n", cpu);
650 hw_dom = resctrl_to_arch_dom(d);
652 cpumask_clear_cpu(cpu, &d->cpu_mask);
653 if (cpumask_empty(&d->cpu_mask)) {
655 * If resctrl is mounted, remove all the
656 * per domain monitor data directories.
658 if (static_branch_unlikely(&rdt_mon_enable_key))
659 rmdir_mondata_subdir_allrdtgrp(r, d->id);
661 if (r->mon_capable && is_mbm_enabled())
662 cancel_delayed_work(&d->mbm_over);
663 if (is_llc_occupancy_enabled() && has_busy_rmid(r, d)) {
665 * When a package is going down, forcefully
666 * decrement rmid->ebusy. There is no way to know
667 * that the L3 was flushed and hence may lead to
668 * incorrect counts in rare scenarios, but leaving
669 * the RMID as busy creates RMID leaks if the
670 * package never comes back.
672 __check_limbo(d, true);
673 cancel_delayed_work(&d->cqm_limbo);
677 * rdt_domain "d" is going to be freed below, so clear
678 * its pointer from pseudo_lock_region struct.
683 kfree(hw_dom->ctrl_val);
684 kfree(hw_dom->mbps_val);
685 bitmap_free(d->rmid_busy_llc);
692 if (r == &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl) {
693 if (is_mbm_enabled() && cpu == d->mbm_work_cpu) {
694 cancel_delayed_work(&d->mbm_over);
695 mbm_setup_overflow_handler(d, 0);
697 if (is_llc_occupancy_enabled() && cpu == d->cqm_work_cpu &&
698 has_busy_rmid(r, d)) {
699 cancel_delayed_work(&d->cqm_limbo);
700 cqm_setup_limbo_handler(d, 0);
705 static void clear_closid_rmid(int cpu)
707 struct resctrl_pqr_state *state = this_cpu_ptr(&pqr_state);
709 state->default_closid = 0;
710 state->default_rmid = 0;
711 state->cur_closid = 0;
713 wrmsr(IA32_PQR_ASSOC, 0, 0);
716 static int resctrl_online_cpu(unsigned int cpu)
718 struct rdt_resource *r;
720 mutex_lock(&rdtgroup_mutex);
721 for_each_capable_rdt_resource(r)
722 domain_add_cpu(cpu, r);
723 /* The cpu is set in default rdtgroup after online. */
724 cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
725 clear_closid_rmid(cpu);
726 mutex_unlock(&rdtgroup_mutex);
731 static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)
735 list_for_each_entry(cr, &r->mon.crdtgrp_list, mon.crdtgrp_list) {
736 if (cpumask_test_and_clear_cpu(cpu, &cr->cpu_mask)) {
742 static int resctrl_offline_cpu(unsigned int cpu)
744 struct rdtgroup *rdtgrp;
745 struct rdt_resource *r;
747 mutex_lock(&rdtgroup_mutex);
748 for_each_capable_rdt_resource(r)
749 domain_remove_cpu(cpu, r);
750 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
751 if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask)) {
752 clear_childcpus(rdtgrp, cpu);
756 clear_closid_rmid(cpu);
757 mutex_unlock(&rdtgroup_mutex);
763 * Choose a width for the resource name and resource data based on the
764 * resource that has widest name and cbm.
766 static __init void rdt_init_padding(void)
768 struct rdt_resource *r;
771 for_each_alloc_capable_rdt_resource(r) {
772 cl = strlen(r->name);
773 if (cl > max_name_width)
776 if (r->data_width > max_data_width)
777 max_data_width = r->data_width;
792 #define RDT_OPT(idx, n, f) \
801 bool force_off, force_on;
804 static struct rdt_options rdt_options[] __initdata = {
805 RDT_OPT(RDT_FLAG_CMT, "cmt", X86_FEATURE_CQM_OCCUP_LLC),
806 RDT_OPT(RDT_FLAG_MBM_TOTAL, "mbmtotal", X86_FEATURE_CQM_MBM_TOTAL),
807 RDT_OPT(RDT_FLAG_MBM_LOCAL, "mbmlocal", X86_FEATURE_CQM_MBM_LOCAL),
808 RDT_OPT(RDT_FLAG_L3_CAT, "l3cat", X86_FEATURE_CAT_L3),
809 RDT_OPT(RDT_FLAG_L3_CDP, "l3cdp", X86_FEATURE_CDP_L3),
810 RDT_OPT(RDT_FLAG_L2_CAT, "l2cat", X86_FEATURE_CAT_L2),
811 RDT_OPT(RDT_FLAG_L2_CDP, "l2cdp", X86_FEATURE_CDP_L2),
812 RDT_OPT(RDT_FLAG_MBA, "mba", X86_FEATURE_MBA),
814 #define NUM_RDT_OPTIONS ARRAY_SIZE(rdt_options)
816 static int __init set_rdt_options(char *str)
818 struct rdt_options *o;
824 while ((tok = strsep(&str, ",")) != NULL) {
825 force_off = *tok == '!';
828 for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
829 if (strcmp(tok, o->name) == 0) {
840 __setup("rdt", set_rdt_options);
842 static bool __init rdt_cpu_has(int flag)
844 bool ret = boot_cpu_has(flag);
845 struct rdt_options *o;
850 for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
851 if (flag == o->flag) {
862 static __init bool get_mem_config(void)
864 struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_MBA];
866 if (!rdt_cpu_has(X86_FEATURE_MBA))
869 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
870 return __get_mem_config_intel(&hw_res->r_resctrl);
871 else if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
872 return __rdt_get_mem_config_amd(&hw_res->r_resctrl);
877 static __init bool get_rdt_alloc_resources(void)
879 struct rdt_resource *r;
882 if (rdt_alloc_capable)
885 if (!boot_cpu_has(X86_FEATURE_RDT_A))
888 if (rdt_cpu_has(X86_FEATURE_CAT_L3)) {
889 r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
890 rdt_get_cache_alloc_cfg(1, r);
891 if (rdt_cpu_has(X86_FEATURE_CDP_L3))
892 rdt_get_cdp_l3_config();
895 if (rdt_cpu_has(X86_FEATURE_CAT_L2)) {
896 /* CPUID 0x10.2 fields are same format at 0x10.1 */
897 r = &rdt_resources_all[RDT_RESOURCE_L2].r_resctrl;
898 rdt_get_cache_alloc_cfg(2, r);
899 if (rdt_cpu_has(X86_FEATURE_CDP_L2))
900 rdt_get_cdp_l2_config();
904 if (get_mem_config())
910 static __init bool get_rdt_mon_resources(void)
912 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
914 if (rdt_cpu_has(X86_FEATURE_CQM_OCCUP_LLC))
915 rdt_mon_features |= (1 << QOS_L3_OCCUP_EVENT_ID);
916 if (rdt_cpu_has(X86_FEATURE_CQM_MBM_TOTAL))
917 rdt_mon_features |= (1 << QOS_L3_MBM_TOTAL_EVENT_ID);
918 if (rdt_cpu_has(X86_FEATURE_CQM_MBM_LOCAL))
919 rdt_mon_features |= (1 << QOS_L3_MBM_LOCAL_EVENT_ID);
921 if (!rdt_mon_features)
924 return !rdt_get_mon_l3_config(r);
927 static __init void __check_quirks_intel(void)
929 switch (boot_cpu_data.x86_model) {
930 case INTEL_FAM6_HASWELL_X:
931 if (!rdt_options[RDT_FLAG_L3_CAT].force_off)
932 cache_alloc_hsw_probe();
934 case INTEL_FAM6_SKYLAKE_X:
935 if (boot_cpu_data.x86_stepping <= 4)
936 set_rdt_options("!cmt,!mbmtotal,!mbmlocal,!l3cat");
938 set_rdt_options("!l3cat");
940 case INTEL_FAM6_BROADWELL_X:
941 intel_rdt_mbm_apply_quirk();
946 static __init void check_quirks(void)
948 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
949 __check_quirks_intel();
952 static __init bool get_rdt_resources(void)
954 rdt_alloc_capable = get_rdt_alloc_resources();
955 rdt_mon_capable = get_rdt_mon_resources();
957 return (rdt_mon_capable || rdt_alloc_capable);
960 static __init void rdt_init_res_defs_intel(void)
962 struct rdt_hw_resource *hw_res;
963 struct rdt_resource *r;
965 for_each_rdt_resource(r) {
966 hw_res = resctrl_to_arch_res(r);
968 if (r->rid == RDT_RESOURCE_L3 ||
969 r->rid == RDT_RESOURCE_L3DATA ||
970 r->rid == RDT_RESOURCE_L3CODE ||
971 r->rid == RDT_RESOURCE_L2 ||
972 r->rid == RDT_RESOURCE_L2DATA ||
973 r->rid == RDT_RESOURCE_L2CODE) {
974 r->cache.arch_has_sparse_bitmaps = false;
975 r->cache.arch_has_empty_bitmaps = false;
976 r->cache.arch_has_per_cpu_cfg = false;
977 } else if (r->rid == RDT_RESOURCE_MBA) {
978 hw_res->msr_base = MSR_IA32_MBA_THRTL_BASE;
979 hw_res->msr_update = mba_wrmsr_intel;
984 static __init void rdt_init_res_defs_amd(void)
986 struct rdt_hw_resource *hw_res;
987 struct rdt_resource *r;
989 for_each_rdt_resource(r) {
990 hw_res = resctrl_to_arch_res(r);
992 if (r->rid == RDT_RESOURCE_L3 ||
993 r->rid == RDT_RESOURCE_L3DATA ||
994 r->rid == RDT_RESOURCE_L3CODE ||
995 r->rid == RDT_RESOURCE_L2 ||
996 r->rid == RDT_RESOURCE_L2DATA ||
997 r->rid == RDT_RESOURCE_L2CODE) {
998 r->cache.arch_has_sparse_bitmaps = true;
999 r->cache.arch_has_empty_bitmaps = true;
1000 r->cache.arch_has_per_cpu_cfg = true;
1001 } else if (r->rid == RDT_RESOURCE_MBA) {
1002 hw_res->msr_base = MSR_IA32_MBA_BW_BASE;
1003 hw_res->msr_update = mba_wrmsr_amd;
1008 static __init void rdt_init_res_defs(void)
1010 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1011 rdt_init_res_defs_intel();
1012 else if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1013 rdt_init_res_defs_amd();
1016 static enum cpuhp_state rdt_online;
1018 /* Runs once on the BSP during boot. */
1019 void resctrl_cpu_detect(struct cpuinfo_x86 *c)
1021 if (!cpu_has(c, X86_FEATURE_CQM_LLC)) {
1022 c->x86_cache_max_rmid = -1;
1023 c->x86_cache_occ_scale = -1;
1024 c->x86_cache_mbm_width_offset = -1;
1028 /* will be overridden if occupancy monitoring exists */
1029 c->x86_cache_max_rmid = cpuid_ebx(0xf);
1031 if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC) ||
1032 cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL) ||
1033 cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)) {
1034 u32 eax, ebx, ecx, edx;
1036 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
1037 cpuid_count(0xf, 1, &eax, &ebx, &ecx, &edx);
1039 c->x86_cache_max_rmid = ecx;
1040 c->x86_cache_occ_scale = ebx;
1041 c->x86_cache_mbm_width_offset = eax & 0xff;
1043 if (c->x86_vendor == X86_VENDOR_AMD && !c->x86_cache_mbm_width_offset)
1044 c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_AMD;
1048 static int __init resctrl_late_init(void)
1050 struct rdt_resource *r;
1054 * Initialize functions(or definitions) that are different
1055 * between vendors here.
1057 rdt_init_res_defs();
1061 if (!get_rdt_resources())
1066 state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
1067 "x86/resctrl/cat:online:",
1068 resctrl_online_cpu, resctrl_offline_cpu);
1072 ret = rdtgroup_init();
1074 cpuhp_remove_state(state);
1079 for_each_alloc_capable_rdt_resource(r)
1080 pr_info("%s allocation detected\n", r->name);
1082 for_each_mon_capable_rdt_resource(r)
1083 pr_info("%s monitoring detected\n", r->name);
1088 late_initcall(resctrl_late_init);
1090 static void __exit resctrl_exit(void)
1092 cpuhp_remove_state(rdt_online);
1096 __exitcall(resctrl_exit);