Linux 4.18.10
[linux/fpc-iii.git] / arch / x86 / kernel / cpu / intel_rdt_ctrlmondata.c
blob116d57b248d3cae479c47848766a730e620aa253
1 /*
2 * Resource Director Technology(RDT)
3 * - Cache Allocation code.
5 * Copyright (C) 2016 Intel Corporation
7 * Authors:
8 * Fenghua Yu <fenghua.yu@intel.com>
9 * Tony Luck <tony.luck@intel.com>
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.
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.
20 * More information about RDT be found in the Intel (R) x86 Architecture
21 * Software Developer Manual June 2016, volume 3, section 17.17.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/kernfs.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29 #include "intel_rdt.h"
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.
37 static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
39 unsigned long bw;
40 int ret;
43 * Only linear delay values is supported for current Intel SKUs.
45 if (!r->membw.delay_linear) {
46 rdt_last_cmd_puts("No support for non-linear MB domains\n");
47 return false;
50 ret = kstrtoul(buf, 10, &bw);
51 if (ret) {
52 rdt_last_cmd_printf("Non-decimal digit in MB value %s\n", buf);
53 return false;
56 if ((bw < r->membw.min_bw || bw > r->default_ctrl) &&
57 !is_mba_sc(r)) {
58 rdt_last_cmd_printf("MB value %ld out of range [%d,%d]\n", bw,
59 r->membw.min_bw, r->default_ctrl);
60 return false;
63 *data = roundup(bw, (unsigned long)r->membw.bw_gran);
64 return true;
67 int parse_bw(char *buf, struct rdt_resource *r, struct rdt_domain *d)
69 unsigned long data;
71 if (d->have_new_ctrl) {
72 rdt_last_cmd_printf("duplicate domain %d\n", d->id);
73 return -EINVAL;
76 if (!bw_validate(buf, &data, r))
77 return -EINVAL;
78 d->new_ctrl = data;
79 d->have_new_ctrl = true;
81 return 0;
85 * Check whether a cache bit mask is valid. The SDM says:
86 * Please note that all (and only) contiguous '1' combinations
87 * are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
88 * Additionally Haswell requires at least two bits set.
90 static bool cbm_validate(char *buf, unsigned long *data, struct rdt_resource *r)
92 unsigned long first_bit, zero_bit, val;
93 unsigned int cbm_len = r->cache.cbm_len;
94 int ret;
96 ret = kstrtoul(buf, 16, &val);
97 if (ret) {
98 rdt_last_cmd_printf("non-hex character in mask %s\n", buf);
99 return false;
102 if (val == 0 || val > r->default_ctrl) {
103 rdt_last_cmd_puts("mask out of range\n");
104 return false;
107 first_bit = find_first_bit(&val, cbm_len);
108 zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
110 if (find_next_bit(&val, cbm_len, zero_bit) < cbm_len) {
111 rdt_last_cmd_printf("mask %lx has non-consecutive 1-bits\n", val);
112 return false;
115 if ((zero_bit - first_bit) < r->cache.min_cbm_bits) {
116 rdt_last_cmd_printf("Need at least %d bits in mask\n",
117 r->cache.min_cbm_bits);
118 return false;
121 *data = val;
122 return true;
126 * Read one cache bit mask (hex). Check that it is valid for the current
127 * resource type.
129 int parse_cbm(char *buf, struct rdt_resource *r, struct rdt_domain *d)
131 unsigned long data;
133 if (d->have_new_ctrl) {
134 rdt_last_cmd_printf("duplicate domain %d\n", d->id);
135 return -EINVAL;
138 if(!cbm_validate(buf, &data, r))
139 return -EINVAL;
140 d->new_ctrl = data;
141 d->have_new_ctrl = true;
143 return 0;
147 * For each domain in this resource we expect to find a series of:
148 * id=mask
149 * separated by ";". The "id" is in decimal, and must match one of
150 * the "id"s for this resource.
152 static int parse_line(char *line, struct rdt_resource *r)
154 char *dom = NULL, *id;
155 struct rdt_domain *d;
156 unsigned long dom_id;
158 next:
159 if (!line || line[0] == '\0')
160 return 0;
161 dom = strsep(&line, ";");
162 id = strsep(&dom, "=");
163 if (!dom || kstrtoul(id, 10, &dom_id)) {
164 rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
165 return -EINVAL;
167 dom = strim(dom);
168 list_for_each_entry(d, &r->domains, list) {
169 if (d->id == dom_id) {
170 if (r->parse_ctrlval(dom, r, d))
171 return -EINVAL;
172 goto next;
175 return -EINVAL;
178 static int update_domains(struct rdt_resource *r, int closid)
180 struct msr_param msr_param;
181 cpumask_var_t cpu_mask;
182 struct rdt_domain *d;
183 bool mba_sc;
184 u32 *dc;
185 int cpu;
187 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
188 return -ENOMEM;
190 msr_param.low = closid;
191 msr_param.high = msr_param.low + 1;
192 msr_param.res = r;
194 mba_sc = is_mba_sc(r);
195 list_for_each_entry(d, &r->domains, list) {
196 dc = !mba_sc ? d->ctrl_val : d->mbps_val;
197 if (d->have_new_ctrl && d->new_ctrl != dc[closid]) {
198 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
199 dc[closid] = d->new_ctrl;
204 * Avoid writing the control msr with control values when
205 * MBA software controller is enabled
207 if (cpumask_empty(cpu_mask) || mba_sc)
208 goto done;
209 cpu = get_cpu();
210 /* Update CBM on this cpu if it's in cpu_mask. */
211 if (cpumask_test_cpu(cpu, cpu_mask))
212 rdt_ctrl_update(&msr_param);
213 /* Update CBM on other cpus. */
214 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
215 put_cpu();
217 done:
218 free_cpumask_var(cpu_mask);
220 return 0;
223 static int rdtgroup_parse_resource(char *resname, char *tok, int closid)
225 struct rdt_resource *r;
227 for_each_alloc_enabled_rdt_resource(r) {
228 if (!strcmp(resname, r->name) && closid < r->num_closid)
229 return parse_line(tok, r);
231 rdt_last_cmd_printf("unknown/unsupported resource name '%s'\n", resname);
232 return -EINVAL;
235 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
236 char *buf, size_t nbytes, loff_t off)
238 struct rdtgroup *rdtgrp;
239 struct rdt_domain *dom;
240 struct rdt_resource *r;
241 char *tok, *resname;
242 int closid, ret = 0;
244 /* Valid input requires a trailing newline */
245 if (nbytes == 0 || buf[nbytes - 1] != '\n')
246 return -EINVAL;
247 buf[nbytes - 1] = '\0';
249 rdtgrp = rdtgroup_kn_lock_live(of->kn);
250 if (!rdtgrp) {
251 rdtgroup_kn_unlock(of->kn);
252 return -ENOENT;
254 rdt_last_cmd_clear();
256 closid = rdtgrp->closid;
258 for_each_alloc_enabled_rdt_resource(r) {
259 list_for_each_entry(dom, &r->domains, list)
260 dom->have_new_ctrl = false;
263 while ((tok = strsep(&buf, "\n")) != NULL) {
264 resname = strim(strsep(&tok, ":"));
265 if (!tok) {
266 rdt_last_cmd_puts("Missing ':'\n");
267 ret = -EINVAL;
268 goto out;
270 if (tok[0] == '\0') {
271 rdt_last_cmd_printf("Missing '%s' value\n", resname);
272 ret = -EINVAL;
273 goto out;
275 ret = rdtgroup_parse_resource(resname, tok, closid);
276 if (ret)
277 goto out;
280 for_each_alloc_enabled_rdt_resource(r) {
281 ret = update_domains(r, closid);
282 if (ret)
283 goto out;
286 out:
287 rdtgroup_kn_unlock(of->kn);
288 return ret ?: nbytes;
291 static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
293 struct rdt_domain *dom;
294 bool sep = false;
295 u32 ctrl_val;
297 seq_printf(s, "%*s:", max_name_width, r->name);
298 list_for_each_entry(dom, &r->domains, list) {
299 if (sep)
300 seq_puts(s, ";");
302 ctrl_val = (!is_mba_sc(r) ? dom->ctrl_val[closid] :
303 dom->mbps_val[closid]);
304 seq_printf(s, r->format_str, dom->id, max_data_width,
305 ctrl_val);
306 sep = true;
308 seq_puts(s, "\n");
311 int rdtgroup_schemata_show(struct kernfs_open_file *of,
312 struct seq_file *s, void *v)
314 struct rdtgroup *rdtgrp;
315 struct rdt_resource *r;
316 int ret = 0;
317 u32 closid;
319 rdtgrp = rdtgroup_kn_lock_live(of->kn);
320 if (rdtgrp) {
321 closid = rdtgrp->closid;
322 for_each_alloc_enabled_rdt_resource(r) {
323 if (closid < r->num_closid)
324 show_doms(s, r, closid);
326 } else {
327 ret = -ENOENT;
329 rdtgroup_kn_unlock(of->kn);
330 return ret;
333 void mon_event_read(struct rmid_read *rr, struct rdt_domain *d,
334 struct rdtgroup *rdtgrp, int evtid, int first)
337 * setup the parameters to send to the IPI to read the data.
339 rr->rgrp = rdtgrp;
340 rr->evtid = evtid;
341 rr->d = d;
342 rr->val = 0;
343 rr->first = first;
345 smp_call_function_any(&d->cpu_mask, mon_event_count, rr, 1);
348 int rdtgroup_mondata_show(struct seq_file *m, void *arg)
350 struct kernfs_open_file *of = m->private;
351 u32 resid, evtid, domid;
352 struct rdtgroup *rdtgrp;
353 struct rdt_resource *r;
354 union mon_data_bits md;
355 struct rdt_domain *d;
356 struct rmid_read rr;
357 int ret = 0;
359 rdtgrp = rdtgroup_kn_lock_live(of->kn);
361 md.priv = of->kn->priv;
362 resid = md.u.rid;
363 domid = md.u.domid;
364 evtid = md.u.evtid;
366 r = &rdt_resources_all[resid];
367 d = rdt_find_domain(r, domid, NULL);
368 if (!d) {
369 ret = -ENOENT;
370 goto out;
373 mon_event_read(&rr, d, rdtgrp, evtid, false);
375 if (rr.val & RMID_VAL_ERROR)
376 seq_puts(m, "Error\n");
377 else if (rr.val & RMID_VAL_UNAVAIL)
378 seq_puts(m, "Unavailable\n");
379 else
380 seq_printf(m, "%llu\n", rr.val * r->mon_scale);
382 out:
383 rdtgroup_kn_unlock(of->kn);
384 return ret;