3d4b397a1181f765aa4878466a0bfa542e1cae69
[linux-2.6-microblaze.git] / arch / x86 / kernel / cpu / intel_rdt.c
1 /*
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>
29 #include <linux/cacheinfo.h>
30 #include <linux/cpuhotplug.h>
31
32 #include <asm/intel_rdt_common.h>
33 #include <asm/intel-family.h>
34 #include <asm/intel_rdt.h>
35
36 /* Mutex to protect rdtgroup access. */
37 DEFINE_MUTEX(rdtgroup_mutex);
38
39 #define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].domains)
40
41 struct rdt_resource rdt_resources_all[] = {
42         {
43                 .name           = "L3",
44                 .domains        = domain_init(RDT_RESOURCE_L3),
45                 .msr_base       = IA32_L3_CBM_BASE,
46                 .min_cbm_bits   = 1,
47                 .cache_level    = 3,
48                 .cbm_idx_multi  = 1,
49                 .cbm_idx_offset = 0
50         },
51         {
52                 .name           = "L3DATA",
53                 .domains        = domain_init(RDT_RESOURCE_L3DATA),
54                 .msr_base       = IA32_L3_CBM_BASE,
55                 .min_cbm_bits   = 1,
56                 .cache_level    = 3,
57                 .cbm_idx_multi  = 2,
58                 .cbm_idx_offset = 0
59         },
60         {
61                 .name           = "L3CODE",
62                 .domains        = domain_init(RDT_RESOURCE_L3CODE),
63                 .msr_base       = IA32_L3_CBM_BASE,
64                 .min_cbm_bits   = 1,
65                 .cache_level    = 3,
66                 .cbm_idx_multi  = 2,
67                 .cbm_idx_offset = 1
68         },
69         {
70                 .name           = "L2",
71                 .domains        = domain_init(RDT_RESOURCE_L2),
72                 .msr_base       = IA32_L2_CBM_BASE,
73                 .min_cbm_bits   = 1,
74                 .cache_level    = 2,
75                 .cbm_idx_multi  = 1,
76                 .cbm_idx_offset = 0
77         },
78 };
79
80 static int cbm_idx(struct rdt_resource *r, int closid)
81 {
82         return closid * r->cbm_idx_multi + r->cbm_idx_offset;
83 }
84
85 /*
86  * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
87  * as they do not have CPUID enumeration support for Cache allocation.
88  * The check for Vendor/Family/Model is not enough to guarantee that
89  * the MSRs won't #GP fault because only the following SKUs support
90  * CAT:
91  *      Intel(R) Xeon(R)  CPU E5-2658  v3  @  2.20GHz
92  *      Intel(R) Xeon(R)  CPU E5-2648L v3  @  1.80GHz
93  *      Intel(R) Xeon(R)  CPU E5-2628L v3  @  2.00GHz
94  *      Intel(R) Xeon(R)  CPU E5-2618L v3  @  2.30GHz
95  *      Intel(R) Xeon(R)  CPU E5-2608L v3  @  2.00GHz
96  *      Intel(R) Xeon(R)  CPU E5-2658A v3  @  2.20GHz
97  *
98  * Probe by trying to write the first of the L3 cach mask registers
99  * and checking that the bits stick. Max CLOSids is always 4 and max cbm length
100  * is always 20 on hsw server parts. The minimum cache bitmask length
101  * allowed for HSW server is always 2 bits. Hardcode all of them.
102  */
103 static inline bool cache_alloc_hsw_probe(void)
104 {
105         if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
106             boot_cpu_data.x86 == 6 &&
107             boot_cpu_data.x86_model == INTEL_FAM6_HASWELL_X) {
108                 struct rdt_resource *r  = &rdt_resources_all[RDT_RESOURCE_L3];
109                 u32 l, h, max_cbm = BIT_MASK(20) - 1;
110
111                 if (wrmsr_safe(IA32_L3_CBM_BASE, max_cbm, 0))
112                         return false;
113                 rdmsr(IA32_L3_CBM_BASE, l, h);
114
115                 /* If all the bits were set in MSR, return success */
116                 if (l != max_cbm)
117                         return false;
118
119                 r->num_closid = 4;
120                 r->cbm_len = 20;
121                 r->max_cbm = max_cbm;
122                 r->min_cbm_bits = 2;
123                 r->capable = true;
124                 r->enabled = true;
125
126                 return true;
127         }
128
129         return false;
130 }
131
132 static void rdt_get_config(int idx, struct rdt_resource *r)
133 {
134         union cpuid_0x10_1_eax eax;
135         union cpuid_0x10_1_edx edx;
136         u32 ebx, ecx;
137
138         cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx, &edx.full);
139         r->num_closid = edx.split.cos_max + 1;
140         r->cbm_len = eax.split.cbm_len + 1;
141         r->max_cbm = BIT_MASK(eax.split.cbm_len + 1) - 1;
142         r->capable = true;
143         r->enabled = true;
144 }
145
146 static void rdt_get_cdp_l3_config(int type)
147 {
148         struct rdt_resource *r_l3 = &rdt_resources_all[RDT_RESOURCE_L3];
149         struct rdt_resource *r = &rdt_resources_all[type];
150
151         r->num_closid = r_l3->num_closid / 2;
152         r->cbm_len = r_l3->cbm_len;
153         r->max_cbm = r_l3->max_cbm;
154         r->capable = true;
155         /*
156          * By default, CDP is disabled. CDP can be enabled by mount parameter
157          * "cdp" during resctrl file system mount time.
158          */
159         r->enabled = false;
160 }
161
162 static inline bool get_rdt_resources(void)
163 {
164         bool ret = false;
165
166         if (cache_alloc_hsw_probe())
167                 return true;
168
169         if (!boot_cpu_has(X86_FEATURE_RDT_A))
170                 return false;
171
172         if (boot_cpu_has(X86_FEATURE_CAT_L3)) {
173                 rdt_get_config(1, &rdt_resources_all[RDT_RESOURCE_L3]);
174                 if (boot_cpu_has(X86_FEATURE_CDP_L3)) {
175                         rdt_get_cdp_l3_config(RDT_RESOURCE_L3DATA);
176                         rdt_get_cdp_l3_config(RDT_RESOURCE_L3CODE);
177                 }
178                 ret = true;
179         }
180         if (boot_cpu_has(X86_FEATURE_CAT_L2)) {
181                 /* CPUID 0x10.2 fields are same format at 0x10.1 */
182                 rdt_get_config(2, &rdt_resources_all[RDT_RESOURCE_L2]);
183                 ret = true;
184         }
185
186         return ret;
187 }
188
189 static int get_cache_id(int cpu, int level)
190 {
191         struct cpu_cacheinfo *ci = get_cpu_cacheinfo(cpu);
192         int i;
193
194         for (i = 0; i < ci->num_leaves; i++) {
195                 if (ci->info_list[i].level == level)
196                         return ci->info_list[i].id;
197         }
198
199         return -1;
200 }
201
202 void rdt_cbm_update(void *arg)
203 {
204         struct msr_param *m = (struct msr_param *)arg;
205         struct rdt_resource *r = m->res;
206         int i, cpu = smp_processor_id();
207         struct rdt_domain *d;
208
209         list_for_each_entry(d, &r->domains, list) {
210                 /* Find the domain that contains this CPU */
211                 if (cpumask_test_cpu(cpu, &d->cpu_mask))
212                         goto found;
213         }
214         pr_info_once("cpu %d not found in any domain for resource %s\n",
215                      cpu, r->name);
216
217         return;
218
219 found:
220         for (i = m->low; i < m->high; i++) {
221                 int idx = cbm_idx(r, i);
222
223                 wrmsrl(r->msr_base + idx, d->cbm[i]);
224         }
225 }
226
227 /*
228  * rdt_find_domain - Find a domain in a resource that matches input resource id
229  *
230  * Search resource r's domain list to find the resource id. If the resource
231  * id is found in a domain, return the domain. Otherwise, if requested by
232  * caller, return the first domain whose id is bigger than the input id.
233  * The domain list is sorted by id in ascending order.
234  */
235 static struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
236                                           struct list_head **pos)
237 {
238         struct rdt_domain *d;
239         struct list_head *l;
240
241         if (id < 0)
242                 return ERR_PTR(id);
243
244         list_for_each(l, &r->domains) {
245                 d = list_entry(l, struct rdt_domain, list);
246                 /* When id is found, return its domain. */
247                 if (id == d->id)
248                         return d;
249                 /* Stop searching when finding id's position in sorted list. */
250                 if (id < d->id)
251                         break;
252         }
253
254         if (pos)
255                 *pos = l;
256
257         return NULL;
258 }
259
260 /*
261  * domain_add_cpu - Add a cpu to a resource's domain list.
262  *
263  * If an existing domain in the resource r's domain list matches the cpu's
264  * resource id, add the cpu in the domain.
265  *
266  * Otherwise, a new domain is allocated and inserted into the right position
267  * in the domain list sorted by id in ascending order.
268  *
269  * The order in the domain list is visible to users when we print entries
270  * in the schemata file and schemata input is validated to have the same order
271  * as this list.
272  */
273 static void domain_add_cpu(int cpu, struct rdt_resource *r)
274 {
275         int i, id = get_cache_id(cpu, r->cache_level);
276         struct list_head *add_pos = NULL;
277         struct rdt_domain *d;
278
279         d = rdt_find_domain(r, id, &add_pos);
280         if (IS_ERR(d)) {
281                 pr_warn("Could't find cache id for cpu %d\n", cpu);
282                 return;
283         }
284
285         if (d) {
286                 cpumask_set_cpu(cpu, &d->cpu_mask);
287                 return;
288         }
289
290         d = kzalloc_node(sizeof(*d), GFP_KERNEL, cpu_to_node(cpu));
291         if (!d)
292                 return;
293
294         d->id = id;
295
296         d->cbm = kmalloc_array(r->num_closid, sizeof(*d->cbm), GFP_KERNEL);
297         if (!d->cbm) {
298                 kfree(d);
299                 return;
300         }
301
302         for (i = 0; i < r->num_closid; i++) {
303                 int idx = cbm_idx(r, i);
304
305                 d->cbm[i] = r->max_cbm;
306                 wrmsrl(r->msr_base + idx, d->cbm[i]);
307         }
308
309         cpumask_set_cpu(cpu, &d->cpu_mask);
310         list_add_tail(&d->list, add_pos);
311         r->num_domains++;
312 }
313
314 static void domain_remove_cpu(int cpu, struct rdt_resource *r)
315 {
316         int id = get_cache_id(cpu, r->cache_level);
317         struct rdt_domain *d;
318
319         d = rdt_find_domain(r, id, NULL);
320         if (IS_ERR_OR_NULL(d)) {
321                 pr_warn("Could't find cache id for cpu %d\n", cpu);
322                 return;
323         }
324
325         cpumask_clear_cpu(cpu, &d->cpu_mask);
326         if (cpumask_empty(&d->cpu_mask)) {
327                 r->num_domains--;
328                 kfree(d->cbm);
329                 list_del(&d->list);
330                 kfree(d);
331         }
332 }
333
334 static int intel_rdt_online_cpu(unsigned int cpu)
335 {
336         struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
337         struct rdt_resource *r;
338
339         mutex_lock(&rdtgroup_mutex);
340         for_each_capable_rdt_resource(r)
341                 domain_add_cpu(cpu, r);
342         state->closid = 0;
343         wrmsr(MSR_IA32_PQR_ASSOC, state->rmid, 0);
344         mutex_unlock(&rdtgroup_mutex);
345
346         return 0;
347 }
348
349 static int intel_rdt_offline_cpu(unsigned int cpu)
350 {
351         struct rdt_resource *r;
352
353         mutex_lock(&rdtgroup_mutex);
354         for_each_capable_rdt_resource(r)
355                 domain_remove_cpu(cpu, r);
356         mutex_unlock(&rdtgroup_mutex);
357
358         return 0;
359 }
360
361 static int __init intel_rdt_late_init(void)
362 {
363         struct rdt_resource *r;
364         int state;
365
366         if (!get_rdt_resources())
367                 return -ENODEV;
368
369         state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
370                                   "x86/rdt/cat:online:",
371                                   intel_rdt_online_cpu, intel_rdt_offline_cpu);
372         if (state < 0)
373                 return state;
374
375         for_each_capable_rdt_resource(r)
376                 pr_info("Intel RDT %s allocation detected\n", r->name);
377
378         return 0;
379 }
380
381 late_initcall(intel_rdt_late_init);