intel_rdt_ctrlmondata.c 8.5 KB
Newer Older
T
Tony Luck 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Resource Director Technology(RDT)
 * - Cache Allocation code.
 *
 * Copyright (C) 2016 Intel Corporation
 *
 * Authors:
 *    Fenghua Yu <fenghua.yu@intel.com>
 *    Tony Luck <tony.luck@intel.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * More information about RDT be found in the Intel (R) x86 Architecture
 * Software Developer Manual June 2016, volume 3, section 17.17.
 */

#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt

#include <linux/kernfs.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
29
#include "intel_rdt.h"
T
Tony Luck 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
 * Check whether MBA bandwidth percentage value is correct. The value is
 * checked against the minimum and max bandwidth values specified by the
 * hardware. The allocated bandwidth percentage is rounded to the next
 * control step available on the hardware.
 */
static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
{
	unsigned long bw;
	int ret;

	/*
	 * Only linear delay values is supported for current Intel SKUs.
	 */
45 46
	if (!r->membw.delay_linear) {
		rdt_last_cmd_puts("No support for non-linear MB domains\n");
47
		return false;
48
	}
49 50

	ret = kstrtoul(buf, 10, &bw);
51 52
	if (ret) {
		rdt_last_cmd_printf("Non-decimal digit in MB value %s\n", buf);
53
		return false;
54
	}
55

56 57 58
	if (bw < r->membw.min_bw || bw > r->default_ctrl) {
		rdt_last_cmd_printf("MB value %ld out of range [%d,%d]\n", bw,
				    r->membw.min_bw, r->default_ctrl);
59
		return false;
60
	}
61 62 63 64 65 66 67 68 69

	*data = roundup(bw, (unsigned long)r->membw.bw_gran);
	return true;
}

int parse_bw(char *buf, struct rdt_resource *r, struct rdt_domain *d)
{
	unsigned long data;

70 71
	if (d->have_new_ctrl) {
		rdt_last_cmd_printf("duplicate domain %d\n", d->id);
72
		return -EINVAL;
73
	}
74 75 76 77 78 79 80 81 82

	if (!bw_validate(buf, &data, r))
		return -EINVAL;
	d->new_ctrl = data;
	d->have_new_ctrl = true;

	return 0;
}

T
Tony Luck 已提交
83 84 85 86 87 88
/*
 * Check whether a cache bit mask is valid. The SDM says:
 *	Please note that all (and only) contiguous '1' combinations
 *	are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
 * Additionally Haswell requires at least two bits set.
 */
89
static bool cbm_validate(char *buf, unsigned long *data, struct rdt_resource *r)
T
Tony Luck 已提交
90
{
91
	unsigned long first_bit, zero_bit, val;
92
	unsigned int cbm_len = r->cache.cbm_len;
93 94 95
	int ret;

	ret = kstrtoul(buf, 16, &val);
96 97
	if (ret) {
		rdt_last_cmd_printf("non-hex character in mask %s\n", buf);
98
		return false;
99
	}
T
Tony Luck 已提交
100

101 102
	if (val == 0 || val > r->default_ctrl) {
		rdt_last_cmd_puts("mask out of range\n");
T
Tony Luck 已提交
103
		return false;
104
	}
T
Tony Luck 已提交
105

106 107
	first_bit = find_first_bit(&val, cbm_len);
	zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
T
Tony Luck 已提交
108

109 110
	if (find_next_bit(&val, cbm_len, zero_bit) < cbm_len) {
		rdt_last_cmd_printf("mask %lx has non-consecutive 1-bits\n", val);
T
Tony Luck 已提交
111
		return false;
112
	}
T
Tony Luck 已提交
113

114 115 116
	if ((zero_bit - first_bit) < r->cache.min_cbm_bits) {
		rdt_last_cmd_printf("Need at least %d bits in mask\n",
				    r->cache.min_cbm_bits);
T
Tony Luck 已提交
117
		return false;
118
	}
119 120

	*data = val;
T
Tony Luck 已提交
121 122 123 124 125 126 127
	return true;
}

/*
 * Read one cache bit mask (hex). Check that it is valid for the current
 * resource type.
 */
