2 * Resource Director Technology(RDT)
3 * - Cache Allocation code.
5 * Copyright (C) 2016 Intel Corporation
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
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 <asm/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)
43 * Only linear delay values is supported for current Intel SKUs.
45 if (!r->membw.delay_linear)
48 ret = kstrtoul(buf, 10, &bw);
52 if (bw < r->membw.min_bw || bw > r->default_ctrl)
55 *data = roundup(bw, (unsigned long)r->membw.bw_gran);
59 int parse_bw(char *buf, struct rdt_resource *r, struct rdt_domain *d)
66 if (!bw_validate(buf, &data, r))
69 d->have_new_ctrl = true;
75 * Check whether a cache bit mask is valid. The SDM says:
76 * Please note that all (and only) contiguous '1' combinations
77 * are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
78 * Additionally Haswell requires at least two bits set.
80 static bool cbm_validate(char *buf, unsigned long *data, struct rdt_resource *r)
82 unsigned long first_bit, zero_bit, val;
83 unsigned int cbm_len = r->cache.cbm_len;
86 ret = kstrtoul(buf, 16, &val);
90 if (val == 0 || val > r->default_ctrl)
93 first_bit = find_first_bit(&val, cbm_len);
94 zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
96 if (find_next_bit(&val, cbm_len, zero_bit) < cbm_len)
99 if ((zero_bit - first_bit) < r->cache.min_cbm_bits)
107 * Read one cache bit mask (hex). Check that it is valid for the current
110 int parse_cbm(char *buf, struct rdt_resource *r, struct rdt_domain *d)
114 if (d->have_new_ctrl)
117 if(!cbm_validate(buf, &data, r))
120 d->have_new_ctrl = true;
126 * For each domain in this resource we expect to find a series of:
128 * separated by ";". The "id" is in decimal, and must match one of
129 * the "id"s for this resource.
131 static int parse_line(char *line, struct rdt_resource *r)
133 char *dom = NULL, *id;
134 struct rdt_domain *d;
135 unsigned long dom_id;
138 if (!line || line[0] == '\0')
140 dom = strsep(&line, ";");
141 id = strsep(&dom, "=");
142 if (!dom || kstrtoul(id, 10, &dom_id))
145 list_for_each_entry(d, &r->domains, list) {
146 if (d->id == dom_id) {
147 if (r->parse_ctrlval(dom, r, d))
155 static int update_domains(struct rdt_resource *r, int closid)
157 struct msr_param msr_param;
158 cpumask_var_t cpu_mask;
159 struct rdt_domain *d;
162 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
165 msr_param.low = closid;
166 msr_param.high = msr_param.low + 1;
169 list_for_each_entry(d, &r->domains, list) {
170 if (d->have_new_ctrl && d->new_ctrl != d->ctrl_val[closid]) {
171 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
172 d->ctrl_val[closid] = d->new_ctrl;
175 if (cpumask_empty(cpu_mask))
178 /* Update CBM on this cpu if it's in cpu_mask. */
179 if (cpumask_test_cpu(cpu, cpu_mask))
180 rdt_ctrl_update(&msr_param);
181 /* Update CBM on other cpus. */
182 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
186 free_cpumask_var(cpu_mask);
191 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
192 char *buf, size_t nbytes, loff_t off)
194 struct rdtgroup *rdtgrp;
195 struct rdt_domain *dom;
196 struct rdt_resource *r;
200 /* Valid input requires a trailing newline */
201 if (nbytes == 0 || buf[nbytes - 1] != '\n')
203 buf[nbytes - 1] = '\0';
205 rdtgrp = rdtgroup_kn_lock_live(of->kn);
207 rdtgroup_kn_unlock(of->kn);
211 closid = rdtgrp->closid;
213 for_each_enabled_rdt_resource(r)
214 list_for_each_entry(dom, &r->domains, list)
215 dom->have_new_ctrl = false;
217 while ((tok = strsep(&buf, "\n")) != NULL) {
218 resname = strim(strsep(&tok, ":"));
223 for_each_enabled_rdt_resource(r) {
224 if (!strcmp(resname, r->name) &&
225 closid < r->num_closid) {
226 ret = parse_line(tok, r);
238 for_each_enabled_rdt_resource(r) {
239 ret = update_domains(r, closid);
245 rdtgroup_kn_unlock(of->kn);
246 return ret ?: nbytes;
249 static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
251 struct rdt_domain *dom;
254 seq_printf(s, "%*s:", max_name_width, r->name);
255 list_for_each_entry(dom, &r->domains, list) {
258 seq_printf(s, r->format_str, dom->id, max_data_width,
259 dom->ctrl_val[closid]);
265 int rdtgroup_schemata_show(struct kernfs_open_file *of,
266 struct seq_file *s, void *v)
268 struct rdtgroup *rdtgrp;
269 struct rdt_resource *r;
272 rdtgrp = rdtgroup_kn_lock_live(of->kn);
274 closid = rdtgrp->closid;
275 for_each_enabled_rdt_resource(r) {
276 if (closid < r->num_closid)
277 show_doms(s, r, closid);
282 rdtgroup_kn_unlock(of->kn);