xref: /openbmc/linux/arch/arm64/kernel/cacheinfo.c (revision 4b92d4add5f6dcf21275185c997d6ecb800054cd)
1c9af7f31SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25d425c18SSudeep Holla /*
35d425c18SSudeep Holla  *  ARM64 cacheinfo support
45d425c18SSudeep Holla  *
55d425c18SSudeep Holla  *  Copyright (C) 2015 ARM Ltd.
65d425c18SSudeep Holla  *  All Rights Reserved
75d425c18SSudeep Holla  */
85d425c18SSudeep Holla 
98571890eSJeremy Linton #include <linux/acpi.h>
105d425c18SSudeep Holla #include <linux/cacheinfo.h>
115d425c18SSudeep Holla #include <linux/of.h>
125d425c18SSudeep Holla 
135d425c18SSudeep Holla #define MAX_CACHE_LEVEL			7	/* Max 7 level supported */
145d425c18SSudeep Holla /* Ctypen, bits[3(n - 1) + 2 : 3(n - 1)], for n = 1 to 7 */
155d425c18SSudeep Holla #define CLIDR_CTYPE_SHIFT(level)	(3 * (level - 1))
165d425c18SSudeep Holla #define CLIDR_CTYPE_MASK(level)		(7 << CLIDR_CTYPE_SHIFT(level))
175d425c18SSudeep Holla #define CLIDR_CTYPE(clidr, level)	\
185d425c18SSudeep Holla 	(((clidr) & CLIDR_CTYPE_MASK(level)) >> CLIDR_CTYPE_SHIFT(level))
195d425c18SSudeep Holla 
207b8c87b2SShaokun Zhang int cache_line_size(void)
217b8c87b2SShaokun Zhang {
227b8c87b2SShaokun Zhang 	if (coherency_max_size != 0)
237b8c87b2SShaokun Zhang 		return coherency_max_size;
247b8c87b2SShaokun Zhang 
258f5c9037SMasayoshi Mizuma 	return cache_line_size_of_cpu();
267b8c87b2SShaokun Zhang }
277b8c87b2SShaokun Zhang EXPORT_SYMBOL_GPL(cache_line_size);
287b8c87b2SShaokun Zhang 
295d425c18SSudeep Holla static inline enum cache_type get_cache_type(int level)
305d425c18SSudeep Holla {
315d425c18SSudeep Holla 	u64 clidr;
325d425c18SSudeep Holla 
335d425c18SSudeep Holla 	if (level > MAX_CACHE_LEVEL)
345d425c18SSudeep Holla 		return CACHE_TYPE_NOCACHE;
35adf75899SMark Rutland 	clidr = read_sysreg(clidr_el1);
365d425c18SSudeep Holla 	return CLIDR_CTYPE(clidr, level);
375d425c18SSudeep Holla }
385d425c18SSudeep Holla 
395d425c18SSudeep Holla static void ci_leaf_init(struct cacheinfo *this_leaf,
405d425c18SSudeep Holla 			 enum cache_type type, unsigned int level)
415d425c18SSudeep Holla {
425d425c18SSudeep Holla 	this_leaf->level = level;
435d425c18SSudeep Holla 	this_leaf->type = type;
445d425c18SSudeep Holla }
455d425c18SSudeep Holla 
46*4b92d4adSThomas Gleixner int init_cache_level(unsigned int cpu)
475d425c18SSudeep Holla {
488571890eSJeremy Linton 	unsigned int ctype, level, leaves, fw_level;
495d425c18SSudeep Holla 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
505d425c18SSudeep Holla 
515d425c18SSudeep Holla 	for (level = 1, leaves = 0; level <= MAX_CACHE_LEVEL; level++) {
525d425c18SSudeep Holla 		ctype = get_cache_type(level);
535d425c18SSudeep Holla 		if (ctype == CACHE_TYPE_NOCACHE) {
545d425c18SSudeep Holla 			level--;
555d425c18SSudeep Holla 			break;
565d425c18SSudeep Holla 		}
575d425c18SSudeep Holla 		/* Separate instruction and data caches */
585d425c18SSudeep Holla 		leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1;
595d425c18SSudeep Holla 	}
605d425c18SSudeep Holla 
618571890eSJeremy Linton 	if (acpi_disabled)
628571890eSJeremy Linton 		fw_level = of_find_last_cache_level(cpu);
638571890eSJeremy Linton 	else
648571890eSJeremy Linton 		fw_level = acpi_find_last_cache_level(cpu);
658571890eSJeremy Linton 
668571890eSJeremy Linton 	if (level < fw_level) {
679a802431SSudeep Holla 		/*
689a802431SSudeep Holla 		 * some external caches not specified in CLIDR_EL1
699a802431SSudeep Holla 		 * the information may be available in the device tree
709a802431SSudeep Holla 		 * only unified external caches are considered here
719a802431SSudeep Holla 		 */
728571890eSJeremy Linton 		leaves += (fw_level - level);
738571890eSJeremy Linton 		level = fw_level;
749a802431SSudeep Holla 	}
759a802431SSudeep Holla 
765d425c18SSudeep Holla 	this_cpu_ci->num_levels = level;
775d425c18SSudeep Holla 	this_cpu_ci->num_leaves = leaves;
785d425c18SSudeep Holla 	return 0;
795d425c18SSudeep Holla }
805d425c18SSudeep Holla 
81*4b92d4adSThomas Gleixner int populate_cache_leaves(unsigned int cpu)
825d425c18SSudeep Holla {
835d425c18SSudeep Holla 	unsigned int level, idx;
845d425c18SSudeep Holla 	enum cache_type type;
855d425c18SSudeep Holla 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
865d425c18SSudeep Holla 	struct cacheinfo *this_leaf = this_cpu_ci->info_list;
875d425c18SSudeep Holla 
885d425c18SSudeep Holla 	for (idx = 0, level = 1; level <= this_cpu_ci->num_levels &&
895d425c18SSudeep Holla 	     idx < this_cpu_ci->num_leaves; idx++, level++) {
905d425c18SSudeep Holla 		type = get_cache_type(level);
915d425c18SSudeep Holla 		if (type == CACHE_TYPE_SEPARATE) {
925d425c18SSudeep Holla 			ci_leaf_init(this_leaf++, CACHE_TYPE_DATA, level);
935d425c18SSudeep Holla 			ci_leaf_init(this_leaf++, CACHE_TYPE_INST, level);
945d425c18SSudeep Holla 		} else {
955d425c18SSudeep Holla 			ci_leaf_init(this_leaf++, type, level);
965d425c18SSudeep Holla 		}
975d425c18SSudeep Holla 	}
985d425c18SSudeep Holla 	return 0;
995d425c18SSudeep Holla }
100