128
int parse_cbm(char *buf, struct rdt_resource *r, struct rdt_domain *d)
T
Tony Luck 已提交
129 130 131
{
	unsigned long data;

132 133
	if (d->have_new_ctrl) {
		rdt_last_cmd_printf("duplicate domain %d\n", d->id);
134
		return -EINVAL;
135
	}
136

137
	if(!cbm_validate(buf, &data, r))
T
Tony Luck 已提交
138
		return -EINVAL;
139 140
	d->new_ctrl = data;
	d->have_new_ctrl = true;
T
Tony Luck 已提交
141 142 143 144 145 146 147

	return 0;
}

/*
 * For each domain in this resource we expect to find a series of:
 *	id=mask
148 149
 * separated by ";". The "id" is in decimal, and must match one of
 * the "id"s for this resource.
T
Tony Luck 已提交
150 151 152 153 154 155 156
 */
static int parse_line(char *line, struct rdt_resource *r)
{
	char *dom = NULL, *id;
	struct rdt_domain *d;
	unsigned long dom_id;

157 158 159 160 161
next:
	if (!line || line[0] == '\0')
		return 0;
	dom = strsep(&line, ";");
	id = strsep(&dom, "=");
162 163
	if (!dom || kstrtoul(id, 10, &dom_id)) {
		rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
164
		return -EINVAL;
165
	}
166
	dom = strim(dom);
T
Tony Luck 已提交
167
	list_for_each_entry(d, &r->domains, list) {
168
		if (d->id == dom_id) {
169
			if (r->parse_ctrlval(dom, r, d))
170 171 172
				return -EINVAL;
			goto next;
		}
T
Tony Luck 已提交
173
	}
174
	return -EINVAL;
T
Tony Luck 已提交
175 176 177 178 179 180 181
}

static int update_domains(struct rdt_resource *r, int closid)
{
	struct msr_param msr_param;
	cpumask_var_t cpu_mask;
	struct rdt_domain *d;
182
	int cpu;
T
Tony Luck 已提交
183 184 185 186 187 188 189 190 191

	if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
		return -ENOMEM;

	msr_param.low = closid;
	msr_param.high = msr_param.low + 1;
	msr_param.res = r;

	list_for_each_entry(d, &r->domains, list) {
192
		if (d->have_new_ctrl && d->new_ctrl != d->ctrl_val[closid]) {
193
			cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
194
			d->ctrl_val[closid] = d->new_ctrl;
195
		}
T
Tony Luck 已提交
196
	}
197 198
	if (cpumask_empty(cpu_mask))
		goto done;
T
Tony Luck 已提交
199 200 201
	cpu = get_cpu();
	/* Update CBM on this cpu if it's in cpu_mask. */
	if (cpumask_test_cpu(cpu, cpu_mask))
202
		rdt_ctrl_update(&msr_param);
T
Tony Luck 已提交
203
	/* Update CBM on other cpus. */
204
	smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
T
Tony Luck 已提交
205 206
	put_cpu();

207
done:
T
Tony Luck 已提交
208 209 210 211 212
	free_cpumask_var(cpu_mask);

	return 0;
}

213 214 215 216
static int rdtgroup_parse_resource(char *resname, char *tok, int closid)
{
	struct rdt_resource *r;

217
	for_each_alloc_enabled_rdt_resource(r) {
218 219 220
		if (!strcmp(resname, r->name) && closid < r->num_closid)
			return parse_line(tok, r);
	}
221
	rdt_last_cmd_printf("unknown/unsupported resource name '%s'\n", resname);
222 223 224
	return -EINVAL;
}

T
Tony Luck 已提交
225 226 227 228
ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
				char *buf, size_t nbytes, loff_t off)
{
	struct rdtgroup *rdtgrp;
229
	struct rdt_domain *dom;
T
Tony Luck 已提交
230 231 232 233 234
	struct rdt_resource *r;
	char *tok, *resname;
	int closid, ret = 0;

	/* Valid input requires a trailing newline */
235
	if (nbytes == 0 || buf[nbytes - 1] != '\n')
T
Tony Luck 已提交
236 237 238 239 240 241 242 243
		return -EINVAL;
	buf[nbytes - 1] = '\0';

	rdtgrp = rdtgroup_kn_lock_live(of->kn);
	if (!rdtgrp) {
		rdtgroup_kn_unlock(of->kn);
		return -ENOENT;
	}
244
	rdt_last_cmd_clear();
T
Tony Luck 已提交
245 246 247

	closid = rdtgrp->closid;

248
	for_each_alloc_enabled_rdt_resource(r) {
249
		list_for_each_entry(dom, &r->domains, list)
250
			dom->have_new_ctrl = false;
251
	}
T
Tony Luck 已提交
252 253

	while ((tok = strsep(&buf, "\n")) != NULL) {
254
		resname = strim(strsep(&tok, ":"));
T
Tony Luck 已提交
255
		if (!tok) {
256
			rdt_last_cmd_puts("Missing ':'\n");
T
Tony Luck 已提交
257 258 259
			ret = -EINVAL;
			goto out;
		}
260 261 262 263 264
		if (tok[0] == '\0') {
			rdt_last_cmd_printf("Missing '%s' value\n", resname);
			ret = -EINVAL;
			goto out;
		}
265 266
		ret = rdtgroup_parse_resource(resname, tok, closid);
		if (ret)
T
Tony Luck 已提交
267 268 269
			goto out;
	}

270
	for_each_alloc_enabled_rdt_resource(r) {
T
Tony Luck 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
		ret = update_domains(r, closid);
		if (ret)
			goto out;
	}

out:
	rdtgroup_kn_unlock(of->kn);
	return ret ?: nbytes;
}

