blob: 303b085b969ff00b17b3a51a5d77c3428ffd36c9 [file] [log] [blame]
Fenghua Yu78e99b42016-10-22 06:19:53 -07001/*
2 * Resource Director Technology(RDT)
3 * - Cache Allocation code.
4 *
5 * Copyright (C) 2016 Intel Corporation
6 *
7 * Authors:
8 * Fenghua Yu <fenghua.yu@intel.com>
9 * Tony Luck <tony.luck@intel.com>
10 * Vikas Shivappa <vikas.shivappa@intel.com>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms and conditions of the GNU General Public License,
14 * version 2, as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * more details.
20 *
21 * More information about RDT be found in the Intel (R) x86 Architecture
22 * Software Developer Manual June 2016, volume 3, section 17.17.
23 */
24
25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
27#include <linux/slab.h>
28#include <linux/err.h>
Tony Luck2264d9c2016-10-28 15:04:41 -070029#include <linux/cacheinfo.h>
30#include <linux/cpuhotplug.h>
Fenghua Yu78e99b42016-10-22 06:19:53 -070031
Fenghua Yu113c6092016-10-22 06:19:54 -070032#include <asm/intel-family.h>
Vikas Shivappa05830202017-07-25 14:14:23 -070033#include <asm/intel_rdt_sched.h>
34#include "intel_rdt.h"
Fenghua Yu113c6092016-10-22 06:19:54 -070035
Vikas Shivappa05b93412017-04-07 17:33:53 -070036#define MAX_MBA_BW 100u
37#define MBA_IS_LINEAR 0x4
38
Tony Luck2264d9c2016-10-28 15:04:41 -070039/* Mutex to protect rdtgroup access. */
40DEFINE_MUTEX(rdtgroup_mutex);
41
Vikas Shivappade016df2017-04-03 14:44:17 -070042/*
Vikas Shivappac39a0e22017-07-25 14:14:20 -070043 * The cached intel_pqr_state is strictly per CPU and can never be
44 * updated from a remote CPU. Functions which modify the state
45 * are called with interrupts disabled and no preemption, which
46 * is sufficient for the protection.
47 */
48DEFINE_PER_CPU(struct intel_pqr_state, pqr_state);
49
Vikas Shivappab09d9812017-07-25 14:14:35 -070050DEFINE_PER_CPU_READ_MOSTLY(struct intel_pqr_state, rdt_cpu_default);
51
Vikas Shivappac39a0e22017-07-25 14:14:20 -070052/*
Vikas Shivappade016df2017-04-03 14:44:17 -070053 * Used to store the max resource name width and max resource data width
54 * to display the schemata in a tabular format
55 */
56int max_name_width, max_data_width;
57
Vikas Shivappa6a445ed2017-07-25 14:14:27 -070058/*
59 * Global boolean for rdt_alloc which is true if any
60 * resource allocation is enabled.
61 */
62bool rdt_alloc_capable;
63
Thomas Gleixner0921c542017-04-14 14:14:31 +020064static void
Vikas Shivappa05b93412017-04-07 17:33:53 -070065mba_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r);
66static void
Thomas Gleixner0921c542017-04-14 14:14:31 +020067cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r);
68
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020069#define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].domains)
70
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -070071struct rdt_resource rdt_resources_all[] = {
Vikas Shivappadd131852017-07-25 14:14:26 -070072 [RDT_RESOURCE_L3] =
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -070073 {
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020074 .name = "L3",
75 .domains = domain_init(RDT_RESOURCE_L3),
76 .msr_base = IA32_L3_CBM_BASE,
Thomas Gleixner0921c542017-04-14 14:14:31 +020077 .msr_update = cat_wrmsr,
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020078 .cache_level = 3,
79 .cache = {
80 .min_cbm_bits = 1,
81 .cbm_idx_mult = 1,
82 .cbm_idx_offset = 0,
83 },
Vikas Shivappac6ea67d2017-04-07 17:33:56 -070084 .parse_ctrlval = parse_cbm,
85 .format_str = "%d=%0*x",
Tony luck5dc1d5c2017-07-25 14:14:29 -070086 .fflags = RFTYPE_RES_CACHE,
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -070087 },
Vikas Shivappadd131852017-07-25 14:14:26 -070088 [RDT_RESOURCE_L3DATA] =
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -070089 {
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020090 .name = "L3DATA",
91 .domains = domain_init(RDT_RESOURCE_L3DATA),
92 .msr_base = IA32_L3_CBM_BASE,
Thomas Gleixner0921c542017-04-14 14:14:31 +020093 .msr_update = cat_wrmsr,
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020094 .cache_level = 3,
95 .cache = {
96 .min_cbm_bits = 1,
97 .cbm_idx_mult = 2,
98 .cbm_idx_offset = 0,
99 },
Vikas Shivappac6ea67d2017-04-07 17:33:56 -0700100 .parse_ctrlval = parse_cbm,
101 .format_str = "%d=%0*x",
Tony luck5dc1d5c2017-07-25 14:14:29 -0700102 .fflags = RFTYPE_RES_CACHE,
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700103 },
Vikas Shivappadd131852017-07-25 14:14:26 -0700104 [RDT_RESOURCE_L3CODE] =
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700105 {
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200106 .name = "L3CODE",
107 .domains = domain_init(RDT_RESOURCE_L3CODE),
108 .msr_base = IA32_L3_CBM_BASE,
Thomas Gleixner0921c542017-04-14 14:14:31 +0200109 .msr_update = cat_wrmsr,
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200110 .cache_level = 3,
111 .cache = {
112 .min_cbm_bits = 1,
113 .cbm_idx_mult = 2,
114 .cbm_idx_offset = 1,
115 },
Vikas Shivappac6ea67d2017-04-07 17:33:56 -0700116 .parse_ctrlval = parse_cbm,
117 .format_str = "%d=%0*x",
Tony luck5dc1d5c2017-07-25 14:14:29 -0700118 .fflags = RFTYPE_RES_CACHE,
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700119 },
Vikas Shivappadd131852017-07-25 14:14:26 -0700120 [RDT_RESOURCE_L2] =
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700121 {
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200122 .name = "L2",
123 .domains = domain_init(RDT_RESOURCE_L2),
124 .msr_base = IA32_L2_CBM_BASE,
Thomas Gleixner0921c542017-04-14 14:14:31 +0200125 .msr_update = cat_wrmsr,
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200126 .cache_level = 2,
127 .cache = {
128 .min_cbm_bits = 1,
129 .cbm_idx_mult = 1,
130 .cbm_idx_offset = 0,
131 },
Vikas Shivappac6ea67d2017-04-07 17:33:56 -0700132 .parse_ctrlval = parse_cbm,
133 .format_str = "%d=%0*x",
Tony luck5dc1d5c2017-07-25 14:14:29 -0700134 .fflags = RFTYPE_RES_CACHE,
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700135 },
Vikas Shivappadd131852017-07-25 14:14:26 -0700136 [RDT_RESOURCE_MBA] =
Vikas Shivappa05b93412017-04-07 17:33:53 -0700137 {
138 .name = "MB",
139 .domains = domain_init(RDT_RESOURCE_MBA),
140 .msr_base = IA32_MBA_THRTL_BASE,
141 .msr_update = mba_wrmsr,
142 .cache_level = 3,
Vikas Shivappa64e8ed32017-04-07 17:33:57 -0700143 .parse_ctrlval = parse_bw,
144 .format_str = "%d=%*d",
Tony luck5dc1d5c2017-07-25 14:14:29 -0700145 .fflags = RFTYPE_RES_MB,
Vikas Shivappa05b93412017-04-07 17:33:53 -0700146 },
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700147};
148
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200149static unsigned int cbm_idx(struct rdt_resource *r, unsigned int closid)
Tony Luck2264d9c2016-10-28 15:04:41 -0700150{
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200151 return closid * r->cache.cbm_idx_mult + r->cache.cbm_idx_offset;
Tony Luck2264d9c2016-10-28 15:04:41 -0700152}
153
Fenghua Yu113c6092016-10-22 06:19:54 -0700154/*
155 * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
156 * as they do not have CPUID enumeration support for Cache allocation.
157 * The check for Vendor/Family/Model is not enough to guarantee that
158 * the MSRs won't #GP fault because only the following SKUs support
159 * CAT:
160 * Intel(R) Xeon(R) CPU E5-2658 v3 @ 2.20GHz
161 * Intel(R) Xeon(R) CPU E5-2648L v3 @ 1.80GHz
162 * Intel(R) Xeon(R) CPU E5-2628L v3 @ 2.00GHz
163 * Intel(R) Xeon(R) CPU E5-2618L v3 @ 2.30GHz
164 * Intel(R) Xeon(R) CPU E5-2608L v3 @ 2.00GHz
165 * Intel(R) Xeon(R) CPU E5-2658A v3 @ 2.20GHz
166 *
167 * Probe by trying to write the first of the L3 cach mask registers
168 * and checking that the bits stick. Max CLOSids is always 4 and max cbm length
169 * is always 20 on hsw server parts. The minimum cache bitmask length
170 * allowed for HSW server is always 2 bits. Hardcode all of them.
171 */
172static inline bool cache_alloc_hsw_probe(void)
173{
174 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
175 boot_cpu_data.x86 == 6 &&
176 boot_cpu_data.x86_model == INTEL_FAM6_HASWELL_X) {
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700177 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
Fenghua Yu113c6092016-10-22 06:19:54 -0700178 u32 l, h, max_cbm = BIT_MASK(20) - 1;
179
180 if (wrmsr_safe(IA32_L3_CBM_BASE, max_cbm, 0))
181 return false;
182 rdmsr(IA32_L3_CBM_BASE, l, h);
183
184 /* If all the bits were set in MSR, return success */
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700185 if (l != max_cbm)
186 return false;
187
188 r->num_closid = 4;
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700189 r->default_ctrl = max_cbm;
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200190 r->cache.cbm_len = 20;
191 r->cache.min_cbm_bits = 2;
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -0700192 r->alloc_capable = true;
193 r->alloc_enabled = true;
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700194
195 return true;
Fenghua Yu113c6092016-10-22 06:19:54 -0700196 }
197
198 return false;
199}
200
Vikas Shivappa05b93412017-04-07 17:33:53 -0700201/*
202 * rdt_get_mb_table() - get a mapping of bandwidth(b/w) percentage values
203 * exposed to user interface and the h/w understandable delay values.
204 *
205 * The non-linear delay values have the granularity of power of two
206 * and also the h/w does not guarantee a curve for configured delay
207 * values vs. actual b/w enforced.
208 * Hence we need a mapping that is pre calibrated so the user can
209 * express the memory b/w as a percentage value.
210 */
211static inline bool rdt_get_mb_table(struct rdt_resource *r)
212{
213 /*
214 * There are no Intel SKUs as of now to support non-linear delay.
215 */
216 pr_info("MBA b/w map not implemented for cpu:%d, model:%d",
217 boot_cpu_data.x86, boot_cpu_data.x86_model);
218
219 return false;
220}
221
222static bool rdt_get_mem_config(struct rdt_resource *r)
223{
224 union cpuid_0x10_3_eax eax;
225 union cpuid_0x10_x_edx edx;
226 u32 ebx, ecx;
227
228 cpuid_count(0x00000010, 3, &eax.full, &ebx, &ecx, &edx.full);
229 r->num_closid = edx.split.cos_max + 1;
230 r->membw.max_delay = eax.split.max_delay + 1;
231 r->default_ctrl = MAX_MBA_BW;
232 if (ecx & MBA_IS_LINEAR) {
233 r->membw.delay_linear = true;
234 r->membw.min_bw = MAX_MBA_BW - r->membw.max_delay;
235 r->membw.bw_gran = MAX_MBA_BW - r->membw.max_delay;
236 } else {
237 if (!rdt_get_mb_table(r))
238 return false;
239 }
240 r->data_width = 3;
241
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -0700242 r->alloc_capable = true;
243 r->alloc_enabled = true;
Vikas Shivappa05b93412017-04-07 17:33:53 -0700244
245 return true;
246}
247
Vikas Shivappa6a445ed2017-07-25 14:14:27 -0700248static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r)
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700249{
250 union cpuid_0x10_1_eax eax;
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700251 union cpuid_0x10_x_edx edx;
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700252 u32 ebx, ecx;
253
254 cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full);
255 r->num_closid = edx.split.cos_max + 1;
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200256 r->cache.cbm_len = eax.split.cbm_len + 1;
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700257 r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200258 r->data_width = (r->cache.cbm_len + 3) / 4;
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -0700259 r->alloc_capable = true;
260 r->alloc_enabled = true;
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700261}
262
263static void rdt_get_cdp_l3_config(int type)
264{
265 struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
266 struct rdt_resource *r = &rdt_resources_all[type];
267
268 r->num_closid = r_l3->num_closid / 2;
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200269 r->cache.cbm_len = r_l3->cache.cbm_len;
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700270 r->default_ctrl = r_l3->default_ctrl;
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200271 r->data_width = (r->cache.cbm_len + 3) / 4;
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -0700272 r->alloc_capable = true;
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700273 /*
274 * By default, CDP is disabled. CDP can be enabled by mount parameter
275 * "cdp" during resctrl file system mount time.
276 */
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -0700277 r->alloc_enabled = false;
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700278}
279
Tony Luck2264d9c2016-10-28 15:04:41 -0700280static int get_cache_id(int cpu, int level)
281{
282 struct cpu_cacheinfo *ci = get_cpu_cacheinfo(cpu);
283 int i;
284
285 for (i = 0; i < ci->num_leaves; i++) {
286 if (ci->info_list[i].level == level)
287 return ci->info_list[i].id;
288 }
289
290 return -1;
291}
292
Vikas Shivappa05b93412017-04-07 17:33:53 -0700293/*
294 * Map the memory b/w percentage value to delay values
295 * that can be written to QOS_MSRs.
296 * There are currently no SKUs which support non linear delay values.
297 */
298static u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)
299{
300 if (r->membw.delay_linear)
301 return MAX_MBA_BW - bw;
302
303 pr_warn_once("Non Linear delay-bw map not supported but queried\n");
304 return r->default_ctrl;
305}
306
307static void
308mba_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
309{
310 unsigned int i;
311
312 /* Write the delay values for mba. */
313 for (i = m->low; i < m->high; i++)
314 wrmsrl(r->msr_base + i, delay_bw_map(d->ctrl_val[i], r));
315}
316
Thomas Gleixner0921c542017-04-14 14:14:31 +0200317static void
318cat_wrmsr(struct rdt_domain *d, struct msr_param *m, struct rdt_resource *r)
319{
320 unsigned int i;
321
322 for (i = m->low; i < m->high; i++)
323 wrmsrl(r->msr_base + cbm_idx(r, i), d->ctrl_val[i]);
324}
325
Vikas Shivappaedf6fa12017-07-25 14:14:28 -0700326struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r)
327{
328 struct rdt_domain *d;
329
330 list_for_each_entry(d, &r->domains, list) {
331 /* Find the domain that contains this CPU */
332 if (cpumask_test_cpu(cpu, &d->cpu_mask))
333 return d;
334 }
335
336 return NULL;
337}
338
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700339void rdt_ctrl_update(void *arg)
Tony Luck2264d9c2016-10-28 15:04:41 -0700340{
Thomas Gleixner0921c542017-04-14 14:14:31 +0200341 struct msr_param *m = arg;
Tony Luck2264d9c2016-10-28 15:04:41 -0700342 struct rdt_resource *r = m->res;
Thomas Gleixner0921c542017-04-14 14:14:31 +0200343 int cpu = smp_processor_id();
Tony Luck2264d9c2016-10-28 15:04:41 -0700344 struct rdt_domain *d;
345
346 list_for_each_entry(d, &r->domains, list) {
347 /* Find the domain that contains this CPU */
Thomas Gleixner0921c542017-04-14 14:14:31 +0200348 if (cpumask_test_cpu(cpu, &d->cpu_mask)) {
349 r->msr_update(d, m, r);
350 return;
351 }
Tony Luck2264d9c2016-10-28 15:04:41 -0700352 }
Thomas Gleixner0921c542017-04-14 14:14:31 +0200353 pr_warn_once("cpu %d not found in any domain for resource %s\n",
Tony Luck2264d9c2016-10-28 15:04:41 -0700354 cpu, r->name);
Tony Luck2264d9c2016-10-28 15:04:41 -0700355}
356
357/*
358 * rdt_find_domain - Find a domain in a resource that matches input resource id
359 *
360 * Search resource r's domain list to find the resource id. If the resource
361 * id is found in a domain, return the domain. Otherwise, if requested by
362 * caller, return the first domain whose id is bigger than the input id.
363 * The domain list is sorted by id in ascending order.
364 */
365static struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
366 struct list_head **pos)
367{
368 struct rdt_domain *d;
369 struct list_head *l;
370
371 if (id < 0)
372 return ERR_PTR(id);
373
374 list_for_each(l, &r->domains) {
375 d = list_entry(l, struct rdt_domain, list);
376 /* When id is found, return its domain. */
377 if (id == d->id)
378 return d;
379 /* Stop searching when finding id's position in sorted list. */
380 if (id < d->id)
381 break;
382 }
383
384 if (pos)
385 *pos = l;
386
387 return NULL;
388}
389
Thomas Gleixner0921c542017-04-14 14:14:31 +0200390static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
391{
392 struct msr_param m;
393 u32 *dc;
394 int i;
395
396 dc = kmalloc_array(r->num_closid, sizeof(*d->ctrl_val), GFP_KERNEL);
397 if (!dc)
398 return -ENOMEM;
399
400 d->ctrl_val = dc;
401
402 /*
403 * Initialize the Control MSRs to having no control.
404 * For Cache Allocation: Set all bits in cbm
405 * For Memory Allocation: Set b/w requested to 100
406 */
407 for (i = 0; i < r->num_closid; i++, dc++)
408 *dc = r->default_ctrl;
409
410 m.low = 0;
411 m.high = r->num_closid;
412 r->msr_update(d, &m, r);
413 return 0;
414}
415
Vikas Shivappaedf6fa12017-07-25 14:14:28 -0700416static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
417{
418 if (is_llc_occupancy_enabled()) {
419 d->rmid_busy_llc = kcalloc(BITS_TO_LONGS(r->num_rmid),
420 sizeof(unsigned long),
421 GFP_KERNEL);
422 if (!d->rmid_busy_llc)
423 return -ENOMEM;
424 }
425
426 return 0;
427}
428
Tony Luck2264d9c2016-10-28 15:04:41 -0700429/*
430 * domain_add_cpu - Add a cpu to a resource's domain list.
431 *
432 * If an existing domain in the resource r's domain list matches the cpu's
433 * resource id, add the cpu in the domain.
434 *
435 * Otherwise, a new domain is allocated and inserted into the right position
436 * in the domain list sorted by id in ascending order.
437 *
438 * The order in the domain list is visible to users when we print entries
439 * in the schemata file and schemata input is validated to have the same order
440 * as this list.
441 */
442static void domain_add_cpu(int cpu, struct rdt_resource *r)
443{
Thomas Gleixner0921c542017-04-14 14:14:31 +0200444 int id = get_cache_id(cpu, r->cache_level);
Tony Luck2264d9c2016-10-28 15:04:41 -0700445 struct list_head *add_pos = NULL;
446 struct rdt_domain *d;
447
448 d = rdt_find_domain(r, id, &add_pos);
449 if (IS_ERR(d)) {
450 pr_warn("Could't find cache id for cpu %d\n", cpu);
451 return;
452 }
453
454 if (d) {
455 cpumask_set_cpu(cpu, &d->cpu_mask);
456 return;
457 }
458
459 d = kzalloc_node(sizeof(*d), GFP_KERNEL, cpu_to_node(cpu));
460 if (!d)
461 return;
462
463 d->id = id;
464
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -0700465 if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
Tony Luck2264d9c2016-10-28 15:04:41 -0700466 kfree(d);
467 return;
468 }
469
Vikas Shivappaedf6fa12017-07-25 14:14:28 -0700470 if (r->mon_capable && domain_setup_mon_state(r, d)) {
471 kfree(d);
472 return;
473 }
474
Tony Luck2264d9c2016-10-28 15:04:41 -0700475 cpumask_set_cpu(cpu, &d->cpu_mask);
476 list_add_tail(&d->list, add_pos);
Tony Luck2264d9c2016-10-28 15:04:41 -0700477}
478
479static void domain_remove_cpu(int cpu, struct rdt_resource *r)
480{
481 int id = get_cache_id(cpu, r->cache_level);
482 struct rdt_domain *d;
483
484 d = rdt_find_domain(r, id, NULL);
485 if (IS_ERR_OR_NULL(d)) {
486 pr_warn("Could't find cache id for cpu %d\n", cpu);
487 return;
488 }
489
490 cpumask_clear_cpu(cpu, &d->cpu_mask);
491 if (cpumask_empty(&d->cpu_mask)) {
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700492 kfree(d->ctrl_val);
Vikas Shivappaedf6fa12017-07-25 14:14:28 -0700493 kfree(d->rmid_busy_llc);
Tony Luck2264d9c2016-10-28 15:04:41 -0700494 list_del(&d->list);
495 kfree(d);
496 }
497}
498
Tony Luck12e01102016-10-28 15:04:45 -0700499static void clear_closid(int cpu)
Tony Luck2264d9c2016-10-28 15:04:41 -0700500{
501 struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
Tony Luck12e01102016-10-28 15:04:45 -0700502
Vikas Shivappab09d9812017-07-25 14:14:35 -0700503 per_cpu(rdt_cpu_default.closid, cpu) = 0;
Tony Luck12e01102016-10-28 15:04:45 -0700504 state->closid = 0;
Vikas Shivappa05830202017-07-25 14:14:23 -0700505 wrmsr(IA32_PQR_ASSOC, state->rmid, 0);
Tony Luck12e01102016-10-28 15:04:45 -0700506}
507
508static int intel_rdt_online_cpu(unsigned int cpu)
509{
Tony Luck2264d9c2016-10-28 15:04:41 -0700510 struct rdt_resource *r;
511
512 mutex_lock(&rdtgroup_mutex);
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -0700513 for_each_alloc_capable_rdt_resource(r)
Tony Luck2264d9c2016-10-28 15:04:41 -0700514 domain_add_cpu(cpu, r);
Tony Luck12e01102016-10-28 15:04:45 -0700515 /* The cpu is set in default rdtgroup after online. */
516 cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
517 clear_closid(cpu);
Tony Luck2264d9c2016-10-28 15:04:41 -0700518 mutex_unlock(&rdtgroup_mutex);
519
520 return 0;
521}
522
523static int intel_rdt_offline_cpu(unsigned int cpu)
Fenghua Yu78e99b42016-10-22 06:19:53 -0700524{
Tony Luck12e01102016-10-28 15:04:45 -0700525 struct rdtgroup *rdtgrp;
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700526 struct rdt_resource *r;
527
Tony Luck2264d9c2016-10-28 15:04:41 -0700528 mutex_lock(&rdtgroup_mutex);
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -0700529 for_each_alloc_capable_rdt_resource(r)
Tony Luck2264d9c2016-10-28 15:04:41 -0700530 domain_remove_cpu(cpu, r);
Tony Luck12e01102016-10-28 15:04:45 -0700531 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
532 if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask))
533 break;
534 }
535 clear_closid(cpu);
Tony Luck2264d9c2016-10-28 15:04:41 -0700536 mutex_unlock(&rdtgroup_mutex);
537
538 return 0;
539}
540
Thomas Gleixner70a1ee92017-04-14 14:07:47 +0200541/*
542 * Choose a width for the resource name and resource data based on the
543 * resource that has widest name and cbm.
544 */
545static __init void rdt_init_padding(void)
546{
547 struct rdt_resource *r;
548 int cl;
549
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -0700550 for_each_alloc_capable_rdt_resource(r) {
Thomas Gleixner70a1ee92017-04-14 14:07:47 +0200551 cl = strlen(r->name);
552 if (cl > max_name_width)
553 max_name_width = cl;
554
555 if (r->data_width > max_data_width)
556 max_data_width = r->data_width;
557 }
558}
559
Vikas Shivappa6a445ed2017-07-25 14:14:27 -0700560static __init bool get_rdt_alloc_resources(void)
Thomas Gleixner70a1ee92017-04-14 14:07:47 +0200561{
562 bool ret = false;
563
564 if (cache_alloc_hsw_probe())
565 return true;
566
567 if (!boot_cpu_has(X86_FEATURE_RDT_A))
568 return false;
569
570 if (boot_cpu_has(X86_FEATURE_CAT_L3)) {
Vikas Shivappa6a445ed2017-07-25 14:14:27 -0700571 rdt_get_cache_alloc_cfg(1, &rdt_resources_all[RDT_RESOURCE_L3]);
Thomas Gleixner70a1ee92017-04-14 14:07:47 +0200572 if (boot_cpu_has(X86_FEATURE_CDP_L3)) {
573 rdt_get_cdp_l3_config(RDT_RESOURCE_L3DATA);
574 rdt_get_cdp_l3_config(RDT_RESOURCE_L3CODE);
575 }
576 ret = true;
577 }
578 if (boot_cpu_has(X86_FEATURE_CAT_L2)) {
579 /* CPUID 0x10.2 fields are same format at 0x10.1 */
Vikas Shivappa6a445ed2017-07-25 14:14:27 -0700580 rdt_get_cache_alloc_cfg(2, &rdt_resources_all[RDT_RESOURCE_L2]);
Thomas Gleixner70a1ee92017-04-14 14:07:47 +0200581 ret = true;
582 }
Vikas Shivappaab66a332017-04-07 17:33:52 -0700583
Vikas Shivappa05b93412017-04-07 17:33:53 -0700584 if (boot_cpu_has(X86_FEATURE_MBA)) {
585 if (rdt_get_mem_config(&rdt_resources_all[RDT_RESOURCE_MBA]))
586 ret = true;
587 }
Thomas Gleixner70a1ee92017-04-14 14:07:47 +0200588 return ret;
589}
590
Vikas Shivappa6a445ed2017-07-25 14:14:27 -0700591static __init bool get_rdt_mon_resources(void)
592{
593 if (boot_cpu_has(X86_FEATURE_CQM_OCCUP_LLC))
594 rdt_mon_features |= (1 << QOS_L3_OCCUP_EVENT_ID);
595 if (boot_cpu_has(X86_FEATURE_CQM_MBM_TOTAL))
596 rdt_mon_features |= (1 << QOS_L3_MBM_TOTAL_EVENT_ID);
597 if (boot_cpu_has(X86_FEATURE_CQM_MBM_LOCAL))
598 rdt_mon_features |= (1 << QOS_L3_MBM_LOCAL_EVENT_ID);
599
600 if (!rdt_mon_features)
601 return false;
602
603 return !rdt_get_mon_l3_config(&rdt_resources_all[RDT_RESOURCE_L3]);
604}
605
606static __init bool get_rdt_resources(void)
607{
608 rdt_alloc_capable = get_rdt_alloc_resources();
609 rdt_mon_capable = get_rdt_mon_resources();
610
611 return (rdt_mon_capable || rdt_alloc_capable);
612}
613
Tony Luck2264d9c2016-10-28 15:04:41 -0700614static int __init intel_rdt_late_init(void)
615{
616 struct rdt_resource *r;
Fenghua Yu5ff193f2016-10-28 15:04:42 -0700617 int state, ret;
Tony Luck2264d9c2016-10-28 15:04:41 -0700618
Fenghua Yu78e99b42016-10-22 06:19:53 -0700619 if (!get_rdt_resources())
620 return -ENODEV;
621
Thomas Gleixner06b57e42017-04-14 14:06:26 +0200622 rdt_init_padding();
623
Tony Luck2264d9c2016-10-28 15:04:41 -0700624 state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
625 "x86/rdt/cat:online:",
626 intel_rdt_online_cpu, intel_rdt_offline_cpu);
627 if (state < 0)
628 return state;
629
Fenghua Yu5ff193f2016-10-28 15:04:42 -0700630 ret = rdtgroup_init();
631 if (ret) {
632 cpuhp_remove_state(state);
633 return ret;
634 }
635
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -0700636 for_each_alloc_capable_rdt_resource(r)
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700637 pr_info("Intel RDT %s allocation detected\n", r->name);
Fenghua Yu78e99b42016-10-22 06:19:53 -0700638
Vikas Shivappa6a445ed2017-07-25 14:14:27 -0700639 for_each_mon_capable_rdt_resource(r)
640 pr_info("Intel RDT %s monitoring detected\n", r->name);
641
Fenghua Yu78e99b42016-10-22 06:19:53 -0700642 return 0;
643}
644
645late_initcall(intel_rdt_late_init);