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
cache_line_size(void)157b8c87b2SShaokun Zhang int cache_line_size(void)
167b8c87b2SShaokun Zhang {
177b8c87b2SShaokun Zhang if (coherency_max_size != 0)
187b8c87b2SShaokun Zhang return coherency_max_size;
197b8c87b2SShaokun Zhang
208f5c9037SMasayoshi Mizuma return cache_line_size_of_cpu();
217b8c87b2SShaokun Zhang }
227b8c87b2SShaokun Zhang EXPORT_SYMBOL_GPL(cache_line_size);
237b8c87b2SShaokun Zhang
get_cache_type(int level)245d425c18SSudeep Holla static inline enum cache_type get_cache_type(int level)
255d425c18SSudeep Holla {
265d425c18SSudeep Holla u64 clidr;
275d425c18SSudeep Holla
285d425c18SSudeep Holla if (level > MAX_CACHE_LEVEL)
295d425c18SSudeep Holla return CACHE_TYPE_NOCACHE;
30adf75899SMark Rutland clidr = read_sysreg(clidr_el1);
315d425c18SSudeep Holla return CLIDR_CTYPE(clidr, level);
325d425c18SSudeep Holla }
335d425c18SSudeep Holla
ci_leaf_init(struct cacheinfo * this_leaf,enum cache_type type,unsigned int level)345d425c18SSudeep Holla static void ci_leaf_init(struct cacheinfo *this_leaf,
355d425c18SSudeep Holla enum cache_type type, unsigned int level)
365d425c18SSudeep Holla {
375d425c18SSudeep Holla this_leaf->level = level;
385d425c18SSudeep Holla this_leaf->type = type;
395d425c18SSudeep Holla }
405d425c18SSudeep Holla
detect_cache_level(unsigned int * level_p,unsigned int * leaves_p)41*c931680cSRadu Rendec static void detect_cache_level(unsigned int *level_p, unsigned int *leaves_p)
425d425c18SSudeep Holla {
43e75d18ceSSudeep Holla unsigned int ctype, level, leaves;
445d425c18SSudeep Holla
455d425c18SSudeep Holla for (level = 1, leaves = 0; level <= MAX_CACHE_LEVEL; level++) {
465d425c18SSudeep Holla ctype = get_cache_type(level);
475d425c18SSudeep Holla if (ctype == CACHE_TYPE_NOCACHE) {
485d425c18SSudeep Holla level--;
495d425c18SSudeep Holla break;
505d425c18SSudeep Holla }
515d425c18SSudeep Holla /* Separate instruction and data caches */
525d425c18SSudeep Holla leaves += (ctype == CACHE_TYPE_SEPARATE) ? 2 : 1;
535d425c18SSudeep Holla }
545d425c18SSudeep Holla
55*c931680cSRadu Rendec *level_p = level;
56*c931680cSRadu Rendec *leaves_p = leaves;
57*c931680cSRadu Rendec }
58*c931680cSRadu Rendec
early_cache_level(unsigned int cpu)59*c931680cSRadu Rendec int early_cache_level(unsigned int cpu)
60*c931680cSRadu Rendec {
61*c931680cSRadu Rendec struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
62*c931680cSRadu Rendec
63*c931680cSRadu Rendec detect_cache_level(&this_cpu_ci->num_levels, &this_cpu_ci->num_leaves);
64*c931680cSRadu Rendec
65*c931680cSRadu Rendec return 0;
66*c931680cSRadu Rendec }
67*c931680cSRadu Rendec
init_cache_level(unsigned int cpu)68*c931680cSRadu Rendec int init_cache_level(unsigned int cpu)
69*c931680cSRadu Rendec {
70*c931680cSRadu Rendec unsigned int level, leaves;
71*c931680cSRadu Rendec int fw_level, ret;
72*c931680cSRadu Rendec struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
73*c931680cSRadu Rendec
74*c931680cSRadu Rendec detect_cache_level(&level, &leaves);
75*c931680cSRadu Rendec
76bd500361SPierre Gondois if (acpi_disabled) {
778571890eSJeremy Linton fw_level = of_find_last_cache_level(cpu);
78bd500361SPierre Gondois } else {
79bd500361SPierre Gondois ret = acpi_get_cache_info(cpu, &fw_level, NULL);
80bd500361SPierre Gondois if (ret < 0)
81d931b83eSPierre Gondois fw_level = 0;
82bd500361SPierre Gondois }
838571890eSJeremy Linton
848571890eSJeremy Linton if (level < fw_level) {
859a802431SSudeep Holla /*
869a802431SSudeep Holla * some external caches not specified in CLIDR_EL1
879a802431SSudeep Holla * the information may be available in the device tree
889a802431SSudeep Holla * only unified external caches are considered here
899a802431SSudeep Holla */
908571890eSJeremy Linton leaves += (fw_level - level);
918571890eSJeremy Linton level = fw_level;
929a802431SSudeep Holla }
939a802431SSudeep Holla
945d425c18SSudeep Holla this_cpu_ci->num_levels = level;
955d425c18SSudeep Holla this_cpu_ci->num_leaves = leaves;
965d425c18SSudeep Holla return 0;
975d425c18SSudeep Holla }
985d425c18SSudeep Holla
populate_cache_leaves(unsigned int cpu)994b92d4adSThomas Gleixner int populate_cache_leaves(unsigned int cpu)
1005d425c18SSudeep Holla {
1015d425c18SSudeep Holla unsigned int level, idx;
1025d425c18SSudeep Holla enum cache_type type;
1035d425c18SSudeep Holla struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
1045d425c18SSudeep Holla struct cacheinfo *this_leaf = this_cpu_ci->info_list;
1055d425c18SSudeep Holla
1065d425c18SSudeep Holla for (idx = 0, level = 1; level <= this_cpu_ci->num_levels &&
1075d425c18SSudeep Holla idx < this_cpu_ci->num_leaves; idx++, level++) {
1085d425c18SSudeep Holla type = get_cache_type(level);
1095d425c18SSudeep Holla if (type == CACHE_TYPE_SEPARATE) {
1105d425c18SSudeep Holla ci_leaf_init(this_leaf++, CACHE_TYPE_DATA, level);
1115d425c18SSudeep Holla ci_leaf_init(this_leaf++, CACHE_TYPE_INST, level);
1125d425c18SSudeep Holla } else {
1135d425c18SSudeep Holla ci_leaf_init(this_leaf++, type, level);
1145d425c18SSudeep Holla }
1155d425c18SSudeep Holla }
1165d425c18SSudeep Holla return 0;
1175d425c18SSudeep Holla }
118