]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/x86/kernel/cpu/intel_rdt_schemata.c
x86/intel_rdt: Make schemata file parsers resource specific
[karo-tx-linux.git] / arch / x86 / kernel / cpu / intel_rdt_schemata.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 <asm/intel_rdt.h>
30
31 /*
32  * Check whether a cache bit mask is valid. The SDM says:
33  *      Please note that all (and only) contiguous '1' combinations
34  *      are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
35  * Additionally Haswell requires at least two bits set.
36  */
37 static bool cbm_validate(char *buf, unsigned long *data, struct rdt_resource *r)
38 {
39         unsigned long first_bit, zero_bit, val;
40         unsigned int cbm_len = r->cache.cbm_len;
41         int ret;
42
43         ret = kstrtoul(buf, 16, &val);
44         if (ret)
45                 return false;
46
47         if (val == 0 || val > r->default_ctrl)
48                 return false;
49
50         first_bit = find_first_bit(&val, cbm_len);
51         zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
52
53         if (find_next_bit(&val, cbm_len, zero_bit) < cbm_len)
54                 return false;
55
56         if ((zero_bit - first_bit) < r->cache.min_cbm_bits)
57                 return false;
58
59         *data = val;
60         return true;
61 }
62
63 /*
64  * Read one cache bit mask (hex). Check that it is valid for the current
65  * resource type.
66  */
67 int parse_cbm(char *buf, struct rdt_resource *r, struct rdt_domain *d)
68 {
69         unsigned long data;
70
71         if (d->have_new_ctrl)
72                 return -EINVAL;
73
74         if(!cbm_validate(buf, &data, r))
75                 return -EINVAL;
76         d->new_ctrl = data;
77         d->have_new_ctrl = true;
78
79         return 0;
80 }
81
82 /*
83  * For each domain in this resource we expect to find a series of:
84  *      id=mask
85  * separated by ";". The "id" is in decimal, and must match one of
86  * the "id"s for this resource.
87  */
88 static int parse_line(char *line, struct rdt_resource *r)
89 {
90         char *dom = NULL, *id;
91         struct rdt_domain *d;
92         unsigned long dom_id;
93
94 next:
95         if (!line || line[0] == '\0')
96                 return 0;
97         dom = strsep(&line, ";");
98         id = strsep(&dom, "=");
99         if (!dom || kstrtoul(id, 10, &dom_id))
100                 return -EINVAL;
101         list_for_each_entry(d, &r->domains, list) {
102                 if (d->id == dom_id) {
103                         if (r->parse_ctrlval(dom, r, d))
104                                 return -EINVAL;
105                         goto next;
106                 }
107         }
108         return -EINVAL;
109 }
110
111 static int update_domains(struct rdt_resource *r, int closid)
112 {
113         struct msr_param msr_param;
114         cpumask_var_t cpu_mask;
115         struct rdt_domain *d;
116         int cpu;
117
118         if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
119                 return -ENOMEM;
120
121         msr_param.low = closid;
122         msr_param.high = msr_param.low + 1;
123         msr_param.res = r;
124
125         list_for_each_entry(d, &r->domains, list) {
126                 if (d->have_new_ctrl && d->new_ctrl != d->ctrl_val[closid]) {
127                         cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
128                         d->ctrl_val[closid] = d->new_ctrl;
129                 }
130         }
131         if (cpumask_empty(cpu_mask))
132                 goto done;
133         cpu = get_cpu();
134         /* Update CBM on this cpu if it's in cpu_mask. */
135         if (cpumask_test_cpu(cpu, cpu_mask))
136                 rdt_ctrl_update(&msr_param);
137         /* Update CBM on other cpus. */
138         smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
139         put_cpu();
140
141 done:
142         free_cpumask_var(cpu_mask);
143
144         return 0;
145 }
146
147 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
148                                 char *buf, size_t nbytes, loff_t off)
149 {
150         struct rdtgroup *rdtgrp;
151         struct rdt_domain *dom;
152         struct rdt_resource *r;
153         char *tok, *resname;
154         int closid, ret = 0;
155
156         /* Valid input requires a trailing newline */
157         if (nbytes == 0 || buf[nbytes - 1] != '\n')
158                 return -EINVAL;
159         buf[nbytes - 1] = '\0';
160
161         rdtgrp = rdtgroup_kn_lock_live(of->kn);
162         if (!rdtgrp) {
163                 rdtgroup_kn_unlock(of->kn);
164                 return -ENOENT;
165         }
166
167         closid = rdtgrp->closid;
168
169         for_each_enabled_rdt_resource(r)
170                 list_for_each_entry(dom, &r->domains, list)
171                         dom->have_new_ctrl = false;
172
173         while ((tok = strsep(&buf, "\n")) != NULL) {
174                 resname = strsep(&tok, ":");
175                 if (!tok) {
176                         ret = -EINVAL;
177                         goto out;
178                 }
179                 for_each_enabled_rdt_resource(r) {
180                         if (!strcmp(resname, r->name) &&
181                             closid < r->num_closid) {
182                                 ret = parse_line(tok, r);
183                                 if (ret)
184                                         goto out;
185                                 break;
186                         }
187                 }
188                 if (!r->name) {
189                         ret = -EINVAL;
190                         goto out;
191                 }
192         }
193
194         for_each_enabled_rdt_resource(r) {
195                 ret = update_domains(r, closid);
196                 if (ret)
197                         goto out;
198         }
199
200 out:
201         rdtgroup_kn_unlock(of->kn);
202         return ret ?: nbytes;
203 }
204
205 static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
206 {
207         struct rdt_domain *dom;
208         bool sep = false;
209
210         seq_printf(s, "%*s:", max_name_width, r->name);
211         list_for_each_entry(dom, &r->domains, list) {
212                 if (sep)
213                         seq_puts(s, ";");
214                 seq_printf(s, r->format_str, dom->id, max_data_width,
215                            dom->ctrl_val[closid]);
216                 sep = true;
217         }
218         seq_puts(s, "\n");
219 }
220
221 int rdtgroup_schemata_show(struct kernfs_open_file *of,
222                            struct seq_file *s, void *v)
223 {
224         struct rdtgroup *rdtgrp;
225         struct rdt_resource *r;
226         int closid, ret = 0;
227
228         rdtgrp = rdtgroup_kn_lock_live(of->kn);
229         if (rdtgrp) {
230                 closid = rdtgrp->closid;
231                 for_each_enabled_rdt_resource(r) {
232                         if (closid < r->num_closid)
233                                 show_doms(s, r, closid);
234                 }
235         } else {
236                 ret = -ENOENT;
237         }
238         rdtgroup_kn_unlock(of->kn);
239         return ret;
240 }