2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2009 Wind River Systems,
7 * written by Ralf Baechle <ralf@linux-mips.org>
9 * Copyright (c) 2013 by Cisco Systems, Inc.
10 * All rights reserved.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
16 #include <linux/edac.h>
17 #include <linux/ctype.h>
19 #include <asm/octeon/octeon.h>
20 #include <asm/octeon/cvmx-lmcx-defs.h>
22 #include "edac_core.h"
23 #include "edac_module.h"
25 #define OCTEON_MAX_MC 4
27 #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
29 struct octeon_lmc_pvt
{
31 unsigned long error_type
;
39 static void octeon_lmc_edac_poll(struct mem_ctl_info
*mci
)
41 union cvmx_lmcx_mem_cfg0 cfg0
;
42 bool do_clear
= false;
45 cfg0
.u64
= cvmx_read_csr(CVMX_LMCX_MEM_CFG0(mci
->mc_idx
));
46 if (cfg0
.s
.sec_err
|| cfg0
.s
.ded_err
) {
47 union cvmx_lmcx_fadr fadr
;
48 fadr
.u64
= cvmx_read_csr(CVMX_LMCX_FADR(mci
->mc_idx
));
49 snprintf(msg
, sizeof(msg
),
50 "DIMM %d rank %d bank %d row %d col %d",
51 fadr
.cn30xx
.fdimm
, fadr
.cn30xx
.fbunk
,
52 fadr
.cn30xx
.fbank
, fadr
.cn30xx
.frow
, fadr
.cn30xx
.fcol
);
56 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED
, mci
, 1, 0, 0, 0,
58 cfg0
.s
.sec_err
= -1; /* Done, re-arm */
63 edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED
, mci
, 1, 0, 0, 0,
65 cfg0
.s
.ded_err
= -1; /* Done, re-arm */
69 cvmx_write_csr(CVMX_LMCX_MEM_CFG0(mci
->mc_idx
), cfg0
.u64
);
72 static void octeon_lmc_edac_poll_o2(struct mem_ctl_info
*mci
)
74 struct octeon_lmc_pvt
*pvt
= mci
->pvt_info
;
75 union cvmx_lmcx_int int_reg
;
76 bool do_clear
= false;
80 int_reg
.u64
= cvmx_read_csr(CVMX_LMCX_INT(mci
->mc_idx
));
83 if (pvt
->error_type
== 1)
84 int_reg
.s
.sec_err
= 1;
85 if (pvt
->error_type
== 2)
86 int_reg
.s
.ded_err
= 1;
89 if (int_reg
.s
.sec_err
|| int_reg
.s
.ded_err
) {
90 union cvmx_lmcx_fadr fadr
;
91 if (likely(!pvt
->inject
))
92 fadr
.u64
= cvmx_read_csr(CVMX_LMCX_FADR(mci
->mc_idx
));
94 fadr
.cn61xx
.fdimm
= pvt
->dimm
;
95 fadr
.cn61xx
.fbunk
= pvt
->rank
;
96 fadr
.cn61xx
.fbank
= pvt
->bank
;
97 fadr
.cn61xx
.frow
= pvt
->row
;
98 fadr
.cn61xx
.fcol
= pvt
->col
;
100 snprintf(msg
, sizeof(msg
),
101 "DIMM %d rank %d bank %d row %d col %d",
102 fadr
.cn61xx
.fdimm
, fadr
.cn61xx
.fbunk
,
103 fadr
.cn61xx
.fbank
, fadr
.cn61xx
.frow
, fadr
.cn61xx
.fcol
);
106 if (int_reg
.s
.sec_err
) {
107 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED
, mci
, 1, 0, 0, 0,
108 -1, -1, -1, msg
, "");
109 int_reg
.s
.sec_err
= -1; /* Done, re-arm */
113 if (int_reg
.s
.ded_err
) {
114 edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED
, mci
, 1, 0, 0, 0,
115 -1, -1, -1, msg
, "");
116 int_reg
.s
.ded_err
= -1; /* Done, re-arm */
121 if (likely(!pvt
->inject
))
122 cvmx_write_csr(CVMX_LMCX_INT(mci
->mc_idx
), int_reg
.u64
);
128 /************************ MC SYSFS parts ***********************************/
130 /* Only a couple naming differences per template, so very similar */
131 #define TEMPLATE_SHOW(reg) \
132 static ssize_t octeon_mc_inject_##reg##_show(struct device *dev, \
133 struct device_attribute *attr, \
136 struct mem_ctl_info *mci = to_mci(dev); \
137 struct octeon_lmc_pvt *pvt = mci->pvt_info; \
138 return sprintf(data, "%016llu\n", (u64)pvt->reg); \
141 #define TEMPLATE_STORE(reg) \
142 static ssize_t octeon_mc_inject_##reg##_store(struct device *dev, \
143 struct device_attribute *attr, \
144 const char *data, size_t count) \
146 struct mem_ctl_info *mci = to_mci(dev); \
147 struct octeon_lmc_pvt *pvt = mci->pvt_info; \
148 if (isdigit(*data)) { \
149 if (!kstrtoul(data, 0, &pvt->reg)) \
155 TEMPLATE_SHOW(inject
);
156 TEMPLATE_STORE(inject
);
158 TEMPLATE_STORE(dimm
);
160 TEMPLATE_STORE(bank
);
162 TEMPLATE_STORE(rank
);
168 static ssize_t
octeon_mc_inject_error_type_store(struct device
*dev
,
169 struct device_attribute
*attr
,
173 struct mem_ctl_info
*mci
= to_mci(dev
);
174 struct octeon_lmc_pvt
*pvt
= mci
->pvt_info
;
176 if (!strncmp(data
, "single", 6))
178 else if (!strncmp(data
, "double", 6))
184 static ssize_t
octeon_mc_inject_error_type_show(struct device
*dev
,
185 struct device_attribute
*attr
,
188 struct mem_ctl_info
*mci
= to_mci(dev
);
189 struct octeon_lmc_pvt
*pvt
= mci
->pvt_info
;
190 if (pvt
->error_type
== 1)
191 return sprintf(data
, "single");
192 else if (pvt
->error_type
== 2)
193 return sprintf(data
, "double");
198 static DEVICE_ATTR(inject
, S_IRUGO
| S_IWUSR
,
199 octeon_mc_inject_inject_show
, octeon_mc_inject_inject_store
);
200 static DEVICE_ATTR(error_type
, S_IRUGO
| S_IWUSR
,
201 octeon_mc_inject_error_type_show
, octeon_mc_inject_error_type_store
);
202 static DEVICE_ATTR(dimm
, S_IRUGO
| S_IWUSR
,
203 octeon_mc_inject_dimm_show
, octeon_mc_inject_dimm_store
);
204 static DEVICE_ATTR(rank
, S_IRUGO
| S_IWUSR
,
205 octeon_mc_inject_rank_show
, octeon_mc_inject_rank_store
);
206 static DEVICE_ATTR(bank
, S_IRUGO
| S_IWUSR
,
207 octeon_mc_inject_bank_show
, octeon_mc_inject_bank_store
);
208 static DEVICE_ATTR(row
, S_IRUGO
| S_IWUSR
,
209 octeon_mc_inject_row_show
, octeon_mc_inject_row_store
);
210 static DEVICE_ATTR(col
, S_IRUGO
| S_IWUSR
,
211 octeon_mc_inject_col_show
, octeon_mc_inject_col_store
);
213 static struct attribute
*octeon_dev_attrs
[] = {
214 &dev_attr_inject
.attr
,
215 &dev_attr_error_type
.attr
,
224 ATTRIBUTE_GROUPS(octeon_dev
);
226 static int octeon_lmc_edac_probe(struct platform_device
*pdev
)
228 struct mem_ctl_info
*mci
;
229 struct edac_mc_layer layers
[1];
234 layers
[0].type
= EDAC_MC_LAYER_CHANNEL
;
236 layers
[0].is_virt_csrow
= false;
238 if (OCTEON_IS_OCTEON1PLUS()) {
239 union cvmx_lmcx_mem_cfg0 cfg0
;
241 cfg0
.u64
= cvmx_read_csr(CVMX_LMCX_MEM_CFG0(0));
242 if (!cfg0
.s
.ecc_ena
) {
243 dev_info(&pdev
->dev
, "Disabled (ECC not enabled)\n");
247 mci
= edac_mc_alloc(mc
, ARRAY_SIZE(layers
), layers
, sizeof(struct octeon_lmc_pvt
));
251 mci
->pdev
= &pdev
->dev
;
252 mci
->dev_name
= dev_name(&pdev
->dev
);
254 mci
->mod_name
= "octeon-lmc";
255 mci
->ctl_name
= "octeon-lmc-err";
256 mci
->edac_check
= octeon_lmc_edac_poll
;
258 if (edac_mc_add_mc_with_groups(mci
, octeon_dev_groups
)) {
259 dev_err(&pdev
->dev
, "edac_mc_add_mc() failed\n");
264 cfg0
.u64
= cvmx_read_csr(CVMX_LMCX_MEM_CFG0(mc
));
265 cfg0
.s
.intr_ded_ena
= 0; /* We poll */
266 cfg0
.s
.intr_sec_ena
= 0;
267 cvmx_write_csr(CVMX_LMCX_MEM_CFG0(mc
), cfg0
.u64
);
270 union cvmx_lmcx_int_en en
;
271 union cvmx_lmcx_config config
;
273 config
.u64
= cvmx_read_csr(CVMX_LMCX_CONFIG(0));
274 if (!config
.s
.ecc_ena
) {
275 dev_info(&pdev
->dev
, "Disabled (ECC not enabled)\n");
279 mci
= edac_mc_alloc(mc
, ARRAY_SIZE(layers
), layers
, sizeof(struct octeon_lmc_pvt
));
283 mci
->pdev
= &pdev
->dev
;
284 mci
->dev_name
= dev_name(&pdev
->dev
);
286 mci
->mod_name
= "octeon-lmc";
287 mci
->ctl_name
= "co_lmc_err";
288 mci
->edac_check
= octeon_lmc_edac_poll_o2
;
290 if (edac_mc_add_mc_with_groups(mci
, octeon_dev_groups
)) {
291 dev_err(&pdev
->dev
, "edac_mc_add_mc() failed\n");
296 en
.u64
= cvmx_read_csr(CVMX_LMCX_MEM_CFG0(mc
));
297 en
.s
.intr_ded_ena
= 0; /* We poll */
298 en
.s
.intr_sec_ena
= 0;
299 cvmx_write_csr(CVMX_LMCX_MEM_CFG0(mc
), en
.u64
);
301 platform_set_drvdata(pdev
, mci
);
306 static int octeon_lmc_edac_remove(struct platform_device
*pdev
)
308 struct mem_ctl_info
*mci
= platform_get_drvdata(pdev
);
310 edac_mc_del_mc(&pdev
->dev
);
315 static struct platform_driver octeon_lmc_edac_driver
= {
316 .probe
= octeon_lmc_edac_probe
,
317 .remove
= octeon_lmc_edac_remove
,
319 .name
= "octeon_lmc_edac",
322 module_platform_driver(octeon_lmc_edac_driver
);
324 MODULE_LICENSE("GPL");
325 MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");