10fbbc3cf40ab68b09d38899f08bc7c2ca97ace0
[linux-2.6-microblaze.git] / arch / x86 / kernel / cpu / resctrl / core.c
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                         .cache = {
70                                 .min_cbm_bits   = 1,
71                                 .cbm_idx_mult   = 1,
72                                 .cbm_idx_offset = 0,
73                         },
74                         .domains                = 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_L3DATA] =
83         {
84                 .r_resctrl = {
85                         .rid                    = RDT_RESOURCE_L3DATA,
86                         .name                   = "L3DATA",
87                         .cache_level            = 3,
88                         .cache = {
89                                 .min_cbm_bits   = 1,
90                                 .cbm_idx_mult   = 2,
91                                 .cbm_idx_offset = 0,
92                         },
93                         .domains                = domain_init(RDT_RESOURCE_L3DATA),
94                         .parse_ctrlval          = parse_cbm,
95                         .format_str             = "%d=%0*x",
96                         .fflags                 = RFTYPE_RES_CACHE,
97                 },
98                 .msr_base               = MSR_IA32_L3_CBM_BASE,
99                 .msr_update             = cat_wrmsr,
100         },
101         [RDT_RESOURCE_L3CODE] =
102         {
103                 .r_resctrl = {
104                         .rid                    = RDT_RESOURCE_L3CODE,
105                         .name                   = "L3CODE",
106                         .cache_level            = 3,
107                         .cache = {
108                                 .min_cbm_bits   = 1,
109                                 .cbm_idx_mult   = 2,
110                                 .cbm_idx_offset = 1,
111                         },
112                         .domains                = domain_init(RDT_RESOURCE_L3CODE),
113                         .parse_ctrlval          = parse_cbm,
114                         .format_str             = "%d=%0*x",
115                         .fflags                 = RFTYPE_RES_CACHE,
116                 },
117                 .msr_base               = MSR_IA32_L3_CBM_BASE,
118                 .msr_update             = cat_wrmsr,
119         },
120         [RDT_RESOURCE_L2] =
121         {
122                 .r_resctrl = {
123                         .rid                    = RDT_RESOURCE_L2,
124                         .name                   = "L2",
125                         .cache_level            = 2,
126                         .cache = {
127                                 .min_cbm_bits   = 1,
128                                 .cbm_idx_mult   = 1,
129                                 .cbm_idx_offset = 0,
130                         },
131                         .domains                = domain_init(RDT_RESOURCE_L2),
132                         .parse_ctrlval          = parse_cbm,
133                         .format_str             = "%d=%0*x",
134                         .fflags                 = RFTYPE_RES_CACHE,
135                 },
136                 .msr_base               = MSR_IA32_L2_CBM_BASE,
137                 .msr_update             = cat_wrmsr,
138         },
139         [RDT_RESOURCE_L2DATA] =
140         {
141                 .r_resctrl = {
142                         .rid                    = RDT_RESOURCE_L2DATA,
143                         .name                   = "L2DATA",
144                         .cache_level            = 2,
145                         .cache = {
146                                 .min_cbm_bits   = 1,
147                                 .cbm_idx_mult   = 2,
148                                 .cbm_idx_offset = 0,
149                         },
150                         .domains                = domain_init(RDT_RESOURCE_L2DATA),
151                         .parse_ctrlval          = parse_cbm,
152                         .format_str             = "%d=%0*x",
153                         .fflags                 = RFTYPE_RES_CACHE,
154                 },
155                 .msr_base               = MSR_IA32_L2_CBM_BASE,
156                 .msr_update             = cat_wrmsr,
157         },
158         [RDT_RESOURCE_L2CODE] =
159         {
160                 .r_resctrl = {
161                         .rid                    = RDT_RESOURCE_L2CODE,
162                         .name                   = "L2CODE",
163                         .cache_level            = 2,
164                         .cache = {
165                                 .min_cbm_bits   = 1,
166                                 .cbm_idx_mult   = 2,
167                                 .cbm_idx_offset = 1,
168                         },
169                         .domains                = domain_init(RDT_RESOURCE_L2CODE),
170                         .parse_ctrlval          = parse_cbm,
171                         .format_str             = "%d=%0*x",
172                         .fflags                 = RFTYPE_RES_CACHE,
173                 },
174                 .msr_base               = MSR_IA32_L2_CBM_BASE,
175                 .msr_update             = cat_wrmsr,
176         },
177         [RDT_RESOURCE_MBA] =
178         {
179                 .r_resctrl = {
180                         .rid                    = RDT_RESOURCE_MBA,
181                         .name                   = "MB",
182                         .cache_level            = 3,
183                         .domains                = domain_init(RDT_RESOURCE_MBA),
184                         .parse_ctrlval          = parse_bw,
185                         .format_str             = "%d=%*u",
186                         .fflags                 = RFTYPE_RES_MB,
187                 },
188         },
189 };
190
191 static unsigned int cbm_idx(struct rdt_resource *r, unsigned int closid)
192 {
193         return closid * r->cache.cbm_idx_mult + r->cache.cbm_idx_offset;
194 }
195
196 /*
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
201  * CAT:
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
208  *
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.
213  */
214 static inline void cache_alloc_hsw_probe(void)
215 {
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;
219
220         if (wrmsr_safe(MSR_IA32_L3_CBM_BASE, max_cbm, 0))
221                 return;
222
223         rdmsr(MSR_IA32_L3_CBM_BASE, l, h);
224
225         /* If all the bits were set in MSR, return success */
226         if (l != max_cbm)
227                 return;
228
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;
236
237         rdt_alloc_capable = true;
238 }
239
240 bool is_mba_sc(struct rdt_resource *r)
241 {
242         if (!r)
243                 return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.mba_sc;
244
245         return r->membw.mba_sc;
246 }
247
248 /*
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.
251  *
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.
257  */
258 static inline bool rdt_get_mb_table(struct rdt_resource *r)
259 {
260         /*
261          * There are no Intel SKUs as of now to support non-linear delay.
262          */
263         pr_info("MBA b/w map not implemented for cpu:%d, model:%d",
264                 boot_cpu_data.x86, boot_cpu_data.x86_model);
265
266         return false;
267 }
268
269 static bool __get_mem_config_intel(struct rdt_resource *r)
270 {
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;
275
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;
285         } else {
286                 if (!rdt_get_mb_table(r))
287                         return false;
288                 r->membw.arch_needs_linear = false;
289         }
290         r->data_width = 3;
291
292         if (boot_cpu_has(X86_FEATURE_PER_THREAD_MBA))
293                 r->membw.throttle_mode = THREAD_THROTTLE_PER_THREAD;
294         else
295                 r->membw.throttle_mode = THREAD_THROTTLE_MAX;
296         thread_throttle_mode_init();
297
298         r->alloc_capable = true;
299         r->alloc_enabled = true;
300
301         return true;
302 }
303
304 static bool __rdt_get_mem_config_amd(struct rdt_resource *r)
305 {
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;
309         u32 ebx, ecx;
310
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;
314
315         /* AMD does not use delay */
316         r->membw.delay_linear = false;
317         r->membw.arch_needs_linear = false;
318
319         /*
320          * AMD does not use memory delay throttle model to control
321          * the allocation like Intel does.
322          */
323         r->membw.throttle_mode = THREAD_THROTTLE_UNDEFINED;
324         r->membw.min_bw = 0;
325         r->membw.bw_gran = 1;
326         /* Max value is 2048, Data width should be 4 in decimal */
327         r->data_width = 4;
328
329         r->alloc_capable = true;
330         r->alloc_enabled = true;
331
332         return true;
333 }
334
335 static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r)
336 {
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;
340         u32 ebx, ecx;
341
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;
350 }
351
352 static void rdt_get_cdp_config(int level, int type)
353 {
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);
358
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;
365         /*
366          * By default, CDP is disabled. CDP can be enabled by mount parameter
367          * "cdp" during resctrl file system mount time.
368          */
369         r->alloc_enabled = false;
370 }
371
372 static void rdt_get_cdp_l3_config(void)
373 {
374         rdt_get_cdp_config(RDT_RESOURCE_L3, RDT_RESOURCE_L3DATA);
375         rdt_get_cdp_config(RDT_RESOURCE_L3, RDT_RESOURCE_L3CODE);
376 }
377
378 static void rdt_get_cdp_l2_config(void)
379 {
380         rdt_get_cdp_config(RDT_RESOURCE_L2, RDT_RESOURCE_L2DATA);
381         rdt_get_cdp_config(RDT_RESOURCE_L2, RDT_RESOURCE_L2CODE);
382 }
383
384 static void
385 mba_wrmsr_amd(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
386 {
387         unsigned int i;
388         struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
389         struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
390
391         for (i = m->low; i < m->high; i++)
392                 wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
393 }
394
395 /*
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.
399  */
400 u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)
401 {
402         if (r->membw.delay_linear)
403                 return MAX_MBA_BW - bw;
404
405         pr_warn_once("Non Linear delay-bw map not supported but queried\n");
406         return r->default_ctrl;
407 }
408
409 static void
410 mba_wrmsr_intel(struct rdt_domain *d, struct msr_param *m,
411                 struct rdt_resource *r)
412 {
413         unsigned int i;
414         struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
415         struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
416
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));
420 }
421
422 static void
423 cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
424 {
425         unsigned int i;
426         struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
427         struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
428
429         for (i = m->low; i < m->high; i++)
430                 wrmsrl(hw_res->msr_base + cbm_idx(r, i), hw_dom->ctrl_val[i]);
431 }
432
433 struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r)
434 {
435         struct rdt_domain *d;
436
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))
440                         return d;
441         }
442
443         return NULL;
444 }
445
446 void rdt_ctrl_update(void *arg)
447 {
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;
453
454         d = get_domain_from_cpu(cpu, r);
455         if (d) {
456                 hw_res->msr_update(d, m, r);
457                 return;
458         }
459         pr_warn_once("cpu %d not found in any domain for resource %s\n",
460                      cpu, r->name);
461 }
462
463 /*
464  * rdt_find_domain - Find a domain in a resource that matches input resource id
465  *
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.
470  */
471 struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
472                                    struct list_head **pos)
473 {
474         struct rdt_domain *d;
475         struct list_head *l;
476
477         if (id < 0)
478                 return ERR_PTR(-ENODEV);
479
480         list_for_each(l, &r->domains) {
481                 d = list_entry(l, struct rdt_domain, list);
482                 /* When id is found, return its domain. */
483                 if (id == d->id)
484                         return d;
485                 /* Stop searching when finding id's position in sorted list. */
486                 if (id < d->id)
487                         break;
488         }
489
490         if (pos)
491                 *pos = l;
492
493         return NULL;
494 }
495
496 void setup_default_ctrlval(struct rdt_resource *r, u32 *dc, u32 *dm)
497 {
498         struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
499         int i;
500
501         /*
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
506          */
507         for (i = 0; i < hw_res->num_closid; i++, dc++, dm++) {
508                 *dc = r->default_ctrl;
509                 *dm = MBA_MAX_MBPS;
510         }
511 }
512
513 static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
514 {
515         struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
516         struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
517         struct msr_param m;
518         u32 *dc, *dm;
519
520         dc = kmalloc_array(hw_res->num_closid, sizeof(*hw_dom->ctrl_val), GFP_KERNEL);
521         if (!dc)
522                 return -ENOMEM;
523
524         dm = kmalloc_array(hw_res->num_closid, sizeof(*hw_dom->mbps_val), GFP_KERNEL);
525         if (!dm) {
526                 kfree(dc);
527                 return -ENOMEM;
528         }
529
530         hw_dom->ctrl_val = dc;
531         hw_dom->mbps_val = dm;
532         setup_default_ctrlval(r, dc, dm);
533
534         m.low = 0;
535         m.high = hw_res->num_closid;
536         hw_res->msr_update(d, &m, r);
537         return 0;
538 }
539
540 static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
541 {
542         size_t tsize;
543
544         if (is_llc_occupancy_enabled()) {
545                 d->rmid_busy_llc = bitmap_zalloc(r->num_rmid, GFP_KERNEL);
546                 if (!d->rmid_busy_llc)
547                         return -ENOMEM;
548                 INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo);
549         }
550         if (is_mbm_total_enabled()) {
551                 tsize = sizeof(*d->mbm_total);
552                 d->mbm_total = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
553                 if (!d->mbm_total) {
554                         bitmap_free(d->rmid_busy_llc);
555                         return -ENOMEM;
556                 }
557         }
558         if (is_mbm_local_enabled()) {
559                 tsize = sizeof(*d->mbm_local);
560                 d->mbm_local = kcalloc(r->num_rmid, tsize, GFP_KERNEL);
561                 if (!d->mbm_local) {
562                         bitmap_free(d->rmid_busy_llc);
563                         kfree(d->mbm_total);
564                         return -ENOMEM;
565                 }
566         }
567
568         if (is_mbm_enabled()) {
569                 INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow);
570                 mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL);
571         }
572
573         return 0;
574 }
575
576 /*
577  * domain_add_cpu - Add a cpu to a resource's domain list.
578  *
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.
581  *
582  * Otherwise, a new domain is allocated and inserted into the right position
583  * in the domain list sorted by id in ascending order.
584  *
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
587  * as this list.
588  */
589 static void domain_add_cpu(int cpu, struct rdt_resource *r)
590 {
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;
595
596         d = rdt_find_domain(r, id, &add_pos);
597         if (IS_ERR(d)) {
598                 pr_warn("Couldn't find cache id for CPU %d\n", cpu);
599                 return;
600         }
601
602         if (d) {
603                 cpumask_set_cpu(cpu, &d->cpu_mask);
604                 if (r->cache.arch_has_per_cpu_cfg)
605                         rdt_domain_reconfigure_cdp(r);
606                 return;
607         }
608
609         hw_dom = kzalloc_node(sizeof(*hw_dom), GFP_KERNEL, cpu_to_node(cpu));
610         if (!hw_dom)
611                 return;
612
613         d = &hw_dom->d_resctrl;
614         d->id = id;
615         cpumask_set_cpu(cpu, &d->cpu_mask);
616
617         rdt_domain_reconfigure_cdp(r);
618
619         if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
620                 kfree(d);
621                 return;
622         }
623
624         if (r->mon_capable && domain_setup_mon_state(r, d)) {
625                 kfree(d);
626                 return;
627         }
628
629         list_add_tail(&d->list, add_pos);
630
631         /*
632          * If resctrl is mounted, add
633          * per domain monitor data directories.
634          */
635         if (static_branch_unlikely(&rdt_mon_enable_key))
636                 mkdir_mondata_subdir_allrdtgrp(r, d);
637 }
638
639 static void domain_remove_cpu(int cpu, struct rdt_resource *r)
640 {
641         int id = get_cpu_cacheinfo_id(cpu, r->cache_level);
642         struct rdt_hw_domain *hw_dom;
643         struct rdt_domain *d;
644
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);
648                 return;
649         }
650         hw_dom = resctrl_to_arch_dom(d);
651
652         cpumask_clear_cpu(cpu, &d->cpu_mask);
653         if (cpumask_empty(&d->cpu_mask)) {
654                 /*
655                  * If resctrl is mounted, remove all the
656                  * per domain monitor data directories.
657                  */
658                 if (static_branch_unlikely(&rdt_mon_enable_key))
659                         rmdir_mondata_subdir_allrdtgrp(r, d->id);
660                 list_del(&d->list);
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)) {
664                         /*
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.
671                          */
672                         __check_limbo(d, true);
673                         cancel_delayed_work(&d->cqm_limbo);
674                 }
675
676                 /*
677                  * rdt_domain "d" is going to be freed below, so clear
678                  * its pointer from pseudo_lock_region struct.
679                  */
680                 if (d->plr)
681                         d->plr->d = NULL;
682
683                 kfree(hw_dom->ctrl_val);
684                 kfree(hw_dom->mbps_val);
685                 bitmap_free(d->rmid_busy_llc);
686                 kfree(d->mbm_total);
687                 kfree(d->mbm_local);
688                 kfree(hw_dom);
689                 return;
690         }
691
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);
696                 }
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);
701                 }
702         }
703 }
704
705 static void clear_closid_rmid(int cpu)
706 {
707         struct resctrl_pqr_state *state = this_cpu_ptr(&pqr_state);
708
709         state->default_closid = 0;
710         state->default_rmid = 0;
711         state->cur_closid = 0;
712         state->cur_rmid = 0;
713         wrmsr(IA32_PQR_ASSOC, 0, 0);
714 }
715
716 static int resctrl_online_cpu(unsigned int cpu)
717 {
718         struct rdt_resource *r;
719
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);
727
728         return 0;
729 }
730
731 static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)
732 {
733         struct rdtgroup *cr;
734
735         list_for_each_entry(cr, &r->mon.crdtgrp_list, mon.crdtgrp_list) {
736                 if (cpumask_test_and_clear_cpu(cpu, &cr->cpu_mask)) {
737                         break;
738                 }
739         }
740 }
741
742 static int resctrl_offline_cpu(unsigned int cpu)
743 {
744         struct rdtgroup *rdtgrp;
745         struct rdt_resource *r;
746
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);
753                         break;
754                 }
755         }
756         clear_closid_rmid(cpu);
757         mutex_unlock(&rdtgroup_mutex);
758
759         return 0;
760 }
761
762 /*
763  * Choose a width for the resource name and resource data based on the
764  * resource that has widest name and cbm.
765  */
766 static __init void rdt_init_padding(void)
767 {
768         struct rdt_resource *r;
769         int cl;
770
771         for_each_alloc_capable_rdt_resource(r) {
772                 cl = strlen(r->name);
773                 if (cl > max_name_width)
774                         max_name_width = cl;
775
776                 if (r->data_width > max_data_width)
777                         max_data_width = r->data_width;
778         }
779 }
780
781 enum {
782         RDT_FLAG_CMT,
783         RDT_FLAG_MBM_TOTAL,
784         RDT_FLAG_MBM_LOCAL,
785         RDT_FLAG_L3_CAT,
786         RDT_FLAG_L3_CDP,
787         RDT_FLAG_L2_CAT,
788         RDT_FLAG_L2_CDP,
789         RDT_FLAG_MBA,
790 };
791
792 #define RDT_OPT(idx, n, f)      \
793 [idx] = {                       \
794         .name = n,              \
795         .flag = f               \
796 }
797
798 struct rdt_options {
799         char    *name;
800         int     flag;
801         bool    force_off, force_on;
802 };
803
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),
813 };
814 #define NUM_RDT_OPTIONS ARRAY_SIZE(rdt_options)
815
816 static int __init set_rdt_options(char *str)
817 {
818         struct rdt_options *o;
819         bool force_off;
820         char *tok;
821
822         if (*str == '=')
823                 str++;
824         while ((tok = strsep(&str, ",")) != NULL) {
825                 force_off = *tok == '!';
826                 if (force_off)
827                         tok++;
828                 for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
829                         if (strcmp(tok, o->name) == 0) {
830                                 if (force_off)
831                                         o->force_off = true;
832                                 else
833                                         o->force_on = true;
834                                 break;
835                         }
836                 }
837         }
838         return 1;
839 }
840 __setup("rdt", set_rdt_options);
841
842 static bool __init rdt_cpu_has(int flag)
843 {
844         bool ret = boot_cpu_has(flag);
845         struct rdt_options *o;
846
847         if (!ret)
848                 return ret;
849
850         for (o = rdt_options; o < &rdt_options[NUM_RDT_OPTIONS]; o++) {
851                 if (flag == o->flag) {
852                         if (o->force_off)
853                                 ret = false;
854                         if (o->force_on)
855                                 ret = true;
856                         break;
857                 }
858         }
859         return ret;
860 }
861
862 static __init bool get_mem_config(void)
863 {
864         struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_MBA];
865
866         if (!rdt_cpu_has(X86_FEATURE_MBA))
867                 return false;
868
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);
873
874         return false;
875 }
876
877 static __init bool get_rdt_alloc_resources(void)
878 {
879         struct rdt_resource *r;
880         bool ret = false;
881
882         if (rdt_alloc_capable)
883                 return true;
884
885         if (!boot_cpu_has(X86_FEATURE_RDT_A))
886                 return false;
887
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();
893                 ret = true;
894         }
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();
901                 ret = true;
902         }
903
904         if (get_mem_config())
905                 ret = true;
906
907         return ret;
908 }
909
910 static __init bool get_rdt_mon_resources(void)
911 {
912         struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
913
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);
920
921         if (!rdt_mon_features)
922                 return false;
923
924         return !rdt_get_mon_l3_config(r);
925 }
926
927 static __init void __check_quirks_intel(void)
928 {
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();
933                 break;
934         case INTEL_FAM6_SKYLAKE_X:
935                 if (boot_cpu_data.x86_stepping <= 4)
936                         set_rdt_options("!cmt,!mbmtotal,!mbmlocal,!l3cat");
937                 else
938                         set_rdt_options("!l3cat");
939                 fallthrough;
940         case INTEL_FAM6_BROADWELL_X:
941                 intel_rdt_mbm_apply_quirk();
942                 break;
943         }
944 }
945
946 static __init void check_quirks(void)
947 {
948         if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
949                 __check_quirks_intel();
950 }
951
952 static __init bool get_rdt_resources(void)
953 {
954         rdt_alloc_capable = get_rdt_alloc_resources();
955         rdt_mon_capable = get_rdt_mon_resources();
956
957         return (rdt_mon_capable || rdt_alloc_capable);
958 }
959
960 static __init void rdt_init_res_defs_intel(void)
961 {
962         struct rdt_hw_resource *hw_res;
963         struct rdt_resource *r;
964
965         for_each_rdt_resource(r) {
966                 hw_res = resctrl_to_arch_res(r);
967
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;
980                 }
981         }
982 }
983
984 static __init void rdt_init_res_defs_amd(void)
985 {
986         struct rdt_hw_resource *hw_res;
987         struct rdt_resource *r;
988
989         for_each_rdt_resource(r) {
990                 hw_res = resctrl_to_arch_res(r);
991
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;
1004                 }
1005         }
1006 }
1007
1008 static __init void rdt_init_res_defs(void)
1009 {
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();
1014 }
1015
1016 static enum cpuhp_state rdt_online;
1017
1018 /* Runs once on the BSP during boot. */
1019 void resctrl_cpu_detect(struct cpuinfo_x86 *c)
1020 {
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;
1025                 return;
1026         }
1027
1028         /* will be overridden if occupancy monitoring exists */
1029         c->x86_cache_max_rmid = cpuid_ebx(0xf);
1030
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;
1035
1036                 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
1037                 cpuid_count(0xf, 1, &eax, &ebx, &ecx, &edx);
1038
1039                 c->x86_cache_max_rmid  = ecx;
1040                 c->x86_cache_occ_scale = ebx;
1041                 c->x86_cache_mbm_width_offset = eax & 0xff;
1042
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;
1045         }
1046 }
1047
1048 static int __init resctrl_late_init(void)
1049 {
1050         struct rdt_resource *r;
1051         int state, ret;
1052
1053         /*
1054          * Initialize functions(or definitions) that are different
1055          * between vendors here.
1056          */
1057         rdt_init_res_defs();
1058
1059         check_quirks();
1060
1061         if (!get_rdt_resources())
1062                 return -ENODEV;
1063
1064         rdt_init_padding();
1065
1066         state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
1067                                   "x86/resctrl/cat:online:",
1068                                   resctrl_online_cpu, resctrl_offline_cpu);
1069         if (state < 0)
1070                 return state;
1071
1072         ret = rdtgroup_init();
1073         if (ret) {
1074                 cpuhp_remove_state(state);
1075                 return ret;
1076         }
1077         rdt_online = state;
1078
1079         for_each_alloc_capable_rdt_resource(r)
1080                 pr_info("%s allocation detected\n", r->name);
1081
1082         for_each_mon_capable_rdt_resource(r)
1083                 pr_info("%s monitoring detected\n", r->name);
1084
1085         return 0;
1086 }
1087
1088 late_initcall(resctrl_late_init);
1089
1090 static void __exit resctrl_exit(void)
1091 {
1092         cpuhp_remove_state(rdt_online);
1093         rdtgroup_exit();
1094 }
1095
1096 __exitcall(resctrl_exit);