1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * SiFive composable cache controller Driver
4  *
5  * Copyright (C) 2018-2022 SiFive, Inc.
6  *
7  */
8 #include <linux/debugfs.h>
9 #include <linux/interrupt.h>
10 #include <linux/of_irq.h>
11 #include <linux/of_address.h>
12 #include <linux/device.h>
13 #include <asm/cacheinfo.h>
14 #include <soc/sifive/sifive_ccache.h>
15 
16 #define SIFIVE_CCACHE_DIRECCFIX_LOW 0x100
17 #define SIFIVE_CCACHE_DIRECCFIX_HIGH 0x104
18 #define SIFIVE_CCACHE_DIRECCFIX_COUNT 0x108
19 
20 #define SIFIVE_CCACHE_DIRECCFAIL_LOW 0x120
21 #define SIFIVE_CCACHE_DIRECCFAIL_HIGH 0x124
22 #define SIFIVE_CCACHE_DIRECCFAIL_COUNT 0x128
23 
24 #define SIFIVE_CCACHE_DATECCFIX_LOW 0x140
25 #define SIFIVE_CCACHE_DATECCFIX_HIGH 0x144
26 #define SIFIVE_CCACHE_DATECCFIX_COUNT 0x148
27 
28 #define SIFIVE_CCACHE_DATECCFAIL_LOW 0x160
29 #define SIFIVE_CCACHE_DATECCFAIL_HIGH 0x164
30 #define SIFIVE_CCACHE_DATECCFAIL_COUNT 0x168
31 
32 #define SIFIVE_CCACHE_CONFIG 0x00
33 #define SIFIVE_CCACHE_WAYENABLE 0x08
34 #define SIFIVE_CCACHE_ECCINJECTERR 0x40
35 
36 #define SIFIVE_CCACHE_MAX_ECCINTR 4
37 
38 static void __iomem *ccache_base;
39 static int g_irq[SIFIVE_CCACHE_MAX_ECCINTR];
40 static struct riscv_cacheinfo_ops ccache_cache_ops;
41 static int level;
42 
43 enum {
44 	DIR_CORR = 0,
45 	DATA_CORR,
46 	DATA_UNCORR,
47 	DIR_UNCORR,
48 };
49 
50 #ifdef CONFIG_DEBUG_FS
51 static struct dentry *sifive_test;
52 
53 static ssize_t ccache_write(struct file *file, const char __user *data,
54 			    size_t count, loff_t *ppos)
55 {
56 	unsigned int val;
57 
58 	if (kstrtouint_from_user(data, count, 0, &val))
59 		return -EINVAL;
60 	if ((val < 0xFF) || (val >= 0x10000 && val < 0x100FF))
61 		writel(val, ccache_base + SIFIVE_CCACHE_ECCINJECTERR);
62 	else
63 		return -EINVAL;
64 	return count;
65 }
66 
67 static const struct file_operations ccache_fops = {
68 	.owner = THIS_MODULE,
69 	.open = simple_open,
70 	.write = ccache_write
71 };
72 
73 static void setup_sifive_debug(void)
74 {
75 	sifive_test = debugfs_create_dir("sifive_ccache_cache", NULL);
76 
77 	debugfs_create_file("sifive_debug_inject_error", 0200,
78 			    sifive_test, NULL, &ccache_fops);
79 }
80 #endif
81 
82 static void ccache_config_read(void)
83 {
84 	u32 regval, val;
85 
86 	regval = readl(ccache_base + SIFIVE_CCACHE_CONFIG);
87 	val = regval & 0xFF;
88 	pr_info("CCACHE: No. of Banks in the cache: %d\n", val);
89 	val = (regval & 0xFF00) >> 8;
90 	pr_info("CCACHE: No. of ways per bank: %d\n", val);
91 	val = (regval & 0xFF0000) >> 16;
92 	pr_info("CCACHE: Sets per bank: %llu\n", (uint64_t)1 << val);
93 	val = (regval & 0xFF000000) >> 24;
94 	pr_info("CCACHE: Bytes per cache block: %llu\n", (uint64_t)1 << val);
95 
96 	regval = readl(ccache_base + SIFIVE_CCACHE_WAYENABLE);
97 	pr_info("CCACHE: Index of the largest way enabled: %d\n", regval);
98 }
99 
100 static const struct of_device_id sifive_ccache_ids[] = {
101 	{ .compatible = "sifive,fu540-c000-ccache" },
102 	{ .compatible = "sifive,fu740-c000-ccache" },
103 	{ .compatible = "sifive,ccache0" },
104 	{ /* end of table */ }
105 };
106 
107 static ATOMIC_NOTIFIER_HEAD(ccache_err_chain);
108 
109 int register_sifive_ccache_error_notifier(struct notifier_block *nb)
110 {
111 	return atomic_notifier_chain_register(&ccache_err_chain, nb);
112 }
113 EXPORT_SYMBOL_GPL(register_sifive_ccache_error_notifier);
114 
115 int unregister_sifive_ccache_error_notifier(struct notifier_block *nb)
116 {
117 	return atomic_notifier_chain_unregister(&ccache_err_chain, nb);
118 }
119 EXPORT_SYMBOL_GPL(unregister_sifive_ccache_error_notifier);
120 
121 static int ccache_largest_wayenabled(void)
122 {
123 	return readl(ccache_base + SIFIVE_CCACHE_WAYENABLE) & 0xFF;
124 }
125 
126 static ssize_t number_of_ways_enabled_show(struct device *dev,
127 					   struct device_attribute *attr,
128 					   char *buf)
129 {
130 	return sprintf(buf, "%u\n", ccache_largest_wayenabled());
131 }
132 
133 static DEVICE_ATTR_RO(number_of_ways_enabled);
134 
135 static struct attribute *priv_attrs[] = {
136 	&dev_attr_number_of_ways_enabled.attr,
137 	NULL,
138 };
139 
140 static const struct attribute_group priv_attr_group = {
141 	.attrs = priv_attrs,
142 };
143 
144 static const struct attribute_group *ccache_get_priv_group(struct cacheinfo
145 							   *this_leaf)
146 {
147 	/* We want to use private group for composable cache only */
148 	if (this_leaf->level == level)
149 		return &priv_attr_group;
150 	else
151 		return NULL;
152 }
153 
154 static irqreturn_t ccache_int_handler(int irq, void *device)
155 {
156 	unsigned int add_h, add_l;
157 
158 	if (irq == g_irq[DIR_CORR]) {
159 		add_h = readl(ccache_base + SIFIVE_CCACHE_DIRECCFIX_HIGH);
160 		add_l = readl(ccache_base + SIFIVE_CCACHE_DIRECCFIX_LOW);
161 		pr_err("CCACHE: DirError @ 0x%08X.%08X\n", add_h, add_l);
162 		/* Reading this register clears the DirError interrupt sig */
163 		readl(ccache_base + SIFIVE_CCACHE_DIRECCFIX_COUNT);
164 		atomic_notifier_call_chain(&ccache_err_chain,
165 					   SIFIVE_CCACHE_ERR_TYPE_CE,
166 					   "DirECCFix");
167 	}
168 	if (irq == g_irq[DIR_UNCORR]) {
169 		add_h = readl(ccache_base + SIFIVE_CCACHE_DIRECCFAIL_HIGH);
170 		add_l = readl(ccache_base + SIFIVE_CCACHE_DIRECCFAIL_LOW);
171 		/* Reading this register clears the DirFail interrupt sig */
172 		readl(ccache_base + SIFIVE_CCACHE_DIRECCFAIL_COUNT);
173 		atomic_notifier_call_chain(&ccache_err_chain,
174 					   SIFIVE_CCACHE_ERR_TYPE_UE,
175 					   "DirECCFail");
176 		panic("CCACHE: DirFail @ 0x%08X.%08X\n", add_h, add_l);
177 	}
178 	if (irq == g_irq[DATA_CORR]) {
179 		add_h = readl(ccache_base + SIFIVE_CCACHE_DATECCFIX_HIGH);
180 		add_l = readl(ccache_base + SIFIVE_CCACHE_DATECCFIX_LOW);
181 		pr_err("CCACHE: DataError @ 0x%08X.%08X\n", add_h, add_l);
182 		/* Reading this register clears the DataError interrupt sig */
183 		readl(ccache_base + SIFIVE_CCACHE_DATECCFIX_COUNT);
184 		atomic_notifier_call_chain(&ccache_err_chain,
185 					   SIFIVE_CCACHE_ERR_TYPE_CE,
186 					   "DatECCFix");
187 	}
188 	if (irq == g_irq[DATA_UNCORR]) {
189 		add_h = readl(ccache_base + SIFIVE_CCACHE_DATECCFAIL_HIGH);
190 		add_l = readl(ccache_base + SIFIVE_CCACHE_DATECCFAIL_LOW);
191 		pr_err("CCACHE: DataFail @ 0x%08X.%08X\n", add_h, add_l);
192 		/* Reading this register clears the DataFail interrupt sig */
193 		readl(ccache_base + SIFIVE_CCACHE_DATECCFAIL_COUNT);
194 		atomic_notifier_call_chain(&ccache_err_chain,
195 					   SIFIVE_CCACHE_ERR_TYPE_UE,
196 					   "DatECCFail");
197 	}
198 
199 	return IRQ_HANDLED;
200 }
201 
202 static int __init sifive_ccache_init(void)
203 {
204 	struct device_node *np;
205 	struct resource res;
206 	int i, rc, intr_num;
207 
208 	np = of_find_matching_node(NULL, sifive_ccache_ids);
209 	if (!np)
210 		return -ENODEV;
211 
212 	if (of_address_to_resource(np, 0, &res))
213 		return -ENODEV;
214 
215 	ccache_base = ioremap(res.start, resource_size(&res));
216 	if (!ccache_base)
217 		return -ENOMEM;
218 
219 	if (of_property_read_u32(np, "cache-level", &level))
220 		return -ENOENT;
221 
222 	intr_num = of_property_count_u32_elems(np, "interrupts");
223 	if (!intr_num) {
224 		pr_err("CCACHE: no interrupts property\n");
225 		return -ENODEV;
226 	}
227 
228 	for (i = 0; i < intr_num; i++) {
229 		g_irq[i] = irq_of_parse_and_map(np, i);
230 		rc = request_irq(g_irq[i], ccache_int_handler, 0, "ccache_ecc",
231 				 NULL);
232 		if (rc) {
233 			pr_err("CCACHE: Could not request IRQ %d\n", g_irq[i]);
234 			return rc;
235 		}
236 	}
237 
238 	ccache_config_read();
239 
240 	ccache_cache_ops.get_priv_group = ccache_get_priv_group;
241 	riscv_set_cacheinfo_ops(&ccache_cache_ops);
242 
243 #ifdef CONFIG_DEBUG_FS
244 	setup_sifive_debug();
245 #endif
246 	return 0;
247 }
248 
249 device_initcall(sifive_ccache_init);
250