static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
{
	struct rdt_domain *dom;
	bool sep = false;

286
	seq_printf(s, "%*s:", max_name_width, r->name);
T
Tony Luck 已提交
287 288 289
	list_for_each_entry(dom, &r->domains, list) {
		if (sep)
			seq_puts(s, ";");
290
		seq_printf(s, r->format_str, dom->id, max_data_width,
291
			   dom->ctrl_val[closid]);
T
Tony Luck 已提交
292 293 294 295 296 297 298 299 300 301
		sep = true;
	}
	seq_puts(s, "\n");
}

int rdtgroup_schemata_show(struct kernfs_open_file *of,
			   struct seq_file *s, void *v)
{
	struct rdtgroup *rdtgrp;
	struct rdt_resource *r;
V
Vikas Shivappa 已提交
302 303
	int ret = 0;
	u32 closid;
T
Tony Luck 已提交
304 305 306 307

	rdtgrp = rdtgroup_kn_lock_live(of->kn);
	if (rdtgrp) {
		closid = rdtgrp->closid;
308
		for_each_alloc_enabled_rdt_resource(r) {
T
Tony Luck 已提交
309 310 311 312 313 314 315 316 317
			if (closid < r->num_closid)
				show_doms(s, r, closid);
		}
	} else {
		ret = -ENOENT;
	}
	rdtgroup_kn_unlock(of->kn);
	return ret;
}
V
Vikas Shivappa 已提交
318 319

void mon_event_read(struct rmid_read *rr, struct rdt_domain *d,
320
		    struct rdtgroup *rdtgrp, int evtid, int first)
V
Vikas Shivappa 已提交
321 322 323 324 325 326
{
	/*
	 * setup the parameters to send to the IPI to read the data.
	 */
	rr->rgrp = rdtgrp;
	rr->evtid = evtid;
327
	rr->d = d;
V
Vikas Shivappa 已提交
328
	rr->val = 0;
329
	rr->first = first;
V
Vikas Shivappa 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

	smp_call_function_any(&d->cpu_mask, mon_event_count, rr, 1);
}

int rdtgroup_mondata_show(struct seq_file *m, void *arg)
{
	struct kernfs_open_file *of = m->private;
	u32 resid, evtid, domid;
	struct rdtgroup *rdtgrp;
	struct rdt_resource *r;
	union mon_data_bits md;
	struct rdt_domain *d;
	struct rmid_read rr;
	int ret = 0;

	rdtgrp = rdtgroup_kn_lock_live(of->kn);

	md.priv = of->kn->priv;
	resid = md.u.rid;
	domid = md.u.domid;
	evtid = md.u.evtid;

	r = &rdt_resources_all[resid];
	d = rdt_find_domain(r, domid, NULL);
	if (!d) {
		ret = -ENOENT;
		goto out;
	}

359
	mon_event_read(&rr, d, rdtgrp, evtid, false);
V
Vikas Shivappa 已提交
360 361 362 363 364 365 366 367 368 369 370 371

	if (rr.val & RMID_VAL_ERROR)
		seq_puts(m, "Error\n");
	else if (rr.val & RMID_VAL_UNAVAIL)
		seq_puts(m, "Unavailable\n");
	else
		seq_printf(m, "%llu\n", rr.val * r->mon_scale);

out:
	rdtgroup_kn_unlock(of->kn);
	return ret;
}