18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * SiFive L2 cache controller Driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2018-2019 SiFive, Inc. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci#include <linux/debugfs.h> 98c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 108c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 118c2ecf20Sopenharmony_ci#include <linux/of_address.h> 128c2ecf20Sopenharmony_ci#include <linux/device.h> 138c2ecf20Sopenharmony_ci#include <asm/cacheinfo.h> 148c2ecf20Sopenharmony_ci#include <soc/sifive/sifive_l2_cache.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#define SIFIVE_L2_DIRECCFIX_LOW 0x100 178c2ecf20Sopenharmony_ci#define SIFIVE_L2_DIRECCFIX_HIGH 0x104 188c2ecf20Sopenharmony_ci#define SIFIVE_L2_DIRECCFIX_COUNT 0x108 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define SIFIVE_L2_DATECCFIX_LOW 0x140 218c2ecf20Sopenharmony_ci#define SIFIVE_L2_DATECCFIX_HIGH 0x144 228c2ecf20Sopenharmony_ci#define SIFIVE_L2_DATECCFIX_COUNT 0x148 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define SIFIVE_L2_DATECCFAIL_LOW 0x160 258c2ecf20Sopenharmony_ci#define SIFIVE_L2_DATECCFAIL_HIGH 0x164 268c2ecf20Sopenharmony_ci#define SIFIVE_L2_DATECCFAIL_COUNT 0x168 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define SIFIVE_L2_CONFIG 0x00 298c2ecf20Sopenharmony_ci#define SIFIVE_L2_WAYENABLE 0x08 308c2ecf20Sopenharmony_ci#define SIFIVE_L2_ECCINJECTERR 0x40 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define SIFIVE_L2_MAX_ECCINTR 3 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic void __iomem *l2_base; 358c2ecf20Sopenharmony_cistatic int g_irq[SIFIVE_L2_MAX_ECCINTR]; 368c2ecf20Sopenharmony_cistatic struct riscv_cacheinfo_ops l2_cache_ops; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cienum { 398c2ecf20Sopenharmony_ci DIR_CORR = 0, 408c2ecf20Sopenharmony_ci DATA_CORR, 418c2ecf20Sopenharmony_ci DATA_UNCORR, 428c2ecf20Sopenharmony_ci}; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_FS 458c2ecf20Sopenharmony_cistatic struct dentry *sifive_test; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_cistatic ssize_t l2_write(struct file *file, const char __user *data, 488c2ecf20Sopenharmony_ci size_t count, loff_t *ppos) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci unsigned int val; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci if (kstrtouint_from_user(data, count, 0, &val)) 538c2ecf20Sopenharmony_ci return -EINVAL; 548c2ecf20Sopenharmony_ci if ((val < 0xFF) || (val >= 0x10000 && val < 0x100FF)) 558c2ecf20Sopenharmony_ci writel(val, l2_base + SIFIVE_L2_ECCINJECTERR); 568c2ecf20Sopenharmony_ci else 578c2ecf20Sopenharmony_ci return -EINVAL; 588c2ecf20Sopenharmony_ci return count; 598c2ecf20Sopenharmony_ci} 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cistatic const struct file_operations l2_fops = { 628c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 638c2ecf20Sopenharmony_ci .open = simple_open, 648c2ecf20Sopenharmony_ci .write = l2_write 658c2ecf20Sopenharmony_ci}; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistatic void setup_sifive_debug(void) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci sifive_test = debugfs_create_dir("sifive_l2_cache", NULL); 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci debugfs_create_file("sifive_debug_inject_error", 0200, 728c2ecf20Sopenharmony_ci sifive_test, NULL, &l2_fops); 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci#endif 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_cistatic void l2_config_read(void) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci u32 regval, val; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci regval = readl(l2_base + SIFIVE_L2_CONFIG); 818c2ecf20Sopenharmony_ci val = regval & 0xFF; 828c2ecf20Sopenharmony_ci pr_info("L2CACHE: No. of Banks in the cache: %d\n", val); 838c2ecf20Sopenharmony_ci val = (regval & 0xFF00) >> 8; 848c2ecf20Sopenharmony_ci pr_info("L2CACHE: No. of ways per bank: %d\n", val); 858c2ecf20Sopenharmony_ci val = (regval & 0xFF0000) >> 16; 868c2ecf20Sopenharmony_ci pr_info("L2CACHE: Sets per bank: %llu\n", (uint64_t)1 << val); 878c2ecf20Sopenharmony_ci val = (regval & 0xFF000000) >> 24; 888c2ecf20Sopenharmony_ci pr_info("L2CACHE: Bytes per cache block: %llu\n", (uint64_t)1 << val); 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci regval = readl(l2_base + SIFIVE_L2_WAYENABLE); 918c2ecf20Sopenharmony_ci pr_info("L2CACHE: Index of the largest way enabled: %d\n", regval); 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cistatic const struct of_device_id sifive_l2_ids[] = { 958c2ecf20Sopenharmony_ci { .compatible = "sifive,fu540-c000-ccache" }, 968c2ecf20Sopenharmony_ci { /* end of table */ }, 978c2ecf20Sopenharmony_ci}; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistatic ATOMIC_NOTIFIER_HEAD(l2_err_chain); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ciint register_sifive_l2_error_notifier(struct notifier_block *nb) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci return atomic_notifier_chain_register(&l2_err_chain, nb); 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(register_sifive_l2_error_notifier); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ciint unregister_sifive_l2_error_notifier(struct notifier_block *nb) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci return atomic_notifier_chain_unregister(&l2_err_chain, nb); 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(unregister_sifive_l2_error_notifier); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cistatic int l2_largest_wayenabled(void) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci return readl(l2_base + SIFIVE_L2_WAYENABLE) & 0xFF; 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic ssize_t number_of_ways_enabled_show(struct device *dev, 1198c2ecf20Sopenharmony_ci struct device_attribute *attr, 1208c2ecf20Sopenharmony_ci char *buf) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", l2_largest_wayenabled()); 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(number_of_ways_enabled); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_cistatic struct attribute *priv_attrs[] = { 1288c2ecf20Sopenharmony_ci &dev_attr_number_of_ways_enabled.attr, 1298c2ecf20Sopenharmony_ci NULL, 1308c2ecf20Sopenharmony_ci}; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic const struct attribute_group priv_attr_group = { 1338c2ecf20Sopenharmony_ci .attrs = priv_attrs, 1348c2ecf20Sopenharmony_ci}; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic const struct attribute_group *l2_get_priv_group(struct cacheinfo *this_leaf) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci /* We want to use private group for L2 cache only */ 1398c2ecf20Sopenharmony_ci if (this_leaf->level == 2) 1408c2ecf20Sopenharmony_ci return &priv_attr_group; 1418c2ecf20Sopenharmony_ci else 1428c2ecf20Sopenharmony_ci return NULL; 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic irqreturn_t l2_int_handler(int irq, void *device) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci unsigned int add_h, add_l; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci if (irq == g_irq[DIR_CORR]) { 1508c2ecf20Sopenharmony_ci add_h = readl(l2_base + SIFIVE_L2_DIRECCFIX_HIGH); 1518c2ecf20Sopenharmony_ci add_l = readl(l2_base + SIFIVE_L2_DIRECCFIX_LOW); 1528c2ecf20Sopenharmony_ci pr_err("L2CACHE: DirError @ 0x%08X.%08X\n", add_h, add_l); 1538c2ecf20Sopenharmony_ci /* Reading this register clears the DirError interrupt sig */ 1548c2ecf20Sopenharmony_ci readl(l2_base + SIFIVE_L2_DIRECCFIX_COUNT); 1558c2ecf20Sopenharmony_ci atomic_notifier_call_chain(&l2_err_chain, SIFIVE_L2_ERR_TYPE_CE, 1568c2ecf20Sopenharmony_ci "DirECCFix"); 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci if (irq == g_irq[DATA_CORR]) { 1598c2ecf20Sopenharmony_ci add_h = readl(l2_base + SIFIVE_L2_DATECCFIX_HIGH); 1608c2ecf20Sopenharmony_ci add_l = readl(l2_base + SIFIVE_L2_DATECCFIX_LOW); 1618c2ecf20Sopenharmony_ci pr_err("L2CACHE: DataError @ 0x%08X.%08X\n", add_h, add_l); 1628c2ecf20Sopenharmony_ci /* Reading this register clears the DataError interrupt sig */ 1638c2ecf20Sopenharmony_ci readl(l2_base + SIFIVE_L2_DATECCFIX_COUNT); 1648c2ecf20Sopenharmony_ci atomic_notifier_call_chain(&l2_err_chain, SIFIVE_L2_ERR_TYPE_CE, 1658c2ecf20Sopenharmony_ci "DatECCFix"); 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci if (irq == g_irq[DATA_UNCORR]) { 1688c2ecf20Sopenharmony_ci add_h = readl(l2_base + SIFIVE_L2_DATECCFAIL_HIGH); 1698c2ecf20Sopenharmony_ci add_l = readl(l2_base + SIFIVE_L2_DATECCFAIL_LOW); 1708c2ecf20Sopenharmony_ci pr_err("L2CACHE: DataFail @ 0x%08X.%08X\n", add_h, add_l); 1718c2ecf20Sopenharmony_ci /* Reading this register clears the DataFail interrupt sig */ 1728c2ecf20Sopenharmony_ci readl(l2_base + SIFIVE_L2_DATECCFAIL_COUNT); 1738c2ecf20Sopenharmony_ci atomic_notifier_call_chain(&l2_err_chain, SIFIVE_L2_ERR_TYPE_UE, 1748c2ecf20Sopenharmony_ci "DatECCFail"); 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci return IRQ_HANDLED; 1788c2ecf20Sopenharmony_ci} 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_cistatic int __init sifive_l2_init(void) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci struct device_node *np; 1838c2ecf20Sopenharmony_ci struct resource res; 1848c2ecf20Sopenharmony_ci int i, rc; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci np = of_find_matching_node(NULL, sifive_l2_ids); 1878c2ecf20Sopenharmony_ci if (!np) 1888c2ecf20Sopenharmony_ci return -ENODEV; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci if (of_address_to_resource(np, 0, &res)) 1918c2ecf20Sopenharmony_ci return -ENODEV; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci l2_base = ioremap(res.start, resource_size(&res)); 1948c2ecf20Sopenharmony_ci if (!l2_base) 1958c2ecf20Sopenharmony_ci return -ENOMEM; 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci for (i = 0; i < SIFIVE_L2_MAX_ECCINTR; i++) { 1988c2ecf20Sopenharmony_ci g_irq[i] = irq_of_parse_and_map(np, i); 1998c2ecf20Sopenharmony_ci rc = request_irq(g_irq[i], l2_int_handler, 0, "l2_ecc", NULL); 2008c2ecf20Sopenharmony_ci if (rc) { 2018c2ecf20Sopenharmony_ci pr_err("L2CACHE: Could not request IRQ %d\n", g_irq[i]); 2028c2ecf20Sopenharmony_ci return rc; 2038c2ecf20Sopenharmony_ci } 2048c2ecf20Sopenharmony_ci } 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci l2_config_read(); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci l2_cache_ops.get_priv_group = l2_get_priv_group; 2098c2ecf20Sopenharmony_ci riscv_set_cacheinfo_ops(&l2_cache_ops); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_FS 2128c2ecf20Sopenharmony_ci setup_sifive_debug(); 2138c2ecf20Sopenharmony_ci#endif 2148c2ecf20Sopenharmony_ci return 0; 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_cidevice_initcall(sifive_l2_init); 217