xref: /openbmc/linux/arch/arm64/kernel/cacheinfo.c (revision bd500361a937c03a3da57178287ce543c8f3681b)
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 {
48e75d18ceSSudeep Holla 	unsigned int ctype, level, leaves;
49*bd500361SPierre Gondois 	int fw_level, ret;
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 
62*bd500361SPierre Gondois 	if (acpi_disabled) {
638571890eSJeremy Linton 		fw_level = of_find_last_cache_level(cpu);
64*bd500361SPierre Gondois 	} else {
65*bd500361SPierre Gondois 		ret = acpi_get_cache_info(cpu, &fw_level, NULL);
66*bd500361SPierre Gondois 		if (ret < 0)
67*bd500361SPierre Gondois 			return ret;
68*bd500361SPierre Gondois 	}
698571890eSJeremy Linton 
70e75d18ceSSudeep Holla 	if (fw_level < 0)
71e75d18ceSSudeep Holla 		return fw_level;
72e75d18ceSSudeep Holla 
738571890eSJeremy Linton 	if (level < fw_level) {
749a802431SSudeep Holla 		/*
759a802431SSudeep Holla 		 * some external caches not specified in CLIDR_EL1
769a802431SSudeep Holla 		 * the information may be available in the device tree
779a802431SSudeep Holla 		 * only unified external caches are considered here
789a802431SSudeep Holla 		 */
798571890eSJeremy Linton 		leaves += (fw_level - level);
808571890eSJeremy Linton 		level = fw_level;
819a802431SSudeep Holla 	}
829a802431SSudeep Holla 
835d425c18SSudeep Holla 	this_cpu_ci->num_levels = level;
845d425c18SSudeep Holla 	this_cpu_ci->num_leaves = leaves;
855d425c18SSudeep Holla 	return 0;
865d425c18SSudeep Holla }
875d425c18SSudeep Holla 
884b92d4adSThomas Gleixner int populate_cache_leaves(unsigned int cpu)
895d425c18SSudeep Holla {
905d425c18SSudeep Holla 	unsigned int level, idx;
915d425c18SSudeep Holla 	enum cache_type type;
925d425c18SSudeep Holla 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
935d425c18SSudeep Holla 	struct cacheinfo *this_leaf = this_cpu_ci->info_list;
945d425c18SSudeep Holla 
955d425c18SSudeep Holla 	for (idx = 0, level = 1; level <= this_cpu_ci->num_levels &&
965d425c18SSudeep Holla 	     idx < this_cpu_ci->num_leaves; idx++, level++) {
975d425c18SSudeep Holla 		type = get_cache_type(level);
985d425c18SSudeep Holla 		if (type == CACHE_TYPE_SEPARATE) {
995d425c18SSudeep Holla 			ci_leaf_init(this_leaf++, CACHE_TYPE_DATA, level);
1005d425c18SSudeep Holla 			ci_leaf_init(this_leaf++, CACHE_TYPE_INST, level);
1015d425c18SSudeep Holla 		} else {
1025d425c18SSudeep Holla 			ci_leaf_init(this_leaf++, type, level);
1035d425c18SSudeep Holla 		}
1045d425c18SSudeep Holla 	}
1055d425c18SSudeep Holla 	return 0;
1065d425c18SSudeep Holla }
107