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