1 /* Intel Sandy Bridge -EN/-EP/-EX Memory Controller kernel module
3 * This driver supports the memory controllers found on the Intel
4 * processor family Sandy Bridge.
6 * This file may be distributed under the terms of the
7 * GNU General Public License version 2 only.
9 * Copyright (c) 2011 by:
10 * Mauro Carvalho Chehab <mchehab@redhat.com>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/pci.h>
16 #include <linux/pci_ids.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/edac.h>
20 #include <linux/mmzone.h>
21 #include <linux/smp.h>
22 #include <linux/bitmap.h>
23 #include <linux/math64.h>
24 #include <asm/processor.h>
27 #include "edac_core.h"
30 static LIST_HEAD(sbridge_edac_list);
31 static DEFINE_MUTEX(sbridge_edac_lock);
35 * Alter this version for the module when modifications are made
37 #define SBRIDGE_REVISION " Ver: 1.0.0 "
38 #define EDAC_MOD_STR "sbridge_edac"
43 #define sbridge_printk(level, fmt, arg...) \
44 edac_printk(level, "sbridge", fmt, ##arg)
46 #define sbridge_mc_printk(mci, level, fmt, arg...) \
47 edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
50 * Get a bit field at register value <v>, from bit <lo> to bit <hi>
52 #define GET_BITFIELD(v, lo, hi) \
53 (((v) & ((1ULL << ((hi) - (lo) + 1)) - 1) << (lo)) >> (lo))
56 * sbridge Memory Controller Registers
60 * FIXME: For now, let's order by device function, as it makes
61 * easier for driver's development process. This table should be
62 * moved to pci_id.h when submitted upstream
64 #define PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0 0x3cf4 /* 12.6 */
65 #define PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1 0x3cf6 /* 12.7 */
66 #define PCI_DEVICE_ID_INTEL_SBRIDGE_BR 0x3cf5 /* 13.6 */
67 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0 0x3ca0 /* 14.0 */
68 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA 0x3ca8 /* 15.0 */
69 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS 0x3c71 /* 15.1 */
70 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0 0x3caa /* 15.2 */
71 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1 0x3cab /* 15.3 */
72 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2 0x3cac /* 15.4 */
73 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3 0x3cad /* 15.5 */
74 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO 0x3cb8 /* 17.0 */
77 * Currently, unused, but will be needed in the future
78 * implementations, as they hold the error counters
80 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR0 0x3c72 /* 16.2 */
81 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR1 0x3c73 /* 16.3 */
82 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR2 0x3c76 /* 16.6 */
83 #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR3 0x3c77 /* 16.7 */
85 /* Devices 12 Function 6, Offsets 0x80 to 0xcc */
86 static const u32 sbridge_dram_rule[] = {
87 0x80, 0x88, 0x90, 0x98, 0xa0,
88 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
91 #define SAD_LIMIT(reg) ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
92 #define DRAM_ATTR(reg) GET_BITFIELD(reg, 2, 3)
93 #define INTERLEAVE_MODE(reg) GET_BITFIELD(reg, 1, 1)
94 #define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
96 static char *get_dram_attr(u32 reg)
98 switch(DRAM_ATTR(reg)) {
110 static const u32 interleave_list[] = {
111 0x84, 0x8c, 0x94, 0x9c, 0xa4,
112 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
114 #define MAX_INTERLEAVE ARRAY_SIZE(interleave_list)
116 #define SAD_PKG0(reg) GET_BITFIELD(reg, 0, 2)
117 #define SAD_PKG1(reg) GET_BITFIELD(reg, 3, 5)
118 #define SAD_PKG2(reg) GET_BITFIELD(reg, 8, 10)
119 #define SAD_PKG3(reg) GET_BITFIELD(reg, 11, 13)
120 #define SAD_PKG4(reg) GET_BITFIELD(reg, 16, 18)
121 #define SAD_PKG5(reg) GET_BITFIELD(reg, 19, 21)
122 #define SAD_PKG6(reg) GET_BITFIELD(reg, 24, 26)
123 #define SAD_PKG7(reg) GET_BITFIELD(reg, 27, 29)
125 static inline int sad_pkg(u32 reg, int interleave)
127 switch (interleave) {
129 return SAD_PKG0(reg);
131 return SAD_PKG1(reg);
133 return SAD_PKG2(reg);
135 return SAD_PKG3(reg);
137 return SAD_PKG4(reg);
139 return SAD_PKG5(reg);
141 return SAD_PKG6(reg);
143 return SAD_PKG7(reg);
149 /* Devices 12 Function 7 */
154 #define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
155 #define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
157 /* Device 13 Function 6 */
159 #define SAD_TARGET 0xf0
161 #define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
163 #define SAD_CONTROL 0xf4
165 #define NODE_ID(reg) GET_BITFIELD(reg, 0, 2)
167 /* Device 14 function 0 */
169 static const u32 tad_dram_rule[] = {
170 0x40, 0x44, 0x48, 0x4c,
171 0x50, 0x54, 0x58, 0x5c,
172 0x60, 0x64, 0x68, 0x6c,
174 #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
176 #define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
177 #define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
178 #define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
179 #define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
180 #define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
181 #define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
182 #define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
184 /* Device 15, function 0 */
188 #define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
189 #define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
190 #define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
192 /* Device 15, function 1 */
194 #define RASENABLES 0xac
195 #define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
197 /* Device 15, functions 2-5 */
199 static const int mtr_regs[] = {
203 #define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
204 #define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
205 #define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
206 #define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
207 #define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
209 static const u32 tad_ch_nilv_offset[] = {
210 0x90, 0x94, 0x98, 0x9c,
211 0xa0, 0xa4, 0xa8, 0xac,
212 0xb0, 0xb4, 0xb8, 0xbc,
214 #define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
215 #define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
217 static const u32 rir_way_limit[] = {
218 0x108, 0x10c, 0x110, 0x114, 0x118,
220 #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
222 #define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
223 #define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
224 #define RIR_LIMIT(reg) ((GET_BITFIELD(reg, 1, 10) << 29)| 0x1fffffff)
226 #define MAX_RIR_WAY 8
228 static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
229 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
230 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
231 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
232 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
233 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
236 #define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
237 #define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
239 /* Device 16, functions 2-7 */
242 * FIXME: Implement the error count reads directly
245 static const u32 correrrcnt[] = {
246 0x104, 0x108, 0x10c, 0x110,
249 #define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
250 #define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
251 #define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
252 #define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
254 static const u32 correrrthrsld[] = {
255 0x11c, 0x120, 0x124, 0x128,
258 #define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
259 #define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
262 /* Device 17, function 0 */
264 #define SB_RANK_CFG_A 0x0328
266 #define IS_RDIMM_ENABLED(reg) GET_BITFIELD(reg, 11, 11)
272 #define NUM_CHANNELS 4
273 #define MAX_DIMMS 3 /* Max DIMMS per channel */
276 struct sbridge_info {
279 u64 (*get_tolm)(struct sbridge_pvt *pvt);
280 u64 (*get_tohm)(struct sbridge_pvt *pvt);
281 const u32 *dram_rule;
285 struct sbridge_channel {
290 struct pci_id_descr {
297 struct pci_id_table {
298 const struct pci_id_descr *descr;
303 struct list_head list;
305 u8 node_id, source_id;
306 struct pci_dev **pdev;
308 struct mem_ctl_info *mci;
312 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
313 struct pci_dev *pci_sad0, *pci_sad1, *pci_ha0;
314 struct pci_dev *pci_br0;
315 struct pci_dev *pci_tad[NUM_CHANNELS];
317 struct sbridge_dev *sbridge_dev;
319 struct sbridge_info info;
320 struct sbridge_channel channel[NUM_CHANNELS];
322 /* Memory type detection */
323 bool is_mirrored, is_lockstep, is_close_pg;
325 /* Fifo double buffers */
326 struct mce mce_entry[MCE_LOG_LEN];
327 struct mce mce_outentry[MCE_LOG_LEN];
329 /* Fifo in/out counters */
330 unsigned mce_in, mce_out;
332 /* Count indicator to show errors not got */
333 unsigned mce_overrun;
335 /* Memory description */
339 #define PCI_DESCR(device, function, device_id, opt) \
341 .func = (function), \
342 .dev_id = (device_id), \
345 static const struct pci_id_descr pci_dev_descr_sbridge[] = {
346 /* Processor Home Agent */
347 { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
349 /* Memory controller */
350 { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
351 { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
352 { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
353 { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
354 { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
355 { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
356 { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
358 /* System Address Decoder */
359 { PCI_DESCR(12, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
360 { PCI_DESCR(12, 7, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
362 /* Broadcast Registers */
363 { PCI_DESCR(13, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
366 #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
367 static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
368 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
369 {0,} /* 0 terminated list. */
373 * pci_device_id table for which devices we are looking for
375 static DEFINE_PCI_DEVICE_TABLE(sbridge_pci_tbl) = {
376 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA)},
377 {0,} /* 0 terminated list. */
381 /****************************************************************************
382 Ancillary status routines
383 ****************************************************************************/
385 static inline int numrank(u32 mtr)
387 int ranks = (1 << RANK_CNT_BITS(mtr));
390 edac_dbg(0, "Invalid number of ranks: %d (max = 4) raw value = %x (%04x)\n",
391 ranks, (unsigned int)RANK_CNT_BITS(mtr), mtr);
398 static inline int numrow(u32 mtr)
400 int rows = (RANK_WIDTH_BITS(mtr) + 12);
402 if (rows < 13 || rows > 18) {
403 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
404 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
411 static inline int numcol(u32 mtr)
413 int cols = (COL_WIDTH_BITS(mtr) + 10);
416 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
417 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
424 static struct sbridge_dev *get_sbridge_dev(u8 bus)
426 struct sbridge_dev *sbridge_dev;
428 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
429 if (sbridge_dev->bus == bus)
436 static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
437 const struct pci_id_table *table)
439 struct sbridge_dev *sbridge_dev;
441 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
445 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
447 if (!sbridge_dev->pdev) {
452 sbridge_dev->bus = bus;
453 sbridge_dev->n_devs = table->n_devs;
454 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
459 static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
461 list_del(&sbridge_dev->list);
462 kfree(sbridge_dev->pdev);
466 static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
470 /* Address range is 32:28 */
471 pci_read_config_dword(pvt->pci_sad1, TOLM, ®);
472 return GET_TOLM(reg);
475 static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
479 pci_read_config_dword(pvt->pci_sad1, TOHM, ®);
480 return GET_TOHM(reg);
483 /****************************************************************************
484 Memory check routines
485 ****************************************************************************/
486 static struct pci_dev *get_pdev_slot_func(u8 bus, unsigned slot,
489 struct sbridge_dev *sbridge_dev = get_sbridge_dev(bus);
495 for (i = 0; i < sbridge_dev->n_devs; i++) {
496 if (!sbridge_dev->pdev[i])
499 if (PCI_SLOT(sbridge_dev->pdev[i]->devfn) == slot &&
500 PCI_FUNC(sbridge_dev->pdev[i]->devfn) == func) {
501 edac_dbg(1, "Associated %02x.%02x.%d with %p\n",
502 bus, slot, func, sbridge_dev->pdev[i]);
503 return sbridge_dev->pdev[i];
511 * check_if_ecc_is_active() - Checks if ECC is active
514 static int check_if_ecc_is_active(const u8 bus)
516 struct pci_dev *pdev = NULL;
519 pdev = get_pdev_slot_func(bus, 15, 0);
521 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
527 pci_read_config_dword(pdev, MCMTR, &mcmtr);
528 if (!IS_ECC_ENABLED(mcmtr)) {
529 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
535 static int get_dimm_config(struct mem_ctl_info *mci)
537 struct sbridge_pvt *pvt = mci->pvt_info;
538 struct dimm_info *dimm;
539 unsigned i, j, banks, ranks, rows, cols, npages;
545 pvt->info.rankcfgr = SB_RANK_CFG_A;
547 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, ®);
548 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
550 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, ®);
551 pvt->sbridge_dev->node_id = NODE_ID(reg);
552 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
553 pvt->sbridge_dev->mc,
554 pvt->sbridge_dev->node_id,
555 pvt->sbridge_dev->source_id);
557 pci_read_config_dword(pvt->pci_ras, RASENABLES, ®);
558 if (IS_MIRROR_ENABLED(reg)) {
559 edac_dbg(0, "Memory mirror is enabled\n");
560 pvt->is_mirrored = true;
562 edac_dbg(0, "Memory mirror is disabled\n");
563 pvt->is_mirrored = false;
566 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
567 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
568 edac_dbg(0, "Lockstep is enabled\n");
569 mode = EDAC_S8ECD8ED;
570 pvt->is_lockstep = true;
572 edac_dbg(0, "Lockstep is disabled\n");
573 mode = EDAC_S4ECD4ED;
574 pvt->is_lockstep = false;
576 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
577 edac_dbg(0, "address map is on closed page mode\n");
578 pvt->is_close_pg = true;
580 edac_dbg(0, "address map is on open page mode\n");
581 pvt->is_close_pg = false;
584 if (pvt->pci_ddrio) {
585 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
587 if (IS_RDIMM_ENABLED(reg)) {
588 /* FIXME: Can also be LRDIMM */
589 edac_dbg(0, "Memory is registered\n");
592 edac_dbg(0, "Memory is unregistered\n");
596 edac_dbg(0, "Cannot determine memory type\n");
600 /* On all supported DDR3 DIMM types, there are 8 banks available */
603 for (i = 0; i < NUM_CHANNELS; i++) {
606 for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
607 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
609 pci_read_config_dword(pvt->pci_tad[i],
611 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
612 if (IS_DIMM_PRESENT(mtr)) {
613 pvt->channel[i].dimms++;
615 ranks = numrank(mtr);
619 /* DDR3 has 8 I/O banks */
620 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
621 npages = MiB_TO_PAGES(size);
623 edac_dbg(0, "mc#%d: channel %d, dimm %d, %Ld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
624 pvt->sbridge_dev->mc, i, j,
626 banks, ranks, rows, cols);
628 dimm->nr_pages = npages;
630 dimm->dtype = (banks == 8) ? DEV_X8 : DEV_X4;
632 dimm->edac_mode = mode;
633 snprintf(dimm->label, sizeof(dimm->label),
634 "CPU_SrcID#%u_Channel#%u_DIMM#%u",
635 pvt->sbridge_dev->source_id, i, j);
643 static void get_memory_layout(const struct mem_ctl_info *mci)
645 struct sbridge_pvt *pvt = mci->pvt_info;
646 int i, j, k, n_sads, n_tads, sad_interl;
654 * Step 1) Get TOLM/TOHM ranges
657 pvt->tolm = pvt->info.get_tolm(pvt);
658 tmp_mb = (1 + pvt->tolm) >> 20;
660 mb = div_u64_rem(tmp_mb, 1000, &kb);
661 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tolm);
663 /* Address range is already 45:25 */
664 pvt->tohm = pvt->info.get_tohm(pvt);
665 tmp_mb = (1 + pvt->tohm) >> 20;
667 mb = div_u64_rem(tmp_mb, 1000, &kb);
668 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tohm);
671 * Step 2) Get SAD range and SAD Interleave list
672 * TAD registers contain the interleave wayness. However, it
673 * seems simpler to just discover it indirectly, with the
677 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
678 /* SAD_LIMIT Address range is 45:26 */
679 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
681 limit = SAD_LIMIT(reg);
683 if (!DRAM_RULE_ENABLE(reg))
689 tmp_mb = (limit + 1) >> 20;
690 mb = div_u64_rem(tmp_mb, 1000, &kb);
691 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
695 ((u64)tmp_mb) << 20L,
696 INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
700 pci_read_config_dword(pvt->pci_sad0, interleave_list[n_sads],
702 sad_interl = sad_pkg(reg, 0);
703 for (j = 0; j < 8; j++) {
704 if (j > 0 && sad_interl == sad_pkg(reg, j))
707 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
708 n_sads, j, sad_pkg(reg, j));
713 * Step 3) Get TAD range
716 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
717 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
719 limit = TAD_LIMIT(reg);
722 tmp_mb = (limit + 1) >> 20;
724 mb = div_u64_rem(tmp_mb, 1000, &kb);
725 edac_dbg(0, "TAD#%d: up to %u.%03u GB (0x%016Lx), socket interleave %d, memory interleave %d, TGT: %d, %d, %d, %d, reg=0x%08x\n",
727 ((u64)tmp_mb) << 20L,
739 * Step 4) Get TAD offsets, per each channel
741 for (i = 0; i < NUM_CHANNELS; i++) {
742 if (!pvt->channel[i].dimms)
744 for (j = 0; j < n_tads; j++) {
745 pci_read_config_dword(pvt->pci_tad[i],
746 tad_ch_nilv_offset[j],
748 tmp_mb = TAD_OFFSET(reg) >> 20;
749 mb = div_u64_rem(tmp_mb, 1000, &kb);
750 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
753 ((u64)tmp_mb) << 20L,
759 * Step 6) Get RIR Wayness/Limit, per each channel
761 for (i = 0; i < NUM_CHANNELS; i++) {
762 if (!pvt->channel[i].dimms)
764 for (j = 0; j < MAX_RIR_RANGES; j++) {
765 pci_read_config_dword(pvt->pci_tad[i],
769 if (!IS_RIR_VALID(reg))
772 tmp_mb = RIR_LIMIT(reg) >> 20;
773 rir_way = 1 << RIR_WAY(reg);
774 mb = div_u64_rem(tmp_mb, 1000, &kb);
775 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
778 ((u64)tmp_mb) << 20L,
782 for (k = 0; k < rir_way; k++) {
783 pci_read_config_dword(pvt->pci_tad[i],
786 tmp_mb = RIR_OFFSET(reg) << 6;
788 mb = div_u64_rem(tmp_mb, 1000, &kb);
789 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
792 ((u64)tmp_mb) << 20L,
793 (u32)RIR_RNK_TGT(reg),
800 struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
802 struct sbridge_dev *sbridge_dev;
804 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
805 if (sbridge_dev->node_id == node_id)
806 return sbridge_dev->mci;
811 static int get_memory_error_data(struct mem_ctl_info *mci,
816 char **area_type, char *msg)
818 struct mem_ctl_info *new_mci;
819 struct sbridge_pvt *pvt = mci->pvt_info;
820 int n_rir, n_sads, n_tads, sad_way, sck_xch;
821 int sad_interl, idx, base_ch;
823 unsigned sad_interleave[MAX_INTERLEAVE];
829 u64 ch_addr, offset, limit, prv = 0;
833 * Step 0) Check if the address is at special memory ranges
834 * The check bellow is probably enough to fill all cases where
835 * the error is not inside a memory, except for the legacy
836 * range (e. g. VGA addresses). It is unlikely, however, that the
837 * memory controller would generate an error on that range.
839 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
840 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
843 if (addr >= (u64)pvt->tohm) {
844 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
851 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
852 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
855 if (!DRAM_RULE_ENABLE(reg))
858 limit = SAD_LIMIT(reg);
860 sprintf(msg, "Can't discover the memory socket");
867 if (n_sads == pvt->info.max_sad) {
868 sprintf(msg, "Can't discover the memory socket");
871 *area_type = get_dram_attr(reg);
872 interleave_mode = INTERLEAVE_MODE(reg);
874 pci_read_config_dword(pvt->pci_sad0, interleave_list[n_sads],
876 sad_interl = sad_pkg(reg, 0);
877 for (sad_way = 0; sad_way < 8; sad_way++) {
878 if (sad_way > 0 && sad_interl == sad_pkg(reg, sad_way))
880 sad_interleave[sad_way] = sad_pkg(reg, sad_way);
881 edac_dbg(0, "SAD interleave #%d: %d\n",
882 sad_way, sad_interleave[sad_way]);
884 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
885 pvt->sbridge_dev->mc,
890 interleave_mode ? "" : "XOR[18:16]");
892 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
894 idx = (addr >> 6) & 7;
908 sprintf(msg, "Can't discover socket interleave");
911 *socket = sad_interleave[idx];
912 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
913 idx, sad_way, *socket);
916 * Move to the proper node structure, in order to access the
917 * right PCI registers
919 new_mci = get_mci_for_node_id(*socket);
921 sprintf(msg, "Struct for socket #%u wasn't initialized",
929 * Step 2) Get memory channel
932 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
933 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
935 limit = TAD_LIMIT(reg);
937 sprintf(msg, "Can't discover the memory channel");
944 ch_way = TAD_CH(reg) + 1;
945 sck_way = TAD_SOCK(reg) + 1;
947 * FIXME: Is it right to always use channel 0 for offsets?
949 pci_read_config_dword(pvt->pci_tad[0],
950 tad_ch_nilv_offset[n_tads],
956 idx = addr >> (6 + sck_way);
960 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
964 base_ch = TAD_TGT0(reg);
967 base_ch = TAD_TGT1(reg);
970 base_ch = TAD_TGT2(reg);
973 base_ch = TAD_TGT3(reg);
976 sprintf(msg, "Can't discover the TAD target");
979 *channel_mask = 1 << base_ch;
981 if (pvt->is_mirrored) {
982 *channel_mask |= 1 << ((base_ch + 2) % 4);
986 sck_xch = 1 << sck_way * (ch_way >> 1);
989 sprintf(msg, "Invalid mirror set. Can't decode addr");
993 sck_xch = (1 << sck_way) * ch_way;
995 if (pvt->is_lockstep)
996 *channel_mask |= 1 << ((base_ch + 1) % 4);
998 offset = TAD_OFFSET(tad_offset);
1000 edac_dbg(0, "TAD#%d: address 0x%016Lx < 0x%016Lx, socket interleave %d, channel interleave %d (offset 0x%08Lx), index %d, base ch: %d, ch mask: 0x%02lx\n",
1011 /* Calculate channel address */
1012 /* Remove the TAD offset */
1014 if (offset > addr) {
1015 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
1020 /* Store the low bits [0:6] of the addr */
1021 ch_addr = addr & 0x7f;
1022 /* Remove socket wayness and remove 6 bits */
1024 addr = div_u64(addr, sck_xch);
1026 /* Divide by channel way */
1027 addr = addr / ch_way;
1029 /* Recover the last 6 bits */
1030 ch_addr |= addr << 6;
1033 * Step 3) Decode rank
1035 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
1036 pci_read_config_dword(pvt->pci_tad[base_ch],
1037 rir_way_limit[n_rir],
1040 if (!IS_RIR_VALID(reg))
1043 limit = RIR_LIMIT(reg);
1044 mb = div_u64_rem(limit >> 20, 1000, &kb);
1045 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
1050 if (ch_addr <= limit)
1053 if (n_rir == MAX_RIR_RANGES) {
1054 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
1058 rir_way = RIR_WAY(reg);
1059 if (pvt->is_close_pg)
1060 idx = (ch_addr >> 6);
1062 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
1063 idx %= 1 << rir_way;
1065 pci_read_config_dword(pvt->pci_tad[base_ch],
1066 rir_offset[n_rir][idx],
1068 *rank = RIR_RNK_TGT(reg);
1070 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
1080 /****************************************************************************
1081 Device initialization routines: put/get, init/exit
1082 ****************************************************************************/
1085 * sbridge_put_all_devices 'put' all the devices that we have
1086 * reserved via 'get'
1088 static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
1093 for (i = 0; i < sbridge_dev->n_devs; i++) {
1094 struct pci_dev *pdev = sbridge_dev->pdev[i];
1097 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
1099 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1104 static void sbridge_put_all_devices(void)
1106 struct sbridge_dev *sbridge_dev, *tmp;
1108 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
1109 sbridge_put_devices(sbridge_dev);
1110 free_sbridge_dev(sbridge_dev);
1115 * sbridge_get_all_devices Find and perform 'get' operation on the MCH's
1116 * device/functions we want to reference for this driver
1118 * Need to 'get' device 16 func 1 and func 2
1120 static int sbridge_get_onedevice(struct pci_dev **prev,
1122 const struct pci_id_table *table,
1123 const unsigned devno)
1125 struct sbridge_dev *sbridge_dev;
1126 const struct pci_id_descr *dev_descr = &table->descr[devno];
1128 struct pci_dev *pdev = NULL;
1131 sbridge_printk(KERN_INFO,
1132 "Seeking for: dev %02x.%d PCI ID %04x:%04x\n",
1133 dev_descr->dev, dev_descr->func,
1134 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1136 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1137 dev_descr->dev_id, *prev);
1145 if (dev_descr->optional)
1151 sbridge_printk(KERN_INFO,
1152 "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
1153 dev_descr->dev, dev_descr->func,
1154 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1156 /* End of list, leave */
1159 bus = pdev->bus->number;
1161 sbridge_dev = get_sbridge_dev(bus);
1163 sbridge_dev = alloc_sbridge_dev(bus, table);
1171 if (sbridge_dev->pdev[devno]) {
1172 sbridge_printk(KERN_ERR,
1173 "Duplicated device for "
1174 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1175 bus, dev_descr->dev, dev_descr->func,
1176 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1181 sbridge_dev->pdev[devno] = pdev;
1184 if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
1185 PCI_FUNC(pdev->devfn) != dev_descr->func)) {
1186 sbridge_printk(KERN_ERR,
1187 "Device PCI ID %04x:%04x "
1188 "has dev %02x:%d.%d instead of dev %02x:%02x.%d\n",
1189 PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
1190 bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1191 bus, dev_descr->dev, dev_descr->func);
1195 /* Be sure that the device is enabled */
1196 if (unlikely(pci_enable_device(pdev) < 0)) {
1197 sbridge_printk(KERN_ERR,
1199 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1200 bus, dev_descr->dev, dev_descr->func,
1201 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1205 edac_dbg(0, "Detected dev %02x:%d.%d PCI ID %04x:%04x\n",
1206 bus, dev_descr->dev, dev_descr->func,
1207 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1210 * As stated on drivers/pci/search.c, the reference count for
1211 * @from is always decremented if it is not %NULL. So, as we need
1212 * to get all devices up to null, we need to do a get for the device
1221 static int sbridge_get_all_devices(u8 *num_mc)
1224 struct pci_dev *pdev = NULL;
1225 const struct pci_id_table *table = pci_dev_descr_sbridge_table;
1227 while (table && table->descr) {
1228 for (i = 0; i < table->n_devs; i++) {
1231 rc = sbridge_get_onedevice(&pdev, num_mc,
1238 sbridge_put_all_devices();
1249 static int mci_bind_devs(struct mem_ctl_info *mci,
1250 struct sbridge_dev *sbridge_dev)
1252 struct sbridge_pvt *pvt = mci->pvt_info;
1253 struct pci_dev *pdev;
1256 for (i = 0; i < sbridge_dev->n_devs; i++) {
1257 pdev = sbridge_dev->pdev[i];
1260 slot = PCI_SLOT(pdev->devfn);
1261 func = PCI_FUNC(pdev->devfn);
1266 pvt->pci_sad0 = pdev;
1269 pvt->pci_sad1 = pdev;
1278 pvt->pci_br0 = pdev;
1287 pvt->pci_ha0 = pdev;
1299 pvt->pci_ras = pdev;
1305 pvt->pci_tad[func - 2] = pdev;
1314 pvt->pci_ddrio = pdev;
1324 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1326 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1330 /* Check if everything were registered */
1331 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
1332 !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
1335 for (i = 0; i < NUM_CHANNELS; i++) {
1336 if (!pvt->pci_tad[i])
1342 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1346 sbridge_printk(KERN_ERR, "Device %d, function %d "
1347 "is out of the expected range\n",
1352 /****************************************************************************
1353 Error check routines
1354 ****************************************************************************/
1357 * While Sandy Bridge has error count registers, SMI BIOS read values from
1358 * and resets the counters. So, they are not reliable for the OS to read
1359 * from them. So, we have no option but to just trust on whatever MCE is
1360 * telling us about the errors.
1362 static void sbridge_mce_output_error(struct mem_ctl_info *mci,
1363 const struct mce *m)
1365 struct mem_ctl_info *new_mci;
1366 struct sbridge_pvt *pvt = mci->pvt_info;
1367 enum hw_event_mc_err_type tp_event;
1368 char *type, *optype, msg[256];
1369 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
1370 bool overflow = GET_BITFIELD(m->status, 62, 62);
1371 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
1372 bool recoverable = GET_BITFIELD(m->status, 56, 56);
1373 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
1374 u32 mscod = GET_BITFIELD(m->status, 16, 31);
1375 u32 errcode = GET_BITFIELD(m->status, 0, 15);
1376 u32 channel = GET_BITFIELD(m->status, 0, 3);
1377 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
1378 long channel_mask, first_channel;
1381 char *area_type = NULL;
1383 if (uncorrected_error) {
1386 tp_event = HW_EVENT_ERR_FATAL;
1389 tp_event = HW_EVENT_ERR_UNCORRECTED;
1393 tp_event = HW_EVENT_ERR_CORRECTED;
1397 * According with Table 15-9 of the Intel Architecture spec vol 3A,
1398 * memory errors should fit in this mask:
1399 * 000f 0000 1mmm cccc (binary)
1401 * f = Correction Report Filtering Bit. If 1, subsequent errors
1405 * If the mask doesn't match, report an error to the parsing logic
1407 if (! ((errcode & 0xef80) == 0x80)) {
1408 optype = "Can't parse: it is not a mem";
1410 switch (optypenum) {
1412 optype = "generic undef request error";
1415 optype = "memory read error";
1418 optype = "memory write error";
1421 optype = "addr/cmd error";
1424 optype = "memory scrubbing error";
1427 optype = "reserved";
1432 rc = get_memory_error_data(mci, m->addr, &socket,
1433 &channel_mask, &rank, &area_type, msg);
1436 new_mci = get_mci_for_node_id(socket);
1438 strcpy(msg, "Error: socket got corrupted!");
1442 pvt = mci->pvt_info;
1444 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
1455 * FIXME: On some memory configurations (mirror, lockstep), the
1456 * Memory Controller can't point the error to a single DIMM. The
1457 * EDAC core should be handling the channel mask, in order to point
1458 * to the group of dimm's where the error may be happening.
1460 snprintf(msg, sizeof(msg),
1461 "%s%s area:%s err_code:%04x:%04x socket:%d channel_mask:%ld rank:%d",
1462 overflow ? " OVERFLOW" : "",
1463 (uncorrected_error && recoverable) ? " recoverable" : "",
1470 edac_dbg(0, "%s\n", msg);
1472 /* FIXME: need support for channel mask */
1474 /* Call the helper to output message */
1475 edac_mc_handle_error(tp_event, mci, core_err_cnt,
1476 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
1481 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
1488 * sbridge_check_error Retrieve and process errors reported by the
1489 * hardware. Called by the Core module.
1491 static void sbridge_check_error(struct mem_ctl_info *mci)
1493 struct sbridge_pvt *pvt = mci->pvt_info;
1499 * MCE first step: Copy all mce errors into a temporary buffer
1500 * We use a double buffering here, to reduce the risk of
1504 count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
1509 m = pvt->mce_outentry;
1510 if (pvt->mce_in + count > MCE_LOG_LEN) {
1511 unsigned l = MCE_LOG_LEN - pvt->mce_in;
1513 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
1519 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
1521 pvt->mce_in += count;
1524 if (pvt->mce_overrun) {
1525 sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
1528 pvt->mce_overrun = 0;
1532 * MCE second step: parse errors and display
1534 for (i = 0; i < count; i++)
1535 sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
1539 * sbridge_mce_check_error Replicates mcelog routine to get errors
1540 * This routine simply queues mcelog errors, and
1541 * return. The error itself should be handled later
1542 * by sbridge_check_error.
1543 * WARNING: As this routine should be called at NMI time, extra care should
1544 * be taken to avoid deadlocks, and to be as fast as possible.
1546 static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
1549 struct mce *mce = (struct mce *)data;
1550 struct mem_ctl_info *mci;
1551 struct sbridge_pvt *pvt;
1553 mci = get_mci_for_node_id(mce->socketid);
1556 pvt = mci->pvt_info;
1559 * Just let mcelog handle it if the error is
1560 * outside the memory controller. A memory error
1561 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
1562 * bit 12 has an special meaning.
1564 if ((mce->status & 0xefff) >> 7 != 1)
1567 printk("sbridge: HANDLING MCE MEMORY ERROR\n");
1569 printk("CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
1570 mce->extcpu, mce->mcgstatus, mce->bank, mce->status);
1571 printk("TSC %llx ", mce->tsc);
1572 printk("ADDR %llx ", mce->addr);
1573 printk("MISC %llx ", mce->misc);
1575 printk("PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
1576 mce->cpuvendor, mce->cpuid, mce->time,
1577 mce->socketid, mce->apicid);
1579 /* Only handle if it is the right mc controller */
1580 if (cpu_data(mce->cpu).phys_proc_id != pvt->sbridge_dev->mc)
1584 if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
1590 /* Copy memory error at the ringbuffer */
1591 memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
1593 pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
1595 /* Handle fatal errors immediately */
1596 if (mce->mcgstatus & 1)
1597 sbridge_check_error(mci);
1599 /* Advice mcelog that the error were handled */
1603 static struct notifier_block sbridge_mce_dec = {
1604 .notifier_call = sbridge_mce_check_error,
1607 /****************************************************************************
1608 EDAC register/unregister logic
1609 ****************************************************************************/
1611 static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
1613 struct mem_ctl_info *mci = sbridge_dev->mci;
1614 struct sbridge_pvt *pvt;
1616 if (unlikely(!mci || !mci->pvt_info)) {
1617 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
1619 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
1623 pvt = mci->pvt_info;
1625 edac_dbg(0, "MC: mci = %p, dev = %p\n",
1626 mci, &sbridge_dev->pdev[0]->dev);
1628 /* Remove MC sysfs nodes */
1629 edac_mc_del_mc(mci->pdev);
1631 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
1632 kfree(mci->ctl_name);
1634 sbridge_dev->mci = NULL;
1637 static int sbridge_register_mci(struct sbridge_dev *sbridge_dev)
1639 struct mem_ctl_info *mci;
1640 struct edac_mc_layer layers[2];
1641 struct sbridge_pvt *pvt;
1644 /* Check the number of active and not disabled channels */
1645 rc = check_if_ecc_is_active(sbridge_dev->bus);
1646 if (unlikely(rc < 0))
1649 /* allocate a new MC control structure */
1650 layers[0].type = EDAC_MC_LAYER_CHANNEL;
1651 layers[0].size = NUM_CHANNELS;
1652 layers[0].is_virt_csrow = false;
1653 layers[1].type = EDAC_MC_LAYER_SLOT;
1654 layers[1].size = MAX_DIMMS;
1655 layers[1].is_virt_csrow = true;
1656 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
1662 edac_dbg(0, "MC: mci = %p, dev = %p\n",
1663 mci, &sbridge_dev->pdev[0]->dev);
1665 pvt = mci->pvt_info;
1666 memset(pvt, 0, sizeof(*pvt));
1668 /* Associate sbridge_dev and mci for future usage */
1669 pvt->sbridge_dev = sbridge_dev;
1670 sbridge_dev->mci = mci;
1672 mci->mtype_cap = MEM_FLAG_DDR3;
1673 mci->edac_ctl_cap = EDAC_FLAG_NONE;
1674 mci->edac_cap = EDAC_FLAG_NONE;
1675 mci->mod_name = "sbridge_edac.c";
1676 mci->mod_ver = SBRIDGE_REVISION;
1677 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
1678 mci->dev_name = pci_name(sbridge_dev->pdev[0]);
1679 mci->ctl_page_to_phys = NULL;
1680 pvt->info.get_tolm = sbridge_get_tolm;
1681 pvt->info.get_tohm = sbridge_get_tohm;
1682 pvt->info.dram_rule = sbridge_dram_rule;
1683 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
1685 /* Set the function pointer to an actual operation function */
1686 mci->edac_check = sbridge_check_error;
1688 /* Store pci devices at mci for faster access */
1689 rc = mci_bind_devs(mci, sbridge_dev);
1690 if (unlikely(rc < 0))
1693 /* Get dimm basic config and the memory layout */
1694 get_dimm_config(mci);
1695 get_memory_layout(mci);
1697 /* record ptr to the generic device */
1698 mci->pdev = &sbridge_dev->pdev[0]->dev;
1700 /* add this new MC control structure to EDAC's list of MCs */
1701 if (unlikely(edac_mc_add_mc(mci))) {
1702 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
1710 kfree(mci->ctl_name);
1712 sbridge_dev->mci = NULL;
1717 * sbridge_probe Probe for ONE instance of device to see if it is
1720 * 0 for FOUND a device
1721 * < 0 for error code
1724 static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1728 struct sbridge_dev *sbridge_dev;
1730 /* get the pci devices we want to reserve for our use */
1731 mutex_lock(&sbridge_edac_lock);
1734 * All memory controllers are allocated at the first pass.
1736 if (unlikely(probed >= 1)) {
1737 mutex_unlock(&sbridge_edac_lock);
1742 rc = sbridge_get_all_devices(&num_mc);
1743 if (unlikely(rc < 0))
1747 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1748 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
1749 mc, mc + 1, num_mc);
1750 sbridge_dev->mc = mc++;
1751 rc = sbridge_register_mci(sbridge_dev);
1752 if (unlikely(rc < 0))
1756 sbridge_printk(KERN_INFO, "Driver loaded.\n");
1758 mutex_unlock(&sbridge_edac_lock);
1762 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
1763 sbridge_unregister_mci(sbridge_dev);
1765 sbridge_put_all_devices();
1767 mutex_unlock(&sbridge_edac_lock);
1772 * sbridge_remove destructor for one instance of device
1775 static void sbridge_remove(struct pci_dev *pdev)
1777 struct sbridge_dev *sbridge_dev;
1782 * we have a trouble here: pdev value for removal will be wrong, since
1783 * it will point to the X58 register used to detect that the machine
1784 * is a Nehalem or upper design. However, due to the way several PCI
1785 * devices are grouped together to provide MC functionality, we need
1786 * to use a different method for releasing the devices
1789 mutex_lock(&sbridge_edac_lock);
1791 if (unlikely(!probed)) {
1792 mutex_unlock(&sbridge_edac_lock);
1796 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
1797 sbridge_unregister_mci(sbridge_dev);
1799 /* Release PCI resources */
1800 sbridge_put_all_devices();
1804 mutex_unlock(&sbridge_edac_lock);
1807 MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
1810 * sbridge_driver pci_driver structure for this module
1813 static struct pci_driver sbridge_driver = {
1814 .name = "sbridge_edac",
1815 .probe = sbridge_probe,
1816 .remove = sbridge_remove,
1817 .id_table = sbridge_pci_tbl,
1821 * sbridge_init Module entry function
1822 * Try to initialize this module for its devices
1824 static int __init sbridge_init(void)
1830 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
1833 pci_rc = pci_register_driver(&sbridge_driver);
1836 mce_register_decode_chain(&sbridge_mce_dec);
1840 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
1847 * sbridge_exit() Module exit function
1848 * Unregister the driver
1850 static void __exit sbridge_exit(void)
1853 pci_unregister_driver(&sbridge_driver);
1854 mce_unregister_decode_chain(&sbridge_mce_dec);
1857 module_init(sbridge_init);
1858 module_exit(sbridge_exit);
1860 module_param(edac_op_state, int, 0444);
1861 MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
1863 MODULE_LICENSE("GPL");
1864 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1865 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
1866 MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge memory controllers - "