ctrlmondata.c 11.8 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
/*
 * 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

26
#include <linux/cpu.h>
T
Tony Luck 已提交
27 28 29
#include <linux/kernfs.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
30
#include "internal.h"
T
Tony Luck 已提交
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * 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.
	 */
46 47
	if (!r->membw.delay_linear) {
		rdt_last_cmd_puts("No support for non-linear MB domains\n");
48
		return false;
49
	}
50 51

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

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

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

68 69
int parse_bw_intel(struct rdt_parse_data *data, struct rdt_resource *r,
		   struct rdt_domain *d)
70
{
71
	unsigned long bw_val;
72

73 74
	if (d->have_new_ctrl) {
		rdt_last_cmd_printf("duplicate domain %d\n", d->id);
75
		return -EINVAL;
76
	}
77

78
	if (!bw_validate(data->buf, &bw_val, r))
79
		return -EINVAL;
80
	d->new_ctrl = bw_val;
81 82 83 84 85
	d->have_new_ctrl = true;

	return 0;
}

T
Tony Luck 已提交
86 87 88 89 90 91
/*
 * 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.
 */
92
static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
T
Tony Luck 已提交
93
{
94
	unsigned long first_bit, zero_bit, val;
95
	unsigned int cbm_len = r->cache.cbm_len;
96 97 98
	int ret;

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

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

109 110
	first_bit = find_first_bit(&val, cbm_len);
	zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
T
Tony Luck 已提交
111

112 113
	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 已提交
114
		return false;
115
	}
T
Tony Luck 已提交
116

117 118 119
	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 已提交
120
		return false;
121
	}
122 123

	*data = val;
T
Tony Luck 已提交
124 125 126 127 128 129 130
	return true;
}

/*
 * Read one cache bit mask (hex). Check that it is valid for the current
 * resource type.
 */
131 132
int parse_cbm(struct rdt_parse_data *data, struct rdt_resource *r,
	      struct rdt_domain *d)
T
Tony Luck 已提交
133
{
134
	struct rdtgroup *rdtgrp = data->rdtgrp;
135
	u32 cbm_val;
T
Tony Luck 已提交
136

137 138
	if (d->have_new_ctrl) {
		rdt_last_cmd_printf("duplicate domain %d\n", d->id);
139
		return -EINVAL;
140
	}
141

142 143 144 145 146 147 148 149 150 151
	/*
	 * Cannot set up more than one pseudo-locked region in a cache
	 * hierarchy.
	 */
	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
	    rdtgroup_pseudo_locked_in_hierarchy(d)) {
		rdt_last_cmd_printf("pseudo-locked region in hierarchy\n");
		return -EINVAL;
	}

152 153 154
	if (!cbm_validate(data->buf, &cbm_val, r))
		return -EINVAL;

155 156 157 158 159 160 161
	if ((rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
	     rdtgrp->mode == RDT_MODE_SHAREABLE) &&
	    rdtgroup_cbm_overlaps_pseudo_locked(d, cbm_val)) {
		rdt_last_cmd_printf("CBM overlaps with pseudo-locked region\n");
		return -EINVAL;
	}

162 163 164 165 166 167
	/*
	 * The CBM may not overlap with the CBM of another closid if
	 * either is exclusive.
	 */
	if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, true)) {
		rdt_last_cmd_printf("overlaps with exclusive group\n");
T
Tony Luck 已提交
168
		return -EINVAL;
169 170 171
	}

	if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, false)) {
172 173
		if (rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
174 175 176 177
			rdt_last_cmd_printf("overlaps with other group\n");
			return -EINVAL;
		}
	}
178 179

	d->new_ctrl = cbm_val;
180
	d->have_new_ctrl = true;
T
Tony Luck 已提交
181 182 183 184 185 186 187

	return 0;
}

/*
 * For each domain in this resource we expect to find a series of:
 *	id=mask
188 189
 * separated by ";". The "id" is in decimal, and must match one of
 * the "id"s for this resource.
T
Tony Luck 已提交
190
 */
191 192
static int parse_line(char *line, struct rdt_resource *r,
		      struct rdtgroup *rdtgrp)
T
Tony Luck 已提交
193
{
194
	struct rdt_parse_data data;
T
Tony Luck 已提交
195 196 197 198
	char *dom = NULL, *id;
	struct rdt_domain *d;
	unsigned long dom_id;

199 200 201 202 203 204
	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
	    r->rid == RDT_RESOURCE_MBA) {
		rdt_last_cmd_puts("Cannot pseudo-lock MBA resource\n");
		return -EINVAL;
	}

205 206 207 208 209
next:
	if (!line || line[0] == '\0')
		return 0;
	dom = strsep(&line, ";");
	id = strsep(&dom, "=");
