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_module.h"
24 #define OCTEON_MAX_MC 4
26 #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
28 struct octeon_lmc_pvt
{
30 unsigned long error_type
;
38 static void octeon_lmc_edac_poll(struct mem_ctl_info
*mci
)
40 union cvmx_lmcx_mem_cfg0 cfg0
;
41 bool do_clear
= false;
44 cfg0
.u64
= cvmx_read_csr(CVMX_LMCX_MEM_CFG0(mci
->mc_idx
));
45 if (cfg0
.s
.sec_err
|| cfg0
.s
.ded_err
) {
46 union cvmx_lmcx_fadr fadr
;
47 fadr
.u64
= cvmx_read_csr(CVMX_LMCX_FADR(mci
->mc_idx
));
48 snprintf(msg
, sizeof(msg
),
49 "DIMM %d rank %d bank %d row %d col %d",
50 fadr
.cn30xx
.fdimm
, fadr
.cn30xx
.fbunk
,
51 fadr
.cn30xx
.fbank
, fadr
.cn30xx
.frow
, fadr
.cn30xx
.fcol
);
55 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED
, mci
, 1, 0, 0, 0,
57 cfg0
.s
.sec_err
= -1; /* Done, re-arm */
62 edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED
, mci
, 1, 0, 0, 0,
64 cfg0
.s
.ded_err
= -1; /* Done, re-arm */
68 cvmx_write_csr(CVMX_LMCX_MEM_CFG0(mci
->mc_idx
), cfg0
.u64
);
71 static void octeon_lmc_edac_poll_o2(struct mem_ctl_info
*mci
)
73 struct octeon_lmc_pvt
*pvt
= mci
->pvt_info
;
74 union cvmx_lmcx_int int_reg
;
75 bool do_clear
= false;
79 int_reg
.u64
= cvmx_read_csr(CVMX_LMCX_INT(mci
->mc_idx
));
81 if (pvt
->error_type
== 1)
82 int_reg
.s
.sec_err
= 1;
83 if (pvt
->error_type
== 2)
84 int_reg
.s
.ded_err
= 1;
87 if (int_reg
.s
.sec_err
|| int_reg
.s
.ded_err
) {
88 union cvmx_lmcx_fadr fadr
;
89 if (likely(!pvt
->inject
))
90 fadr
.u64
= cvmx_read_csr(CVMX_LMCX_FADR(mci
->mc_idx
));
92 fadr
.cn61xx
.fdimm
= pvt
->dimm
;
93 fadr
.cn61xx
.fbunk
= pvt
->rank
;
94 fadr
.cn61xx
.fbank
= pvt
->bank
;
95 fadr
.cn61xx
.frow
= pvt
->row
;
96 fadr
.cn61xx
.fcol
= pvt
->col
;
98 snprintf(msg
, sizeof(msg
),
99 "DIMM %d rank %d bank %d row %d col %d",
100 fadr
.cn61xx
.fdimm
, fadr
.cn61xx
.fbunk
,
101 fadr
.cn61xx
.fbank
, fadr
.cn61xx
.frow
, fadr
.cn61xx
.fcol
);
104 if (int_reg
.s
.sec_err
) {
105 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED
, mci
, 1, 0, 0, 0,
106 -1, -1, -1, msg
, "");
107 int_reg
.s
.sec_err
= -1; /* Done, re-arm */
111 if (int_reg
.s
.ded_err
) {
112 edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED
, mci
, 1, 0, 0, 0,
113 -1, -1, -1, msg
, "");
114 int_reg
.s
.ded_err
= -1; /* Done, re-arm */
119 if (likely(!pvt
->inject
))
120 cvmx_write_csr(CVMX_LMCX_INT(mci
->mc_idx
), int_reg
.u64
);
126 /************************ MC SYSFS parts ***********************************/
128 /* Only a couple naming differences per template, so very similar */
129 #define TEMPLATE_SHOW(reg) \
130 static ssize_t octeon_mc_inject_##reg##_show(struct device *dev, \
131 struct device_attribute *attr, \
134 struct mem_ctl_info *mci = to_mci(dev); \
135 struct octeon_lmc_pvt *pvt = mci->pvt_info; \
136 return sprintf(data, "%016llu\n", (u64)pvt->reg); \
139 #define TEMPLATE_STORE(reg) \
140 static ssize_t octeon_mc_inject_##reg##_store(struct device *dev, \
141 struct device_attribute *attr, \
142 const char *data, size_t count) \
144 struct mem_ctl_info *mci = to_mci(dev); \
145 struct octeon_lmc_pvt *pvt = mci->pvt_info; \
146 if (isdigit(*data)) { \
147 if (!kstrtoul(data, 0, &pvt->reg)) \
153 TEMPLATE_SHOW(inject
);
154 TEMPLATE_STORE(inject
);
156 TEMPLATE_STORE(dimm
);
158 TEMPLATE_STORE(bank
);
160 TEMPLATE_STORE(rank
);
166 static ssize_t
octeon_mc_inject_error_type_store(struct device
*dev
,
167 struct device_attribute
*attr
,
171 struct mem_ctl_info
*mci
= to_mci(dev
);
172 struct octeon_lmc_pvt
*pvt
= mci
->pvt_info
;
174 if (!strncmp(data
, "single", 6))
176 else if (!strncmp(data
, "double", 6))
182 static ssize_t
octeon_mc_inject_error_type_show(struct device
*dev
,
183 struct device_attribute
*attr
,
186 struct mem_ctl_info
*mci
= to_mci(dev
);
187 struct octeon_lmc_pvt
*pvt
= mci
->pvt_info
;
188 if (pvt
->error_type
== 1)
189 return sprintf(data
, "single");
190 else if (pvt
->error_type
== 2)
191 return sprintf(data
, "double");
196 static DEVICE_ATTR(inject
, S_IRUGO
| S_IWUSR
,
197 octeon_mc_inject_inject_show
, octeon_mc_inject_inject_store
);
198 static DEVICE_ATTR(error_type
, S_IRUGO
| S_IWUSR
,
199 octeon_mc_inject_error_type_show
, octeon_mc_inject_error_type_store
);
200 static DEVICE_ATTR(dimm
, S_IRUGO
| S_IWUSR
,
201 octeon_mc_inject_dimm_show
, octeon_mc_inject_dimm_store
);
202 static DEVICE_ATTR(rank
, S_IRUGO
| S_IWUSR
,
203 octeon_mc_inject_rank_show
, octeon_mc_inject_rank_store
);
204 static DEVICE_ATTR(bank
, S_IRUGO
| S_IWUSR
,
205 octeon_mc_inject_bank_show
, octeon_mc_inject_bank_store
);
206 static DEVICE_ATTR(row
, S_IRUGO
| S_IWUSR
,
207 octeon_mc_inject_row_show
, octeon_mc_inject_row_store
);
208 static DEVICE_ATTR(col
, S_IRUGO
| S_IWUSR
,
209 octeon_mc_inject_col_show
, octeon_mc_inject_col_store
);
211 static struct attribute
*octeon_dev_attrs
[] = {
212 &dev_attr_inject
.attr
,
213 &dev_attr_error_type
.attr
,
222 ATTRIBUTE_GROUPS(octeon_dev
);
224 static int octeon_lmc_edac_probe(struct platform_device
*pdev
)
226 struct mem_ctl_info
*mci
;
227 struct edac_mc_layer layers
[1];
232 layers
[0].type
= EDAC_MC_LAYER_CHANNEL
;
234 layers
[0].is_virt_csrow
= false;
236 if (OCTEON_IS_OCTEON1PLUS()) {
237 union cvmx_lmcx_mem_cfg0 cfg0
;
239 cfg0
.u64
= cvmx_read_csr(CVMX_LMCX_MEM_CFG0(0));
240 if (!cfg0
.s
.ecc_ena
) {
241 dev_info(&pdev
->dev
, "Disabled (ECC not enabled)\n");
245 mci
= edac_mc_alloc(mc
, ARRAY_SIZE(layers
), layers
, sizeof(struct octeon_lmc_pvt
));
249 mci
->pdev
= &pdev
->dev
;
250 mci
->dev_name
= dev_name(&pdev
->dev
);
252 mci
->mod_name
= "octeon-lmc";
253 mci
->ctl_name
= "octeon-lmc-err";
254 mci
->edac_check
= octeon_lmc_edac_poll
;
256 if (edac_mc_add_mc_with_groups(mci
, octeon_dev_groups
)) {
257 dev_err(&pdev
->dev
, "edac_mc_add_mc() failed\n");
262 cfg0
.u64
= cvmx_read_csr(CVMX_LMCX_MEM_CFG0(mc
));
263 cfg0
.s
.intr_ded_ena
= 0; /* We poll */
264 cfg0
.s
.intr_sec_ena
= 0;
265 cvmx_write_csr(CVMX_LMCX_MEM_CFG0(mc
), cfg0
.u64
);
268 union cvmx_lmcx_int_en en
;
269 union cvmx_lmcx_config config
;
271 config
.u64
= cvmx_read_csr(CVMX_LMCX_CONFIG(0));
272 if (!config
.s
.ecc_ena
) {
273 dev_info(&pdev
->dev
, "Disabled (ECC not enabled)\n");
277 mci
= edac_mc_alloc(mc
, ARRAY_SIZE(layers
), layers
, sizeof(struct octeon_lmc_pvt
));
281 mci
->pdev
= &pdev
->dev
;
282 mci
->dev_name
= dev_name(&pdev
->dev
);
284 mci
->mod_name
= "octeon-lmc";
285 mci
->ctl_name
= "co_lmc_err";
286 mci
->edac_check
= octeon_lmc_edac_poll_o2
;
288 if (edac_mc_add_mc_with_groups(mci
, octeon_dev_groups
)) {
289 dev_err(&pdev
->dev
, "edac_mc_add_mc() failed\n");
294 en
.u64
= cvmx_read_csr(CVMX_LMCX_MEM_CFG0(mc
));
295 en
.s
.intr_ded_ena
= 0; /* We poll */
296 en
.s
.intr_sec_ena
= 0;
297 cvmx_write_csr(CVMX_LMCX_MEM_CFG0(mc
), en
.u64
);
299 platform_set_drvdata(pdev
, mci
);
304 static int octeon_lmc_edac_remove(struct platform_device
*pdev
)
306 struct mem_ctl_info
*mci
= platform_get_drvdata(pdev
);
308 edac_mc_del_mc(&pdev
->dev
);
313 static struct platform_driver octeon_lmc_edac_driver
= {
314 .probe
= octeon_lmc_edac_probe
,
315 .remove
= octeon_lmc_edac_remove
,
317 .name
= "octeon_lmc_edac",
320 module_platform_driver(octeon_lmc_edac_driver
);
322 MODULE_LICENSE("GPL");
323 MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");