blob: b5158572f6abf77c372eeb6632dbecbc11b13954 [file] [log] [blame]
Loc Ho0d442932015-05-22 17:32:59 -06001/*
2 * APM X-Gene SoC EDAC (error detection and correction)
3 *
4 * Copyright (c) 2015, Applied Micro Circuits Corporation
5 * Author: Feng Kan <fkan@apm.com>
6 * Loc Ho <lho@apm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <linux/ctype.h>
23#include <linux/edac.h>
24#include <linux/interrupt.h>
25#include <linux/mfd/syscon.h>
26#include <linux/module.h>
27#include <linux/of.h>
28#include <linux/of_address.h>
29#include <linux/regmap.h>
30
31#include "edac_core.h"
32
33#define EDAC_MOD_STR "xgene_edac"
34
35/* Global error configuration status registers (CSR) */
36#define PCPHPERRINTSTS 0x0000
37#define PCPHPERRINTMSK 0x0004
38#define MCU_CTL_ERR_MASK BIT(12)
39#define IOB_PA_ERR_MASK BIT(11)
40#define IOB_BA_ERR_MASK BIT(10)
41#define IOB_XGIC_ERR_MASK BIT(9)
42#define IOB_RB_ERR_MASK BIT(8)
43#define L3C_UNCORR_ERR_MASK BIT(5)
44#define MCU_UNCORR_ERR_MASK BIT(4)
45#define PMD3_MERR_MASK BIT(3)
46#define PMD2_MERR_MASK BIT(2)
47#define PMD1_MERR_MASK BIT(1)
48#define PMD0_MERR_MASK BIT(0)
49#define PCPLPERRINTSTS 0x0008
50#define PCPLPERRINTMSK 0x000C
51#define CSW_SWITCH_TRACE_ERR_MASK BIT(2)
52#define L3C_CORR_ERR_MASK BIT(1)
53#define MCU_CORR_ERR_MASK BIT(0)
54#define MEMERRINTSTS 0x0010
55#define MEMERRINTMSK 0x0014
56
57struct xgene_edac {
58 struct device *dev;
59 struct regmap *csw_map;
60 struct regmap *mcba_map;
61 struct regmap *mcbb_map;
62 struct regmap *efuse_map;
63 void __iomem *pcp_csr;
64 spinlock_t lock;
65 struct dentry *dfs;
66
67 struct list_head mcus;
68 struct list_head pmds;
69
70 struct mutex mc_lock;
71 int mc_active_mask;
72 int mc_registered_mask;
73};
74
75static void xgene_edac_pcp_rd(struct xgene_edac *edac, u32 reg, u32 *val)
76{
77 *val = readl(edac->pcp_csr + reg);
78}
79
80static void xgene_edac_pcp_clrbits(struct xgene_edac *edac, u32 reg,
81 u32 bits_mask)
82{
83 u32 val;
84
85 spin_lock(&edac->lock);
86 val = readl(edac->pcp_csr + reg);
87 val &= ~bits_mask;
88 writel(val, edac->pcp_csr + reg);
89 spin_unlock(&edac->lock);
90}
91
92static void xgene_edac_pcp_setbits(struct xgene_edac *edac, u32 reg,
93 u32 bits_mask)
94{
95 u32 val;
96
97 spin_lock(&edac->lock);
98 val = readl(edac->pcp_csr + reg);
99 val |= bits_mask;
100 writel(val, edac->pcp_csr + reg);
101 spin_unlock(&edac->lock);
102}
103
104/* Memory controller error CSR */
105#define MCU_MAX_RANK 8
106#define MCU_RANK_STRIDE 0x40
107
108#define MCUGECR 0x0110
109#define MCU_GECR_DEMANDUCINTREN_MASK BIT(0)
110#define MCU_GECR_BACKUCINTREN_MASK BIT(1)
111#define MCU_GECR_CINTREN_MASK BIT(2)
112#define MUC_GECR_MCUADDRERREN_MASK BIT(9)
113#define MCUGESR 0x0114
114#define MCU_GESR_ADDRNOMATCH_ERR_MASK BIT(7)
115#define MCU_GESR_ADDRMULTIMATCH_ERR_MASK BIT(6)
116#define MCU_GESR_PHYP_ERR_MASK BIT(3)
117#define MCUESRR0 0x0314
118#define MCU_ESRR_MULTUCERR_MASK BIT(3)
119#define MCU_ESRR_BACKUCERR_MASK BIT(2)
120#define MCU_ESRR_DEMANDUCERR_MASK BIT(1)
121#define MCU_ESRR_CERR_MASK BIT(0)
122#define MCUESRRA0 0x0318
123#define MCUEBLRR0 0x031c
124#define MCU_EBLRR_ERRBANK_RD(src) (((src) & 0x00000007) >> 0)
125#define MCUERCRR0 0x0320
126#define MCU_ERCRR_ERRROW_RD(src) (((src) & 0xFFFF0000) >> 16)
127#define MCU_ERCRR_ERRCOL_RD(src) ((src) & 0x00000FFF)
128#define MCUSBECNT0 0x0324
129#define MCU_SBECNT_COUNT(src) ((src) & 0xFFFF)
130
131#define CSW_CSWCR 0x0000
132#define CSW_CSWCR_DUALMCB_MASK BIT(0)
133
134#define MCBADDRMR 0x0000
135#define MCBADDRMR_MCU_INTLV_MODE_MASK BIT(3)
136#define MCBADDRMR_DUALMCU_MODE_MASK BIT(2)
137#define MCBADDRMR_MCB_INTLV_MODE_MASK BIT(1)
138#define MCBADDRMR_ADDRESS_MODE_MASK BIT(0)
139
140struct xgene_edac_mc_ctx {
141 struct list_head next;
142 char *name;
143 struct mem_ctl_info *mci;
144 struct xgene_edac *edac;
145 void __iomem *mcu_csr;
146 u32 mcu_id;
147};
148
149static ssize_t xgene_edac_mc_err_inject_write(struct file *file,
150 const char __user *data,
151 size_t count, loff_t *ppos)
152{
153 struct mem_ctl_info *mci = file->private_data;
154 struct xgene_edac_mc_ctx *ctx = mci->pvt_info;
155 int i;
156
157 for (i = 0; i < MCU_MAX_RANK; i++) {
158 writel(MCU_ESRR_MULTUCERR_MASK | MCU_ESRR_BACKUCERR_MASK |
159 MCU_ESRR_DEMANDUCERR_MASK | MCU_ESRR_CERR_MASK,
160 ctx->mcu_csr + MCUESRRA0 + i * MCU_RANK_STRIDE);
161 }
162 return count;
163}
164
165static const struct file_operations xgene_edac_mc_debug_inject_fops = {
166 .open = simple_open,
167 .write = xgene_edac_mc_err_inject_write,
168 .llseek = generic_file_llseek,
169};
170
171static void xgene_edac_mc_create_debugfs_node(struct mem_ctl_info *mci)
172{
173 if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
174 return;
175#ifdef CONFIG_EDAC_DEBUG
176 if (!mci->debugfs)
177 return;
178 debugfs_create_file("inject_ctrl", S_IWUSR, mci->debugfs, mci,
179 &xgene_edac_mc_debug_inject_fops);
180#endif
181}
182
183static void xgene_edac_mc_check(struct mem_ctl_info *mci)
184{
185 struct xgene_edac_mc_ctx *ctx = mci->pvt_info;
186 unsigned int pcp_hp_stat;
187 unsigned int pcp_lp_stat;
188 u32 reg;
189 u32 rank;
190 u32 bank;
191 u32 count;
192 u32 col_row;
193
194 xgene_edac_pcp_rd(ctx->edac, PCPHPERRINTSTS, &pcp_hp_stat);
195 xgene_edac_pcp_rd(ctx->edac, PCPLPERRINTSTS, &pcp_lp_stat);
196 if (!((MCU_UNCORR_ERR_MASK & pcp_hp_stat) ||
197 (MCU_CTL_ERR_MASK & pcp_hp_stat) ||
198 (MCU_CORR_ERR_MASK & pcp_lp_stat)))
199 return;
200
201 for (rank = 0; rank < MCU_MAX_RANK; rank++) {
202 reg = readl(ctx->mcu_csr + MCUESRR0 + rank * MCU_RANK_STRIDE);
203
204 /* Detect uncorrectable memory error */
205 if (reg & (MCU_ESRR_DEMANDUCERR_MASK |
206 MCU_ESRR_BACKUCERR_MASK)) {
207 /* Detected uncorrectable memory error */
208 edac_mc_chipset_printk(mci, KERN_ERR, "X-Gene",
209 "MCU uncorrectable error at rank %d\n", rank);
210
211 edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci,
212 1, 0, 0, 0, 0, 0, -1, mci->ctl_name, "");
213 }
214
215 /* Detect correctable memory error */
216 if (reg & MCU_ESRR_CERR_MASK) {
217 bank = readl(ctx->mcu_csr + MCUEBLRR0 +
218 rank * MCU_RANK_STRIDE);
219 col_row = readl(ctx->mcu_csr + MCUERCRR0 +
220 rank * MCU_RANK_STRIDE);
221 count = readl(ctx->mcu_csr + MCUSBECNT0 +
222 rank * MCU_RANK_STRIDE);
223 edac_mc_chipset_printk(mci, KERN_WARNING, "X-Gene",
224 "MCU correctable error at rank %d bank %d column %d row %d count %d\n",
225 rank, MCU_EBLRR_ERRBANK_RD(bank),
226 MCU_ERCRR_ERRCOL_RD(col_row),
227 MCU_ERCRR_ERRROW_RD(col_row),
228 MCU_SBECNT_COUNT(count));
229
230 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci,
231 1, 0, 0, 0, 0, 0, -1, mci->ctl_name, "");
232 }
233
234 /* Clear all error registers */
235 writel(0x0, ctx->mcu_csr + MCUEBLRR0 + rank * MCU_RANK_STRIDE);
236 writel(0x0, ctx->mcu_csr + MCUERCRR0 + rank * MCU_RANK_STRIDE);
237 writel(0x0, ctx->mcu_csr + MCUSBECNT0 +
238 rank * MCU_RANK_STRIDE);
239 writel(reg, ctx->mcu_csr + MCUESRR0 + rank * MCU_RANK_STRIDE);
240 }
241
242 /* Detect memory controller error */
243 reg = readl(ctx->mcu_csr + MCUGESR);
244 if (reg) {
245 if (reg & MCU_GESR_ADDRNOMATCH_ERR_MASK)
246 edac_mc_chipset_printk(mci, KERN_WARNING, "X-Gene",
247 "MCU address miss-match error\n");
248 if (reg & MCU_GESR_ADDRMULTIMATCH_ERR_MASK)
249 edac_mc_chipset_printk(mci, KERN_WARNING, "X-Gene",
250 "MCU address multi-match error\n");
251
252 writel(reg, ctx->mcu_csr + MCUGESR);
253 }
254}
255
256static void xgene_edac_mc_irq_ctl(struct mem_ctl_info *mci, bool enable)
257{
258 struct xgene_edac_mc_ctx *ctx = mci->pvt_info;
259 unsigned int val;
260
261 if (edac_op_state != EDAC_OPSTATE_INT)
262 return;
263
264 mutex_lock(&ctx->edac->mc_lock);
265
266 /*
267 * As there is only single bit for enable error and interrupt mask,
268 * we must only enable top level interrupt after all MCUs are
269 * registered. Otherwise, if there is an error and the corresponding
270 * MCU has not registered, the interrupt will never get cleared. To
271 * determine all MCU have registered, we will keep track of active
272 * MCUs and registered MCUs.
273 */
274 if (enable) {
275 /* Set registered MCU bit */
276 ctx->edac->mc_registered_mask |= 1 << ctx->mcu_id;
277
278 /* Enable interrupt after all active MCU registered */
279 if (ctx->edac->mc_registered_mask ==
280 ctx->edac->mc_active_mask) {
281 /* Enable memory controller top level interrupt */
282 xgene_edac_pcp_clrbits(ctx->edac, PCPHPERRINTMSK,
283 MCU_UNCORR_ERR_MASK |
284 MCU_CTL_ERR_MASK);
285 xgene_edac_pcp_clrbits(ctx->edac, PCPLPERRINTMSK,
286 MCU_CORR_ERR_MASK);
287 }
288
289 /* Enable MCU interrupt and error reporting */
290 val = readl(ctx->mcu_csr + MCUGECR);
291 val |= MCU_GECR_DEMANDUCINTREN_MASK |
292 MCU_GECR_BACKUCINTREN_MASK |
293 MCU_GECR_CINTREN_MASK |
294 MUC_GECR_MCUADDRERREN_MASK;
295 writel(val, ctx->mcu_csr + MCUGECR);
296 } else {
297 /* Disable MCU interrupt */
298 val = readl(ctx->mcu_csr + MCUGECR);
299 val &= ~(MCU_GECR_DEMANDUCINTREN_MASK |
300 MCU_GECR_BACKUCINTREN_MASK |
301 MCU_GECR_CINTREN_MASK |
302 MUC_GECR_MCUADDRERREN_MASK);
303 writel(val, ctx->mcu_csr + MCUGECR);
304
305 /* Disable memory controller top level interrupt */
306 xgene_edac_pcp_setbits(ctx->edac, PCPHPERRINTMSK,
307 MCU_UNCORR_ERR_MASK | MCU_CTL_ERR_MASK);
308 xgene_edac_pcp_setbits(ctx->edac, PCPLPERRINTMSK,
309 MCU_CORR_ERR_MASK);
310
311 /* Clear registered MCU bit */
312 ctx->edac->mc_registered_mask &= ~(1 << ctx->mcu_id);
313 }
314
315 mutex_unlock(&ctx->edac->mc_lock);
316}
317
318static int xgene_edac_mc_is_active(struct xgene_edac_mc_ctx *ctx, int mc_idx)
319{
320 unsigned int reg;
321 u32 mcu_mask;
322
323 if (regmap_read(ctx->edac->csw_map, CSW_CSWCR, &reg))
324 return 0;
325
326 if (reg & CSW_CSWCR_DUALMCB_MASK) {
327 /*
328 * Dual MCB active - Determine if all 4 active or just MCU0
329 * and MCU2 active
330 */
331 if (regmap_read(ctx->edac->mcbb_map, MCBADDRMR, &reg))
332 return 0;
333 mcu_mask = (reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0xF : 0x5;
334 } else {
335 /*
336 * Single MCB active - Determine if MCU0/MCU1 or just MCU0
337 * active
338 */
339 if (regmap_read(ctx->edac->mcba_map, MCBADDRMR, &reg))
340 return 0;
341 mcu_mask = (reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0x3 : 0x1;
342 }
343
344 /* Save active MC mask if hasn't set already */
345 if (!ctx->edac->mc_active_mask)
346 ctx->edac->mc_active_mask = mcu_mask;
347
348 return (mcu_mask & (1 << mc_idx)) ? 1 : 0;
349}
350
351static int xgene_edac_mc_add(struct xgene_edac *edac, struct device_node *np)
352{
353 struct mem_ctl_info *mci;
354 struct edac_mc_layer layers[2];
355 struct xgene_edac_mc_ctx tmp_ctx;
356 struct xgene_edac_mc_ctx *ctx;
357 struct resource res;
358 int rc;
359
360 memset(&tmp_ctx, 0, sizeof(tmp_ctx));
361 tmp_ctx.edac = edac;
362
363 if (!devres_open_group(edac->dev, xgene_edac_mc_add, GFP_KERNEL))
364 return -ENOMEM;
365
366 rc = of_address_to_resource(np, 0, &res);
367 if (rc < 0) {
368 dev_err(edac->dev, "no MCU resource address\n");
369 goto err_group;
370 }
371 tmp_ctx.mcu_csr = devm_ioremap_resource(edac->dev, &res);
372 if (IS_ERR(tmp_ctx.mcu_csr)) {
373 dev_err(edac->dev, "unable to map MCU resource\n");
374 rc = PTR_ERR(tmp_ctx.mcu_csr);
375 goto err_group;
376 }
377
378 /* Ignore non-active MCU */
379 if (of_property_read_u32(np, "memory-controller", &tmp_ctx.mcu_id)) {
380 dev_err(edac->dev, "no memory-controller property\n");
381 rc = -ENODEV;
382 goto err_group;
383 }
384 if (!xgene_edac_mc_is_active(&tmp_ctx, tmp_ctx.mcu_id)) {
385 rc = -ENODEV;
386 goto err_group;
387 }
388
389 layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
390 layers[0].size = 4;
391 layers[0].is_virt_csrow = true;
392 layers[1].type = EDAC_MC_LAYER_CHANNEL;
393 layers[1].size = 2;
394 layers[1].is_virt_csrow = false;
395 mci = edac_mc_alloc(tmp_ctx.mcu_id, ARRAY_SIZE(layers), layers,
396 sizeof(*ctx));
397 if (!mci) {
398 rc = -ENOMEM;
399 goto err_group;
400 }
401
402 ctx = mci->pvt_info;
403 *ctx = tmp_ctx; /* Copy over resource value */
404 ctx->name = "xgene_edac_mc_err";
405 ctx->mci = mci;
406 mci->pdev = &mci->dev;
407 mci->ctl_name = ctx->name;
408 mci->dev_name = ctx->name;
409
410 mci->mtype_cap = MEM_FLAG_RDDR | MEM_FLAG_RDDR2 | MEM_FLAG_RDDR3 |
411 MEM_FLAG_DDR | MEM_FLAG_DDR2 | MEM_FLAG_DDR3;
412 mci->edac_ctl_cap = EDAC_FLAG_SECDED;
413 mci->edac_cap = EDAC_FLAG_SECDED;
414 mci->mod_name = EDAC_MOD_STR;
415 mci->mod_ver = "0.1";
416 mci->ctl_page_to_phys = NULL;
417 mci->scrub_cap = SCRUB_FLAG_HW_SRC;
418 mci->scrub_mode = SCRUB_HW_SRC;
419
420 if (edac_op_state == EDAC_OPSTATE_POLL)
421 mci->edac_check = xgene_edac_mc_check;
422
423 if (edac_mc_add_mc(mci)) {
424 dev_err(edac->dev, "edac_mc_add_mc failed\n");
425 rc = -EINVAL;
426 goto err_free;
427 }
428
429 xgene_edac_mc_create_debugfs_node(mci);
430
431 list_add(&ctx->next, &edac->mcus);
432
433 xgene_edac_mc_irq_ctl(mci, true);
434
435 devres_remove_group(edac->dev, xgene_edac_mc_add);
436
437 dev_info(edac->dev, "X-Gene EDAC MC registered\n");
438 return 0;
439
440err_free:
441 edac_mc_free(mci);
442err_group:
443 devres_release_group(edac->dev, xgene_edac_mc_add);
444 return rc;
445}
446
447static int xgene_edac_mc_remove(struct xgene_edac_mc_ctx *mcu)
448{
449 xgene_edac_mc_irq_ctl(mcu->mci, false);
450 edac_mc_del_mc(&mcu->mci->dev);
451 edac_mc_free(mcu->mci);
452 return 0;
453}
454
455/* CPU L1/L2 error CSR */
456#define MAX_CPU_PER_PMD 2
457#define CPU_CSR_STRIDE 0x00100000
458#define CPU_L2C_PAGE 0x000D0000
459#define CPU_MEMERR_L2C_PAGE 0x000E0000
460#define CPU_MEMERR_CPU_PAGE 0x000F0000
461
462#define MEMERR_CPU_ICFECR_PAGE_OFFSET 0x0000
463#define MEMERR_CPU_ICFESR_PAGE_OFFSET 0x0004
464#define MEMERR_CPU_ICFESR_ERRWAY_RD(src) (((src) & 0xFF000000) >> 24)
465#define MEMERR_CPU_ICFESR_ERRINDEX_RD(src) (((src) & 0x003F0000) >> 16)
466#define MEMERR_CPU_ICFESR_ERRINFO_RD(src) (((src) & 0x0000FF00) >> 8)
467#define MEMERR_CPU_ICFESR_ERRTYPE_RD(src) (((src) & 0x00000070) >> 4)
468#define MEMERR_CPU_ICFESR_MULTCERR_MASK BIT(2)
469#define MEMERR_CPU_ICFESR_CERR_MASK BIT(0)
470#define MEMERR_CPU_LSUESR_PAGE_OFFSET 0x000c
471#define MEMERR_CPU_LSUESR_ERRWAY_RD(src) (((src) & 0xFF000000) >> 24)
472#define MEMERR_CPU_LSUESR_ERRINDEX_RD(src) (((src) & 0x003F0000) >> 16)
473#define MEMERR_CPU_LSUESR_ERRINFO_RD(src) (((src) & 0x0000FF00) >> 8)
474#define MEMERR_CPU_LSUESR_ERRTYPE_RD(src) (((src) & 0x00000070) >> 4)
475#define MEMERR_CPU_LSUESR_MULTCERR_MASK BIT(2)
476#define MEMERR_CPU_LSUESR_CERR_MASK BIT(0)
477#define MEMERR_CPU_LSUECR_PAGE_OFFSET 0x0008
478#define MEMERR_CPU_MMUECR_PAGE_OFFSET 0x0010
479#define MEMERR_CPU_MMUESR_PAGE_OFFSET 0x0014
480#define MEMERR_CPU_MMUESR_ERRWAY_RD(src) (((src) & 0xFF000000) >> 24)
481#define MEMERR_CPU_MMUESR_ERRINDEX_RD(src) (((src) & 0x007F0000) >> 16)
482#define MEMERR_CPU_MMUESR_ERRINFO_RD(src) (((src) & 0x0000FF00) >> 8)
483#define MEMERR_CPU_MMUESR_ERRREQSTR_LSU_MASK BIT(7)
484#define MEMERR_CPU_MMUESR_ERRTYPE_RD(src) (((src) & 0x00000070) >> 4)
485#define MEMERR_CPU_MMUESR_MULTCERR_MASK BIT(2)
486#define MEMERR_CPU_MMUESR_CERR_MASK BIT(0)
487#define MEMERR_CPU_ICFESRA_PAGE_OFFSET 0x0804
488#define MEMERR_CPU_LSUESRA_PAGE_OFFSET 0x080c
489#define MEMERR_CPU_MMUESRA_PAGE_OFFSET 0x0814
490
491#define MEMERR_L2C_L2ECR_PAGE_OFFSET 0x0000
492#define MEMERR_L2C_L2ESR_PAGE_OFFSET 0x0004
493#define MEMERR_L2C_L2ESR_ERRSYN_RD(src) (((src) & 0xFF000000) >> 24)
494#define MEMERR_L2C_L2ESR_ERRWAY_RD(src) (((src) & 0x00FC0000) >> 18)
495#define MEMERR_L2C_L2ESR_ERRCPU_RD(src) (((src) & 0x00020000) >> 17)
496#define MEMERR_L2C_L2ESR_ERRGROUP_RD(src) (((src) & 0x0000E000) >> 13)
497#define MEMERR_L2C_L2ESR_ERRACTION_RD(src) (((src) & 0x00001C00) >> 10)
498#define MEMERR_L2C_L2ESR_ERRTYPE_RD(src) (((src) & 0x00000300) >> 8)
499#define MEMERR_L2C_L2ESR_MULTUCERR_MASK BIT(3)
500#define MEMERR_L2C_L2ESR_MULTICERR_MASK BIT(2)
501#define MEMERR_L2C_L2ESR_UCERR_MASK BIT(1)
502#define MEMERR_L2C_L2ESR_ERR_MASK BIT(0)
503#define MEMERR_L2C_L2EALR_PAGE_OFFSET 0x0008
504#define CPUX_L2C_L2RTOCR_PAGE_OFFSET 0x0010
505#define MEMERR_L2C_L2EAHR_PAGE_OFFSET 0x000c
506#define CPUX_L2C_L2RTOSR_PAGE_OFFSET 0x0014
507#define MEMERR_L2C_L2RTOSR_MULTERR_MASK BIT(1)
508#define MEMERR_L2C_L2RTOSR_ERR_MASK BIT(0)
509#define CPUX_L2C_L2RTOALR_PAGE_OFFSET 0x0018
510#define CPUX_L2C_L2RTOAHR_PAGE_OFFSET 0x001c
511#define MEMERR_L2C_L2ESRA_PAGE_OFFSET 0x0804
512
513/*
514 * Processor Module Domain (PMD) context - Context for a pair of processsors.
515 * Each PMD consists of 2 CPUs and a shared L2 cache. Each CPU consists of
516 * its own L1 cache.
517 */
518struct xgene_edac_pmd_ctx {
519 struct list_head next;
520 struct device ddev;
521 char *name;
522 struct xgene_edac *edac;
523 struct edac_device_ctl_info *edac_dev;
524 void __iomem *pmd_csr;
525 u32 pmd;
526};
527
528static void xgene_edac_pmd_l1_check(struct edac_device_ctl_info *edac_dev,
529 int cpu_idx)
530{
531 struct xgene_edac_pmd_ctx *ctx = edac_dev->pvt_info;
532 void __iomem *pg_f;
533 u32 val;
534
535 pg_f = ctx->pmd_csr + cpu_idx * CPU_CSR_STRIDE + CPU_MEMERR_CPU_PAGE;
536
537 val = readl(pg_f + MEMERR_CPU_ICFESR_PAGE_OFFSET);
538 if (val) {
539 dev_err(edac_dev->dev,
540 "CPU%d L1 memory error ICF 0x%08X Way 0x%02X Index 0x%02X Info 0x%02X\n",
541 ctx->pmd * MAX_CPU_PER_PMD + cpu_idx, val,
542 MEMERR_CPU_ICFESR_ERRWAY_RD(val),
543 MEMERR_CPU_ICFESR_ERRINDEX_RD(val),
544 MEMERR_CPU_ICFESR_ERRINFO_RD(val));
545 if (val & MEMERR_CPU_ICFESR_CERR_MASK)
546 dev_err(edac_dev->dev,
547 "One or more correctable error\n");
548 if (val & MEMERR_CPU_ICFESR_MULTCERR_MASK)
549 dev_err(edac_dev->dev, "Multiple correctable error\n");
550 switch (MEMERR_CPU_ICFESR_ERRTYPE_RD(val)) {
551 case 1:
552 dev_err(edac_dev->dev, "L1 TLB multiple hit\n");
553 break;
554 case 2:
555 dev_err(edac_dev->dev, "Way select multiple hit\n");
556 break;
557 case 3:
558 dev_err(edac_dev->dev, "Physical tag parity error\n");
559 break;
560 case 4:
561 case 5:
562 dev_err(edac_dev->dev, "L1 data parity error\n");
563 break;
564 case 6:
565 dev_err(edac_dev->dev, "L1 pre-decode parity error\n");
566 break;
567 }
568
569 /* Clear any HW errors */
570 writel(val, pg_f + MEMERR_CPU_ICFESR_PAGE_OFFSET);
571
572 if (val & (MEMERR_CPU_ICFESR_CERR_MASK |
573 MEMERR_CPU_ICFESR_MULTCERR_MASK))
574 edac_device_handle_ce(edac_dev, 0, 0,
575 edac_dev->ctl_name);
576 }
577
578 val = readl(pg_f + MEMERR_CPU_LSUESR_PAGE_OFFSET);
579 if (val) {
580 dev_err(edac_dev->dev,
581 "CPU%d memory error LSU 0x%08X Way 0x%02X Index 0x%02X Info 0x%02X\n",
582 ctx->pmd * MAX_CPU_PER_PMD + cpu_idx, val,
583 MEMERR_CPU_LSUESR_ERRWAY_RD(val),
584 MEMERR_CPU_LSUESR_ERRINDEX_RD(val),
585 MEMERR_CPU_LSUESR_ERRINFO_RD(val));
586 if (val & MEMERR_CPU_LSUESR_CERR_MASK)
587 dev_err(edac_dev->dev,
588 "One or more correctable error\n");
589 if (val & MEMERR_CPU_LSUESR_MULTCERR_MASK)
590 dev_err(edac_dev->dev, "Multiple correctable error\n");
591 switch (MEMERR_CPU_LSUESR_ERRTYPE_RD(val)) {
592 case 0:
593 dev_err(edac_dev->dev, "Load tag error\n");
594 break;
595 case 1:
596 dev_err(edac_dev->dev, "Load data error\n");
597 break;
598 case 2:
599 dev_err(edac_dev->dev, "WSL multihit error\n");
600 break;
601 case 3:
602 dev_err(edac_dev->dev, "Store tag error\n");
603 break;
604 case 4:
605 dev_err(edac_dev->dev,
606 "DTB multihit from load pipeline error\n");
607 break;
608 case 5:
609 dev_err(edac_dev->dev,
610 "DTB multihit from store pipeline error\n");
611 break;
612 }
613
614 /* Clear any HW errors */
615 writel(val, pg_f + MEMERR_CPU_LSUESR_PAGE_OFFSET);
616
617 if (val & (MEMERR_CPU_LSUESR_CERR_MASK |
618 MEMERR_CPU_LSUESR_MULTCERR_MASK))
619 edac_device_handle_ce(edac_dev, 0, 0,
620 edac_dev->ctl_name);
621 }
622
623 val = readl(pg_f + MEMERR_CPU_MMUESR_PAGE_OFFSET);
624 if (val) {
625 dev_err(edac_dev->dev,
626 "CPU%d memory error MMU 0x%08X Way 0x%02X Index 0x%02X Info 0x%02X %s\n",
627 ctx->pmd * MAX_CPU_PER_PMD + cpu_idx, val,
628 MEMERR_CPU_MMUESR_ERRWAY_RD(val),
629 MEMERR_CPU_MMUESR_ERRINDEX_RD(val),
630 MEMERR_CPU_MMUESR_ERRINFO_RD(val),
631 val & MEMERR_CPU_MMUESR_ERRREQSTR_LSU_MASK ? "LSU" :
632 "ICF");
633 if (val & MEMERR_CPU_MMUESR_CERR_MASK)
634 dev_err(edac_dev->dev,
635 "One or more correctable error\n");
636 if (val & MEMERR_CPU_MMUESR_MULTCERR_MASK)
637 dev_err(edac_dev->dev, "Multiple correctable error\n");
638 switch (MEMERR_CPU_MMUESR_ERRTYPE_RD(val)) {
639 case 0:
640 dev_err(edac_dev->dev, "Stage 1 UTB hit error\n");
641 break;
642 case 1:
643 dev_err(edac_dev->dev, "Stage 1 UTB miss error\n");
644 break;
645 case 2:
646 dev_err(edac_dev->dev, "Stage 1 UTB allocate error\n");
647 break;
648 case 3:
649 dev_err(edac_dev->dev,
650 "TMO operation single bank error\n");
651 break;
652 case 4:
653 dev_err(edac_dev->dev, "Stage 2 UTB error\n");
654 break;
655 case 5:
656 dev_err(edac_dev->dev, "Stage 2 UTB miss error\n");
657 break;
658 case 6:
659 dev_err(edac_dev->dev, "Stage 2 UTB allocate error\n");
660 break;
661 case 7:
662 dev_err(edac_dev->dev,
663 "TMO operation multiple bank error\n");
664 break;
665 }
666
667 /* Clear any HW errors */
668 writel(val, pg_f + MEMERR_CPU_MMUESR_PAGE_OFFSET);
669
670 edac_device_handle_ce(edac_dev, 0, 0, edac_dev->ctl_name);
671 }
672}
673
674static void xgene_edac_pmd_l2_check(struct edac_device_ctl_info *edac_dev)
675{
676 struct xgene_edac_pmd_ctx *ctx = edac_dev->pvt_info;
677 void __iomem *pg_d;
678 void __iomem *pg_e;
679 u32 val_hi;
680 u32 val_lo;
681 u32 val;
682
683 /* Check L2 */
684 pg_e = ctx->pmd_csr + CPU_MEMERR_L2C_PAGE;
685 val = readl(pg_e + MEMERR_L2C_L2ESR_PAGE_OFFSET);
686 if (val) {
687 val_lo = readl(pg_e + MEMERR_L2C_L2EALR_PAGE_OFFSET);
688 val_hi = readl(pg_e + MEMERR_L2C_L2EAHR_PAGE_OFFSET);
689 dev_err(edac_dev->dev,
690 "PMD%d memory error L2C L2ESR 0x%08X @ 0x%08X.%08X\n",
691 ctx->pmd, val, val_hi, val_lo);
692 dev_err(edac_dev->dev,
693 "ErrSyndrome 0x%02X ErrWay 0x%02X ErrCpu %d ErrGroup 0x%02X ErrAction 0x%02X\n",
694 MEMERR_L2C_L2ESR_ERRSYN_RD(val),
695 MEMERR_L2C_L2ESR_ERRWAY_RD(val),
696 MEMERR_L2C_L2ESR_ERRCPU_RD(val),
697 MEMERR_L2C_L2ESR_ERRGROUP_RD(val),
698 MEMERR_L2C_L2ESR_ERRACTION_RD(val));
699
700 if (val & MEMERR_L2C_L2ESR_ERR_MASK)
701 dev_err(edac_dev->dev,
702 "One or more correctable error\n");
703 if (val & MEMERR_L2C_L2ESR_MULTICERR_MASK)
704 dev_err(edac_dev->dev, "Multiple correctable error\n");
705 if (val & MEMERR_L2C_L2ESR_UCERR_MASK)
706 dev_err(edac_dev->dev,
707 "One or more uncorrectable error\n");
708 if (val & MEMERR_L2C_L2ESR_MULTUCERR_MASK)
709 dev_err(edac_dev->dev,
710 "Multiple uncorrectable error\n");
711
712 switch (MEMERR_L2C_L2ESR_ERRTYPE_RD(val)) {
713 case 0:
714 dev_err(edac_dev->dev, "Outbound SDB parity error\n");
715 break;
716 case 1:
717 dev_err(edac_dev->dev, "Inbound SDB parity error\n");
718 break;
719 case 2:
720 dev_err(edac_dev->dev, "Tag ECC error\n");
721 break;
722 case 3:
723 dev_err(edac_dev->dev, "Data ECC error\n");
724 break;
725 }
726
727 /* Clear any HW errors */
728 writel(val, pg_e + MEMERR_L2C_L2ESR_PAGE_OFFSET);
729
730 if (val & (MEMERR_L2C_L2ESR_ERR_MASK |
731 MEMERR_L2C_L2ESR_MULTICERR_MASK))
732 edac_device_handle_ce(edac_dev, 0, 0,
733 edac_dev->ctl_name);
734 if (val & (MEMERR_L2C_L2ESR_UCERR_MASK |
735 MEMERR_L2C_L2ESR_MULTUCERR_MASK))
736 edac_device_handle_ue(edac_dev, 0, 0,
737 edac_dev->ctl_name);
738 }
739
740 /* Check if any memory request timed out on L2 cache */
741 pg_d = ctx->pmd_csr + CPU_L2C_PAGE;
742 val = readl(pg_d + CPUX_L2C_L2RTOSR_PAGE_OFFSET);
743 if (val) {
744 val_lo = readl(pg_d + CPUX_L2C_L2RTOALR_PAGE_OFFSET);
745 val_hi = readl(pg_d + CPUX_L2C_L2RTOAHR_PAGE_OFFSET);
746 dev_err(edac_dev->dev,
747 "PMD%d L2C error L2C RTOSR 0x%08X @ 0x%08X.%08X\n",
748 ctx->pmd, val, val_hi, val_lo);
749 writel(val, pg_d + CPUX_L2C_L2RTOSR_PAGE_OFFSET);
750 }
751}
752
753static void xgene_edac_pmd_check(struct edac_device_ctl_info *edac_dev)
754{
755 struct xgene_edac_pmd_ctx *ctx = edac_dev->pvt_info;
756 unsigned int pcp_hp_stat;
757 int i;
758
759 xgene_edac_pcp_rd(ctx->edac, PCPHPERRINTSTS, &pcp_hp_stat);
760 if (!((PMD0_MERR_MASK << ctx->pmd) & pcp_hp_stat))
761 return;
762
763 /* Check CPU L1 error */
764 for (i = 0; i < MAX_CPU_PER_PMD; i++)
765 xgene_edac_pmd_l1_check(edac_dev, i);
766
767 /* Check CPU L2 error */
768 xgene_edac_pmd_l2_check(edac_dev);
769}
770
771static void xgene_edac_pmd_cpu_hw_cfg(struct edac_device_ctl_info *edac_dev,
772 int cpu)
773{
774 struct xgene_edac_pmd_ctx *ctx = edac_dev->pvt_info;
775 void __iomem *pg_f = ctx->pmd_csr + cpu * CPU_CSR_STRIDE +
776 CPU_MEMERR_CPU_PAGE;
777
778 /*
779 * Enable CPU memory error:
780 * MEMERR_CPU_ICFESRA, MEMERR_CPU_LSUESRA, and MEMERR_CPU_MMUESRA
781 */
782 writel(0x00000301, pg_f + MEMERR_CPU_ICFECR_PAGE_OFFSET);
783 writel(0x00000301, pg_f + MEMERR_CPU_LSUECR_PAGE_OFFSET);
784 writel(0x00000101, pg_f + MEMERR_CPU_MMUECR_PAGE_OFFSET);
785}
786
787static bool xgene_edac_pmd_l2c_version1(void)
788{
789 /* Check all chips with PMD L2C version 1 HW */
790 #define REVIDR_MINOR_REV(revidr) ((revidr) & 0x00000007)
791
792 switch (MIDR_VARIANT(read_cpuid_id())) {
793 case 0:
794 switch (MIDR_REVISION(read_cpuid_id())) {
795 case 0:
796
797 switch (REVIDR_MINOR_REV(read_cpuid(REVIDR_EL1))) {
798 case 1:
799 case 2:
800 return true;
801 };
802 break;
803 case 1:
804 if (REVIDR_MINOR_REV(read_cpuid(REVIDR_EL1)) == 1)
805 return true;
806 break;
807 }
808 break;
809 case 1:
810 switch (MIDR_REVISION(read_cpuid_id())) {
811 case 0:
812 switch (REVIDR_MINOR_REV(read_cpuid(REVIDR_EL1))) {
813 case 1:
814 return true;
815 };
816 break;
817 case 1:
818 switch (REVIDR_MINOR_REV(read_cpuid(REVIDR_EL1))) {
819 case 1:
820 case 0:
821 return true;
822 };
823 break;
824 }
825 break;
826 }
827
828 return false;
829}
830
831static void xgene_edac_pmd_hw_cfg(struct edac_device_ctl_info *edac_dev)
832{
833 struct xgene_edac_pmd_ctx *ctx = edac_dev->pvt_info;
834 void __iomem *pg_d = ctx->pmd_csr + CPU_L2C_PAGE;
835 void __iomem *pg_e = ctx->pmd_csr + CPU_MEMERR_L2C_PAGE;
836
837 /* Enable PMD memory error - MEMERR_L2C_L2ECR and L2C_L2RTOCR */
838 writel(0x00000703, pg_e + MEMERR_L2C_L2ECR_PAGE_OFFSET);
839 /* Configure L2C HW request time out feature if supported */
840 if (!xgene_edac_pmd_l2c_version1())
841 writel(0x00000119, pg_d + CPUX_L2C_L2RTOCR_PAGE_OFFSET);
842}
843
844static void xgene_edac_pmd_hw_ctl(struct edac_device_ctl_info *edac_dev,
845 bool enable)
846{
847 struct xgene_edac_pmd_ctx *ctx = edac_dev->pvt_info;
848 int i;
849
850 /* Enable PMD error interrupt */
851 if (edac_dev->op_state == OP_RUNNING_INTERRUPT) {
852 if (enable)
853 xgene_edac_pcp_clrbits(ctx->edac, PCPHPERRINTMSK,
854 PMD0_MERR_MASK << ctx->pmd);
855 else
856 xgene_edac_pcp_setbits(ctx->edac, PCPHPERRINTMSK,
857 PMD0_MERR_MASK << ctx->pmd);
858 }
859
860 if (enable) {
861 xgene_edac_pmd_hw_cfg(edac_dev);
862
863 /* Two CPUs per a PMD */
864 for (i = 0; i < MAX_CPU_PER_PMD; i++)
865 xgene_edac_pmd_cpu_hw_cfg(edac_dev, i);
866 }
867}
868
869static ssize_t xgene_edac_pmd_l1_inject_ctrl_write(struct file *file,
870 const char __user *data,
871 size_t count, loff_t *ppos)
872{
873 struct edac_device_ctl_info *edac_dev = file->private_data;
874 struct xgene_edac_pmd_ctx *ctx = edac_dev->pvt_info;
875 void __iomem *cpux_pg_f;
876 int i;
877
878 for (i = 0; i < MAX_CPU_PER_PMD; i++) {
879 cpux_pg_f = ctx->pmd_csr + i * CPU_CSR_STRIDE +
880 CPU_MEMERR_CPU_PAGE;
881
882 writel(MEMERR_CPU_ICFESR_MULTCERR_MASK |
883 MEMERR_CPU_ICFESR_CERR_MASK,
884 cpux_pg_f + MEMERR_CPU_ICFESRA_PAGE_OFFSET);
885 writel(MEMERR_CPU_LSUESR_MULTCERR_MASK |
886 MEMERR_CPU_LSUESR_CERR_MASK,
887 cpux_pg_f + MEMERR_CPU_LSUESRA_PAGE_OFFSET);
888 writel(MEMERR_CPU_MMUESR_MULTCERR_MASK |
889 MEMERR_CPU_MMUESR_CERR_MASK,
890 cpux_pg_f + MEMERR_CPU_MMUESRA_PAGE_OFFSET);
891 }
892 return count;
893}
894
895static ssize_t xgene_edac_pmd_l2_inject_ctrl_write(struct file *file,
896 const char __user *data,
897 size_t count, loff_t *ppos)
898{
899 struct edac_device_ctl_info *edac_dev = file->private_data;
900 struct xgene_edac_pmd_ctx *ctx = edac_dev->pvt_info;
901 void __iomem *pg_e = ctx->pmd_csr + CPU_MEMERR_L2C_PAGE;
902
903 writel(MEMERR_L2C_L2ESR_MULTUCERR_MASK |
904 MEMERR_L2C_L2ESR_MULTICERR_MASK |
905 MEMERR_L2C_L2ESR_UCERR_MASK |
906 MEMERR_L2C_L2ESR_ERR_MASK,
907 pg_e + MEMERR_L2C_L2ESRA_PAGE_OFFSET);
908 return count;
909}
910
911static const struct file_operations xgene_edac_pmd_debug_inject_fops[] = {
912 {
913 .open = simple_open,
914 .write = xgene_edac_pmd_l1_inject_ctrl_write,
915 .llseek = generic_file_llseek, },
916 {
917 .open = simple_open,
918 .write = xgene_edac_pmd_l2_inject_ctrl_write,
919 .llseek = generic_file_llseek, },
920 { }
921};
922
923static void xgene_edac_pmd_create_debugfs_nodes(
924 struct edac_device_ctl_info *edac_dev)
925{
926 struct xgene_edac_pmd_ctx *ctx = edac_dev->pvt_info;
927 struct dentry *edac_debugfs;
928 char name[30];
929
930 if (!IS_ENABLED(CONFIG_EDAC_DEBUG))
931 return;
932
933 /*
934 * Todo: Switch to common EDAC debug file system for edac device
935 * when available.
936 */
937 if (!ctx->edac->dfs) {
938 ctx->edac->dfs = debugfs_create_dir(edac_dev->dev->kobj.name,
939 NULL);
940 if (!ctx->edac->dfs)
941 return;
942 }
943 sprintf(name, "PMD%d", ctx->pmd);
944 edac_debugfs = debugfs_create_dir(name, ctx->edac->dfs);
945 if (!edac_debugfs)
946 return;
947
948 debugfs_create_file("l1_inject_ctrl", S_IWUSR, edac_debugfs, edac_dev,
949 &xgene_edac_pmd_debug_inject_fops[0]);
950 debugfs_create_file("l2_inject_ctrl", S_IWUSR, edac_debugfs, edac_dev,
951 &xgene_edac_pmd_debug_inject_fops[1]);
952}
953
954static int xgene_edac_pmd_available(u32 efuse, int pmd)
955{
956 return (efuse & (1 << pmd)) ? 0 : 1;
957}
958
959static int xgene_edac_pmd_add(struct xgene_edac *edac, struct device_node *np)
960{
961 struct edac_device_ctl_info *edac_dev;
962 struct xgene_edac_pmd_ctx *ctx;
963 struct resource res;
964 char edac_name[10];
965 u32 pmd;
966 int rc;
967 u32 val;
968
969 if (!devres_open_group(edac->dev, xgene_edac_pmd_add, GFP_KERNEL))
970 return -ENOMEM;
971
972 /* Determine if this PMD is disabled */
973 if (of_property_read_u32(np, "pmd-controller", &pmd)) {
974 dev_err(edac->dev, "no pmd-controller property\n");
975 rc = -ENODEV;
976 goto err_group;
977 }
978 rc = regmap_read(edac->efuse_map, 0, &val);
979 if (rc)
980 goto err_group;
981 if (!xgene_edac_pmd_available(val, pmd)) {
982 rc = -ENODEV;
983 goto err_group;
984 }
985
986 sprintf(edac_name, "l2c%d", pmd);
987 edac_dev = edac_device_alloc_ctl_info(sizeof(*ctx),
988 edac_name, 1, "l2c", 1, 2, NULL,
989 0, edac_device_alloc_index());
990 if (!edac_dev) {
991 rc = -ENOMEM;
992 goto err_group;
993 }
994
995 ctx = edac_dev->pvt_info;
996 ctx->name = "xgene_pmd_err";
997 ctx->pmd = pmd;
998 ctx->edac = edac;
999 ctx->edac_dev = edac_dev;
1000 ctx->ddev = *edac->dev;
1001 edac_dev->dev = &ctx->ddev;
1002 edac_dev->ctl_name = ctx->name;
1003 edac_dev->dev_name = ctx->name;
1004 edac_dev->mod_name = EDAC_MOD_STR;
1005
1006 rc = of_address_to_resource(np, 0, &res);
1007 if (rc < 0) {
1008 dev_err(edac->dev, "no PMD resource address\n");
1009 goto err_free;
1010 }
1011 ctx->pmd_csr = devm_ioremap_resource(edac->dev, &res);
1012 if (IS_ERR(ctx->pmd_csr)) {
1013 dev_err(edac->dev,
1014 "devm_ioremap_resource failed for PMD resource address\n");
1015 rc = PTR_ERR(ctx->pmd_csr);
1016 goto err_free;
1017 }
1018
1019 if (edac_op_state == EDAC_OPSTATE_POLL)
1020 edac_dev->edac_check = xgene_edac_pmd_check;
1021
1022 xgene_edac_pmd_create_debugfs_nodes(edac_dev);
1023
1024 rc = edac_device_add_device(edac_dev);
1025 if (rc > 0) {
1026 dev_err(edac->dev, "edac_device_add_device failed\n");
1027 rc = -ENOMEM;
1028 goto err_free;
1029 }
1030
1031 if (edac_op_state == EDAC_OPSTATE_INT)
1032 edac_dev->op_state = OP_RUNNING_INTERRUPT;
1033
1034 list_add(&ctx->next, &edac->pmds);
1035
1036 xgene_edac_pmd_hw_ctl(edac_dev, 1);
1037
1038 devres_remove_group(edac->dev, xgene_edac_pmd_add);
1039
1040 dev_info(edac->dev, "X-Gene EDAC PMD%d registered\n", ctx->pmd);
1041 return 0;
1042
1043err_free:
1044 edac_device_free_ctl_info(edac_dev);
1045err_group:
1046 devres_release_group(edac->dev, xgene_edac_pmd_add);
1047 return rc;
1048}
1049
1050static int xgene_edac_pmd_remove(struct xgene_edac_pmd_ctx *pmd)
1051{
1052 struct edac_device_ctl_info *edac_dev = pmd->edac_dev;
1053
1054 xgene_edac_pmd_hw_ctl(edac_dev, 0);
1055 edac_device_del_device(edac_dev->dev);
1056 edac_device_free_ctl_info(edac_dev);
1057 return 0;
1058}
1059
1060static irqreturn_t xgene_edac_isr(int irq, void *dev_id)
1061{
1062 struct xgene_edac *ctx = dev_id;
1063 struct xgene_edac_pmd_ctx *pmd;
1064 unsigned int pcp_hp_stat;
1065 unsigned int pcp_lp_stat;
1066
1067 xgene_edac_pcp_rd(ctx, PCPHPERRINTSTS, &pcp_hp_stat);
1068 xgene_edac_pcp_rd(ctx, PCPLPERRINTSTS, &pcp_lp_stat);
1069 if ((MCU_UNCORR_ERR_MASK & pcp_hp_stat) ||
1070 (MCU_CTL_ERR_MASK & pcp_hp_stat) ||
1071 (MCU_CORR_ERR_MASK & pcp_lp_stat)) {
1072 struct xgene_edac_mc_ctx *mcu;
1073
1074 list_for_each_entry(mcu, &ctx->mcus, next) {
1075 xgene_edac_mc_check(mcu->mci);
1076 }
1077 }
1078
1079 list_for_each_entry(pmd, &ctx->pmds, next) {
1080 if ((PMD0_MERR_MASK << pmd->pmd) & pcp_hp_stat)
1081 xgene_edac_pmd_check(pmd->edac_dev);
1082 }
1083
1084 return IRQ_HANDLED;
1085}
1086
1087static int xgene_edac_probe(struct platform_device *pdev)
1088{
1089 struct xgene_edac *edac;
1090 struct device_node *child;
1091 struct resource *res;
1092 int rc;
1093
1094 edac = devm_kzalloc(&pdev->dev, sizeof(*edac), GFP_KERNEL);
1095 if (!edac)
1096 return -ENOMEM;
1097
1098 edac->dev = &pdev->dev;
1099 platform_set_drvdata(pdev, edac);
1100 INIT_LIST_HEAD(&edac->mcus);
1101 INIT_LIST_HEAD(&edac->pmds);
1102 spin_lock_init(&edac->lock);
1103 mutex_init(&edac->mc_lock);
1104
1105 edac->csw_map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
1106 "regmap-csw");
1107 if (IS_ERR(edac->csw_map)) {
1108 dev_err(edac->dev, "unable to get syscon regmap csw\n");
1109 rc = PTR_ERR(edac->csw_map);
1110 goto out_err;
1111 }
1112
1113 edac->mcba_map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
1114 "regmap-mcba");
1115 if (IS_ERR(edac->mcba_map)) {
1116 dev_err(edac->dev, "unable to get syscon regmap mcba\n");
1117 rc = PTR_ERR(edac->mcba_map);
1118 goto out_err;
1119 }
1120
1121 edac->mcbb_map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
1122 "regmap-mcbb");
1123 if (IS_ERR(edac->mcbb_map)) {
1124 dev_err(edac->dev, "unable to get syscon regmap mcbb\n");
1125 rc = PTR_ERR(edac->mcbb_map);
1126 goto out_err;
1127 }
1128 edac->efuse_map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
1129 "regmap-efuse");
1130 if (IS_ERR(edac->efuse_map)) {
1131 dev_err(edac->dev, "unable to get syscon regmap efuse\n");
1132 rc = PTR_ERR(edac->efuse_map);
1133 goto out_err;
1134 }
1135
1136 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1137 edac->pcp_csr = devm_ioremap_resource(&pdev->dev, res);
1138 if (IS_ERR(edac->pcp_csr)) {
1139 dev_err(&pdev->dev, "no PCP resource address\n");
1140 rc = PTR_ERR(edac->pcp_csr);
1141 goto out_err;
1142 }
1143
1144 if (edac_op_state == EDAC_OPSTATE_INT) {
1145 int irq;
1146 int i;
1147
1148 for (i = 0; i < 3; i++) {
1149 irq = platform_get_irq(pdev, i);
1150 if (irq < 0) {
1151 dev_err(&pdev->dev, "No IRQ resource\n");
1152 rc = -EINVAL;
1153 goto out_err;
1154 }
1155 rc = devm_request_irq(&pdev->dev, irq,
1156 xgene_edac_isr, IRQF_SHARED,
1157 dev_name(&pdev->dev), edac);
1158 if (rc) {
1159 dev_err(&pdev->dev,
1160 "Could not request IRQ %d\n", irq);
1161 goto out_err;
1162 }
1163 }
1164 }
1165
1166 for_each_child_of_node(pdev->dev.of_node, child) {
1167 if (!of_device_is_available(child))
1168 continue;
1169 if (of_device_is_compatible(child, "apm,xgene-edac-mc"))
1170 xgene_edac_mc_add(edac, child);
1171 if (of_device_is_compatible(child, "apm,xgene-edac-pmd"))
1172 xgene_edac_pmd_add(edac, child);
1173 }
1174
1175 return 0;
1176
1177out_err:
1178 return rc;
1179}
1180
1181static int xgene_edac_remove(struct platform_device *pdev)
1182{
1183 struct xgene_edac *edac = dev_get_drvdata(&pdev->dev);
1184 struct xgene_edac_mc_ctx *mcu;
1185 struct xgene_edac_mc_ctx *temp_mcu;
1186 struct xgene_edac_pmd_ctx *pmd;
1187 struct xgene_edac_pmd_ctx *temp_pmd;
1188
1189 list_for_each_entry_safe(mcu, temp_mcu, &edac->mcus, next) {
1190 xgene_edac_mc_remove(mcu);
1191 }
1192
1193 list_for_each_entry_safe(pmd, temp_pmd, &edac->pmds, next) {
1194 xgene_edac_pmd_remove(pmd);
1195 }
1196 return 0;
1197}
1198
1199static const struct of_device_id xgene_edac_of_match[] = {
1200 { .compatible = "apm,xgene-edac" },
1201 {},
1202};
1203MODULE_DEVICE_TABLE(of, xgene_edac_of_match);
1204
1205static struct platform_driver xgene_edac_driver = {
1206 .probe = xgene_edac_probe,
1207 .remove = xgene_edac_remove,
1208 .driver = {
1209 .name = "xgene-edac",
1210 .owner = THIS_MODULE,
1211 .of_match_table = xgene_edac_of_match,
1212 },
1213};
1214
1215static int __init xgene_edac_init(void)
1216{
1217 int rc;
1218
1219 /* Make sure error reporting method is sane */
1220 switch (edac_op_state) {
1221 case EDAC_OPSTATE_POLL:
1222 case EDAC_OPSTATE_INT:
1223 break;
1224 default:
1225 edac_op_state = EDAC_OPSTATE_INT;
1226 break;
1227 }
1228
1229 rc = platform_driver_register(&xgene_edac_driver);
1230 if (rc) {
1231 edac_printk(KERN_ERR, EDAC_MOD_STR,
1232 "EDAC fails to register\n");
1233 goto reg_failed;
1234 }
1235
1236 return 0;
1237
1238reg_failed:
1239 return rc;
1240}
1241module_init(xgene_edac_init);
1242
1243static void __exit xgene_edac_exit(void)
1244{
1245 platform_driver_unregister(&xgene_edac_driver);
1246}
1247module_exit(xgene_edac_exit);
1248
1249MODULE_LICENSE("GPL");
1250MODULE_AUTHOR("Feng Kan <fkan@apm.com>");
1251MODULE_DESCRIPTION("APM X-Gene EDAC driver");
1252module_param(edac_op_state, int, 0444);
1253MODULE_PARM_DESC(edac_op_state,
1254 "EDAC error reporting state: 0=Poll, 2=Interrupt");