210 211
	if (!dom || kstrtoul(id, 10, &dom_id)) {
		rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
212
		return -EINVAL;
213
	}
214
	dom = strim(dom);
T
Tony Luck 已提交
215
	list_for_each_entry(d, &r->domains, list) {
216
		if (d->id == dom_id) {
217 218 219
			data.buf = dom;
			data.rdtgrp = rdtgrp;
			if (r->parse_ctrlval(&data, r, d))
220
				return -EINVAL;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
			if (rdtgrp->mode ==  RDT_MODE_PSEUDO_LOCKSETUP) {
				/*
				 * In pseudo-locking setup mode and just
				 * parsed a valid CBM that should be
				 * pseudo-locked. Only one locked region per
				 * resource group and domain so just do
				 * the required initialization for single
				 * region and return.
				 */
				rdtgrp->plr->r = r;
				rdtgrp->plr->d = d;
				rdtgrp->plr->cbm = d->new_ctrl;
				d->plr = rdtgrp->plr;
				return 0;
			}
236 237
			goto next;
		}
T
Tony Luck 已提交
238
	}
239
	return -EINVAL;
T
Tony Luck 已提交
240 241
}

242
int update_domains(struct rdt_resource *r, int closid)
T
Tony Luck 已提交
243 244 245 246
{
	struct msr_param msr_param;
	cpumask_var_t cpu_mask;
	struct rdt_domain *d;
247 248
	bool mba_sc;
	u32 *dc;
249
	int cpu;
T
Tony Luck 已提交
250 251 252 253 254 255 256 257

	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;

258
	mba_sc = is_mba_sc(r);
T
Tony Luck 已提交
259
	list_for_each_entry(d, &r->domains, list) {
260 261
		dc = !mba_sc ? d->ctrl_val : d->mbps_val;
		if (d->have_new_ctrl && d->new_ctrl != dc[closid]) {
262
			cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
263
			dc[closid] = d->new_ctrl;
264
		}
T
Tony Luck 已提交
265
	}
266 267 268 269 270 271

	/*
	 * Avoid writing the control msr with control values when
	 * MBA software controller is enabled
	 */
	if (cpumask_empty(cpu_mask) || mba_sc)
272
		goto done;
T
Tony Luck 已提交
273 274 275
	cpu = get_cpu();
	/* Update CBM on this cpu if it's in cpu_mask. */
	if (cpumask_test_cpu(cpu, cpu_mask))
276
		rdt_ctrl_update(&msr_param);
T
Tony Luck 已提交
277
	/* Update CBM on other cpus. */
278
	smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
T
Tony Luck 已提交
279 280
	put_cpu();

281
done:
T
Tony Luck 已提交
282 283 284 285 286
	free_cpumask_var(cpu_mask);

	return 0;
}

287 288
static int rdtgroup_parse_resource(char *resname, char *tok,
				   struct rdtgroup *rdtgrp)
289 290 291
{
	struct rdt_resource *r;

292
	for_each_alloc_enabled_rdt_resource(r) {
293 294
		if (!strcmp(resname, r->name) && rdtgrp->closid < r->num_closid)
			return parse_line(tok, r, rdtgrp);
295
	}
296
	rdt_last_cmd_printf("unknown/unsupported resource name '%s'\n", resname);
297 298 299
	return -EINVAL;
}

T
Tony Luck 已提交
300 301 302 303
ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
				char *buf, size_t nbytes, loff_t off)
{
	struct rdtgroup *rdtgrp;
304
	struct rdt_domain *dom;
T
Tony Luck 已提交
305 306
	struct rdt_resource *r;
	char *tok, *resname;
307
	int ret = 0;
T
Tony Luck 已提交
308 309

	/* Valid input requires a trailing newline */
310
	if (nbytes == 0 || buf[nbytes - 1] != '\n')
T
Tony Luck 已提交
311 312 313
		return -EINVAL;
	buf[nbytes - 1] = '\0';

314
	cpus_read_lock();
T
Tony Luck 已提交
315 316 317
	rdtgrp = rdtgroup_kn_lock_live(of->kn);
	if (!rdtgrp) {
		rdtgroup_kn_unlock(of->kn);
318
		cpus_read_unlock();
T
Tony Luck 已提交
319 320
		return -ENOENT;
	}
321
	rdt_last_cmd_clear();
T
Tony Luck 已提交
322

323 324 325 326 327 328 329 330 331 332
	/*
	 * No changes to pseudo-locked region allowed. It has to be removed
	 * and re-created instead.
	 */
	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
		ret = -EINVAL;
		rdt_last_cmd_puts("resource group is pseudo-locked\n");
		goto out;
	}

333
	for_each_alloc_enabled_rdt_resource(r) {
334
		list_for_each_entry(dom, &r->domains, list)
335
			dom->have_new_ctrl = false;
336
	}
