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