Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Resource Director Technology(RDT)
  4 * - Cache Allocation code.
  5 *
  6 * Copyright (C) 2016 Intel Corporation
  7 *
  8 * Authors:
  9 *    Fenghua Yu <fenghua.yu@intel.com>
 10 *    Tony Luck <tony.luck@intel.com>
 11 *
 12 * More information about RDT be found in the Intel (R) x86 Architecture
 13 * Software Developer Manual June 2016, volume 3, section 17.17.
 14 */
 15
 16#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
 17
 18#include <linux/cpu.h>
 19#include <linux/kernfs.h>
 20#include <linux/seq_file.h>
 21#include <linux/slab.h>
 22#include "internal.h"
 23
 24/*
 25 * Check whether MBA bandwidth percentage value is correct. The value is
 26 * checked against the minimum and maximum bandwidth values specified by
 27 * the hardware. The allocated bandwidth percentage is rounded to the next
 28 * control step available on the hardware.
 29 */
 30static bool bw_validate_amd(char *buf, unsigned long *data,
 31			    struct rdt_resource *r)
 32{
 33	unsigned long bw;
 34	int ret;
 35
 36	ret = kstrtoul(buf, 10, &bw);
 37	if (ret) {
 38		rdt_last_cmd_printf("Non-decimal digit in MB value %s\n", buf);
 39		return false;
 40	}
 41
 42	if (bw < r->membw.min_bw || bw > r->default_ctrl) {
 43		rdt_last_cmd_printf("MB value %ld out of range [%d,%d]\n", bw,
 44				    r->membw.min_bw, r->default_ctrl);
 45		return false;
 46	}
 47
 48	*data = roundup(bw, (unsigned long)r->membw.bw_gran);
 49	return true;
 50}
 51
 52int parse_bw_amd(struct rdt_parse_data *data, struct rdt_resource *r,
 53		 struct rdt_domain *d)
 54{
 55	unsigned long bw_val;
 56
 57	if (d->have_new_ctrl) {
 58		rdt_last_cmd_printf("Duplicate domain %d\n", d->id);
 59		return -EINVAL;
 60	}
 61
 62	if (!bw_validate_amd(data->buf, &bw_val, r))
 63		return -EINVAL;
 64
 65	d->new_ctrl = bw_val;
 66	d->have_new_ctrl = true;
 67
 68	return 0;
 69}
 70
 71/*
 72 * Check whether MBA bandwidth percentage value is correct. The value is
 73 * checked against the minimum and max bandwidth values specified by the
 74 * hardware. The allocated bandwidth percentage is rounded to the next
 75 * control step available on the hardware.
 76 */
 77static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
 78{
 79	unsigned long bw;
 80	int ret;
 81
 82	/*
 83	 * Only linear delay values is supported for current Intel SKUs.
 84	 */
 85	if (!r->membw.delay_linear) {
 86		rdt_last_cmd_puts("No support for non-linear MB domains\n");
 87		return false;
 88	}
 89
 90	ret = kstrtoul(buf, 10, &bw);
 91	if (ret) {
 92		rdt_last_cmd_printf("Non-decimal digit in MB value %s\n", buf);
 93		return false;
 94	}
 95
 96	if ((bw < r->membw.min_bw || bw > r->default_ctrl) &&
 97	    !is_mba_sc(r)) {
 98		rdt_last_cmd_printf("MB value %ld out of range [%d,%d]\n", bw,
 99				    r->membw.min_bw, r->default_ctrl);
100		return false;
101	}
102
103	*data = roundup(bw, (unsigned long)r->membw.bw_gran);
104	return true;
105}
106
107int parse_bw_intel(struct rdt_parse_data *data, struct rdt_resource *r,
108		   struct rdt_domain *d)
109{
 
 
 
110	unsigned long bw_val;
111
112	if (d->have_new_ctrl) {
 
113		rdt_last_cmd_printf("Duplicate domain %d\n", d->id);
114		return -EINVAL;
115	}
116
117	if (!bw_validate(data->buf, &bw_val, r))
118		return -EINVAL;
119	d->new_ctrl = bw_val;
120	d->have_new_ctrl = true;
 
 
 
 
 
 
121
122	return 0;
123}
124
125/*
126 * Check whether a cache bit mask is valid. The SDM says:
127 *	Please note that all (and only) contiguous '1' combinations
128 *	are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
129 * Additionally Haswell requires at least two bits set.
 
 
 
 
130 */
131bool cbm_validate_intel(char *buf, u32 *data, struct rdt_resource *r)
132{
133	unsigned long first_bit, zero_bit, val;
134	unsigned int cbm_len = r->cache.cbm_len;
135	int ret;
136
137	ret = kstrtoul(buf, 16, &val);
138	if (ret) {
139		rdt_last_cmd_printf("Non-hex character in the mask %s\n", buf);
140		return false;
141	}
142
143	if (val == 0 || val > r->default_ctrl) {
144		rdt_last_cmd_puts("Mask out of range\n");
145		return false;
146	}
147
148	first_bit = find_first_bit(&val, cbm_len);
149	zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
150
151	if (find_next_bit(&val, cbm_len, zero_bit) < cbm_len) {
 
 
152		rdt_last_cmd_printf("The mask %lx has non-consecutive 1-bits\n", val);
153		return false;
154	}
155
156	if ((zero_bit - first_bit) < r->cache.min_cbm_bits) {
157		rdt_last_cmd_printf("Need at least %d bits in the mask\n",
158				    r->cache.min_cbm_bits);
159		return false;
160	}
161
162	*data = val;
163	return true;
164}
165
166/*
167 * Check whether a cache bit mask is valid. AMD allows non-contiguous
168 * bitmasks
169 */
170bool cbm_validate_amd(char *buf, u32 *data, struct rdt_resource *r)
171{
172	unsigned long val;
173	int ret;
174
175	ret = kstrtoul(buf, 16, &val);
176	if (ret) {
177		rdt_last_cmd_printf("Non-hex character in the mask %s\n", buf);
178		return false;
179	}
180
181	if (val > r->default_ctrl) {
182		rdt_last_cmd_puts("Mask out of range\n");
183		return false;
184	}
185
186	*data = val;
187	return true;
188}
189
190/*
191 * Read one cache bit mask (hex). Check that it is valid for the current
192 * resource type.
193 */
194int parse_cbm(struct rdt_parse_data *data, struct rdt_resource *r,
195	      struct rdt_domain *d)
196{
197	struct rdtgroup *rdtgrp = data->rdtgrp;
 
 
198	u32 cbm_val;
199
200	if (d->have_new_ctrl) {
 
201		rdt_last_cmd_printf("Duplicate domain %d\n", d->id);
202		return -EINVAL;
203	}
204
205	/*
206	 * Cannot set up more than one pseudo-locked region in a cache
207	 * hierarchy.
208	 */
209	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
210	    rdtgroup_pseudo_locked_in_hierarchy(d)) {
211		rdt_last_cmd_puts("Pseudo-locked region in hierarchy\n");
212		return -EINVAL;
213	}
214
215	if (!r->cbm_validate(data->buf, &cbm_val, r))
216		return -EINVAL;
217
218	if ((rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
219	     rdtgrp->mode == RDT_MODE_SHAREABLE) &&
220	    rdtgroup_cbm_overlaps_pseudo_locked(d, cbm_val)) {
221		rdt_last_cmd_puts("CBM overlaps with pseudo-locked region\n");
222		return -EINVAL;
223	}
224
225	/*
226	 * The CBM may not overlap with the CBM of another closid if
227	 * either is exclusive.
228	 */
229	if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, true)) {
230		rdt_last_cmd_puts("Overlaps with exclusive group\n");
231		return -EINVAL;
232	}
233
234	if (rdtgroup_cbm_overlaps(r, d, cbm_val, rdtgrp->closid, false)) {
235		if (rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
236		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
237			rdt_last_cmd_puts("Overlaps with other group\n");
238			return -EINVAL;
239		}
240	}
241
242	d->new_ctrl = cbm_val;
243	d->have_new_ctrl = true;
244
245	return 0;
246}
247
248/*
249 * For each domain in this resource we expect to find a series of:
250 *	id=mask
251 * separated by ";". The "id" is in decimal, and must match one of
252 * the "id"s for this resource.
253 */
254static int parse_line(char *line, struct rdt_resource *r,
255		      struct rdtgroup *rdtgrp)
256{
 
 
 
257	struct rdt_parse_data data;
258	char *dom = NULL, *id;
259	struct rdt_domain *d;
260	unsigned long dom_id;
261
 
 
 
262	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
263	    r->rid == RDT_RESOURCE_MBA) {
264		rdt_last_cmd_puts("Cannot pseudo-lock MBA resource\n");
265		return -EINVAL;
266	}
267
268next:
269	if (!line || line[0] == '\0')
270		return 0;
271	dom = strsep(&line, ";");
272	id = strsep(&dom, "=");
273	if (!dom || kstrtoul(id, 10, &dom_id)) {
274		rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
275		return -EINVAL;
276	}
277	dom = strim(dom);
278	list_for_each_entry(d, &r->domains, list) {
279		if (d->id == dom_id) {
280			data.buf = dom;
281			data.rdtgrp = rdtgrp;
282			if (r->parse_ctrlval(&data, r, d))
283				return -EINVAL;
284			if (rdtgrp->mode ==  RDT_MODE_PSEUDO_LOCKSETUP) {
 
285				/*
286				 * In pseudo-locking setup mode and just
287				 * parsed a valid CBM that should be
288				 * pseudo-locked. Only one locked region per
289				 * resource group and domain so just do
290				 * the required initialization for single
291				 * region and return.
292				 */
293				rdtgrp->plr->r = r;
294				rdtgrp->plr->d = d;
295				rdtgrp->plr->cbm = d->new_ctrl;
296				d->plr = rdtgrp->plr;
297				return 0;
298			}
299			goto next;
300		}
301	}
302	return -EINVAL;
303}
304
305int update_domains(struct rdt_resource *r, int closid)
306{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307	struct msr_param msr_param;
 
308	cpumask_var_t cpu_mask;
309	struct rdt_domain *d;
310	bool mba_sc;
311	u32 *dc;
312	int cpu;
 
313
314	if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
315		return -ENOMEM;
316
317	msr_param.low = closid;
318	msr_param.high = msr_param.low + 1;
319	msr_param.res = r;
320
321	mba_sc = is_mba_sc(r);
322	list_for_each_entry(d, &r->domains, list) {
323		dc = !mba_sc ? d->ctrl_val : d->mbps_val;
324		if (d->have_new_ctrl && d->new_ctrl != dc[closid]) {
325			cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
326			dc[closid] = d->new_ctrl;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327		}
328	}
329
330	/*
331	 * Avoid writing the control msr with control values when
332	 * MBA software controller is enabled
333	 */
334	if (cpumask_empty(cpu_mask) || mba_sc)
335		goto done;
336	cpu = get_cpu();
337	/* Update resource control msr on this CPU if it's in cpu_mask. */
338	if (cpumask_test_cpu(cpu, cpu_mask))
339		rdt_ctrl_update(&msr_param);
340	/* Update resource control msr on other CPUs. */
341	smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
342	put_cpu();
343
344done:
345	free_cpumask_var(cpu_mask);
346
347	return 0;
348}
349
350static int rdtgroup_parse_resource(char *resname, char *tok,
351				   struct rdtgroup *rdtgrp)
352{
353	struct rdt_resource *r;
354
355	for_each_alloc_enabled_rdt_resource(r) {
356		if (!strcmp(resname, r->name) && rdtgrp->closid < r->num_closid)
357			return parse_line(tok, r, rdtgrp);
358	}
359	rdt_last_cmd_printf("Unknown or unsupported resource name '%s'\n", resname);
360	return -EINVAL;
361}
362
363ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
364				char *buf, size_t nbytes, loff_t off)
365{
 
366	struct rdtgroup *rdtgrp;
367	struct rdt_domain *dom;
368	struct rdt_resource *r;
369	char *tok, *resname;
370	int ret = 0;
371
372	/* Valid input requires a trailing newline */
373	if (nbytes == 0 || buf[nbytes - 1] != '\n')
374		return -EINVAL;
375	buf[nbytes - 1] = '\0';
376
377	cpus_read_lock();
378	rdtgrp = rdtgroup_kn_lock_live(of->kn);
379	if (!rdtgrp) {
380		rdtgroup_kn_unlock(of->kn);
381		cpus_read_unlock();
382		return -ENOENT;
383	}
384	rdt_last_cmd_clear();
385
386	/*
387	 * No changes to pseudo-locked region allowed. It has to be removed
388	 * and re-created instead.
389	 */
390	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
391		ret = -EINVAL;
392		rdt_last_cmd_puts("Resource group is pseudo-locked\n");
393		goto out;
394	}
395
396	for_each_alloc_enabled_rdt_resource(r) {
397		list_for_each_entry(dom, &r->domains, list)
398			dom->have_new_ctrl = false;
399	}
400
401	while ((tok = strsep(&buf, "\n")) != NULL) {
402		resname = strim(strsep(&tok, ":"));
403		if (!tok) {
404			rdt_last_cmd_puts("Missing ':'\n");
405			ret = -EINVAL;
406			goto out;
407		}
408		if (tok[0] == '\0') {
409			rdt_last_cmd_printf("Missing '%s' value\n", resname);
410			ret = -EINVAL;
411			goto out;
412		}
413		ret = rdtgroup_parse_resource(resname, tok, rdtgrp);
414		if (ret)
415			goto out;
416	}
417
418	for_each_alloc_enabled_rdt_resource(r) {
419		ret = update_domains(r, rdtgrp->closid);
 
 
 
 
 
 
 
 
 
420		if (ret)
421			goto out;
422	}
423
424	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
425		/*
426		 * If pseudo-locking fails we keep the resource group in
427		 * mode RDT_MODE_PSEUDO_LOCKSETUP with its class of service
428		 * active and updated for just the domain the pseudo-locked
429		 * region was requested for.
430		 */
431		ret = rdtgroup_pseudo_lock_create(rdtgrp);
432	}
433
434out:
 
