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(unsigned long var, struct rdt_resource *r)
39 unsigned long first_bit, zero_bit;
41 if (var == 0 || var > r->max_cbm)
44 first_bit = find_first_bit(&var, r->cbm_len);
45 zero_bit = find_next_zero_bit(&var, r->cbm_len, first_bit);
47 if (find_next_bit(&var, r->cbm_len, zero_bit) < r->cbm_len)
50 if ((zero_bit - first_bit) < r->min_cbm_bits)
56 * Read one cache bit mask (hex). Check that it is valid for the current
59 static int parse_cbm(char *buf, struct rdt_resource *r)
64 ret = kstrtoul(buf, 16, &data);
67 if (!cbm_validate(data, r))
69 r->tmp_cbms[r->num_tmp_cbms++] = data;
75 * For each domain in this resource we expect to find a series of:
77 * separated by ";". The "id" is in decimal, and must appear in the
80 static int parse_line(char *line, struct rdt_resource *r)
82 char *dom = NULL, *id;
86 list_for_each_entry(d, &r->domains, list) {
87 dom = strsep(&line, ";");
90 id = strsep(&dom, "=");
91 if (kstrtoul(id, 10, &dom_id) || dom_id != d->id)
93 if (parse_cbm(dom, r))
97 /* Any garbage at the end of the line? */
103 static int update_domains(struct rdt_resource *r, int closid)
105 struct msr_param msr_param;
106 cpumask_var_t cpu_mask;
107 struct rdt_domain *d;
110 if (!zalloc_cpumask_var(&cpu_mask, GFP_KERNEL))
113 msr_param.low = closid;
114 msr_param.high = msr_param.low + 1;
117 list_for_each_entry(d, &r->domains, list) {
118 cpumask_set_cpu(cpumask_any(&d->cpu_mask), cpu_mask);
119 d->cbm[msr_param.low] = r->tmp_cbms[idx++];
122 /* Update CBM on this cpu if it's in cpu_mask. */
123 if (cpumask_test_cpu(cpu, cpu_mask))
124 rdt_cbm_update(&msr_param);
125 /* Update CBM on other cpus. */
126 smp_call_function_many(cpu_mask, rdt_cbm_update, &msr_param, 1);
129 free_cpumask_var(cpu_mask);
134 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
135 char *buf, size_t nbytes, loff_t off)
137 struct rdtgroup *rdtgrp;
138 struct rdt_resource *r;
143 /* Valid input requires a trailing newline */
144 if (nbytes == 0 || buf[nbytes - 1] != '\n')
146 buf[nbytes - 1] = '\0';
148 rdtgrp = rdtgroup_kn_lock_live(of->kn);
150 rdtgroup_kn_unlock(of->kn);
154 closid = rdtgrp->closid;
156 /* get scratch space to save all the masks while we validate input */
157 for_each_enabled_rdt_resource(r) {
158 r->tmp_cbms = kcalloc(r->num_domains, sizeof(*l3_cbms),
167 while ((tok = strsep(&buf, "\n")) != NULL) {
168 resname = strsep(&tok, ":");
173 for_each_enabled_rdt_resource(r) {
174 if (!strcmp(resname, r->name) &&
175 closid < r->num_closid) {
176 ret = parse_line(tok, r);
188 /* Did the parser find all the masks we need? */
189 for_each_enabled_rdt_resource(r) {
190 if (r->num_tmp_cbms != r->num_domains) {
196 for_each_enabled_rdt_resource(r) {
197 ret = update_domains(r, closid);
203 rdtgroup_kn_unlock(of->kn);
204 for_each_enabled_rdt_resource(r) {
208 return ret ?: nbytes;
211 static void show_doms(struct seq_file *s, struct rdt_resource *r, int closid)
213 struct rdt_domain *dom;
216 seq_printf(s, "%s:", r->name);
217 list_for_each_entry(dom, &r->domains, list) {
220 seq_printf(s, "%d=%x", dom->id, dom->cbm[closid]);
226 int rdtgroup_schemata_show(struct kernfs_open_file *of,
227 struct seq_file *s, void *v)
229 struct rdtgroup *rdtgrp;
230 struct rdt_resource *r;
233 rdtgrp = rdtgroup_kn_lock_live(of->kn);
235 closid = rdtgrp->closid;
236 for_each_enabled_rdt_resource(r) {
237 if (closid < r->num_closid)
238 show_doms(s, r, closid);
243 rdtgroup_kn_unlock(of->kn);