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