435	rdtgroup_kn_unlock(of->kn);
436	cpus_read_unlock();
437	return ret ?: nbytes;
438}
439
440static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
 
441{
 
 
 
 
 
 
 
 
 
442	struct rdt_domain *dom;
443	bool sep = false;
444	u32 ctrl_val;
445
446	seq_printf(s, "%*s:", max_name_width, r->name);
 
 
 
447	list_for_each_entry(dom, &r->domains, list) {
448		if (sep)
449			seq_puts(s, ";");
450
451		ctrl_val = (!is_mba_sc(r) ? dom->ctrl_val[closid] :
452			    dom->mbps_val[closid]);
 
 
 
 
453		seq_printf(s, r->format_str, dom->id, max_data_width,
454			   ctrl_val);
455		sep = true;
456	}
457	seq_puts(s, "\n");
458}
459
460int rdtgroup_schemata_show(struct kernfs_open_file *of,
461			   struct seq_file *s, void *v)
462{
 
463	struct rdtgroup *rdtgrp;
464	struct rdt_resource *r;
465	int ret = 0;
466	u32 closid;
467
468	rdtgrp = rdtgroup_kn_lock_live(of->kn);
469	if (rdtgrp) {
470		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
471			for_each_alloc_enabled_rdt_resource(r)
472				seq_printf(s, "%s:uninitialized\n", r->name);
 
473		} else if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
474			if (!rdtgrp->plr->d) {
475				rdt_last_cmd_clear();
476				rdt_last_cmd_puts("Cache domain offline\n");
477				ret = -ENODEV;
478			} else {
479				seq_printf(s, "%s:%d=%x\n",
480					   rdtgrp->plr->r->name,
481					   rdtgrp->plr->d->id,
482					   rdtgrp->plr->cbm);
483			}
484		} else {
485			closid = rdtgrp->closid;
486			for_each_alloc_enabled_rdt_resource(r) {
487				if (closid < r->num_closid)
488					show_doms(s, r, closid);
489			}
490		}
491	} else {
492		ret = -ENOENT;
493	}
494	rdtgroup_kn_unlock(of->kn);
495	return ret;
496}
497
498void mon_event_read(struct rmid_read *rr, struct rdt_domain *d,
499		    struct rdtgroup *rdtgrp, int evtid, int first)
 
 
 
 
 
 
 
 
500{
 
 
 
 
 
501	/*
502	 * setup the parameters to send to the IPI to read the data.
503	 */
504	rr->rgrp = rdtgrp;
505	rr->evtid = evtid;
 
506	rr->d = d;
507	rr->val = 0;
508	rr->first = first;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
509
510	smp_call_function_any(&d->cpu_mask, mon_event_count, rr, 1);
511}
512
513int rdtgroup_mondata_show(struct seq_file *m, void *arg)
514{
515	struct kernfs_open_file *of = m->private;
516	u32 resid, evtid, domid;
517	struct rdtgroup *rdtgrp;
518	struct rdt_resource *r;
519	union mon_data_bits md;
520	struct rdt_domain *d;
521	struct rmid_read rr;
522	int ret = 0;
523
524	rdtgrp = rdtgroup_kn_lock_live(of->kn);
525	if (!rdtgrp) {
526		ret = -ENOENT;
527		goto out;
528	}
529
530	md.priv = of->kn->priv;
531	resid = md.u.rid;
532	domid = md.u.domid;
533	evtid = md.u.evtid;
534
535	r = &rdt_resources_all[resid];
536	d = rdt_find_domain(r, domid, NULL);
537	if (IS_ERR_OR_NULL(d)) {
538		ret = -ENOENT;
539		goto out;
540	}
541
542	mon_event_read(&rr, d, rdtgrp, evtid, false);
543
544	if (rr.val & RMID_VAL_ERROR)
545		seq_puts(m, "Error\n");
546	else if (rr.val & RMID_VAL_UNAVAIL)
547		seq_puts(m, "Unavailable\n");
548	else
549		seq_printf(m, "%llu\n", rr.val * r->mon_scale);
550
551out:
552	rdtgroup_kn_unlock(of->kn);
553	return ret;
554}
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Resource Director Technology(RDT)
  4 * - Cache Allocation code.
  5 *
  6 * Copyright (C) 2016 Intel Corporation
  7 *
  8 * Authors:
  9 *    Fenghua Yu <fenghua.yu@intel.com>
 10 *    Tony Luck <tony.luck@intel.com>
 11 *
 12 * More information about RDT be found in the Intel (R) x86 Architecture
 13 * Software Developer Manual June 2016, volume 3, section 17.17.
 14 */
 15
 16#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
 17
 18#include <linux/cpu.h>
 19#include <linux/kernfs.h>
 20#include <linux/seq_file.h>
 21#include <linux/slab.h>
 22#include <linux/tick.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 23
 24#include "internal.h"
 
 
 
 
 
 
 
 25
 26/*
 27 * Check whether MBA bandwidth percentage value is correct. The value is
 28 * checked against the minimum and max bandwidth values specified by the
 29 * hardware. The allocated bandwidth percentage is rounded to the next
 30 * control step available on the hardware.
 31 */
 32static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
 33{
 34	unsigned long bw;
 35	int ret;
 36
 37	/*
 38	 * Only linear delay values is supported for current Intel SKUs.
 39	 */
 40	if (!r->membw.delay_linear && r->membw.arch_needs_linear) {
 41		rdt_last_cmd_puts("No support for non-linear MB domains\n");
 42		return false;
 43	}
 44
 45	ret = kstrtoul(buf, 10, &bw);
 46	if (ret) {
 47		rdt_last_cmd_printf("Non-decimal digit in MB value %s\n", buf);
 48		return false;
 49	}
 50
 51	if ((bw < r->membw.min_bw || bw > r->default_ctrl) &&
 52	    !is_mba_sc(r)) {
 53		rdt_last_cmd_printf("MB value %ld out of range [%d,%d]\n", bw,
 54				    r->membw.min_bw, r->default_ctrl);
 55		return false;
 56	}
 57
 58	*data = roundup(bw, (unsigned long)r->membw.bw_gran);
 59	return true;
 60}
 61
 62int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
 63	     struct rdt_domain *d)
 64{
 65	struct resctrl_staged_config *cfg;
 66	u32 closid = data->rdtgrp->closid;
 67	struct rdt_resource *r = s->res;
 68	unsigned long bw_val;
 69
 70	cfg = &d->staged_config[s->conf_type];
 71	if (cfg->have_new_ctrl) {
 72		rdt_last_cmd_printf("Duplicate domain %d\n", d->id);
 73		return -EINVAL;
 74	}
 75
 76	if (!bw_validate(data->buf, &bw_val, r))
 77		return -EINVAL;
 78
 79	if (is_mba_sc(r)) {
 80		d->mbps_val[closid] = bw_val;
 81		return 0;
 82	}
 83
 84	cfg->new_ctrl = bw_val;
 85	cfg->have_new_ctrl = true;
 86
 87	return 0;
 88}
 89
 90/*
 91 * Check whether a cache bit mask is valid.
 92 * On Intel CPUs, non-contiguous 1s value support is indicated by CPUID:
 93 *   - CPUID.0x10.1:ECX[3]: L3 non-contiguous 1s value supported if 1
 94 *   - CPUID.0x10.2:ECX[3]: L2 non-contiguous 1s value supported if 1
 95 *
 96 * Haswell does not support a non-contiguous 1s value and additionally
 97 * requires at least two bits set.
 98 * AMD allows non-contiguous bitmasks.
 99 */
