3f0c33d5b65862f0e3f8573f72a8dead1dd1511e
[linux-2.6-microblaze.git] / arch / x86 / kernel / cpu / resctrl / ctrlmondata.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  *
12  * More information about RDT be found in the Intel (R) x86 Architecture
13  * Software Developer Manual June 2016, volume 3, section 17.17.
14  */
15
16 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
17
18 #include <linux/cpu.h>
19 #include <linux/kernfs.h>
20 #include <linux/seq_file.h>
21 #include <linux/slab.h>
22 #include "internal.h"
23
24 /*
25  * Check whether MBA bandwidth percentage value is correct. The value is
26  * checked against the minimum and max bandwidth values specified by the
27  * hardware. The allocated bandwidth percentage is rounded to the next
28  * control step available on the hardware.
29  */
30 static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
31 {
32         unsigned long bw;
33         int ret;
34
35         /*
36          * Only linear delay values is supported for current Intel SKUs.
37          */
38         if (!r->membw.delay_linear && r->membw.arch_needs_linear) {
39                 rdt_last_cmd_puts("No support for non-linear MB domains\n");
40                 return false;
41         }
42
43         ret = kstrtoul(buf, 10, &bw);
44         if (ret) {
45                 rdt_last_cmd_printf("Non-decimal digit in MB value %s\n", buf);
46                 return false;
47         }
48
49         if ((bw < r->membw.min_bw || bw > r->default_ctrl) &&
50             !is_mba_sc(r)) {
51                 rdt_last_cmd_printf("MB value %ld out of range [%d,%d]\n", bw,
52                                     r->membw.min_bw, r->default_ctrl);
53                 return false;
54         }
55
56         *data = roundup(bw, (unsigned long)r->membw.bw_gran);
57         return true;
58 }
59
60 int parse_bw(struct rdt_parse_data *data, struct rdt_resource *r,
61              struct rdt_domain *d)
62 {
63         unsigned long bw_val;
64
65         if (d->have_new_ctrl) {
66                 rdt_last_cmd_printf("Duplicate domain %d\n", d->id);
67                 return -EINVAL;
68         }
69
70         if (!bw_validate(data->buf, &bw_val, r))
71                 return -EINVAL;
72         d->new_ctrl = bw_val;
73         d->have_new_ctrl = true;
74
75         return 0;
76 }
77
78 /*
79  * Check whether a cache bit mask is valid.
80  * For Intel the SDM says:
81  *      Please note that all (and only) contiguous '1' combinations
82  *      are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
83  * Additionally Haswell requires at least two bits set.
84  * AMD allows non-contiguous bitmasks.
85  */
86 static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
87 {
88         unsigned long first_bit, zero_bit, val;
89         unsigned int cbm_len = r->cache.cbm_len;
90         int ret;
91
92         ret = kstrtoul(buf, 16, &val);
93         if (ret) {
94                 rdt_last_cmd_printf("Non-hex character in the mask %s\n", buf);
95                 return false;
96         }
97
98         if ((!r->cache.arch_has_empty_bitmaps && val == 0) ||
99             val > r->default_ctrl) {
100                 rdt_last_cmd_puts("Mask out of range\n");
101                 return false;
102         }
103
104         first_bit = find_first_bit(&val, cbm_len);
105         zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
106
107         /* Are non-contiguous bitmaps allowed? */
108         if (!r->cache.arch_has_sparse_bitmaps &&
109             (find_next_bit(&val, cbm_len, zero_bit) < cbm_len)) {
110                 rdt_last_cmd_printf("The mask %lx has non-consecutive 1-bits\n", val);
111                 return false;
112         }
113
114         if ((zero_bit - first_bit) < r->cache.min_cbm_bits) {
115                 rdt_last_cmd_printf("Need at least %d bits in the mask\n",
116                                     r->cache.min_cbm_bits);
117                 return false;
118         }
119
120         *data = val;
121         return true;
122 }
123
124 /*
125  * Read one cache bit mask (hex). Check that it is valid for the current
126  * resource type.
127  */
128 int parse_cbm(struct rdt_parse_data *data, struct rdt_resource *r,
129               struct rdt_domain *d)
130 {
131         struct rdtgroup *rdtgrp = data->rdtgrp;
132         u32 cbm_val;
133
134         if (d->have_new_ctrl) {
135                 rdt_last_cmd_printf("Duplicate domain %d\n", d->id);
136                 return -EINVAL;
137         }
138
139         /*
140          * Cannot set up more than one pseudo-locked region in a cache
141          * hierarchy.
142          */
143         if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
144             rdtgroup_pseudo_locked_in_hierarchy(d)) {
145                 rdt_last_cmd_puts("Pseudo-locked region in hierarchy\n");
146                 return -EINVAL;
147         }
148
149         if (!cbm_validate(data->buf, &cbm_val, r))
150                 return -EINVAL;
151
152         if ((rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
153              rdtgrp->mode == RDT_MODE_SHAREABLE) &&
154             rdtgroup_cbm_overlaps_pseudo_locked(d, cbm_val)) {
155                 rdt_last_cmd_puts("CBM overlaps with pseudo-locked region\n");
156                 return -EINVAL;
157         }
158
159         /*
160          * The CBM may not overlap with the CBM of another closid if
161          * either is exclusive.
162          */
163         if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, true)) {
164                 rdt_last_cmd_puts("Overlaps with exclusive group\n");
165                 return -EINVAL;
166         }
167
168         if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, false)) {
169                 if (rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
170                     rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
171                         rdt_last_cmd_puts("Overlaps with other group\n");
172                         return -EINVAL;
173                 }
174         }
175
176         d->new_ctrl = cbm_val;
177         d->have_new_ctrl = true;
178
179         return 0;
180 }
181
182 /*
183  * For each domain in this resource we expect to find a series of:
184  *      id=mask
185  * separated by ";". The "id" is in decimal, and must match one of
186  * the "id"s for this resource.
187  */
188 static int parse_line(char *line, struct rdt_resource *r,
189                       struct rdtgroup *rdtgrp)
190 {
191         struct rdt_parse_data data;
192         char *dom = NULL, *id;
193         struct rdt_domain *d;
194         unsigned long dom_id;
195
196         if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
197             r->rid == RDT_RESOURCE_MBA) {
198                 rdt_last_cmd_puts("Cannot pseudo-lock MBA resource\n");
199                 return -EINVAL;
200         }
201
202 next:
203         if (!line || line[0] == '\0')
204                 return 0;
205         dom = strsep(&line, ";");
206         id = strsep(&dom, "=");
207         if (!dom || kstrtoul(id, 10, &dom_id)) {
208                 rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
209                 return -EINVAL;
210         }
211         dom = strim(dom);
212         list_for_each_entry(d, &r->domains, list) {
213                 if (d->id == dom_id) {
214                         data.buf = dom;
215                         data.rdtgrp = rdtgrp;
216                         if (r->parse_ctrlval(&data, r, d))
217                                 return -EINVAL;
218                         if (rdtgrp->mode ==  RDT_MODE_PSEUDO_LOCKSETUP) {
219                                 /*
220                                  * In pseudo-locking setup mode and just
221                                  * parsed a valid CBM that should be
222                                  * pseudo-locked. Only one locked region per
223                                  * resource group and domain so just do
224                                  * the required initialization for single
225                                  * region and return.
226                                  */
227                                 rdtgrp->plr->r = r;
228                                 rdtgrp->plr->d = d;
229                                 rdtgrp->plr->cbm = d->new_ctrl;
230                                 d->plr = rdtgrp->plr;
231                                 return 0;
232                         }
233                         goto next;
234                 }
235         }
236         return -EINVAL;
237 }
238
239 int update_domains(struct rdt_resource *r, int closid)
240 {
241         struct msr_param msr_param;
242         cpumask_var_t cpu_mask;
243         struct rdt_domain *d;
244         bool mba_sc;
245         u32 *dc;
246         int cpu;
247
248         if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
249                 return -ENOMEM;
250
251         msr_param.low = closid;
252         msr_param.high = msr_param.low + 1;
253         msr_param.res = r;
254
255         mba_sc = is_mba_sc(r);
256         list_for_each_entry(d, &r->domains, list) {
257                 dc = !mba_sc ? d->ctrl_val : d->mbps_val;
258                 if (d->have_new_ctrl && d->new_ctrl != dc[closid]) {
259                         cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
260                         dc[closid] = d->new_ctrl;
261                 }
262         }
263
264         /*
265          * Avoid writing the control msr with control values when
266          * MBA software controller is enabled
267          */
268         if (cpumask_empty(cpu_mask) || mba_sc)
269                 goto done;
270         cpu = get_cpu();
271         /* Update resource control msr on this CPU if it's in cpu_mask. */
272         if (cpumask_test_cpu(cpu, cpu_mask))
273                 rdt_ctrl_update(&msr_param);
274         /* Update resource control msr on other CPUs. */
275         smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
276         put_cpu();
277
278 done:
279         free_cpumask_var(cpu_mask);
280
281         return 0;
282 }
283
284 static int rdtgroup_parse_resource(char *resname, char *tok,
285                                    struct rdtgroup *rdtgrp)
286 {
287         struct rdt_hw_resource *hw_res;
288         struct rdt_resource *r;
289
290         for_each_alloc_enabled_rdt_resource(r) {
291                 hw_res = resctrl_to_arch_res(r);
292                 if (!strcmp(resname, r->name) && rdtgrp->closid < hw_res->num_closid)
293                         return parse_line(tok, r, rdtgrp);
294         }
295         rdt_last_cmd_printf("Unknown or unsupported resource name '%s'\n", resname);
296         return -EINVAL;
297 }
298
299 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
300                                 char *buf, size_t nbytes, loff_t off)
301 {
302         struct rdtgroup *rdtgrp;
303         struct rdt_domain *dom;
304         struct rdt_resource *r;
305         char *tok, *resname;
306         int ret = 0;
307
308         /* Valid input requires a trailing newline */
309         if (nbytes == 0 || buf[nbytes - 1] != '\n')
310                 return -EINVAL;
311         buf[nbytes - 1] = '\0';
312
313         cpus_read_lock();
314         rdtgrp = rdtgroup_kn_lock_live(of->kn);
315         if (!rdtgrp) {
316                 rdtgroup_kn_unlock(of->kn);
317                 cpus_read_unlock();
318                 return -ENOENT;
319         }
320         rdt_last_cmd_clear();
321
322         /*
323          * No changes to pseudo-locked region allowed. It has to be removed
324          * and re-created instead.
325          */
326         if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
327                 ret = -EINVAL;
328                 rdt_last_cmd_puts("Resource group is pseudo-locked\n");
329                 goto out;
330         }
331
332         for_each_alloc_enabled_rdt_resource(r) {
333                 list_for_each_entry(dom, &r->domains, list)
334                         dom->have_new_ctrl = false;
335         }
336
337         while ((tok = strsep(&buf, "\n")) != NULL) {
338                 resname = strim(strsep(&tok, ":"));
339                 if (!tok) {
340                         rdt_last_cmd_puts("Missing ':'\n");
341                         ret = -EINVAL;
342                         goto out;
343                 }
344                 if (tok[0] == '\0') {
345                         rdt_last_cmd_printf("Missing '%s' value\n", resname);
346                         ret = -EINVAL;
347                         goto out;
348                 }
349                 ret = rdtgroup_parse_resource(resname, tok, rdtgrp);
350                 if (ret)
351                         goto out;
352         }
353
354         for_each_alloc_enabled_rdt_resource(r) {
355                 ret = update_domains(r, rdtgrp->closid);
356                 if (ret)
357                         goto out;
358         }
359
360         if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
361                 /*
362                  * If pseudo-locking fails we keep the resource group in
363                  * mode RDT_MODE_PSEUDO_LOCKSETUP with its class of service
364                  * active and updated for just the domain the pseudo-locked
365                  * region was requested for.
366                  */
367                 ret = rdtgroup_pseudo_lock_create(rdtgrp);
368         }
369
370 out:
371         rdtgroup_kn_unlock(of->kn);
372         cpus_read_unlock();
373         return ret ?: nbytes;
374 }
375
376 static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
377 {
378         struct rdt_domain *dom;
379         bool sep = false;
380         u32 ctrl_val;
381
382         seq_printf(s, "%*s:", max_name_width, r->name);
383         list_for_each_entry(dom, &r->domains, list) {
384                 if (sep)
385                         seq_puts(s, ";");
386
387                 ctrl_val = (!is_mba_sc(r) ? dom->ctrl_val[closid] :
388                             dom->mbps_val[closid]);
389                 seq_printf(s, r->format_str, dom->id, max_data_width,
390                            ctrl_val);
391                 sep = true;
392         }
393         seq_puts(s, "\n");
394 }
395
396 int rdtgroup_schemata_show(struct kernfs_open_file *of,
397                            struct seq_file *s, void *v)
398 {
399         struct rdt_hw_resource *hw_res;
400         struct rdtgroup *rdtgrp;
401         struct rdt_resource *r;
402         int ret = 0;
403         u32 closid;
404
405         rdtgrp = rdtgroup_kn_lock_live(of->kn);
406         if (rdtgrp) {
407                 if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
408                         for_each_alloc_enabled_rdt_resource(r)
409                                 seq_printf(s, "%s:uninitialized\n", r->name);
410                 } else if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
411                         if (!rdtgrp->plr->d) {
412                                 rdt_last_cmd_clear();
413                                 rdt_last_cmd_puts("Cache domain offline\n");
414                                 ret = -ENODEV;
415                         } else {
416                                 seq_printf(s, "%s:%d=%x\n",
417                                            rdtgrp->plr->r->name,
418                                            rdtgrp->plr->d->id,
419                                            rdtgrp->plr->cbm);
420                         }
421                 } else {
422                         closid = rdtgrp->closid;
423                         for_each_alloc_enabled_rdt_resource(r) {
424                                 hw_res = resctrl_to_arch_res(r);
425                                 if (closid < hw_res->num_closid)
426                                         show_doms(s, r, closid);
427                         }
428                 }
429         } else {
430                 ret = -ENOENT;
431         }
432         rdtgroup_kn_unlock(of->kn);
433         return ret;
434 }
435
436 void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
437                     struct rdt_domain *d, struct rdtgroup *rdtgrp,
438                     int evtid, int first)
439 {
440         /*
441          * setup the parameters to send to the IPI to read the data.
442          */
443         rr->rgrp = rdtgrp;
444         rr->evtid = evtid;
445         rr->r = r;
446         rr->d = d;
447         rr->val = 0;
448         rr->first = first;
449
450         smp_call_function_any(&d->cpu_mask, mon_event_count, rr, 1);
451 }
452
453 int rdtgroup_mondata_show(struct seq_file *m, void *arg)
454 {
455         struct kernfs_open_file *of = m->private;
456         struct rdt_hw_resource *hw_res;
457         u32 resid, evtid, domid;
458         struct rdtgroup *rdtgrp;
459         struct rdt_resource *r;
460         union mon_data_bits md;
461         struct rdt_domain *d;
462         struct rmid_read rr;
463         int ret = 0;
464
465         rdtgrp = rdtgroup_kn_lock_live(of->kn);
466         if (!rdtgrp) {
467                 ret = -ENOENT;
468                 goto out;
469         }
470
471         md.priv = of->kn->priv;
472         resid = md.u.rid;
473         domid = md.u.domid;
474         evtid = md.u.evtid;
475
476         hw_res = &rdt_resources_all[resid];
477         r = &hw_res->r_resctrl;
478         d = rdt_find_domain(r, domid, NULL);
479         if (IS_ERR_OR_NULL(d)) {
480                 ret = -ENOENT;
481                 goto out;
482         }
483
484         mon_event_read(&rr, r, d, rdtgrp, evtid, false);
485
486         if (rr.val & RMID_VAL_ERROR)
487                 seq_puts(m, "Error\n");
488         else if (rr.val & RMID_VAL_UNAVAIL)
489                 seq_puts(m, "Unavailable\n");
490         else
491                 seq_printf(m, "%llu\n", rr.val * hw_res->mon_scale);
492
493 out:
494         rdtgroup_kn_unlock(of->kn);
495         return ret;
496 }