cache.c 4.4 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Extract CPU cache information and expose them via sysfs.
 *
 *    Copyright IBM Corp. 2012
 *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
 */

8
#include <linux/seq_file.h>
9
#include <linux/cpu.h>
10
#include <linux/cacheinfo.h>
11 12 13 14 15 16 17 18 19 20
#include <asm/facility.h>

enum {
	CACHE_SCOPE_NOTEXISTS,
	CACHE_SCOPE_PRIVATE,
	CACHE_SCOPE_SHARED,
	CACHE_SCOPE_RESERVED,
};

enum {
21 22 23 24
	CTYPE_SEPARATE,
	CTYPE_DATA,
	CTYPE_INSTRUCTION,
	CTYPE_UNIFIED,
25 26 27 28 29 30 31 32 33 34 35
};

enum {
	EXTRACT_TOPOLOGY,
	EXTRACT_LINE_SIZE,
	EXTRACT_SIZE,
	EXTRACT_ASSOCIATIVITY,
};

enum {
	CACHE_TI_UNIFIED = 0,
36 37
	CACHE_TI_DATA = 0,
	CACHE_TI_INSTRUCTION,
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
};

struct cache_info {
	unsigned char	    : 4;
	unsigned char scope : 2;
	unsigned char type  : 2;
};

#define CACHE_MAX_LEVEL 8
union cache_topology {
	struct cache_info ci[CACHE_MAX_LEVEL];
	unsigned long long raw;
};

static const char * const cache_type_string[] = {
53
	"",
54
	"Instruction",
55 56
	"Data",
	"",
57 58 59
	"Unified",
};

60 61 62 63 64 65
static const enum cache_type cache_type_map[] = {
	[CTYPE_SEPARATE] = CACHE_TYPE_SEPARATE,
	[CTYPE_DATA] = CACHE_TYPE_DATA,
	[CTYPE_INSTRUCTION] = CACHE_TYPE_INST,
	[CTYPE_UNIFIED] = CACHE_TYPE_UNIFIED,
};
66

67 68
void show_cacheinfo(struct seq_file *m)
{
69
	struct cpu_cacheinfo *this_cpu_ci;
70
	struct cacheinfo *cache;
71
	int idx;
72

73 74
	if (!test_facility(34))
		return;
75
	this_cpu_ci = get_cpu_cacheinfo(cpumask_any(cpu_online_mask));
76 77 78
	for (idx = 0; idx < this_cpu_ci->num_leaves; idx++) {
		cache = this_cpu_ci->info_list + idx;
		seq_printf(m, "cache%-11d: ", idx);
79 80
		seq_printf(m, "level=%d ", cache->level);
		seq_printf(m, "type=%s ", cache_type_string[cache->type]);
81 82 83 84 85
		seq_printf(m, "scope=%s ",
			   cache->disable_sysfs ? "Shared" : "Private");
		seq_printf(m, "size=%dK ", cache->size >> 10);
		seq_printf(m, "line_size=%u ", cache->coherency_line_size);
		seq_printf(m, "associativity=%d", cache->ways_of_associativity);
86 87 88 89
		seq_puts(m, "\n");
	}
}

90 91 92 93 94 95 96 97 98 99
static inline enum cache_type get_cache_type(struct cache_info *ci, int level)
{
	if (level >= CACHE_MAX_LEVEL)
		return CACHE_TYPE_NOCACHE;
	ci += level;
	if (ci->scope != CACHE_SCOPE_SHARED && ci->scope != CACHE_SCOPE_PRIVATE)
		return CACHE_TYPE_NOCACHE;
	return cache_type_map[ci->type];
}

100 101
static inline unsigned long ecag(int ai, int li, int ti)
{
102
	return __ecag(ECAG_CACHE_ATTRIBUTE, ai << 4 | li << 1 | ti);
103 104
}

105
static void ci_leaf_init(struct cacheinfo *this_leaf, int private,
106
			 enum cache_type type, unsigned int level, int cpu)
107
{
108
	int ti, num_sets;
109

110
	if (type == CACHE_TYPE_INST)
111 112 113
		ti = CACHE_TI_INSTRUCTION;
	else
		ti = CACHE_TI_UNIFIED;
114 115 116
	this_leaf->level = level + 1;
	this_leaf->type = type;
	this_leaf->coherency_line_size = ecag(EXTRACT_LINE_SIZE, level, ti);
117
	this_leaf->ways_of_associativity = ecag(EXTRACT_ASSOCIATIVITY, level, ti);
118 119 120 121 122 123 124
	this_leaf->size = ecag(EXTRACT_SIZE, level, ti);
	num_sets = this_leaf->size / this_leaf->coherency_line_size;
	num_sets /= this_leaf->ways_of_associativity;
	this_leaf->number_of_sets = num_sets;
	cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
	if (!private)
		this_leaf->disable_sysfs = true;
125 126
}

127
int init_cache_level(unsigned int cpu)
128
{
129 130 131 132
	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
	unsigned int level = 0, leaves = 0;
	union cache_topology ct;
	enum cache_type ctype;
133

134 135
	if (!test_facility(34))
		return -EOPNOTSUPP;
136 137 138 139 140 141
	if (!this_cpu_ci)
		return -EINVAL;
	ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0);
	do {
		ctype = get_cache_type(&ct.ci[0], level);
		if (ctype == CACHE_TYPE_NOCACHE)
142
			break;
143 144 145 146 147 148
		/* Separate instruction and data caches */
		leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1;
	} while (++level < CACHE_MAX_LEVEL);
	this_cpu_ci->num_levels = level;
	this_cpu_ci->num_leaves = leaves;
	return 0;
149 150
}

151
int populate_cache_leaves(unsigned int cpu)
152
{
153 154
	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
	struct cacheinfo *this_leaf = this_cpu_ci->info_list;
155 156 157
	unsigned int level, idx, pvt;
	union cache_topology ct;
	enum cache_type ctype;
158

159 160
	if (!test_facility(34))
		return -EOPNOTSUPP;
161 162 163 164 165 166 167 168
	ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0);
	for (idx = 0, level = 0; level < this_cpu_ci->num_levels &&
	     idx < this_cpu_ci->num_leaves; idx++, level++) {
		if (!this_leaf)
			return -EINVAL;
		pvt = (ct.ci[level].scope == CACHE_SCOPE_PRIVATE) ? 1 : 0;
		ctype = get_cache_type(&ct.ci[0], level);
		if (ctype == CACHE_TYPE_SEPARATE) {
169 170
			ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_DATA, level, cpu);
			ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_INST, level, cpu);
171
		} else {
172
			ci_leaf_init(this_leaf++, pvt, ctype, level, cpu);
173
		}
174 175 176
	}
	return 0;
}