GNU Linux-libre 4.14.294-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/kernfs.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29 #include "intel_rdt.h"
30
31 /*
32  * Check whether MBA bandwidth percentage value is correct. The value is
33  * checked against the minimum and max bandwidth values specified by the
34  * hardware. The allocated bandwidth percentage is rounded to the next
35  * control step available on the hardware.
36  */
37 static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
38 {
39         unsigned long bw;
40         int ret;
41
42         /*
43          * Only linear delay values is supported for current Intel SKUs.
44          */
45         if (!r->membw.delay_linear)
46                 return false;
47
48         ret = kstrtoul(buf, 10, &bw);
49         if (ret)
50                 return false;
51
52         if (bw < r->membw.min_bw || bw > r->default_ctrl)
53                 return false;
54
55         *data = roundup(bw, (unsigned long)r->membw.bw_gran);
56         return true;
57 }
58
59 int parse_bw(char *buf, struct rdt_resource *r, struct rdt_domain *d)
60 {
61         unsigned long data;
62
63         if (d->have_new_ctrl)
64                 return -EINVAL;
65
66         if (!bw_validate(buf, &data, r))
67                 return -EINVAL;
68         d->new_ctrl = data;
69         d->have_new_ctrl = true;
70
71         return 0;
72 }
73
74 /*
75  * Check whether a cache bit mask is valid. The SDM says:
76  *      Please note that all (and only) contiguous '1' combinations
77  *      are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
78  * Additionally Haswell requires at least two bits set.
79  */
80 static bool cbm_validate(char *buf, unsigned long *data, struct rdt_resource *r)
81 {
82         unsigned long first_bit, zero_bit, val;
83         unsigned int cbm_len = r->cache.cbm_len;
84         int ret;
85
86         ret = kstrtoul(buf, 16, &val);
87         if (ret)
88                 return false;
89
90         if (val == 0 || val > r->default_ctrl)
91                 return false;
92
93         first_bit = find_first_bit(&val, cbm_len);
94         zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
95
96         if (find_next_bit(&val, cbm_len, zero_bit) < cbm_len)
97                 return false;
98
99         if ((zero_bit - first_bit) < r->cache.min_cbm_bits)
100                 return false;
101
102         *data = val;
103         return true;
104 }
105
106 /*
107  * Read one cache bit mask (hex). Check that it is valid for the current
108  * resource type.
109  */
110 int parse_cbm(char *buf, struct rdt_resource *r, struct rdt_domain *d)
111 {
112         unsigned long data;
113
114         if (d->have_new_ctrl)
115                 return -EINVAL;
116
117         if(!cbm_validate(buf, &data, r))
118                 return -EINVAL;
119         d->new_ctrl = data;
120         d->have_new_ctrl = true;
121
122         return 0;
123 }
124
125 /*
126  * For each domain in this resource we expect to find a series of:
127  *      id=mask
128  * separated by ";". The "id" is in decimal, and must match one of
129  * the "id"s for this resource.
130  */
131 static int parse_line(char *line, struct rdt_resource *r)
132 {
133         char *dom = NULL, *id;
134         struct rdt_domain *d;
135         unsigned long dom_id;
136
137 next:
138         if (!line || line[0] == '\0')
139                 return 0;
140         dom = strsep(&line, ";");
141         id = strsep(&dom, "=");
142         if (!dom || kstrtoul(id, 10, &dom_id))
143                 return -EINVAL;
144         dom = strim(dom);
145         list_for_each_entry(d, &r->domains, list) {
146                 if (d->id == dom_id) {
147                         if (r->parse_ctrlval(dom, r, d))
148                                 return -EINVAL;
149                         goto next;
150                 }
151         }
152         return -EINVAL;
153 }
154
155 static int update_domains(struct rdt_resource *r, int closid)
156 {
157         struct msr_param msr_param;
158         cpumask_var_t cpu_mask;
159         struct rdt_domain *d;
160         int cpu;
161
162         if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
163                 return -ENOMEM;
164
165         msr_param.low = closid;
166         msr_param.high = msr_param.low + 1;
167         msr_param.res = r;
168
169         list_for_each_entry(d, &r->domains, list) {
170                 if (d->have_new_ctrl && d->new_ctrl != d->ctrl_val[closid]) {
171                         cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
172                         d->ctrl_val[closid] = d->new_ctrl;
173                 }
174         }
175         if (cpumask_empty(cpu_mask))
176                 goto done;
177         cpu = get_cpu();
178         /* Update CBM on this cpu if it's in cpu_mask. */
179         if (cpumask_test_cpu(cpu, cpu_mask))
180                 rdt_ctrl_update(&msr_param);
181         /* Update CBM on other cpus. */
182         smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
183         put_cpu();
184
185 done:
186         free_cpumask_var(cpu_mask);
187
188         return 0;
189 }
190
191 static int rdtgroup_parse_resource(char *resname, char *tok, int closid)
192 {
193         struct rdt_resource *r;
194
195         for_each_alloc_enabled_rdt_resource(r) {
196                 if (!strcmp(resname, r->name) && closid < r->num_closid)
197                         return parse_line(tok, r);
198         }
199         return -EINVAL;
200 }
201
202 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
203                                 char *buf, size_t nbytes, loff_t off)
204 {
205         struct rdtgroup *rdtgrp;
206         struct rdt_domain *dom;
207         struct rdt_resource *r;
208         char *tok, *resname;
209         int closid, ret = 0;
210
211         /* Valid input requires a trailing newline */
212         if (nbytes == 0 || buf[nbytes - 1] != '\n')
213                 return -EINVAL;
214         buf[nbytes - 1] = '\0';
215
216         rdtgrp = rdtgroup_kn_lock_live(of->kn);
217         if (!rdtgrp) {
218                 rdtgroup_kn_unlock(of->kn);
219                 return -ENOENT;
220         }
221
222         closid = rdtgrp->closid;
223
224         for_each_alloc_enabled_rdt_resource(r) {
225                 list_for_each_entry(dom, &r->domains, list)
226                         dom->have_new_ctrl = false;
227         }
228
229         while ((tok = strsep(&buf, "\n")) != NULL) {
230                 resname = strim(strsep(&tok, ":"));
231                 if (!tok) {
232                         ret = -EINVAL;
233                         goto out;
234                 }
235                 ret = rdtgroup_parse_resource(resname, tok, closid);
236                 if (ret)
237                         goto out;
238         }
239
240         for_each_alloc_enabled_rdt_resource(r) {
241                 ret = update_domains(r, closid);
242                 if (ret)
243                         goto out;
244         }
245
246 out:
247         rdtgroup_kn_unlock(of->kn);
248         return ret ?: nbytes;
249 }
250
251 static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
252 {
253         struct rdt_domain *dom;
254         bool sep = false;
255
256         seq_printf(s, "%*s:", max_name_width, r->name);
257         list_for_each_entry(dom, &r->domains, list) {
258                 if (sep)
259                         seq_puts(s, ";");
260                 seq_printf(s, r->format_str, dom->id, max_data_width,
261                            dom->ctrl_val[closid]);
262                 sep = true;
263         }
264         seq_puts(s, "\n");
265 }
266
267 int rdtgroup_schemata_show(struct kernfs_open_file *of,
268                            struct seq_file *s, void *v)
269 {
270         struct rdtgroup *rdtgrp;
271         struct rdt_resource *r;
272         int ret = 0;
273         u32 closid;
274
275         rdtgrp = rdtgroup_kn_lock_live(of->kn);
276         if (rdtgrp) {
277                 closid = rdtgrp->closid;
278                 for_each_alloc_enabled_rdt_resource(r) {
279                         if (closid < r->num_closid)
280                                 show_doms(s, r, closid);
281                 }
282         } else {
283                 ret = -ENOENT;
284         }
285         rdtgroup_kn_unlock(of->kn);
286         return ret;
287 }
288
289 void mon_event_read(struct rmid_read *rr, struct rdt_domain *d,
290                     struct rdtgroup *rdtgrp, int evtid, int first)
291 {
292         /*
293          * setup the parameters to send to the IPI to read the data.
294          */
295         rr->rgrp = rdtgrp;
296         rr->evtid = evtid;
297         rr->d = d;
298         rr->val = 0;
299         rr->first = first;
300
301         smp_call_function_any(&d->cpu_mask, mon_event_count, rr, 1);
302 }
303
304 int rdtgroup_mondata_show(struct seq_file *m, void *arg)
305 {
306         struct kernfs_open_file *of = m->private;
307         u32 resid, evtid, domid;
308         struct rdtgroup *rdtgrp;
309         struct rdt_resource *r;
310         union mon_data_bits md;
311         struct rdt_domain *d;
312         struct rmid_read rr;
313         int ret = 0;
314
315         rdtgrp = rdtgroup_kn_lock_live(of->kn);
316         if (!rdtgrp) {
317                 ret = -ENOENT;
318                 goto out;
319         }
320
321         md.priv = of->kn->priv;
322         resid = md.u.rid;
323         domid = md.u.domid;
324         evtid = md.u.evtid;
325
326         r = &rdt_resources_all[resid];
327         d = rdt_find_domain(r, domid, NULL);
328         if (!d) {
329                 ret = -ENOENT;
330                 goto out;
331         }
332
333         mon_event_read(&rr, d, rdtgrp, evtid, false);
334
335         if (rr.val & RMID_VAL_ERROR)
336                 seq_puts(m, "Error\n");
337         else if (rr.val & RMID_VAL_UNAVAIL)
338                 seq_puts(m, "Unavailable\n");
339         else
340                 seq_printf(m, "%llu\n", rr.val * r->mon_scale);
341
342 out:
343         rdtgroup_kn_unlock(of->kn);
344         return ret;
345 }