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 proccess. 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 dram_rule
[] = {
87 0x80, 0x88, 0x90, 0x98, 0xa0,
88 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
90 #define MAX_SAD ARRAY_SIZE(dram_rule)
92 #define SAD_LIMIT(reg) ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
93 #define DRAM_ATTR(reg) GET_BITFIELD(reg, 2, 3)
94 #define INTERLEAVE_MODE(reg) GET_BITFIELD(reg, 1, 1)
95 #define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
97 static char *get_dram_attr(u32 reg
)
99 switch(DRAM_ATTR(reg
)) {
111 static const u32 interleave_list
[] = {
112 0x84, 0x8c, 0x94, 0x9c, 0xa4,
113 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
115 #define MAX_INTERLEAVE ARRAY_SIZE(interleave_list)
117 #define SAD_PKG0(reg) GET_BITFIELD(reg, 0, 2)
118 #define SAD_PKG1(reg) GET_BITFIELD(reg, 3, 5)
119 #define SAD_PKG2(reg) GET_BITFIELD(reg, 8, 10)
120 #define SAD_PKG3(reg) GET_BITFIELD(reg, 11, 13)
121 #define SAD_PKG4(reg) GET_BITFIELD(reg, 16, 18)
122 #define SAD_PKG5(reg) GET_BITFIELD(reg, 19, 21)
123 #define SAD_PKG6(reg) GET_BITFIELD(reg, 24, 26)
124 #define SAD_PKG7(reg) GET_BITFIELD(reg, 27, 29)
126 static inline int sad_pkg(u32 reg
, int interleave
)
128 switch (interleave
) {
130 return SAD_PKG0(reg
);
132 return SAD_PKG1(reg
);
134 return SAD_PKG2(reg
);
136 return SAD_PKG3(reg
);
138 return SAD_PKG4(reg
);
140 return SAD_PKG5(reg
);
142 return SAD_PKG6(reg
);
144 return SAD_PKG7(reg
);
150 /* Devices 12 Function 7 */
155 #define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
156 #define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
158 /* Device 13 Function 6 */
160 #define SAD_TARGET 0xf0
162 #define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
164 #define SAD_CONTROL 0xf4
166 #define NODE_ID(reg) GET_BITFIELD(reg, 0, 2)
168 /* Device 14 function 0 */
170 static const u32 tad_dram_rule
[] = {
171 0x40, 0x44, 0x48, 0x4c,
172 0x50, 0x54, 0x58, 0x5c,
173 0x60, 0x64, 0x68, 0x6c,
175 #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
177 #define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
178 #define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
179 #define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
180 #define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
181 #define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
182 #define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
183 #define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
185 /* Device 15, function 0 */
189 #define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
190 #define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
191 #define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
193 /* Device 15, function 1 */
195 #define RASENABLES 0xac
196 #define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
198 /* Device 15, functions 2-5 */
200 static const int mtr_regs
[] = {
204 #define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
205 #define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
206 #define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
207 #define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
208 #define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
210 static const u32 tad_ch_nilv_offset
[] = {
211 0x90, 0x94, 0x98, 0x9c,
212 0xa0, 0xa4, 0xa8, 0xac,
213 0xb0, 0xb4, 0xb8, 0xbc,
215 #define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
216 #define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
218 static const u32 rir_way_limit
[] = {
219 0x108, 0x10c, 0x110, 0x114, 0x118,
221 #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
223 #define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
224 #define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
225 #define RIR_LIMIT(reg) ((GET_BITFIELD(reg, 1, 10) << 29)| 0x1fffffff)
227 #define MAX_RIR_WAY 8
229 static const u32 rir_offset
[MAX_RIR_RANGES
][MAX_RIR_WAY
] = {
230 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
231 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
232 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
233 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
234 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
237 #define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
238 #define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
240 /* Device 16, functions 2-7 */
243 * FIXME: Implement the error count reads directly
246 static const u32 correrrcnt
[] = {
247 0x104, 0x108, 0x10c, 0x110,
250 #define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
251 #define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
252 #define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
253 #define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
255 static const u32 correrrthrsld
[] = {
256 0x11c, 0x120, 0x124, 0x128,
259 #define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
260 #define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
263 /* Device 17, function 0 */
265 #define RANK_CFG_A 0x0328
267 #define IS_RDIMM_ENABLED(reg) GET_BITFIELD(reg, 11, 11)
273 #define NUM_CHANNELS 4
274 #define MAX_DIMMS 3 /* Max DIMMS per channel */
276 struct sbridge_info
{
280 struct sbridge_channel
{
285 struct pci_id_descr
{
292 struct pci_id_table
{
293 const struct pci_id_descr
*descr
;
298 struct list_head list
;
300 u8 node_id
, source_id
;
301 struct pci_dev
**pdev
;
303 struct mem_ctl_info
*mci
;
307 struct pci_dev
*pci_ta
, *pci_ddrio
, *pci_ras
;
308 struct pci_dev
*pci_sad0
, *pci_sad1
, *pci_ha0
;
309 struct pci_dev
*pci_br
;
310 struct pci_dev
*pci_tad
[NUM_CHANNELS
];
312 struct sbridge_dev
*sbridge_dev
;
314 struct sbridge_info info
;
315 struct sbridge_channel channel
[NUM_CHANNELS
];
317 int csrow_map
[NUM_CHANNELS
][MAX_DIMMS
];
319 /* Memory type detection */
320 bool is_mirrored
, is_lockstep
, is_close_pg
;
322 /* Fifo double buffers */
323 struct mce mce_entry
[MCE_LOG_LEN
];
324 struct mce mce_outentry
[MCE_LOG_LEN
];
326 /* Fifo in/out counters */
327 unsigned mce_in
, mce_out
;
329 /* Count indicator to show errors not got */
330 unsigned mce_overrun
;
332 /* Memory description */
336 #define PCI_DESCR(device, function, device_id) \
338 .func = (function), \
339 .dev_id = (device_id)
341 static const struct pci_id_descr pci_dev_descr_sbridge
[] = {
342 /* Processor Home Agent */
343 { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0
) },
345 /* Memory controller */
346 { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA
) },
347 { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS
) },
348 { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0
) },
349 { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1
) },
350 { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2
) },
351 { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3
) },
352 { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO
) },
354 /* System Address Decoder */
355 { PCI_DESCR(12, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0
) },
356 { PCI_DESCR(12, 7, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1
) },
358 /* Broadcast Registers */
359 { PCI_DESCR(13, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_BR
) },
362 #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
363 static const struct pci_id_table pci_dev_descr_sbridge_table
[] = {
364 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge
),
365 {0,} /* 0 terminated list. */
369 * pci_device_id table for which devices we are looking for
371 static DEFINE_PCI_DEVICE_TABLE(sbridge_pci_tbl
) = {
372 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA
)},
373 {0,} /* 0 terminated list. */
377 /****************************************************************************
378 Anciliary status routines
379 ****************************************************************************/
381 static inline int numrank(u32 mtr
)
383 int ranks
= (1 << RANK_CNT_BITS(mtr
));
386 debugf0("Invalid number of ranks: %d (max = 4) raw value = %x (%04x)",
387 ranks
, (unsigned int)RANK_CNT_BITS(mtr
), mtr
);
394 static inline int numrow(u32 mtr
)
396 int rows
= (RANK_WIDTH_BITS(mtr
) + 12);
398 if (rows
< 13 || rows
> 18) {
399 debugf0("Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)",
400 rows
, (unsigned int)RANK_WIDTH_BITS(mtr
), mtr
);
407 static inline int numcol(u32 mtr
)
409 int cols
= (COL_WIDTH_BITS(mtr
) + 10);
412 debugf0("Invalid number of cols: %d (max = 4) raw value = %x (%04x)",
413 cols
, (unsigned int)COL_WIDTH_BITS(mtr
), mtr
);
420 static struct sbridge_dev
*get_sbridge_dev(u8 bus
)
422 struct sbridge_dev
*sbridge_dev
;
424 list_for_each_entry(sbridge_dev
, &sbridge_edac_list
, list
) {
425 if (sbridge_dev
->bus
== bus
)
432 static struct sbridge_dev
*alloc_sbridge_dev(u8 bus
,
433 const struct pci_id_table
*table
)
435 struct sbridge_dev
*sbridge_dev
;
437 sbridge_dev
= kzalloc(sizeof(*sbridge_dev
), GFP_KERNEL
);
441 sbridge_dev
->pdev
= kzalloc(sizeof(*sbridge_dev
->pdev
) * table
->n_devs
,
443 if (!sbridge_dev
->pdev
) {
448 sbridge_dev
->bus
= bus
;
449 sbridge_dev
->n_devs
= table
->n_devs
;
450 list_add_tail(&sbridge_dev
->list
, &sbridge_edac_list
);
455 static void free_sbridge_dev(struct sbridge_dev
*sbridge_dev
)
457 list_del(&sbridge_dev
->list
);
458 kfree(sbridge_dev
->pdev
);
462 /****************************************************************************
463 Memory check routines
464 ****************************************************************************/
465 static struct pci_dev
*get_pdev_slot_func(u8 bus
, unsigned slot
,
468 struct sbridge_dev
*sbridge_dev
= get_sbridge_dev(bus
);
474 for (i
= 0; i
< sbridge_dev
->n_devs
; i
++) {
475 if (!sbridge_dev
->pdev
[i
])
478 if (PCI_SLOT(sbridge_dev
->pdev
[i
]->devfn
) == slot
&&
479 PCI_FUNC(sbridge_dev
->pdev
[i
]->devfn
) == func
) {
480 debugf1("Associated %02x.%02x.%d with %p\n",
481 bus
, slot
, func
, sbridge_dev
->pdev
[i
]);
482 return sbridge_dev
->pdev
[i
];
490 * sbridge_get_active_channels() - gets the number of channels and csrows
492 * @channels: Number of channels that will be returned
493 * @csrows: Number of csrows found
495 * Since EDAC core needs to know in advance the number of available channels
496 * and csrows, in order to allocate memory for csrows/channels, it is needed
497 * to run two similar steps. At the first step, implemented on this function,
498 * it checks the number of csrows/channels present at one socket, identified
499 * by the associated PCI bus.
500 * this is used in order to properly allocate the size of mci components.
501 * Note: one csrow is one dimm.
503 static int sbridge_get_active_channels(const u8 bus
, unsigned *channels
,
506 struct pci_dev
*pdev
= NULL
;
513 pdev
= get_pdev_slot_func(bus
, 15, 0);
515 sbridge_printk(KERN_ERR
, "Couldn't find PCI device "
521 pci_read_config_dword(pdev
, MCMTR
, &mcmtr
);
522 if (!IS_ECC_ENABLED(mcmtr
)) {
523 sbridge_printk(KERN_ERR
, "ECC is disabled. Aborting\n");
527 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
530 /* Device 15 functions 2 - 5 */
531 pdev
= get_pdev_slot_func(bus
, 15, 2 + i
);
533 sbridge_printk(KERN_ERR
, "Couldn't find PCI device "
540 for (j
= 0; j
< ARRAY_SIZE(mtr_regs
); j
++) {
541 pci_read_config_dword(pdev
, mtr_regs
[j
], &mtr
);
542 debugf1("Bus#%02x channel #%d MTR%d = %x\n", bus
, i
, j
, mtr
);
543 if (IS_DIMM_PRESENT(mtr
))
548 debugf0("Number of active channels: %d, number of active dimms: %d\n",
554 static int get_dimm_config(const struct mem_ctl_info
*mci
)
556 struct sbridge_pvt
*pvt
= mci
->pvt_info
;
557 struct csrow_info
*csr
;
558 unsigned i
, j
, banks
, ranks
, rows
, cols
, npages
;
561 unsigned long last_page
= 0;
566 pci_read_config_dword(pvt
->pci_br
, SAD_TARGET
, ®
);
567 pvt
->sbridge_dev
->source_id
= SOURCE_ID(reg
);
569 pci_read_config_dword(pvt
->pci_br
, SAD_CONTROL
, ®
);
570 pvt
->sbridge_dev
->node_id
= NODE_ID(reg
);
571 debugf0("mc#%d: Node ID: %d, source ID: %d\n",
572 pvt
->sbridge_dev
->mc
,
573 pvt
->sbridge_dev
->node_id
,
574 pvt
->sbridge_dev
->source_id
);
576 pci_read_config_dword(pvt
->pci_ras
, RASENABLES
, ®
);
577 if (IS_MIRROR_ENABLED(reg
)) {
578 debugf0("Memory mirror is enabled\n");
579 pvt
->is_mirrored
= true;
581 debugf0("Memory mirror is disabled\n");
582 pvt
->is_mirrored
= false;
585 pci_read_config_dword(pvt
->pci_ta
, MCMTR
, &pvt
->info
.mcmtr
);
586 if (IS_LOCKSTEP_ENABLED(pvt
->info
.mcmtr
)) {
587 debugf0("Lockstep is enabled\n");
588 mode
= EDAC_S8ECD8ED
;
589 pvt
->is_lockstep
= true;
591 debugf0("Lockstep is disabled\n");
592 mode
= EDAC_S4ECD4ED
;
593 pvt
->is_lockstep
= false;
595 if (IS_CLOSE_PG(pvt
->info
.mcmtr
)) {
596 debugf0("address map is on closed page mode\n");
597 pvt
->is_close_pg
= true;
599 debugf0("address map is on open page mode\n");
600 pvt
->is_close_pg
= false;
603 pci_read_config_dword(pvt
->pci_ddrio
, RANK_CFG_A
, ®
);
604 if (IS_RDIMM_ENABLED(reg
)) {
605 /* FIXME: Can also be LRDIMM */
606 debugf0("Memory is registered\n");
609 debugf0("Memory is unregistered\n");
613 /* On all supported DDR3 DIMM types, there are 8 banks available */
616 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
619 for (j
= 0; j
< ARRAY_SIZE(mtr_regs
); j
++) {
620 pci_read_config_dword(pvt
->pci_tad
[i
],
622 debugf4("Channel #%d MTR%d = %x\n", i
, j
, mtr
);
623 if (IS_DIMM_PRESENT(mtr
)) {
624 pvt
->channel
[i
].dimms
++;
626 ranks
= numrank(mtr
);
630 /* DDR3 has 8 I/O banks */
631 size
= ((u64
)rows
* cols
* banks
* ranks
) >> (20 - 3);
632 npages
= MiB_TO_PAGES(size
);
634 debugf0("mc#%d: channel %d, dimm %d, %Ld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
635 pvt
->sbridge_dev
->mc
, i
, j
,
637 banks
, ranks
, rows
, cols
);
638 csr
= &mci
->csrows
[csrow
];
640 csr
->first_page
= last_page
;
641 csr
->last_page
= last_page
+ npages
- 1;
642 csr
->page_mask
= 0UL; /* Unused */
643 csr
->nr_pages
= npages
;
645 csr
->csrow_idx
= csrow
;
646 csr
->dtype
= (banks
== 8) ? DEV_X8
: DEV_X4
;
650 csr
->edac_mode
= mode
;
651 csr
->nr_channels
= 1;
652 csr
->channels
[0].chan_idx
= i
;
653 csr
->channels
[0].ce_count
= 0;
654 pvt
->csrow_map
[i
][j
] = csrow
;
655 snprintf(csr
->channels
[0].label
,
656 sizeof(csr
->channels
[0].label
),
657 "CPU_SrcID#%u_Channel#%u_DIMM#%u",
658 pvt
->sbridge_dev
->source_id
, i
, j
);
668 static void get_memory_layout(const struct mem_ctl_info
*mci
)
670 struct sbridge_pvt
*pvt
= mci
->pvt_info
;
671 int i
, j
, k
, n_sads
, n_tads
, sad_interl
;
679 * Step 1) Get TOLM/TOHM ranges
682 /* Address range is 32:28 */
683 pci_read_config_dword(pvt
->pci_sad1
, TOLM
,
685 pvt
->tolm
= GET_TOLM(reg
);
686 tmp_mb
= (1 + pvt
->tolm
) >> 20;
688 mb
= div_u64_rem(tmp_mb
, 1000, &kb
);
689 debugf0("TOLM: %u.%03u GB (0x%016Lx)\n",
690 mb
, kb
, (u64
)pvt
->tolm
);
692 /* Address range is already 45:25 */
693 pci_read_config_dword(pvt
->pci_sad1
, TOHM
,
695 pvt
->tohm
= GET_TOHM(reg
);
696 tmp_mb
= (1 + pvt
->tohm
) >> 20;
698 mb
= div_u64_rem(tmp_mb
, 1000, &kb
);
699 debugf0("TOHM: %u.%03u GB (0x%016Lx)",
700 mb
, kb
, (u64
)pvt
->tohm
);
703 * Step 2) Get SAD range and SAD Interleave list
704 * TAD registers contain the interleave wayness. However, it
705 * seems simpler to just discover it indirectly, with the
709 for (n_sads
= 0; n_sads
< MAX_SAD
; n_sads
++) {
710 /* SAD_LIMIT Address range is 45:26 */
711 pci_read_config_dword(pvt
->pci_sad0
, dram_rule
[n_sads
],
713 limit
= SAD_LIMIT(reg
);
715 if (!DRAM_RULE_ENABLE(reg
))
721 tmp_mb
= (limit
+ 1) >> 20;
722 mb
= div_u64_rem(tmp_mb
, 1000, &kb
);
723 debugf0("SAD#%d %s up to %u.%03u GB (0x%016Lx) %s reg=0x%08x\n",
727 ((u64
)tmp_mb
) << 20L,
728 INTERLEAVE_MODE(reg
) ? "Interleave: 8:6" : "Interleave: [8:6]XOR[18:16]",
732 pci_read_config_dword(pvt
->pci_sad0
, interleave_list
[n_sads
],
734 sad_interl
= sad_pkg(reg
, 0);
735 for (j
= 0; j
< 8; j
++) {
736 if (j
> 0 && sad_interl
== sad_pkg(reg
, j
))
739 debugf0("SAD#%d, interleave #%d: %d\n",
740 n_sads
, j
, sad_pkg(reg
, j
));
745 * Step 3) Get TAD range
748 for (n_tads
= 0; n_tads
< MAX_TAD
; n_tads
++) {
749 pci_read_config_dword(pvt
->pci_ha0
, tad_dram_rule
[n_tads
],
751 limit
= TAD_LIMIT(reg
);
754 tmp_mb
= (limit
+ 1) >> 20;
756 mb
= div_u64_rem(tmp_mb
, 1000, &kb
);
757 debugf0("TAD#%d: up to %u.%03u GB (0x%016Lx), socket interleave %d, memory interleave %d, TGT: %d, %d, %d, %d, reg=0x%08x\n",
759 ((u64
)tmp_mb
) << 20L,
771 * Step 4) Get TAD offsets, per each channel
773 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
774 if (!pvt
->channel
[i
].dimms
)
776 for (j
= 0; j
< n_tads
; j
++) {
777 pci_read_config_dword(pvt
->pci_tad
[i
],
778 tad_ch_nilv_offset
[j
],
780 tmp_mb
= TAD_OFFSET(reg
) >> 20;
781 mb
= div_u64_rem(tmp_mb
, 1000, &kb
);
782 debugf0("TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
785 ((u64
)tmp_mb
) << 20L,
791 * Step 6) Get RIR Wayness/Limit, per each channel
793 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
794 if (!pvt
->channel
[i
].dimms
)
796 for (j
= 0; j
< MAX_RIR_RANGES
; j
++) {
797 pci_read_config_dword(pvt
->pci_tad
[i
],
801 if (!IS_RIR_VALID(reg
))
804 tmp_mb
= RIR_LIMIT(reg
) >> 20;
805 rir_way
= 1 << RIR_WAY(reg
);
806 mb
= div_u64_rem(tmp_mb
, 1000, &kb
);
807 debugf0("CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
810 ((u64
)tmp_mb
) << 20L,
814 for (k
= 0; k
< rir_way
; k
++) {
815 pci_read_config_dword(pvt
->pci_tad
[i
],
818 tmp_mb
= RIR_OFFSET(reg
) << 6;
820 mb
= div_u64_rem(tmp_mb
, 1000, &kb
);
821 debugf0("CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
824 ((u64
)tmp_mb
) << 20L,
825 (u32
)RIR_RNK_TGT(reg
),
832 struct mem_ctl_info
*get_mci_for_node_id(u8 node_id
)
834 struct sbridge_dev
*sbridge_dev
;
836 list_for_each_entry(sbridge_dev
, &sbridge_edac_list
, list
) {
837 if (sbridge_dev
->node_id
== node_id
)
838 return sbridge_dev
->mci
;
843 static int get_memory_error_data(struct mem_ctl_info
*mci
,
850 struct mem_ctl_info
*new_mci
;
851 struct sbridge_pvt
*pvt
= mci
->pvt_info
;
853 int n_rir
, n_sads
, n_tads
, sad_way
, sck_xch
;
854 int sad_interl
, idx
, base_ch
;
856 unsigned sad_interleave
[MAX_INTERLEAVE
];
862 u64 ch_addr
, offset
, limit
, prv
= 0;
866 * Step 0) Check if the address is at special memory ranges
867 * The check bellow is probably enough to fill all cases where
868 * the error is not inside a memory, except for the legacy
869 * range (e. g. VGA addresses). It is unlikely, however, that the
870 * memory controller would generate an error on that range.
872 if ((addr
> (u64
) pvt
->tolm
) && (addr
< (1LL << 32))) {
873 sprintf(msg
, "Error at TOLM area, on addr 0x%08Lx", addr
);
874 edac_mc_handle_ce_no_info(mci
, msg
);
877 if (addr
>= (u64
)pvt
->tohm
) {
878 sprintf(msg
, "Error at MMIOH area, on addr 0x%016Lx", addr
);
879 edac_mc_handle_ce_no_info(mci
, msg
);
886 for (n_sads
= 0; n_sads
< MAX_SAD
; n_sads
++) {
887 pci_read_config_dword(pvt
->pci_sad0
, dram_rule
[n_sads
],
890 if (!DRAM_RULE_ENABLE(reg
))
893 limit
= SAD_LIMIT(reg
);
895 sprintf(msg
, "Can't discover the memory socket");
896 edac_mc_handle_ce_no_info(mci
, msg
);
903 if (n_sads
== MAX_SAD
) {
904 sprintf(msg
, "Can't discover the memory socket");
905 edac_mc_handle_ce_no_info(mci
, msg
);
908 area_type
= get_dram_attr(reg
);
909 interleave_mode
= INTERLEAVE_MODE(reg
);
911 pci_read_config_dword(pvt
->pci_sad0
, interleave_list
[n_sads
],
913 sad_interl
= sad_pkg(reg
, 0);
914 for (sad_way
= 0; sad_way
< 8; sad_way
++) {
915 if (sad_way
> 0 && sad_interl
== sad_pkg(reg
, sad_way
))
917 sad_interleave
[sad_way
] = sad_pkg(reg
, sad_way
);
918 debugf0("SAD interleave #%d: %d\n",
919 sad_way
, sad_interleave
[sad_way
]);
921 debugf0("mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
922 pvt
->sbridge_dev
->mc
,
927 interleave_mode
? "" : "XOR[18:16]");
929 idx
= ((addr
>> 6) ^ (addr
>> 16)) & 7;
931 idx
= (addr
>> 6) & 7;
945 sprintf(msg
, "Can't discover socket interleave");
946 edac_mc_handle_ce_no_info(mci
, msg
);
949 *socket
= sad_interleave
[idx
];
950 debugf0("SAD interleave index: %d (wayness %d) = CPU socket %d\n",
951 idx
, sad_way
, *socket
);
954 * Move to the proper node structure, in order to access the
955 * right PCI registers
957 new_mci
= get_mci_for_node_id(*socket
);
959 sprintf(msg
, "Struct for socket #%u wasn't initialized",
961 edac_mc_handle_ce_no_info(mci
, msg
);
968 * Step 2) Get memory channel
971 for (n_tads
= 0; n_tads
< MAX_TAD
; n_tads
++) {
972 pci_read_config_dword(pvt
->pci_ha0
, tad_dram_rule
[n_tads
],
974 limit
= TAD_LIMIT(reg
);
976 sprintf(msg
, "Can't discover the memory channel");
977 edac_mc_handle_ce_no_info(mci
, msg
);
984 ch_way
= TAD_CH(reg
) + 1;
985 sck_way
= TAD_SOCK(reg
) + 1;
987 * FIXME: Is it right to always use channel 0 for offsets?
989 pci_read_config_dword(pvt
->pci_tad
[0],
990 tad_ch_nilv_offset
[n_tads
],
996 idx
= addr
>> (6 + sck_way
);
1000 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
1004 base_ch
= TAD_TGT0(reg
);
1007 base_ch
= TAD_TGT1(reg
);
1010 base_ch
= TAD_TGT2(reg
);
1013 base_ch
= TAD_TGT3(reg
);
1016 sprintf(msg
, "Can't discover the TAD target");
1017 edac_mc_handle_ce_no_info(mci
, msg
);
1020 *channel_mask
= 1 << base_ch
;
1022 if (pvt
->is_mirrored
) {
1023 *channel_mask
|= 1 << ((base_ch
+ 2) % 4);
1027 sck_xch
= 1 << sck_way
* (ch_way
>> 1);
1030 sprintf(msg
, "Invalid mirror set. Can't decode addr");
1031 edac_mc_handle_ce_no_info(mci
, msg
);
1035 sck_xch
= (1 << sck_way
) * ch_way
;
1037 if (pvt
->is_lockstep
)
1038 *channel_mask
|= 1 << ((base_ch
+ 1) % 4);
1040 offset
= TAD_OFFSET(tad_offset
);
1042 debugf0("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",
1053 /* Calculate channel address */
1054 /* Remove the TAD offset */
1056 if (offset
> addr
) {
1057 sprintf(msg
, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
1059 edac_mc_handle_ce_no_info(mci
, msg
);
1063 /* Store the low bits [0:6] of the addr */
1064 ch_addr
= addr
& 0x7f;
1065 /* Remove socket wayness and remove 6 bits */
1067 addr
= div_u64(addr
, sck_xch
);
1069 /* Divide by channel way */
1070 addr
= addr
/ ch_way
;
1072 /* Recover the last 6 bits */
1073 ch_addr
|= addr
<< 6;
1076 * Step 3) Decode rank
1078 for (n_rir
= 0; n_rir
< MAX_RIR_RANGES
; n_rir
++) {
1079 pci_read_config_dword(pvt
->pci_tad
[base_ch
],
1080 rir_way_limit
[n_rir
],
1083 if (!IS_RIR_VALID(reg
))
1086 limit
= RIR_LIMIT(reg
);
1087 mb
= div_u64_rem(limit
>> 20, 1000, &kb
);
1088 debugf0("RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
1093 if (ch_addr
<= limit
)
1096 if (n_rir
== MAX_RIR_RANGES
) {
1097 sprintf(msg
, "Can't discover the memory rank for ch addr 0x%08Lx",
1099 edac_mc_handle_ce_no_info(mci
, msg
);
1102 rir_way
= RIR_WAY(reg
);
1103 if (pvt
->is_close_pg
)
1104 idx
= (ch_addr
>> 6);
1106 idx
= (ch_addr
>> 13); /* FIXME: Datasheet says to shift by 15 */
1107 idx
%= 1 << rir_way
;
1109 pci_read_config_dword(pvt
->pci_tad
[base_ch
],
1110 rir_offset
[n_rir
][idx
],
1112 *rank
= RIR_RNK_TGT(reg
);
1114 debugf0("RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
1124 /****************************************************************************
1125 Device initialization routines: put/get, init/exit
1126 ****************************************************************************/
1129 * sbridge_put_all_devices 'put' all the devices that we have
1130 * reserved via 'get'
1132 static void sbridge_put_devices(struct sbridge_dev
*sbridge_dev
)
1136 debugf0(__FILE__
": %s()\n", __func__
);
1137 for (i
= 0; i
< sbridge_dev
->n_devs
; i
++) {
1138 struct pci_dev
*pdev
= sbridge_dev
->pdev
[i
];
1141 debugf0("Removing dev %02x:%02x.%d\n",
1143 PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
));
1148 static void sbridge_put_all_devices(void)
1150 struct sbridge_dev
*sbridge_dev
, *tmp
;
1152 list_for_each_entry_safe(sbridge_dev
, tmp
, &sbridge_edac_list
, list
) {
1153 sbridge_put_devices(sbridge_dev
);
1154 free_sbridge_dev(sbridge_dev
);
1159 * sbridge_get_all_devices Find and perform 'get' operation on the MCH's
1160 * device/functions we want to reference for this driver
1162 * Need to 'get' device 16 func 1 and func 2
1164 static int sbridge_get_onedevice(struct pci_dev
**prev
,
1166 const struct pci_id_table
*table
,
1167 const unsigned devno
)
1169 struct sbridge_dev
*sbridge_dev
;
1170 const struct pci_id_descr
*dev_descr
= &table
->descr
[devno
];
1172 struct pci_dev
*pdev
= NULL
;
1175 sbridge_printk(KERN_INFO
,
1176 "Seeking for: dev %02x.%d PCI ID %04x:%04x\n",
1177 dev_descr
->dev
, dev_descr
->func
,
1178 PCI_VENDOR_ID_INTEL
, dev_descr
->dev_id
);
1180 pdev
= pci_get_device(PCI_VENDOR_ID_INTEL
,
1181 dev_descr
->dev_id
, *prev
);
1189 if (dev_descr
->optional
)
1195 sbridge_printk(KERN_INFO
,
1196 "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
1197 dev_descr
->dev
, dev_descr
->func
,
1198 PCI_VENDOR_ID_INTEL
, dev_descr
->dev_id
);
1200 /* End of list, leave */
1203 bus
= pdev
->bus
->number
;
1205 sbridge_dev
= get_sbridge_dev(bus
);
1207 sbridge_dev
= alloc_sbridge_dev(bus
, table
);
1215 if (sbridge_dev
->pdev
[devno
]) {
1216 sbridge_printk(KERN_ERR
,
1217 "Duplicated device for "
1218 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1219 bus
, dev_descr
->dev
, dev_descr
->func
,
1220 PCI_VENDOR_ID_INTEL
, dev_descr
->dev_id
);
1225 sbridge_dev
->pdev
[devno
] = pdev
;
1228 if (unlikely(PCI_SLOT(pdev
->devfn
) != dev_descr
->dev
||
1229 PCI_FUNC(pdev
->devfn
) != dev_descr
->func
)) {
1230 sbridge_printk(KERN_ERR
,
1231 "Device PCI ID %04x:%04x "
1232 "has dev %02x:%d.%d instead of dev %02x:%02x.%d\n",
1233 PCI_VENDOR_ID_INTEL
, dev_descr
->dev_id
,
1234 bus
, PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
),
1235 bus
, dev_descr
->dev
, dev_descr
->func
);
1239 /* Be sure that the device is enabled */
1240 if (unlikely(pci_enable_device(pdev
) < 0)) {
1241 sbridge_printk(KERN_ERR
,
1243 "dev %02x:%d.%d PCI ID %04x:%04x\n",
1244 bus
, dev_descr
->dev
, dev_descr
->func
,
1245 PCI_VENDOR_ID_INTEL
, dev_descr
->dev_id
);
1249 debugf0("Detected dev %02x:%d.%d PCI ID %04x:%04x\n",
1250 bus
, dev_descr
->dev
,
1252 PCI_VENDOR_ID_INTEL
, dev_descr
->dev_id
);
1255 * As stated on drivers/pci/search.c, the reference count for
1256 * @from is always decremented if it is not %NULL. So, as we need
1257 * to get all devices up to null, we need to do a get for the device
1266 static int sbridge_get_all_devices(u8
*num_mc
)
1269 struct pci_dev
*pdev
= NULL
;
1270 const struct pci_id_table
*table
= pci_dev_descr_sbridge_table
;
1272 while (table
&& table
->descr
) {
1273 for (i
= 0; i
< table
->n_devs
; i
++) {
1276 rc
= sbridge_get_onedevice(&pdev
, num_mc
,
1283 sbridge_put_all_devices();
1294 static int mci_bind_devs(struct mem_ctl_info
*mci
,
1295 struct sbridge_dev
*sbridge_dev
)
1297 struct sbridge_pvt
*pvt
= mci
->pvt_info
;
1298 struct pci_dev
*pdev
;
1301 for (i
= 0; i
< sbridge_dev
->n_devs
; i
++) {
1302 pdev
= sbridge_dev
->pdev
[i
];
1305 slot
= PCI_SLOT(pdev
->devfn
);
1306 func
= PCI_FUNC(pdev
->devfn
);
1311 pvt
->pci_sad0
= pdev
;
1314 pvt
->pci_sad1
= pdev
;
1332 pvt
->pci_ha0
= pdev
;
1344 pvt
->pci_ras
= pdev
;
1350 pvt
->pci_tad
[func
- 2] = pdev
;
1359 pvt
->pci_ddrio
= pdev
;
1369 debugf0("Associated PCI %02x.%02d.%d with dev = %p\n",
1371 PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
),
1375 /* Check if everything were registered */
1376 if (!pvt
->pci_sad0
|| !pvt
->pci_sad1
|| !pvt
->pci_ha0
||
1377 !pvt
-> pci_tad
|| !pvt
->pci_ras
|| !pvt
->pci_ta
||
1381 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
1382 if (!pvt
->pci_tad
[i
])
1388 sbridge_printk(KERN_ERR
, "Some needed devices are missing\n");
1392 sbridge_printk(KERN_ERR
, "Device %d, function %d "
1393 "is out of the expected range\n",
1398 /****************************************************************************
1399 Error check routines
1400 ****************************************************************************/
1403 * While Sandy Bridge has error count registers, SMI BIOS read values from
1404 * and resets the counters. So, they are not reliable for the OS to read
1405 * from them. So, we have no option but to just trust on whatever MCE is
1406 * telling us about the errors.
1408 static void sbridge_mce_output_error(struct mem_ctl_info
*mci
,
1409 const struct mce
*m
)
1411 struct mem_ctl_info
*new_mci
;
1412 struct sbridge_pvt
*pvt
= mci
->pvt_info
;
1413 char *type
, *optype
, *msg
, *recoverable_msg
;
1414 bool ripv
= GET_BITFIELD(m
->mcgstatus
, 0, 0);
1415 bool overflow
= GET_BITFIELD(m
->status
, 62, 62);
1416 bool uncorrected_error
= GET_BITFIELD(m
->status
, 61, 61);
1417 bool recoverable
= GET_BITFIELD(m
->status
, 56, 56);
1418 u32 core_err_cnt
= GET_BITFIELD(m
->status
, 38, 52);
1419 u32 mscod
= GET_BITFIELD(m
->status
, 16, 31);
1420 u32 errcode
= GET_BITFIELD(m
->status
, 0, 15);
1421 u32 channel
= GET_BITFIELD(m
->status
, 0, 3);
1422 u32 optypenum
= GET_BITFIELD(m
->status
, 4, 6);
1423 long channel_mask
, first_channel
;
1425 int csrow
, rc
, dimm
;
1426 char *area_type
= "Unknown";
1434 * According with Table 15-9 of the Intel Archictecture spec vol 3A,
1435 * memory errors should fit in this mask:
1436 * 000f 0000 1mmm cccc (binary)
1438 * f = Correction Report Filtering Bit. If 1, subsequent errors
1442 * If the mask doesn't match, report an error to the parsing logic
1444 if (! ((errcode
& 0xef80) == 0x80)) {
1445 optype
= "Can't parse: it is not a mem";
1447 switch (optypenum
) {
1449 optype
= "generic undef request";
1452 optype
= "memory read";
1455 optype
= "memory write";
1458 optype
= "addr/cmd";
1461 optype
= "memory scrubbing";
1464 optype
= "reserved";
1469 rc
= get_memory_error_data(mci
, m
->addr
, &socket
,
1470 &channel_mask
, &rank
, area_type
);
1473 new_mci
= get_mci_for_node_id(socket
);
1475 edac_mc_handle_ce_no_info(mci
, "Error: socket got corrupted!");
1479 pvt
= mci
->pvt_info
;
1481 first_channel
= find_first_bit(&channel_mask
, NUM_CHANNELS
);
1490 csrow
= pvt
->csrow_map
[first_channel
][dimm
];
1492 if (uncorrected_error
&& recoverable
)
1493 recoverable_msg
= " recoverable";
1495 recoverable_msg
= "";
1498 * FIXME: What should we do with "channel" information on mcelog?
1499 * Probably, we can just discard it, as the channel information
1500 * comes from the get_memory_error_data() address decoding
1502 msg
= kasprintf(GFP_ATOMIC
,
1503 "%d %s error(s): %s on %s area %s%s: cpu=%d Err=%04x:%04x (ch=%d), "
1504 "addr = 0x%08llx => socket=%d, Channel=%ld(mask=%ld), rank=%d\n",
1510 overflow
? "OVERFLOW" : "",
1513 channel
, /* 1111b means not specified */
1514 (long long) m
->addr
,
1516 first_channel
, /* This is the real channel on SB */
1522 /* Call the helper to output message */
1523 if (uncorrected_error
)
1524 edac_mc_handle_fbd_ue(mci
, csrow
, 0, 0, msg
);
1526 edac_mc_handle_fbd_ce(mci
, csrow
, 0, msg
);
1532 * sbridge_check_error Retrieve and process errors reported by the
1533 * hardware. Called by the Core module.
1535 static void sbridge_check_error(struct mem_ctl_info
*mci
)
1537 struct sbridge_pvt
*pvt
= mci
->pvt_info
;
1543 * MCE first step: Copy all mce errors into a temporary buffer
1544 * We use a double buffering here, to reduce the risk of
1548 count
= (pvt
->mce_out
+ MCE_LOG_LEN
- pvt
->mce_in
)
1553 m
= pvt
->mce_outentry
;
1554 if (pvt
->mce_in
+ count
> MCE_LOG_LEN
) {
1555 unsigned l
= MCE_LOG_LEN
- pvt
->mce_in
;
1557 memcpy(m
, &pvt
->mce_entry
[pvt
->mce_in
], sizeof(*m
) * l
);
1563 memcpy(m
, &pvt
->mce_entry
[pvt
->mce_in
], sizeof(*m
) * count
);
1565 pvt
->mce_in
+= count
;
1568 if (pvt
->mce_overrun
) {
1569 sbridge_printk(KERN_ERR
, "Lost %d memory errors\n",
1572 pvt
->mce_overrun
= 0;
1576 * MCE second step: parse errors and display
1578 for (i
= 0; i
< count
; i
++)
1579 sbridge_mce_output_error(mci
, &pvt
->mce_outentry
[i
]);
1583 * sbridge_mce_check_error Replicates mcelog routine to get errors
1584 * This routine simply queues mcelog errors, and
1585 * return. The error itself should be handled later
1586 * by sbridge_check_error.
1587 * WARNING: As this routine should be called at NMI time, extra care should
1588 * be taken to avoid deadlocks, and to be as fast as possible.
1590 static int sbridge_mce_check_error(struct notifier_block
*nb
, unsigned long val
,
1593 struct mce
*mce
= (struct mce
*)data
;
1594 struct mem_ctl_info
*mci
;
1595 struct sbridge_pvt
*pvt
;
1597 mci
= get_mci_for_node_id(mce
->socketid
);
1600 pvt
= mci
->pvt_info
;
1603 * Just let mcelog handle it if the error is
1604 * outside the memory controller. A memory error
1605 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
1606 * bit 12 has an special meaning.
1608 if ((mce
->status
& 0xefff) >> 7 != 1)
1611 printk("sbridge: HANDLING MCE MEMORY ERROR\n");
1613 printk("CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
1614 mce
->extcpu
, mce
->mcgstatus
, mce
->bank
, mce
->status
);
1615 printk("TSC %llx ", mce
->tsc
);
1616 printk("ADDR %llx ", mce
->addr
);
1617 printk("MISC %llx ", mce
->misc
);
1619 printk("PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
1620 mce
->cpuvendor
, mce
->cpuid
, mce
->time
,
1621 mce
->socketid
, mce
->apicid
);
1623 /* Only handle if it is the right mc controller */
1624 if (cpu_data(mce
->cpu
).phys_proc_id
!= pvt
->sbridge_dev
->mc
)
1628 if ((pvt
->mce_out
+ 1) % MCE_LOG_LEN
== pvt
->mce_in
) {
1634 /* Copy memory error at the ringbuffer */
1635 memcpy(&pvt
->mce_entry
[pvt
->mce_out
], mce
, sizeof(*mce
));
1637 pvt
->mce_out
= (pvt
->mce_out
+ 1) % MCE_LOG_LEN
;
1639 /* Handle fatal errors immediately */
1640 if (mce
->mcgstatus
& 1)
1641 sbridge_check_error(mci
);
1643 /* Advice mcelog that the error were handled */
1647 static struct notifier_block sbridge_mce_dec
= {
1648 .notifier_call
= sbridge_mce_check_error
,
1651 /****************************************************************************
1652 EDAC register/unregister logic
1653 ****************************************************************************/
1655 static void sbridge_unregister_mci(struct sbridge_dev
*sbridge_dev
)
1657 struct mem_ctl_info
*mci
= sbridge_dev
->mci
;
1658 struct sbridge_pvt
*pvt
;
1660 if (unlikely(!mci
|| !mci
->pvt_info
)) {
1661 debugf0("MC: " __FILE__
": %s(): dev = %p\n",
1662 __func__
, &sbridge_dev
->pdev
[0]->dev
);
1664 sbridge_printk(KERN_ERR
, "Couldn't find mci handler\n");
1668 pvt
= mci
->pvt_info
;
1670 debugf0("MC: " __FILE__
": %s(): mci = %p, dev = %p\n",
1671 __func__
, mci
, &sbridge_dev
->pdev
[0]->dev
);
1673 /* Remove MC sysfs nodes */
1674 edac_mc_del_mc(mci
->dev
);
1676 debugf1("%s: free mci struct\n", mci
->ctl_name
);
1677 kfree(mci
->ctl_name
);
1679 sbridge_dev
->mci
= NULL
;
1682 static int sbridge_register_mci(struct sbridge_dev
*sbridge_dev
)
1684 struct mem_ctl_info
*mci
;
1685 struct sbridge_pvt
*pvt
;
1686 int rc
, channels
, csrows
;
1688 /* Check the number of active and not disabled channels */
1689 rc
= sbridge_get_active_channels(sbridge_dev
->bus
, &channels
, &csrows
);
1690 if (unlikely(rc
< 0))
1693 /* allocate a new MC control structure */
1694 mci
= edac_mc_alloc(sizeof(*pvt
), csrows
, channels
, sbridge_dev
->mc
);
1698 debugf0("MC: " __FILE__
": %s(): mci = %p, dev = %p\n",
1699 __func__
, mci
, &sbridge_dev
->pdev
[0]->dev
);
1701 pvt
= mci
->pvt_info
;
1702 memset(pvt
, 0, sizeof(*pvt
));
1704 /* Associate sbridge_dev and mci for future usage */
1705 pvt
->sbridge_dev
= sbridge_dev
;
1706 sbridge_dev
->mci
= mci
;
1708 mci
->mtype_cap
= MEM_FLAG_DDR3
;
1709 mci
->edac_ctl_cap
= EDAC_FLAG_NONE
;
1710 mci
->edac_cap
= EDAC_FLAG_NONE
;
1711 mci
->mod_name
= "sbridge_edac.c";
1712 mci
->mod_ver
= SBRIDGE_REVISION
;
1713 mci
->ctl_name
= kasprintf(GFP_KERNEL
, "Sandy Bridge Socket#%d", mci
->mc_idx
);
1714 mci
->dev_name
= pci_name(sbridge_dev
->pdev
[0]);
1715 mci
->ctl_page_to_phys
= NULL
;
1717 /* Set the function pointer to an actual operation function */
1718 mci
->edac_check
= sbridge_check_error
;
1720 /* Store pci devices at mci for faster access */
1721 rc
= mci_bind_devs(mci
, sbridge_dev
);
1722 if (unlikely(rc
< 0))
1725 /* Get dimm basic config and the memory layout */
1726 get_dimm_config(mci
);
1727 get_memory_layout(mci
);
1729 /* record ptr to the generic device */
1730 mci
->dev
= &sbridge_dev
->pdev
[0]->dev
;
1732 /* add this new MC control structure to EDAC's list of MCs */
1733 if (unlikely(edac_mc_add_mc(mci
))) {
1734 debugf0("MC: " __FILE__
1735 ": %s(): failed edac_mc_add_mc()\n", __func__
);
1743 kfree(mci
->ctl_name
);
1745 sbridge_dev
->mci
= NULL
;
1750 * sbridge_probe Probe for ONE instance of device to see if it is
1753 * 0 for FOUND a device
1754 * < 0 for error code
1757 static int __devinit
sbridge_probe(struct pci_dev
*pdev
,
1758 const struct pci_device_id
*id
)
1762 struct sbridge_dev
*sbridge_dev
;
1764 /* get the pci devices we want to reserve for our use */
1765 mutex_lock(&sbridge_edac_lock
);
1768 * All memory controllers are allocated at the first pass.
1770 if (unlikely(probed
>= 1)) {
1771 mutex_unlock(&sbridge_edac_lock
);
1776 rc
= sbridge_get_all_devices(&num_mc
);
1777 if (unlikely(rc
< 0))
1781 list_for_each_entry(sbridge_dev
, &sbridge_edac_list
, list
) {
1782 debugf0("Registering MC#%d (%d of %d)\n", mc
, mc
+ 1, num_mc
);
1783 sbridge_dev
->mc
= mc
++;
1784 rc
= sbridge_register_mci(sbridge_dev
);
1785 if (unlikely(rc
< 0))
1789 sbridge_printk(KERN_INFO
, "Driver loaded.\n");
1791 mutex_unlock(&sbridge_edac_lock
);
1795 list_for_each_entry(sbridge_dev
, &sbridge_edac_list
, list
)
1796 sbridge_unregister_mci(sbridge_dev
);
1798 sbridge_put_all_devices();
1800 mutex_unlock(&sbridge_edac_lock
);
1805 * sbridge_remove destructor for one instance of device
1808 static void __devexit
sbridge_remove(struct pci_dev
*pdev
)
1810 struct sbridge_dev
*sbridge_dev
;
1812 debugf0(__FILE__
": %s()\n", __func__
);
1815 * we have a trouble here: pdev value for removal will be wrong, since
1816 * it will point to the X58 register used to detect that the machine
1817 * is a Nehalem or upper design. However, due to the way several PCI
1818 * devices are grouped together to provide MC functionality, we need
1819 * to use a different method for releasing the devices
1822 mutex_lock(&sbridge_edac_lock
);
1824 if (unlikely(!probed
)) {
1825 mutex_unlock(&sbridge_edac_lock
);
1829 list_for_each_entry(sbridge_dev
, &sbridge_edac_list
, list
)
1830 sbridge_unregister_mci(sbridge_dev
);
1832 /* Release PCI resources */
1833 sbridge_put_all_devices();
1837 mutex_unlock(&sbridge_edac_lock
);
1840 MODULE_DEVICE_TABLE(pci
, sbridge_pci_tbl
);
1843 * sbridge_driver pci_driver structure for this module
1846 static struct pci_driver sbridge_driver
= {
1847 .name
= "sbridge_edac",
1848 .probe
= sbridge_probe
,
1849 .remove
= __devexit_p(sbridge_remove
),
1850 .id_table
= sbridge_pci_tbl
,
1854 * sbridge_init Module entry function
1855 * Try to initialize this module for its devices
1857 static int __init
sbridge_init(void)
1861 debugf2("MC: " __FILE__
": %s()\n", __func__
);
1863 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
1866 pci_rc
= pci_register_driver(&sbridge_driver
);
1869 mce_register_decode_chain(&sbridge_mce_dec
);
1873 sbridge_printk(KERN_ERR
, "Failed to register device with error %d.\n",
1880 * sbridge_exit() Module exit function
1881 * Unregister the driver
1883 static void __exit
sbridge_exit(void)
1885 debugf2("MC: " __FILE__
": %s()\n", __func__
);
1886 pci_unregister_driver(&sbridge_driver
);
1887 mce_unregister_decode_chain(&sbridge_mce_dec
);
1890 module_init(sbridge_init
);
1891 module_exit(sbridge_exit
);
1893 module_param(edac_op_state
, int, 0444);
1894 MODULE_PARM_DESC(edac_op_state
, "EDAC Error Reporting state: 0=Poll,1=NMI");
1896 MODULE_LICENSE("GPL");
1897 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1898 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
1899 MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge memory controllers - "