summaryrefslogtreecommitdiffstats
path: root/arch/s390/kernel/cache.c
blob: 5e20bab4df22882628400a7641172c2eca3254f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Extract CPU cache information and expose them via sysfs.
 *
 *    Copyright IBM Corp. 2012
 *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
 */

#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/cpu.h>
#include <asm/facility.h>

struct cache {
	unsigned long size;
	unsigned int line_size;
	unsigned int associativity;
	unsigned int nr_sets;
	int level;
	int type;
	struct list_head list;
};

struct cache_dir {
	struct kobject *kobj;
	struct cache_index_dir *index;
};

struct cache_index_dir {
	struct kobject kobj;
	int cpu;
	struct cache *cache;
	struct cache_index_dir *next;
};

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

enum {
	CACHE_TYPE_SEPARATE,
	CACHE_TYPE_DATA,
	CACHE_TYPE_INSTRUCTION,
	CACHE_TYPE_UNIFIED,
};

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

enum {
	CACHE_TI_UNIFIED = 0,
	CACHE_TI_INSTRUCTION = 0,
	CACHE_TI_DATA,
};

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[] = {
	"Data",
	"Instruction",
	"Unified",
};

static struct cache_dir *cache_dir_cpu[NR_CPUS];
static LIST_HEAD(cache_list);

static inline unsigned long ecag(int ai, int li, int ti)
{
	unsigned long cmd, val;

	cmd = ai << 4 | li << 1 | ti;
	asm volatile(".insn	rsy,0xeb000000004c,%0,0,0(%1)" /* ecag */
		     : "=d" (val) : "a" (cmd));
	return val;
}

static int __init cache_add(int level, int type)
{
	struct cache *cache;
	int ti;

	cache = kzalloc(sizeof(*cache), GFP_KERNEL);
	if (!cache)
		return -ENOMEM;
	ti = type == CACHE_TYPE_DATA ? CACHE_TI_DATA : CACHE_TI_UNIFIED;
	cache->size = ecag(EXTRACT_SIZE, level, ti);
	cache->line_size = ecag(EXTRACT_LINE_SIZE, level, ti);
	cache->associativity = ecag(EXTRACT_ASSOCIATIVITY, level, ti);
	cache->nr_sets = cache->size / cache->associativity;
	cache->nr_sets /= cache->line_size;
	cache->level = level + 1;
	cache->type = type;
	list_add_tail(&cache->list, &cache_list);
	return 0;
}

static void __init cache_build_info(void)
{
	struct cache *cache, *next;
	union cache_topology ct;
	int level, rc;

	ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0);
	for (level = 0; level < CACHE_MAX_LEVEL; level++) {
		switch (ct.ci[level].scope) {
		case CACHE_SCOPE_NOTEXISTS:
		case CACHE_SCOPE_RESERVED:
		case CACHE_SCOPE_SHARED:
			return;
		case CACHE_SCOPE_PRIVATE:
			break;
		}
		if (ct.ci[level].type == CACHE_TYPE_SEPARATE) {
			rc  = cache_add(level, CACHE_TYPE_DATA);
			rc |= cache_add(level, CACHE_TYPE_INSTRUCTION);
		} else {
			rc = cache_add(level, ct.ci[level].type);
		}
		if (rc)
			goto error;
	}
	return;
error:
	list_for_each_entry_safe(cache, next, &cache_list, list) {
		list_del(&cache->list);
		kfree(cache);
	}
}

static struct cache_dir *__cpuinit cache_create_cache_dir(int cpu)
{
	struct cache_dir *cache_dir;
	struct kobject *kobj = NULL;
	struct device *dev;

	dev = get_cpu_device(cpu);
	if (!dev)
		goto out;
	kobj = kobject_create_and_add("cache", &dev->kobj);
	if (!kobj)
		goto out;
	cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL);
	if (!cache_dir)
		goto out;
	cache_dir->kobj = kobj;
	cache_dir_cpu[cpu] = cache_dir;
	return cache_dir;
out:
	kobject_put(kobj);
	return NULL;
}

static struct cache_index_dir *kobj_to_cache_index_dir(struct kobject *kobj)
{
	return container_of(kobj, struct cache_index_dir, kobj);
}

static void cache_index_release(struct kobject *kobj)
{
	struct cache_index_dir *index;

	index = kobj_to_cache_index_dir(kobj);
	kfree(index);
}

static ssize_t cache_index_show(struct kobject *kobj,
				struct attribute *attr, char *buf)
{
	struct kobj_attribute *kobj_attr;

	kobj_attr = container_of(attr, struct kobj_attribute, attr);
	return kobj_attr->show(kobj, kobj_attr, buf);
}

