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