xref: /openbmc/linux/arch/arm64/kernel/cacheinfo.c (revision e75d18cecbb3805895d8ed64da4f78575ec96043)
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 
464b92d4adSThomas Gleixner int init_cache_level(unsigned int cpu)
475d425c18SSudeep Holla {
48*e75d18ceSSudeep Holla 	unsigned int ctype, level, leaves;
49*e75d18ceSSudeep Holla 	int fw_level;
505d425c18SSudeep Holla 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
515d425c18SSudeep Holla 
525d425c18SSudeep Holla 	for (level = 1, leaves = 0; level <= MAX_CACHE_LEVEL; level++) {
535d425c18SSudeep Holla 		ctype = get_cache_type(level);
545d425c18SSudeep Holla 		if (ctype == CACHE_TYPE_NOCACHE) {
555d425c18SSudeep Holla 			level--;
565d425c18SSudeep Holla 			break;
575d425c18SSudeep Holla 		}
585d425c18SSudeep Holla 		/* Separate instruction and data caches */
595d425c18SSudeep Holla 		leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1;
605d425c18SSudeep Holla 	}
615d425c18SSudeep Holla 
628571890eSJeremy Linton 	if (acpi_disabled)
638571890eSJeremy Linton 		fw_level = of_find_last_cache_level(cpu);
648571890eSJeremy Linton 	else
658571890eSJeremy Linton 		fw_level = acpi_find_last_cache_level(cpu);
668571890eSJeremy Linton 
67*e75d18ceSSudeep Holla 	if (fw_level < 0)
68*e75d18ceSSudeep Holla 		return fw_level;
69*e75d18ceSSudeep Holla 
708571890eSJeremy Linton 	if (level < fw_level) {
719a802431SSudeep Holla 		/*
729a802431SSudeep Holla 		 * some external caches not specified in CLIDR_EL1
739a802431SSudeep Holla 		 * the information may be available in the device tree
749a802431SSudeep Holla 		 * only unified external caches are considered here
759a802431SSudeep Holla 		 */
768571890eSJeremy Linton 		leaves += (fw_level - level);
778571890eSJeremy Linton 		level = fw_level;
789a802431SSudeep Holla 	}
799a802431SSudeep Holla 
805d425c18SSudeep Holla 	this_cpu_ci->num_levels = level;
815d425c18SSudeep Holla 	this_cpu_ci->num_leaves = leaves;
825d425c18SSudeep Holla 	return 0;
835d425c18SSudeep Holla }
845d425c18SSudeep Holla 
854b92d4adSThomas Gleixner int populate_cache_leaves(unsigned int cpu)
865d425c18SSudeep Holla {
875d425c18SSudeep Holla 	unsigned int level, idx;
885d425c18SSudeep Holla 	enum cache_type type;
895d425c18SSudeep Holla 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
905d425c18SSudeep Holla 	struct cacheinfo *this_leaf = this_cpu_ci->info_list;
915d425c18SSudeep Holla 
925d425c18SSudeep Holla 	for (idx = 0, level = 1; level <= this_cpu_ci->num_levels &&
935d425c18SSudeep Holla 	     idx < this_cpu_ci->num_leaves; idx++, level++) {
945d425c18SSudeep Holla 		type = get_cache_type(level);
955d425c18SSudeep Holla 		if (type == CACHE_TYPE_SEPARATE) {
965d425c18SSudeep Holla 			ci_leaf_init(this_leaf++, CACHE_TYPE_DATA, level);
975d425c18SSudeep Holla 			ci_leaf_init(this_leaf++, CACHE_TYPE_INST, level);
985d425c18SSudeep Holla 		} else {
995d425c18SSudeep Holla 			ci_leaf_init(this_leaf++, type, level);
1005d425c18SSudeep Holla 		}
1015d425c18SSudeep Holla 	}
1025d425c18SSudeep Holla 	return 0;
1035d425c18SSudeep Holla }
104