Commit 2264d9c7 authored by Tony Luck's avatar Tony Luck Committed by Thomas Gleixner

x86/intel_rdt: Build structures for each resource based on cache topology

We use the cpu hotplug notifier to catch each cpu in turn and look at
its cache topology w.r.t each of the resource groups. As we discover
new resources, we initialize the bitmask array for each to the default
(full access) value.
Signed-off-by: default avatarTony Luck <tony.luck@intel.com>
Signed-off-by: default avatarFenghua Yu <fenghua.yu@intel.com>
Cc: "Ravi V Shankar" <ravi.v.shankar@intel.com>
Cc: "Shaohua Li" <shli@fb.com>
Cc: "Sai Prakhya" <sai.praneeth.prakhya@intel.com>
Cc: "Peter Zijlstra" <peterz@infradead.org>
Cc: "Stephane Eranian" <eranian@google.com>
Cc: "Dave Hansen" <dave.hansen@intel.com>
Cc: "David Carrillo-Cisneros" <davidcc@google.com>
Cc: "Nilay Vaish" <nilayvaish@gmail.com>
Cc: "Vikas Shivappa" <vikas.shivappa@linux.intel.com>
Cc: "Ingo Molnar" <mingo@elte.hu>
Cc: "Borislav Petkov" <bp@suse.de>
Cc: "H. Peter Anvin" <h.peter.anvin@intel.com>
Link: http://lkml.kernel.org/r/1477692289-37412-3-git-send-email-fenghua.yu@intel.comSigned-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
parent f20e5789
...@@ -39,6 +39,34 @@ struct rdt_resource { ...@@ -39,6 +39,34 @@ struct rdt_resource {
int cbm_idx_offset; int cbm_idx_offset;
}; };
/**
* struct rdt_domain - group of cpus sharing an RDT resource
* @list: all instances of this resource
* @id: unique id for this instance
* @cpu_mask: which cpus share this resource
* @cbm: array of cache bit masks (indexed by CLOSID)
*/
struct rdt_domain {
struct list_head list;
int id;
struct cpumask cpu_mask;
u32 *cbm;
};
/**
* struct msr_param - set a range of MSRs from a domain
* @res: The resource to use
* @low: Beginning index from base MSR
* @high: End index
*/
struct msr_param {
struct rdt_resource *res;
int low;
int high;
};
extern struct mutex rdtgroup_mutex;
extern struct rdt_resource rdt_resources_all[]; extern struct rdt_resource rdt_resources_all[];
enum { enum {
...@@ -56,6 +84,11 @@ enum { ...@@ -56,6 +84,11 @@ enum {
r++) \ r++) \
if (r->capable) if (r->capable)
#define for_each_enabled_rdt_resource(r) \
for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\
r++) \
if (r->enabled)
/* CPUID.(EAX=10H, ECX=ResID=1).EAX */ /* CPUID.(EAX=10H, ECX=ResID=1).EAX */
union cpuid_0x10_1_eax { union cpuid_0x10_1_eax {
struct { struct {
...@@ -71,4 +104,6 @@ union cpuid_0x10_1_edx { ...@@ -71,4 +104,6 @@ union cpuid_0x10_1_edx {
} split; } split;
unsigned int full; unsigned int full;
}; };
void rdt_cbm_update(void *arg);
#endif /* _ASM_X86_INTEL_RDT_H */ #endif /* _ASM_X86_INTEL_RDT_H */
...@@ -26,11 +26,16 @@ ...@@ -26,11 +26,16 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/cacheinfo.h>
#include <linux/cpuhotplug.h>
#include <asm/intel_rdt_common.h> #include <asm/intel_rdt_common.h>
#include <asm/intel-family.h> #include <asm/intel-family.h>
#include <asm/intel_rdt.h> #include <asm/intel_rdt.h>
/* Mutex to protect rdtgroup access. */
DEFINE_MUTEX(rdtgroup_mutex);
#define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].domains) #define domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].domains)
struct rdt_resource rdt_resources_all[] = { struct rdt_resource rdt_resources_all[] = {
...@@ -72,6 +77,11 @@ struct rdt_resource rdt_resources_all[] = { ...@@ -72,6 +77,11 @@ struct rdt_resource rdt_resources_all[] = {
}, },
}; };
static int cbm_idx(struct rdt_resource *r, int closid)
{
return closid * r->cbm_idx_multi + r->cbm_idx_offset;
}
/* /*
* cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
* as they do not have CPUID enumeration support for Cache allocation. * as they do not have CPUID enumeration support for Cache allocation.
...@@ -176,13 +186,192 @@ static inline bool get_rdt_resources(void) ...@@ -176,13 +186,192 @@ static inline bool get_rdt_resources(void)
return ret; return ret;
} }
static int get_cache_id(int cpu, int level)
{
struct cpu_cacheinfo *ci = get_cpu_cacheinfo(cpu);
int i;
for (i = 0; i < ci->num_leaves; i++) {
if (ci->info_list[i].level == level)
return ci->info_list[i].id;
}
return -1;
}
void rdt_cbm_update(void *arg)
{
struct msr_param *m = (struct msr_param *)arg;
struct rdt_resource *r = m->res;
int i, cpu = smp_processor_id();
struct rdt_domain *d;
list_for_each_entry(d, &r->domains, list) {
/* Find the domain that contains this CPU */
if (cpumask_test_cpu(cpu, &d->cpu_mask))
goto found;
}
pr_info_once("cpu %d not found in any domain for resource %s\n",
cpu, r->name);
return;
found:
for (i = m->low; i < m->high; i++) {
int idx = cbm_idx(r, i);
wrmsrl(r->msr_base + idx, d->cbm[i]);
}
}
/*
* rdt_find_domain - Find a domain in a resource that matches input resource id
*
* Search resource r's domain list to find the resource id. If the resource
* id is found in a domain, return the domain. Otherwise, if requested by
* caller, return the first domain whose id is bigger than the input id.
* The domain list is sorted by id in ascending order.
*/
static struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
struct list_head **pos)
{
struct rdt_domain *d;
struct list_head *l;
if (id < 0)
return ERR_PTR(id);
list_for_each(l, &r->domains) {
d = list_entry(l, struct rdt_domain, list);
/* When id is found, return its domain. */
if (id == d->id)
return d;
/* Stop searching when finding id's position in sorted list. */
if (id < d->id)
break;
}
if (pos)
*pos = l;
return NULL;
}
/*
* domain_add_cpu - Add a cpu to a resource's domain list.
*
* If an existing domain in the resource r's domain list matches the cpu's
* resource id, add the cpu in the domain.
*
* Otherwise, a new domain is allocated and inserted into the right position
* in the domain list sorted by id in ascending order.
*
* The order in the domain list is visible to users when we print entries
* in the schemata file and schemata input is validated to have the same order
* as this list.
*/
static void domain_add_cpu(int cpu, struct rdt_resource *r)
{
int i, id = get_cache_id(cpu, r->cache_level);
struct list_head *add_pos = NULL;
struct rdt_domain *d;
d = rdt_find_domain(r, id, &add_pos);
if (IS_ERR(d)) {
pr_warn("Could't find cache id for cpu %d\n", cpu);
return;
}
if (d) {
cpumask_set_cpu(cpu, &d->cpu_mask);
return;
}
d = kzalloc_node(sizeof(*d), GFP_KERNEL, cpu_to_node(cpu));
if (!d)
return;
d->id = id;
d->cbm = kmalloc_array(r->num_closid, sizeof(*d->cbm), GFP_KERNEL);
if (!d->cbm) {
kfree(d);
return;
}
for (i = 0; i < r->num_closid; i++) {
int idx = cbm_idx(r, i);
d->cbm[i] = r->max_cbm;
wrmsrl(r->msr_base + idx, d->cbm[i]);
}
cpumask_set_cpu(cpu, &d->cpu_mask);
list_add_tail(&d->list, add_pos);
r->num_domains++;
}
static void domain_remove_cpu(int cpu, struct rdt_resource *r)
{
int id = get_cache_id(cpu, r->cache_level);
struct rdt_domain *d;
d = rdt_find_domain(r, id, NULL);
if (IS_ERR_OR_NULL(d)) {
pr_warn("Could't find cache id for cpu %d\n", cpu);
return;
}
cpumask_clear_cpu(cpu, &d->cpu_mask);
if (cpumask_empty(&d->cpu_mask)) {
r->num_domains--;
kfree(d->cbm);
list_del(&d->list);
kfree(d);
}
}
static int intel_rdt_online_cpu(unsigned int cpu)
{
struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
struct rdt_resource *r;
mutex_lock(&rdtgroup_mutex);
for_each_capable_rdt_resource(r)
domain_add_cpu(cpu, r);
state->closid = 0;
wrmsr(MSR_IA32_PQR_ASSOC, state->rmid, 0);
mutex_unlock(&rdtgroup_mutex);
return 0;
}
static int intel_rdt_offline_cpu(unsigned int cpu)
{
struct rdt_resource *r;
mutex_lock(&rdtgroup_mutex);
for_each_capable_rdt_resource(r)
domain_remove_cpu(cpu, r);
mutex_unlock(&rdtgroup_mutex);
return 0;
}
static int __init intel_rdt_late_init(void) static int __init intel_rdt_late_init(void)
{ {
struct rdt_resource *r; struct rdt_resource *r;
int state;
if (!get_rdt_resources()) if (!get_rdt_resources())
return -ENODEV; return -ENODEV;
state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
"x86/rdt/cat:online:",
intel_rdt_online_cpu, intel_rdt_offline_cpu);
if (state < 0)
return state;
for_each_capable_rdt_resource(r) for_each_capable_rdt_resource(r)
pr_info("Intel RDT %s allocation detected\n", r->name); pr_info("Intel RDT %s allocation detected\n", r->name);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment