2 * edac_mc kernel module
3 * (C) 2005, 2006 Linux Networx (http://lnxi.com)
4 * This file may be distributed under the terms of the
5 * GNU General Public License.
7 * Written by Thayne Harbaugh
8 * Based on work by Dan Hollis <goemon at anime dot net> and others.
9 * http://www.anime.net/~goemon/linux-ecc/
11 * Modified by Dave Peterson and Doug Thompson
15 #include <linux/module.h>
16 #include <linux/proc_fs.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/smp.h>
20 #include <linux/init.h>
21 #include <linux/sysctl.h>
22 #include <linux/highmem.h>
23 #include <linux/timer.h>
24 #include <linux/slab.h>
25 #include <linux/jiffies.h>
26 #include <linux/spinlock.h>
27 #include <linux/list.h>
28 #include <linux/sysdev.h>
29 #include <linux/ctype.h>
30 #include <asm/uaccess.h>
34 #include "edac_module.h"
37 /* lock to memory controller's control array */
38 static DECLARE_MUTEX(mem_ctls_mutex);
39 static struct list_head mc_devices = LIST_HEAD_INIT(mc_devices);
41 #ifdef CONFIG_EDAC_DEBUG
43 static void edac_mc_dump_channel(struct channel_info *chan)
45 debugf4("\tchannel = %p\n", chan);
46 debugf4("\tchannel->chan_idx = %d\n", chan->chan_idx);
47 debugf4("\tchannel->ce_count = %d\n", chan->ce_count);
48 debugf4("\tchannel->label = '%s'\n", chan->label);
49 debugf4("\tchannel->csrow = %p\n\n", chan->csrow);
52 static void edac_mc_dump_csrow(struct csrow_info *csrow)
54 debugf4("\tcsrow = %p\n", csrow);
55 debugf4("\tcsrow->csrow_idx = %d\n", csrow->csrow_idx);
56 debugf4("\tcsrow->first_page = 0x%lx\n",
58 debugf4("\tcsrow->last_page = 0x%lx\n", csrow->last_page);
59 debugf4("\tcsrow->page_mask = 0x%lx\n", csrow->page_mask);
60 debugf4("\tcsrow->nr_pages = 0x%x\n", csrow->nr_pages);
61 debugf4("\tcsrow->nr_channels = %d\n",
63 debugf4("\tcsrow->channels = %p\n", csrow->channels);
64 debugf4("\tcsrow->mci = %p\n\n", csrow->mci);
67 static void edac_mc_dump_mci(struct mem_ctl_info *mci)
69 debugf3("\tmci = %p\n", mci);
70 debugf3("\tmci->mtype_cap = %lx\n", mci->mtype_cap);
71 debugf3("\tmci->edac_ctl_cap = %lx\n", mci->edac_ctl_cap);
72 debugf3("\tmci->edac_cap = %lx\n", mci->edac_cap);
73 debugf4("\tmci->edac_check = %p\n", mci->edac_check);
74 debugf3("\tmci->nr_csrows = %d, csrows = %p\n",
75 mci->nr_csrows, mci->csrows);
76 debugf3("\tdev = %p\n", mci->dev);
77 debugf3("\tmod_name:ctl_name = %s:%s\n",
78 mci->mod_name, mci->ctl_name);
79 debugf3("\tpvt_info = %p\n\n", mci->pvt_info);
82 #endif /* CONFIG_EDAC_DEBUG */
84 /* 'ptr' points to a possibly unaligned item X such that sizeof(X) is 'size'.
85 * Adjust 'ptr' so that its alignment is at least as stringent as what the
86 * compiler would provide for X and return the aligned result.
88 * If 'size' is a constant, the compiler will optimize this whole function
89 * down to either a no-op or the addition of a constant to the value of 'ptr'.
91 char * edac_align_ptr(void *ptr, unsigned size)
95 /* Here we assume that the alignment of a "long long" is the most
96 * stringent alignment that the compiler will ever provide by default.
97 * As far as I know, this is a reasonable assumption.
99 if (size > sizeof(long))
100 align = sizeof(long long);
101 else if (size > sizeof(int))
102 align = sizeof(long);
103 else if (size > sizeof(short))
105 else if (size > sizeof(char))
106 align = sizeof(short);
115 return (char *) (((unsigned long) ptr) + align - r);
119 * edac_mc_alloc: Allocate a struct mem_ctl_info structure
120 * @size_pvt: size of private storage needed
121 * @nr_csrows: Number of CWROWS needed for this MC
122 * @nr_chans: Number of channels for the MC
124 * Everything is kmalloc'ed as one big chunk - more efficient.
125 * Only can be used if all structures have the same lifetime - otherwise
126 * you have to allocate and initialize your own structures.
128 * Use edac_mc_free() to free mc structures allocated by this function.
131 * NULL allocation failed
132 * struct mem_ctl_info pointer
134 struct mem_ctl_info *edac_mc_alloc(unsigned sz_pvt, unsigned nr_csrows,
137 struct mem_ctl_info *mci;
138 struct csrow_info *csi, *csrow;
139 struct channel_info *chi, *chp, *chan;
144 /* Figure out the offsets of the various items from the start of an mc
145 * structure. We want the alignment of each item to be at least as
146 * stringent as what the compiler would provide if we could simply
147 * hardcode everything into a single struct.
149 mci = (struct mem_ctl_info *) 0;
150 csi = (struct csrow_info *)edac_align_ptr(&mci[1], sizeof(*csi));
151 chi = (struct channel_info *)
152 edac_align_ptr(&csi[nr_csrows], sizeof(*chi));
153 pvt = edac_align_ptr(&chi[nr_chans * nr_csrows], sz_pvt);
154 size = ((unsigned long) pvt) + sz_pvt;
156 if ((mci = kmalloc(size, GFP_KERNEL)) == NULL)
159 /* Adjust pointers so they point within the memory we just allocated
160 * rather than an imaginary chunk of memory located at address 0.
162 csi = (struct csrow_info *) (((char *) mci) + ((unsigned long) csi));
163 chi = (struct channel_info *) (((char *) mci) + ((unsigned long) chi));
164 pvt = sz_pvt ? (((char *) mci) + ((unsigned long) pvt)) : NULL;
166 memset(mci, 0, size); /* clear all fields */
169 mci->nr_csrows = nr_csrows;
171 for (row = 0; row < nr_csrows; row++) {
173 csrow->csrow_idx = row;
175 csrow->nr_channels = nr_chans;
176 chp = &chi[row * nr_chans];
177 csrow->channels = chp;
179 for (chn = 0; chn < nr_chans; chn++) {
181 chan->chan_idx = chn;
188 EXPORT_SYMBOL_GPL(edac_mc_alloc);
191 * edac_mc_free: Free a previously allocated 'mci' structure
192 * @mci: pointer to a struct mem_ctl_info structure
194 void edac_mc_free(struct mem_ctl_info *mci)
198 EXPORT_SYMBOL_GPL(edac_mc_free);
200 static struct mem_ctl_info *find_mci_by_dev(struct device *dev)
202 struct mem_ctl_info *mci;
203 struct list_head *item;
205 debugf3("%s()\n", __func__);
207 list_for_each(item, &mc_devices) {
208 mci = list_entry(item, struct mem_ctl_info, link);
217 /* Return 0 on success, 1 on failure.
218 * Before calling this function, caller must
219 * assign a unique value to mci->mc_idx.
221 static int add_mc_to_global_list (struct mem_ctl_info *mci)
223 struct list_head *item, *insert_before;
224 struct mem_ctl_info *p;
226 insert_before = &mc_devices;
228 if (unlikely((p = find_mci_by_dev(mci->dev)) != NULL))
231 list_for_each(item, &mc_devices) {
232 p = list_entry(item, struct mem_ctl_info, link);
234 if (p->mc_idx >= mci->mc_idx) {
235 if (unlikely(p->mc_idx == mci->mc_idx))
238 insert_before = item;
243 list_add_tail_rcu(&mci->link, insert_before);
247 edac_printk(KERN_WARNING, EDAC_MC,
248 "%s (%s) %s %s already assigned %d\n", p->dev->bus_id,
249 dev_name(p->dev), p->mod_name, p->ctl_name, p->mc_idx);
253 edac_printk(KERN_WARNING, EDAC_MC,
254 "bug in low-level driver: attempt to assign\n"
255 " duplicate mc_idx %d in %s()\n", p->mc_idx, __func__);
259 static void complete_mc_list_del(struct rcu_head *head)
261 struct mem_ctl_info *mci;
263 mci = container_of(head, struct mem_ctl_info, rcu);
264 INIT_LIST_HEAD(&mci->link);
265 complete(&mci->complete);
268 static void del_mc_from_global_list(struct mem_ctl_info *mci)
270 list_del_rcu(&mci->link);
271 init_completion(&mci->complete);
272 call_rcu(&mci->rcu, complete_mc_list_del);
273 wait_for_completion(&mci->complete);
277 * edac_mc_find: Search for a mem_ctl_info structure whose index is 'idx'.
279 * If found, return a pointer to the structure.
282 * Caller must hold mem_ctls_mutex.
284 struct mem_ctl_info * edac_mc_find(int idx)
286 struct list_head *item;
287 struct mem_ctl_info *mci;
289 list_for_each(item, &mc_devices) {
290 mci = list_entry(item, struct mem_ctl_info, link);
292 if (mci->mc_idx >= idx) {
293 if (mci->mc_idx == idx)
302 EXPORT_SYMBOL(edac_mc_find);
305 * edac_mc_add_mc: Insert the 'mci' structure into the mci global list and
306 * create sysfs entries associated with mci structure
307 * @mci: pointer to the mci structure to be added to the list
308 * @mc_idx: A unique numeric identifier to be assigned to the 'mci' structure.
315 /* FIXME - should a warning be printed if no error detection? correction? */
316 int edac_mc_add_mc(struct mem_ctl_info *mci, int mc_idx)
318 debugf0("%s()\n", __func__);
319 mci->mc_idx = mc_idx;
320 #ifdef CONFIG_EDAC_DEBUG
321 if (edac_debug_level >= 3)
322 edac_mc_dump_mci(mci);
324 if (edac_debug_level >= 4) {
327 for (i = 0; i < mci->nr_csrows; i++) {
330 edac_mc_dump_csrow(&mci->csrows[i]);
331 for (j = 0; j < mci->csrows[i].nr_channels; j++)
332 edac_mc_dump_channel(
333 &mci->csrows[i].channels[j]);
337 down(&mem_ctls_mutex);
339 if (add_mc_to_global_list(mci))
342 /* set load time so that error rate can be tracked */
343 mci->start_time = jiffies;
345 if (edac_create_sysfs_mci_device(mci)) {
346 edac_mc_printk(mci, KERN_WARNING,
347 "failed to create sysfs device\n");
351 /* Report action taken */
352 edac_mc_printk(mci, KERN_INFO, "Giving out device to %s %s: DEV %s\n",
353 mci->mod_name, mci->ctl_name, dev_name(mci->dev));
359 del_mc_from_global_list(mci);
365 EXPORT_SYMBOL_GPL(edac_mc_add_mc);
368 * edac_mc_del_mc: Remove sysfs entries for specified mci structure and
369 * remove mci structure from global list
370 * @pdev: Pointer to 'struct device' representing mci structure to remove.
372 * Return pointer to removed mci structure, or NULL if device not found.
374 struct mem_ctl_info * edac_mc_del_mc(struct device *dev)
376 struct mem_ctl_info *mci;
378 debugf0("MC: %s()\n", __func__);
379 down(&mem_ctls_mutex);
381 if ((mci = find_mci_by_dev(dev)) == NULL) {
386 edac_remove_sysfs_mci_device(mci);
387 del_mc_from_global_list(mci);
389 edac_printk(KERN_INFO, EDAC_MC,
390 "Removed device %d for %s %s: DEV %s\n", mci->mc_idx,
391 mci->mod_name, mci->ctl_name, dev_name(mci->dev));
394 EXPORT_SYMBOL_GPL(edac_mc_del_mc);
396 static void edac_mc_scrub_block(unsigned long page, unsigned long offset,
401 unsigned long flags = 0;
403 debugf3("%s()\n", __func__);
405 /* ECC error page was not in our memory. Ignore it. */
409 /* Find the actual page structure then map it and fix */
410 pg = pfn_to_page(page);
413 local_irq_save(flags);
415 virt_addr = kmap_atomic(pg, KM_BOUNCE_READ);
417 /* Perform architecture specific atomic scrub operation */
418 atomic_scrub(virt_addr + offset, size);
420 /* Unmap and complete */
421 kunmap_atomic(virt_addr, KM_BOUNCE_READ);
424 local_irq_restore(flags);
427 /* FIXME - should return -1 */
428 int edac_mc_find_csrow_by_page(struct mem_ctl_info *mci, unsigned long page)
430 struct csrow_info *csrows = mci->csrows;
433 debugf1("MC%d: %s(): 0x%lx\n", mci->mc_idx, __func__, page);
436 for (i = 0; i < mci->nr_csrows; i++) {
437 struct csrow_info *csrow = &csrows[i];
439 if (csrow->nr_pages == 0)
442 debugf3("MC%d: %s(): first(0x%lx) page(0x%lx) last(0x%lx) "
443 "mask(0x%lx)\n", mci->mc_idx, __func__,
444 csrow->first_page, page, csrow->last_page,
447 if ((page >= csrow->first_page) &&
448 (page <= csrow->last_page) &&
449 ((page & csrow->page_mask) ==
450 (csrow->first_page & csrow->page_mask))) {
457 edac_mc_printk(mci, KERN_ERR,
458 "could not look up page error address %lx\n",
459 (unsigned long) page);
463 EXPORT_SYMBOL_GPL(edac_mc_find_csrow_by_page);
465 /* FIXME - setable log (warning/emerg) levels */
466 /* FIXME - integrate with evlog: http://evlog.sourceforge.net/ */
467 void edac_mc_handle_ce(struct mem_ctl_info *mci,
468 unsigned long page_frame_number, unsigned long offset_in_page,
469 unsigned long syndrome, int row, int channel, const char *msg)
471 unsigned long remapped_page;
473 debugf3("MC%d: %s()\n", mci->mc_idx, __func__);
475 /* FIXME - maybe make panic on INTERNAL ERROR an option */
476 if (row >= mci->nr_csrows || row < 0) {
477 /* something is wrong */
478 edac_mc_printk(mci, KERN_ERR,
479 "INTERNAL ERROR: row out of range "
480 "(%d >= %d)\n", row, mci->nr_csrows);
481 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
485 if (channel >= mci->csrows[row].nr_channels || channel < 0) {
486 /* something is wrong */
487 edac_mc_printk(mci, KERN_ERR,
488 "INTERNAL ERROR: channel out of range "
489 "(%d >= %d)\n", channel,
490 mci->csrows[row].nr_channels);
491 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
495 if (edac_get_log_ce())
496 /* FIXME - put in DIMM location */
497 edac_mc_printk(mci, KERN_WARNING,
498 "CE page 0x%lx, offset 0x%lx, grain %d, syndrome "
499 "0x%lx, row %d, channel %d, label \"%s\": %s\n",
500 page_frame_number, offset_in_page,
501 mci->csrows[row].grain, syndrome, row, channel,
502 mci->csrows[row].channels[channel].label, msg);
505 mci->csrows[row].ce_count++;
506 mci->csrows[row].channels[channel].ce_count++;
508 if (mci->scrub_mode & SCRUB_SW_SRC) {
510 * Some MC's can remap memory so that it is still available
511 * at a different address when PCI devices map into memory.
512 * MC's that can't do this lose the memory where PCI devices
513 * are mapped. This mapping is MC dependant and so we call
514 * back into the MC driver for it to map the MC page to
515 * a physical (CPU) page which can then be mapped to a virtual
516 * page - which can then be scrubbed.
518 remapped_page = mci->ctl_page_to_phys ?
519 mci->ctl_page_to_phys(mci, page_frame_number) :
522 edac_mc_scrub_block(remapped_page, offset_in_page,
523 mci->csrows[row].grain);
526 EXPORT_SYMBOL_GPL(edac_mc_handle_ce);
528 void edac_mc_handle_ce_no_info(struct mem_ctl_info *mci, const char *msg)
530 if (edac_get_log_ce())
531 edac_mc_printk(mci, KERN_WARNING,
532 "CE - no information available: %s\n", msg);
534 mci->ce_noinfo_count++;
537 EXPORT_SYMBOL_GPL(edac_mc_handle_ce_no_info);
539 void edac_mc_handle_ue(struct mem_ctl_info *mci,
540 unsigned long page_frame_number, unsigned long offset_in_page,
541 int row, const char *msg)
543 int len = EDAC_MC_LABEL_LEN * 4;
544 char labels[len + 1];
549 debugf3("MC%d: %s()\n", mci->mc_idx, __func__);
551 /* FIXME - maybe make panic on INTERNAL ERROR an option */
552 if (row >= mci->nr_csrows || row < 0) {
553 /* something is wrong */
554 edac_mc_printk(mci, KERN_ERR,
555 "INTERNAL ERROR: row out of range "
556 "(%d >= %d)\n", row, mci->nr_csrows);
557 edac_mc_handle_ue_no_info(mci, "INTERNAL ERROR");
561 chars = snprintf(pos, len + 1, "%s",
562 mci->csrows[row].channels[0].label);
566 for (chan = 1; (chan < mci->csrows[row].nr_channels) && (len > 0);
568 chars = snprintf(pos, len + 1, ":%s",
569 mci->csrows[row].channels[chan].label);
574 if (edac_get_log_ue())
575 edac_mc_printk(mci, KERN_EMERG,
576 "UE page 0x%lx, offset 0x%lx, grain %d, row %d, "
577 "labels \"%s\": %s\n", page_frame_number,
578 offset_in_page, mci->csrows[row].grain, row, labels,
581 if (edac_get_panic_on_ue())
582 panic("EDAC MC%d: UE page 0x%lx, offset 0x%lx, grain %d, "
583 "row %d, labels \"%s\": %s\n", mci->mc_idx,
584 page_frame_number, offset_in_page,
585 mci->csrows[row].grain, row, labels, msg);
588 mci->csrows[row].ue_count++;
590 EXPORT_SYMBOL_GPL(edac_mc_handle_ue);
592 void edac_mc_handle_ue_no_info(struct mem_ctl_info *mci, const char *msg)
594 if (edac_get_panic_on_ue())
595 panic("EDAC MC%d: Uncorrected Error", mci->mc_idx);
597 if (edac_get_log_ue())
598 edac_mc_printk(mci, KERN_WARNING,
599 "UE - no information available: %s\n", msg);
600 mci->ue_noinfo_count++;
603 EXPORT_SYMBOL_GPL(edac_mc_handle_ue_no_info);
606 /*************************************************************
607 * On Fully Buffered DIMM modules, this help function is
608 * called to process UE events
610 void edac_mc_handle_fbd_ue(struct mem_ctl_info *mci,
612 unsigned int channela,
613 unsigned int channelb,
616 int len = EDAC_MC_LABEL_LEN * 4;
617 char labels[len + 1];
621 if (csrow >= mci->nr_csrows) {
622 /* something is wrong */
623 edac_mc_printk(mci, KERN_ERR,
624 "INTERNAL ERROR: row out of range (%d >= %d)\n",
625 csrow, mci->nr_csrows);
626 edac_mc_handle_ue_no_info(mci, "INTERNAL ERROR");
630 if (channela >= mci->csrows[csrow].nr_channels) {
631 /* something is wrong */
632 edac_mc_printk(mci, KERN_ERR,
633 "INTERNAL ERROR: channel-a out of range "
635 channela, mci->csrows[csrow].nr_channels);
636 edac_mc_handle_ue_no_info(mci, "INTERNAL ERROR");
640 if (channelb >= mci->csrows[csrow].nr_channels) {
641 /* something is wrong */
642 edac_mc_printk(mci, KERN_ERR,
643 "INTERNAL ERROR: channel-b out of range "
645 channelb, mci->csrows[csrow].nr_channels);
646 edac_mc_handle_ue_no_info(mci, "INTERNAL ERROR");
651 mci->csrows[csrow].ue_count++;
653 /* Generate the DIMM labels from the specified channels */
654 chars = snprintf(pos, len + 1, "%s",
655 mci->csrows[csrow].channels[channela].label);
656 len -= chars; pos += chars;
657 chars = snprintf(pos, len + 1, "-%s",
658 mci->csrows[csrow].channels[channelb].label);
660 if (edac_get_log_ue())
661 edac_mc_printk(mci, KERN_EMERG,
662 "UE row %d, channel-a= %d channel-b= %d "
663 "labels \"%s\": %s\n", csrow, channela, channelb,
666 if (edac_get_panic_on_ue())
667 panic("UE row %d, channel-a= %d channel-b= %d "
668 "labels \"%s\": %s\n", csrow, channela,
669 channelb, labels, msg);
671 EXPORT_SYMBOL(edac_mc_handle_fbd_ue);
673 /*************************************************************
674 * On Fully Buffered DIMM modules, this help function is
675 * called to process CE events
677 void edac_mc_handle_fbd_ce(struct mem_ctl_info *mci,
679 unsigned int channel,
683 /* Ensure boundary values */
684 if (csrow >= mci->nr_csrows) {
685 /* something is wrong */
686 edac_mc_printk(mci, KERN_ERR,
687 "INTERNAL ERROR: row out of range (%d >= %d)\n",
688 csrow, mci->nr_csrows);
689 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
692 if (channel >= mci->csrows[csrow].nr_channels) {
693 /* something is wrong */
694 edac_mc_printk(mci, KERN_ERR,
695 "INTERNAL ERROR: channel out of range (%d >= %d)\n",
696 channel, mci->csrows[csrow].nr_channels);
697 edac_mc_handle_ce_no_info(mci, "INTERNAL ERROR");
701 if (edac_get_log_ce())
702 /* FIXME - put in DIMM location */
703 edac_mc_printk(mci, KERN_WARNING,
704 "CE row %d, channel %d, label \"%s\": %s\n",
706 mci->csrows[csrow].channels[channel].label,
710 mci->csrows[csrow].ce_count++;
711 mci->csrows[csrow].channels[channel].ce_count++;
713 EXPORT_SYMBOL(edac_mc_handle_fbd_ce);
717 * Iterate over all MC instances and check for ECC, et al, errors
719 void edac_check_mc_devices(void)
721 struct list_head *item;
722 struct mem_ctl_info *mci;
724 debugf3("%s()\n", __func__);
725 down(&mem_ctls_mutex);
727 list_for_each(item, &mc_devices) {
728 mci = list_entry(item, struct mem_ctl_info, link);
730 if (mci->edac_check != NULL)
731 mci->edac_check(mci);