Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Extract CPU cache information and expose them via sysfs. |
| 4 | * |
| 5 | * Copyright IBM Corp. 2012 |
| 6 | * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com> |
| 7 | */ |
| 8 | |
Heiko Carstens | 6668022 | 2012-08-29 14:12:20 +0200 | [diff] [blame] | 9 | #include <linux/seq_file.h> |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 10 | #include <linux/cpu.h> |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 11 | #include <linux/cacheinfo.h> |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 12 | #include <asm/facility.h> |
| 13 | |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 14 | enum { |
| 15 | CACHE_SCOPE_NOTEXISTS, |
| 16 | CACHE_SCOPE_PRIVATE, |
| 17 | CACHE_SCOPE_SHARED, |
| 18 | CACHE_SCOPE_RESERVED, |
| 19 | }; |
| 20 | |
| 21 | enum { |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 22 | CTYPE_SEPARATE, |
| 23 | CTYPE_DATA, |
| 24 | CTYPE_INSTRUCTION, |
| 25 | CTYPE_UNIFIED, |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | enum { |
| 29 | EXTRACT_TOPOLOGY, |
| 30 | EXTRACT_LINE_SIZE, |
| 31 | EXTRACT_SIZE, |
| 32 | EXTRACT_ASSOCIATIVITY, |
| 33 | }; |
| 34 | |
| 35 | enum { |
| 36 | CACHE_TI_UNIFIED = 0, |
Heiko Carstens | d18f99c | 2012-10-18 13:13:41 +0200 | [diff] [blame] | 37 | CACHE_TI_DATA = 0, |
| 38 | CACHE_TI_INSTRUCTION, |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | struct cache_info { |
| 42 | unsigned char : 4; |
| 43 | unsigned char scope : 2; |
| 44 | unsigned char type : 2; |
| 45 | }; |
| 46 | |
| 47 | #define CACHE_MAX_LEVEL 8 |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 48 | union cache_topology { |
| 49 | struct cache_info ci[CACHE_MAX_LEVEL]; |
| 50 | unsigned long long raw; |
| 51 | }; |
| 52 | |
| 53 | static const char * const cache_type_string[] = { |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 54 | "", |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 55 | "Instruction", |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 56 | "Data", |
| 57 | "", |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 58 | "Unified", |
| 59 | }; |
| 60 | |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 61 | static const enum cache_type cache_type_map[] = { |
| 62 | [CTYPE_SEPARATE] = CACHE_TYPE_SEPARATE, |
| 63 | [CTYPE_DATA] = CACHE_TYPE_DATA, |
| 64 | [CTYPE_INSTRUCTION] = CACHE_TYPE_INST, |
| 65 | [CTYPE_UNIFIED] = CACHE_TYPE_UNIFIED, |
| 66 | }; |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 67 | |
Heiko Carstens | 6668022 | 2012-08-29 14:12:20 +0200 | [diff] [blame] | 68 | void show_cacheinfo(struct seq_file *m) |
| 69 | { |
Heiko Carstens | 45cce4c | 2015-02-09 12:54:16 +0100 | [diff] [blame] | 70 | struct cpu_cacheinfo *this_cpu_ci; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 71 | struct cacheinfo *cache; |
Heiko Carstens | 45cce4c | 2015-02-09 12:54:16 +0100 | [diff] [blame] | 72 | int idx; |
Heiko Carstens | 6668022 | 2012-08-29 14:12:20 +0200 | [diff] [blame] | 73 | |
Heiko Carstens | 77bb36e | 2015-03-18 13:22:00 +0100 | [diff] [blame] | 74 | if (!test_facility(34)) |
| 75 | return; |
Heiko Carstens | 45cce4c | 2015-02-09 12:54:16 +0100 | [diff] [blame] | 76 | this_cpu_ci = get_cpu_cacheinfo(cpumask_any(cpu_online_mask)); |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 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); |
Heiko Carstens | 6668022 | 2012-08-29 14:12:20 +0200 | [diff] [blame] | 80 | seq_printf(m, "level=%d ", cache->level); |
| 81 | seq_printf(m, "type=%s ", cache_type_string[cache->type]); |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 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); |
Heiko Carstens | 6668022 | 2012-08-29 14:12:20 +0200 | [diff] [blame] | 87 | seq_puts(m, "\n"); |
Heiko Carstens | 6668022 | 2012-08-29 14:12:20 +0200 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 91 | static inline enum cache_type get_cache_type(struct cache_info *ci, int level) |
| 92 | { |
| 93 | if (level >= CACHE_MAX_LEVEL) |
| 94 | return CACHE_TYPE_NOCACHE; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 95 | ci += level; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 96 | if (ci->scope != CACHE_SCOPE_SHARED && ci->scope != CACHE_SCOPE_PRIVATE) |
| 97 | return CACHE_TYPE_NOCACHE; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 98 | return cache_type_map[ci->type]; |
| 99 | } |
| 100 | |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 101 | static inline unsigned long ecag(int ai, int li, int ti) |
| 102 | { |
Heiko Carstens | 097a116 | 2016-04-14 12:35:22 +0200 | [diff] [blame] | 103 | return __ecag(ECAG_CACHE_ATTRIBUTE, ai << 4 | li << 1 | ti); |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 106 | static void ci_leaf_init(struct cacheinfo *this_leaf, int private, |
Heiko Carstens | 4fd4f1c | 2015-02-11 14:50:10 +0100 | [diff] [blame] | 107 | enum cache_type type, unsigned int level, int cpu) |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 108 | { |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 109 | int ti, num_sets; |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 110 | |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 111 | if (type == CACHE_TYPE_INST) |
Heiko Carstens | d18f99c | 2012-10-18 13:13:41 +0200 | [diff] [blame] | 112 | ti = CACHE_TI_INSTRUCTION; |
| 113 | else |
| 114 | ti = CACHE_TI_UNIFIED; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 115 | this_leaf->level = level + 1; |
| 116 | this_leaf->type = type; |
| 117 | this_leaf->coherency_line_size = ecag(EXTRACT_LINE_SIZE, level, ti); |
Heiko Carstens | f4dce5c | 2015-02-11 14:57:46 +0100 | [diff] [blame] | 118 | this_leaf->ways_of_associativity = ecag(EXTRACT_ASSOCIATIVITY, level, ti); |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 119 | this_leaf->size = ecag(EXTRACT_SIZE, level, ti); |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 120 | num_sets = this_leaf->size / this_leaf->coherency_line_size; |
| 121 | num_sets /= this_leaf->ways_of_associativity; |
| 122 | this_leaf->number_of_sets = num_sets; |
| 123 | cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map); |
| 124 | if (!private) |
| 125 | this_leaf->disable_sysfs = true; |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 128 | int init_cache_level(unsigned int cpu) |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 129 | { |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 130 | struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); |
| 131 | unsigned int level = 0, leaves = 0; |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 132 | union cache_topology ct; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 133 | enum cache_type ctype; |
| 134 | |
Heiko Carstens | 0b991f5 | 2015-07-27 09:53:49 +0200 | [diff] [blame] | 135 | if (!test_facility(34)) |
| 136 | return -EOPNOTSUPP; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 137 | if (!this_cpu_ci) |
| 138 | return -EINVAL; |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 139 | ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0); |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 140 | do { |
| 141 | ctype = get_cache_type(&ct.ci[0], level); |
| 142 | if (ctype == CACHE_TYPE_NOCACHE) |
Heiko Carstens | 6668022 | 2012-08-29 14:12:20 +0200 | [diff] [blame] | 143 | break; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 144 | /* Separate instruction and data caches */ |
| 145 | leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1; |
| 146 | } while (++level < CACHE_MAX_LEVEL); |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 147 | this_cpu_ci->num_levels = level; |
| 148 | this_cpu_ci->num_leaves = leaves; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int populate_cache_leaves(unsigned int cpu) |
| 153 | { |
Heiko Carstens | f4dce5c | 2015-02-11 14:57:46 +0100 | [diff] [blame] | 154 | struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu); |
| 155 | struct cacheinfo *this_leaf = this_cpu_ci->info_list; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 156 | unsigned int level, idx, pvt; |
| 157 | union cache_topology ct; |
| 158 | enum cache_type ctype; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 159 | |
Heiko Carstens | 77bb36e | 2015-03-18 13:22:00 +0100 | [diff] [blame] | 160 | if (!test_facility(34)) |
| 161 | return -EOPNOTSUPP; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 162 | ct.raw = ecag(EXTRACT_TOPOLOGY, 0, 0); |
| 163 | for (idx = 0, level = 0; level < this_cpu_ci->num_levels && |
| 164 | idx < this_cpu_ci->num_leaves; idx++, level++) { |
| 165 | if (!this_leaf) |
| 166 | return -EINVAL; |
Sudeep Holla | d97d929 | 2015-01-08 07:41:52 +0000 | [diff] [blame] | 167 | pvt = (ct.ci[level].scope == CACHE_SCOPE_PRIVATE) ? 1 : 0; |
| 168 | ctype = get_cache_type(&ct.ci[0], level); |
| 169 | if (ctype == CACHE_TYPE_SEPARATE) { |
Heiko Carstens | 4fd4f1c | 2015-02-11 14:50:10 +0100 | [diff] [blame] | 170 | ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_DATA, level, cpu); |
| 171 | ci_leaf_init(this_leaf++, pvt, CACHE_TYPE_INST, level, cpu); |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 172 | } else { |
Heiko Carstens | 4fd4f1c | 2015-02-11 14:50:10 +0100 | [diff] [blame] | 173 | ci_leaf_init(this_leaf++, pvt, ctype, level, cpu); |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 174 | } |
Heiko Carstens | 881730a | 2012-08-23 16:31:13 +0200 | [diff] [blame] | 175 | } |
| 176 | return 0; |
| 177 | } |