100static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
101{
102	unsigned long first_bit, zero_bit, val;
103	unsigned int cbm_len = r->cache.cbm_len;
104	int ret;
105
106	ret = kstrtoul(buf, 16, &val);
107	if (ret) {
108		rdt_last_cmd_printf("Non-hex character in the mask %s\n", buf);
109		return false;
110	}
111
112	if ((r->cache.min_cbm_bits > 0 && val == 0) || val > r->default_ctrl) {
113		rdt_last_cmd_puts("Mask out of range\n");
114		return false;
115	}
116
117	first_bit = find_first_bit(&val, cbm_len);
118	zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
119
120	/* Are non-contiguous bitmasks allowed? */
121	if (!r->cache.arch_has_sparse_bitmasks &&
122	    (find_next_bit(&val, cbm_len, zero_bit) < cbm_len)) {
123		rdt_last_cmd_printf("The mask %lx has non-consecutive 1-bits\n", val);
124		return false;
125	}
126
127	if ((zero_bit - first_bit) < r->cache.min_cbm_bits) {
128		rdt_last_cmd_printf("Need at least %d bits in the mask\n",
129				    r->cache.min_cbm_bits);
130		return false;
131	}
132
133	*data = val;
134	return true;
135}
136
137/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138 * Read one cache bit mask (hex). Check that it is valid for the current
139 * resource type.
140 */
141int parse_cbm(struct rdt_parse_data *data, struct resctrl_schema *s,
142	      struct rdt_domain *d)
143{
144	struct rdtgroup *rdtgrp = data->rdtgrp;
145	struct resctrl_staged_config *cfg;
146	struct rdt_resource *r = s->res;
147	u32 cbm_val;
148
149	cfg = &d->staged_config[s->conf_type];
150	if (cfg->have_new_ctrl) {
151		rdt_last_cmd_printf("Duplicate domain %d\n", d->id);
152		return -EINVAL;
153	}
154
155	/*
156	 * Cannot set up more than one pseudo-locked region in a cache
157	 * hierarchy.
158	 */
159	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
160	    rdtgroup_pseudo_locked_in_hierarchy(d)) {
161		rdt_last_cmd_puts("Pseudo-locked region in hierarchy\n");
162		return -EINVAL;
163	}
164
165	if (!cbm_validate(data->buf, &cbm_val, r))
166		return -EINVAL;
167
168	if ((rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
169	     rdtgrp->mode == RDT_MODE_SHAREABLE) &&
170	    rdtgroup_cbm_overlaps_pseudo_locked(d, cbm_val)) {
171		rdt_last_cmd_puts("CBM overlaps with pseudo-locked region\n");
172		return -EINVAL;
173	}
174
175	/*
176	 * The CBM may not overlap with the CBM of another closid if
177	 * either is exclusive.
178	 */
179	if (rdtgroup_cbm_overlaps(s, d, cbm_val, rdtgrp->closid, true)) {
180		rdt_last_cmd_puts("Overlaps with exclusive group\n");
181		return -EINVAL;
182	}
183
184	if (rdtgroup_cbm_overlaps(s, d, cbm_val, rdtgrp->closid, false)) {
185		if (rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
186		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
187			rdt_last_cmd_puts("Overlaps with other group\n");
188			return -EINVAL;
189		}
190	}
191
192	cfg->new_ctrl = cbm_val;
193	cfg->have_new_ctrl = true;
194
195	return 0;
196}
197
198/*
199 * For each domain in this resource we expect to find a series of:
200 *	id=mask
201 * separated by ";". The "id" is in decimal, and must match one of
202 * the "id"s for this resource.
203 */
204static int parse_line(char *line, struct resctrl_schema *s,
205		      struct rdtgroup *rdtgrp)
206{
207	enum resctrl_conf_type t = s->conf_type;
208	struct resctrl_staged_config *cfg;
209	struct rdt_resource *r = s->res;
210	struct rdt_parse_data data;
211	char *dom = NULL, *id;
212	struct rdt_domain *d;
213	unsigned long dom_id;
214
215	/* Walking r->domains, ensure it can't race with cpuhp */
216	lockdep_assert_cpus_held();
217
218	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
219	    (r->rid == RDT_RESOURCE_MBA || r->rid == RDT_RESOURCE_SMBA)) {
220		rdt_last_cmd_puts("Cannot pseudo-lock MBA resource\n");
221		return -EINVAL;
222	}
223
224next:
225	if (!line || line[0] == '\0')
226		return 0;
227	dom = strsep(&line, ";");
228	id = strsep(&dom, "=");
229	if (!dom || kstrtoul(id, 10, &dom_id)) {
230		rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
231		return -EINVAL;
232	}
233	dom = strim(dom);
234	list_for_each_entry(d, &r->domains, list) {
235		if (d->id == dom_id) {
236			data.buf = dom;
237			data.rdtgrp = rdtgrp;
238			if (r->parse_ctrlval(&data, s, d))
239				return -EINVAL;
240			if (rdtgrp->mode ==  RDT_MODE_PSEUDO_LOCKSETUP) {
241				cfg = &d->staged_config[t];
242				/*
243				 * In pseudo-locking setup mode and just
244				 * parsed a valid CBM that should be
245				 * pseudo-locked. Only one locked region per
246				 * resource group and domain so just do
247				 * the required initialization for single
248				 * region and return.
249				 */
250				rdtgrp->plr->s = s;
251				rdtgrp->plr->d = d;
252				rdtgrp->plr->cbm = cfg->new_ctrl;
253				d->plr = rdtgrp->plr;
254				return 0;
255			}
256			goto next;
257		}
258	}
259	return -EINVAL;
260}
261
262static u32 get_config_index(u32 closid, enum resctrl_conf_type type)
263{
264	switch (type) {
265	default:
266	case CDP_NONE:
267		return closid;
268	case CDP_CODE:
269		return closid * 2 + 1;
270	case CDP_DATA:
271		return closid * 2;
272	}
273}
274
275static bool apply_config(struct rdt_hw_domain *hw_dom,
276			 struct resctrl_staged_config *cfg, u32 idx,
277			 cpumask_var_t cpu_mask)
278{
279	struct rdt_domain *dom = &hw_dom->d_resctrl;
280
281	if (cfg->new_ctrl != hw_dom->ctrl_val[idx]) {
282		cpumask_set_cpu(cpumask_any(&dom->cpu_mask), cpu_mask);
283		hw_dom->ctrl_val[idx] = cfg->new_ctrl;
284
285		return true;
286	}
287
288	return false;
289}
290
291int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_domain *d,
292			    u32 closid, enum resctrl_conf_type t, u32 cfg_val)
293{
294	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
295	struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
296	u32 idx = get_config_index(closid, t);
297	struct msr_param msr_param;
298
299	if (!cpumask_test_cpu(smp_processor_id(), &d->cpu_mask))
300		return -EINVAL;
301
302	hw_dom->ctrl_val[idx] = cfg_val;
303
304	msr_param.res = r;
305	msr_param.low = idx;
306	msr_param.high = idx + 1;
307	hw_res->msr_update(d, &msr_param, r);
308
309	return 0;
310}
311
312int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid)
313{
314	struct resctrl_staged_config *cfg;
315	struct rdt_hw_domain *hw_dom;
316	struct msr_param msr_param;
317	enum resctrl_conf_type t;
318	cpumask_var_t cpu_mask;
319	struct rdt_domain *d;
320	u32 idx;
321
322	/* Walking r->domains, ensure it can't race with cpuhp */
323	lockdep_assert_cpus_held();
324
325	if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
326		return -ENOMEM;
327
328	msr_param.res = NULL;
 
 
 
 
329	list_for_each_entry(d, &r->domains, list) {
330		hw_dom = resctrl_to_arch_dom(d);
331		for (t = 0; t < CDP_NUM_TYPES; t++) {
332			cfg = &hw_dom->d_resctrl.staged_config[t];
333			if (!cfg->have_new_ctrl)
334				continue;
335
336			idx = get_config_index(closid, t);
337			if (!apply_config(hw_dom, cfg, idx, cpu_mask))
338				continue;
339
340			if (!msr_param.res) {
341				msr_param.low = idx;
342				msr_param.high = msr_param.low + 1;
343				msr_param.res = r;
344			} else {
345				msr_param.low = min(msr_param.low, idx);
346				msr_param.high = max(msr_param.high, idx + 1);
347			}
348		}
349	}
350
351	if (cpumask_empty(cpu_mask))
 
 
 
 
352		goto done;
353
354	/* Update resource control msr on all the CPUs. */
355	on_each_cpu_mask(cpu_mask, rdt_ctrl_update, &msr_param, 1);
 
 
 
 
356
357done:
358	free_cpumask_var(cpu_mask);
359
360	return 0;
361}
362
363static int rdtgroup_parse_resource(char *resname, char *tok,
364				   struct rdtgroup *rdtgrp)
365{
366	struct resctrl_schema *s;
367
368	list_for_each_entry(s, &resctrl_schema_all, list) {
369		if (!strcmp(resname, s->name) && rdtgrp->closid < s->num_closid)
370			return parse_line(tok, s, rdtgrp);
371	}
372	rdt_last_cmd_printf("Unknown or unsupported resource name '%s'\n", resname);
373	return -EINVAL;
374}
375
376ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
377				char *buf, size_t nbytes, loff_t off)
378{
379	struct resctrl_schema *s;
380	struct rdtgroup *rdtgrp;
 
381	struct rdt_resource *r;
382	char *tok, *resname;
383	int ret = 0;
384
385	/* Valid input requires a trailing newline */
386	if (nbytes == 0 || buf[nbytes - 1] != '\n')
387		return -EINVAL;
388	buf[nbytes - 1] = '\0';
389
 
390	rdtgrp = rdtgroup_kn_lock_live(of->kn);
391	if (!rdtgrp) {
392		rdtgroup_kn_unlock(of->kn);
 
393		return -ENOENT;
394	}
395	rdt_last_cmd_clear();
396
397	/*
398	 * No changes to pseudo-locked region allowed. It has to be removed
399	 * and re-created instead.
400	 */
401	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
402		ret = -EINVAL;
403		rdt_last_cmd_puts("Resource group is pseudo-locked\n");
404		goto out;
405	}
406
407	rdt_staged_configs_clear();
 
 
 
