1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * SiFive L2 cache controller Driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2018-2019 SiFive, Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun #include <linux/debugfs.h>
9*4882a593Smuzhiyun #include <linux/interrupt.h>
10*4882a593Smuzhiyun #include <linux/of_irq.h>
11*4882a593Smuzhiyun #include <linux/of_address.h>
12*4882a593Smuzhiyun #include <linux/device.h>
13*4882a593Smuzhiyun #include <asm/cacheinfo.h>
14*4882a593Smuzhiyun #include <soc/sifive/sifive_l2_cache.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #define SIFIVE_L2_DIRECCFIX_LOW 0x100
17*4882a593Smuzhiyun #define SIFIVE_L2_DIRECCFIX_HIGH 0x104
18*4882a593Smuzhiyun #define SIFIVE_L2_DIRECCFIX_COUNT 0x108
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define SIFIVE_L2_DATECCFIX_LOW 0x140
21*4882a593Smuzhiyun #define SIFIVE_L2_DATECCFIX_HIGH 0x144
22*4882a593Smuzhiyun #define SIFIVE_L2_DATECCFIX_COUNT 0x148
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #define SIFIVE_L2_DATECCFAIL_LOW 0x160
25*4882a593Smuzhiyun #define SIFIVE_L2_DATECCFAIL_HIGH 0x164
26*4882a593Smuzhiyun #define SIFIVE_L2_DATECCFAIL_COUNT 0x168
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #define SIFIVE_L2_CONFIG 0x00
29*4882a593Smuzhiyun #define SIFIVE_L2_WAYENABLE 0x08
30*4882a593Smuzhiyun #define SIFIVE_L2_ECCINJECTERR 0x40
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define SIFIVE_L2_MAX_ECCINTR 3
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun static void __iomem *l2_base;
35*4882a593Smuzhiyun static int g_irq[SIFIVE_L2_MAX_ECCINTR];
36*4882a593Smuzhiyun static struct riscv_cacheinfo_ops l2_cache_ops;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun enum {
39*4882a593Smuzhiyun DIR_CORR = 0,
40*4882a593Smuzhiyun DATA_CORR,
41*4882a593Smuzhiyun DATA_UNCORR,
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
45*4882a593Smuzhiyun static struct dentry *sifive_test;
46*4882a593Smuzhiyun
l2_write(struct file * file,const char __user * data,size_t count,loff_t * ppos)47*4882a593Smuzhiyun static ssize_t l2_write(struct file *file, const char __user *data,
48*4882a593Smuzhiyun size_t count, loff_t *ppos)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun unsigned int val;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun if (kstrtouint_from_user(data, count, 0, &val))
53*4882a593Smuzhiyun return -EINVAL;
54*4882a593Smuzhiyun if ((val < 0xFF) || (val >= 0x10000 && val < 0x100FF))
55*4882a593Smuzhiyun writel(val, l2_base + SIFIVE_L2_ECCINJECTERR);
56*4882a593Smuzhiyun else
57*4882a593Smuzhiyun return -EINVAL;
58*4882a593Smuzhiyun return count;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun static const struct file_operations l2_fops = {
62*4882a593Smuzhiyun .owner = THIS_MODULE,
63*4882a593Smuzhiyun .open = simple_open,
64*4882a593Smuzhiyun .write = l2_write
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun
setup_sifive_debug(void)67*4882a593Smuzhiyun static void setup_sifive_debug(void)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun sifive_test = debugfs_create_dir("sifive_l2_cache", NULL);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun debugfs_create_file("sifive_debug_inject_error", 0200,
72*4882a593Smuzhiyun sifive_test, NULL, &l2_fops);
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun #endif
75*4882a593Smuzhiyun
l2_config_read(void)76*4882a593Smuzhiyun static void l2_config_read(void)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun u32 regval, val;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun regval = readl(l2_base + SIFIVE_L2_CONFIG);
81*4882a593Smuzhiyun val = regval & 0xFF;
82*4882a593Smuzhiyun pr_info("L2CACHE: No. of Banks in the cache: %d\n", val);
83*4882a593Smuzhiyun val = (regval & 0xFF00) >> 8;
84*4882a593Smuzhiyun pr_info("L2CACHE: No. of ways per bank: %d\n", val);
85*4882a593Smuzhiyun val = (regval & 0xFF0000) >> 16;
86*4882a593Smuzhiyun pr_info("L2CACHE: Sets per bank: %llu\n", (uint64_t)1 << val);
87*4882a593Smuzhiyun val = (regval & 0xFF000000) >> 24;
88*4882a593Smuzhiyun pr_info("L2CACHE: Bytes per cache block: %llu\n", (uint64_t)1 << val);
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun regval = readl(l2_base + SIFIVE_L2_WAYENABLE);
91*4882a593Smuzhiyun pr_info("L2CACHE: Index of the largest way enabled: %d\n", regval);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun static const struct of_device_id sifive_l2_ids[] = {
95*4882a593Smuzhiyun { .compatible = "sifive,fu540-c000-ccache" },
96*4882a593Smuzhiyun { /* end of table */ },
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun static ATOMIC_NOTIFIER_HEAD(l2_err_chain);
100*4882a593Smuzhiyun
register_sifive_l2_error_notifier(struct notifier_block * nb)101*4882a593Smuzhiyun int register_sifive_l2_error_notifier(struct notifier_block *nb)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun return atomic_notifier_chain_register(&l2_err_chain, nb);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(register_sifive_l2_error_notifier);
106*4882a593Smuzhiyun
unregister_sifive_l2_error_notifier(struct notifier_block * nb)107*4882a593Smuzhiyun int unregister_sifive_l2_error_notifier(struct notifier_block *nb)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun return atomic_notifier_chain_unregister(&l2_err_chain, nb);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(unregister_sifive_l2_error_notifier);
112*4882a593Smuzhiyun
l2_largest_wayenabled(void)113*4882a593Smuzhiyun static int l2_largest_wayenabled(void)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun return readl(l2_base + SIFIVE_L2_WAYENABLE) & 0xFF;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
number_of_ways_enabled_show(struct device * dev,struct device_attribute * attr,char * buf)118*4882a593Smuzhiyun static ssize_t number_of_ways_enabled_show(struct device *dev,
119*4882a593Smuzhiyun struct device_attribute *attr,
120*4882a593Smuzhiyun char *buf)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun return sprintf(buf, "%u\n", l2_largest_wayenabled());
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun static DEVICE_ATTR_RO(number_of_ways_enabled);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun static struct attribute *priv_attrs[] = {
128*4882a593Smuzhiyun &dev_attr_number_of_ways_enabled.attr,
129*4882a593Smuzhiyun NULL,
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun static const struct attribute_group priv_attr_group = {
133*4882a593Smuzhiyun .attrs = priv_attrs,
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun
l2_get_priv_group(struct cacheinfo * this_leaf)136*4882a593Smuzhiyun static const struct attribute_group *l2_get_priv_group(struct cacheinfo *this_leaf)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun /* We want to use private group for L2 cache only */
139*4882a593Smuzhiyun if (this_leaf->level == 2)
140*4882a593Smuzhiyun return &priv_attr_group;
141*4882a593Smuzhiyun else
142*4882a593Smuzhiyun return NULL;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
l2_int_handler(int irq,void * device)145*4882a593Smuzhiyun static irqreturn_t l2_int_handler(int irq, void *device)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun unsigned int add_h, add_l;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun if (irq == g_irq[DIR_CORR]) {
150*4882a593Smuzhiyun add_h = readl(l2_base + SIFIVE_L2_DIRECCFIX_HIGH);
151*4882a593Smuzhiyun add_l = readl(l2_base + SIFIVE_L2_DIRECCFIX_LOW);
152*4882a593Smuzhiyun pr_err("L2CACHE: DirError @ 0x%08X.%08X\n", add_h, add_l);
153*4882a593Smuzhiyun /* Reading this register clears the DirError interrupt sig */
154*4882a593Smuzhiyun readl(l2_base + SIFIVE_L2_DIRECCFIX_COUNT);
155*4882a593Smuzhiyun atomic_notifier_call_chain(&l2_err_chain, SIFIVE_L2_ERR_TYPE_CE,
156*4882a593Smuzhiyun "DirECCFix");
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun if (irq == g_irq[DATA_CORR]) {
159*4882a593Smuzhiyun add_h = readl(l2_base + SIFIVE_L2_DATECCFIX_HIGH);
160*4882a593Smuzhiyun add_l = readl(l2_base + SIFIVE_L2_DATECCFIX_LOW);
161*4882a593Smuzhiyun pr_err("L2CACHE: DataError @ 0x%08X.%08X\n", add_h, add_l);
162*4882a593Smuzhiyun /* Reading this register clears the DataError interrupt sig */
163*4882a593Smuzhiyun readl(l2_base + SIFIVE_L2_DATECCFIX_COUNT);
164*4882a593Smuzhiyun atomic_notifier_call_chain(&l2_err_chain, SIFIVE_L2_ERR_TYPE_CE,
165*4882a593Smuzhiyun "DatECCFix");
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun if (irq == g_irq[DATA_UNCORR]) {
168*4882a593Smuzhiyun add_h = readl(l2_base + SIFIVE_L2_DATECCFAIL_HIGH);
169*4882a593Smuzhiyun add_l = readl(l2_base + SIFIVE_L2_DATECCFAIL_LOW);
170*4882a593Smuzhiyun pr_err("L2CACHE: DataFail @ 0x%08X.%08X\n", add_h, add_l);
171*4882a593Smuzhiyun /* Reading this register clears the DataFail interrupt sig */
172*4882a593Smuzhiyun readl(l2_base + SIFIVE_L2_DATECCFAIL_COUNT);
173*4882a593Smuzhiyun atomic_notifier_call_chain(&l2_err_chain, SIFIVE_L2_ERR_TYPE_UE,
174*4882a593Smuzhiyun "DatECCFail");
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun return IRQ_HANDLED;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
sifive_l2_init(void)180*4882a593Smuzhiyun static int __init sifive_l2_init(void)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun struct device_node *np;
183*4882a593Smuzhiyun struct resource res;
184*4882a593Smuzhiyun int i, rc;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun np = of_find_matching_node(NULL, sifive_l2_ids);
187*4882a593Smuzhiyun if (!np)
188*4882a593Smuzhiyun return -ENODEV;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun if (of_address_to_resource(np, 0, &res))
191*4882a593Smuzhiyun return -ENODEV;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun l2_base = ioremap(res.start, resource_size(&res));
194*4882a593Smuzhiyun if (!l2_base)
195*4882a593Smuzhiyun return -ENOMEM;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun for (i = 0; i < SIFIVE_L2_MAX_ECCINTR; i++) {
198*4882a593Smuzhiyun g_irq[i] = irq_of_parse_and_map(np, i);
199*4882a593Smuzhiyun rc = request_irq(g_irq[i], l2_int_handler, 0, "l2_ecc", NULL);
200*4882a593Smuzhiyun if (rc) {
201*4882a593Smuzhiyun pr_err("L2CACHE: Could not request IRQ %d\n", g_irq[i]);
202*4882a593Smuzhiyun return rc;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun l2_config_read();
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun l2_cache_ops.get_priv_group = l2_get_priv_group;
209*4882a593Smuzhiyun riscv_set_cacheinfo_ops(&l2_cache_ops);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
212*4882a593Smuzhiyun setup_sifive_debug();
213*4882a593Smuzhiyun #endif
214*4882a593Smuzhiyun return 0;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun device_initcall(sifive_l2_init);
217