sifive_ccache.c 7.2 KB

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