408
409	while ((tok = strsep(&buf, "\n")) != NULL) {
410		resname = strim(strsep(&tok, ":"));
411		if (!tok) {
412			rdt_last_cmd_puts("Missing ':'\n");
413			ret = -EINVAL;
414			goto out;
415		}
416		if (tok[0] == '\0') {
417			rdt_last_cmd_printf("Missing '%s' value\n", resname);
418			ret = -EINVAL;
419			goto out;
420		}
421		ret = rdtgroup_parse_resource(resname, tok, rdtgrp);
422		if (ret)
423			goto out;
424	}
425
426	list_for_each_entry(s, &resctrl_schema_all, list) {
427		r = s->res;
428
429		/*
430		 * Writes to mba_sc resources update the software controller,
431		 * not the control MSR.
432		 */
433		if (is_mba_sc(r))
434			continue;
435
436		ret = resctrl_arch_update_domains(r, rdtgrp->closid);
437		if (ret)
438			goto out;
439	}
440
441	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
442		/*
443		 * If pseudo-locking fails we keep the resource group in
444		 * mode RDT_MODE_PSEUDO_LOCKSETUP with its class of service
445		 * active and updated for just the domain the pseudo-locked
446		 * region was requested for.
447		 */
448		ret = rdtgroup_pseudo_lock_create(rdtgrp);
449	}
450
451out:
452	rdt_staged_configs_clear();
453	rdtgroup_kn_unlock(of->kn);
 