#define DEFINE_CACHE_ATTR(_name, _format, _value)			\
static ssize_t cache_##_name##_show(struct kobject *kobj,		\
				    struct kobj_attribute *attr,	\
				    char *buf)				\
{									\
	struct cache_index_dir *index;					\
									\
	index = kobj_to_cache_index_dir(kobj);				\
	return sprintf(buf, _format, _value);				\
}									\
static struct kobj_attribute cache_##_name##_attr =			\
	__ATTR(_name, 0444, cache_##_name##_show, NULL);

DEFINE_CACHE_ATTR(size, "%luK\n", index->cache->size >> 10);
DEFINE_CACHE_ATTR(coherency_line_size, "%u\n", index->cache->line_size);
DEFINE_CACHE_ATTR(number_of_sets, "%u\n", index->cache->nr_sets);
DEFINE_CACHE_ATTR(ways_of_associativity, "%u\n", index->cache->associativity);
DEFINE_CACHE_ATTR(type, "%s\n", cache_type_string[index->cache->type - 1]);
DEFINE_CACHE_ATTR(level, "%d\n", index->cache->level);

static ssize_t shared_cpu_map_func(struct kobject *kobj, int type, char *buf)
{
	struct cache_index_dir *index;
	int len;

	index = kobj_to_cache_index_dir(kobj);
	len = type ?
		cpulist_scnprintf(buf, PAGE_SIZE - 2, cpumask_of(index->cpu)) :
		cpumask_scnprintf(buf, PAGE_SIZE - 2, cpumask_of(index->cpu));
	len += sprintf(&buf[len], "\n");
	return len;
}

static ssize_t shared_cpu_map_show(struct kobject *kobj,
				   struct kobj_attribute *attr, char *buf)
{
	return shared_cpu_map_func(kobj, 0, buf);
}
static struct kobj_attribute cache_shared_cpu_map_attr =
	__ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL);

static ssize_t shared_cpu_list_show(struct kobject *kobj,
				    struct kobj_attribute *attr, char *buf)
{
	return shared_cpu_map_func(kobj, 1, buf);
}
static struct kobj_attribute cache_shared_cpu_list_attr =
	__ATTR(shared_cpu_list, 0444, shared_cpu_list_show, NULL);

static struct attribute *cache_index_default_attrs[] = {
	&cache_type_attr.attr,
	&cache_size_attr.attr,
	&cache_number_of_sets_attr.attr,
	&cache_ways_of_associativity_attr.attr,
	&cache_level_attr.attr,
	&cache_coherency_line_size_attr.attr,
	&cache_shared_cpu_map_attr.attr,
	&cache_shared_cpu_list_attr.attr,
	NULL,
};

static const struct sysfs_ops cache_index_ops = {
	.show = cache_index_show,
};

static struct kobj_type cache_index_type = {
	.sysfs_ops = &cache_index_ops,
	.release = cache_index_release,
	.default_attrs = cache_index_default_attrs,
};

static int __cpuinit cache_create_index_dir(struct cache_dir *cache_dir,
					    struct cache *cache, int index,
					    int cpu)
{
	struct cache_index_dir *index_dir;
	int rc;

	index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL);
	if (!index_dir)
		return -ENOMEM;
	index_dir->cache = cache;
	index_dir->cpu = cpu;
	rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type,
				  cache_dir->kobj, "index%d", index);
	if (rc)
		goto out;
	index_dir->next = cache_dir->index;
	cache_dir->index = index_dir;
	return 0;
out:
	kfree(index_dir);
	return rc;
}

static int __cpuinit cache_add_cpu(int cpu)
{
	struct cache_dir *cache_dir;
	struct cache *cache;
	int rc, index = 0;

	if (list_empty(&cache_list))
		return 0;
	cache_dir = cache_create_cache_dir(cpu);
	if (!cache_dir)
		return -ENOMEM;
	list_for_each_entry(cache, &cache_list, list) {
		rc = cache_create_index_dir(cache_dir, cache, index, cpu);
		if (rc)
			return rc;
		index++;
	}
	return 0;
}

static void __cpuinit cache_remove_cpu(int cpu)
{
	struct cache_index_dir *index, *next;
	struct cache_dir *cache_dir;

	cache_dir = cache_dir_cpu[cpu];
	if (!cache_dir)
		return;
	index = cache_dir->index;
	while (index) {
		next = index->next;
		kobject_put(&index->kobj);
		index = next;
	}
	kobject_put(cache_dir->kobj);
	kfree(cache_dir);
	cache_dir_cpu[cpu] = NULL;
}

static int __cpuinit cache_hotplug(struct notifier_block *nfb,
				   unsigned long action, void *hcpu)
{
	int cpu = (long)hcpu;
	int rc = 0;

	switch (action & ~CPU_TASKS_FROZEN) {
	case CPU_ONLINE:
		rc = cache_add_cpu(cpu);
		if (rc)
			cache_remove_cpu(cpu);
		break;
	case CPU_DEAD:
		cache_remove_cpu(cpu);
		break;
	}
	return rc ? NOTIFY_BAD : NOTIFY_OK;
}

static int __init cache_init(void)
{
	int cpu;

	if (!test_facility(34))
		return 0;
	cache_build_info();
	for_each_online_cpu(cpu)
		cache_add_cpu(cpu);
	hotcpu_notifier(cache_hotplug, 0);
	return 0;
}
device_initcall(cache_init);