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 a cache bit mask is valid. The SDM says:
33 * Please note that all (and only) contiguous '1' combinations
34 * are allowed (e.g. FFFFH, 0FF0H, 003CH, etc.).
35 * Additionally Haswell requires at least two bits set.
37 static bool cbm_validate(char *buf, unsigned long *data, struct rdt_resource *r)
39 unsigned long first_bit, zero_bit, val;
40 unsigned int cbm_len = r->cache.cbm_len;
43 ret = kstrtoul(buf, 16, &val);
47 if (val == 0 || val > r->default_ctrl)
50 first_bit = find_first_bit(&val, cbm_len);
51 zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
53 if (find_next_bit(&val, cbm_len, zero_bit) < cbm_len)
56 if ((zero_bit - first_bit) < r->cache.min_cbm_bits)
64 * Read one cache bit mask (hex). Check that it is valid for the current
67 int parse_cbm(char *buf, struct rdt_resource *r, struct rdt_domain *d)
74 if(!cbm_validate(buf, &data, r))
77 d->have_new_ctrl = true;
83 * For each domain in this resource we expect to find a series of:
85 * separated by ";". The "id" is in decimal, and must match one of
86 * the "id"s for this resource.
88 static int parse_line(char *line, struct rdt_resource *r)
90 char *dom = NULL, *id;
95 if (!line || line[0] == '\0')
97 dom = strsep(&line, ";");
98 id = strsep(&dom, "=");
99 if (!dom || kstrtoul(id, 10, &dom_id))
101 list_for_each_entry(d, &r->domains, list) {
102 if (d->id == dom_id) {
103 if (r->parse_ctrlval(dom, r, d))
111 static int update_domains(struct rdt_resource *r, int closid)
113 struct msr_param msr_param;
114 cpumask_var_t cpu_mask;
115 struct rdt_domain *d;
118 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
121 msr_param.low = closid;
122 msr_param.high = msr_param.low + 1;
125 list_for_each_entry(d, &r->domains, list) {
126 if (d->have_new_ctrl && d->new_ctrl != d->ctrl_val[closid]) {
127 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
128 d->ctrl_val[closid] = d->new_ctrl;
131 if (cpumask_empty(cpu_mask))
134 /* Update CBM on this cpu if it's in cpu_mask. */
135 if (cpumask_test_cpu(cpu, cpu_mask))
136 rdt_ctrl_update(&msr_param);
137 /* Update CBM on other cpus. */
138 smp_call_function_many(cpu_mask, rdt_ctrl_update, &msr_param, 1);
142 free_cpumask_var(cpu_mask);
147 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
148 char *buf, size_t nbytes, loff_t off)
150 struct rdtgroup *rdtgrp;
151 struct rdt_domain *dom;
152 struct rdt_resource *r;
156 /* Valid input requires a trailing newline */
157 if (nbytes == 0 || buf[nbytes - 1] != '\n')
159 buf[nbytes - 1] = '\0';
161 rdtgrp = rdtgroup_kn_lock_live(of->kn);
163 rdtgroup_kn_unlock(of->kn);
167 closid = rdtgrp->closid;
169 for_each_enabled_rdt_resource(r)
170 list_for_each_entry(dom, &r->domains, list)
171 dom->have_new_ctrl = false;
173 while ((tok = strsep(&buf, "\n")) != NULL) {
174 resname = strsep(&tok, ":");
179 for_each_enabled_rdt_resource(r) {
180 if (!strcmp(resname, r->name) &&
181 closid < r->num_closid) {
182 ret = parse_line(tok, r);
194 for_each_enabled_rdt_resource(r) {
195 ret = update_domains(r, closid);
201 rdtgroup_kn_unlock(of->kn);
202 return ret ?: nbytes;
205 static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
207 struct rdt_domain *dom;
210 seq_printf(s, "%*s:", max_name_width, r->name);
211 list_for_each_entry(dom, &r->domains, list) {
214 seq_printf(s, r->format_str, dom->id, max_data_width,
215 dom->ctrl_val[closid]);
221 int rdtgroup_schemata_show(struct kernfs_open_file *of,
222 struct seq_file *s, void *v)
224 struct rdtgroup *rdtgrp;
225 struct rdt_resource *r;
228 rdtgrp = rdtgroup_kn_lock_live(of->kn);
230 closid = rdtgrp->closid;
231 for_each_enabled_rdt_resource(r) {
232 if (closid < r->num_closid)
233 show_doms(s, r, closid);
238 rdtgroup_kn_unlock(of->kn);