Linux Audio

Check our new training course

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