1 // SPDX-License-Identifier: GPL-2.0
3 * SiFive composable cache controller Driver
5 * Copyright (C) 2018-2022 SiFive, Inc.
9 #define pr_fmt(fmt) "CCACHE: " fmt
11 #include <linux/debugfs.h>
12 #include <linux/interrupt.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_address.h>
15 #include <linux/device.h>
16 #include <linux/bitfield.h>
17 #include <asm/cacheinfo.h>
18 #include <soc/sifive/sifive_ccache.h>
20 #define SIFIVE_CCACHE_DIRECCFIX_LOW 0x100
21 #define SIFIVE_CCACHE_DIRECCFIX_HIGH 0x104
22 #define SIFIVE_CCACHE_DIRECCFIX_COUNT 0x108
24 #define SIFIVE_CCACHE_DIRECCFAIL_LOW 0x120
25 #define SIFIVE_CCACHE_DIRECCFAIL_HIGH 0x124
26 #define SIFIVE_CCACHE_DIRECCFAIL_COUNT 0x128
28 #define SIFIVE_CCACHE_DATECCFIX_LOW 0x140
29 #define SIFIVE_CCACHE_DATECCFIX_HIGH 0x144
30 #define SIFIVE_CCACHE_DATECCFIX_COUNT 0x148
32 #define SIFIVE_CCACHE_DATECCFAIL_LOW 0x160
33 #define SIFIVE_CCACHE_DATECCFAIL_HIGH 0x164
34 #define SIFIVE_CCACHE_DATECCFAIL_COUNT 0x168
36 #define SIFIVE_CCACHE_CONFIG 0x00
37 #define SIFIVE_CCACHE_CONFIG_BANK_MASK GENMASK_ULL(7, 0)
38 #define SIFIVE_CCACHE_CONFIG_WAYS_MASK GENMASK_ULL(15, 8)
39 #define SIFIVE_CCACHE_CONFIG_SETS_MASK GENMASK_ULL(23, 16)
40 #define SIFIVE_CCACHE_CONFIG_BLKS_MASK GENMASK_ULL(31, 24)
42 #define SIFIVE_CCACHE_WAYENABLE 0x08
43 #define SIFIVE_CCACHE_ECCINJECTERR 0x40
45 #define SIFIVE_CCACHE_MAX_ECCINTR 4
47 static void __iomem *ccache_base;
48 static int g_irq[SIFIVE_CCACHE_MAX_ECCINTR];
49 static struct riscv_cacheinfo_ops ccache_cache_ops;
59 #ifdef CONFIG_DEBUG_FS
60 static struct dentry *sifive_test;
62 static ssize_t ccache_write(struct file *file, const char __user *data,
63 size_t count, loff_t *ppos)
67 if (kstrtouint_from_user(data, count, 0, &val))
69 if ((val < 0xFF) || (val >= 0x10000 && val < 0x100FF))
70 writel(val, ccache_base + SIFIVE_CCACHE_ECCINJECTERR);
76 static const struct file_operations ccache_fops = {
82 static void setup_sifive_debug(void)
84 sifive_test = debugfs_create_dir("sifive_ccache_cache", NULL);
86 debugfs_create_file("sifive_debug_inject_error", 0200,
87 sifive_test, NULL, &ccache_fops);
91 static void ccache_config_read(void)
95 cfg = readl(ccache_base + SIFIVE_CCACHE_CONFIG);
96 pr_info("%llu banks, %llu ways, sets/bank=%llu, bytes/block=%llu\n",
97 FIELD_GET(SIFIVE_CCACHE_CONFIG_BANK_MASK, cfg),
98 FIELD_GET(SIFIVE_CCACHE_CONFIG_WAYS_MASK, cfg),
99 BIT_ULL(FIELD_GET(SIFIVE_CCACHE_CONFIG_SETS_MASK, cfg)),
100 BIT_ULL(FIELD_GET(SIFIVE_CCACHE_CONFIG_BLKS_MASK, cfg)));
102 cfg = readl(ccache_base + SIFIVE_CCACHE_WAYENABLE);
103 pr_info("Index of the largest way enabled: %u\n", cfg);
106 static const struct of_device_id sifive_ccache_ids[] = {
107 { .compatible = "sifive,fu540-c000-ccache" },
108 { .compatible = "sifive,fu740-c000-ccache" },
109 { .compatible = "sifive,ccache0" },
110 { .compatible = "starfive,jh7110-ccache" },
111 { /* end of table */ }
114 static ATOMIC_NOTIFIER_HEAD(ccache_err_chain);
116 int register_sifive_ccache_error_notifier(struct notifier_block *nb)
118 return atomic_notifier_chain_register(&ccache_err_chain, nb);
120 EXPORT_SYMBOL_GPL(register_sifive_ccache_error_notifier);
122 int unregister_sifive_ccache_error_notifier(struct notifier_block *nb)
124 return atomic_notifier_chain_unregister(&ccache_err_chain, nb);
126 EXPORT_SYMBOL_GPL(unregister_sifive_ccache_error_notifier);
128 static int ccache_largest_wayenabled(void)
130 return readl(ccache_base + SIFIVE_CCACHE_WAYENABLE) & 0xFF;
133 static ssize_t number_of_ways_enabled_show(struct device *dev,
134 struct device_attribute *attr,
137 return sprintf(buf, "%u\n", ccache_largest_wayenabled());
140 static DEVICE_ATTR_RO(number_of_ways_enabled);
142 static struct attribute *priv_attrs[] = {
143 &dev_attr_number_of_ways_enabled.attr,
147 static const struct attribute_group priv_attr_group = {
151 static const struct attribute_group *ccache_get_priv_group(struct cacheinfo
154 /* We want to use private group for composable cache only */
155 if (this_leaf->level == level)
156 return &priv_attr_group;
161 static irqreturn_t ccache_int_handler(int irq, void *device)
163 unsigned int add_h, add_l;
165 if (irq == g_irq[DIR_CORR]) {
166 add_h = readl(ccache_base + SIFIVE_CCACHE_DIRECCFIX_HIGH);
167 add_l = readl(ccache_base + SIFIVE_CCACHE_DIRECCFIX_LOW);
168 pr_err("DirError @ 0x%08X.%08X\n", add_h, add_l);
169 /* Reading this register clears the DirError interrupt sig */
170 readl(ccache_base + SIFIVE_CCACHE_DIRECCFIX_COUNT);
171 atomic_notifier_call_chain(&ccache_err_chain,
172 SIFIVE_CCACHE_ERR_TYPE_CE,
175 if (irq == g_irq[DIR_UNCORR]) {
176 add_h = readl(ccache_base + SIFIVE_CCACHE_DIRECCFAIL_HIGH);
177 add_l = readl(ccache_base + SIFIVE_CCACHE_DIRECCFAIL_LOW);
178 /* Reading this register clears the DirFail interrupt sig */
179 readl(ccache_base + SIFIVE_CCACHE_DIRECCFAIL_COUNT);
180 atomic_notifier_call_chain(&ccache_err_chain,
181 SIFIVE_CCACHE_ERR_TYPE_UE,
183 panic("CCACHE: DirFail @ 0x%08X.%08X\n", add_h, add_l);
185 if (irq == g_irq[DATA_CORR]) {
186 add_h = readl(ccache_base + SIFIVE_CCACHE_DATECCFIX_HIGH);
187 add_l = readl(ccache_base + SIFIVE_CCACHE_DATECCFIX_LOW);
188 pr_err("DataError @ 0x%08X.%08X\n", add_h, add_l);
189 /* Reading this register clears the DataError interrupt sig */
190 readl(ccache_base + SIFIVE_CCACHE_DATECCFIX_COUNT);
191 atomic_notifier_call_chain(&ccache_err_chain,
192 SIFIVE_CCACHE_ERR_TYPE_CE,
195 if (irq == g_irq[DATA_UNCORR]) {
196 add_h = readl(ccache_base + SIFIVE_CCACHE_DATECCFAIL_HIGH);
197 add_l = readl(ccache_base + SIFIVE_CCACHE_DATECCFAIL_LOW);
198 pr_err("DataFail @ 0x%08X.%08X\n", add_h, add_l);
199 /* Reading this register clears the DataFail interrupt sig */
200 readl(ccache_base + SIFIVE_CCACHE_DATECCFAIL_COUNT);
201 atomic_notifier_call_chain(&ccache_err_chain,
202 SIFIVE_CCACHE_ERR_TYPE_UE,
209 static int __init sifive_ccache_init(void)
211 struct device_node *np;
215 np = of_find_matching_node(NULL, sifive_ccache_ids);
219 if (of_address_to_resource(np, 0, &res)) {
224 ccache_base = ioremap(res.start, resource_size(&res));
230 if (of_property_read_u32(np, "cache-level", &level)) {
235 intr_num = of_property_count_u32_elems(np, "interrupts");
237 pr_err("No interrupts property\n");
242 for (i = 0; i < intr_num; i++) {
243 g_irq[i] = irq_of_parse_and_map(np, i);
244 rc = request_irq(g_irq[i], ccache_int_handler, 0, "ccache_ecc",
247 pr_err("Could not request IRQ %d\n", g_irq[i]);
253 ccache_config_read();
255 ccache_cache_ops.get_priv_group = ccache_get_priv_group;
256 riscv_set_cacheinfo_ops(&ccache_cache_ops);
258 #ifdef CONFIG_DEBUG_FS
259 setup_sifive_debug();
265 free_irq(g_irq[i], NULL);
267 iounmap(ccache_base);
273 device_initcall(sifive_ccache_init);