2 * edac_mc kernel module
3 * (C) 2005-2007 Linux Networx (http://lnxi.com)
5 * This file may be distributed under the terms of the
6 * GNU General Public License.
8 * Written Doug Thompson <norsk5@xmission.com> www.softwarebitmaker.com
10 * (c) 2012 - Mauro Carvalho Chehab <mchehab@redhat.com>
11 * The entire API were re-written, and ported to use struct device
15 #include <linux/ctype.h>
16 #include <linux/slab.h>
17 #include <linux/edac.h>
18 #include <linux/bug.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/uaccess.h>
22 #include "edac_core.h"
23 #include "edac_module.h"
25 /* MC EDAC Controls, setable by module parameter, and sysfs */
26 static int edac_mc_log_ue
= 1;
27 static int edac_mc_log_ce
= 1;
28 static int edac_mc_panic_on_ue
;
29 static int edac_mc_poll_msec
= 1000;
31 /* Getter functions for above */
32 int edac_mc_get_log_ue(void)
34 return edac_mc_log_ue
;
37 int edac_mc_get_log_ce(void)
39 return edac_mc_log_ce
;
42 int edac_mc_get_panic_on_ue(void)
44 return edac_mc_panic_on_ue
;
47 /* this is temporary */
48 int edac_mc_get_poll_msec(void)
50 return edac_mc_poll_msec
;
53 static int edac_set_poll_msec(const char *val
, struct kernel_param
*kp
)
61 ret
= strict_strtol(val
, 0, &l
);
62 if (ret
== -EINVAL
|| ((int)l
!= l
))
64 *((int *)kp
->arg
) = l
;
66 /* notify edac_mc engine to reset the poll period */
67 edac_mc_reset_delay_period(l
);
72 /* Parameter declarations for above */
73 module_param(edac_mc_panic_on_ue
, int, 0644);
74 MODULE_PARM_DESC(edac_mc_panic_on_ue
, "Panic on uncorrected error: 0=off 1=on");
75 module_param(edac_mc_log_ue
, int, 0644);
76 MODULE_PARM_DESC(edac_mc_log_ue
,
77 "Log uncorrectable error to console: 0=off 1=on");
78 module_param(edac_mc_log_ce
, int, 0644);
79 MODULE_PARM_DESC(edac_mc_log_ce
,
80 "Log correctable error to console: 0=off 1=on");
81 module_param_call(edac_mc_poll_msec
, edac_set_poll_msec
, param_get_int
,
82 &edac_mc_poll_msec
, 0644);
83 MODULE_PARM_DESC(edac_mc_poll_msec
, "Polling period in milliseconds");
85 static struct device
*mci_pdev
;
88 * various constants for Memory Controllers
90 static const char *mem_types
[] = {
91 [MEM_EMPTY
] = "Empty",
92 [MEM_RESERVED
] = "Reserved",
93 [MEM_UNKNOWN
] = "Unknown",
97 [MEM_SDR
] = "Unbuffered-SDR",
98 [MEM_RDR
] = "Registered-SDR",
99 [MEM_DDR
] = "Unbuffered-DDR",
100 [MEM_RDDR
] = "Registered-DDR",
102 [MEM_DDR2
] = "Unbuffered-DDR2",
103 [MEM_FB_DDR2
] = "FullyBuffered-DDR2",
104 [MEM_RDDR2
] = "Registered-DDR2",
106 [MEM_DDR3
] = "Unbuffered-DDR3",
107 [MEM_RDDR3
] = "Registered-DDR3"
110 static const char *dev_types
[] = {
111 [DEV_UNKNOWN
] = "Unknown",
121 static const char *edac_caps
[] = {
122 [EDAC_UNKNOWN
] = "Unknown",
123 [EDAC_NONE
] = "None",
124 [EDAC_RESERVED
] = "Reserved",
125 [EDAC_PARITY
] = "PARITY",
127 [EDAC_SECDED
] = "SECDED",
128 [EDAC_S2ECD2ED
] = "S2ECD2ED",
129 [EDAC_S4ECD4ED
] = "S4ECD4ED",
130 [EDAC_S8ECD8ED
] = "S8ECD8ED",
131 [EDAC_S16ECD16ED
] = "S16ECD16ED"
134 #ifdef CONFIG_EDAC_LEGACY_SYSFS
136 * EDAC sysfs CSROW data structures and methods
139 #define to_csrow(k) container_of(k, struct csrow_info, dev)
142 * We need it to avoid namespace conflicts between the legacy API
143 * and the per-dimm/per-rank one
145 #define DEVICE_ATTR_LEGACY(_name, _mode, _show, _store) \
146 struct device_attribute dev_attr_legacy_##_name = __ATTR(_name, _mode, _show, _store)
148 struct dev_ch_attribute
{
149 struct device_attribute attr
;
153 #define DEVICE_CHANNEL(_name, _mode, _show, _store, _var) \
154 struct dev_ch_attribute dev_attr_legacy_##_name = \
155 { __ATTR(_name, _mode, _show, _store), (_var) }
157 #define to_channel(k) (container_of(k, struct dev_ch_attribute, attr)->channel)
159 /* Set of more default csrow<id> attribute show/store functions */
160 static ssize_t
csrow_ue_count_show(struct device
*dev
,
161 struct device_attribute
*mattr
, char *data
)
163 struct csrow_info
*csrow
= to_csrow(dev
);
165 return sprintf(data
, "%u\n", csrow
->ue_count
);
168 static ssize_t
csrow_ce_count_show(struct device
*dev
,
169 struct device_attribute
*mattr
, char *data
)
171 struct csrow_info
*csrow
= to_csrow(dev
);
173 return sprintf(data
, "%u\n", csrow
->ce_count
);
176 static ssize_t
csrow_size_show(struct device
*dev
,
177 struct device_attribute
*mattr
, char *data
)
179 struct csrow_info
*csrow
= to_csrow(dev
);
183 for (i
= 0; i
< csrow
->nr_channels
; i
++)
184 nr_pages
+= csrow
->channels
[i
]->dimm
->nr_pages
;
185 return sprintf(data
, "%u\n", PAGES_TO_MiB(nr_pages
));
188 static ssize_t
csrow_mem_type_show(struct device
*dev
,
189 struct device_attribute
*mattr
, char *data
)
191 struct csrow_info
*csrow
= to_csrow(dev
);
193 return sprintf(data
, "%s\n", mem_types
[csrow
->channels
[0]->dimm
->mtype
]);
196 static ssize_t
csrow_dev_type_show(struct device
*dev
,
197 struct device_attribute
*mattr
, char *data
)
199 struct csrow_info
*csrow
= to_csrow(dev
);
201 return sprintf(data
, "%s\n", dev_types
[csrow
->channels
[0]->dimm
->dtype
]);
204 static ssize_t
csrow_edac_mode_show(struct device
*dev
,
205 struct device_attribute
*mattr
,
208 struct csrow_info
*csrow
= to_csrow(dev
);
210 return sprintf(data
, "%s\n", edac_caps
[csrow
->channels
[0]->dimm
->edac_mode
]);
213 /* show/store functions for DIMM Label attributes */
214 static ssize_t
channel_dimm_label_show(struct device
*dev
,
215 struct device_attribute
*mattr
,
218 struct csrow_info
*csrow
= to_csrow(dev
);
219 unsigned chan
= to_channel(mattr
);
220 struct rank_info
*rank
= csrow
->channels
[chan
];
222 /* if field has not been initialized, there is nothing to send */
223 if (!rank
->dimm
->label
[0])
226 return snprintf(data
, EDAC_MC_LABEL_LEN
, "%s\n",
230 static ssize_t
channel_dimm_label_store(struct device
*dev
,
231 struct device_attribute
*mattr
,
232 const char *data
, size_t count
)
234 struct csrow_info
*csrow
= to_csrow(dev
);
235 unsigned chan
= to_channel(mattr
);
236 struct rank_info
*rank
= csrow
->channels
[chan
];
238 ssize_t max_size
= 0;
240 max_size
= min((ssize_t
) count
, (ssize_t
) EDAC_MC_LABEL_LEN
- 1);
241 strncpy(rank
->dimm
->label
, data
, max_size
);
242 rank
->dimm
->label
[max_size
] = '\0';
247 /* show function for dynamic chX_ce_count attribute */
248 static ssize_t
channel_ce_count_show(struct device
*dev
,
249 struct device_attribute
*mattr
, char *data
)
251 struct csrow_info
*csrow
= to_csrow(dev
);
252 unsigned chan
= to_channel(mattr
);
253 struct rank_info
*rank
= csrow
->channels
[chan
];
255 return sprintf(data
, "%u\n", rank
->ce_count
);
258 /* cwrow<id>/attribute files */
259 DEVICE_ATTR_LEGACY(size_mb
, S_IRUGO
, csrow_size_show
, NULL
);
260 DEVICE_ATTR_LEGACY(dev_type
, S_IRUGO
, csrow_dev_type_show
, NULL
);
261 DEVICE_ATTR_LEGACY(mem_type
, S_IRUGO
, csrow_mem_type_show
, NULL
);
262 DEVICE_ATTR_LEGACY(edac_mode
, S_IRUGO
, csrow_edac_mode_show
, NULL
);
263 DEVICE_ATTR_LEGACY(ue_count
, S_IRUGO
, csrow_ue_count_show
, NULL
);
264 DEVICE_ATTR_LEGACY(ce_count
, S_IRUGO
, csrow_ce_count_show
, NULL
);
266 /* default attributes of the CSROW<id> object */
267 static struct attribute
*csrow_attrs
[] = {
268 &dev_attr_legacy_dev_type
.attr
,
269 &dev_attr_legacy_mem_type
.attr
,
270 &dev_attr_legacy_edac_mode
.attr
,
271 &dev_attr_legacy_size_mb
.attr
,
272 &dev_attr_legacy_ue_count
.attr
,
273 &dev_attr_legacy_ce_count
.attr
,
277 static struct attribute_group csrow_attr_grp
= {
278 .attrs
= csrow_attrs
,
281 static const struct attribute_group
*csrow_attr_groups
[] = {
286 static void csrow_attr_release(struct device
*dev
)
288 struct csrow_info
*csrow
= container_of(dev
, struct csrow_info
, dev
);
290 debugf1("Releasing csrow device %s\n", dev_name(dev
));
294 static struct device_type csrow_attr_type
= {
295 .groups
= csrow_attr_groups
,
296 .release
= csrow_attr_release
,
300 * possible dynamic channel DIMM Label attribute files
304 #define EDAC_NR_CHANNELS 6
306 DEVICE_CHANNEL(ch0_dimm_label
, S_IRUGO
| S_IWUSR
,
307 channel_dimm_label_show
, channel_dimm_label_store
, 0);
308 DEVICE_CHANNEL(ch1_dimm_label
, S_IRUGO
| S_IWUSR
,
309 channel_dimm_label_show
, channel_dimm_label_store
, 1);
310 DEVICE_CHANNEL(ch2_dimm_label
, S_IRUGO
| S_IWUSR
,
311 channel_dimm_label_show
, channel_dimm_label_store
, 2);
312 DEVICE_CHANNEL(ch3_dimm_label
, S_IRUGO
| S_IWUSR
,
313 channel_dimm_label_show
, channel_dimm_label_store
, 3);
314 DEVICE_CHANNEL(ch4_dimm_label
, S_IRUGO
| S_IWUSR
,
315 channel_dimm_label_show
, channel_dimm_label_store
, 4);
316 DEVICE_CHANNEL(ch5_dimm_label
, S_IRUGO
| S_IWUSR
,
317 channel_dimm_label_show
, channel_dimm_label_store
, 5);
319 /* Total possible dynamic DIMM Label attribute file table */
320 static struct device_attribute
*dynamic_csrow_dimm_attr
[] = {
321 &dev_attr_legacy_ch0_dimm_label
.attr
,
322 &dev_attr_legacy_ch1_dimm_label
.attr
,
323 &dev_attr_legacy_ch2_dimm_label
.attr
,
324 &dev_attr_legacy_ch3_dimm_label
.attr
,
325 &dev_attr_legacy_ch4_dimm_label
.attr
,
326 &dev_attr_legacy_ch5_dimm_label
.attr
329 /* possible dynamic channel ce_count attribute files */
330 DEVICE_CHANNEL(ch0_ce_count
, S_IRUGO
| S_IWUSR
,
331 channel_ce_count_show
, NULL
, 0);
332 DEVICE_CHANNEL(ch1_ce_count
, S_IRUGO
| S_IWUSR
,
333 channel_ce_count_show
, NULL
, 1);
334 DEVICE_CHANNEL(ch2_ce_count
, S_IRUGO
| S_IWUSR
,
335 channel_ce_count_show
, NULL
, 2);
336 DEVICE_CHANNEL(ch3_ce_count
, S_IRUGO
| S_IWUSR
,
337 channel_ce_count_show
, NULL
, 3);
338 DEVICE_CHANNEL(ch4_ce_count
, S_IRUGO
| S_IWUSR
,
339 channel_ce_count_show
, NULL
, 4);
340 DEVICE_CHANNEL(ch5_ce_count
, S_IRUGO
| S_IWUSR
,
341 channel_ce_count_show
, NULL
, 5);
343 /* Total possible dynamic ce_count attribute file table */
344 static struct device_attribute
*dynamic_csrow_ce_count_attr
[] = {
345 &dev_attr_legacy_ch0_ce_count
.attr
,
346 &dev_attr_legacy_ch1_ce_count
.attr
,
347 &dev_attr_legacy_ch2_ce_count
.attr
,
348 &dev_attr_legacy_ch3_ce_count
.attr
,
349 &dev_attr_legacy_ch4_ce_count
.attr
,
350 &dev_attr_legacy_ch5_ce_count
.attr
353 static inline int nr_pages_per_csrow(struct csrow_info
*csrow
)
355 int chan
, nr_pages
= 0;
357 for (chan
= 0; chan
< csrow
->nr_channels
; chan
++)
358 nr_pages
+= csrow
->channels
[chan
]->dimm
->nr_pages
;
363 /* Create a CSROW object under specifed edac_mc_device */
364 static int edac_create_csrow_object(struct mem_ctl_info
*mci
,
365 struct csrow_info
*csrow
, int index
)
369 if (csrow
->nr_channels
>= EDAC_NR_CHANNELS
)
372 csrow
->dev
.type
= &csrow_attr_type
;
373 csrow
->dev
.bus
= &mci
->bus
;
374 device_initialize(&csrow
->dev
);
375 csrow
->dev
.parent
= &mci
->dev
;
376 dev_set_name(&csrow
->dev
, "csrow%d", index
);
377 dev_set_drvdata(&csrow
->dev
, csrow
);
379 debugf0("%s(): creating (virtual) csrow node %s\n", __func__
,
380 dev_name(&csrow
->dev
));
382 err
= device_add(&csrow
->dev
);
386 for (chan
= 0; chan
< csrow
->nr_channels
; chan
++) {
387 /* Only expose populated DIMMs */
388 if (!csrow
->channels
[chan
]->dimm
->nr_pages
)
390 err
= device_create_file(&csrow
->dev
,
391 dynamic_csrow_dimm_attr
[chan
]);
394 err
= device_create_file(&csrow
->dev
,
395 dynamic_csrow_ce_count_attr
[chan
]);
397 device_remove_file(&csrow
->dev
,
398 dynamic_csrow_dimm_attr
[chan
]);
406 for (--chan
; chan
>= 0; chan
--) {
407 device_remove_file(&csrow
->dev
,
408 dynamic_csrow_dimm_attr
[chan
]);
409 device_remove_file(&csrow
->dev
,
410 dynamic_csrow_ce_count_attr
[chan
]);
412 put_device(&csrow
->dev
);
417 /* Create a CSROW object under specifed edac_mc_device */
418 static int edac_create_csrow_objects(struct mem_ctl_info
*mci
)
421 struct csrow_info
*csrow
;
423 for (i
= 0; i
< mci
->nr_csrows
; i
++) {
424 csrow
= mci
->csrows
[i
];
425 if (!nr_pages_per_csrow(csrow
))
427 err
= edac_create_csrow_object(mci
, mci
->csrows
[i
], i
);
434 for (--i
; i
>= 0; i
--) {
435 csrow
= mci
->csrows
[i
];
436 if (!nr_pages_per_csrow(csrow
))
438 for (chan
= csrow
->nr_channels
- 1; chan
>= 0; chan
--) {
439 if (!csrow
->channels
[chan
]->dimm
->nr_pages
)
441 device_remove_file(&csrow
->dev
,
442 dynamic_csrow_dimm_attr
[chan
]);
443 device_remove_file(&csrow
->dev
,
444 dynamic_csrow_ce_count_attr
[chan
]);
446 put_device(&mci
->csrows
[i
]->dev
);
452 static void edac_delete_csrow_objects(struct mem_ctl_info
*mci
)
455 struct csrow_info
*csrow
;
457 for (i
= mci
->nr_csrows
- 1; i
>= 0; i
--) {
458 csrow
= mci
->csrows
[i
];
459 if (!nr_pages_per_csrow(csrow
))
461 for (chan
= csrow
->nr_channels
- 1; chan
>= 0; chan
--) {
462 if (!csrow
->channels
[chan
]->dimm
->nr_pages
)
464 debugf1("Removing csrow %d channel %d sysfs nodes\n",
466 device_remove_file(&csrow
->dev
,
467 dynamic_csrow_dimm_attr
[chan
]);
468 device_remove_file(&csrow
->dev
,
469 dynamic_csrow_ce_count_attr
[chan
]);
471 put_device(&mci
->csrows
[i
]->dev
);
472 device_del(&mci
->csrows
[i
]->dev
);
478 * Per-dimm (or per-rank) devices
481 #define to_dimm(k) container_of(k, struct dimm_info, dev)
483 /* show/store functions for DIMM Label attributes */
484 static ssize_t
dimmdev_location_show(struct device
*dev
,
485 struct device_attribute
*mattr
, char *data
)
487 struct dimm_info
*dimm
= to_dimm(dev
);
488 struct mem_ctl_info
*mci
= dimm
->mci
;
492 for (i
= 0; i
< mci
->n_layers
; i
++) {
493 p
+= sprintf(p
, "%s %d ",
494 edac_layer_name
[mci
->layers
[i
].type
],
501 static ssize_t
dimmdev_label_show(struct device
*dev
,
502 struct device_attribute
*mattr
, char *data
)
504 struct dimm_info
*dimm
= to_dimm(dev
);
506 /* if field has not been initialized, there is nothing to send */
510 return snprintf(data
, EDAC_MC_LABEL_LEN
, "%s\n", dimm
->label
);
513 static ssize_t
dimmdev_label_store(struct device
*dev
,
514 struct device_attribute
*mattr
,
518 struct dimm_info
*dimm
= to_dimm(dev
);
520 ssize_t max_size
= 0;
522 max_size
= min((ssize_t
) count
, (ssize_t
) EDAC_MC_LABEL_LEN
- 1);
523 strncpy(dimm
->label
, data
, max_size
);
524 dimm
->label
[max_size
] = '\0';
529 static ssize_t
dimmdev_size_show(struct device
*dev
,
530 struct device_attribute
*mattr
, char *data
)
532 struct dimm_info
*dimm
= to_dimm(dev
);
534 return sprintf(data
, "%u\n", PAGES_TO_MiB(dimm
->nr_pages
));
537 static ssize_t
dimmdev_mem_type_show(struct device
*dev
,
538 struct device_attribute
*mattr
, char *data
)
540 struct dimm_info
*dimm
= to_dimm(dev
);
542 return sprintf(data
, "%s\n", mem_types
[dimm
->mtype
]);
545 static ssize_t
dimmdev_dev_type_show(struct device
*dev
,
546 struct device_attribute
*mattr
, char *data
)
548 struct dimm_info
*dimm
= to_dimm(dev
);
550 return sprintf(data
, "%s\n", dev_types
[dimm
->dtype
]);
553 static ssize_t
dimmdev_edac_mode_show(struct device
*dev
,
554 struct device_attribute
*mattr
,
557 struct dimm_info
*dimm
= to_dimm(dev
);
559 return sprintf(data
, "%s\n", edac_caps
[dimm
->edac_mode
]);
562 /* dimm/rank attribute files */
563 static DEVICE_ATTR(dimm_label
, S_IRUGO
| S_IWUSR
,
564 dimmdev_label_show
, dimmdev_label_store
);
565 static DEVICE_ATTR(dimm_location
, S_IRUGO
, dimmdev_location_show
, NULL
);
566 static DEVICE_ATTR(size
, S_IRUGO
, dimmdev_size_show
, NULL
);
567 static DEVICE_ATTR(dimm_mem_type
, S_IRUGO
, dimmdev_mem_type_show
, NULL
);
568 static DEVICE_ATTR(dimm_dev_type
, S_IRUGO
, dimmdev_dev_type_show
, NULL
);
569 static DEVICE_ATTR(dimm_edac_mode
, S_IRUGO
, dimmdev_edac_mode_show
, NULL
);
571 /* attributes of the dimm<id>/rank<id> object */
572 static struct attribute
*dimm_attrs
[] = {
573 &dev_attr_dimm_label
.attr
,
574 &dev_attr_dimm_location
.attr
,
576 &dev_attr_dimm_mem_type
.attr
,
577 &dev_attr_dimm_dev_type
.attr
,
578 &dev_attr_dimm_edac_mode
.attr
,
582 static struct attribute_group dimm_attr_grp
= {
586 static const struct attribute_group
*dimm_attr_groups
[] = {
591 static void dimm_attr_release(struct device
*dev
)
593 struct dimm_info
*dimm
= container_of(dev
, struct dimm_info
, dev
);
595 debugf1("Releasing dimm device %s\n", dev_name(dev
));
599 static struct device_type dimm_attr_type
= {
600 .groups
= dimm_attr_groups
,
601 .release
= dimm_attr_release
,
604 /* Create a DIMM object under specifed memory controller device */
605 static int edac_create_dimm_object(struct mem_ctl_info
*mci
,
606 struct dimm_info
*dimm
,
612 dimm
->dev
.type
= &dimm_attr_type
;
613 dimm
->dev
.bus
= &mci
->bus
;
614 device_initialize(&dimm
->dev
);
616 dimm
->dev
.parent
= &mci
->dev
;
617 if (mci
->mem_is_per_rank
)
618 dev_set_name(&dimm
->dev
, "rank%d", index
);
620 dev_set_name(&dimm
->dev
, "dimm%d", index
);
621 dev_set_drvdata(&dimm
->dev
, dimm
);
622 pm_runtime_forbid(&mci
->dev
);
624 err
= device_add(&dimm
->dev
);
626 debugf0("%s(): creating rank/dimm device %s\n", __func__
,
627 dev_name(&dimm
->dev
));
633 * Memory controller device
636 #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
638 static ssize_t
mci_reset_counters_store(struct device
*dev
,
639 struct device_attribute
*mattr
,
640 const char *data
, size_t count
)
642 struct mem_ctl_info
*mci
= to_mci(dev
);
643 int cnt
, row
, chan
, i
;
646 mci
->ue_noinfo_count
= 0;
647 mci
->ce_noinfo_count
= 0;
649 for (row
= 0; row
< mci
->nr_csrows
; row
++) {
650 struct csrow_info
*ri
= mci
->csrows
[row
];
655 for (chan
= 0; chan
< ri
->nr_channels
; chan
++)
656 ri
->channels
[chan
]->ce_count
= 0;
660 for (i
= 0; i
< mci
->n_layers
; i
++) {
661 cnt
*= mci
->layers
[i
].size
;
662 memset(mci
->ce_per_layer
[i
], 0, cnt
* sizeof(u32
));
663 memset(mci
->ue_per_layer
[i
], 0, cnt
* sizeof(u32
));
666 mci
->start_time
= jiffies
;
670 /* Memory scrubbing interface:
672 * A MC driver can limit the scrubbing bandwidth based on the CPU type.
673 * Therefore, ->set_sdram_scrub_rate should be made to return the actual
674 * bandwidth that is accepted or 0 when scrubbing is to be disabled.
676 * Negative value still means that an error has occurred while setting
679 static ssize_t
mci_sdram_scrub_rate_store(struct device
*dev
,
680 struct device_attribute
*mattr
,
681 const char *data
, size_t count
)
683 struct mem_ctl_info
*mci
= to_mci(dev
);
684 unsigned long bandwidth
= 0;
687 if (!mci
->set_sdram_scrub_rate
)
690 if (strict_strtoul(data
, 10, &bandwidth
) < 0)
693 new_bw
= mci
->set_sdram_scrub_rate(mci
, bandwidth
);
695 edac_printk(KERN_WARNING
, EDAC_MC
,
696 "Error setting scrub rate to: %lu\n", bandwidth
);
704 * ->get_sdram_scrub_rate() return value semantics same as above.
706 static ssize_t
mci_sdram_scrub_rate_show(struct device
*dev
,
707 struct device_attribute
*mattr
,
710 struct mem_ctl_info
*mci
= to_mci(dev
);
713 if (!mci
->get_sdram_scrub_rate
)
716 bandwidth
= mci
->get_sdram_scrub_rate(mci
);
718 edac_printk(KERN_DEBUG
, EDAC_MC
, "Error reading scrub rate\n");
722 return sprintf(data
, "%d\n", bandwidth
);
725 /* default attribute files for the MCI object */
726 static ssize_t
mci_ue_count_show(struct device
*dev
,
727 struct device_attribute
*mattr
,
730 struct mem_ctl_info
*mci
= to_mci(dev
);
732 return sprintf(data
, "%d\n", mci
->ue_mc
);
735 static ssize_t
mci_ce_count_show(struct device
*dev
,
736 struct device_attribute
*mattr
,
739 struct mem_ctl_info
*mci
= to_mci(dev
);
741 return sprintf(data
, "%d\n", mci
->ce_mc
);
744 static ssize_t
mci_ce_noinfo_show(struct device
*dev
,
745 struct device_attribute
*mattr
,
748 struct mem_ctl_info
*mci
= to_mci(dev
);
750 return sprintf(data
, "%d\n", mci
->ce_noinfo_count
);
753 static ssize_t
mci_ue_noinfo_show(struct device
*dev
,
754 struct device_attribute
*mattr
,
757 struct mem_ctl_info
*mci
= to_mci(dev
);
759 return sprintf(data
, "%d\n", mci
->ue_noinfo_count
);
762 static ssize_t
mci_seconds_show(struct device
*dev
,
763 struct device_attribute
*mattr
,
766 struct mem_ctl_info
*mci
= to_mci(dev
);
768 return sprintf(data
, "%ld\n", (jiffies
- mci
->start_time
) / HZ
);
771 static ssize_t
mci_ctl_name_show(struct device
*dev
,
772 struct device_attribute
*mattr
,
775 struct mem_ctl_info
*mci
= to_mci(dev
);
777 return sprintf(data
, "%s\n", mci
->ctl_name
);
780 static ssize_t
mci_size_mb_show(struct device
*dev
,
781 struct device_attribute
*mattr
,
784 struct mem_ctl_info
*mci
= to_mci(dev
);
785 int total_pages
= 0, csrow_idx
, j
;
787 for (csrow_idx
= 0; csrow_idx
< mci
->nr_csrows
; csrow_idx
++) {
788 struct csrow_info
*csrow
= mci
->csrows
[csrow_idx
];
790 for (j
= 0; j
< csrow
->nr_channels
; j
++) {
791 struct dimm_info
*dimm
= csrow
->channels
[j
]->dimm
;
793 total_pages
+= dimm
->nr_pages
;
797 return sprintf(data
, "%u\n", PAGES_TO_MiB(total_pages
));
800 static ssize_t
mci_max_location_show(struct device
*dev
,
801 struct device_attribute
*mattr
,
804 struct mem_ctl_info
*mci
= to_mci(dev
);
808 for (i
= 0; i
< mci
->n_layers
; i
++) {
809 p
+= sprintf(p
, "%s %d ",
810 edac_layer_name
[mci
->layers
[i
].type
],
811 mci
->layers
[i
].size
- 1);
817 #ifdef CONFIG_EDAC_DEBUG
818 static ssize_t
edac_fake_inject_write(struct file
*file
,
819 const char __user
*data
,
820 size_t count
, loff_t
*ppos
)
822 struct device
*dev
= file
->private_data
;
823 struct mem_ctl_info
*mci
= to_mci(dev
);
824 static enum hw_event_mc_err_type type
;
826 type
= mci
->fake_inject_ue
? HW_EVENT_ERR_UNCORRECTED
827 : HW_EVENT_ERR_CORRECTED
;
830 "Generating a %s fake error to %d.%d.%d to test core handling. NOTE: this won't test the driver-specific decoding logic.\n",
831 (type
== HW_EVENT_ERR_UNCORRECTED
) ? "UE" : "CE",
832 mci
->fake_inject_layer
[0],
833 mci
->fake_inject_layer
[1],
834 mci
->fake_inject_layer
[2]
836 edac_mc_handle_error(type
, mci
, 0, 0, 0,
837 mci
->fake_inject_layer
[0],
838 mci
->fake_inject_layer
[1],
839 mci
->fake_inject_layer
[2],
840 "FAKE ERROR", "for EDAC testing only", NULL
);
845 static int debugfs_open(struct inode
*inode
, struct file
*file
)
847 file
->private_data
= inode
->i_private
;
851 static const struct file_operations debug_fake_inject_fops
= {
852 .open
= debugfs_open
,
853 .write
= edac_fake_inject_write
,
854 .llseek
= generic_file_llseek
,
858 /* default Control file */
859 DEVICE_ATTR(reset_counters
, S_IWUSR
, NULL
, mci_reset_counters_store
);
861 /* default Attribute files */
862 DEVICE_ATTR(mc_name
, S_IRUGO
, mci_ctl_name_show
, NULL
);
863 DEVICE_ATTR(size_mb
, S_IRUGO
, mci_size_mb_show
, NULL
);
864 DEVICE_ATTR(seconds_since_reset
, S_IRUGO
, mci_seconds_show
, NULL
);
865 DEVICE_ATTR(ue_noinfo_count
, S_IRUGO
, mci_ue_noinfo_show
, NULL
);
866 DEVICE_ATTR(ce_noinfo_count
, S_IRUGO
, mci_ce_noinfo_show
, NULL
);
867 DEVICE_ATTR(ue_count
, S_IRUGO
, mci_ue_count_show
, NULL
);
868 DEVICE_ATTR(ce_count
, S_IRUGO
, mci_ce_count_show
, NULL
);
869 DEVICE_ATTR(max_location
, S_IRUGO
, mci_max_location_show
, NULL
);
871 /* memory scrubber attribute file */
872 DEVICE_ATTR(sdram_scrub_rate
, S_IRUGO
| S_IWUSR
, mci_sdram_scrub_rate_show
,
873 mci_sdram_scrub_rate_store
);
875 static struct attribute
*mci_attrs
[] = {
876 &dev_attr_reset_counters
.attr
,
877 &dev_attr_mc_name
.attr
,
878 &dev_attr_size_mb
.attr
,
879 &dev_attr_seconds_since_reset
.attr
,
880 &dev_attr_ue_noinfo_count
.attr
,
881 &dev_attr_ce_noinfo_count
.attr
,
882 &dev_attr_ue_count
.attr
,
883 &dev_attr_ce_count
.attr
,
884 &dev_attr_sdram_scrub_rate
.attr
,
885 &dev_attr_max_location
.attr
,
889 static struct attribute_group mci_attr_grp
= {
893 static const struct attribute_group
*mci_attr_groups
[] = {
898 static void mci_attr_release(struct device
*dev
)
900 struct mem_ctl_info
*mci
= container_of(dev
, struct mem_ctl_info
, dev
);
902 debugf1("Releasing csrow device %s\n", dev_name(dev
));
906 static struct device_type mci_attr_type
= {
907 .groups
= mci_attr_groups
,
908 .release
= mci_attr_release
,
911 #ifdef CONFIG_EDAC_DEBUG
912 int edac_create_debug_nodes(struct mem_ctl_info
*mci
)
914 struct dentry
*d
, *parent
;
918 d
= debugfs_create_dir(mci
->dev
.kobj
.name
, mci
->debugfs
);
923 for (i
= 0; i
< mci
->n_layers
; i
++) {
924 sprintf(name
, "fake_inject_%s",
925 edac_layer_name
[mci
->layers
[i
].type
]);
926 d
= debugfs_create_u8(name
, S_IRUGO
| S_IWUSR
, parent
,
927 &mci
->fake_inject_layer
[i
]);
932 d
= debugfs_create_bool("fake_inject_ue", S_IRUGO
| S_IWUSR
, parent
,
933 &mci
->fake_inject_ue
);
937 d
= debugfs_create_file("fake_inject", S_IWUSR
, parent
,
939 &debug_fake_inject_fops
);
945 debugfs_remove(mci
->debugfs
);
951 * Create a new Memory Controller kobject instance,
952 * mc<id> under the 'mc' directory
958 int edac_create_sysfs_mci_device(struct mem_ctl_info
*mci
)
963 * The memory controller needs its own bus, in order to avoid
964 * namespace conflicts at /sys/bus/edac.
966 mci
->bus
.name
= kasprintf(GFP_KERNEL
, "mc%d", mci
->mc_idx
);
969 debugf0("creating bus %s\n",mci
->bus
.name
);
970 err
= bus_register(&mci
->bus
);
974 /* get the /sys/devices/system/edac subsys reference */
975 mci
->dev
.type
= &mci_attr_type
;
976 device_initialize(&mci
->dev
);
978 mci
->dev
.parent
= mci_pdev
;
979 mci
->dev
.bus
= &mci
->bus
;
980 dev_set_name(&mci
->dev
, "mc%d", mci
->mc_idx
);
981 dev_set_drvdata(&mci
->dev
, mci
);
982 pm_runtime_forbid(&mci
->dev
);
984 debugf0("%s(): creating device %s\n", __func__
,
985 dev_name(&mci
->dev
));
986 err
= device_add(&mci
->dev
);
988 bus_unregister(&mci
->bus
);
989 kfree(mci
->bus
.name
);
994 * Create the dimm/rank devices
996 for (i
= 0; i
< mci
->tot_dimms
; i
++) {
997 struct dimm_info
*dimm
= mci
->dimms
[i
];
998 /* Only expose populated DIMMs */
999 if (dimm
->nr_pages
== 0)
1001 #ifdef CONFIG_EDAC_DEBUG
1002 debugf1("%s creating dimm%d, located at ",
1004 if (edac_debug_level
>= 1) {
1006 for (lay
= 0; lay
< mci
->n_layers
; lay
++)
1007 printk(KERN_CONT
"%s %d ",
1008 edac_layer_name
[mci
->layers
[lay
].type
],
1009 dimm
->location
[lay
]);
1010 printk(KERN_CONT
"\n");
1013 err
= edac_create_dimm_object(mci
, dimm
, i
);
1015 debugf1("%s() failure: create dimm %d obj\n",
1021 #ifdef CONFIG_EDAC_LEGACY_SYSFS
1022 err
= edac_create_csrow_objects(mci
);
1027 #ifdef CONFIG_EDAC_DEBUG
1028 edac_create_debug_nodes(mci
);
1033 for (i
--; i
>= 0; i
--) {
1034 struct dimm_info
*dimm
= mci
->dimms
[i
];
1035 if (dimm
->nr_pages
== 0)
1037 put_device(&dimm
->dev
);
1038 device_del(&dimm
->dev
);
1040 put_device(&mci
->dev
);
1041 device_del(&mci
->dev
);
1042 bus_unregister(&mci
->bus
);
1043 kfree(mci
->bus
.name
);
1048 * remove a Memory Controller instance
1050 void edac_remove_sysfs_mci_device(struct mem_ctl_info
*mci
)
1054 debugf0("%s()\n", __func__
);
1056 #ifdef CONFIG_EDAC_DEBUG
1057 debugfs_remove(mci
->debugfs
);
1059 #ifdef CONFIG_EDAC_LEGACY_SYSFS
1060 edac_delete_csrow_objects(mci
);
1063 for (i
= 0; i
< mci
->tot_dimms
; i
++) {
1064 struct dimm_info
*dimm
= mci
->dimms
[i
];
1065 if (dimm
->nr_pages
== 0)
1067 debugf0("%s(): removing device %s\n", __func__
,
1068 dev_name(&dimm
->dev
));
1069 put_device(&dimm
->dev
);
1070 device_del(&dimm
->dev
);
1074 void edac_unregister_sysfs(struct mem_ctl_info
*mci
)
1076 debugf1("Unregistering device %s\n", dev_name(&mci
->dev
));
1077 put_device(&mci
->dev
);
1078 device_del(&mci
->dev
);
1079 bus_unregister(&mci
->bus
);
1080 kfree(mci
->bus
.name
);
1083 static void mc_attr_release(struct device
*dev
)
1086 * There's no container structure here, as this is just the mci
1087 * parent device, used to create the /sys/devices/mc sysfs node.
1088 * So, there are no attributes on it.
1090 debugf1("Releasing device %s\n", dev_name(dev
));
1094 static struct device_type mc_attr_type
= {
1095 .release
= mc_attr_release
,
1098 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1100 int __init
edac_mc_sysfs_init(void)
1102 struct bus_type
*edac_subsys
;
1105 /* get the /sys/devices/system/edac subsys reference */
1106 edac_subsys
= edac_get_sysfs_subsys();
1107 if (edac_subsys
== NULL
) {
1108 debugf1("%s() no edac_subsys\n", __func__
);
1112 mci_pdev
= kzalloc(sizeof(*mci_pdev
), GFP_KERNEL
);
1114 mci_pdev
->bus
= edac_subsys
;
1115 mci_pdev
->type
= &mc_attr_type
;
1116 device_initialize(mci_pdev
);
1117 dev_set_name(mci_pdev
, "mc");
1119 err
= device_add(mci_pdev
);
1123 debugf0("device %s created\n", dev_name(mci_pdev
));
1128 void __exit
edac_mc_sysfs_exit(void)
1130 put_device(mci_pdev
);
1131 device_del(mci_pdev
);
1132 edac_put_sysfs_subsys();