454	return ret ?: nbytes;
455}
456
457u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_domain *d,
458			    u32 closid, enum resctrl_conf_type type)
459{
460	struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
461	u32 idx = get_config_index(closid, type);
462
463	return hw_dom->ctrl_val[idx];
464}
465
466static void show_doms(struct seq_file *s, struct resctrl_schema *schema, int closid)
467{
468	struct rdt_resource *r = schema->res;
469	struct rdt_domain *dom;
470	bool sep = false;
471	u32 ctrl_val;
472
473	/* Walking r->domains, ensure it can't race with cpuhp */
474	lockdep_assert_cpus_held();
475
476	seq_printf(s, "%*s:", max_name_width, schema->name);
477	list_for_each_entry(dom, &r->domains, list) {
478		if (sep)
479			seq_puts(s, ";");
480
481		if (is_mba_sc(r))
482			ctrl_val = dom->mbps_val[closid];
483		else
484			ctrl_val = resctrl_arch_get_config(r, dom, closid,
485							   schema->conf_type);
486
487		seq_printf(s, r->format_str, dom->id, max_data_width,
488			   ctrl_val);
489		sep = true;
490	}
491	seq_puts(s, "\n");
492}
493
494int rdtgroup_schemata_show(struct kernfs_open_file *of,
495			   struct seq_file *s, void *v)
496{
497	struct resctrl_schema *schema;
498	struct rdtgroup *rdtgrp;
 
499	int ret = 0;
500	u32 closid;
501
502	rdtgrp = rdtgroup_kn_lock_live(of->kn);
503	if (rdtgrp) {
504		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
505			list_for_each_entry(schema, &resctrl_schema_all, list) {
506				seq_printf(s, "%s:uninitialized\n", schema->name);
507			}
508		} else if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
509			if (!rdtgrp->plr->d) {
510				rdt_last_cmd_clear();
511				rdt_last_cmd_puts("Cache domain offline\n");
512				ret = -ENODEV;
513			} else {
514				seq_printf(s, "%s:%d=%x\n",
515					   rdtgrp->plr->s->res->name,
516					   rdtgrp->plr->d->id,
517					   rdtgrp->plr->cbm);
518			}
519		} else {
520			closid = rdtgrp->closid;
521			list_for_each_entry(schema, &resctrl_schema_all, list) {
522				if (closid < schema->num_closid)
523					show_doms(s, schema, closid);
524			}
525		}
526	} else {
527		ret = -ENOENT;
528	}
529	rdtgroup_kn_unlock(of->kn);
530	return ret;
531}
532
533static int smp_mon_event_count(void *arg)
534{
535	mon_event_count(arg);
536
537	return 0;
538}
539
540void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
541		    struct rdt_domain *d, struct rdtgroup *rdtgrp,
542		    int evtid, int first)
543{
544	int cpu;
545
546	/* When picking a CPU from cpu_mask, ensure it can't race with cpuhp */
547	lockdep_assert_cpus_held();
548
549	/*
550	 * Setup the parameters to pass to mon_event_count() to read the data.
551	 */
552	rr->rgrp = rdtgrp;
553	rr->evtid = evtid;
554	rr->r = r;
555	rr->d = d;
556	rr->val = 0;
557	rr->first = first;
558	rr->arch_mon_ctx = resctrl_arch_mon_ctx_alloc(r, evtid);
559	if (IS_ERR(rr->arch_mon_ctx)) {
560		rr->err = -EINVAL;
561		return;
562	}
563
564	cpu = cpumask_any_housekeeping(&d->cpu_mask, RESCTRL_PICK_ANY_CPU);
565
566	/*
567	 * cpumask_any_housekeeping() prefers housekeeping CPUs, but
568	 * are all the CPUs nohz_full? If yes, pick a CPU to IPI.
569	 * MPAM's resctrl_arch_rmid_read() is unable to read the
570	 * counters on some platforms if its called in IRQ context.
571	 */
572	if (tick_nohz_full_cpu(cpu))
573		smp_call_function_any(&d->cpu_mask, mon_event_count, rr, 1);
574	else
575		smp_call_on_cpu(cpu, smp_mon_event_count, rr, false);
576
577	resctrl_arch_mon_ctx_free(r, evtid, rr->arch_mon_ctx);
578}
579
580int rdtgroup_mondata_show(struct seq_file *m, void *arg)
581{
582	struct kernfs_open_file *of = m->private;
583	u32 resid, evtid, domid;
584	struct rdtgroup *rdtgrp;
585	struct rdt_resource *r;
586	union mon_data_bits md;
587	struct rdt_domain *d;
588	struct rmid_read rr;
589	int ret = 0;
590
591	rdtgrp = rdtgroup_kn_lock_live(of->kn);
592	if (!rdtgrp) {
593		ret = -ENOENT;
594		goto out;
595	}
596
597	md.priv = of->kn->priv;
598	resid = md.u.rid;
599	domid = md.u.domid;
600	evtid = md.u.evtid;
601
602	r = &rdt_resources_all[resid].r_resctrl;
603	d = rdt_find_domain(r, domid, NULL);
604	if (IS_ERR_OR_NULL(d)) {
605		ret = -ENOENT;
606		goto out;
607	}
608
609	mon_event_read(&rr, r, d, rdtgrp, evtid, false);
610
611	if (rr.err == -EIO)
612		seq_puts(m, "Error\n");
613	else if (rr.err == -EINVAL)
614		seq_puts(m, "Unavailable\n");
615	else
616		seq_printf(m, "%llu\n", rr.val);
617
618out:
619	rdtgroup_kn_unlock(of->kn);
620	return ret;
621}