]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/s390/kernel/cache.c
Merge tag 'armsoc-soc' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[mirror_ubuntu-bionic-kernel.git] / arch / s390 / kernel / cache.c
CommitLineData
881730ad
HC
1/*
2 * Extract CPU cache information and expose them via sysfs.
3 *
4 * Copyright IBM Corp. 2012
5 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
6 */
7
6668022c 8#include <linux/seq_file.h>
881730ad 9#include <linux/cpu.h>
d97d929f 10#include <linux/cacheinfo.h>
881730ad
HC
11#include <asm/facility.h>
12
881730ad
HC
13enum {
14 CACHE_SCOPE_NOTEXISTS,
15 CACHE_SCOPE_PRIVATE,
16 CACHE_SCOPE_SHARED,
17 CACHE_SCOPE_RESERVED,
18};
19
20enum {
d97d929f
SH
21 CTYPE_SEPARATE,
22 CTYPE_DATA,
23 CTYPE_INSTRUCTION,
24 CTYPE_UNIFIED,
881730ad
HC
25};
26
27enum {
28 EXTRACT_TOPOLOGY,
29 EXTRACT_LINE_SIZE,
30 EXTRACT_SIZE,
31 EXTRACT_ASSOCIATIVITY,
32};
33
34enum {
35 CACHE_TI_UNIFIED = 0,
d18f99c2
HC
36 CACHE_TI_DATA = 0,
37 CACHE_TI_INSTRUCTION,
881730ad
HC
38};
39
40struct cache_info {
41 unsigned char : 4;
42 unsigned char scope : 2;
43 unsigned char type : 2;
44};
45
46#define CACHE_MAX_LEVEL 8
881730ad
HC
47union cache_topology {
48 struct cache_info ci[CACHE_MAX_LEVEL];
49 unsigned long long raw;
50};
51
52static const char * const cache_type_string[] = {
d97d929f 53 "",
881730ad 54 "Instruction",
d97d929f
SH
55 "Data",
56 "",
881730ad
HC
57 "Unified",
58};
59
d97d929f
SH
60static const enum cache_type cache_type_map[] = {
61 [CTYPE_SEPARATE] = CACHE_TYPE_SEPARATE,
62 [CTYPE_DATA] = CACHE_TYPE_DATA,
63 [CTYPE_INSTRUCTION] = CACHE_TYPE_INST,
64 [CTYPE_UNIFIED] = CACHE_TYPE_UNIFIED,
65};
881730ad 66
6668022c
HC
67void show_cacheinfo(struct seq_file *m)
68{
45cce4cc 69 struct cpu_cacheinfo *this_cpu_ci;
d97d929f 70 struct cacheinfo *cache;
45cce4cc 71 int idx;
6668022c 72
77bb36e5
HC
73 if (!test_facility(34))
74 return;
45cce4cc
HC
75 get_online_cpus();
76 this_cpu_ci = get_cpu_cacheinfo(cpumask_any(cpu_online_mask));
d97d929f
SH
77 for (idx = 0; idx < this_cpu_ci->num_leaves; idx++) {
78 cache = this_cpu_ci->info_list + idx;
79 seq_printf(m, "cache%-11d: ", idx);
6668022c
HC
80 seq_printf(m, "level=%d ", cache->level);
81 seq_printf(m, "type=%s ", cache_type_string[cache->type]);
d97d929f
SH
82 seq_printf(m, "scope=%s ",
83 cache->disable_sysfs ? "Shared" : "Private");
84 seq_printf(m, "size=%dK ", cache->size >> 10);
85 seq_printf(m, "line_size=%u ", cache->coherency_line_size);
86 seq_printf(m, "associativity=%d", cache->ways_of_associativity);
6668022c 87 seq_puts(m, "\n");
6668022c 88 }
45cce4cc 89 put_online_cpus();
6668022c
HC
90}
91
d97d929f
SH
92static inline enum cache_type get_cache_type(struct cache_info *ci, int level)
93{
94 if (level >= CACHE_MAX_LEVEL)
95 return CACHE_TYPE_NOCACHE;
d97d929f 96 ci += level;
d97d929f
SH
97 if (ci->scope != CACHE_SCOPE_SHARED && ci->scope != CACHE_SCOPE_PRIVATE)
98 return CACHE_TYPE_NOCACHE;
d97d929f
SH
99 return cache_type_map[ci->type];
100}
101
881730ad
HC
102static inline unsigned long ecag(int ai, int li, int ti)
103{
104 unsigned long cmd, val;
105
106 cmd = ai << 4 | li << 1 | ti;
107 asm volatile(".insn rsy,0xeb000000004c,%0,0,0(%1)" /* ecag */
108 : "=d" (val) : "a" (cmd));
109 return val;
110}
111
d97d929f 112static void ci_leaf_init(struct cacheinfo *this_leaf, int private,
4fd4f1c7 113 enum cache_type type, unsigned int level, int cpu)
881730ad 114{
d97d929f 115 int ti, num_sets;
881730ad 116
d97d929f 117 if (type == CACHE_TYPE_INST)
d18f99c2
HC
118 ti = CACHE_TI_INSTRUCTION;
119 else
120 ti = CACHE_TI_UNIFIED;
d97d929f
SH
121 this_leaf->level = level + 1;
122 this_leaf->type = type;
123 this_leaf->coherency_line_size = ecag(EXTRACT_LINE_SIZE, level, ti);
f4dce5c9 124 this_leaf->ways_of_associativity = ecag(EXTRACT_ASSOCIATIVITY, level, ti);
d97d929f 125 this_leaf->size = ecag(EXTRACT_SIZE, level, ti);
d97d929f
SH
126 num_sets = this_leaf->size / this_leaf->coherency_line_size;
127 num_sets /= this_leaf->ways_of_associativity;
128 this_leaf->number_of_sets = num_sets;
129 cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
130 if (!private)
131 this_leaf->disable_sysfs = true;
881730ad 132}
881730ad 133
d97d929f 134int init_cache_level(unsigned int cpu)
881730ad 135{
d97d929f
SH
136 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
137 unsigned int level = 0, leaves = 0;
138 union cache_topology ct;
139 enum cache_type ctype;
881730ad 140
d97d929f
SH
141 if (!this_cpu_ci)
142 return -EINVAL;
d97d929f
SH
143 ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0);
144 do {
145 ctype = get_cache_type(&ct.ci[0], level);
146 if (ctype == CACHE_TYPE_NOCACHE)
6668022c 147 break;
d97d929f
SH
148 /* Separate instruction and data caches */
149 leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1;
150 } while (++level < CACHE_MAX_LEVEL);
d97d929f
SH
151 this_cpu_ci->num_levels = level;
152 this_cpu_ci->num_leaves = leaves;
d97d929f 153 return 0;
881730ad
HC
154}
155
d97d929f 156int populate_cache_leaves(unsigned int cpu)
881730ad 157{
f4dce5c9
HC
158 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
159 struct cacheinfo *this_leaf = this_cpu_ci->info_list;
d97d929f
SH
160 unsigned int level, idx, pvt;
161 union cache_topology ct;
162 enum cache_type ctype;
881730ad 163
77bb36e5
HC
164 if (!test_facility(34))
165 return -EOPNOTSUPP;
d97d929f
SH
166 ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0);
167 for (idx = 0, level = 0; level < this_cpu_ci->num_levels &&
168 idx < this_cpu_ci->num_leaves; idx++, level++) {
169 if (!this_leaf)
170 return -EINVAL;
d97d929f
SH
171 pvt = (ct.ci[level].scope == CACHE_SCOPE_PRIVATE) ? 1 : 0;
172 ctype = get_cache_type(&ct.ci[0], level);
173 if (ctype == CACHE_TYPE_SEPARATE) {
4fd4f1c7
HC
174 ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_DATA, level, cpu);
175 ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_INST, level, cpu);
d97d929f 176 } else {
4fd4f1c7 177 ci_leaf_init(this_leaf++, pvt, ctype, level, cpu);
d97d929f 178 }
881730ad 179 }
881730ad
HC
180 return 0;
181}