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("creating (virtual) csrow node %s\n", dev_name(&csrow->dev));
381 err = device_add(&csrow->dev);
385 for (chan = 0; chan < csrow->nr_channels; chan++) {
386 /* Only expose populated DIMMs */
387 if (!csrow->channels[chan]->dimm->nr_pages)
389 err = device_create_file(&csrow->dev,
390 dynamic_csrow_dimm_attr[chan]);
393 err = device_create_file(&csrow->dev,
394 dynamic_csrow_ce_count_attr[chan]);
396 device_remove_file(&csrow->dev,
397 dynamic_csrow_dimm_attr[chan]);
405 for (--chan; chan >= 0; chan--) {
406 device_remove_file(&csrow->dev,
407 dynamic_csrow_dimm_attr[chan]);
408 device_remove_file(&csrow->dev,
409 dynamic_csrow_ce_count_attr[chan]);
411 put_device(&csrow->dev);
416 /* Create a CSROW object under specifed edac_mc_device */
417 static int edac_create_csrow_objects(struct mem_ctl_info *mci)
420 struct csrow_info *csrow;
422 for (i = 0; i < mci->nr_csrows; i++) {
423 csrow = mci->csrows[i];
424 if (!nr_pages_per_csrow(csrow))
426 err = edac_create_csrow_object(mci, mci->csrows[i], i);
433 for (--i; i >= 0; i--) {
434 csrow = mci->csrows[i];
435 if (!nr_pages_per_csrow(csrow))
437 for (chan = csrow->nr_channels - 1; chan >= 0; chan--) {
438 if (!csrow->channels[chan]->dimm->nr_pages)
440 device_remove_file(&csrow->dev,
441 dynamic_csrow_dimm_attr[chan]);
442 device_remove_file(&csrow->dev,
443 dynamic_csrow_ce_count_attr[chan]);
445 put_device(&mci->csrows[i]->dev);
451 static void edac_delete_csrow_objects(struct mem_ctl_info *mci)
454 struct csrow_info *csrow;
456 for (i = mci->nr_csrows - 1; i >= 0; i--) {
457 csrow = mci->csrows[i];
458 if (!nr_pages_per_csrow(csrow))
460 for (chan = csrow->nr_channels - 1; chan >= 0; chan--) {
461 if (!csrow->channels[chan]->dimm->nr_pages)
463 debugf1("Removing csrow %d channel %d sysfs nodes\n",
465 device_remove_file(&csrow->dev,
466 dynamic_csrow_dimm_attr[chan]);
467 device_remove_file(&csrow->dev,
468 dynamic_csrow_ce_count_attr[chan]);
470 put_device(&mci->csrows[i]->dev);
471 device_del(&mci->csrows[i]->dev);
477 * Per-dimm (or per-rank) devices
480 #define to_dimm(k) container_of(k, struct dimm_info, dev)
482 /* show/store functions for DIMM Label attributes */
483 static ssize_t dimmdev_location_show(struct device *dev,
484 struct device_attribute *mattr, char *data)
486 struct dimm_info *dimm = to_dimm(dev);
487 struct mem_ctl_info *mci = dimm->mci;
491 for (i = 0; i < mci->n_layers; i++) {
492 p += sprintf(p, "%s %d ",
493 edac_layer_name[mci->layers[i].type],
500 static ssize_t dimmdev_label_show(struct device *dev,
501 struct device_attribute *mattr, char *data)
503 struct dimm_info *dimm = to_dimm(dev);
505 /* if field has not been initialized, there is nothing to send */
509 return snprintf(data, EDAC_MC_LABEL_LEN, "%s\n", dimm->label);
512 static ssize_t dimmdev_label_store(struct device *dev,
513 struct device_attribute *mattr,
517 struct dimm_info *dimm = to_dimm(dev);
519 ssize_t max_size = 0;
521 max_size = min((ssize_t) count, (ssize_t) EDAC_MC_LABEL_LEN - 1);
522 strncpy(dimm->label, data, max_size);
523 dimm->label[max_size] = '\0';
528 static ssize_t dimmdev_size_show(struct device *dev,
529 struct device_attribute *mattr, char *data)
531 struct dimm_info *dimm = to_dimm(dev);
533 return sprintf(data, "%u\n", PAGES_TO_MiB(dimm->nr_pages));
536 static ssize_t dimmdev_mem_type_show(struct device *dev,
537 struct device_attribute *mattr, char *data)
539 struct dimm_info *dimm = to_dimm(dev);
541 return sprintf(data, "%s\n", mem_types[dimm->mtype]);
544 static ssize_t dimmdev_dev_type_show(struct device *dev,
545 struct device_attribute *mattr, char *data)
547 struct dimm_info *dimm = to_dimm(dev);
549 return sprintf(data, "%s\n", dev_types[dimm->dtype]);
552 static ssize_t dimmdev_edac_mode_show(struct device *dev,
553 struct device_attribute *mattr,
556 struct dimm_info *dimm = to_dimm(dev);
558 return sprintf(data, "%s\n", edac_caps[dimm->edac_mode]);
561 /* dimm/rank attribute files */
562 static DEVICE_ATTR(dimm_label, S_IRUGO | S_IWUSR,
563 dimmdev_label_show, dimmdev_label_store);
564 static DEVICE_ATTR(dimm_location, S_IRUGO, dimmdev_location_show, NULL);
565 static DEVICE_ATTR(size, S_IRUGO, dimmdev_size_show, NULL);
566 static DEVICE_ATTR(dimm_mem_type, S_IRUGO, dimmdev_mem_type_show, NULL);
567 static DEVICE_ATTR(dimm_dev_type, S_IRUGO, dimmdev_dev_type_show, NULL);
568 static DEVICE_ATTR(dimm_edac_mode, S_IRUGO, dimmdev_edac_mode_show, NULL);
570 /* attributes of the dimm<id>/rank<id> object */
571 static struct attribute *dimm_attrs[] = {
572 &dev_attr_dimm_label.attr,
573 &dev_attr_dimm_location.attr,
575 &dev_attr_dimm_mem_type.attr,
576 &dev_attr_dimm_dev_type.attr,
577 &dev_attr_dimm_edac_mode.attr,
581 static struct attribute_group dimm_attr_grp = {
585 static const struct attribute_group *dimm_attr_groups[] = {
590 static void dimm_attr_release(struct device *dev)
592 struct dimm_info *dimm = container_of(dev, struct dimm_info, dev);
594 debugf1("Releasing dimm device %s\n", dev_name(dev));
598 static struct device_type dimm_attr_type = {
599 .groups = dimm_attr_groups,
600 .release = dimm_attr_release,
603 /* Create a DIMM object under specifed memory controller device */
604 static int edac_create_dimm_object(struct mem_ctl_info *mci,
605 struct dimm_info *dimm,
611 dimm->dev.type = &dimm_attr_type;
612 dimm->dev.bus = &mci->bus;
613 device_initialize(&dimm->dev);
615 dimm->dev.parent = &mci->dev;
616 if (mci->mem_is_per_rank)
617 dev_set_name(&dimm->dev, "rank%d", index);
619 dev_set_name(&dimm->dev, "dimm%d", index);
620 dev_set_drvdata(&dimm->dev, dimm);
621 pm_runtime_forbid(&mci->dev);
623 err = device_add(&dimm->dev);
625 debugf0("creating rank/dimm device %s\n", dev_name(&dimm->dev));
631 * Memory controller device
634 #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
636 static ssize_t mci_reset_counters_store(struct device *dev,
637 struct device_attribute *mattr,
638 const char *data, size_t count)
640 struct mem_ctl_info *mci = to_mci(dev);
641 int cnt, row, chan, i;
644 mci->ue_noinfo_count = 0;
645 mci->ce_noinfo_count = 0;
647 for (row = 0; row < mci->nr_csrows; row++) {
648 struct csrow_info *ri = mci->csrows[row];
653 for (chan = 0; chan < ri->nr_channels; chan++)
654 ri->channels[chan]->ce_count = 0;
658 for (i = 0; i < mci->n_layers; i++) {
659 cnt *= mci->layers[i].size;
660 memset(mci->ce_per_layer[i], 0, cnt * sizeof(u32));
661 memset(mci->ue_per_layer[i], 0, cnt * sizeof(u32));
664 mci->start_time = jiffies;
668 /* Memory scrubbing interface:
670 * A MC driver can limit the scrubbing bandwidth based on the CPU type.
671 * Therefore, ->set_sdram_scrub_rate should be made to return the actual
672 * bandwidth that is accepted or 0 when scrubbing is to be disabled.
674 * Negative value still means that an error has occurred while setting
677 static ssize_t mci_sdram_scrub_rate_store(struct device *dev,
678 struct device_attribute *mattr,
679 const char *data, size_t count)
681 struct mem_ctl_info *mci = to_mci(dev);
682 unsigned long bandwidth = 0;
685 if (!mci->set_sdram_scrub_rate)
688 if (strict_strtoul(data, 10, &bandwidth) < 0)
691 new_bw = mci->set_sdram_scrub_rate(mci, bandwidth);
693 edac_printk(KERN_WARNING, EDAC_MC,
694 "Error setting scrub rate to: %lu\n", bandwidth);
702 * ->get_sdram_scrub_rate() return value semantics same as above.
704 static ssize_t mci_sdram_scrub_rate_show(struct device *dev,
705 struct device_attribute *mattr,
708 struct mem_ctl_info *mci = to_mci(dev);
711 if (!mci->get_sdram_scrub_rate)
714 bandwidth = mci->get_sdram_scrub_rate(mci);
716 edac_printk(KERN_DEBUG, EDAC_MC, "Error reading scrub rate\n");
720 return sprintf(data, "%d\n", bandwidth);
723 /* default attribute files for the MCI object */
724 static ssize_t mci_ue_count_show(struct device *dev,
725 struct device_attribute *mattr,
728 struct mem_ctl_info *mci = to_mci(dev);
730 return sprintf(data, "%d\n", mci->ue_mc);
733 static ssize_t mci_ce_count_show(struct device *dev,
734 struct device_attribute *mattr,
737 struct mem_ctl_info *mci = to_mci(dev);
739 return sprintf(data, "%d\n", mci->ce_mc);
742 static ssize_t mci_ce_noinfo_show(struct device *dev,
743 struct device_attribute *mattr,
746 struct mem_ctl_info *mci = to_mci(dev);
748 return sprintf(data, "%d\n", mci->ce_noinfo_count);
751 static ssize_t mci_ue_noinfo_show(struct device *dev,
752 struct device_attribute *mattr,
755 struct mem_ctl_info *mci = to_mci(dev);
757 return sprintf(data, "%d\n", mci->ue_noinfo_count);
760 static ssize_t mci_seconds_show(struct device *dev,
761 struct device_attribute *mattr,
764 struct mem_ctl_info *mci = to_mci(dev);
766 return sprintf(data, "%ld\n", (jiffies - mci->start_time) / HZ);
769 static ssize_t mci_ctl_name_show(struct device *dev,
770 struct device_attribute *mattr,
773 struct mem_ctl_info *mci = to_mci(dev);
775 return sprintf(data, "%s\n", mci->ctl_name);
778 static ssize_t mci_size_mb_show(struct device *dev,
779 struct device_attribute *mattr,
782 struct mem_ctl_info *mci = to_mci(dev);
783 int total_pages = 0, csrow_idx, j;
785 for (csrow_idx = 0; csrow_idx < mci->nr_csrows; csrow_idx++) {
786 struct csrow_info *csrow = mci->csrows[csrow_idx];
788 for (j = 0; j < csrow->nr_channels; j++) {
789 struct dimm_info *dimm = csrow->channels[j]->dimm;
791 total_pages += dimm->nr_pages;
795 return sprintf(data, "%u\n", PAGES_TO_MiB(total_pages));
798 static ssize_t mci_max_location_show(struct device *dev,
799 struct device_attribute *mattr,
802 struct mem_ctl_info *mci = to_mci(dev);
806 for (i = 0; i < mci->n_layers; i++) {
807 p += sprintf(p, "%s %d ",
808 edac_layer_name[mci->layers[i].type],
809 mci->layers[i].size - 1);
815 #ifdef CONFIG_EDAC_DEBUG
816 static ssize_t edac_fake_inject_write(struct file *file,
817 const char __user *data,
818 size_t count, loff_t *ppos)
820 struct device *dev = file->private_data;
821 struct mem_ctl_info *mci = to_mci(dev);
822 static enum hw_event_mc_err_type type;
824 type = mci->fake_inject_ue ? HW_EVENT_ERR_UNCORRECTED
825 : HW_EVENT_ERR_CORRECTED;
828 "Generating a %s fake error to %d.%d.%d to test core handling. NOTE: this won't test the driver-specific decoding logic.\n",
829 (type == HW_EVENT_ERR_UNCORRECTED) ? "UE" : "CE",
830 mci->fake_inject_layer[0],
831 mci->fake_inject_layer[1],
832 mci->fake_inject_layer[2]
834 edac_mc_handle_error(type, mci, 0, 0, 0,
835 mci->fake_inject_layer[0],
836 mci->fake_inject_layer[1],
837 mci->fake_inject_layer[2],
838 "FAKE ERROR", "for EDAC testing only", NULL);
843 static int debugfs_open(struct inode *inode, struct file *file)
845 file->private_data = inode->i_private;
849 static const struct file_operations debug_fake_inject_fops = {
850 .open = debugfs_open,
851 .write = edac_fake_inject_write,
852 .llseek = generic_file_llseek,
856 /* default Control file */
857 DEVICE_ATTR(reset_counters, S_IWUSR, NULL, mci_reset_counters_store);
859 /* default Attribute files */
860 DEVICE_ATTR(mc_name, S_IRUGO, mci_ctl_name_show, NULL);
861 DEVICE_ATTR(size_mb, S_IRUGO, mci_size_mb_show, NULL);
862 DEVICE_ATTR(seconds_since_reset, S_IRUGO, mci_seconds_show, NULL);
863 DEVICE_ATTR(ue_noinfo_count, S_IRUGO, mci_ue_noinfo_show, NULL);
864 DEVICE_ATTR(ce_noinfo_count, S_IRUGO, mci_ce_noinfo_show, NULL);
865 DEVICE_ATTR(ue_count, S_IRUGO, mci_ue_count_show, NULL);
866 DEVICE_ATTR(ce_count, S_IRUGO, mci_ce_count_show, NULL);
867 DEVICE_ATTR(max_location, S_IRUGO, mci_max_location_show, NULL);
869 /* memory scrubber attribute file */
870 DEVICE_ATTR(sdram_scrub_rate, S_IRUGO | S_IWUSR, mci_sdram_scrub_rate_show,
871 mci_sdram_scrub_rate_store);
873 static struct attribute *mci_attrs[] = {
874 &dev_attr_reset_counters.attr,
875 &dev_attr_mc_name.attr,
876 &dev_attr_size_mb.attr,
877 &dev_attr_seconds_since_reset.attr,
878 &dev_attr_ue_noinfo_count.attr,
879 &dev_attr_ce_noinfo_count.attr,
880 &dev_attr_ue_count.attr,
881 &dev_attr_ce_count.attr,
882 &dev_attr_sdram_scrub_rate.attr,
883 &dev_attr_max_location.attr,
887 static struct attribute_group mci_attr_grp = {
891 static const struct attribute_group *mci_attr_groups[] = {
896 static void mci_attr_release(struct device *dev)
898 struct mem_ctl_info *mci = container_of(dev, struct mem_ctl_info, dev);
900 debugf1("Releasing csrow device %s\n", dev_name(dev));
904 static struct device_type mci_attr_type = {
905 .groups = mci_attr_groups,
906 .release = mci_attr_release,
909 #ifdef CONFIG_EDAC_DEBUG
910 int edac_create_debug_nodes(struct mem_ctl_info *mci)
912 struct dentry *d, *parent;
916 d = debugfs_create_dir(mci->dev.kobj.name, mci->debugfs);
921 for (i = 0; i < mci->n_layers; i++) {
922 sprintf(name, "fake_inject_%s",
923 edac_layer_name[mci->layers[i].type]);
924 d = debugfs_create_u8(name, S_IRUGO | S_IWUSR, parent,
925 &mci->fake_inject_layer[i]);
930 d = debugfs_create_bool("fake_inject_ue", S_IRUGO | S_IWUSR, parent,
931 &mci->fake_inject_ue);
935 d = debugfs_create_file("fake_inject", S_IWUSR, parent,
937 &debug_fake_inject_fops);
943 debugfs_remove(mci->debugfs);
949 * Create a new Memory Controller kobject instance,
950 * mc<id> under the 'mc' directory
956 int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
961 * The memory controller needs its own bus, in order to avoid
962 * namespace conflicts at /sys/bus/edac.
964 mci->bus.name = kasprintf(GFP_KERNEL, "mc%d", mci->mc_idx);
967 debugf0("creating bus %s\n",mci->bus.name);
968 err = bus_register(&mci->bus);
972 /* get the /sys/devices/system/edac subsys reference */
973 mci->dev.type = &mci_attr_type;
974 device_initialize(&mci->dev);
976 mci->dev.parent = mci_pdev;
977 mci->dev.bus = &mci->bus;
978 dev_set_name(&mci->dev, "mc%d", mci->mc_idx);
979 dev_set_drvdata(&mci->dev, mci);
980 pm_runtime_forbid(&mci->dev);
982 debugf0("creating device %s\n", dev_name(&mci->dev));
983 err = device_add(&mci->dev);
985 bus_unregister(&mci->bus);
986 kfree(mci->bus.name);
991 * Create the dimm/rank devices
993 for (i = 0; i < mci->tot_dimms; i++) {
994 struct dimm_info *dimm = mci->dimms[i];
995 /* Only expose populated DIMMs */
996 if (dimm->nr_pages == 0)
998 #ifdef CONFIG_EDAC_DEBUG
999 debugf1("creating dimm%d, located at ",
1001 if (edac_debug_level >= 1) {
1003 for (lay = 0; lay < mci->n_layers; lay++)
1004 printk(KERN_CONT "%s %d ",
1005 edac_layer_name[mci->layers[lay].type],
1006 dimm->location[lay]);
1007 printk(KERN_CONT "\n");
1010 err = edac_create_dimm_object(mci, dimm, i);
1012 debugf1("failure: create dimm %d obj\n",
1018 #ifdef CONFIG_EDAC_LEGACY_SYSFS
1019 err = edac_create_csrow_objects(mci);
1024 #ifdef CONFIG_EDAC_DEBUG
1025 edac_create_debug_nodes(mci);
1030 for (i--; i >= 0; i--) {
1031 struct dimm_info *dimm = mci->dimms[i];
1032 if (dimm->nr_pages == 0)
1034 put_device(&dimm->dev);
1035 device_del(&dimm->dev);
1037 put_device(&mci->dev);
1038 device_del(&mci->dev);
1039 bus_unregister(&mci->bus);
1040 kfree(mci->bus.name);
1045 * remove a Memory Controller instance
1047 void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
1053 #ifdef CONFIG_EDAC_DEBUG
1054 debugfs_remove(mci->debugfs);
1056 #ifdef CONFIG_EDAC_LEGACY_SYSFS
1057 edac_delete_csrow_objects(mci);
1060 for (i = 0; i < mci->tot_dimms; i++) {
1061 struct dimm_info *dimm = mci->dimms[i];
1062 if (dimm->nr_pages == 0)
1064 debugf0("removing device %s\n", dev_name(&dimm->dev));
1065 put_device(&dimm->dev);
1066 device_del(&dimm->dev);
1070 void edac_unregister_sysfs(struct mem_ctl_info *mci)
1072 debugf1("Unregistering device %s\n", dev_name(&mci->dev));
1073 put_device(&mci->dev);
1074 device_del(&mci->dev);
1075 bus_unregister(&mci->bus);
1076 kfree(mci->bus.name);
1079 static void mc_attr_release(struct device *dev)
1082 * There's no container structure here, as this is just the mci
1083 * parent device, used to create the /sys/devices/mc sysfs node.
1084 * So, there are no attributes on it.
1086 debugf1("Releasing device %s\n", dev_name(dev));
1090 static struct device_type mc_attr_type = {
1091 .release = mc_attr_release,
1094 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1096 int __init edac_mc_sysfs_init(void)
1098 struct bus_type *edac_subsys;
1101 /* get the /sys/devices/system/edac subsys reference */
1102 edac_subsys = edac_get_sysfs_subsys();
1103 if (edac_subsys == NULL) {
1104 debugf1("no edac_subsys\n");
1108 mci_pdev = kzalloc(sizeof(*mci_pdev), GFP_KERNEL);
1110 mci_pdev->bus = edac_subsys;
1111 mci_pdev->type = &mc_attr_type;
1112 device_initialize(mci_pdev);
1113 dev_set_name(mci_pdev, "mc");
1115 err = device_add(mci_pdev);
1119 debugf0("device %s created\n", dev_name(mci_pdev));
1124 void __exit edac_mc_sysfs_exit(void)
1126 put_device(mci_pdev);
1127 device_del(mci_pdev);
1128 edac_put_sysfs_subsys();