blob: 8d7e050567c27cd02b8db61e8159a7a9362f6ee5 [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>
33#include <asm/intel_rdt.h>
34
Tony Luck2264d9c2016-10-28 15:04:41 -070035/* Mutex to protect rdtgroup access. */
36DEFINE_MUTEX(rdtgroup_mutex);
37
Tony Luck12e01102016-10-28 15:04:45 -070038DEFINE_PER_CPU_READ_MOSTLY(int, cpu_closid);
39
Vikas Shivappade016df2017-04-03 14:44:17 -070040/*
41 * Used to store the max resource name width and max resource data width
42 * to display the schemata in a tabular format
43 */
44int max_name_width, max_data_width;
45
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020046#define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].domains)
47
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -070048struct rdt_resource rdt_resources_all[] = {
49 {
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020050 .name = "L3",
51 .domains = domain_init(RDT_RESOURCE_L3),
52 .msr_base = IA32_L3_CBM_BASE,
53 .cache_level = 3,
54 .cache = {
55 .min_cbm_bits = 1,
56 .cbm_idx_mult = 1,
57 .cbm_idx_offset = 0,
58 },
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -070059 },
60 {
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020061 .name = "L3DATA",
62 .domains = domain_init(RDT_RESOURCE_L3DATA),
63 .msr_base = IA32_L3_CBM_BASE,
64 .cache_level = 3,
65 .cache = {
66 .min_cbm_bits = 1,
67 .cbm_idx_mult = 2,
68 .cbm_idx_offset = 0,
69 },
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -070070 },
71 {
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020072 .name = "L3CODE",
73 .domains = domain_init(RDT_RESOURCE_L3CODE),
74 .msr_base = IA32_L3_CBM_BASE,
75 .cache_level = 3,
76 .cache = {
77 .min_cbm_bits = 1,
78 .cbm_idx_mult = 2,
79 .cbm_idx_offset = 1,
80 },
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -070081 },
82 {
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020083 .name = "L2",
84 .domains = domain_init(RDT_RESOURCE_L2),
85 .msr_base = IA32_L2_CBM_BASE,
86 .cache_level = 2,
87 .cache = {
88 .min_cbm_bits = 1,
89 .cbm_idx_mult = 1,
90 .cbm_idx_offset = 0,
91 },
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -070092 },
93};
94
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020095static unsigned int cbm_idx(struct rdt_resource *r, unsigned int closid)
Tony Luck2264d9c2016-10-28 15:04:41 -070096{
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +020097 return closid * r->cache.cbm_idx_mult + r->cache.cbm_idx_offset;
Tony Luck2264d9c2016-10-28 15:04:41 -070098}
99
Fenghua Yu113c6092016-10-22 06:19:54 -0700100/*
101 * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
102 * as they do not have CPUID enumeration support for Cache allocation.
103 * The check for Vendor/Family/Model is not enough to guarantee that
104 * the MSRs won't #GP fault because only the following SKUs support
105 * CAT:
106 * Intel(R) Xeon(R) CPU E5-2658 v3 @ 2.20GHz
107 * Intel(R) Xeon(R) CPU E5-2648L v3 @ 1.80GHz
108 * Intel(R) Xeon(R) CPU E5-2628L v3 @ 2.00GHz
109 * Intel(R) Xeon(R) CPU E5-2618L v3 @ 2.30GHz
110 * Intel(R) Xeon(R) CPU E5-2608L v3 @ 2.00GHz
111 * Intel(R) Xeon(R) CPU E5-2658A v3 @ 2.20GHz
112 *
113 * Probe by trying to write the first of the L3 cach mask registers
114 * and checking that the bits stick. Max CLOSids is always 4 and max cbm length
115 * is always 20 on hsw server parts. The minimum cache bitmask length
116 * allowed for HSW server is always 2 bits. Hardcode all of them.
117 */
118static inline bool cache_alloc_hsw_probe(void)
119{
120 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
121 boot_cpu_data.x86 == 6 &&
122 boot_cpu_data.x86_model == INTEL_FAM6_HASWELL_X) {
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700123 struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3];
Fenghua Yu113c6092016-10-22 06:19:54 -0700124 u32 l, h, max_cbm = BIT_MASK(20) - 1;
125
126 if (wrmsr_safe(IA32_L3_CBM_BASE, max_cbm, 0))
127 return false;
128 rdmsr(IA32_L3_CBM_BASE, l, h);
129
130 /* If all the bits were set in MSR, return success */
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700131 if (l != max_cbm)
132 return false;
133
134 r->num_closid = 4;
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700135 r->default_ctrl = max_cbm;
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200136 r->cache.cbm_len = 20;
137 r->cache.min_cbm_bits = 2;
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700138 r->capable = true;
139 r->enabled = true;
140
141 return true;
Fenghua Yu113c6092016-10-22 06:19:54 -0700142 }
143
144 return false;
145}
146
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700147static void rdt_get_cache_config(int idx, struct rdt_resource *r)
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700148{
149 union cpuid_0x10_1_eax eax;
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700150 union cpuid_0x10_x_edx edx;
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700151 u32 ebx, ecx;
152
153 cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full);
154 r->num_closid = edx.split.cos_max + 1;
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200155 r->cache.cbm_len = eax.split.cbm_len + 1;
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700156 r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200157 r->data_width = (r->cache.cbm_len + 3) / 4;
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700158 r->capable = true;
159 r->enabled = true;
160}
161
162static void rdt_get_cdp_l3_config(int type)
163{
164 struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
165 struct rdt_resource *r = &rdt_resources_all[type];
166
167 r->num_closid = r_l3->num_closid / 2;
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200168 r->cache.cbm_len = r_l3->cache.cbm_len;
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700169 r->default_ctrl = r_l3->default_ctrl;
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200170 r->data_width = (r->cache.cbm_len + 3) / 4;
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700171 r->capable = true;
172 /*
173 * By default, CDP is disabled. CDP can be enabled by mount parameter
174 * "cdp" during resctrl file system mount time.
175 */
176 r->enabled = false;
177}
178
Tony Luck2264d9c2016-10-28 15:04:41 -0700179static int get_cache_id(int cpu, int level)
180{
181 struct cpu_cacheinfo *ci = get_cpu_cacheinfo(cpu);
182 int i;
183
184 for (i = 0; i < ci->num_leaves; i++) {
185 if (ci->info_list[i].level == level)
186 return ci->info_list[i].id;
187 }
188
189 return -1;
190}
191
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700192void rdt_ctrl_update(void *arg)
Tony Luck2264d9c2016-10-28 15:04:41 -0700193{
194 struct msr_param *m = (struct msr_param *)arg;
195 struct rdt_resource *r = m->res;
196 int i, cpu = smp_processor_id();
197 struct rdt_domain *d;
198
199 list_for_each_entry(d, &r->domains, list) {
200 /* Find the domain that contains this CPU */
201 if (cpumask_test_cpu(cpu, &d->cpu_mask))
202 goto found;
203 }
204 pr_info_once("cpu %d not found in any domain for resource %s\n",
205 cpu, r->name);
206
207 return;
208
209found:
210 for (i = m->low; i < m->high; i++) {
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200211 unsigned int idx = cbm_idx(r, i);
Tony Luck2264d9c2016-10-28 15:04:41 -0700212
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700213 wrmsrl(r->msr_base + idx, d->ctrl_val[i]);
Tony Luck2264d9c2016-10-28 15:04:41 -0700214 }
215}
216
217/*
218 * rdt_find_domain - Find a domain in a resource that matches input resource id
219 *
220 * Search resource r's domain list to find the resource id. If the resource
221 * id is found in a domain, return the domain. Otherwise, if requested by
222 * caller, return the first domain whose id is bigger than the input id.
223 * The domain list is sorted by id in ascending order.
224 */
225static struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
226 struct list_head **pos)
227{
228 struct rdt_domain *d;
229 struct list_head *l;
230
231 if (id < 0)
232 return ERR_PTR(id);
233
234 list_for_each(l, &r->domains) {
235 d = list_entry(l, struct rdt_domain, list);
236 /* When id is found, return its domain. */
237 if (id == d->id)
238 return d;
239 /* Stop searching when finding id's position in sorted list. */
240 if (id < d->id)
241 break;
242 }
243
244 if (pos)
245 *pos = l;
246
247 return NULL;
248}
249
250/*
251 * domain_add_cpu - Add a cpu to a resource's domain list.
252 *
253 * If an existing domain in the resource r's domain list matches the cpu's
254 * resource id, add the cpu in the domain.
255 *
256 * Otherwise, a new domain is allocated and inserted into the right position
257 * in the domain list sorted by id in ascending order.
258 *
259 * The order in the domain list is visible to users when we print entries
260 * in the schemata file and schemata input is validated to have the same order
261 * as this list.
262 */
263static void domain_add_cpu(int cpu, struct rdt_resource *r)
264{
265 int i, id = get_cache_id(cpu, r->cache_level);
266 struct list_head *add_pos = NULL;
267 struct rdt_domain *d;
268
269 d = rdt_find_domain(r, id, &add_pos);
270 if (IS_ERR(d)) {
271 pr_warn("Could't find cache id for cpu %d\n", cpu);
272 return;
273 }
274
275 if (d) {
276 cpumask_set_cpu(cpu, &d->cpu_mask);
277 return;
278 }
279
280 d = kzalloc_node(sizeof(*d), GFP_KERNEL, cpu_to_node(cpu));
281 if (!d)
282 return;
283
284 d->id = id;
285
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700286 d->ctrl_val = kmalloc_array(r->num_closid, sizeof(*d->ctrl_val), GFP_KERNEL);
287 if (!d->ctrl_val) {
Tony Luck2264d9c2016-10-28 15:04:41 -0700288 kfree(d);
289 return;
290 }
291
292 for (i = 0; i < r->num_closid; i++) {
Thomas Gleixnerd3e11b42017-04-14 13:00:36 +0200293 unsigned int idx = cbm_idx(r, i);
Tony Luck2264d9c2016-10-28 15:04:41 -0700294
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700295 d->ctrl_val[i] = r->default_ctrl;
296 wrmsrl(r->msr_base + idx, d->ctrl_val[i]);
Tony Luck2264d9c2016-10-28 15:04:41 -0700297 }
298
299 cpumask_set_cpu(cpu, &d->cpu_mask);
300 list_add_tail(&d->list, add_pos);
Tony Luck2264d9c2016-10-28 15:04:41 -0700301}
302
303static void domain_remove_cpu(int cpu, struct rdt_resource *r)
304{
305 int id = get_cache_id(cpu, r->cache_level);
306 struct rdt_domain *d;
307
308 d = rdt_find_domain(r, id, NULL);
309 if (IS_ERR_OR_NULL(d)) {
310 pr_warn("Could't find cache id for cpu %d\n", cpu);
311 return;
312 }
313
314 cpumask_clear_cpu(cpu, &d->cpu_mask);
315 if (cpumask_empty(&d->cpu_mask)) {
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700316 kfree(d->ctrl_val);
Tony Luck2264d9c2016-10-28 15:04:41 -0700317 list_del(&d->list);
318 kfree(d);
319 }
320}
321
Tony Luck12e01102016-10-28 15:04:45 -0700322static void clear_closid(int cpu)
Tony Luck2264d9c2016-10-28 15:04:41 -0700323{
324 struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
Tony Luck12e01102016-10-28 15:04:45 -0700325
326 per_cpu(cpu_closid, cpu) = 0;
327 state->closid = 0;
328 wrmsr(MSR_IA32_PQR_ASSOC, state->rmid, 0);
329}
330
331static int intel_rdt_online_cpu(unsigned int cpu)
332{
Tony Luck2264d9c2016-10-28 15:04:41 -0700333 struct rdt_resource *r;
334
335 mutex_lock(&rdtgroup_mutex);
336 for_each_capable_rdt_resource(r)
337 domain_add_cpu(cpu, r);
Tony Luck12e01102016-10-28 15:04:45 -0700338 /* The cpu is set in default rdtgroup after online. */
339 cpumask_set_cpu(cpu, &rdtgroup_default.cpu_mask);
340 clear_closid(cpu);
Tony Luck2264d9c2016-10-28 15:04:41 -0700341 mutex_unlock(&rdtgroup_mutex);
342
343 return 0;
344}
345
346static int intel_rdt_offline_cpu(unsigned int cpu)
Fenghua Yu78e99b42016-10-22 06:19:53 -0700347{
Tony Luck12e01102016-10-28 15:04:45 -0700348 struct rdtgroup *rdtgrp;
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700349 struct rdt_resource *r;
350
Tony Luck2264d9c2016-10-28 15:04:41 -0700351 mutex_lock(&rdtgroup_mutex);
352 for_each_capable_rdt_resource(r)
353 domain_remove_cpu(cpu, r);
Tony Luck12e01102016-10-28 15:04:45 -0700354 list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
355 if (cpumask_test_and_clear_cpu(cpu, &rdtgrp->cpu_mask))
356 break;
357 }
358 clear_closid(cpu);
Tony Luck2264d9c2016-10-28 15:04:41 -0700359 mutex_unlock(&rdtgroup_mutex);
360
361 return 0;
362}
363
Thomas Gleixner70a1ee92017-04-14 14:07:47 +0200364/*
365 * Choose a width for the resource name and resource data based on the
366 * resource that has widest name and cbm.
367 */
368static __init void rdt_init_padding(void)
369{
370 struct rdt_resource *r;
371 int cl;
372
373 for_each_enabled_rdt_resource(r) {
374 cl = strlen(r->name);
375 if (cl > max_name_width)
376 max_name_width = cl;
377
378 if (r->data_width > max_data_width)
379 max_data_width = r->data_width;
380 }
381}
382
383static __init bool get_rdt_resources(void)
384{
385 bool ret = false;
386
387 if (cache_alloc_hsw_probe())
388 return true;
389
390 if (!boot_cpu_has(X86_FEATURE_RDT_A))
391 return false;
392
393 if (boot_cpu_has(X86_FEATURE_CAT_L3)) {
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700394 rdt_get_cache_config(1, &rdt_resources_all[RDT_RESOURCE_L3]);
Thomas Gleixner70a1ee92017-04-14 14:07:47 +0200395 if (boot_cpu_has(X86_FEATURE_CDP_L3)) {
396 rdt_get_cdp_l3_config(RDT_RESOURCE_L3DATA);
397 rdt_get_cdp_l3_config(RDT_RESOURCE_L3CODE);
398 }
399 ret = true;
400 }
401 if (boot_cpu_has(X86_FEATURE_CAT_L2)) {
402 /* CPUID 0x10.2 fields are same format at 0x10.1 */
Vikas Shivappa2545e9f2017-04-07 17:33:51 -0700403 rdt_get_cache_config(2, &rdt_resources_all[RDT_RESOURCE_L2]);
Thomas Gleixner70a1ee92017-04-14 14:07:47 +0200404 ret = true;
405 }
406 return ret;
407}
408
Tony Luck2264d9c2016-10-28 15:04:41 -0700409static int __init intel_rdt_late_init(void)
410{
411 struct rdt_resource *r;
Fenghua Yu5ff193f2016-10-28 15:04:42 -0700412 int state, ret;
Tony Luck2264d9c2016-10-28 15:04:41 -0700413
Fenghua Yu78e99b42016-10-22 06:19:53 -0700414 if (!get_rdt_resources())
415 return -ENODEV;
416
Thomas Gleixner06b57e42017-04-14 14:06:26 +0200417 rdt_init_padding();
418
Tony Luck2264d9c2016-10-28 15:04:41 -0700419 state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
420 "x86/rdt/cat:online:",
421 intel_rdt_online_cpu, intel_rdt_offline_cpu);
422 if (state < 0)
423 return state;
424
Fenghua Yu5ff193f2016-10-28 15:04:42 -0700425 ret = rdtgroup_init();
426 if (ret) {
427 cpuhp_remove_state(state);
428 return ret;
429 }
430
Fenghua Yuc1c7c3f2016-10-22 06:19:55 -0700431 for_each_capable_rdt_resource(r)
432 pr_info("Intel RDT %s allocation detected\n", r->name);
Fenghua Yu78e99b42016-10-22 06:19:53 -0700433
434 return 0;
435}
436
437late_initcall(intel_rdt_late_init);