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