T
Tony Luck 已提交
337 338

	while ((tok = strsep(&buf, "\n")) != NULL) {
339
		resname = strim(strsep(&tok, ":"));
T
Tony Luck 已提交
340
		if (!tok) {
341
			rdt_last_cmd_puts("Missing ':'\n");
T
Tony Luck 已提交
342 343 344
			ret = -EINVAL;
			goto out;
		}
345 346 347 348 349
		if (tok[0] == '\0') {
			rdt_last_cmd_printf("Missing '%s' value\n", resname);
			ret = -EINVAL;
			goto out;
		}
350
		ret = rdtgroup_parse_resource(resname, tok, rdtgrp);
351
		if (ret)
T
Tony Luck 已提交
352 353 354
			goto out;
	}

355
	for_each_alloc_enabled_rdt_resource(r) {
356
		ret = update_domains(r, rdtgrp->closid);
T
Tony Luck 已提交
357 358 359 360
		if (ret)
			goto out;
	}

361 362 363 364 365 366 367 368 369 370
	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
		/*
		 * If pseudo-locking fails we keep the resource group in
		 * mode RDT_MODE_PSEUDO_LOCKSETUP with its class of service
		 * active and updated for just the domain the pseudo-locked
		 * region was requested for.
		 */
		ret = rdtgroup_pseudo_lock_create(rdtgrp);
	}

T
Tony Luck 已提交
371 372
out:
	rdtgroup_kn_unlock(of->kn);
373
	cpus_read_unlock();
T
Tony Luck 已提交
374 375 376 377 378 379 380
	return ret ?: nbytes;
}

static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
{
	struct rdt_domain *dom;
	bool sep = false;
381
	u32 ctrl_val;
T
Tony Luck 已提交
382

383
	seq_printf(s, "%*s:", max_name_width, r->name);
T
Tony Luck 已提交
384 385 386
	list_for_each_entry(dom, &r->domains, list) {
		if (sep)
			seq_puts(s, ";");
387 388 389

		ctrl_val = (!is_mba_sc(r) ? dom->ctrl_val[closid] :
			    dom->mbps_val[closid]);
390
		seq_printf(s, r->format_str, dom->id, max_data_width,
391
			   ctrl_val);
T
Tony Luck 已提交
392 393 394 395 396 397 398 399 400 401
		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 已提交
402 403
	int ret = 0;
	u32 closid;
T
Tony Luck 已提交
404 405 406

	rdtgrp = rdtgroup_kn_lock_live(of->kn);
	if (rdtgrp) {
407 408 409
		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
			for_each_alloc_enabled_rdt_resource(r)
				seq_printf(s, "%s:uninitialized\n", r->name);
410
		} else if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
411 412 413 414 415 416 417 418 419 420
			if (!rdtgrp->plr->d) {
				rdt_last_cmd_clear();
				rdt_last_cmd_puts("Cache domain offline\n");
				ret = -ENODEV;
			} else {
				seq_printf(s, "%s:%d=%x\n",
					   rdtgrp->plr->r->name,
					   rdtgrp->plr->d->id,
					   rdtgrp->plr->cbm);
			}
421 422 423 424 425 426
		} else {
			closid = rdtgrp->closid;
			for_each_alloc_enabled_rdt_resource(r) {
				if (closid < r->num_closid)
					show_doms(s, r, closid);
			}
T
Tony Luck 已提交
427 428 429 430 431 432 433
		}
	} else {
		ret = -ENOENT;
	}
	rdtgroup_kn_unlock(of->kn);
	return ret;
}
V
Vikas Shivappa 已提交
434 435

void mon_event_read(struct rmid_read *rr, struct rdt_domain *d,
436
		    struct rdtgroup *rdtgrp, int evtid, int first)
V
Vikas Shivappa 已提交
437 438 439 440 441 442
{
	/*
	 * setup the parameters to send to the IPI to read the data.
	 */
	rr->rgrp = rdtgrp;
	rr->evtid = evtid;
443
	rr->d = d;
V
Vikas Shivappa 已提交
444
	rr->val = 0;
445
	rr->first = first;
V
Vikas Shivappa 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

	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);
462 463 464 465
	if (!rdtgrp) {
		ret = -ENOENT;
		goto out;
	}
V
Vikas Shivappa 已提交
466 467 468 469 470 471 472 473

	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);
474
	if (IS_ERR_OR_NULL(d)) {
V
Vikas Shivappa 已提交
475 476 477 478
		ret = -ENOENT;
		goto out;
	}

479
	mon_event_read(&rr, d, rdtgrp, evtid, false);
V
Vikas Shivappa 已提交
480 481 482 483 484 485 486 487 488 489 490 491

	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;
}