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
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.1.1 "
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) & GENMASK_ULL(hi, lo)) >> (lo))
55 /* Devices 12 Function 6, Offsets 0x80 to 0xcc */
56 static const u32 sbridge_dram_rule[] = {
57 0x80, 0x88, 0x90, 0x98, 0xa0,
58 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
61 static const u32 ibridge_dram_rule[] = {
62 0x60, 0x68, 0x70, 0x78, 0x80,
63 0x88, 0x90, 0x98, 0xa0, 0xa8,
64 0xb0, 0xb8, 0xc0, 0xc8, 0xd0,
65 0xd8, 0xe0, 0xe8, 0xf0, 0xf8,
68 #define SAD_LIMIT(reg) ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
69 #define DRAM_ATTR(reg) GET_BITFIELD(reg, 2, 3)
70 #define INTERLEAVE_MODE(reg) GET_BITFIELD(reg, 1, 1)
71 #define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
72 #define A7MODE(reg) GET_BITFIELD(reg, 26, 26)
74 static char *get_dram_attr(u32 reg)
76 switch(DRAM_ATTR(reg)) {
88 static const u32 sbridge_interleave_list[] = {
89 0x84, 0x8c, 0x94, 0x9c, 0xa4,
90 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
93 static const u32 ibridge_interleave_list[] = {
94 0x64, 0x6c, 0x74, 0x7c, 0x84,
95 0x8c, 0x94, 0x9c, 0xa4, 0xac,
96 0xb4, 0xbc, 0xc4, 0xcc, 0xd4,
97 0xdc, 0xe4, 0xec, 0xf4, 0xfc,
100 struct interleave_pkg {
105 static const struct interleave_pkg sbridge_interleave_pkg[] = {
116 static const struct interleave_pkg ibridge_interleave_pkg[] = {
127 static inline int sad_pkg(const struct interleave_pkg *table, u32 reg,
130 return GET_BITFIELD(reg, table[interleave].start,
131 table[interleave].end);
134 /* Devices 12 Function 7 */
138 #define HASWELL_TOLM 0xd0
139 #define HASWELL_TOHM_0 0xd4
140 #define HASWELL_TOHM_1 0xd8
142 #define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
143 #define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
145 /* Device 13 Function 6 */
147 #define SAD_TARGET 0xf0
149 #define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
151 #define SAD_CONTROL 0xf4
153 /* Device 14 function 0 */
155 static const u32 tad_dram_rule[] = {
156 0x40, 0x44, 0x48, 0x4c,
157 0x50, 0x54, 0x58, 0x5c,
158 0x60, 0x64, 0x68, 0x6c,
160 #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
162 #define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
163 #define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
164 #define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
165 #define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
166 #define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
167 #define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
168 #define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
170 /* Device 15, function 0 */
174 #define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
175 #define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
176 #define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
178 /* Device 15, function 1 */
180 #define RASENABLES 0xac
181 #define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
183 /* Device 15, functions 2-5 */
185 static const int mtr_regs[] = {
189 #define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
190 #define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
191 #define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
192 #define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
193 #define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
195 static const u32 tad_ch_nilv_offset[] = {
196 0x90, 0x94, 0x98, 0x9c,
197 0xa0, 0xa4, 0xa8, 0xac,
198 0xb0, 0xb4, 0xb8, 0xbc,
200 #define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
201 #define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
203 static const u32 rir_way_limit[] = {
204 0x108, 0x10c, 0x110, 0x114, 0x118,
206 #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
208 #define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
209 #define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
211 #define MAX_RIR_WAY 8
213 static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
214 { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
215 { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
216 { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
217 { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
218 { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
221 #define RIR_RNK_TGT(type, reg) (((type) == BROADWELL) ? \
222 GET_BITFIELD(reg, 20, 23) : GET_BITFIELD(reg, 16, 19))
224 #define RIR_OFFSET(type, reg) (((type) == HASWELL || (type) == BROADWELL) ? \
225 GET_BITFIELD(reg, 2, 15) : GET_BITFIELD(reg, 2, 14))
227 /* Device 16, functions 2-7 */
230 * FIXME: Implement the error count reads directly
233 static const u32 correrrcnt[] = {
234 0x104, 0x108, 0x10c, 0x110,
237 #define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
238 #define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
239 #define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
240 #define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
242 static const u32 correrrthrsld[] = {
243 0x11c, 0x120, 0x124, 0x128,
246 #define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
247 #define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
250 /* Device 17, function 0 */
252 #define SB_RANK_CFG_A 0x0328
254 #define IB_RANK_CFG_A 0x0320
260 #define NUM_CHANNELS 8 /* 2MC per socket, four chan per MC */
261 #define MAX_DIMMS 3 /* Max DIMMS per channel */
262 #define CHANNEL_UNSPECIFIED 0xf /* Intel IA32 SDM 15-14 */
272 struct sbridge_info {
276 u64 (*get_tolm)(struct sbridge_pvt *pvt);
277 u64 (*get_tohm)(struct sbridge_pvt *pvt);
278 u64 (*rir_limit)(u32 reg);
279 const u32 *dram_rule;
280 const u32 *interleave_list;
281 const struct interleave_pkg *interleave_pkg;
284 u8 (*get_node_id)(struct sbridge_pvt *pvt);
285 enum mem_type (*get_memory_type)(struct sbridge_pvt *pvt);
286 enum dev_type (*get_width)(struct sbridge_pvt *pvt, u32 mtr);
287 struct pci_dev *pci_vtd;
290 struct sbridge_channel {
295 struct pci_id_descr {
300 struct pci_id_table {
301 const struct pci_id_descr *descr;
306 struct list_head list;
308 u8 node_id, source_id;
309 struct pci_dev **pdev;
311 struct mem_ctl_info *mci;
315 struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
316 struct pci_dev *pci_sad0, *pci_sad1;
317 struct pci_dev *pci_ha0, *pci_ha1;
318 struct pci_dev *pci_br0, *pci_br1;
319 struct pci_dev *pci_ha1_ta;
320 struct pci_dev *pci_tad[NUM_CHANNELS];
322 struct sbridge_dev *sbridge_dev;
324 struct sbridge_info info;
325 struct sbridge_channel channel[NUM_CHANNELS];
327 /* Memory type detection */
328 bool is_mirrored, is_lockstep, is_close_pg;
330 /* Fifo double buffers */
331 struct mce mce_entry[MCE_LOG_LEN];
332 struct mce mce_outentry[MCE_LOG_LEN];
334 /* Fifo in/out counters */
335 unsigned mce_in, mce_out;
337 /* Count indicator to show errors not got */
338 unsigned mce_overrun;
340 /* Memory description */
344 #define PCI_DESCR(device_id, opt) \
345 .dev_id = (device_id), \
348 static const struct pci_id_descr pci_dev_descr_sbridge[] = {
349 /* Processor Home Agent */
350 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
352 /* Memory controller */
353 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
354 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
355 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
356 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
357 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
358 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
359 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
361 /* System Address Decoder */
362 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
363 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
365 /* Broadcast Registers */
366 { PCI_DESCR(PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
369 #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
370 static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
371 PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
372 {0,} /* 0 terminated list. */
375 /* This changes depending if 1HA or 2HA:
377 * 0x0eb8 (17.0) is DDRIO0
379 * 0x0ebc (17.4) is DDRIO0
381 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0 0x0eb8
382 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0 0x0ebc
385 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0 0x0ea0
386 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA 0x0ea8
387 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS 0x0e71
388 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0 0x0eaa
389 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1 0x0eab
390 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2 0x0eac
391 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3 0x0ead
392 #define PCI_DEVICE_ID_INTEL_IBRIDGE_SAD 0x0ec8
393 #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR0 0x0ec9
394 #define PCI_DEVICE_ID_INTEL_IBRIDGE_BR1 0x0eca
395 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1 0x0e60
396 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA 0x0e68
397 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS 0x0e79
398 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 0x0e6a
399 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1 0x0e6b
400 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2 0x0e6c
401 #define PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3 0x0e6d
403 static const struct pci_id_descr pci_dev_descr_ibridge[] = {
404 /* Processor Home Agent */
405 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0, 0) },
407 /* Memory controller */
408 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA, 0) },
409 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS, 0) },
410 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0, 0) },
411 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1, 0) },
412 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2, 0) },
413 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3, 0) },
415 /* System Address Decoder */
416 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_SAD, 0) },
418 /* Broadcast Registers */
419 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR0, 1) },
420 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_BR1, 0) },
422 /* Optional, mode 2HA */
423 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1, 1) },
425 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TA, 1) },
426 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_RAS, 1) },
428 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0, 1) },
429 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1, 1) },
430 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2, 1) },
431 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3, 1) },
433 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0, 1) },
434 { PCI_DESCR(PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0, 1) },
437 static const struct pci_id_table pci_dev_descr_ibridge_table[] = {
438 PCI_ID_TABLE_ENTRY(pci_dev_descr_ibridge),
439 {0,} /* 0 terminated list. */
442 /* Haswell support */
445 * - 3 DDR3 channels, 2 DPC per channel
448 * - 4 DDR4 channels, 3 DPC per channel
451 * - 4 DDR4 channels, 3 DPC per channel
454 * - each IMC interfaces with a SMI 2 channel
455 * - each SMI channel interfaces with a scalable memory buffer
456 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
458 #define HASWELL_DDRCRCLKCONTROLS 0xa10 /* Ditto on Broadwell */
459 #define HASWELL_HASYSDEFEATURE2 0x84
460 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC 0x2f28
461 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0 0x2fa0
462 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1 0x2f60
463 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA 0x2fa8
464 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL 0x2f71
465 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA 0x2f68
466 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL 0x2f79
467 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0 0x2ffc
468 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1 0x2ffd
469 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0 0x2faa
470 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1 0x2fab
471 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2 0x2fac
472 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3 0x2fad
473 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 0x2f6a
474 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1 0x2f6b
475 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2 0x2f6c
476 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3 0x2f6d
477 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0 0x2fbd
478 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1 0x2fbf
479 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2 0x2fb9
480 #define PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3 0x2fbb
481 static const struct pci_id_descr pci_dev_descr_haswell[] = {
482 /* first item must be the HA */
483 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0, 0) },
485 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0, 0) },
486 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1, 0) },
488 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1, 1) },
490 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA, 0) },
491 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL, 0) },
492 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0, 0) },
493 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1, 0) },
494 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2, 1) },
495 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3, 1) },
497 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0, 1) },
498 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1, 1) },
499 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2, 1) },
500 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3, 1) },
502 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA, 1) },
503 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_THERMAL, 1) },
504 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0, 1) },
505 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1, 1) },
506 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2, 1) },
507 { PCI_DESCR(PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3, 1) },
510 static const struct pci_id_table pci_dev_descr_haswell_table[] = {
511 PCI_ID_TABLE_ENTRY(pci_dev_descr_haswell),
512 {0,} /* 0 terminated list. */
520 * - 2 DDR3 channels, 2 DPC per channel
523 * - 4 DDR4 channels, 3 DPC per channel
526 * - 4 DDR4 channels, 3 DPC per channel
529 * - each IMC interfaces with a SMI 2 channel
530 * - each SMI channel interfaces with a scalable memory buffer
531 * - each scalable memory buffer supports 4 DDR3/DDR4 channels, 3 DPC
533 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC 0x6f28
534 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0 0x6fa0
535 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1 0x6f60
536 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA 0x6fa8
537 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL 0x6f71
538 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA 0x6f68
539 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL 0x6f79
540 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0 0x6ffc
541 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1 0x6ffd
542 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0 0x6faa
543 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1 0x6fab
544 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2 0x6fac
545 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3 0x6fad
546 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 0x6f6a
547 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1 0x6f6b
548 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2 0x6f6c
549 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3 0x6f6d
550 #define PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0 0x6faf
552 static const struct pci_id_descr pci_dev_descr_broadwell[] = {
553 /* first item must be the HA */
554 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0, 0) },
556 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0, 0) },
557 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1, 0) },
559 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1, 1) },
561 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA, 0) },
562 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL, 0) },
563 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0, 0) },
564 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1, 0) },
565 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2, 1) },
566 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3, 1) },
568 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0, 1) },
570 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA, 1) },
571 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_THERMAL, 1) },
572 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0, 1) },
573 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1, 1) },
574 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2, 1) },
575 { PCI_DESCR(PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3, 1) },
578 static const struct pci_id_table pci_dev_descr_broadwell_table[] = {
579 PCI_ID_TABLE_ENTRY(pci_dev_descr_broadwell),
580 {0,} /* 0 terminated list. */
584 * pci_device_id table for which devices we are looking for
586 static const struct pci_device_id sbridge_pci_tbl[] = {
587 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0)},
588 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA)},
589 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0)},
590 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0)},
591 {0,} /* 0 terminated list. */
595 /****************************************************************************
596 Ancillary status routines
597 ****************************************************************************/
599 static inline int numrank(enum type type, u32 mtr)
601 int ranks = (1 << RANK_CNT_BITS(mtr));
604 if (type == HASWELL || type == BROADWELL)
608 edac_dbg(0, "Invalid number of ranks: %d (max = %i) raw value = %x (%04x)\n",
609 ranks, max, (unsigned int)RANK_CNT_BITS(mtr), mtr);
616 static inline int numrow(u32 mtr)
618 int rows = (RANK_WIDTH_BITS(mtr) + 12);
620 if (rows < 13 || rows > 18) {
621 edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
622 rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
629 static inline int numcol(u32 mtr)
631 int cols = (COL_WIDTH_BITS(mtr) + 10);
634 edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
635 cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
642 static struct sbridge_dev *get_sbridge_dev(u8 bus)
644 struct sbridge_dev *sbridge_dev;
646 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
647 if (sbridge_dev->bus == bus)
654 static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
655 const struct pci_id_table *table)
657 struct sbridge_dev *sbridge_dev;
659 sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
663 sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
665 if (!sbridge_dev->pdev) {
670 sbridge_dev->bus = bus;
671 sbridge_dev->n_devs = table->n_devs;
672 list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
677 static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
679 list_del(&sbridge_dev->list);
680 kfree(sbridge_dev->pdev);
684 static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
688 /* Address range is 32:28 */
689 pci_read_config_dword(pvt->pci_sad1, TOLM, ®);
690 return GET_TOLM(reg);
693 static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
697 pci_read_config_dword(pvt->pci_sad1, TOHM, ®);
698 return GET_TOHM(reg);
701 static u64 ibridge_get_tolm(struct sbridge_pvt *pvt)
705 pci_read_config_dword(pvt->pci_br1, TOLM, ®);
707 return GET_TOLM(reg);
710 static u64 ibridge_get_tohm(struct sbridge_pvt *pvt)
714 pci_read_config_dword(pvt->pci_br1, TOHM, ®);
716 return GET_TOHM(reg);
719 static u64 rir_limit(u32 reg)
721 return ((u64)GET_BITFIELD(reg, 1, 10) << 29) | 0x1fffffff;
724 static enum mem_type get_memory_type(struct sbridge_pvt *pvt)
729 if (pvt->pci_ddrio) {
730 pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
732 if (GET_BITFIELD(reg, 11, 11))
733 /* FIXME: Can also be LRDIMM */
743 static enum mem_type haswell_get_memory_type(struct sbridge_pvt *pvt)
746 bool registered = false;
747 enum mem_type mtype = MEM_UNKNOWN;
752 pci_read_config_dword(pvt->pci_ddrio,
753 HASWELL_DDRCRCLKCONTROLS, ®);
755 if (GET_BITFIELD(reg, 16, 16))
758 pci_read_config_dword(pvt->pci_ta, MCMTR, ®);
759 if (GET_BITFIELD(reg, 14, 14)) {
775 static enum dev_type sbridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
777 /* there's no way to figure out */
781 static enum dev_type __ibridge_get_width(u32 mtr)
803 static enum dev_type ibridge_get_width(struct sbridge_pvt *pvt, u32 mtr)
806 * ddr3_width on the documentation but also valid for DDR4 on
809 return __ibridge_get_width(GET_BITFIELD(mtr, 7, 8));
812 static enum dev_type broadwell_get_width(struct sbridge_pvt *pvt, u32 mtr)
814 /* ddr3_width on the documentation but also valid for DDR4 */
815 return __ibridge_get_width(GET_BITFIELD(mtr, 8, 9));
818 static u8 get_node_id(struct sbridge_pvt *pvt)
821 pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, ®);
822 return GET_BITFIELD(reg, 0, 2);
825 static u8 haswell_get_node_id(struct sbridge_pvt *pvt)
829 pci_read_config_dword(pvt->pci_sad1, SAD_CONTROL, ®);
830 return GET_BITFIELD(reg, 0, 3);
833 static u64 haswell_get_tolm(struct sbridge_pvt *pvt)
837 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOLM, ®);
838 return (GET_BITFIELD(reg, 26, 31) << 26) | 0x3ffffff;
841 static u64 haswell_get_tohm(struct sbridge_pvt *pvt)
846 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_0, ®);
847 rc = GET_BITFIELD(reg, 26, 31);
848 pci_read_config_dword(pvt->info.pci_vtd, HASWELL_TOHM_1, ®);
849 rc = ((reg << 6) | rc) << 26;
851 return rc | 0x1ffffff;
854 static u64 haswell_rir_limit(u32 reg)
856 return (((u64)GET_BITFIELD(reg, 1, 11) + 1) << 29) - 1;
859 static inline u8 sad_pkg_socket(u8 pkg)
861 /* on Ivy Bridge, nodeID is SASS, where A is HA and S is node id */
862 return ((pkg >> 3) << 2) | (pkg & 0x3);
865 static inline u8 sad_pkg_ha(u8 pkg)
867 return (pkg >> 2) & 0x1;
870 /****************************************************************************
871 Memory check routines
872 ****************************************************************************/
873 static struct pci_dev *get_pdev_same_bus(u8 bus, u32 id)
875 struct pci_dev *pdev = NULL;
878 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, id, pdev);
879 if (pdev && pdev->bus->number == bus)
887 * check_if_ecc_is_active() - Checks if ECC is active
889 * @type: Memory controller type
890 * returns: 0 in case ECC is active, -ENODEV if it can't be determined or
893 static int check_if_ecc_is_active(const u8 bus, enum type type)
895 struct pci_dev *pdev = NULL;
900 id = PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA;
903 id = PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA;
906 id = PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA;
909 id = PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA;
915 pdev = get_pdev_same_bus(bus, id);
917 sbridge_printk(KERN_ERR, "Couldn't find PCI device "
918 "%04x:%04x! on bus %02d\n",
919 PCI_VENDOR_ID_INTEL, id, bus);
923 pci_read_config_dword(pdev, MCMTR, &mcmtr);
924 if (!IS_ECC_ENABLED(mcmtr)) {
925 sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
931 static int get_dimm_config(struct mem_ctl_info *mci)
933 struct sbridge_pvt *pvt = mci->pvt_info;
934 struct dimm_info *dimm;
935 unsigned i, j, banks, ranks, rows, cols, npages;
941 if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL)
942 pci_read_config_dword(pvt->pci_sad1, SAD_TARGET, ®);
944 pci_read_config_dword(pvt->pci_br0, SAD_TARGET, ®);
946 pvt->sbridge_dev->source_id = SOURCE_ID(reg);
948 pvt->sbridge_dev->node_id = pvt->info.get_node_id(pvt);
949 edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
950 pvt->sbridge_dev->mc,
951 pvt->sbridge_dev->node_id,
952 pvt->sbridge_dev->source_id);
954 pci_read_config_dword(pvt->pci_ras, RASENABLES, ®);
955 if (IS_MIRROR_ENABLED(reg)) {
956 edac_dbg(0, "Memory mirror is enabled\n");
957 pvt->is_mirrored = true;
959 edac_dbg(0, "Memory mirror is disabled\n");
960 pvt->is_mirrored = false;
963 pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
964 if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
965 edac_dbg(0, "Lockstep is enabled\n");
966 mode = EDAC_S8ECD8ED;
967 pvt->is_lockstep = true;
969 edac_dbg(0, "Lockstep is disabled\n");
970 mode = EDAC_S4ECD4ED;
971 pvt->is_lockstep = false;
973 if (IS_CLOSE_PG(pvt->info.mcmtr)) {
974 edac_dbg(0, "address map is on closed page mode\n");
975 pvt->is_close_pg = true;
977 edac_dbg(0, "address map is on open page mode\n");
978 pvt->is_close_pg = false;
981 mtype = pvt->info.get_memory_type(pvt);
982 if (mtype == MEM_RDDR3 || mtype == MEM_RDDR4)
983 edac_dbg(0, "Memory is registered\n");
984 else if (mtype == MEM_UNKNOWN)
985 edac_dbg(0, "Cannot determine memory type\n");
987 edac_dbg(0, "Memory is unregistered\n");
989 if (mtype == MEM_DDR4 || mtype == MEM_RDDR4)
994 for (i = 0; i < NUM_CHANNELS; i++) {
997 if (!pvt->pci_tad[i])
999 for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
1000 dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
1002 pci_read_config_dword(pvt->pci_tad[i],
1004 edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
1005 if (IS_DIMM_PRESENT(mtr)) {
1006 pvt->channel[i].dimms++;
1008 ranks = numrank(pvt->info.type, mtr);
1012 size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
1013 npages = MiB_TO_PAGES(size);
1015 edac_dbg(0, "mc#%d: ha %d channel %d, dimm %d, %lld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
1016 pvt->sbridge_dev->mc, i/4, i%4, j,
1018 banks, ranks, rows, cols);
1020 dimm->nr_pages = npages;
1022 dimm->dtype = pvt->info.get_width(pvt, mtr);
1023 dimm->mtype = mtype;
1024 dimm->edac_mode = mode;
1025 snprintf(dimm->label, sizeof(dimm->label),
1026 "CPU_SrcID#%u_Ha#%u_Chan#%u_DIMM#%u",
1027 pvt->sbridge_dev->source_id, i/4, i%4, j);
1035 static void get_memory_layout(const struct mem_ctl_info *mci)
1037 struct sbridge_pvt *pvt = mci->pvt_info;
1038 int i, j, k, n_sads, n_tads, sad_interl;
1046 * Step 1) Get TOLM/TOHM ranges
1049 pvt->tolm = pvt->info.get_tolm(pvt);
1050 tmp_mb = (1 + pvt->tolm) >> 20;
1052 gb = div_u64_rem(tmp_mb, 1024, &mb);
1053 edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n",
1054 gb, (mb*1000)/1024, (u64)pvt->tolm);
1056 /* Address range is already 45:25 */
1057 pvt->tohm = pvt->info.get_tohm(pvt);
1058 tmp_mb = (1 + pvt->tohm) >> 20;
1060 gb = div_u64_rem(tmp_mb, 1024, &mb);
1061 edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n",
1062 gb, (mb*1000)/1024, (u64)pvt->tohm);
1065 * Step 2) Get SAD range and SAD Interleave list
1066 * TAD registers contain the interleave wayness. However, it
1067 * seems simpler to just discover it indirectly, with the
1071 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1072 /* SAD_LIMIT Address range is 45:26 */
1073 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
1075 limit = SAD_LIMIT(reg);
1077 if (!DRAM_RULE_ENABLE(reg))
1083 tmp_mb = (limit + 1) >> 20;
1084 gb = div_u64_rem(tmp_mb, 1024, &mb);
1085 edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
1089 ((u64)tmp_mb) << 20L,
1090 INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
1094 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
1096 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1097 for (j = 0; j < 8; j++) {
1098 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, j);
1099 if (j > 0 && sad_interl == pkg)
1102 edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
1108 * Step 3) Get TAD range
1111 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
1112 pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
1114 limit = TAD_LIMIT(reg);
1117 tmp_mb = (limit + 1) >> 20;
1119 gb = div_u64_rem(tmp_mb, 1024, &mb);
1120 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",
1121 n_tads, gb, (mb*1000)/1024,
1122 ((u64)tmp_mb) << 20L,
1123 (u32)(1 << TAD_SOCK(reg)),
1124 (u32)TAD_CH(reg) + 1,
1134 * Step 4) Get TAD offsets, per each channel
1136 for (i = 0; i < NUM_CHANNELS; i++) {
1137 if (!pvt->channel[i].dimms)
1139 for (j = 0; j < n_tads; j++) {
1140 pci_read_config_dword(pvt->pci_tad[i],
1141 tad_ch_nilv_offset[j],
1143 tmp_mb = TAD_OFFSET(reg) >> 20;
1144 gb = div_u64_rem(tmp_mb, 1024, &mb);
1145 edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
1148 ((u64)tmp_mb) << 20L,
1154 * Step 6) Get RIR Wayness/Limit, per each channel
1156 for (i = 0; i < NUM_CHANNELS; i++) {
1157 if (!pvt->channel[i].dimms)
1159 for (j = 0; j < MAX_RIR_RANGES; j++) {
1160 pci_read_config_dword(pvt->pci_tad[i],
1164 if (!IS_RIR_VALID(reg))
1167 tmp_mb = pvt->info.rir_limit(reg) >> 20;
1168 rir_way = 1 << RIR_WAY(reg);
1169 gb = div_u64_rem(tmp_mb, 1024, &mb);
1170 edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
1173 ((u64)tmp_mb) << 20L,
1177 for (k = 0; k < rir_way; k++) {
1178 pci_read_config_dword(pvt->pci_tad[i],
1181 tmp_mb = RIR_OFFSET(pvt->info.type, reg) << 6;
1183 gb = div_u64_rem(tmp_mb, 1024, &mb);
1184 edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
1187 ((u64)tmp_mb) << 20L,
1188 (u32)RIR_RNK_TGT(pvt->info.type, reg),
1195 static struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
1197 struct sbridge_dev *sbridge_dev;
1199 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
1200 if (sbridge_dev->node_id == node_id)
1201 return sbridge_dev->mci;
1206 static int get_memory_error_data(struct mem_ctl_info *mci,
1211 char **area_type, char *msg)
1213 struct mem_ctl_info *new_mci;
1214 struct sbridge_pvt *pvt = mci->pvt_info;
1215 struct pci_dev *pci_ha;
1216 int n_rir, n_sads, n_tads, sad_way, sck_xch;
1217 int sad_interl, idx, base_ch;
1218 int interleave_mode, shiftup = 0;
1219 unsigned sad_interleave[pvt->info.max_interleave];
1221 u8 ch_way, sck_way, pkg, sad_ha = 0, ch_add = 0;
1225 u64 ch_addr, offset, limit = 0, prv = 0;
1229 * Step 0) Check if the address is at special memory ranges
1230 * The check bellow is probably enough to fill all cases where
1231 * the error is not inside a memory, except for the legacy
1232 * range (e. g. VGA addresses). It is unlikely, however, that the
1233 * memory controller would generate an error on that range.
1235 if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
1236 sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
1239 if (addr >= (u64)pvt->tohm) {
1240 sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
1245 * Step 1) Get socket
1247 for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
1248 pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
1251 if (!DRAM_RULE_ENABLE(reg))
1254 limit = SAD_LIMIT(reg);
1256 sprintf(msg, "Can't discover the memory socket");
1263 if (n_sads == pvt->info.max_sad) {
1264 sprintf(msg, "Can't discover the memory socket");
1268 *area_type = get_dram_attr(dram_rule);
1269 interleave_mode = INTERLEAVE_MODE(dram_rule);
1271 pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
1274 if (pvt->info.type == SANDY_BRIDGE) {
1275 sad_interl = sad_pkg(pvt->info.interleave_pkg, reg, 0);
1276 for (sad_way = 0; sad_way < 8; sad_way++) {
1277 u32 pkg = sad_pkg(pvt->info.interleave_pkg, reg, sad_way);
1278 if (sad_way > 0 && sad_interl == pkg)
1280 sad_interleave[sad_way] = pkg;
1281 edac_dbg(0, "SAD interleave #%d: %d\n",
1282 sad_way, sad_interleave[sad_way]);
1284 edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
1285 pvt->sbridge_dev->mc,
1290 !interleave_mode ? "" : "XOR[18:16]");
1291 if (interleave_mode)
1292 idx = ((addr >> 6) ^ (addr >> 16)) & 7;
1294 idx = (addr >> 6) & 7;
1308 sprintf(msg, "Can't discover socket interleave");
1311 *socket = sad_interleave[idx];
1312 edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
1313 idx, sad_way, *socket);
1314 } else if (pvt->info.type == HASWELL || pvt->info.type == BROADWELL) {
1315 int bits, a7mode = A7MODE(dram_rule);
1318 /* A7 mode swaps P9 with P6 */
1319 bits = GET_BITFIELD(addr, 7, 8) << 1;
1320 bits |= GET_BITFIELD(addr, 9, 9);
1322 bits = GET_BITFIELD(addr, 6, 8);
1324 if (interleave_mode == 0) {
1325 /* interleave mode will XOR {8,7,6} with {18,17,16} */
1326 idx = GET_BITFIELD(addr, 16, 18);
1331 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
1332 *socket = sad_pkg_socket(pkg);
1333 sad_ha = sad_pkg_ha(pkg);
1338 /* MCChanShiftUpEnable */
1339 pci_read_config_dword(pvt->pci_ha0,
1340 HASWELL_HASYSDEFEATURE2, ®);
1341 shiftup = GET_BITFIELD(reg, 22, 22);
1344 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %i, shiftup: %i\n",
1345 idx, *socket, sad_ha, shiftup);
1347 /* Ivy Bridge's SAD mode doesn't support XOR interleave mode */
1348 idx = (addr >> 6) & 7;
1349 pkg = sad_pkg(pvt->info.interleave_pkg, reg, idx);
1350 *socket = sad_pkg_socket(pkg);
1351 sad_ha = sad_pkg_ha(pkg);
1354 edac_dbg(0, "SAD interleave package: %d = CPU socket %d, HA %d\n",
1355 idx, *socket, sad_ha);
1361 * Move to the proper node structure, in order to access the
1362 * right PCI registers
1364 new_mci = get_mci_for_node_id(*socket);
1366 sprintf(msg, "Struct for socket #%u wasn't initialized",
1371 pvt = mci->pvt_info;
1374 * Step 2) Get memory channel
1377 if (pvt->info.type == SANDY_BRIDGE)
1378 pci_ha = pvt->pci_ha0;
1381 pci_ha = pvt->pci_ha1;
1383 pci_ha = pvt->pci_ha0;
1385 for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
1386 pci_read_config_dword(pci_ha, tad_dram_rule[n_tads], ®);
1387 limit = TAD_LIMIT(reg);
1389 sprintf(msg, "Can't discover the memory channel");
1396 if (n_tads == MAX_TAD) {
1397 sprintf(msg, "Can't discover the memory channel");
1401 ch_way = TAD_CH(reg) + 1;
1402 sck_way = TAD_SOCK(reg);
1407 idx = (addr >> (6 + sck_way + shiftup)) & 0x3;
1411 * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
1415 base_ch = TAD_TGT0(reg);
1418 base_ch = TAD_TGT1(reg);
1421 base_ch = TAD_TGT2(reg);
1424 base_ch = TAD_TGT3(reg);
1427 sprintf(msg, "Can't discover the TAD target");
1430 *channel_mask = 1 << base_ch;
1432 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
1433 tad_ch_nilv_offset[n_tads],
1436 if (pvt->is_mirrored) {
1437 *channel_mask |= 1 << ((base_ch + 2) % 4);
1441 sck_xch = (1 << sck_way) * (ch_way >> 1);
1444 sprintf(msg, "Invalid mirror set. Can't decode addr");
1448 sck_xch = (1 << sck_way) * ch_way;
1450 if (pvt->is_lockstep)
1451 *channel_mask |= 1 << ((base_ch + 1) % 4);
1453 offset = TAD_OFFSET(tad_offset);
1455 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",
1466 /* Calculate channel address */
1467 /* Remove the TAD offset */
1469 if (offset > addr) {
1470 sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
1475 ch_addr = addr - offset;
1476 ch_addr >>= (6 + shiftup);
1478 ch_addr <<= (6 + shiftup);
1479 ch_addr |= addr & ((1 << (6 + shiftup)) - 1);
1482 * Step 3) Decode rank
1484 for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
1485 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
1486 rir_way_limit[n_rir],
1489 if (!IS_RIR_VALID(reg))
1492 limit = pvt->info.rir_limit(reg);
1493 gb = div_u64_rem(limit >> 20, 1024, &mb);
1494 edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
1499 if (ch_addr <= limit)
1502 if (n_rir == MAX_RIR_RANGES) {
1503 sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
1507 rir_way = RIR_WAY(reg);
1509 if (pvt->is_close_pg)
1510 idx = (ch_addr >> 6);
1512 idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
1513 idx %= 1 << rir_way;
1515 pci_read_config_dword(pvt->pci_tad[ch_add + base_ch],
1516 rir_offset[n_rir][idx],
1518 *rank = RIR_RNK_TGT(pvt->info.type, reg);
1520 edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
1530 /****************************************************************************
1531 Device initialization routines: put/get, init/exit
1532 ****************************************************************************/
1535 * sbridge_put_all_devices 'put' all the devices that we have
1536 * reserved via 'get'
1538 static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
1543 for (i = 0; i < sbridge_dev->n_devs; i++) {
1544 struct pci_dev *pdev = sbridge_dev->pdev[i];
1547 edac_dbg(0, "Removing dev %02x:%02x.%d\n",
1549 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1554 static void sbridge_put_all_devices(void)
1556 struct sbridge_dev *sbridge_dev, *tmp;
1558 list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
1559 sbridge_put_devices(sbridge_dev);
1560 free_sbridge_dev(sbridge_dev);
1564 static int sbridge_get_onedevice(struct pci_dev **prev,
1566 const struct pci_id_table *table,
1567 const unsigned devno)
1569 struct sbridge_dev *sbridge_dev;
1570 const struct pci_id_descr *dev_descr = &table->descr[devno];
1571 struct pci_dev *pdev = NULL;
1574 sbridge_printk(KERN_DEBUG,
1575 "Seeking for: PCI ID %04x:%04x\n",
1576 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1578 pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1579 dev_descr->dev_id, *prev);
1587 if (dev_descr->optional)
1590 /* if the HA wasn't found */
1594 sbridge_printk(KERN_INFO,
1595 "Device not found: %04x:%04x\n",
1596 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1598 /* End of list, leave */
1601 bus = pdev->bus->number;
1603 sbridge_dev = get_sbridge_dev(bus);
1605 sbridge_dev = alloc_sbridge_dev(bus, table);
1613 if (sbridge_dev->pdev[devno]) {
1614 sbridge_printk(KERN_ERR,
1615 "Duplicated device for %04x:%04x\n",
1616 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1621 sbridge_dev->pdev[devno] = pdev;
1623 /* Be sure that the device is enabled */
1624 if (unlikely(pci_enable_device(pdev) < 0)) {
1625 sbridge_printk(KERN_ERR,
1626 "Couldn't enable %04x:%04x\n",
1627 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1631 edac_dbg(0, "Detected %04x:%04x\n",
1632 PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
1635 * As stated on drivers/pci/search.c, the reference count for
1636 * @from is always decremented if it is not %NULL. So, as we need
1637 * to get all devices up to null, we need to do a get for the device
1647 * sbridge_get_all_devices - Find and perform 'get' operation on the MCH's
1648 * devices we want to reference for this driver.
1649 * @num_mc: pointer to the memory controllers count, to be incremented in case
1651 * @table: model specific table
1653 * returns 0 in case of success or error code
1655 static int sbridge_get_all_devices(u8 *num_mc,
1656 const struct pci_id_table *table)
1659 struct pci_dev *pdev = NULL;
1661 while (table && table->descr) {
1662 for (i = 0; i < table->n_devs; i++) {
1665 rc = sbridge_get_onedevice(&pdev, num_mc,
1672 sbridge_put_all_devices();
1683 static int sbridge_mci_bind_devs(struct mem_ctl_info *mci,
1684 struct sbridge_dev *sbridge_dev)
1686 struct sbridge_pvt *pvt = mci->pvt_info;
1687 struct pci_dev *pdev;
1688 u8 saw_chan_mask = 0;
1691 for (i = 0; i < sbridge_dev->n_devs; i++) {
1692 pdev = sbridge_dev->pdev[i];
1696 switch (pdev->device) {
1697 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0:
1698 pvt->pci_sad0 = pdev;
1700 case PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1:
1701 pvt->pci_sad1 = pdev;
1703 case PCI_DEVICE_ID_INTEL_SBRIDGE_BR:
1704 pvt->pci_br0 = pdev;
1706 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
1707 pvt->pci_ha0 = pdev;
1709 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA:
1712 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS:
1713 pvt->pci_ras = pdev;
1715 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0:
1716 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1:
1717 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2:
1718 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3:
1720 int id = pdev->device - PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0;
1721 pvt->pci_tad[id] = pdev;
1722 saw_chan_mask |= 1 << id;
1725 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO:
1726 pvt->pci_ddrio = pdev;
1732 edac_dbg(0, "Associated PCI %02x:%02x, bus %d with dev = %p\n",
1733 pdev->vendor, pdev->device,
1738 /* Check if everything were registered */
1739 if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
1740 !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
1743 if (saw_chan_mask != 0x0f)
1748 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1752 sbridge_printk(KERN_ERR, "Unexpected device %02x:%02x\n",
1753 PCI_VENDOR_ID_INTEL, pdev->device);
1757 static int ibridge_mci_bind_devs(struct mem_ctl_info *mci,
1758 struct sbridge_dev *sbridge_dev)
1760 struct sbridge_pvt *pvt = mci->pvt_info;
1761 struct pci_dev *pdev;
1762 u8 saw_chan_mask = 0;
1765 for (i = 0; i < sbridge_dev->n_devs; i++) {
1766 pdev = sbridge_dev->pdev[i];
1770 switch (pdev->device) {
1771 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0:
1772 pvt->pci_ha0 = pdev;
1774 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
1777 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_RAS:
1778 pvt->pci_ras = pdev;
1780 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0:
1781 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD1:
1782 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD2:
1783 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD3:
1785 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TAD0;
1786 pvt->pci_tad[id] = pdev;
1787 saw_chan_mask |= 1 << id;
1790 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_2HA_DDRIO0:
1791 pvt->pci_ddrio = pdev;
1793 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_1HA_DDRIO0:
1794 pvt->pci_ddrio = pdev;
1796 case PCI_DEVICE_ID_INTEL_IBRIDGE_SAD:
1797 pvt->pci_sad0 = pdev;
1799 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR0:
1800 pvt->pci_br0 = pdev;
1802 case PCI_DEVICE_ID_INTEL_IBRIDGE_BR1:
1803 pvt->pci_br1 = pdev;
1805 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1:
1806 pvt->pci_ha1 = pdev;
1808 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0:
1809 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD1:
1810 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD2:
1811 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD3:
1813 int id = pdev->device - PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA1_TAD0 + 4;
1814 pvt->pci_tad[id] = pdev;
1815 saw_chan_mask |= 1 << id;
1822 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1824 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1828 /* Check if everything were registered */
1829 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_br0 ||
1830 !pvt->pci_br1 || !pvt->pci_tad || !pvt->pci_ras ||
1834 if (saw_chan_mask != 0x0f && /* -EN */
1835 saw_chan_mask != 0x33 && /* -EP */
1836 saw_chan_mask != 0xff) /* -EX */
1841 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1845 sbridge_printk(KERN_ERR,
1846 "Unexpected device %02x:%02x\n", PCI_VENDOR_ID_INTEL,
1851 static int haswell_mci_bind_devs(struct mem_ctl_info *mci,
1852 struct sbridge_dev *sbridge_dev)
1854 struct sbridge_pvt *pvt = mci->pvt_info;
1855 struct pci_dev *pdev;
1856 u8 saw_chan_mask = 0;
1859 /* there's only one device per system; not tied to any bus */
1860 if (pvt->info.pci_vtd == NULL)
1861 /* result will be checked later */
1862 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
1863 PCI_DEVICE_ID_INTEL_HASWELL_IMC_VTD_MISC,
1866 for (i = 0; i < sbridge_dev->n_devs; i++) {
1867 pdev = sbridge_dev->pdev[i];
1871 switch (pdev->device) {
1872 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD0:
1873 pvt->pci_sad0 = pdev;
1875 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_CBO_SAD1:
1876 pvt->pci_sad1 = pdev;
1878 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
1879 pvt->pci_ha0 = pdev;
1881 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TA:
1884 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_THERMAL:
1885 pvt->pci_ras = pdev;
1887 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0:
1888 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD1:
1889 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD2:
1890 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD3:
1892 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0_TAD0;
1894 pvt->pci_tad[id] = pdev;
1895 saw_chan_mask |= 1 << id;
1898 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0:
1899 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD1:
1900 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD2:
1901 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD3:
1903 int id = pdev->device - PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TAD0 + 4;
1905 pvt->pci_tad[id] = pdev;
1906 saw_chan_mask |= 1 << id;
1909 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO0:
1910 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO1:
1911 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO2:
1912 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_DDRIO3:
1913 if (!pvt->pci_ddrio)
1914 pvt->pci_ddrio = pdev;
1916 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1:
1917 pvt->pci_ha1 = pdev;
1919 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA1_TA:
1920 pvt->pci_ha1_ta = pdev;
1926 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
1928 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
1932 /* Check if everything were registered */
1933 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
1934 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
1937 if (saw_chan_mask != 0x0f && /* -EN */
1938 saw_chan_mask != 0x33 && /* -EP */
1939 saw_chan_mask != 0xff) /* -EX */
1944 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
1948 static int broadwell_mci_bind_devs(struct mem_ctl_info *mci,
1949 struct sbridge_dev *sbridge_dev)
1951 struct sbridge_pvt *pvt = mci->pvt_info;
1952 struct pci_dev *pdev;
1953 u8 saw_chan_mask = 0;
1956 /* there's only one device per system; not tied to any bus */
1957 if (pvt->info.pci_vtd == NULL)
1958 /* result will be checked later */
1959 pvt->info.pci_vtd = pci_get_device(PCI_VENDOR_ID_INTEL,
1960 PCI_DEVICE_ID_INTEL_BROADWELL_IMC_VTD_MISC,
1963 for (i = 0; i < sbridge_dev->n_devs; i++) {
1964 pdev = sbridge_dev->pdev[i];
1968 switch (pdev->device) {
1969 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD0:
1970 pvt->pci_sad0 = pdev;
1972 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_CBO_SAD1:
1973 pvt->pci_sad1 = pdev;
1975 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
1976 pvt->pci_ha0 = pdev;
1978 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TA:
1981 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_THERMAL:
1982 pvt->pci_ras = pdev;
1984 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0:
1985 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD1:
1986 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD2:
1987 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD3:
1989 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0_TAD0;
1990 pvt->pci_tad[id] = pdev;
1991 saw_chan_mask |= 1 << id;
1994 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0:
1995 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD1:
1996 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD2:
1997 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD3:
1999 int id = pdev->device - PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TAD0 + 4;
2000 pvt->pci_tad[id] = pdev;
2001 saw_chan_mask |= 1 << id;
2004 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_DDRIO0:
2005 pvt->pci_ddrio = pdev;
2007 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1:
2008 pvt->pci_ha1 = pdev;
2010 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA1_TA:
2011 pvt->pci_ha1_ta = pdev;
2017 edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
2019 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2023 /* Check if everything were registered */
2024 if (!pvt->pci_sad0 || !pvt->pci_ha0 || !pvt->pci_sad1 ||
2025 !pvt->pci_ras || !pvt->pci_ta || !pvt->info.pci_vtd)
2028 if (saw_chan_mask != 0x0f && /* -EN */
2029 saw_chan_mask != 0x33 && /* -EP */
2030 saw_chan_mask != 0xff) /* -EX */
2035 sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
2039 /****************************************************************************
2040 Error check routines
2041 ****************************************************************************/
2044 * While Sandy Bridge has error count registers, SMI BIOS read values from
2045 * and resets the counters. So, they are not reliable for the OS to read
2046 * from them. So, we have no option but to just trust on whatever MCE is
2047 * telling us about the errors.
2049 static void sbridge_mce_output_error(struct mem_ctl_info *mci,
2050 const struct mce *m)
2052 struct mem_ctl_info *new_mci;
2053 struct sbridge_pvt *pvt = mci->pvt_info;
2054 enum hw_event_mc_err_type tp_event;
2055 char *type, *optype, msg[256];
2056 bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
2057 bool overflow = GET_BITFIELD(m->status, 62, 62);
2058 bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
2060 u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
2061 u32 mscod = GET_BITFIELD(m->status, 16, 31);
2062 u32 errcode = GET_BITFIELD(m->status, 0, 15);
2063 u32 channel = GET_BITFIELD(m->status, 0, 3);
2064 u32 optypenum = GET_BITFIELD(m->status, 4, 6);
2065 long channel_mask, first_channel;
2066 u8 rank, socket, ha;
2068 char *area_type = NULL;
2070 if (pvt->info.type != SANDY_BRIDGE)
2073 recoverable = GET_BITFIELD(m->status, 56, 56);
2075 if (uncorrected_error) {
2078 tp_event = HW_EVENT_ERR_FATAL;
2081 tp_event = HW_EVENT_ERR_UNCORRECTED;
2085 tp_event = HW_EVENT_ERR_CORRECTED;
2089 * According with Table 15-9 of the Intel Architecture spec vol 3A,
2090 * memory errors should fit in this mask:
2091 * 000f 0000 1mmm cccc (binary)
2093 * f = Correction Report Filtering Bit. If 1, subsequent errors
2097 * If the mask doesn't match, report an error to the parsing logic
2099 if (! ((errcode & 0xef80) == 0x80)) {
2100 optype = "Can't parse: it is not a mem";
2102 switch (optypenum) {
2104 optype = "generic undef request error";
2107 optype = "memory read error";
2110 optype = "memory write error";
2113 optype = "addr/cmd error";
2116 optype = "memory scrubbing error";
2119 optype = "reserved";
2124 /* Only decode errors with an valid address (ADDRV) */
2125 if (!GET_BITFIELD(m->status, 58, 58))
2128 rc = get_memory_error_data(mci, m->addr, &socket, &ha,
2129 &channel_mask, &rank, &area_type, msg);
2132 new_mci = get_mci_for_node_id(socket);
2134 strcpy(msg, "Error: socket got corrupted!");
2138 pvt = mci->pvt_info;
2140 first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
2151 * FIXME: On some memory configurations (mirror, lockstep), the
2152 * Memory Controller can't point the error to a single DIMM. The
2153 * EDAC core should be handling the channel mask, in order to point
2154 * to the group of dimm's where the error may be happening.
2156 if (!pvt->is_lockstep && !pvt->is_mirrored && !pvt->is_close_pg)
2157 channel = first_channel;
2159 snprintf(msg, sizeof(msg),
2160 "%s%s area:%s err_code:%04x:%04x socket:%d ha:%d channel_mask:%ld rank:%d",
2161 overflow ? " OVERFLOW" : "",
2162 (uncorrected_error && recoverable) ? " recoverable" : "",
2169 edac_dbg(0, "%s\n", msg);
2171 /* FIXME: need support for channel mask */
2173 if (channel == CHANNEL_UNSPECIFIED)
2176 /* Call the helper to output message */
2177 edac_mc_handle_error(tp_event, mci, core_err_cnt,
2178 m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
2179 4*ha+channel, dimm, -1,
2183 edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
2190 * sbridge_check_error Retrieve and process errors reported by the
2191 * hardware. Called by the Core module.
2193 static void sbridge_check_error(struct mem_ctl_info *mci)
2195 struct sbridge_pvt *pvt = mci->pvt_info;
2201 * MCE first step: Copy all mce errors into a temporary buffer
2202 * We use a double buffering here, to reduce the risk of
2206 count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
2211 m = pvt->mce_outentry;
2212 if (pvt->mce_in + count > MCE_LOG_LEN) {
2213 unsigned l = MCE_LOG_LEN - pvt->mce_in;
2215 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
2221 memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
2223 pvt->mce_in += count;
2226 if (pvt->mce_overrun) {
2227 sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
2230 pvt->mce_overrun = 0;
2234 * MCE second step: parse errors and display
2236 for (i = 0; i < count; i++)
2237 sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
2241 * sbridge_mce_check_error Replicates mcelog routine to get errors
2242 * This routine simply queues mcelog errors, and
2243 * return. The error itself should be handled later
2244 * by sbridge_check_error.
2245 * WARNING: As this routine should be called at NMI time, extra care should
2246 * be taken to avoid deadlocks, and to be as fast as possible.
2248 static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
2251 struct mce *mce = (struct mce *)data;
2252 struct mem_ctl_info *mci;
2253 struct sbridge_pvt *pvt;
2256 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
2259 mci = get_mci_for_node_id(mce->socketid);
2262 pvt = mci->pvt_info;
2265 * Just let mcelog handle it if the error is
2266 * outside the memory controller. A memory error
2267 * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
2268 * bit 12 has an special meaning.
2270 if ((mce->status & 0xefff) >> 7 != 1)
2273 if (mce->mcgstatus & MCG_STATUS_MCIP)
2278 sbridge_mc_printk(mci, KERN_DEBUG, "HANDLING MCE MEMORY ERROR\n");
2280 sbridge_mc_printk(mci, KERN_DEBUG, "CPU %d: Machine Check %s: %Lx "
2281 "Bank %d: %016Lx\n", mce->extcpu, type,
2282 mce->mcgstatus, mce->bank, mce->status);
2283 sbridge_mc_printk(mci, KERN_DEBUG, "TSC %llx ", mce->tsc);
2284 sbridge_mc_printk(mci, KERN_DEBUG, "ADDR %llx ", mce->addr);
2285 sbridge_mc_printk(mci, KERN_DEBUG, "MISC %llx ", mce->misc);
2287 sbridge_mc_printk(mci, KERN_DEBUG, "PROCESSOR %u:%x TIME %llu SOCKET "
2288 "%u APIC %x\n", mce->cpuvendor, mce->cpuid,
2289 mce->time, mce->socketid, mce->apicid);
2292 if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
2298 /* Copy memory error at the ringbuffer */
2299 memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
2301 pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
2303 /* Handle fatal errors immediately */
2304 if (mce->mcgstatus & 1)
2305 sbridge_check_error(mci);
2307 /* Advice mcelog that the error were handled */
2311 static struct notifier_block sbridge_mce_dec = {
2312 .notifier_call = sbridge_mce_check_error,
2315 /****************************************************************************
2316 EDAC register/unregister logic
2317 ****************************************************************************/
2319 static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
2321 struct mem_ctl_info *mci = sbridge_dev->mci;
2322 struct sbridge_pvt *pvt;
2324 if (unlikely(!mci || !mci->pvt_info)) {
2325 edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
2327 sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
2331 pvt = mci->pvt_info;
2333 edac_dbg(0, "MC: mci = %p, dev = %p\n",
2334 mci, &sbridge_dev->pdev[0]->dev);
2336 /* Remove MC sysfs nodes */
2337 edac_mc_del_mc(mci->pdev);
2339 edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
2340 kfree(mci->ctl_name);
2342 sbridge_dev->mci = NULL;
2345 static int sbridge_register_mci(struct sbridge_dev *sbridge_dev, enum type type)
2347 struct mem_ctl_info *mci;
2348 struct edac_mc_layer layers[2];
2349 struct sbridge_pvt *pvt;
2350 struct pci_dev *pdev = sbridge_dev->pdev[0];
2353 /* Check the number of active and not disabled channels */
2354 rc = check_if_ecc_is_active(sbridge_dev->bus, type);
2355 if (unlikely(rc < 0))
2358 /* allocate a new MC control structure */
2359 layers[0].type = EDAC_MC_LAYER_CHANNEL;
2360 layers[0].size = NUM_CHANNELS;
2361 layers[0].is_virt_csrow = false;
2362 layers[1].type = EDAC_MC_LAYER_SLOT;
2363 layers[1].size = MAX_DIMMS;
2364 layers[1].is_virt_csrow = true;
2365 mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
2371 edac_dbg(0, "MC: mci = %p, dev = %p\n",
2374 pvt = mci->pvt_info;
2375 memset(pvt, 0, sizeof(*pvt));
2377 /* Associate sbridge_dev and mci for future usage */
2378 pvt->sbridge_dev = sbridge_dev;
2379 sbridge_dev->mci = mci;
2381 mci->mtype_cap = MEM_FLAG_DDR3;
2382 mci->edac_ctl_cap = EDAC_FLAG_NONE;
2383 mci->edac_cap = EDAC_FLAG_NONE;
2384 mci->mod_name = "sbridge_edac.c";
2385 mci->mod_ver = SBRIDGE_REVISION;
2386 mci->dev_name = pci_name(pdev);
2387 mci->ctl_page_to_phys = NULL;
2389 /* Set the function pointer to an actual operation function */
2390 mci->edac_check = sbridge_check_error;
2392 pvt->info.type = type;
2395 pvt->info.rankcfgr = IB_RANK_CFG_A;
2396 pvt->info.get_tolm = ibridge_get_tolm;
2397 pvt->info.get_tohm = ibridge_get_tohm;
2398 pvt->info.dram_rule = ibridge_dram_rule;
2399 pvt->info.get_memory_type = get_memory_type;
2400 pvt->info.get_node_id = get_node_id;
2401 pvt->info.rir_limit = rir_limit;
2402 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2403 pvt->info.interleave_list = ibridge_interleave_list;
2404 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2405 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2406 pvt->info.get_width = ibridge_get_width;
2407 mci->ctl_name = kasprintf(GFP_KERNEL, "Ivy Bridge Socket#%d", mci->mc_idx);
2409 /* Store pci devices at mci for faster access */
2410 rc = ibridge_mci_bind_devs(mci, sbridge_dev);
2411 if (unlikely(rc < 0))
2415 pvt->info.rankcfgr = SB_RANK_CFG_A;
2416 pvt->info.get_tolm = sbridge_get_tolm;
2417 pvt->info.get_tohm = sbridge_get_tohm;
2418 pvt->info.dram_rule = sbridge_dram_rule;
2419 pvt->info.get_memory_type = get_memory_type;
2420 pvt->info.get_node_id = get_node_id;
2421 pvt->info.rir_limit = rir_limit;
2422 pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
2423 pvt->info.interleave_list = sbridge_interleave_list;
2424 pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
2425 pvt->info.interleave_pkg = sbridge_interleave_pkg;
2426 pvt->info.get_width = sbridge_get_width;
2427 mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
2429 /* Store pci devices at mci for faster access */
2430 rc = sbridge_mci_bind_devs(mci, sbridge_dev);
2431 if (unlikely(rc < 0))
2435 /* rankcfgr isn't used */
2436 pvt->info.get_tolm = haswell_get_tolm;
2437 pvt->info.get_tohm = haswell_get_tohm;
2438 pvt->info.dram_rule = ibridge_dram_rule;
2439 pvt->info.get_memory_type = haswell_get_memory_type;
2440 pvt->info.get_node_id = haswell_get_node_id;
2441 pvt->info.rir_limit = haswell_rir_limit;
2442 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2443 pvt->info.interleave_list = ibridge_interleave_list;
2444 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2445 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2446 pvt->info.get_width = ibridge_get_width;
2447 mci->ctl_name = kasprintf(GFP_KERNEL, "Haswell Socket#%d", mci->mc_idx);
2449 /* Store pci devices at mci for faster access */
2450 rc = haswell_mci_bind_devs(mci, sbridge_dev);
2451 if (unlikely(rc < 0))
2455 /* rankcfgr isn't used */
2456 pvt->info.get_tolm = haswell_get_tolm;
2457 pvt->info.get_tohm = haswell_get_tohm;
2458 pvt->info.dram_rule = ibridge_dram_rule;
2459 pvt->info.get_memory_type = haswell_get_memory_type;
2460 pvt->info.get_node_id = haswell_get_node_id;
2461 pvt->info.rir_limit = haswell_rir_limit;
2462 pvt->info.max_sad = ARRAY_SIZE(ibridge_dram_rule);
2463 pvt->info.interleave_list = ibridge_interleave_list;
2464 pvt->info.max_interleave = ARRAY_SIZE(ibridge_interleave_list);
2465 pvt->info.interleave_pkg = ibridge_interleave_pkg;
2466 pvt->info.get_width = broadwell_get_width;
2467 mci->ctl_name = kasprintf(GFP_KERNEL, "Broadwell Socket#%d", mci->mc_idx);
2469 /* Store pci devices at mci for faster access */
2470 rc = broadwell_mci_bind_devs(mci, sbridge_dev);
2471 if (unlikely(rc < 0))
2476 /* Get dimm basic config and the memory layout */
2477 get_dimm_config(mci);
2478 get_memory_layout(mci);
2480 /* record ptr to the generic device */
2481 mci->pdev = &pdev->dev;
2483 /* add this new MC control structure to EDAC's list of MCs */
2484 if (unlikely(edac_mc_add_mc(mci))) {
2485 edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
2493 kfree(mci->ctl_name);
2495 sbridge_dev->mci = NULL;
2500 * sbridge_probe Probe for ONE instance of device to see if it is
2503 * 0 for FOUND a device
2504 * < 0 for error code
2507 static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2511 struct sbridge_dev *sbridge_dev;
2512 enum type type = SANDY_BRIDGE;
2514 /* get the pci devices we want to reserve for our use */
2515 mutex_lock(&sbridge_edac_lock);
2518 * All memory controllers are allocated at the first pass.
2520 if (unlikely(probed >= 1)) {
2521 mutex_unlock(&sbridge_edac_lock);
2526 switch (pdev->device) {
2527 case PCI_DEVICE_ID_INTEL_IBRIDGE_IMC_HA0_TA:
2528 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_ibridge_table);
2531 case PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0:
2532 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_sbridge_table);
2533 type = SANDY_BRIDGE;
2535 case PCI_DEVICE_ID_INTEL_HASWELL_IMC_HA0:
2536 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_haswell_table);
2539 case PCI_DEVICE_ID_INTEL_BROADWELL_IMC_HA0:
2540 rc = sbridge_get_all_devices(&num_mc, pci_dev_descr_broadwell_table);
2544 if (unlikely(rc < 0)) {
2545 edac_dbg(0, "couldn't get all devices for 0x%x\n", pdev->device);
2551 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
2552 edac_dbg(0, "Registering MC#%d (%d of %d)\n",
2553 mc, mc + 1, num_mc);
2555 sbridge_dev->mc = mc++;
2556 rc = sbridge_register_mci(sbridge_dev, type);
2557 if (unlikely(rc < 0))
2561 sbridge_printk(KERN_INFO, "%s\n", SBRIDGE_REVISION);
2563 mutex_unlock(&sbridge_edac_lock);
2567 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2568 sbridge_unregister_mci(sbridge_dev);
2570 sbridge_put_all_devices();
2572 mutex_unlock(&sbridge_edac_lock);
2577 * sbridge_remove destructor for one instance of device
2580 static void sbridge_remove(struct pci_dev *pdev)
2582 struct sbridge_dev *sbridge_dev;
2587 * we have a trouble here: pdev value for removal will be wrong, since
2588 * it will point to the X58 register used to detect that the machine
2589 * is a Nehalem or upper design. However, due to the way several PCI
2590 * devices are grouped together to provide MC functionality, we need
2591 * to use a different method for releasing the devices
2594 mutex_lock(&sbridge_edac_lock);
2596 if (unlikely(!probed)) {
2597 mutex_unlock(&sbridge_edac_lock);
2601 list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
2602 sbridge_unregister_mci(sbridge_dev);
2604 /* Release PCI resources */
2605 sbridge_put_all_devices();
2609 mutex_unlock(&sbridge_edac_lock);
2612 MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
2615 * sbridge_driver pci_driver structure for this module
2618 static struct pci_driver sbridge_driver = {
2619 .name = "sbridge_edac",
2620 .probe = sbridge_probe,
2621 .remove = sbridge_remove,
2622 .id_table = sbridge_pci_tbl,
2626 * sbridge_init Module entry function
2627 * Try to initialize this module for its devices
2629 static int __init sbridge_init(void)
2635 /* Ensure that the OPSTATE is set correctly for POLL or NMI */
2638 pci_rc = pci_register_driver(&sbridge_driver);
2640 mce_register_decode_chain(&sbridge_mce_dec);
2641 if (get_edac_report_status() == EDAC_REPORTING_DISABLED)
2642 sbridge_printk(KERN_WARNING, "Loading driver, error reporting disabled.\n");
2646 sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
2653 * sbridge_exit() Module exit function
2654 * Unregister the driver
2656 static void __exit sbridge_exit(void)
2659 pci_unregister_driver(&sbridge_driver);
2660 mce_unregister_decode_chain(&sbridge_mce_dec);
2663 module_init(sbridge_init);
2664 module_exit(sbridge_exit);
2666 module_param(edac_op_state, int, 0444);
2667 MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
2669 MODULE_LICENSE("GPL");
2670 MODULE_AUTHOR("Mauro Carvalho Chehab");
2671 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
2672 MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge and Ivy Bridge memory controllers - "